Fix boot once option
[qemu/ar7.git] / hw / virtio-blk.c
blobb05d15ecdddf0b2eb68aea15f9149b863e01b34c
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 <sysemu.h>
16 #include "virtio-blk.h"
17 #include "block_int.h"
18 #ifdef __linux__
19 # include <scsi/sg.h>
20 #endif
22 typedef struct VirtIOBlock
24 VirtIODevice vdev;
25 BlockDriverState *bs;
26 VirtQueue *vq;
27 void *rq;
28 QEMUBH *bh;
29 BlockConf *conf;
30 unsigned short sector_mask;
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 BlockInterfaceErrorAction action =
64 drive_get_on_error(req->dev->bs, is_read);
65 VirtIOBlock *s = req->dev;
67 if (action == BLOCK_ERR_IGNORE) {
68 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
69 return 0;
72 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
73 || action == BLOCK_ERR_STOP_ANY) {
74 req->next = s->rq;
75 s->rq = req;
76 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
77 vm_stop(0);
78 } else {
79 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
80 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
83 return 1;
86 static void virtio_blk_rw_complete(void *opaque, int ret)
88 VirtIOBlockReq *req = opaque;
90 if (ret) {
91 int is_read = !(req->out->type & VIRTIO_BLK_T_OUT);
92 if (virtio_blk_handle_rw_error(req, -ret, is_read))
93 return;
96 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
99 static void virtio_blk_flush_complete(void *opaque, int ret)
101 VirtIOBlockReq *req = opaque;
103 virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK);
106 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
108 VirtIOBlockReq *req = qemu_mallocz(sizeof(*req));
109 req->dev = s;
110 return req;
113 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
115 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
117 if (req != NULL) {
118 if (!virtqueue_pop(s->vq, &req->elem)) {
119 qemu_free(req);
120 return NULL;
124 return req;
127 #ifdef __linux__
128 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
130 struct sg_io_hdr hdr;
131 int ret;
132 int status;
133 int i;
136 * We require at least one output segment each for the virtio_blk_outhdr
137 * and the SCSI command block.
139 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
140 * and the sense buffer pointer in the input segments.
142 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
143 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
144 return;
148 * No support for bidirection commands yet.
150 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
151 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
152 return;
156 * The scsi inhdr is placed in the second-to-last input segment, just
157 * before the regular inhdr.
159 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
161 memset(&hdr, 0, sizeof(struct sg_io_hdr));
162 hdr.interface_id = 'S';
163 hdr.cmd_len = req->elem.out_sg[1].iov_len;
164 hdr.cmdp = req->elem.out_sg[1].iov_base;
165 hdr.dxfer_len = 0;
167 if (req->elem.out_num > 2) {
169 * If there are more than the minimally required 2 output segments
170 * there is write payload starting from the third iovec.
172 hdr.dxfer_direction = SG_DXFER_TO_DEV;
173 hdr.iovec_count = req->elem.out_num - 2;
175 for (i = 0; i < hdr.iovec_count; i++)
176 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
178 hdr.dxferp = req->elem.out_sg + 2;
180 } else if (req->elem.in_num > 3) {
182 * If we have more than 3 input segments the guest wants to actually
183 * read data.
185 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
186 hdr.iovec_count = req->elem.in_num - 3;
187 for (i = 0; i < hdr.iovec_count; i++)
188 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
190 hdr.dxferp = req->elem.in_sg;
191 } else {
193 * Some SCSI commands don't actually transfer any data.
195 hdr.dxfer_direction = SG_DXFER_NONE;
198 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
199 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
201 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
202 if (ret) {
203 status = VIRTIO_BLK_S_UNSUPP;
204 hdr.status = ret;
205 hdr.resid = hdr.dxfer_len;
206 } else if (hdr.status) {
207 status = VIRTIO_BLK_S_IOERR;
208 } else {
209 status = VIRTIO_BLK_S_OK;
212 req->scsi->errors = hdr.status;
213 req->scsi->residual = hdr.resid;
214 req->scsi->sense_len = hdr.sb_len_wr;
215 req->scsi->data_len = hdr.dxfer_len;
217 virtio_blk_req_complete(req, status);
219 #else
220 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
222 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
224 #endif /* __linux__ */
226 static void do_multiwrite(BlockDriverState *bs, BlockRequest *blkreq,
227 int num_writes)
229 int i, ret;
230 ret = bdrv_aio_multiwrite(bs, blkreq, num_writes);
232 if (ret != 0) {
233 for (i = 0; i < num_writes; i++) {
234 if (blkreq[i].error) {
235 virtio_blk_rw_complete(blkreq[i].opaque, -EIO);
241 static void virtio_blk_handle_flush(VirtIOBlockReq *req)
243 BlockDriverAIOCB *acb;
245 acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
246 if (!acb) {
247 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
251 static void virtio_blk_handle_write(BlockRequest *blkreq, int *num_writes,
252 VirtIOBlockReq *req, BlockDriverState **old_bs)
254 if (req->out->sector & req->dev->sector_mask) {
255 virtio_blk_rw_complete(req, -EIO);
256 return;
259 if (req->dev->bs != *old_bs || *num_writes == 32) {
260 if (*old_bs != NULL) {
261 do_multiwrite(*old_bs, blkreq, *num_writes);
263 *num_writes = 0;
264 *old_bs = req->dev->bs;
267 blkreq[*num_writes].sector = req->out->sector;
268 blkreq[*num_writes].nb_sectors = req->qiov.size / 512;
269 blkreq[*num_writes].qiov = &req->qiov;
270 blkreq[*num_writes].cb = virtio_blk_rw_complete;
271 blkreq[*num_writes].opaque = req;
272 blkreq[*num_writes].error = 0;
274 (*num_writes)++;
277 static void virtio_blk_handle_read(VirtIOBlockReq *req)
279 BlockDriverAIOCB *acb;
281 if (req->out->sector & req->dev->sector_mask) {
282 virtio_blk_rw_complete(req, -EIO);
283 return;
286 acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
287 req->qiov.size / 512, virtio_blk_rw_complete, req);
288 if (!acb) {
289 virtio_blk_rw_complete(req, -EIO);
293 typedef struct MultiReqBuffer {
294 BlockRequest blkreq[32];
295 int num_writes;
296 BlockDriverState *old_bs;
297 } MultiReqBuffer;
299 static void virtio_blk_handle_request(VirtIOBlockReq *req,
300 MultiReqBuffer *mrb)
302 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
303 fprintf(stderr, "virtio-blk missing headers\n");
304 exit(1);
307 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
308 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
309 fprintf(stderr, "virtio-blk header not in correct element\n");
310 exit(1);
313 req->out = (void *)req->elem.out_sg[0].iov_base;
314 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
316 if (req->out->type & VIRTIO_BLK_T_FLUSH) {
317 virtio_blk_handle_flush(req);
318 } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
319 virtio_blk_handle_scsi(req);
320 } else if (req->out->type & VIRTIO_BLK_T_OUT) {
321 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
322 req->elem.out_num - 1);
323 virtio_blk_handle_write(mrb->blkreq, &mrb->num_writes,
324 req, &mrb->old_bs);
325 } else {
326 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
327 req->elem.in_num - 1);
328 virtio_blk_handle_read(req);
332 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
334 VirtIOBlock *s = to_virtio_blk(vdev);
335 VirtIOBlockReq *req;
336 MultiReqBuffer mrb = {
337 .num_writes = 0,
338 .old_bs = NULL,
341 while ((req = virtio_blk_get_request(s))) {
342 virtio_blk_handle_request(req, &mrb);
345 if (mrb.num_writes > 0) {
346 do_multiwrite(mrb.old_bs, mrb.blkreq, mrb.num_writes);
350 * FIXME: Want to check for completions before returning to guest mode,
351 * so cached reads and writes are reported as quickly as possible. But
352 * that should be done in the generic block layer.
356 static void virtio_blk_dma_restart_bh(void *opaque)
358 VirtIOBlock *s = opaque;
359 VirtIOBlockReq *req = s->rq;
360 MultiReqBuffer mrb = {
361 .num_writes = 0,
362 .old_bs = NULL,
365 qemu_bh_delete(s->bh);
366 s->bh = NULL;
368 s->rq = NULL;
370 while (req) {
371 virtio_blk_handle_request(req, &mrb);
372 req = req->next;
375 if (mrb.num_writes > 0) {
376 do_multiwrite(mrb.old_bs, mrb.blkreq, mrb.num_writes);
380 static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
382 VirtIOBlock *s = opaque;
384 if (!running)
385 return;
387 if (!s->bh) {
388 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
389 qemu_bh_schedule(s->bh);
393 static void virtio_blk_reset(VirtIODevice *vdev)
396 * This should cancel pending requests, but can't do nicely until there
397 * are per-device request lists.
399 qemu_aio_flush();
402 /* coalesce internal state, copy to pci i/o region 0
404 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
406 VirtIOBlock *s = to_virtio_blk(vdev);
407 struct virtio_blk_config blkcfg;
408 uint64_t capacity;
409 int cylinders, heads, secs;
411 bdrv_get_geometry(s->bs, &capacity);
412 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
413 memset(&blkcfg, 0, sizeof(blkcfg));
414 stq_raw(&blkcfg.capacity, capacity);
415 stl_raw(&blkcfg.seg_max, 128 - 2);
416 stw_raw(&blkcfg.cylinders, cylinders);
417 blkcfg.heads = heads;
418 blkcfg.sectors = secs & ~s->sector_mask;
419 blkcfg.blk_size = s->conf->logical_block_size;
420 blkcfg.size_max = 0;
421 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
422 blkcfg.alignment_offset = 0;
423 blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size;
424 blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size;
425 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
428 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
430 VirtIOBlock *s = to_virtio_blk(vdev);
432 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
433 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
434 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
435 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
437 if (bdrv_enable_write_cache(s->bs))
438 features |= (1 << VIRTIO_BLK_F_WCACHE);
440 if (bdrv_is_read_only(s->bs))
441 features |= 1 << VIRTIO_BLK_F_RO;
443 return features;
446 static void virtio_blk_save(QEMUFile *f, void *opaque)
448 VirtIOBlock *s = opaque;
449 VirtIOBlockReq *req = s->rq;
451 virtio_save(&s->vdev, f);
453 while (req) {
454 qemu_put_sbyte(f, 1);
455 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
456 req = req->next;
458 qemu_put_sbyte(f, 0);
461 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
463 VirtIOBlock *s = opaque;
465 if (version_id != 2)
466 return -EINVAL;
468 virtio_load(&s->vdev, f);
469 while (qemu_get_sbyte(f)) {
470 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
471 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
472 req->next = s->rq;
473 s->rq = req->next;
476 return 0;
479 VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf)
481 VirtIOBlock *s;
482 int cylinders, heads, secs;
483 static int virtio_blk_id;
485 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
486 sizeof(struct virtio_blk_config),
487 sizeof(VirtIOBlock));
489 s->vdev.get_config = virtio_blk_update_config;
490 s->vdev.get_features = virtio_blk_get_features;
491 s->vdev.reset = virtio_blk_reset;
492 s->bs = conf->dinfo->bdrv;
493 s->conf = conf;
494 s->rq = NULL;
495 s->sector_mask = (s->conf->logical_block_size / 512) - 1;
496 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
498 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
500 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
501 register_savevm("virtio-blk", virtio_blk_id++, 2,
502 virtio_blk_save, virtio_blk_load, s);
504 return &s->vdev;