use --libexecdir instead of ignoring it first and reinventing it later
[qemu/ar7.git] / hw / virtio-blk.c
blob6f6d172fd0d0ef1cab1dce98d7297aa48f95d0d8
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 int is_read)
69 BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
70 VirtIOBlock *s = req->dev;
72 if (action == BLOCK_ERR_IGNORE) {
73 bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_IGNORE, is_read);
74 return 0;
77 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
78 || action == BLOCK_ERR_STOP_ANY) {
79 req->next = s->rq;
80 s->rq = req;
81 bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_STOP, is_read);
82 vm_stop(RUN_STATE_IO_ERROR);
83 bdrv_iostatus_set_err(s->bs, error);
84 } else {
85 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
86 bdrv_acct_done(s->bs, &req->acct);
87 g_free(req);
88 bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_REPORT, is_read);
91 return 1;
94 static void virtio_blk_rw_complete(void *opaque, int ret)
96 VirtIOBlockReq *req = opaque;
98 trace_virtio_blk_rw_complete(req, ret);
100 if (ret) {
101 int is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
102 if (virtio_blk_handle_rw_error(req, -ret, is_read))
103 return;
106 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
107 bdrv_acct_done(req->dev->bs, &req->acct);
108 g_free(req);
111 static void virtio_blk_flush_complete(void *opaque, int ret)
113 VirtIOBlockReq *req = opaque;
115 if (ret) {
116 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
117 return;
121 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
122 bdrv_acct_done(req->dev->bs, &req->acct);
123 g_free(req);
126 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
128 VirtIOBlockReq *req = g_malloc(sizeof(*req));
129 req->dev = s;
130 req->qiov.size = 0;
131 req->next = NULL;
132 return req;
135 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
137 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
139 if (req != NULL) {
140 if (!virtqueue_pop(s->vq, &req->elem)) {
141 g_free(req);
142 return NULL;
146 return req;
149 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
151 #ifdef __linux__
152 int ret;
153 int i;
154 #endif
155 int status = VIRTIO_BLK_S_OK;
158 * We require at least one output segment each for the virtio_blk_outhdr
159 * and the SCSI command block.
161 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
162 * and the sense buffer pointer in the input segments.
164 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
165 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
166 g_free(req);
167 return;
171 * The scsi inhdr is placed in the second-to-last input segment, just
172 * before the regular inhdr.
174 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
176 if (!req->dev->blk->scsi) {
177 status = VIRTIO_BLK_S_UNSUPP;
178 goto fail;
182 * No support for bidirection commands yet.
184 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
185 status = VIRTIO_BLK_S_UNSUPP;
186 goto fail;
189 #ifdef __linux__
190 struct sg_io_hdr hdr;
191 memset(&hdr, 0, sizeof(struct sg_io_hdr));
192 hdr.interface_id = 'S';
193 hdr.cmd_len = req->elem.out_sg[1].iov_len;
194 hdr.cmdp = req->elem.out_sg[1].iov_base;
195 hdr.dxfer_len = 0;
197 if (req->elem.out_num > 2) {
199 * If there are more than the minimally required 2 output segments
200 * there is write payload starting from the third iovec.
202 hdr.dxfer_direction = SG_DXFER_TO_DEV;
203 hdr.iovec_count = req->elem.out_num - 2;
205 for (i = 0; i < hdr.iovec_count; i++)
206 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
208 hdr.dxferp = req->elem.out_sg + 2;
210 } else if (req->elem.in_num > 3) {
212 * If we have more than 3 input segments the guest wants to actually
213 * read data.
215 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
216 hdr.iovec_count = req->elem.in_num - 3;
217 for (i = 0; i < hdr.iovec_count; i++)
218 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
220 hdr.dxferp = req->elem.in_sg;
221 } else {
223 * Some SCSI commands don't actually transfer any data.
225 hdr.dxfer_direction = SG_DXFER_NONE;
228 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
229 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
231 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
232 if (ret) {
233 status = VIRTIO_BLK_S_UNSUPP;
234 goto fail;
238 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
239 * clear the masked_status field [hence status gets cleared too, see
240 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
241 * status has occurred. However they do set DRIVER_SENSE in driver_status
242 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
244 if (hdr.status == 0 && hdr.sb_len_wr > 0) {
245 hdr.status = CHECK_CONDITION;
248 stl_p(&req->scsi->errors,
249 hdr.status | (hdr.msg_status << 8) |
250 (hdr.host_status << 16) | (hdr.driver_status << 24));
251 stl_p(&req->scsi->residual, hdr.resid);
252 stl_p(&req->scsi->sense_len, hdr.sb_len_wr);
253 stl_p(&req->scsi->data_len, hdr.dxfer_len);
255 virtio_blk_req_complete(req, status);
256 g_free(req);
257 return;
258 #else
259 abort();
260 #endif
262 fail:
263 /* Just put anything nonzero so that the ioctl fails in the guest. */
264 stl_p(&req->scsi->errors, 255);
265 virtio_blk_req_complete(req, status);
266 g_free(req);
269 typedef struct MultiReqBuffer {
270 BlockRequest blkreq[32];
271 unsigned int num_writes;
272 } MultiReqBuffer;
274 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
276 int i, ret;
278 if (!mrb->num_writes) {
279 return;
282 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
283 if (ret != 0) {
284 for (i = 0; i < mrb->num_writes; i++) {
285 if (mrb->blkreq[i].error) {
286 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
291 mrb->num_writes = 0;
294 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
296 bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
299 * Make sure all outstanding writes are posted to the backing device.
301 virtio_submit_multiwrite(req->dev->bs, mrb);
302 bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
305 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
307 BlockRequest *blkreq;
308 uint64_t sector;
310 sector = ldq_p(&req->out->sector);
312 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
314 trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
316 if (sector & req->dev->sector_mask) {
317 virtio_blk_rw_complete(req, -EIO);
318 return;
320 if (req->qiov.size % req->dev->conf->logical_block_size) {
321 virtio_blk_rw_complete(req, -EIO);
322 return;
325 if (mrb->num_writes == 32) {
326 virtio_submit_multiwrite(req->dev->bs, mrb);
329 blkreq = &mrb->blkreq[mrb->num_writes];
330 blkreq->sector = sector;
331 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
332 blkreq->qiov = &req->qiov;
333 blkreq->cb = virtio_blk_rw_complete;
334 blkreq->opaque = req;
335 blkreq->error = 0;
337 mrb->num_writes++;
340 static void virtio_blk_handle_read(VirtIOBlockReq *req)
342 uint64_t sector;
344 sector = ldq_p(&req->out->sector);
346 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
348 trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
350 if (sector & req->dev->sector_mask) {
351 virtio_blk_rw_complete(req, -EIO);
352 return;
354 if (req->qiov.size % req->dev->conf->logical_block_size) {
355 virtio_blk_rw_complete(req, -EIO);
356 return;
358 bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
359 req->qiov.size / BDRV_SECTOR_SIZE,
360 virtio_blk_rw_complete, req);
363 static void virtio_blk_handle_request(VirtIOBlockReq *req,
364 MultiReqBuffer *mrb)
366 uint32_t type;
368 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
369 error_report("virtio-blk missing headers");
370 exit(1);
373 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
374 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
375 error_report("virtio-blk header not in correct element");
376 exit(1);
379 req->out = (void *)req->elem.out_sg[0].iov_base;
380 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
382 type = ldl_p(&req->out->type);
384 if (type & VIRTIO_BLK_T_FLUSH) {
385 virtio_blk_handle_flush(req, mrb);
386 } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
387 virtio_blk_handle_scsi(req);
388 } else if (type & VIRTIO_BLK_T_GET_ID) {
389 VirtIOBlock *s = req->dev;
392 * NB: per existing s/n string convention the string is
393 * terminated by '\0' only when shorter than buffer.
395 strncpy(req->elem.in_sg[0].iov_base,
396 s->blk->serial ? s->blk->serial : "",
397 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
398 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
399 g_free(req);
400 } else if (type & VIRTIO_BLK_T_OUT) {
401 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
402 req->elem.out_num - 1);
403 virtio_blk_handle_write(req, mrb);
404 } else {
405 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
406 req->elem.in_num - 1);
407 virtio_blk_handle_read(req);
411 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
413 VirtIOBlock *s = to_virtio_blk(vdev);
414 VirtIOBlockReq *req;
415 MultiReqBuffer mrb = {
416 .num_writes = 0,
419 while ((req = virtio_blk_get_request(s))) {
420 virtio_blk_handle_request(req, &mrb);
423 virtio_submit_multiwrite(s->bs, &mrb);
426 * FIXME: Want to check for completions before returning to guest mode,
427 * so cached reads and writes are reported as quickly as possible. But
428 * that should be done in the generic block layer.
432 static void virtio_blk_dma_restart_bh(void *opaque)
434 VirtIOBlock *s = opaque;
435 VirtIOBlockReq *req = s->rq;
436 MultiReqBuffer mrb = {
437 .num_writes = 0,
440 qemu_bh_delete(s->bh);
441 s->bh = NULL;
443 s->rq = NULL;
445 while (req) {
446 virtio_blk_handle_request(req, &mrb);
447 req = req->next;
450 virtio_submit_multiwrite(s->bs, &mrb);
453 static void virtio_blk_dma_restart_cb(void *opaque, int running,
454 RunState state)
456 VirtIOBlock *s = opaque;
458 if (!running)
459 return;
461 if (!s->bh) {
462 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
463 qemu_bh_schedule(s->bh);
467 static void virtio_blk_reset(VirtIODevice *vdev)
470 * This should cancel pending requests, but can't do nicely until there
471 * are per-device request lists.
473 bdrv_drain_all();
476 /* coalesce internal state, copy to pci i/o region 0
478 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
480 VirtIOBlock *s = to_virtio_blk(vdev);
481 struct virtio_blk_config blkcfg;
482 uint64_t capacity;
483 int blk_size = s->conf->logical_block_size;
485 bdrv_get_geometry(s->bs, &capacity);
486 memset(&blkcfg, 0, sizeof(blkcfg));
487 stq_raw(&blkcfg.capacity, capacity);
488 stl_raw(&blkcfg.seg_max, 128 - 2);
489 stw_raw(&blkcfg.cylinders, s->conf->cyls);
490 stl_raw(&blkcfg.blk_size, blk_size);
491 stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
492 stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
493 blkcfg.heads = s->conf->heads;
495 * We must ensure that the block device capacity is a multiple of
496 * the logical block size. If that is not the case, lets use
497 * sector_mask to adopt the geometry to have a correct picture.
498 * For those devices where the capacity is ok for the given geometry
499 * we dont touch the sector value of the geometry, since some devices
500 * (like s390 dasd) need a specific value. Here the capacity is already
501 * cyls*heads*secs*blk_size and the sector value is not block size
502 * divided by 512 - instead it is the amount of blk_size blocks
503 * per track (cylinder).
505 if (bdrv_getlength(s->bs) / s->conf->heads / s->conf->secs % blk_size) {
506 blkcfg.sectors = s->conf->secs & ~s->sector_mask;
507 } else {
508 blkcfg.sectors = s->conf->secs;
510 blkcfg.size_max = 0;
511 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
512 blkcfg.alignment_offset = 0;
513 blkcfg.wce = bdrv_enable_write_cache(s->bs);
514 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
517 static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
519 VirtIOBlock *s = to_virtio_blk(vdev);
520 struct virtio_blk_config blkcfg;
522 memcpy(&blkcfg, config, sizeof(blkcfg));
523 bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
526 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
528 VirtIOBlock *s = to_virtio_blk(vdev);
530 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
531 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
532 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
533 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
534 features |= (1 << VIRTIO_BLK_F_SCSI);
536 if (bdrv_enable_write_cache(s->bs))
537 features |= (1 << VIRTIO_BLK_F_WCE);
539 if (bdrv_is_read_only(s->bs))
540 features |= 1 << VIRTIO_BLK_F_RO;
542 return features;
545 static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
547 VirtIOBlock *s = to_virtio_blk(vdev);
548 uint32_t features;
550 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
551 return;
554 features = vdev->guest_features;
555 bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE)));
558 static void virtio_blk_save(QEMUFile *f, void *opaque)
560 VirtIOBlock *s = opaque;
561 VirtIOBlockReq *req = s->rq;
563 virtio_save(&s->vdev, f);
565 while (req) {
566 qemu_put_sbyte(f, 1);
567 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
568 req = req->next;
570 qemu_put_sbyte(f, 0);
573 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
575 VirtIOBlock *s = opaque;
576 int ret;
578 if (version_id != 2)
579 return -EINVAL;
581 ret = virtio_load(&s->vdev, f);
582 if (ret) {
583 return ret;
586 while (qemu_get_sbyte(f)) {
587 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
588 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
589 req->next = s->rq;
590 s->rq = req;
592 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
593 req->elem.in_num, 1);
594 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
595 req->elem.out_num, 0);
598 return 0;
601 static void virtio_blk_resize(void *opaque)
603 VirtIOBlock *s = opaque;
605 virtio_notify_config(&s->vdev);
608 static const BlockDevOps virtio_block_ops = {
609 .resize_cb = virtio_blk_resize,
612 VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
614 VirtIOBlock *s;
615 static int virtio_blk_id;
617 if (!blk->conf.bs) {
618 error_report("drive property not set");
619 return NULL;
621 if (!bdrv_is_inserted(blk->conf.bs)) {
622 error_report("Device needs media, but drive is empty");
623 return NULL;
626 blkconf_serial(&blk->conf, &blk->serial);
627 if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
628 return NULL;
631 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
632 sizeof(struct virtio_blk_config),
633 sizeof(VirtIOBlock));
635 s->vdev.get_config = virtio_blk_update_config;
636 s->vdev.set_config = virtio_blk_set_config;
637 s->vdev.get_features = virtio_blk_get_features;
638 s->vdev.set_status = virtio_blk_set_status;
639 s->vdev.reset = virtio_blk_reset;
640 s->bs = blk->conf.bs;
641 s->conf = &blk->conf;
642 s->blk = blk;
643 s->rq = NULL;
644 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
646 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
648 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
649 s->qdev = dev;
650 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
651 virtio_blk_save, virtio_blk_load, s);
652 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
653 bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size);
655 bdrv_iostatus_enable(s->bs);
656 add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
658 return &s->vdev;
661 void virtio_blk_exit(VirtIODevice *vdev)
663 VirtIOBlock *s = to_virtio_blk(vdev);
664 unregister_savevm(s->qdev, "virtio-blk", s);
665 blockdev_mark_auto_del(s->bs);
666 virtio_cleanup(vdev);