tcg-sparc: Optimize setcond2 equality compare with 0.
[qemu.git] / hw / virtio-blk.c
blobe25cc96477d0628d866c7735de15503bacb6c634
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.h"
16 #include "trace.h"
17 #include "hw/block-common.h"
18 #include "blockdev.h"
19 #include "virtio-blk.h"
20 #include "scsi-defs.h"
21 #ifdef __linux__
22 # include <scsi/sg.h>
23 #endif
25 typedef struct VirtIOBlock
27 VirtIODevice vdev;
28 BlockDriverState *bs;
29 VirtQueue *vq;
30 void *rq;
31 QEMUBH *bh;
32 BlockConf *conf;
33 VirtIOBlkConf *blk;
34 unsigned short sector_mask;
35 DeviceState *qdev;
36 } VirtIOBlock;
38 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
40 return (VirtIOBlock *)vdev;
43 typedef struct VirtIOBlockReq
45 VirtIOBlock *dev;
46 VirtQueueElement elem;
47 struct virtio_blk_inhdr *in;
48 struct virtio_blk_outhdr *out;
49 struct virtio_scsi_inhdr *scsi;
50 QEMUIOVector qiov;
51 struct VirtIOBlockReq *next;
52 BlockAcctCookie acct;
53 } VirtIOBlockReq;
55 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
57 VirtIOBlock *s = req->dev;
59 trace_virtio_blk_req_complete(req, status);
61 stb_p(&req->in->status, status);
62 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
63 virtio_notify(&s->vdev, s->vq);
66 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
67 bool is_read)
69 BlockErrorAction action = bdrv_get_error_action(req->dev->bs, is_read, error);
70 VirtIOBlock *s = req->dev;
72 if (action == BDRV_ACTION_STOP) {
73 req->next = s->rq;
74 s->rq = req;
75 } else if (action == BDRV_ACTION_REPORT) {
76 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
77 bdrv_acct_done(s->bs, &req->acct);
78 g_free(req);
81 bdrv_error_action(s->bs, action, is_read, error);
82 return action != BDRV_ACTION_IGNORE;
85 static void virtio_blk_rw_complete(void *opaque, int ret)
87 VirtIOBlockReq *req = opaque;
89 trace_virtio_blk_rw_complete(req, ret);
91 if (ret) {
92 bool is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
93 if (virtio_blk_handle_rw_error(req, -ret, is_read))
94 return;
97 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
98 bdrv_acct_done(req->dev->bs, &req->acct);
99 g_free(req);
102 static void virtio_blk_flush_complete(void *opaque, int ret)
104 VirtIOBlockReq *req = opaque;
106 if (ret) {
107 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
108 return;
112 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
113 bdrv_acct_done(req->dev->bs, &req->acct);
114 g_free(req);
117 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
119 VirtIOBlockReq *req = g_malloc(sizeof(*req));
120 req->dev = s;
121 req->qiov.size = 0;
122 req->next = NULL;
123 return req;
126 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
128 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
130 if (req != NULL) {
131 if (!virtqueue_pop(s->vq, &req->elem)) {
132 g_free(req);
133 return NULL;
137 return req;
140 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
142 #ifdef __linux__
143 int ret;
144 int i;
145 #endif
146 int status = VIRTIO_BLK_S_OK;
149 * We require at least one output segment each for the virtio_blk_outhdr
150 * and the SCSI command block.
152 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
153 * and the sense buffer pointer in the input segments.
155 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
156 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
157 g_free(req);
158 return;
162 * The scsi inhdr is placed in the second-to-last input segment, just
163 * before the regular inhdr.
165 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
167 if (!req->dev->blk->scsi) {
168 status = VIRTIO_BLK_S_UNSUPP;
169 goto fail;
173 * No support for bidirection commands yet.
175 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
176 status = VIRTIO_BLK_S_UNSUPP;
177 goto fail;
180 #ifdef __linux__
181 struct sg_io_hdr hdr;
182 memset(&hdr, 0, sizeof(struct sg_io_hdr));
183 hdr.interface_id = 'S';
184 hdr.cmd_len = req->elem.out_sg[1].iov_len;
185 hdr.cmdp = req->elem.out_sg[1].iov_base;
186 hdr.dxfer_len = 0;
188 if (req->elem.out_num > 2) {
190 * If there are more than the minimally required 2 output segments
191 * there is write payload starting from the third iovec.
193 hdr.dxfer_direction = SG_DXFER_TO_DEV;
194 hdr.iovec_count = req->elem.out_num - 2;
196 for (i = 0; i < hdr.iovec_count; i++)
197 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
199 hdr.dxferp = req->elem.out_sg + 2;
201 } else if (req->elem.in_num > 3) {
203 * If we have more than 3 input segments the guest wants to actually
204 * read data.
206 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
207 hdr.iovec_count = req->elem.in_num - 3;
208 for (i = 0; i < hdr.iovec_count; i++)
209 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
211 hdr.dxferp = req->elem.in_sg;
212 } else {
214 * Some SCSI commands don't actually transfer any data.
216 hdr.dxfer_direction = SG_DXFER_NONE;
219 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
220 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
222 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
223 if (ret) {
224 status = VIRTIO_BLK_S_UNSUPP;
225 goto fail;
229 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
230 * clear the masked_status field [hence status gets cleared too, see
231 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
232 * status has occurred. However they do set DRIVER_SENSE in driver_status
233 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
235 if (hdr.status == 0 && hdr.sb_len_wr > 0) {
236 hdr.status = CHECK_CONDITION;
239 stl_p(&req->scsi->errors,
240 hdr.status | (hdr.msg_status << 8) |
241 (hdr.host_status << 16) | (hdr.driver_status << 24));
242 stl_p(&req->scsi->residual, hdr.resid);
243 stl_p(&req->scsi->sense_len, hdr.sb_len_wr);
244 stl_p(&req->scsi->data_len, hdr.dxfer_len);
246 virtio_blk_req_complete(req, status);
247 g_free(req);
248 return;
249 #else
250 abort();
251 #endif
253 fail:
254 /* Just put anything nonzero so that the ioctl fails in the guest. */
255 stl_p(&req->scsi->errors, 255);
256 virtio_blk_req_complete(req, status);
257 g_free(req);
260 typedef struct MultiReqBuffer {
261 BlockRequest blkreq[32];
262 unsigned int num_writes;
263 } MultiReqBuffer;
265 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
267 int i, ret;
269 if (!mrb->num_writes) {
270 return;
273 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
274 if (ret != 0) {
275 for (i = 0; i < mrb->num_writes; i++) {
276 if (mrb->blkreq[i].error) {
277 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
282 mrb->num_writes = 0;
285 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
287 bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
290 * Make sure all outstanding writes are posted to the backing device.
292 virtio_submit_multiwrite(req->dev->bs, mrb);
293 bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
296 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
298 BlockRequest *blkreq;
299 uint64_t sector;
301 sector = ldq_p(&req->out->sector);
303 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
305 trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
307 if (sector & req->dev->sector_mask) {
308 virtio_blk_rw_complete(req, -EIO);
309 return;
311 if (req->qiov.size % req->dev->conf->logical_block_size) {
312 virtio_blk_rw_complete(req, -EIO);
313 return;
316 if (mrb->num_writes == 32) {
317 virtio_submit_multiwrite(req->dev->bs, mrb);
320 blkreq = &mrb->blkreq[mrb->num_writes];
321 blkreq->sector = sector;
322 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
323 blkreq->qiov = &req->qiov;
324 blkreq->cb = virtio_blk_rw_complete;
325 blkreq->opaque = req;
326 blkreq->error = 0;
328 mrb->num_writes++;
331 static void virtio_blk_handle_read(VirtIOBlockReq *req)
333 uint64_t sector;
335 sector = ldq_p(&req->out->sector);
337 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
339 trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
341 if (sector & req->dev->sector_mask) {
342 virtio_blk_rw_complete(req, -EIO);
343 return;
345 if (req->qiov.size % req->dev->conf->logical_block_size) {
346 virtio_blk_rw_complete(req, -EIO);
347 return;
349 bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
350 req->qiov.size / BDRV_SECTOR_SIZE,
351 virtio_blk_rw_complete, req);
354 static void virtio_blk_handle_request(VirtIOBlockReq *req,
355 MultiReqBuffer *mrb)
357 uint32_t type;
359 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
360 error_report("virtio-blk missing headers");
361 exit(1);
364 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
365 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
366 error_report("virtio-blk header not in correct element");
367 exit(1);
370 req->out = (void *)req->elem.out_sg[0].iov_base;
371 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
373 type = ldl_p(&req->out->type);
375 if (type & VIRTIO_BLK_T_FLUSH) {
376 virtio_blk_handle_flush(req, mrb);
377 } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
378 virtio_blk_handle_scsi(req);
379 } else if (type & VIRTIO_BLK_T_GET_ID) {
380 VirtIOBlock *s = req->dev;
383 * NB: per existing s/n string convention the string is
384 * terminated by '\0' only when shorter than buffer.
386 strncpy(req->elem.in_sg[0].iov_base,
387 s->blk->serial ? s->blk->serial : "",
388 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
389 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
390 g_free(req);
391 } else if (type & VIRTIO_BLK_T_OUT) {
392 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
393 req->elem.out_num - 1);
394 virtio_blk_handle_write(req, mrb);
395 } else {
396 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
397 req->elem.in_num - 1);
398 virtio_blk_handle_read(req);
402 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
404 VirtIOBlock *s = to_virtio_blk(vdev);
405 VirtIOBlockReq *req;
406 MultiReqBuffer mrb = {
407 .num_writes = 0,
410 while ((req = virtio_blk_get_request(s))) {
411 virtio_blk_handle_request(req, &mrb);
414 virtio_submit_multiwrite(s->bs, &mrb);
417 * FIXME: Want to check for completions before returning to guest mode,
418 * so cached reads and writes are reported as quickly as possible. But
419 * that should be done in the generic block layer.
423 static void virtio_blk_dma_restart_bh(void *opaque)
425 VirtIOBlock *s = opaque;
426 VirtIOBlockReq *req = s->rq;
427 MultiReqBuffer mrb = {
428 .num_writes = 0,
431 qemu_bh_delete(s->bh);
432 s->bh = NULL;
434 s->rq = NULL;
436 while (req) {
437 virtio_blk_handle_request(req, &mrb);
438 req = req->next;
441 virtio_submit_multiwrite(s->bs, &mrb);
444 static void virtio_blk_dma_restart_cb(void *opaque, int running,
445 RunState state)
447 VirtIOBlock *s = opaque;
449 if (!running)
450 return;
452 if (!s->bh) {
453 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
454 qemu_bh_schedule(s->bh);
458 static void virtio_blk_reset(VirtIODevice *vdev)
461 * This should cancel pending requests, but can't do nicely until there
462 * are per-device request lists.
464 bdrv_drain_all();
467 /* coalesce internal state, copy to pci i/o region 0
469 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
471 VirtIOBlock *s = to_virtio_blk(vdev);
472 struct virtio_blk_config blkcfg;
473 uint64_t capacity;
474 int blk_size = s->conf->logical_block_size;
476 bdrv_get_geometry(s->bs, &capacity);
477 memset(&blkcfg, 0, sizeof(blkcfg));
478 stq_raw(&blkcfg.capacity, capacity);
479 stl_raw(&blkcfg.seg_max, 128 - 2);
480 stw_raw(&blkcfg.cylinders, s->conf->cyls);
481 stl_raw(&blkcfg.blk_size, blk_size);
482 stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
483 stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
484 blkcfg.heads = s->conf->heads;
486 * We must ensure that the block device capacity is a multiple of
487 * the logical block size. If that is not the case, lets use
488 * sector_mask to adopt the geometry to have a correct picture.
489 * For those devices where the capacity is ok for the given geometry
490 * we dont touch the sector value of the geometry, since some devices
491 * (like s390 dasd) need a specific value. Here the capacity is already
492 * cyls*heads*secs*blk_size and the sector value is not block size
493 * divided by 512 - instead it is the amount of blk_size blocks
494 * per track (cylinder).
496 if (bdrv_getlength(s->bs) / s->conf->heads / s->conf->secs % blk_size) {
497 blkcfg.sectors = s->conf->secs & ~s->sector_mask;
498 } else {
499 blkcfg.sectors = s->conf->secs;
501 blkcfg.size_max = 0;
502 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
503 blkcfg.alignment_offset = 0;
504 blkcfg.wce = bdrv_enable_write_cache(s->bs);
505 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
508 static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
510 VirtIOBlock *s = to_virtio_blk(vdev);
511 struct virtio_blk_config blkcfg;
513 memcpy(&blkcfg, config, sizeof(blkcfg));
514 bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
517 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
519 VirtIOBlock *s = to_virtio_blk(vdev);
521 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
522 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
523 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
524 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
525 features |= (1 << VIRTIO_BLK_F_SCSI);
527 if (bdrv_enable_write_cache(s->bs))
528 features |= (1 << VIRTIO_BLK_F_WCE);
530 if (bdrv_is_read_only(s->bs))
531 features |= 1 << VIRTIO_BLK_F_RO;
533 return features;
536 static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
538 VirtIOBlock *s = to_virtio_blk(vdev);
539 uint32_t features;
541 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
542 return;
545 features = vdev->guest_features;
546 bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE)));
549 static void virtio_blk_save(QEMUFile *f, void *opaque)
551 VirtIOBlock *s = opaque;
552 VirtIOBlockReq *req = s->rq;
554 virtio_save(&s->vdev, f);
556 while (req) {
557 qemu_put_sbyte(f, 1);
558 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
559 req = req->next;
561 qemu_put_sbyte(f, 0);
564 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
566 VirtIOBlock *s = opaque;
567 int ret;
569 if (version_id != 2)
570 return -EINVAL;
572 ret = virtio_load(&s->vdev, f);
573 if (ret) {
574 return ret;
577 while (qemu_get_sbyte(f)) {
578 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
579 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
580 req->next = s->rq;
581 s->rq = req;
583 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
584 req->elem.in_num, 1);
585 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
586 req->elem.out_num, 0);
589 return 0;
592 static void virtio_blk_resize(void *opaque)
594 VirtIOBlock *s = opaque;
596 virtio_notify_config(&s->vdev);
599 static const BlockDevOps virtio_block_ops = {
600 .resize_cb = virtio_blk_resize,
603 VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
605 VirtIOBlock *s;
606 static int virtio_blk_id;
608 if (!blk->conf.bs) {
609 error_report("drive property not set");
610 return NULL;
612 if (!bdrv_is_inserted(blk->conf.bs)) {
613 error_report("Device needs media, but drive is empty");
614 return NULL;
617 blkconf_serial(&blk->conf, &blk->serial);
618 if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
619 return NULL;
622 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
623 sizeof(struct virtio_blk_config),
624 sizeof(VirtIOBlock));
626 s->vdev.get_config = virtio_blk_update_config;
627 s->vdev.set_config = virtio_blk_set_config;
628 s->vdev.get_features = virtio_blk_get_features;
629 s->vdev.set_status = virtio_blk_set_status;
630 s->vdev.reset = virtio_blk_reset;
631 s->bs = blk->conf.bs;
632 s->conf = &blk->conf;
633 s->blk = blk;
634 s->rq = NULL;
635 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
637 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
639 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
640 s->qdev = dev;
641 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
642 virtio_blk_save, virtio_blk_load, s);
643 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
644 bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size);
646 bdrv_iostatus_enable(s->bs);
647 add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
649 return &s->vdev;
652 void virtio_blk_exit(VirtIODevice *vdev)
654 VirtIOBlock *s = to_virtio_blk(vdev);
655 unregister_savevm(s->qdev, "virtio-blk", s);
656 blockdev_mark_auto_del(s->bs);
657 virtio_cleanup(vdev);