block: Drop tray status tracking, no longer used
[qemu.git] / hw / virtio-blk.c
blobd5d4757f626d9a01180a56d45fa2431e0bd0bd2c
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 "blockdev.h"
18 #include "virtio-blk.h"
19 #ifdef __linux__
20 # include <scsi/sg.h>
21 #endif
23 typedef struct VirtIOBlock
25 VirtIODevice vdev;
26 BlockDriverState *bs;
27 VirtQueue *vq;
28 void *rq;
29 QEMUBH *bh;
30 BlockConf *conf;
31 char *serial;
32 unsigned short sector_mask;
33 DeviceState *qdev;
34 } VirtIOBlock;
36 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
38 return (VirtIOBlock *)vdev;
41 typedef struct VirtIOBlockReq
43 VirtIOBlock *dev;
44 VirtQueueElement elem;
45 struct virtio_blk_inhdr *in;
46 struct virtio_blk_outhdr *out;
47 struct virtio_scsi_inhdr *scsi;
48 QEMUIOVector qiov;
49 struct VirtIOBlockReq *next;
50 BlockAcctCookie acct;
51 } VirtIOBlockReq;
53 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
55 VirtIOBlock *s = req->dev;
57 trace_virtio_blk_req_complete(req, status);
59 stb_p(&req->in->status, status);
60 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
61 virtio_notify(&s->vdev, s->vq);
64 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
65 int is_read)
67 BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
68 VirtIOBlock *s = req->dev;
70 if (action == BLOCK_ERR_IGNORE) {
71 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
72 return 0;
75 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
76 || action == BLOCK_ERR_STOP_ANY) {
77 req->next = s->rq;
78 s->rq = req;
79 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
80 vm_stop(VMSTOP_DISKFULL);
81 } else {
82 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
83 bdrv_acct_done(s->bs, &req->acct);
84 g_free(req);
85 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
88 return 1;
91 static void virtio_blk_rw_complete(void *opaque, int ret)
93 VirtIOBlockReq *req = opaque;
95 trace_virtio_blk_rw_complete(req, ret);
97 if (ret) {
98 int is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
99 if (virtio_blk_handle_rw_error(req, -ret, is_read))
100 return;
103 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
104 bdrv_acct_done(req->dev->bs, &req->acct);
105 g_free(req);
108 static void virtio_blk_flush_complete(void *opaque, int ret)
110 VirtIOBlockReq *req = opaque;
112 if (ret) {
113 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
114 return;
118 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
119 bdrv_acct_done(req->dev->bs, &req->acct);
120 g_free(req);
123 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
125 VirtIOBlockReq *req = g_malloc(sizeof(*req));
126 req->dev = s;
127 req->qiov.size = 0;
128 req->next = NULL;
129 return req;
132 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
134 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
136 if (req != NULL) {
137 if (!virtqueue_pop(s->vq, &req->elem)) {
138 g_free(req);
139 return NULL;
143 return req;
146 #ifdef __linux__
147 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
149 struct sg_io_hdr hdr;
150 int ret;
151 int status;
152 int i;
155 * We require at least one output segment each for the virtio_blk_outhdr
156 * and the SCSI command block.
158 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
159 * and the sense buffer pointer in the input segments.
161 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
162 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
163 g_free(req);
164 return;
168 * No support for bidirection commands yet.
170 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
171 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
172 g_free(req);
173 return;
177 * The scsi inhdr is placed in the second-to-last input segment, just
178 * before the regular inhdr.
180 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
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 hdr.status = ret;
226 hdr.resid = hdr.dxfer_len;
227 } else if (hdr.status) {
228 status = VIRTIO_BLK_S_IOERR;
229 } else {
230 status = VIRTIO_BLK_S_OK;
233 stl_p(&req->scsi->errors, hdr.status);
234 stl_p(&req->scsi->residual, hdr.resid);
235 stl_p(&req->scsi->sense_len, hdr.sb_len_wr);
236 stl_p(&req->scsi->data_len, hdr.dxfer_len);
238 virtio_blk_req_complete(req, status);
239 g_free(req);
241 #else
242 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
244 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
245 g_free(req);
247 #endif /* __linux__ */
249 typedef struct MultiReqBuffer {
250 BlockRequest blkreq[32];
251 unsigned int num_writes;
252 } MultiReqBuffer;
254 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
256 int i, ret;
258 if (!mrb->num_writes) {
259 return;
262 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
263 if (ret != 0) {
264 for (i = 0; i < mrb->num_writes; i++) {
265 if (mrb->blkreq[i].error) {
266 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
271 mrb->num_writes = 0;
274 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
276 BlockDriverAIOCB *acb;
278 bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
281 * Make sure all outstanding writes are posted to the backing device.
283 virtio_submit_multiwrite(req->dev->bs, mrb);
285 acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
286 if (!acb) {
287 virtio_blk_flush_complete(req, -EIO);
291 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
293 BlockRequest *blkreq;
294 uint64_t sector;
296 sector = ldq_p(&req->out->sector);
298 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
300 trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
302 if (sector & req->dev->sector_mask) {
303 virtio_blk_rw_complete(req, -EIO);
304 return;
306 if (req->qiov.size % req->dev->conf->logical_block_size) {
307 virtio_blk_rw_complete(req, -EIO);
308 return;
311 if (mrb->num_writes == 32) {
312 virtio_submit_multiwrite(req->dev->bs, mrb);
315 blkreq = &mrb->blkreq[mrb->num_writes];
316 blkreq->sector = sector;
317 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
318 blkreq->qiov = &req->qiov;
319 blkreq->cb = virtio_blk_rw_complete;
320 blkreq->opaque = req;
321 blkreq->error = 0;
323 mrb->num_writes++;
326 static void virtio_blk_handle_read(VirtIOBlockReq *req)
328 BlockDriverAIOCB *acb;
329 uint64_t sector;
331 sector = ldq_p(&req->out->sector);
333 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
335 if (sector & req->dev->sector_mask) {
336 virtio_blk_rw_complete(req, -EIO);
337 return;
339 if (req->qiov.size % req->dev->conf->logical_block_size) {
340 virtio_blk_rw_complete(req, -EIO);
341 return;
344 acb = bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
345 req->qiov.size / BDRV_SECTOR_SIZE,
346 virtio_blk_rw_complete, req);
347 if (!acb) {
348 virtio_blk_rw_complete(req, -EIO);
352 static void virtio_blk_handle_request(VirtIOBlockReq *req,
353 MultiReqBuffer *mrb)
355 uint32_t type;
357 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
358 error_report("virtio-blk missing headers");
359 exit(1);
362 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
363 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
364 error_report("virtio-blk header not in correct element");
365 exit(1);
368 req->out = (void *)req->elem.out_sg[0].iov_base;
369 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
371 type = ldl_p(&req->out->type);
373 if (type & VIRTIO_BLK_T_FLUSH) {
374 virtio_blk_handle_flush(req, mrb);
375 } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
376 virtio_blk_handle_scsi(req);
377 } else if (type & VIRTIO_BLK_T_GET_ID) {
378 VirtIOBlock *s = req->dev;
381 * NB: per existing s/n string convention the string is
382 * terminated by '\0' only when shorter than buffer.
384 strncpy(req->elem.in_sg[0].iov_base,
385 s->serial ? s->serial : "",
386 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
387 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
388 g_free(req);
389 } else if (type & VIRTIO_BLK_T_OUT) {
390 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
391 req->elem.out_num - 1);
392 virtio_blk_handle_write(req, mrb);
393 } else {
394 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
395 req->elem.in_num - 1);
396 virtio_blk_handle_read(req);
400 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
402 VirtIOBlock *s = to_virtio_blk(vdev);
403 VirtIOBlockReq *req;
404 MultiReqBuffer mrb = {
405 .num_writes = 0,
408 while ((req = virtio_blk_get_request(s))) {
409 virtio_blk_handle_request(req, &mrb);
412 virtio_submit_multiwrite(s->bs, &mrb);
415 * FIXME: Want to check for completions before returning to guest mode,
416 * so cached reads and writes are reported as quickly as possible. But
417 * that should be done in the generic block layer.
421 static void virtio_blk_dma_restart_bh(void *opaque)
423 VirtIOBlock *s = opaque;
424 VirtIOBlockReq *req = s->rq;
425 MultiReqBuffer mrb = {
426 .num_writes = 0,
429 qemu_bh_delete(s->bh);
430 s->bh = NULL;
432 s->rq = NULL;
434 while (req) {
435 virtio_blk_handle_request(req, &mrb);
436 req = req->next;
439 virtio_submit_multiwrite(s->bs, &mrb);
442 static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
444 VirtIOBlock *s = opaque;
446 if (!running)
447 return;
449 if (!s->bh) {
450 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
451 qemu_bh_schedule(s->bh);
455 static void virtio_blk_reset(VirtIODevice *vdev)
458 * This should cancel pending requests, but can't do nicely until there
459 * are per-device request lists.
461 qemu_aio_flush();
464 /* coalesce internal state, copy to pci i/o region 0
466 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
468 VirtIOBlock *s = to_virtio_blk(vdev);
469 struct virtio_blk_config blkcfg;
470 uint64_t capacity;
471 int cylinders, heads, secs;
473 bdrv_get_geometry(s->bs, &capacity);
474 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
475 memset(&blkcfg, 0, sizeof(blkcfg));
476 stq_raw(&blkcfg.capacity, capacity);
477 stl_raw(&blkcfg.seg_max, 128 - 2);
478 stw_raw(&blkcfg.cylinders, cylinders);
479 blkcfg.heads = heads;
480 blkcfg.sectors = secs & ~s->sector_mask;
481 blkcfg.blk_size = s->conf->logical_block_size;
482 blkcfg.size_max = 0;
483 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
484 blkcfg.alignment_offset = 0;
485 blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size;
486 blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size;
487 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
490 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
492 VirtIOBlock *s = to_virtio_blk(vdev);
494 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
495 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
496 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
497 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
499 if (bdrv_enable_write_cache(s->bs))
500 features |= (1 << VIRTIO_BLK_F_WCACHE);
502 if (bdrv_is_read_only(s->bs))
503 features |= 1 << VIRTIO_BLK_F_RO;
505 return features;
508 static void virtio_blk_save(QEMUFile *f, void *opaque)
510 VirtIOBlock *s = opaque;
511 VirtIOBlockReq *req = s->rq;
513 virtio_save(&s->vdev, f);
515 while (req) {
516 qemu_put_sbyte(f, 1);
517 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
518 req = req->next;
520 qemu_put_sbyte(f, 0);
523 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
525 VirtIOBlock *s = opaque;
527 if (version_id != 2)
528 return -EINVAL;
530 virtio_load(&s->vdev, f);
531 while (qemu_get_sbyte(f)) {
532 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
533 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
534 req->next = s->rq;
535 s->rq = req;
537 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
538 req->elem.in_num, 1);
539 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
540 req->elem.out_num, 0);
543 return 0;
546 static void virtio_blk_resize(void *opaque)
548 VirtIOBlock *s = opaque;
550 virtio_notify_config(&s->vdev);
553 static const BlockDevOps virtio_block_ops = {
554 .resize_cb = virtio_blk_resize,
557 VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
558 char **serial)
560 VirtIOBlock *s;
561 int cylinders, heads, secs;
562 static int virtio_blk_id;
563 DriveInfo *dinfo;
565 if (!conf->bs) {
566 error_report("virtio-blk-pci: drive property not set");
567 return NULL;
569 if (!bdrv_is_inserted(conf->bs)) {
570 error_report("Device needs media, but drive is empty");
571 return NULL;
574 if (!*serial) {
575 /* try to fall back to value set with legacy -drive serial=... */
576 dinfo = drive_get_by_blockdev(conf->bs);
577 if (*dinfo->serial) {
578 *serial = strdup(dinfo->serial);
582 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
583 sizeof(struct virtio_blk_config),
584 sizeof(VirtIOBlock));
586 s->vdev.get_config = virtio_blk_update_config;
587 s->vdev.get_features = virtio_blk_get_features;
588 s->vdev.reset = virtio_blk_reset;
589 s->bs = conf->bs;
590 s->conf = conf;
591 s->serial = *serial;
592 s->rq = NULL;
593 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
594 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
596 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
598 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
599 s->qdev = dev;
600 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
601 virtio_blk_save, virtio_blk_load, s);
602 bdrv_set_removable(s->bs, 0);
603 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
604 s->bs->buffer_alignment = conf->logical_block_size;
606 add_boot_device_path(conf->bootindex, dev, "/disk@0,0");
608 return &s->vdev;
611 void virtio_blk_exit(VirtIODevice *vdev)
613 VirtIOBlock *s = to_virtio_blk(vdev);
614 unregister_savevm(s->qdev, "virtio-blk", s);
615 virtio_cleanup(vdev);