Merge remote branch 'origin/master' into staging
[qemu.git] / hw / virtio-blk.c
blobf50069d20b05d521a8102115140b8dd95098b284
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 "virtio-blk.h"
17 #ifdef __linux__
18 # include <scsi/sg.h>
19 #endif
21 typedef struct VirtIOBlock
23 VirtIODevice vdev;
24 BlockDriverState *bs;
25 VirtQueue *vq;
26 void *rq;
27 QEMUBH *bh;
28 BlockConf *conf;
29 unsigned short sector_mask;
30 char sn[BLOCK_SERIAL_STRLEN];
31 } VirtIOBlock;
33 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
35 return (VirtIOBlock *)vdev;
38 typedef struct VirtIOBlockReq
40 VirtIOBlock *dev;
41 VirtQueueElement elem;
42 struct virtio_blk_inhdr *in;
43 struct virtio_blk_outhdr *out;
44 struct virtio_scsi_inhdr *scsi;
45 QEMUIOVector qiov;
46 struct VirtIOBlockReq *next;
47 } VirtIOBlockReq;
49 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
51 VirtIOBlock *s = req->dev;
53 req->in->status = status;
54 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
55 virtio_notify(&s->vdev, s->vq);
57 qemu_free(req);
60 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
61 int is_read)
63 BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
64 VirtIOBlock *s = req->dev;
66 if (action == BLOCK_ERR_IGNORE) {
67 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
68 return 0;
71 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
72 || action == BLOCK_ERR_STOP_ANY) {
73 req->next = s->rq;
74 s->rq = req;
75 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
76 vm_stop(0);
77 } else {
78 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
79 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
82 return 1;
85 static void virtio_blk_rw_complete(void *opaque, int ret)
87 VirtIOBlockReq *req = opaque;
89 if (ret) {
90 int is_read = !(req->out->type & VIRTIO_BLK_T_OUT);
91 if (virtio_blk_handle_rw_error(req, -ret, is_read))
92 return;
95 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
98 static void virtio_blk_flush_complete(void *opaque, int ret)
100 VirtIOBlockReq *req = opaque;
102 virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK);
105 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
107 VirtIOBlockReq *req = qemu_malloc(sizeof(*req));
108 req->dev = s;
109 req->qiov.size = 0;
110 req->next = NULL;
111 return req;
114 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
116 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
118 if (req != NULL) {
119 if (!virtqueue_pop(s->vq, &req->elem)) {
120 qemu_free(req);
121 return NULL;
125 return req;
128 #ifdef __linux__
129 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
131 struct sg_io_hdr hdr;
132 int ret;
133 int status;
134 int i;
137 * We require at least one output segment each for the virtio_blk_outhdr
138 * and the SCSI command block.
140 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
141 * and the sense buffer pointer in the input segments.
143 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
144 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
145 return;
149 * No support for bidirection commands yet.
151 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
152 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
153 return;
157 * The scsi inhdr is placed in the second-to-last input segment, just
158 * before the regular inhdr.
160 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
162 memset(&hdr, 0, sizeof(struct sg_io_hdr));
163 hdr.interface_id = 'S';
164 hdr.cmd_len = req->elem.out_sg[1].iov_len;
165 hdr.cmdp = req->elem.out_sg[1].iov_base;
166 hdr.dxfer_len = 0;
168 if (req->elem.out_num > 2) {
170 * If there are more than the minimally required 2 output segments
171 * there is write payload starting from the third iovec.
173 hdr.dxfer_direction = SG_DXFER_TO_DEV;
174 hdr.iovec_count = req->elem.out_num - 2;
176 for (i = 0; i < hdr.iovec_count; i++)
177 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
179 hdr.dxferp = req->elem.out_sg + 2;
181 } else if (req->elem.in_num > 3) {
183 * If we have more than 3 input segments the guest wants to actually
184 * read data.
186 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
187 hdr.iovec_count = req->elem.in_num - 3;
188 for (i = 0; i < hdr.iovec_count; i++)
189 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
191 hdr.dxferp = req->elem.in_sg;
192 } else {
194 * Some SCSI commands don't actually transfer any data.
196 hdr.dxfer_direction = SG_DXFER_NONE;
199 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
200 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
202 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
203 if (ret) {
204 status = VIRTIO_BLK_S_UNSUPP;
205 hdr.status = ret;
206 hdr.resid = hdr.dxfer_len;
207 } else if (hdr.status) {
208 status = VIRTIO_BLK_S_IOERR;
209 } else {
210 status = VIRTIO_BLK_S_OK;
213 req->scsi->errors = hdr.status;
214 req->scsi->residual = hdr.resid;
215 req->scsi->sense_len = hdr.sb_len_wr;
216 req->scsi->data_len = hdr.dxfer_len;
218 virtio_blk_req_complete(req, status);
220 #else
221 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
223 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
225 #endif /* __linux__ */
227 typedef struct MultiReqBuffer {
228 BlockRequest blkreq[32];
229 unsigned int num_writes;
230 } MultiReqBuffer;
232 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
234 int i, ret;
236 if (!mrb->num_writes) {
237 return;
240 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
241 if (ret != 0) {
242 for (i = 0; i < mrb->num_writes; i++) {
243 if (mrb->blkreq[i].error) {
244 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
249 mrb->num_writes = 0;
252 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
254 BlockDriverAIOCB *acb;
257 * Make sure all outstanding writes are posted to the backing device.
259 virtio_submit_multiwrite(req->dev->bs, mrb);
261 acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
262 if (!acb) {
263 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
267 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
269 BlockRequest *blkreq;
271 if (req->out->sector & req->dev->sector_mask) {
272 virtio_blk_rw_complete(req, -EIO);
273 return;
276 if (mrb->num_writes == 32) {
277 virtio_submit_multiwrite(req->dev->bs, mrb);
280 blkreq = &mrb->blkreq[mrb->num_writes];
281 blkreq->sector = req->out->sector;
282 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
283 blkreq->qiov = &req->qiov;
284 blkreq->cb = virtio_blk_rw_complete;
285 blkreq->opaque = req;
286 blkreq->error = 0;
288 mrb->num_writes++;
291 static void virtio_blk_handle_read(VirtIOBlockReq *req)
293 BlockDriverAIOCB *acb;
295 if (req->out->sector & req->dev->sector_mask) {
296 virtio_blk_rw_complete(req, -EIO);
297 return;
300 acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
301 req->qiov.size / BDRV_SECTOR_SIZE,
302 virtio_blk_rw_complete, req);
303 if (!acb) {
304 virtio_blk_rw_complete(req, -EIO);
308 static void virtio_blk_handle_request(VirtIOBlockReq *req,
309 MultiReqBuffer *mrb)
311 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
312 fprintf(stderr, "virtio-blk missing headers\n");
313 exit(1);
316 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
317 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
318 fprintf(stderr, "virtio-blk header not in correct element\n");
319 exit(1);
322 req->out = (void *)req->elem.out_sg[0].iov_base;
323 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
325 if (req->out->type & VIRTIO_BLK_T_FLUSH) {
326 virtio_blk_handle_flush(req, mrb);
327 } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
328 virtio_blk_handle_scsi(req);
329 } else if (req->out->type & VIRTIO_BLK_T_GET_ID) {
330 VirtIOBlock *s = req->dev;
332 memcpy(req->elem.in_sg[0].iov_base, s->sn,
333 MIN(req->elem.in_sg[0].iov_len, sizeof(s->sn)));
334 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
335 } else if (req->out->type & VIRTIO_BLK_T_OUT) {
336 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
337 req->elem.out_num - 1);
338 virtio_blk_handle_write(req, mrb);
339 } else {
340 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
341 req->elem.in_num - 1);
342 virtio_blk_handle_read(req);
346 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
348 VirtIOBlock *s = to_virtio_blk(vdev);
349 VirtIOBlockReq *req;
350 MultiReqBuffer mrb = {
351 .num_writes = 0,
354 while ((req = virtio_blk_get_request(s))) {
355 virtio_blk_handle_request(req, &mrb);
358 virtio_submit_multiwrite(s->bs, &mrb);
361 * FIXME: Want to check for completions before returning to guest mode,
362 * so cached reads and writes are reported as quickly as possible. But
363 * that should be done in the generic block layer.
367 static void virtio_blk_dma_restart_bh(void *opaque)
369 VirtIOBlock *s = opaque;
370 VirtIOBlockReq *req = s->rq;
371 MultiReqBuffer mrb = {
372 .num_writes = 0,
375 qemu_bh_delete(s->bh);
376 s->bh = NULL;
378 s->rq = NULL;
380 while (req) {
381 virtio_blk_handle_request(req, &mrb);
382 req = req->next;
385 virtio_submit_multiwrite(s->bs, &mrb);
388 static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
390 VirtIOBlock *s = opaque;
392 if (!running)
393 return;
395 if (!s->bh) {
396 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
397 qemu_bh_schedule(s->bh);
401 static void virtio_blk_reset(VirtIODevice *vdev)
404 * This should cancel pending requests, but can't do nicely until there
405 * are per-device request lists.
407 qemu_aio_flush();
410 /* coalesce internal state, copy to pci i/o region 0
412 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
414 VirtIOBlock *s = to_virtio_blk(vdev);
415 struct virtio_blk_config blkcfg;
416 uint64_t capacity;
417 int cylinders, heads, secs;
419 bdrv_get_geometry(s->bs, &capacity);
420 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
421 memset(&blkcfg, 0, sizeof(blkcfg));
422 stq_raw(&blkcfg.capacity, capacity);
423 stl_raw(&blkcfg.seg_max, 128 - 2);
424 stw_raw(&blkcfg.cylinders, cylinders);
425 blkcfg.heads = heads;
426 blkcfg.sectors = secs & ~s->sector_mask;
427 blkcfg.blk_size = s->conf->logical_block_size;
428 blkcfg.size_max = 0;
429 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
430 blkcfg.alignment_offset = 0;
431 blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size;
432 blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size;
433 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
436 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
438 VirtIOBlock *s = to_virtio_blk(vdev);
440 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
441 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
442 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
443 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
445 if (bdrv_enable_write_cache(s->bs))
446 features |= (1 << VIRTIO_BLK_F_WCACHE);
448 if (bdrv_is_read_only(s->bs))
449 features |= 1 << VIRTIO_BLK_F_RO;
451 return features;
454 static void virtio_blk_save(QEMUFile *f, void *opaque)
456 VirtIOBlock *s = opaque;
457 VirtIOBlockReq *req = s->rq;
459 virtio_save(&s->vdev, f);
461 while (req) {
462 qemu_put_sbyte(f, 1);
463 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
464 req = req->next;
466 qemu_put_sbyte(f, 0);
469 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
471 VirtIOBlock *s = opaque;
473 if (version_id != 2)
474 return -EINVAL;
476 virtio_load(&s->vdev, f);
477 while (qemu_get_sbyte(f)) {
478 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
479 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
480 req->next = s->rq;
481 s->rq = req;
484 return 0;
487 VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf)
489 VirtIOBlock *s;
490 int cylinders, heads, secs;
491 static int virtio_blk_id;
492 DriveInfo *dinfo;
494 if (!conf->bs) {
495 error_report("virtio-blk-pci: drive property not set");
496 return NULL;
498 if (!bdrv_is_inserted(conf->bs)) {
499 error_report("Device needs media, but drive is empty");
500 return NULL;
503 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
504 sizeof(struct virtio_blk_config),
505 sizeof(VirtIOBlock));
507 s->vdev.get_config = virtio_blk_update_config;
508 s->vdev.get_features = virtio_blk_get_features;
509 s->vdev.reset = virtio_blk_reset;
510 s->bs = conf->bs;
511 s->conf = conf;
512 s->rq = NULL;
513 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
514 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
516 /* NB: per existing s/n string convention the string is terminated
517 * by '\0' only when less than sizeof (s->sn)
519 dinfo = drive_get_by_blockdev(s->bs);
520 strncpy(s->sn, dinfo->serial, sizeof (s->sn));
522 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
524 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
525 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
526 virtio_blk_save, virtio_blk_load, s);
527 bdrv_set_removable(s->bs, 0);
529 return &s->vdev;