]> git.proxmox.com Git - mirror_qemu.git/blob - hw/virtio-blk.c
hd-geometry: Switch to uint32_t to match BlockConf
[mirror_qemu.git] / hw / virtio-blk.c
1 /*
2 * Virtio Block Device
3 *
4 * Copyright IBM, Corp. 2007
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
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
24
25 typedef struct VirtIOBlock
26 {
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;
37
38 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
39 {
40 return (VirtIOBlock *)vdev;
41 }
42
43 typedef struct VirtIOBlockReq
44 {
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;
54
55 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
56 {
57 VirtIOBlock *s = req->dev;
58
59 trace_virtio_blk_req_complete(req, status);
60
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);
64 }
65
66 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
67 int is_read)
68 {
69 BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
70 VirtIOBlock *s = req->dev;
71
72 if (action == BLOCK_ERR_IGNORE) {
73 bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_IGNORE, is_read);
74 return 0;
75 }
76
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);
89 }
90
91 return 1;
92 }
93
94 static void virtio_blk_rw_complete(void *opaque, int ret)
95 {
96 VirtIOBlockReq *req = opaque;
97
98 trace_virtio_blk_rw_complete(req, ret);
99
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;
104 }
105
106 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
107 bdrv_acct_done(req->dev->bs, &req->acct);
108 g_free(req);
109 }
110
111 static void virtio_blk_flush_complete(void *opaque, int ret)
112 {
113 VirtIOBlockReq *req = opaque;
114
115 if (ret) {
116 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
117 return;
118 }
119 }
120
121 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
122 bdrv_acct_done(req->dev->bs, &req->acct);
123 g_free(req);
124 }
125
126 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
127 {
128 VirtIOBlockReq *req = g_malloc(sizeof(*req));
129 req->dev = s;
130 req->qiov.size = 0;
131 req->next = NULL;
132 return req;
133 }
134
135 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
136 {
137 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
138
139 if (req != NULL) {
140 if (!virtqueue_pop(s->vq, &req->elem)) {
141 g_free(req);
142 return NULL;
143 }
144 }
145
146 return req;
147 }
148
149 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
150 {
151 #ifdef __linux__
152 int ret;
153 int i;
154 #endif
155 int status = VIRTIO_BLK_S_OK;
156
157 /*
158 * We require at least one output segment each for the virtio_blk_outhdr
159 * and the SCSI command block.
160 *
161 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
162 * and the sense buffer pointer in the input segments.
163 */
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;
168 }
169
170 /*
171 * The scsi inhdr is placed in the second-to-last input segment, just
172 * before the regular inhdr.
173 */
174 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
175
176 if (!req->dev->blk->scsi) {
177 status = VIRTIO_BLK_S_UNSUPP;
178 goto fail;
179 }
180
181 /*
182 * No support for bidirection commands yet.
183 */
184 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
185 status = VIRTIO_BLK_S_UNSUPP;
186 goto fail;
187 }
188
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;
196
197 if (req->elem.out_num > 2) {
198 /*
199 * If there are more than the minimally required 2 output segments
200 * there is write payload starting from the third iovec.
201 */
202 hdr.dxfer_direction = SG_DXFER_TO_DEV;
203 hdr.iovec_count = req->elem.out_num - 2;
204
205 for (i = 0; i < hdr.iovec_count; i++)
206 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
207
208 hdr.dxferp = req->elem.out_sg + 2;
209
210 } else if (req->elem.in_num > 3) {
211 /*
212 * If we have more than 3 input segments the guest wants to actually
213 * read data.
214 */
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;
219
220 hdr.dxferp = req->elem.in_sg;
221 } else {
222 /*
223 * Some SCSI commands don't actually transfer any data.
224 */
225 hdr.dxfer_direction = SG_DXFER_NONE;
226 }
227
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;
230
231 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
232 if (ret) {
233 status = VIRTIO_BLK_S_UNSUPP;
234 goto fail;
235 }
236
237 /*
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.
243 */
244 if (hdr.status == 0 && hdr.sb_len_wr > 0) {
245 hdr.status = CHECK_CONDITION;
246 }
247
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);
254
255 virtio_blk_req_complete(req, status);
256 g_free(req);
257 #else
258 abort();
259 #endif
260
261 fail:
262 /* Just put anything nonzero so that the ioctl fails in the guest. */
263 stl_p(&req->scsi->errors, 255);
264 virtio_blk_req_complete(req, status);
265 g_free(req);
266 }
267
268 typedef struct MultiReqBuffer {
269 BlockRequest blkreq[32];
270 unsigned int num_writes;
271 } MultiReqBuffer;
272
273 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
274 {
275 int i, ret;
276
277 if (!mrb->num_writes) {
278 return;
279 }
280
281 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
282 if (ret != 0) {
283 for (i = 0; i < mrb->num_writes; i++) {
284 if (mrb->blkreq[i].error) {
285 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
286 }
287 }
288 }
289
290 mrb->num_writes = 0;
291 }
292
293 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
294 {
295 bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
296
297 /*
298 * Make sure all outstanding writes are posted to the backing device.
299 */
300 virtio_submit_multiwrite(req->dev->bs, mrb);
301 bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
302 }
303
304 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
305 {
306 BlockRequest *blkreq;
307 uint64_t sector;
308
309 sector = ldq_p(&req->out->sector);
310
311 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
312
313 trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
314
315 if (sector & req->dev->sector_mask) {
316 virtio_blk_rw_complete(req, -EIO);
317 return;
318 }
319 if (req->qiov.size % req->dev->conf->logical_block_size) {
320 virtio_blk_rw_complete(req, -EIO);
321 return;
322 }
323
324 if (mrb->num_writes == 32) {
325 virtio_submit_multiwrite(req->dev->bs, mrb);
326 }
327
328 blkreq = &mrb->blkreq[mrb->num_writes];
329 blkreq->sector = sector;
330 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
331 blkreq->qiov = &req->qiov;
332 blkreq->cb = virtio_blk_rw_complete;
333 blkreq->opaque = req;
334 blkreq->error = 0;
335
336 mrb->num_writes++;
337 }
338
339 static void virtio_blk_handle_read(VirtIOBlockReq *req)
340 {
341 uint64_t sector;
342
343 sector = ldq_p(&req->out->sector);
344
345 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
346
347 trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
348
349 if (sector & req->dev->sector_mask) {
350 virtio_blk_rw_complete(req, -EIO);
351 return;
352 }
353 if (req->qiov.size % req->dev->conf->logical_block_size) {
354 virtio_blk_rw_complete(req, -EIO);
355 return;
356 }
357 bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
358 req->qiov.size / BDRV_SECTOR_SIZE,
359 virtio_blk_rw_complete, req);
360 }
361
362 static void virtio_blk_handle_request(VirtIOBlockReq *req,
363 MultiReqBuffer *mrb)
364 {
365 uint32_t type;
366
367 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
368 error_report("virtio-blk missing headers");
369 exit(1);
370 }
371
372 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
373 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
374 error_report("virtio-blk header not in correct element");
375 exit(1);
376 }
377
378 req->out = (void *)req->elem.out_sg[0].iov_base;
379 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
380
381 type = ldl_p(&req->out->type);
382
383 if (type & VIRTIO_BLK_T_FLUSH) {
384 virtio_blk_handle_flush(req, mrb);
385 } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
386 virtio_blk_handle_scsi(req);
387 } else if (type & VIRTIO_BLK_T_GET_ID) {
388 VirtIOBlock *s = req->dev;
389
390 /*
391 * NB: per existing s/n string convention the string is
392 * terminated by '\0' only when shorter than buffer.
393 */
394 strncpy(req->elem.in_sg[0].iov_base,
395 s->blk->serial ? s->blk->serial : "",
396 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
397 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
398 g_free(req);
399 } else if (type & VIRTIO_BLK_T_OUT) {
400 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
401 req->elem.out_num - 1);
402 virtio_blk_handle_write(req, mrb);
403 } else {
404 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
405 req->elem.in_num - 1);
406 virtio_blk_handle_read(req);
407 }
408 }
409
410 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
411 {
412 VirtIOBlock *s = to_virtio_blk(vdev);
413 VirtIOBlockReq *req;
414 MultiReqBuffer mrb = {
415 .num_writes = 0,
416 };
417
418 while ((req = virtio_blk_get_request(s))) {
419 virtio_blk_handle_request(req, &mrb);
420 }
421
422 virtio_submit_multiwrite(s->bs, &mrb);
423
424 /*
425 * FIXME: Want to check for completions before returning to guest mode,
426 * so cached reads and writes are reported as quickly as possible. But
427 * that should be done in the generic block layer.
428 */
429 }
430
431 static void virtio_blk_dma_restart_bh(void *opaque)
432 {
433 VirtIOBlock *s = opaque;
434 VirtIOBlockReq *req = s->rq;
435 MultiReqBuffer mrb = {
436 .num_writes = 0,
437 };
438
439 qemu_bh_delete(s->bh);
440 s->bh = NULL;
441
442 s->rq = NULL;
443
444 while (req) {
445 virtio_blk_handle_request(req, &mrb);
446 req = req->next;
447 }
448
449 virtio_submit_multiwrite(s->bs, &mrb);
450 }
451
452 static void virtio_blk_dma_restart_cb(void *opaque, int running,
453 RunState state)
454 {
455 VirtIOBlock *s = opaque;
456
457 if (!running)
458 return;
459
460 if (!s->bh) {
461 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
462 qemu_bh_schedule(s->bh);
463 }
464 }
465
466 static void virtio_blk_reset(VirtIODevice *vdev)
467 {
468 /*
469 * This should cancel pending requests, but can't do nicely until there
470 * are per-device request lists.
471 */
472 bdrv_drain_all();
473 }
474
475 /* coalesce internal state, copy to pci i/o region 0
476 */
477 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
478 {
479 VirtIOBlock *s = to_virtio_blk(vdev);
480 struct virtio_blk_config blkcfg;
481 uint64_t capacity;
482 int cylinders, heads, secs;
483 int blk_size = s->conf->logical_block_size;
484
485 bdrv_get_geometry(s->bs, &capacity);
486 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
487 memset(&blkcfg, 0, sizeof(blkcfg));
488 stq_raw(&blkcfg.capacity, capacity);
489 stl_raw(&blkcfg.seg_max, 128 - 2);
490 stw_raw(&blkcfg.cylinders, cylinders);
491 stl_raw(&blkcfg.blk_size, blk_size);
492 stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
493 stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
494 blkcfg.heads = heads;
495 /*
496 * We must ensure that the block device capacity is a multiple of
497 * the logical block size. If that is not the case, lets use
498 * sector_mask to adopt the geometry to have a correct picture.
499 * For those devices where the capacity is ok for the given geometry
500 * we dont touch the sector value of the geometry, since some devices
501 * (like s390 dasd) need a specific value. Here the capacity is already
502 * cyls*heads*secs*blk_size and the sector value is not block size
503 * divided by 512 - instead it is the amount of blk_size blocks
504 * per track (cylinder).
505 */
506 if (bdrv_getlength(s->bs) / heads / secs % blk_size) {
507 blkcfg.sectors = secs & ~s->sector_mask;
508 } else {
509 blkcfg.sectors = secs;
510 }
511 blkcfg.size_max = 0;
512 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
513 blkcfg.alignment_offset = 0;
514 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
515 }
516
517 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
518 {
519 VirtIOBlock *s = to_virtio_blk(vdev);
520
521 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
522 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
523 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
524 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
525 features |= (1 << VIRTIO_BLK_F_SCSI);
526
527 if (bdrv_enable_write_cache(s->bs))
528 features |= (1 << VIRTIO_BLK_F_WCACHE);
529
530 if (bdrv_is_read_only(s->bs))
531 features |= 1 << VIRTIO_BLK_F_RO;
532
533 return features;
534 }
535
536 static void virtio_blk_save(QEMUFile *f, void *opaque)
537 {
538 VirtIOBlock *s = opaque;
539 VirtIOBlockReq *req = s->rq;
540
541 virtio_save(&s->vdev, f);
542
543 while (req) {
544 qemu_put_sbyte(f, 1);
545 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
546 req = req->next;
547 }
548 qemu_put_sbyte(f, 0);
549 }
550
551 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
552 {
553 VirtIOBlock *s = opaque;
554 int ret;
555
556 if (version_id != 2)
557 return -EINVAL;
558
559 ret = virtio_load(&s->vdev, f);
560 if (ret) {
561 return ret;
562 }
563
564 while (qemu_get_sbyte(f)) {
565 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
566 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
567 req->next = s->rq;
568 s->rq = req;
569
570 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
571 req->elem.in_num, 1);
572 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
573 req->elem.out_num, 0);
574 }
575
576 return 0;
577 }
578
579 static void virtio_blk_resize(void *opaque)
580 {
581 VirtIOBlock *s = opaque;
582
583 virtio_notify_config(&s->vdev);
584 }
585
586 static const BlockDevOps virtio_block_ops = {
587 .resize_cb = virtio_blk_resize,
588 };
589
590 VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
591 {
592 VirtIOBlock *s;
593 uint32_t cylinders, heads, secs;
594 static int virtio_blk_id;
595 DriveInfo *dinfo;
596
597 if (!blk->conf.bs) {
598 error_report("drive property not set");
599 return NULL;
600 }
601 if (!bdrv_is_inserted(blk->conf.bs)) {
602 error_report("Device needs media, but drive is empty");
603 return NULL;
604 }
605
606 if (!blk->serial) {
607 /* try to fall back to value set with legacy -drive serial=... */
608 dinfo = drive_get_by_blockdev(blk->conf.bs);
609 if (*dinfo->serial) {
610 blk->serial = strdup(dinfo->serial);
611 }
612 }
613
614 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
615 sizeof(struct virtio_blk_config),
616 sizeof(VirtIOBlock));
617
618 s->vdev.get_config = virtio_blk_update_config;
619 s->vdev.get_features = virtio_blk_get_features;
620 s->vdev.reset = virtio_blk_reset;
621 s->bs = blk->conf.bs;
622 s->conf = &blk->conf;
623 s->blk = blk;
624 s->rq = NULL;
625 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
626 hd_geometry_guess(s->bs, &cylinders, &heads, &secs, NULL);
627
628 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
629
630 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
631 s->qdev = dev;
632 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
633 virtio_blk_save, virtio_blk_load, s);
634 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
635 bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size);
636
637 bdrv_iostatus_enable(s->bs);
638 add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
639
640 return &s->vdev;
641 }
642
643 void virtio_blk_exit(VirtIODevice *vdev)
644 {
645 VirtIOBlock *s = to_virtio_blk(vdev);
646 unregister_savevm(s->qdev, "virtio-blk", s);
647 blockdev_mark_auto_del(s->bs);
648 virtio_cleanup(vdev);
649 }