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