1
2
3
4
5
6
7
8
9
10
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
25typedef 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
38static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
39{
40 return (VirtIOBlock *)vdev;
41}
42
43typedef 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
55static 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
66static 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
85static 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
102static 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
117static 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
126static 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
140static 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
150
151
152
153
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
163
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
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
191
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
204
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
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
230
231
232
233
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
253fail:
254
255 stl_p(&req->scsi->errors, 255);
256 virtio_blk_req_complete(req, status);
257 g_free(req);
258}
259
260typedef struct MultiReqBuffer {
261 BlockRequest blkreq[32];
262 unsigned int num_writes;
263} MultiReqBuffer;
264
265static 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
285static 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
291
292 virtio_submit_multiwrite(req->dev->bs, mrb);
293 bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
294}
295
296static 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
331static 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
354static 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
384
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
402static 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
418
419
420
421}
422
423static 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
444static 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
458static void virtio_blk_reset(VirtIODevice *vdev)
459{
460
461
462
463
464 bdrv_drain_all();
465}
466
467
468
469static 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
487
488
489
490
491
492
493
494
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
508static 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
517static 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_WCE);
529
530 if (bdrv_is_read_only(s->bs))
531 features |= 1 << VIRTIO_BLK_F_RO;
532
533 return features;
534}
535
536static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
537{
538 VirtIOBlock *s = to_virtio_blk(vdev);
539 uint32_t features;
540
541 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
542 return;
543 }
544
545 features = vdev->guest_features;
546 bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE)));
547}
548
549static void virtio_blk_save(QEMUFile *f, void *opaque)
550{
551 VirtIOBlock *s = opaque;
552 VirtIOBlockReq *req = s->rq;
553
554 virtio_save(&s->vdev, f);
555
556 while (req) {
557 qemu_put_sbyte(f, 1);
558 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
559 req = req->next;
560 }
561 qemu_put_sbyte(f, 0);
562}
563
564static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
565{
566 VirtIOBlock *s = opaque;
567 int ret;
568
569 if (version_id != 2)
570 return -EINVAL;
571
572 ret = virtio_load(&s->vdev, f);
573 if (ret) {
574 return ret;
575 }
576
577 while (qemu_get_sbyte(f)) {
578 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
579 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
580 req->next = s->rq;
581 s->rq = req;
582
583 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
584 req->elem.in_num, 1);
585 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
586 req->elem.out_num, 0);
587 }
588
589 return 0;
590}
591
592static void virtio_blk_resize(void *opaque)
593{
594 VirtIOBlock *s = opaque;
595
596 virtio_notify_config(&s->vdev);
597}
598
599static const BlockDevOps virtio_block_ops = {
600 .resize_cb = virtio_blk_resize,
601};
602
603VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
604{
605 VirtIOBlock *s;
606 static int virtio_blk_id;
607
608 if (!blk->conf.bs) {
609 error_report("drive property not set");
610 return NULL;
611 }
612 if (!bdrv_is_inserted(blk->conf.bs)) {
613 error_report("Device needs media, but drive is empty");
614 return NULL;
615 }
616
617 blkconf_serial(&blk->conf, &blk->serial);
618 if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
619 return NULL;
620 }
621
622 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
623 sizeof(struct virtio_blk_config),
624 sizeof(VirtIOBlock));
625
626 s->vdev.get_config = virtio_blk_update_config;
627 s->vdev.set_config = virtio_blk_set_config;
628 s->vdev.get_features = virtio_blk_get_features;
629 s->vdev.set_status = virtio_blk_set_status;
630 s->vdev.reset = virtio_blk_reset;
631 s->bs = blk->conf.bs;
632 s->conf = &blk->conf;
633 s->blk = blk;
634 s->rq = NULL;
635 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
636
637 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
638
639 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
640 s->qdev = dev;
641 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
642 virtio_blk_save, virtio_blk_load, s);
643 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
644 bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size);
645
646 bdrv_iostatus_enable(s->bs);
647 add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
648
649 return &s->vdev;
650}
651
652void virtio_blk_exit(VirtIODevice *vdev)
653{
654 VirtIOBlock *s = to_virtio_blk(vdev);
655 unregister_savevm(s->qdev, "virtio-blk", s);
656 blockdev_mark_auto_del(s->bs);
657 virtio_cleanup(vdev);
658}
659