1
2
3
4
5
6
7
8
9
10
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#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
21#include "hw/dataplane/virtio-blk.h"
22#endif
23#include "scsi-defs.h"
24#ifdef __linux__
25# include <scsi/sg.h>
26#endif
27
28typedef struct VirtIOBlock
29{
30 VirtIODevice vdev;
31 BlockDriverState *bs;
32 VirtQueue *vq;
33 void *rq;
34 QEMUBH *bh;
35 BlockConf *conf;
36 VirtIOBlkConf *blk;
37 unsigned short sector_mask;
38 DeviceState *qdev;
39 VMChangeStateEntry *change;
40#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
41 VirtIOBlockDataPlane *dataplane;
42#endif
43} VirtIOBlock;
44
45static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
46{
47 return (VirtIOBlock *)vdev;
48}
49
50typedef struct VirtIOBlockReq
51{
52 VirtIOBlock *dev;
53 VirtQueueElement elem;
54 struct virtio_blk_inhdr *in;
55 struct virtio_blk_outhdr *out;
56 struct virtio_scsi_inhdr *scsi;
57 QEMUIOVector qiov;
58 struct VirtIOBlockReq *next;
59 BlockAcctCookie acct;
60} VirtIOBlockReq;
61
62static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
63{
64 VirtIOBlock *s = req->dev;
65
66 trace_virtio_blk_req_complete(req, status);
67
68 stb_p(&req->in->status, status);
69 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
70 virtio_notify(&s->vdev, s->vq);
71}
72
73static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
74 bool is_read)
75{
76 BlockErrorAction action = bdrv_get_error_action(req->dev->bs, is_read, error);
77 VirtIOBlock *s = req->dev;
78
79 if (action == BDRV_ACTION_STOP) {
80 req->next = s->rq;
81 s->rq = req;
82 } else if (action == BDRV_ACTION_REPORT) {
83 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
84 bdrv_acct_done(s->bs, &req->acct);
85 g_free(req);
86 }
87
88 bdrv_error_action(s->bs, action, is_read, error);
89 return action != BDRV_ACTION_IGNORE;
90}
91
92static void virtio_blk_rw_complete(void *opaque, int ret)
93{
94 VirtIOBlockReq *req = opaque;
95
96 trace_virtio_blk_rw_complete(req, ret);
97
98 if (ret) {
99 bool is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
100 if (virtio_blk_handle_rw_error(req, -ret, is_read))
101 return;
102 }
103
104 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
105 bdrv_acct_done(req->dev->bs, &req->acct);
106 g_free(req);
107}
108
109static void virtio_blk_flush_complete(void *opaque, int ret)
110{
111 VirtIOBlockReq *req = opaque;
112
113 if (ret) {
114 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
115 return;
116 }
117 }
118
119 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
120 bdrv_acct_done(req->dev->bs, &req->acct);
121 g_free(req);
122}
123
124static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
125{
126 VirtIOBlockReq *req = g_malloc(sizeof(*req));
127 req->dev = s;
128 req->qiov.size = 0;
129 req->next = NULL;
130 return req;
131}
132
133static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
134{
135 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
136
137 if (req != NULL) {
138 if (!virtqueue_pop(s->vq, &req->elem)) {
139 g_free(req);
140 return NULL;
141 }
142 }
143
144 return req;
145}
146
147static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
148{
149#ifdef __linux__
150 int ret;
151 int i;
152#endif
153 int status = VIRTIO_BLK_S_OK;
154
155
156
157
158
159
160
161
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;
166 }
167
168
169
170
171
172 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
173
174 if (!req->dev->blk->scsi) {
175 status = VIRTIO_BLK_S_UNSUPP;
176 goto fail;
177 }
178
179
180
181
182 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
183 status = VIRTIO_BLK_S_UNSUPP;
184 goto fail;
185 }
186
187#ifdef __linux__
188 struct sg_io_hdr hdr;
189 memset(&hdr, 0, sizeof(struct sg_io_hdr));
190 hdr.interface_id = 'S';
191 hdr.cmd_len = req->elem.out_sg[1].iov_len;
192 hdr.cmdp = req->elem.out_sg[1].iov_base;
193 hdr.dxfer_len = 0;
194
195 if (req->elem.out_num > 2) {
196
197
198
199
200 hdr.dxfer_direction = SG_DXFER_TO_DEV;
201 hdr.iovec_count = req->elem.out_num - 2;
202
203 for (i = 0; i < hdr.iovec_count; i++)
204 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
205
206 hdr.dxferp = req->elem.out_sg + 2;
207
208 } else if (req->elem.in_num > 3) {
209
210
211
212
213 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
214 hdr.iovec_count = req->elem.in_num - 3;
215 for (i = 0; i < hdr.iovec_count; i++)
216 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
217
218 hdr.dxferp = req->elem.in_sg;
219 } else {
220
221
222
223 hdr.dxfer_direction = SG_DXFER_NONE;
224 }
225
226 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
227 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
228
229 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
230 if (ret) {
231 status = VIRTIO_BLK_S_UNSUPP;
232 goto fail;
233 }
234
235
236
237
238
239
240
241
242 if (hdr.status == 0 && hdr.sb_len_wr > 0) {
243 hdr.status = CHECK_CONDITION;
244 }
245
246 stl_p(&req->scsi->errors,
247 hdr.status | (hdr.msg_status << 8) |
248 (hdr.host_status << 16) | (hdr.driver_status << 24));
249 stl_p(&req->scsi->residual, hdr.resid);
250 stl_p(&req->scsi->sense_len, hdr.sb_len_wr);
251 stl_p(&req->scsi->data_len, hdr.dxfer_len);
252
253 virtio_blk_req_complete(req, status);
254 g_free(req);
255 return;
256#else
257 abort();
258#endif
259
260fail:
261
262 stl_p(&req->scsi->errors, 255);
263 virtio_blk_req_complete(req, status);
264 g_free(req);
265}
266
267typedef struct MultiReqBuffer {
268 BlockRequest blkreq[32];
269 unsigned int num_writes;
270} MultiReqBuffer;
271
272static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
273{
274 int i, ret;
275
276 if (!mrb->num_writes) {
277 return;
278 }
279
280 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
281 if (ret != 0) {
282 for (i = 0; i < mrb->num_writes; i++) {
283 if (mrb->blkreq[i].error) {
284 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
285 }
286 }
287 }
288
289 mrb->num_writes = 0;
290}
291
292static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
293{
294 bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
295
296
297
298
299 virtio_submit_multiwrite(req->dev->bs, mrb);
300 bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
301}
302
303static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
304{
305 BlockRequest *blkreq;
306 uint64_t sector;
307
308 sector = ldq_p(&req->out->sector);
309
310 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
311
312 trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
313
314 if (sector & req->dev->sector_mask) {
315 virtio_blk_rw_complete(req, -EIO);
316 return;
317 }
318 if (req->qiov.size % req->dev->conf->logical_block_size) {
319 virtio_blk_rw_complete(req, -EIO);
320 return;
321 }
322
323 if (mrb->num_writes == 32) {
324 virtio_submit_multiwrite(req->dev->bs, mrb);
325 }
326
327 blkreq = &mrb->blkreq[mrb->num_writes];
328 blkreq->sector = sector;
329 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
330 blkreq->qiov = &req->qiov;
331 blkreq->cb = virtio_blk_rw_complete;
332 blkreq->opaque = req;
333 blkreq->error = 0;
334
335 mrb->num_writes++;
336}
337
338static void virtio_blk_handle_read(VirtIOBlockReq *req)
339{
340 uint64_t sector;
341
342 sector = ldq_p(&req->out->sector);
343
344 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
345
346 trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
347
348 if (sector & req->dev->sector_mask) {
349 virtio_blk_rw_complete(req, -EIO);
350 return;
351 }
352 if (req->qiov.size % req->dev->conf->logical_block_size) {
353 virtio_blk_rw_complete(req, -EIO);
354 return;
355 }
356 bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
357 req->qiov.size / BDRV_SECTOR_SIZE,
358 virtio_blk_rw_complete, req);
359}
360
361static void virtio_blk_handle_request(VirtIOBlockReq *req,
362 MultiReqBuffer *mrb)
363{
364 uint32_t type;
365
366 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
367 error_report("virtio-blk missing headers");
368 exit(1);
369 }
370
371 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
372 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
373 error_report("virtio-blk header not in correct element");
374 exit(1);
375 }
376
377 req->out = (void *)req->elem.out_sg[0].iov_base;
378 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
379
380 type = ldl_p(&req->out->type);
381
382 if (type & VIRTIO_BLK_T_FLUSH) {
383 virtio_blk_handle_flush(req, mrb);
384 } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
385 virtio_blk_handle_scsi(req);
386 } else if (type & VIRTIO_BLK_T_GET_ID) {
387 VirtIOBlock *s = req->dev;
388
389
390
391
392
393 strncpy(req->elem.in_sg[0].iov_base,
394 s->blk->serial ? s->blk->serial : "",
395 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
396 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
397 g_free(req);
398 } else if (type & VIRTIO_BLK_T_OUT) {
399 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
400 req->elem.out_num - 1);
401 virtio_blk_handle_write(req, mrb);
402 } else if (type == VIRTIO_BLK_T_IN || type == VIRTIO_BLK_T_BARRIER) {
403
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 } else {
408 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
409 g_free(req);
410 }
411}
412
413static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
414{
415 VirtIOBlock *s = to_virtio_blk(vdev);
416 VirtIOBlockReq *req;
417 MultiReqBuffer mrb = {
418 .num_writes = 0,
419 };
420
421#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
422
423
424
425 if (s->dataplane) {
426 virtio_blk_data_plane_start(s->dataplane);
427 return;
428 }
429#endif
430
431 while ((req = virtio_blk_get_request(s))) {
432 virtio_blk_handle_request(req, &mrb);
433 }
434
435 virtio_submit_multiwrite(s->bs, &mrb);
436
437
438
439
440
441
442}
443
444static void virtio_blk_dma_restart_bh(void *opaque)
445{
446 VirtIOBlock *s = opaque;
447 VirtIOBlockReq *req = s->rq;
448 MultiReqBuffer mrb = {
449 .num_writes = 0,
450 };
451
452 qemu_bh_delete(s->bh);
453 s->bh = NULL;
454
455 s->rq = NULL;
456
457 while (req) {
458 virtio_blk_handle_request(req, &mrb);
459 req = req->next;
460 }
461
462 virtio_submit_multiwrite(s->bs, &mrb);
463}
464
465static void virtio_blk_dma_restart_cb(void *opaque, int running,
466 RunState state)
467{
468 VirtIOBlock *s = opaque;
469
470 if (!running) {
471 return;
472 }
473
474 if (!s->bh) {
475 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
476 qemu_bh_schedule(s->bh);
477 }
478}
479
480static void virtio_blk_reset(VirtIODevice *vdev)
481{
482#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
483 VirtIOBlock *s = to_virtio_blk(vdev);
484
485 if (s->dataplane) {
486 virtio_blk_data_plane_stop(s->dataplane);
487 }
488#endif
489
490
491
492
493
494 bdrv_drain_all();
495}
496
497
498
499static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
500{
501 VirtIOBlock *s = to_virtio_blk(vdev);
502 struct virtio_blk_config blkcfg;
503 uint64_t capacity;
504 int blk_size = s->conf->logical_block_size;
505
506 bdrv_get_geometry(s->bs, &capacity);
507 memset(&blkcfg, 0, sizeof(blkcfg));
508 stq_raw(&blkcfg.capacity, capacity);
509 stl_raw(&blkcfg.seg_max, 128 - 2);
510 stw_raw(&blkcfg.cylinders, s->conf->cyls);
511 stl_raw(&blkcfg.blk_size, blk_size);
512 stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
513 stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
514 blkcfg.heads = s->conf->heads;
515
516
517
518
519
520
521
522
523
524
525
526 if (bdrv_getlength(s->bs) / s->conf->heads / s->conf->secs % blk_size) {
527 blkcfg.sectors = s->conf->secs & ~s->sector_mask;
528 } else {
529 blkcfg.sectors = s->conf->secs;
530 }
531 blkcfg.size_max = 0;
532 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
533 blkcfg.alignment_offset = 0;
534 blkcfg.wce = bdrv_enable_write_cache(s->bs);
535 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
536}
537
538static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
539{
540 VirtIOBlock *s = to_virtio_blk(vdev);
541 struct virtio_blk_config blkcfg;
542
543 memcpy(&blkcfg, config, sizeof(blkcfg));
544 bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
545}
546
547static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
548{
549 VirtIOBlock *s = to_virtio_blk(vdev);
550
551 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
552 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
553 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
554 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
555 features |= (1 << VIRTIO_BLK_F_SCSI);
556
557 if (s->blk->config_wce) {
558 features |= (1 << VIRTIO_BLK_F_CONFIG_WCE);
559 }
560 if (bdrv_enable_write_cache(s->bs))
561 features |= (1 << VIRTIO_BLK_F_WCE);
562
563 if (bdrv_is_read_only(s->bs))
564 features |= 1 << VIRTIO_BLK_F_RO;
565
566 return features;
567}
568
569static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
570{
571 VirtIOBlock *s = to_virtio_blk(vdev);
572 uint32_t features;
573
574#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
575 if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER |
576 VIRTIO_CONFIG_S_DRIVER_OK))) {
577 virtio_blk_data_plane_stop(s->dataplane);
578 }
579#endif
580
581 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
582 return;
583 }
584
585 features = vdev->guest_features;
586 bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE)));
587}
588
589static void virtio_blk_save(QEMUFile *f, void *opaque)
590{
591 VirtIOBlock *s = opaque;
592 VirtIOBlockReq *req = s->rq;
593
594 virtio_save(&s->vdev, f);
595
596 while (req) {
597 qemu_put_sbyte(f, 1);
598 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
599 req = req->next;
600 }
601 qemu_put_sbyte(f, 0);
602}
603
604static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
605{
606 VirtIOBlock *s = opaque;
607 int ret;
608
609 if (version_id != 2)
610 return -EINVAL;
611
612 ret = virtio_load(&s->vdev, f);
613 if (ret) {
614 return ret;
615 }
616
617 while (qemu_get_sbyte(f)) {
618 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
619 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
620 req->next = s->rq;
621 s->rq = req;
622
623 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
624 req->elem.in_num, 1);
625 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
626 req->elem.out_num, 0);
627 }
628
629 return 0;
630}
631
632static void virtio_blk_resize(void *opaque)
633{
634 VirtIOBlock *s = opaque;
635
636 virtio_notify_config(&s->vdev);
637}
638
639static const BlockDevOps virtio_block_ops = {
640 .resize_cb = virtio_blk_resize,
641};
642
643VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
644{
645 VirtIOBlock *s;
646 static int virtio_blk_id;
647
648 if (!blk->conf.bs) {
649 error_report("drive property not set");
650 return NULL;
651 }
652 if (!bdrv_is_inserted(blk->conf.bs)) {
653 error_report("Device needs media, but drive is empty");
654 return NULL;
655 }
656
657 blkconf_serial(&blk->conf, &blk->serial);
658 if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
659 return NULL;
660 }
661
662 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
663 sizeof(struct virtio_blk_config),
664 sizeof(VirtIOBlock));
665
666 s->vdev.get_config = virtio_blk_update_config;
667 s->vdev.set_config = virtio_blk_set_config;
668 s->vdev.get_features = virtio_blk_get_features;
669 s->vdev.set_status = virtio_blk_set_status;
670 s->vdev.reset = virtio_blk_reset;
671 s->bs = blk->conf.bs;
672 s->conf = &blk->conf;
673 s->blk = blk;
674 s->rq = NULL;
675 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
676
677 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
678#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
679 if (!virtio_blk_data_plane_create(&s->vdev, blk, &s->dataplane)) {
680 virtio_cleanup(&s->vdev);
681 return NULL;
682 }
683#endif
684
685 s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
686 s->qdev = dev;
687 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
688 virtio_blk_save, virtio_blk_load, s);
689 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
690 bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size);
691
692 bdrv_iostatus_enable(s->bs);
693 add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
694
695 return &s->vdev;
696}
697
698void virtio_blk_exit(VirtIODevice *vdev)
699{
700 VirtIOBlock *s = to_virtio_blk(vdev);
701
702#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
703 virtio_blk_data_plane_destroy(s->dataplane);
704 s->dataplane = NULL;
705#endif
706 qemu_del_vm_change_state_handler(s->change);
707 unregister_savevm(s->qdev, "virtio-blk", s);
708 blockdev_mark_auto_del(s->bs);
709 virtio_cleanup(vdev);
710}
711