coroutine: use AioContext for CoQueue BH
[qemu/kevin.git] / hw / virtio-blk.c
blob6b6923665519301d3baa9351610cf5024c1355ba
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 "qemu/error-report.h"
16 #include "trace.h"
17 #include "hw/block-common.h"
18 #include "sysemu/blockdev.h"
19 #include "hw/virtio-blk.h"
20 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
21 #include "dataplane/virtio-blk.h"
22 #endif
23 #include "hw/scsi-defs.h"
24 #ifdef __linux__
25 # include <scsi/sg.h>
26 #endif
28 typedef struct VirtIOBlock
30 VirtIODevice vdev;
31 BlockDriverState *bs;
32 VirtQueue *vq;
33 void *rq;
34 QEMUBH *bh;
35 BlockConf *conf;
36 VirtIOBlkConf *blk;
37 unsigned short sector_mask;
38 DeviceState *qdev;
39 VMChangeStateEntry *change;
40 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
41 VirtIOBlockDataPlane *dataplane;
42 #endif
43 } VirtIOBlock;
45 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
47 return (VirtIOBlock *)vdev;
50 typedef struct VirtIOBlockReq
52 VirtIOBlock *dev;
53 VirtQueueElement elem;
54 struct virtio_blk_inhdr *in;
55 struct virtio_blk_outhdr *out;
56 struct virtio_scsi_inhdr *scsi;
57 QEMUIOVector qiov;
58 struct VirtIOBlockReq *next;
59 BlockAcctCookie acct;
60 } VirtIOBlockReq;
62 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
64 VirtIOBlock *s = req->dev;
66 trace_virtio_blk_req_complete(req, status);
68 stb_p(&req->in->status, status);
69 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
70 virtio_notify(&s->vdev, s->vq);
73 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
74 bool is_read)
76 BlockErrorAction action = bdrv_get_error_action(req->dev->bs, is_read, error);
77 VirtIOBlock *s = req->dev;
79 if (action == BDRV_ACTION_STOP) {
80 req->next = s->rq;
81 s->rq = req;
82 } else if (action == BDRV_ACTION_REPORT) {
83 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
84 bdrv_acct_done(s->bs, &req->acct);
85 g_free(req);
88 bdrv_error_action(s->bs, action, is_read, error);
89 return action != BDRV_ACTION_IGNORE;
92 static void virtio_blk_rw_complete(void *opaque, int ret)
94 VirtIOBlockReq *req = opaque;
96 trace_virtio_blk_rw_complete(req, ret);
98 if (ret) {
99 bool is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
100 if (virtio_blk_handle_rw_error(req, -ret, is_read))
101 return;
104 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
105 bdrv_acct_done(req->dev->bs, &req->acct);
106 g_free(req);
109 static void virtio_blk_flush_complete(void *opaque, int ret)
111 VirtIOBlockReq *req = opaque;
113 if (ret) {
114 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
115 return;
119 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
120 bdrv_acct_done(req->dev->bs, &req->acct);
121 g_free(req);
124 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
126 VirtIOBlockReq *req = g_malloc(sizeof(*req));
127 req->dev = s;
128 req->qiov.size = 0;
129 req->next = NULL;
130 return req;
133 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
135 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
137 if (req != NULL) {
138 if (!virtqueue_pop(s->vq, &req->elem)) {
139 g_free(req);
140 return NULL;
144 return req;
147 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
149 #ifdef __linux__
150 int ret;
151 int i;
152 #endif
153 int status = VIRTIO_BLK_S_OK;
156 * We require at least one output segment each for the virtio_blk_outhdr
157 * and the SCSI command block.
159 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
160 * and the sense buffer pointer in the input segments.
162 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
163 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
164 g_free(req);
165 return;
169 * The scsi inhdr is placed in the second-to-last input segment, just
170 * before the regular inhdr.
172 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
174 if (!req->dev->blk->scsi) {
175 status = VIRTIO_BLK_S_UNSUPP;
176 goto fail;
180 * No support for bidirection commands yet.
182 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
183 status = VIRTIO_BLK_S_UNSUPP;
184 goto fail;
187 #ifdef __linux__
188 struct sg_io_hdr hdr;
189 memset(&hdr, 0, sizeof(struct sg_io_hdr));
190 hdr.interface_id = 'S';
191 hdr.cmd_len = req->elem.out_sg[1].iov_len;
192 hdr.cmdp = req->elem.out_sg[1].iov_base;
193 hdr.dxfer_len = 0;
195 if (req->elem.out_num > 2) {
197 * If there are more than the minimally required 2 output segments
198 * there is write payload starting from the third iovec.
200 hdr.dxfer_direction = SG_DXFER_TO_DEV;
201 hdr.iovec_count = req->elem.out_num - 2;
203 for (i = 0; i < hdr.iovec_count; i++)
204 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
206 hdr.dxferp = req->elem.out_sg + 2;
208 } else if (req->elem.in_num > 3) {
210 * If we have more than 3 input segments the guest wants to actually
211 * read data.
213 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
214 hdr.iovec_count = req->elem.in_num - 3;
215 for (i = 0; i < hdr.iovec_count; i++)
216 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
218 hdr.dxferp = req->elem.in_sg;
219 } else {
221 * Some SCSI commands don't actually transfer any data.
223 hdr.dxfer_direction = SG_DXFER_NONE;
226 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
227 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
229 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
230 if (ret) {
231 status = VIRTIO_BLK_S_UNSUPP;
232 goto fail;
236 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
237 * clear the masked_status field [hence status gets cleared too, see
238 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
239 * status has occurred. However they do set DRIVER_SENSE in driver_status
240 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
242 if (hdr.status == 0 && hdr.sb_len_wr > 0) {
243 hdr.status = CHECK_CONDITION;
246 stl_p(&req->scsi->errors,
247 hdr.status | (hdr.msg_status << 8) |
248 (hdr.host_status << 16) | (hdr.driver_status << 24));
249 stl_p(&req->scsi->residual, hdr.resid);
250 stl_p(&req->scsi->sense_len, hdr.sb_len_wr);
251 stl_p(&req->scsi->data_len, hdr.dxfer_len);
253 virtio_blk_req_complete(req, status);
254 g_free(req);
255 return;
256 #else
257 abort();
258 #endif
260 fail:
261 /* Just put anything nonzero so that the ioctl fails in the guest. */
262 stl_p(&req->scsi->errors, 255);
263 virtio_blk_req_complete(req, status);
264 g_free(req);
267 typedef struct MultiReqBuffer {
268 BlockRequest blkreq[32];
269 unsigned int num_writes;
270 } MultiReqBuffer;
272 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
274 int i, ret;
276 if (!mrb->num_writes) {
277 return;
280 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
281 if (ret != 0) {
282 for (i = 0; i < mrb->num_writes; i++) {
283 if (mrb->blkreq[i].error) {
284 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
289 mrb->num_writes = 0;
292 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
294 bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
297 * Make sure all outstanding writes are posted to the backing device.
299 virtio_submit_multiwrite(req->dev->bs, mrb);
300 bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
303 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
305 BlockRequest *blkreq;
306 uint64_t sector;
308 sector = ldq_p(&req->out->sector);
310 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
312 trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
314 if (sector & req->dev->sector_mask) {
315 virtio_blk_rw_complete(req, -EIO);
316 return;
318 if (req->qiov.size % req->dev->conf->logical_block_size) {
319 virtio_blk_rw_complete(req, -EIO);
320 return;
323 if (mrb->num_writes == 32) {
324 virtio_submit_multiwrite(req->dev->bs, mrb);
327 blkreq = &mrb->blkreq[mrb->num_writes];
328 blkreq->sector = sector;
329 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
330 blkreq->qiov = &req->qiov;
331 blkreq->cb = virtio_blk_rw_complete;
332 blkreq->opaque = req;
333 blkreq->error = 0;
335 mrb->num_writes++;
338 static void virtio_blk_handle_read(VirtIOBlockReq *req)
340 uint64_t sector;
342 sector = ldq_p(&req->out->sector);
344 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
346 trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
348 if (sector & req->dev->sector_mask) {
349 virtio_blk_rw_complete(req, -EIO);
350 return;
352 if (req->qiov.size % req->dev->conf->logical_block_size) {
353 virtio_blk_rw_complete(req, -EIO);
354 return;
356 bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
357 req->qiov.size / BDRV_SECTOR_SIZE,
358 virtio_blk_rw_complete, req);
361 static void virtio_blk_handle_request(VirtIOBlockReq *req,
362 MultiReqBuffer *mrb)
364 uint32_t type;
366 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
367 error_report("virtio-blk missing headers");
368 exit(1);
371 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
372 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
373 error_report("virtio-blk header not in correct element");
374 exit(1);
377 req->out = (void *)req->elem.out_sg[0].iov_base;
378 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
380 type = ldl_p(&req->out->type);
382 if (type & VIRTIO_BLK_T_FLUSH) {
383 virtio_blk_handle_flush(req, mrb);
384 } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
385 virtio_blk_handle_scsi(req);
386 } else if (type & VIRTIO_BLK_T_GET_ID) {
387 VirtIOBlock *s = req->dev;
390 * NB: per existing s/n string convention the string is
391 * terminated by '\0' only when shorter than buffer.
393 strncpy(req->elem.in_sg[0].iov_base,
394 s->blk->serial ? s->blk->serial : "",
395 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
396 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
397 g_free(req);
398 } else if (type & VIRTIO_BLK_T_OUT) {
399 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
400 req->elem.out_num - 1);
401 virtio_blk_handle_write(req, mrb);
402 } else if (type == VIRTIO_BLK_T_IN || type == VIRTIO_BLK_T_BARRIER) {
403 /* VIRTIO_BLK_T_IN is 0, so we can't just & it. */
404 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
405 req->elem.in_num - 1);
406 virtio_blk_handle_read(req);
407 } else {
408 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
409 g_free(req);
413 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
415 VirtIOBlock *s = to_virtio_blk(vdev);
416 VirtIOBlockReq *req;
417 MultiReqBuffer mrb = {
418 .num_writes = 0,
421 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
422 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
423 * dataplane here instead of waiting for .set_status().
425 if (s->dataplane) {
426 virtio_blk_data_plane_start(s->dataplane);
427 return;
429 #endif
431 while ((req = virtio_blk_get_request(s))) {
432 virtio_blk_handle_request(req, &mrb);
435 virtio_submit_multiwrite(s->bs, &mrb);
438 * FIXME: Want to check for completions before returning to guest mode,
439 * so cached reads and writes are reported as quickly as possible. But
440 * that should be done in the generic block layer.
444 static void virtio_blk_dma_restart_bh(void *opaque)
446 VirtIOBlock *s = opaque;
447 VirtIOBlockReq *req = s->rq;
448 MultiReqBuffer mrb = {
449 .num_writes = 0,
452 qemu_bh_delete(s->bh);
453 s->bh = NULL;
455 s->rq = NULL;
457 while (req) {
458 virtio_blk_handle_request(req, &mrb);
459 req = req->next;
462 virtio_submit_multiwrite(s->bs, &mrb);
465 static void virtio_blk_dma_restart_cb(void *opaque, int running,
466 RunState state)
468 VirtIOBlock *s = opaque;
470 if (!running) {
471 return;
474 if (!s->bh) {
475 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
476 qemu_bh_schedule(s->bh);
480 static void virtio_blk_reset(VirtIODevice *vdev)
482 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
483 VirtIOBlock *s = to_virtio_blk(vdev);
485 if (s->dataplane) {
486 virtio_blk_data_plane_stop(s->dataplane);
488 #endif
491 * This should cancel pending requests, but can't do nicely until there
492 * are per-device request lists.
494 bdrv_drain_all();
497 /* coalesce internal state, copy to pci i/o region 0
499 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
501 VirtIOBlock *s = to_virtio_blk(vdev);
502 struct virtio_blk_config blkcfg;
503 uint64_t capacity;
504 int blk_size = s->conf->logical_block_size;
506 bdrv_get_geometry(s->bs, &capacity);
507 memset(&blkcfg, 0, sizeof(blkcfg));
508 stq_raw(&blkcfg.capacity, capacity);
509 stl_raw(&blkcfg.seg_max, 128 - 2);
510 stw_raw(&blkcfg.cylinders, s->conf->cyls);
511 stl_raw(&blkcfg.blk_size, blk_size);
512 stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
513 stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
514 blkcfg.heads = s->conf->heads;
516 * We must ensure that the block device capacity is a multiple of
517 * the logical block size. If that is not the case, lets use
518 * sector_mask to adopt the geometry to have a correct picture.
519 * For those devices where the capacity is ok for the given geometry
520 * we dont touch the sector value of the geometry, since some devices
521 * (like s390 dasd) need a specific value. Here the capacity is already
522 * cyls*heads*secs*blk_size and the sector value is not block size
523 * divided by 512 - instead it is the amount of blk_size blocks
524 * per track (cylinder).
526 if (bdrv_getlength(s->bs) / s->conf->heads / s->conf->secs % blk_size) {
527 blkcfg.sectors = s->conf->secs & ~s->sector_mask;
528 } else {
529 blkcfg.sectors = s->conf->secs;
531 blkcfg.size_max = 0;
532 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
533 blkcfg.alignment_offset = 0;
534 blkcfg.wce = bdrv_enable_write_cache(s->bs);
535 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
538 static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
540 VirtIOBlock *s = to_virtio_blk(vdev);
541 struct virtio_blk_config blkcfg;
543 memcpy(&blkcfg, config, sizeof(blkcfg));
544 bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
547 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
549 VirtIOBlock *s = to_virtio_blk(vdev);
551 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
552 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
553 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
554 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
555 features |= (1 << VIRTIO_BLK_F_SCSI);
557 if (s->blk->config_wce) {
558 features |= (1 << VIRTIO_BLK_F_CONFIG_WCE);
560 if (bdrv_enable_write_cache(s->bs))
561 features |= (1 << VIRTIO_BLK_F_WCE);
563 if (bdrv_is_read_only(s->bs))
564 features |= 1 << VIRTIO_BLK_F_RO;
566 return features;
569 static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
571 VirtIOBlock *s = to_virtio_blk(vdev);
572 uint32_t features;
574 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
575 if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER |
576 VIRTIO_CONFIG_S_DRIVER_OK))) {
577 virtio_blk_data_plane_stop(s->dataplane);
579 #endif
581 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
582 return;
585 features = vdev->guest_features;
586 bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE)));
589 static void virtio_blk_save(QEMUFile *f, void *opaque)
591 VirtIOBlock *s = opaque;
592 VirtIOBlockReq *req = s->rq;
594 virtio_save(&s->vdev, f);
596 while (req) {
597 qemu_put_sbyte(f, 1);
598 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
599 req = req->next;
601 qemu_put_sbyte(f, 0);
604 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
606 VirtIOBlock *s = opaque;
607 int ret;
609 if (version_id != 2)
610 return -EINVAL;
612 ret = virtio_load(&s->vdev, f);
613 if (ret) {
614 return ret;
617 while (qemu_get_sbyte(f)) {
618 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
619 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
620 req->next = s->rq;
621 s->rq = req;
623 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
624 req->elem.in_num, 1);
625 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
626 req->elem.out_num, 0);
629 return 0;
632 static void virtio_blk_resize(void *opaque)
634 VirtIOBlock *s = opaque;
636 virtio_notify_config(&s->vdev);
639 static const BlockDevOps virtio_block_ops = {
640 .resize_cb = virtio_blk_resize,
643 VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
645 VirtIOBlock *s;
646 static int virtio_blk_id;
648 if (!blk->conf.bs) {
649 error_report("drive property not set");
650 return NULL;
652 if (!bdrv_is_inserted(blk->conf.bs)) {
653 error_report("Device needs media, but drive is empty");
654 return NULL;
657 blkconf_serial(&blk->conf, &blk->serial);
658 if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
659 return NULL;
662 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
663 sizeof(struct virtio_blk_config),
664 sizeof(VirtIOBlock));
666 s->vdev.get_config = virtio_blk_update_config;
667 s->vdev.set_config = virtio_blk_set_config;
668 s->vdev.get_features = virtio_blk_get_features;
669 s->vdev.set_status = virtio_blk_set_status;
670 s->vdev.reset = virtio_blk_reset;
671 s->bs = blk->conf.bs;
672 s->conf = &blk->conf;
673 s->blk = blk;
674 s->rq = NULL;
675 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
677 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
678 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
679 if (!virtio_blk_data_plane_create(&s->vdev, blk, &s->dataplane)) {
680 virtio_cleanup(&s->vdev);
681 return NULL;
683 #endif
685 s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
686 s->qdev = dev;
687 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
688 virtio_blk_save, virtio_blk_load, s);
689 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
690 bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size);
692 bdrv_iostatus_enable(s->bs);
693 add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
695 return &s->vdev;
698 void virtio_blk_exit(VirtIODevice *vdev)
700 VirtIOBlock *s = to_virtio_blk(vdev);
702 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
703 virtio_blk_data_plane_destroy(s->dataplane);
704 s->dataplane = NULL;
705 #endif
706 qemu_del_vm_change_state_handler(s->change);
707 unregister_savevm(s->qdev, "virtio-blk", s);
708 blockdev_mark_auto_del(s->bs);
709 virtio_cleanup(vdev);