e1000: Use PCI DMA stub functions
[qemu.git] / hw / virtio-blk.c
blob2a5d1a92b30b36944ce994ce6e9238f81c227173
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(RUN_STATE_IO_ERROR);
81 bdrv_iostatus_set_err(s->bs, error);
82 } else {
83 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
84 bdrv_acct_done(s->bs, &req->acct);
85 g_free(req);
86 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
89 return 1;
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 int 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 #ifdef __linux__
148 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
150 struct sg_io_hdr hdr;
151 int ret;
152 int status;
153 int i;
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 * No support for bidirection commands yet.
171 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
172 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
173 g_free(req);
174 return;
178 * The scsi inhdr is placed in the second-to-last input segment, just
179 * before the regular inhdr.
181 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
183 memset(&hdr, 0, sizeof(struct sg_io_hdr));
184 hdr.interface_id = 'S';
185 hdr.cmd_len = req->elem.out_sg[1].iov_len;
186 hdr.cmdp = req->elem.out_sg[1].iov_base;
187 hdr.dxfer_len = 0;
189 if (req->elem.out_num > 2) {
191 * If there are more than the minimally required 2 output segments
192 * there is write payload starting from the third iovec.
194 hdr.dxfer_direction = SG_DXFER_TO_DEV;
195 hdr.iovec_count = req->elem.out_num - 2;
197 for (i = 0; i < hdr.iovec_count; i++)
198 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
200 hdr.dxferp = req->elem.out_sg + 2;
202 } else if (req->elem.in_num > 3) {
204 * If we have more than 3 input segments the guest wants to actually
205 * read data.
207 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
208 hdr.iovec_count = req->elem.in_num - 3;
209 for (i = 0; i < hdr.iovec_count; i++)
210 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
212 hdr.dxferp = req->elem.in_sg;
213 } else {
215 * Some SCSI commands don't actually transfer any data.
217 hdr.dxfer_direction = SG_DXFER_NONE;
220 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
221 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
223 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
224 if (ret) {
225 status = VIRTIO_BLK_S_UNSUPP;
226 hdr.status = ret;
227 hdr.resid = hdr.dxfer_len;
228 } else if (hdr.status) {
229 status = VIRTIO_BLK_S_IOERR;
230 } else {
231 status = VIRTIO_BLK_S_OK;
234 stl_p(&req->scsi->errors, hdr.status);
235 stl_p(&req->scsi->residual, hdr.resid);
236 stl_p(&req->scsi->sense_len, hdr.sb_len_wr);
237 stl_p(&req->scsi->data_len, hdr.dxfer_len);
239 virtio_blk_req_complete(req, status);
240 g_free(req);
242 #else
243 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
245 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
246 g_free(req);
248 #endif /* __linux__ */
250 typedef struct MultiReqBuffer {
251 BlockRequest blkreq[32];
252 unsigned int num_writes;
253 } MultiReqBuffer;
255 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
257 int i, ret;
259 if (!mrb->num_writes) {
260 return;
263 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
264 if (ret != 0) {
265 for (i = 0; i < mrb->num_writes; i++) {
266 if (mrb->blkreq[i].error) {
267 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
272 mrb->num_writes = 0;
275 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
277 BlockDriverAIOCB *acb;
279 bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
282 * Make sure all outstanding writes are posted to the backing device.
284 virtio_submit_multiwrite(req->dev->bs, mrb);
286 acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
287 if (!acb) {
288 virtio_blk_flush_complete(req, -EIO);
292 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
294 BlockRequest *blkreq;
295 uint64_t sector;
297 sector = ldq_p(&req->out->sector);
299 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
301 trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
303 if (sector & req->dev->sector_mask) {
304 virtio_blk_rw_complete(req, -EIO);
305 return;
307 if (req->qiov.size % req->dev->conf->logical_block_size) {
308 virtio_blk_rw_complete(req, -EIO);
309 return;
312 if (mrb->num_writes == 32) {
313 virtio_submit_multiwrite(req->dev->bs, mrb);
316 blkreq = &mrb->blkreq[mrb->num_writes];
317 blkreq->sector = sector;
318 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
319 blkreq->qiov = &req->qiov;
320 blkreq->cb = virtio_blk_rw_complete;
321 blkreq->opaque = req;
322 blkreq->error = 0;
324 mrb->num_writes++;
327 static void virtio_blk_handle_read(VirtIOBlockReq *req)
329 BlockDriverAIOCB *acb;
330 uint64_t sector;
332 sector = ldq_p(&req->out->sector);
334 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
336 if (sector & req->dev->sector_mask) {
337 virtio_blk_rw_complete(req, -EIO);
338 return;
340 if (req->qiov.size % req->dev->conf->logical_block_size) {
341 virtio_blk_rw_complete(req, -EIO);
342 return;
345 acb = bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
346 req->qiov.size / BDRV_SECTOR_SIZE,
347 virtio_blk_rw_complete, req);
348 if (!acb) {
349 virtio_blk_rw_complete(req, -EIO);
353 static void virtio_blk_handle_request(VirtIOBlockReq *req,
354 MultiReqBuffer *mrb)
356 uint32_t type;
358 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
359 error_report("virtio-blk missing headers");
360 exit(1);
363 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
364 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
365 error_report("virtio-blk header not in correct element");
366 exit(1);
369 req->out = (void *)req->elem.out_sg[0].iov_base;
370 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
372 type = ldl_p(&req->out->type);
374 if (type & VIRTIO_BLK_T_FLUSH) {
375 virtio_blk_handle_flush(req, mrb);
376 } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
377 virtio_blk_handle_scsi(req);
378 } else if (type & VIRTIO_BLK_T_GET_ID) {
379 VirtIOBlock *s = req->dev;
382 * NB: per existing s/n string convention the string is
383 * terminated by '\0' only when shorter than buffer.
385 strncpy(req->elem.in_sg[0].iov_base,
386 s->serial ? s->serial : "",
387 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
388 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
389 g_free(req);
390 } else if (type & VIRTIO_BLK_T_OUT) {
391 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
392 req->elem.out_num - 1);
393 virtio_blk_handle_write(req, mrb);
394 } else {
395 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
396 req->elem.in_num - 1);
397 virtio_blk_handle_read(req);
401 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
403 VirtIOBlock *s = to_virtio_blk(vdev);
404 VirtIOBlockReq *req;
405 MultiReqBuffer mrb = {
406 .num_writes = 0,
409 while ((req = virtio_blk_get_request(s))) {
410 virtio_blk_handle_request(req, &mrb);
413 virtio_submit_multiwrite(s->bs, &mrb);
416 * FIXME: Want to check for completions before returning to guest mode,
417 * so cached reads and writes are reported as quickly as possible. But
418 * that should be done in the generic block layer.
422 static void virtio_blk_dma_restart_bh(void *opaque)
424 VirtIOBlock *s = opaque;
425 VirtIOBlockReq *req = s->rq;
426 MultiReqBuffer mrb = {
427 .num_writes = 0,
430 qemu_bh_delete(s->bh);
431 s->bh = NULL;
433 s->rq = NULL;
435 while (req) {
436 virtio_blk_handle_request(req, &mrb);
437 req = req->next;
440 virtio_submit_multiwrite(s->bs, &mrb);
443 static void virtio_blk_dma_restart_cb(void *opaque, int running,
444 RunState state)
446 VirtIOBlock *s = opaque;
448 if (!running)
449 return;
451 if (!s->bh) {
452 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
453 qemu_bh_schedule(s->bh);
457 static void virtio_blk_reset(VirtIODevice *vdev)
460 * This should cancel pending requests, but can't do nicely until there
461 * are per-device request lists.
463 qemu_aio_flush();
466 /* coalesce internal state, copy to pci i/o region 0
468 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
470 VirtIOBlock *s = to_virtio_blk(vdev);
471 struct virtio_blk_config blkcfg;
472 uint64_t capacity;
473 int cylinders, heads, secs;
475 bdrv_get_geometry(s->bs, &capacity);
476 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
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, cylinders);
481 blkcfg.heads = heads;
482 blkcfg.sectors = secs & ~s->sector_mask;
483 blkcfg.blk_size = s->conf->logical_block_size;
484 blkcfg.size_max = 0;
485 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
486 blkcfg.alignment_offset = 0;
487 blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size;
488 blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size;
489 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
492 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
494 VirtIOBlock *s = to_virtio_blk(vdev);
496 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
497 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
498 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
499 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
501 if (bdrv_enable_write_cache(s->bs))
502 features |= (1 << VIRTIO_BLK_F_WCACHE);
504 if (bdrv_is_read_only(s->bs))
505 features |= 1 << VIRTIO_BLK_F_RO;
507 return features;
510 static void virtio_blk_save(QEMUFile *f, void *opaque)
512 VirtIOBlock *s = opaque;
513 VirtIOBlockReq *req = s->rq;
515 virtio_save(&s->vdev, f);
517 while (req) {
518 qemu_put_sbyte(f, 1);
519 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
520 req = req->next;
522 qemu_put_sbyte(f, 0);
525 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
527 VirtIOBlock *s = opaque;
529 if (version_id != 2)
530 return -EINVAL;
532 virtio_load(&s->vdev, f);
533 while (qemu_get_sbyte(f)) {
534 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
535 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
536 req->next = s->rq;
537 s->rq = req;
539 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
540 req->elem.in_num, 1);
541 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
542 req->elem.out_num, 0);
545 return 0;
548 static void virtio_blk_resize(void *opaque)
550 VirtIOBlock *s = opaque;
552 virtio_notify_config(&s->vdev);
555 static const BlockDevOps virtio_block_ops = {
556 .resize_cb = virtio_blk_resize,
559 VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
560 char **serial)
562 VirtIOBlock *s;
563 int cylinders, heads, secs;
564 static int virtio_blk_id;
565 DriveInfo *dinfo;
567 if (!conf->bs) {
568 error_report("virtio-blk-pci: drive property not set");
569 return NULL;
571 if (!bdrv_is_inserted(conf->bs)) {
572 error_report("Device needs media, but drive is empty");
573 return NULL;
576 if (!*serial) {
577 /* try to fall back to value set with legacy -drive serial=... */
578 dinfo = drive_get_by_blockdev(conf->bs);
579 if (*dinfo->serial) {
580 *serial = strdup(dinfo->serial);
584 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
585 sizeof(struct virtio_blk_config),
586 sizeof(VirtIOBlock));
588 s->vdev.get_config = virtio_blk_update_config;
589 s->vdev.get_features = virtio_blk_get_features;
590 s->vdev.reset = virtio_blk_reset;
591 s->bs = conf->bs;
592 s->conf = conf;
593 s->serial = *serial;
594 s->rq = NULL;
595 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
596 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
598 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
600 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
601 s->qdev = dev;
602 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
603 virtio_blk_save, virtio_blk_load, s);
604 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
605 bdrv_set_buffer_alignment(s->bs, conf->logical_block_size);
607 bdrv_iostatus_enable(s->bs);
608 add_boot_device_path(conf->bootindex, dev, "/disk@0,0");
610 return &s->vdev;
613 void virtio_blk_exit(VirtIODevice *vdev)
615 VirtIOBlock *s = to_virtio_blk(vdev);
616 unregister_savevm(s->qdev, "virtio-blk", s);
617 virtio_cleanup(vdev);