tcg-s390: Icache flush is a no-op.
[qemu/mdroth.git] / hw / virtio-blk.c
blob5d7f1a22008d4b5e05e7bbaa9feb33dd6b87745f
1 /*
2 * Virtio Block Device
4 * Copyright IBM, Corp. 2007
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include <qemu-common.h>
15 #include <sysemu.h>
16 #include "virtio-blk.h"
17 #include "block_int.h"
18 #ifdef __linux__
19 # include <scsi/sg.h>
20 #endif
22 typedef struct VirtIOBlock
24 VirtIODevice vdev;
25 BlockDriverState *bs;
26 VirtQueue *vq;
27 void *rq;
28 QEMUBH *bh;
29 BlockConf *conf;
30 unsigned short sector_mask;
31 } VirtIOBlock;
33 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
35 return (VirtIOBlock *)vdev;
38 typedef struct VirtIOBlockReq
40 VirtIOBlock *dev;
41 VirtQueueElement elem;
42 struct virtio_blk_inhdr *in;
43 struct virtio_blk_outhdr *out;
44 struct virtio_scsi_inhdr *scsi;
45 QEMUIOVector qiov;
46 struct VirtIOBlockReq *next;
47 } VirtIOBlockReq;
49 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
51 VirtIOBlock *s = req->dev;
53 req->in->status = status;
54 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
55 virtio_notify(&s->vdev, s->vq);
57 qemu_free(req);
60 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
61 int is_read)
63 BlockInterfaceErrorAction action =
64 drive_get_on_error(req->dev->bs, is_read);
65 VirtIOBlock *s = req->dev;
67 if (action == BLOCK_ERR_IGNORE) {
68 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
69 return 0;
72 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
73 || action == BLOCK_ERR_STOP_ANY) {
74 req->next = s->rq;
75 s->rq = req;
76 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
77 vm_stop(0);
78 } else {
79 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
80 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
83 return 1;
86 static void virtio_blk_rw_complete(void *opaque, int ret)
88 VirtIOBlockReq *req = opaque;
90 if (ret) {
91 int is_read = !(req->out->type & VIRTIO_BLK_T_OUT);
92 if (virtio_blk_handle_rw_error(req, -ret, is_read))
93 return;
96 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
99 static void virtio_blk_flush_complete(void *opaque, int ret)
101 VirtIOBlockReq *req = opaque;
103 virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK);
106 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
108 VirtIOBlockReq *req = qemu_malloc(sizeof(*req));
109 req->dev = s;
110 req->qiov.size = 0;
111 req->next = NULL;
112 return req;
115 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
117 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
119 if (req != NULL) {
120 if (!virtqueue_pop(s->vq, &req->elem)) {
121 qemu_free(req);
122 return NULL;
126 return req;
129 #ifdef __linux__
130 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
132 struct sg_io_hdr hdr;
133 int ret;
134 int status;
135 int i;
138 * We require at least one output segment each for the virtio_blk_outhdr
139 * and the SCSI command block.
141 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
142 * and the sense buffer pointer in the input segments.
144 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
145 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
146 return;
150 * No support for bidirection commands yet.
152 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
153 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
154 return;
158 * The scsi inhdr is placed in the second-to-last input segment, just
159 * before the regular inhdr.
161 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
163 memset(&hdr, 0, sizeof(struct sg_io_hdr));
164 hdr.interface_id = 'S';
165 hdr.cmd_len = req->elem.out_sg[1].iov_len;
166 hdr.cmdp = req->elem.out_sg[1].iov_base;
167 hdr.dxfer_len = 0;
169 if (req->elem.out_num > 2) {
171 * If there are more than the minimally required 2 output segments
172 * there is write payload starting from the third iovec.
174 hdr.dxfer_direction = SG_DXFER_TO_DEV;
175 hdr.iovec_count = req->elem.out_num - 2;
177 for (i = 0; i < hdr.iovec_count; i++)
178 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
180 hdr.dxferp = req->elem.out_sg + 2;
182 } else if (req->elem.in_num > 3) {
184 * If we have more than 3 input segments the guest wants to actually
185 * read data.
187 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
188 hdr.iovec_count = req->elem.in_num - 3;
189 for (i = 0; i < hdr.iovec_count; i++)
190 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
192 hdr.dxferp = req->elem.in_sg;
193 } else {
195 * Some SCSI commands don't actually transfer any data.
197 hdr.dxfer_direction = SG_DXFER_NONE;
200 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
201 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
203 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
204 if (ret) {
205 status = VIRTIO_BLK_S_UNSUPP;
206 hdr.status = ret;
207 hdr.resid = hdr.dxfer_len;
208 } else if (hdr.status) {
209 status = VIRTIO_BLK_S_IOERR;
210 } else {
211 status = VIRTIO_BLK_S_OK;
214 req->scsi->errors = hdr.status;
215 req->scsi->residual = hdr.resid;
216 req->scsi->sense_len = hdr.sb_len_wr;
217 req->scsi->data_len = hdr.dxfer_len;
219 virtio_blk_req_complete(req, status);
221 #else
222 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
224 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
226 #endif /* __linux__ */
228 static void do_multiwrite(BlockDriverState *bs, BlockRequest *blkreq,
229 int num_writes)
231 int i, ret;
232 ret = bdrv_aio_multiwrite(bs, blkreq, num_writes);
234 if (ret != 0) {
235 for (i = 0; i < num_writes; i++) {
236 if (blkreq[i].error) {
237 virtio_blk_rw_complete(blkreq[i].opaque, -EIO);
243 static void virtio_blk_handle_flush(BlockRequest *blkreq, int *num_writes,
244 VirtIOBlockReq *req, BlockDriverState **old_bs)
246 BlockDriverAIOCB *acb;
249 * Make sure all outstanding writes are posted to the backing device.
251 if (*old_bs != NULL) {
252 do_multiwrite(*old_bs, blkreq, *num_writes);
254 *num_writes = 0;
255 *old_bs = req->dev->bs;
257 acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
258 if (!acb) {
259 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
263 static void virtio_blk_handle_write(BlockRequest *blkreq, int *num_writes,
264 VirtIOBlockReq *req, BlockDriverState **old_bs)
266 if (req->out->sector & req->dev->sector_mask) {
267 virtio_blk_rw_complete(req, -EIO);
268 return;
271 if (req->dev->bs != *old_bs || *num_writes == 32) {
272 if (*old_bs != NULL) {
273 do_multiwrite(*old_bs, blkreq, *num_writes);
275 *num_writes = 0;
276 *old_bs = req->dev->bs;
279 blkreq[*num_writes].sector = req->out->sector;
280 blkreq[*num_writes].nb_sectors = req->qiov.size / 512;
281 blkreq[*num_writes].qiov = &req->qiov;
282 blkreq[*num_writes].cb = virtio_blk_rw_complete;
283 blkreq[*num_writes].opaque = req;
284 blkreq[*num_writes].error = 0;
286 (*num_writes)++;
289 static void virtio_blk_handle_read(VirtIOBlockReq *req)
291 BlockDriverAIOCB *acb;
293 if (req->out->sector & req->dev->sector_mask) {
294 virtio_blk_rw_complete(req, -EIO);
295 return;
298 acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
299 req->qiov.size / 512, virtio_blk_rw_complete, req);
300 if (!acb) {
301 virtio_blk_rw_complete(req, -EIO);
305 typedef struct MultiReqBuffer {
306 BlockRequest blkreq[32];
307 int num_writes;
308 BlockDriverState *old_bs;
309 } MultiReqBuffer;
311 static void virtio_blk_handle_request(VirtIOBlockReq *req,
312 MultiReqBuffer *mrb)
314 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
315 fprintf(stderr, "virtio-blk missing headers\n");
316 exit(1);
319 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
320 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
321 fprintf(stderr, "virtio-blk header not in correct element\n");
322 exit(1);
325 req->out = (void *)req->elem.out_sg[0].iov_base;
326 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
328 if (req->out->type & VIRTIO_BLK_T_FLUSH) {
329 virtio_blk_handle_flush(mrb->blkreq, &mrb->num_writes,
330 req, &mrb->old_bs);
331 } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
332 virtio_blk_handle_scsi(req);
333 } else if (req->out->type & VIRTIO_BLK_T_OUT) {
334 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
335 req->elem.out_num - 1);
336 virtio_blk_handle_write(mrb->blkreq, &mrb->num_writes,
337 req, &mrb->old_bs);
338 } else {
339 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
340 req->elem.in_num - 1);
341 virtio_blk_handle_read(req);
345 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
347 VirtIOBlock *s = to_virtio_blk(vdev);
348 VirtIOBlockReq *req;
349 MultiReqBuffer mrb = {
350 .num_writes = 0,
351 .old_bs = NULL,
354 while ((req = virtio_blk_get_request(s))) {
355 virtio_blk_handle_request(req, &mrb);
358 if (mrb.num_writes > 0) {
359 do_multiwrite(mrb.old_bs, mrb.blkreq, mrb.num_writes);
363 * FIXME: Want to check for completions before returning to guest mode,
364 * so cached reads and writes are reported as quickly as possible. But
365 * that should be done in the generic block layer.
369 static void virtio_blk_dma_restart_bh(void *opaque)
371 VirtIOBlock *s = opaque;
372 VirtIOBlockReq *req = s->rq;
373 MultiReqBuffer mrb = {
374 .num_writes = 0,
375 .old_bs = NULL,
378 qemu_bh_delete(s->bh);
379 s->bh = NULL;
381 s->rq = NULL;
383 while (req) {
384 virtio_blk_handle_request(req, &mrb);
385 req = req->next;
388 if (mrb.num_writes > 0) {
389 do_multiwrite(mrb.old_bs, mrb.blkreq, mrb.num_writes);
393 static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
395 VirtIOBlock *s = opaque;
397 if (!running)
398 return;
400 if (!s->bh) {
401 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
402 qemu_bh_schedule(s->bh);
406 static void virtio_blk_reset(VirtIODevice *vdev)
409 * This should cancel pending requests, but can't do nicely until there
410 * are per-device request lists.
412 qemu_aio_flush();
415 /* coalesce internal state, copy to pci i/o region 0
417 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
419 VirtIOBlock *s = to_virtio_blk(vdev);
420 struct virtio_blk_config blkcfg;
421 uint64_t capacity;
422 int cylinders, heads, secs;
424 bdrv_get_geometry(s->bs, &capacity);
425 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
426 memset(&blkcfg, 0, sizeof(blkcfg));
427 stq_raw(&blkcfg.capacity, capacity);
428 stl_raw(&blkcfg.seg_max, 128 - 2);
429 stw_raw(&blkcfg.cylinders, cylinders);
430 blkcfg.heads = heads;
431 blkcfg.sectors = secs & ~s->sector_mask;
432 blkcfg.blk_size = s->conf->logical_block_size;
433 blkcfg.size_max = 0;
434 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
435 blkcfg.alignment_offset = 0;
436 blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size;
437 blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size;
438 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
441 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
443 VirtIOBlock *s = to_virtio_blk(vdev);
445 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
446 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
447 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
448 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
450 if (bdrv_enable_write_cache(s->bs))
451 features |= (1 << VIRTIO_BLK_F_WCACHE);
453 if (bdrv_is_read_only(s->bs))
454 features |= 1 << VIRTIO_BLK_F_RO;
456 return features;
459 static void virtio_blk_save(QEMUFile *f, void *opaque)
461 VirtIOBlock *s = opaque;
462 VirtIOBlockReq *req = s->rq;
464 virtio_save(&s->vdev, f);
466 while (req) {
467 qemu_put_sbyte(f, 1);
468 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
469 req = req->next;
471 qemu_put_sbyte(f, 0);
474 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
476 VirtIOBlock *s = opaque;
478 if (version_id != 2)
479 return -EINVAL;
481 virtio_load(&s->vdev, f);
482 while (qemu_get_sbyte(f)) {
483 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
484 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
485 req->next = s->rq;
486 s->rq = req->next;
489 return 0;
492 VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf)
494 VirtIOBlock *s;
495 int cylinders, heads, secs;
496 static int virtio_blk_id;
498 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
499 sizeof(struct virtio_blk_config),
500 sizeof(VirtIOBlock));
502 s->vdev.get_config = virtio_blk_update_config;
503 s->vdev.get_features = virtio_blk_get_features;
504 s->vdev.reset = virtio_blk_reset;
505 s->bs = conf->dinfo->bdrv;
506 s->conf = conf;
507 s->rq = NULL;
508 s->sector_mask = (s->conf->logical_block_size / 512) - 1;
509 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
511 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
513 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
514 register_savevm("virtio-blk", virtio_blk_id++, 2,
515 virtio_blk_save, virtio_blk_load, s);
517 return &s->vdev;