xref: /openbmc/qemu/hw/block/virtio-blk.c (revision 1b5fc0de)
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/iov.h"
16 #include "qemu/error-report.h"
17 #include "trace.h"
18 #include "hw/block/block.h"
19 #include "sysemu/blockdev.h"
20 #include "hw/virtio/virtio-blk.h"
21 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
22 # include "dataplane/virtio-blk.h"
23 # include "migration/migration.h"
24 #endif
25 #include "block/scsi.h"
26 #ifdef __linux__
27 # include <scsi/sg.h>
28 #endif
29 #include "hw/virtio/virtio-bus.h"
30 
31 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
32 {
33     VirtIOBlockReq *req = g_slice_new0(VirtIOBlockReq);
34     req->dev = s;
35     req->elem = g_slice_new0(VirtQueueElement);
36     return req;
37 }
38 
39 static void virtio_blk_free_request(VirtIOBlockReq *req)
40 {
41     if (req) {
42         g_slice_free(VirtQueueElement, req->elem);
43         g_slice_free(VirtIOBlockReq, req);
44     }
45 }
46 
47 static void virtio_blk_complete_request(VirtIOBlockReq *req,
48                                         unsigned char status)
49 {
50     VirtIOBlock *s = req->dev;
51     VirtIODevice *vdev = VIRTIO_DEVICE(s);
52 
53     trace_virtio_blk_req_complete(req, status);
54 
55     stb_p(&req->in->status, status);
56     virtqueue_push(s->vq, req->elem, req->qiov.size + sizeof(*req->in));
57     virtio_notify(vdev, s->vq);
58 }
59 
60 static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
61 {
62     req->dev->complete_request(req, status);
63 }
64 
65 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
66     bool is_read)
67 {
68     BlockErrorAction action = bdrv_get_error_action(req->dev->bs, is_read, error);
69     VirtIOBlock *s = req->dev;
70 
71     if (action == BLOCK_ERROR_ACTION_STOP) {
72         req->next = s->rq;
73         s->rq = req;
74     } else if (action == BLOCK_ERROR_ACTION_REPORT) {
75         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
76         bdrv_acct_done(s->bs, &req->acct);
77         virtio_blk_free_request(req);
78     }
79 
80     bdrv_error_action(s->bs, action, is_read, error);
81     return action != BLOCK_ERROR_ACTION_IGNORE;
82 }
83 
84 static void virtio_blk_rw_complete(void *opaque, int ret)
85 {
86     VirtIOBlockReq *req = opaque;
87 
88     trace_virtio_blk_rw_complete(req, ret);
89 
90     if (ret) {
91         bool is_read = !(ldl_p(&req->out.type) & VIRTIO_BLK_T_OUT);
92         if (virtio_blk_handle_rw_error(req, -ret, is_read))
93             return;
94     }
95 
96     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
97     bdrv_acct_done(req->dev->bs, &req->acct);
98     virtio_blk_free_request(req);
99 }
100 
101 static void virtio_blk_flush_complete(void *opaque, int ret)
102 {
103     VirtIOBlockReq *req = opaque;
104 
105     if (ret) {
106         if (virtio_blk_handle_rw_error(req, -ret, 0)) {
107             return;
108         }
109     }
110 
111     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
112     bdrv_acct_done(req->dev->bs, &req->acct);
113     virtio_blk_free_request(req);
114 }
115 
116 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
117 {
118     VirtIOBlockReq *req = virtio_blk_alloc_request(s);
119 
120     if (!virtqueue_pop(s->vq, req->elem)) {
121         virtio_blk_free_request(req);
122         return NULL;
123     }
124 
125     return req;
126 }
127 
128 int virtio_blk_handle_scsi_req(VirtIOBlock *blk,
129                                VirtQueueElement *elem)
130 {
131     int status = VIRTIO_BLK_S_OK;
132     struct virtio_scsi_inhdr *scsi = NULL;
133 #ifdef __linux__
134     int i;
135     struct sg_io_hdr hdr;
136 #endif
137 
138     /*
139      * We require at least one output segment each for the virtio_blk_outhdr
140      * and the SCSI command block.
141      *
142      * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
143      * and the sense buffer pointer in the input segments.
144      */
145     if (elem->out_num < 2 || elem->in_num < 3) {
146         status = VIRTIO_BLK_S_IOERR;
147         goto fail;
148     }
149 
150     /*
151      * The scsi inhdr is placed in the second-to-last input segment, just
152      * before the regular inhdr.
153      */
154     scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
155 
156     if (!blk->blk.scsi) {
157         status = VIRTIO_BLK_S_UNSUPP;
158         goto fail;
159     }
160 
161     /*
162      * No support for bidirection commands yet.
163      */
164     if (elem->out_num > 2 && elem->in_num > 3) {
165         status = VIRTIO_BLK_S_UNSUPP;
166         goto fail;
167     }
168 
169 #ifdef __linux__
170     memset(&hdr, 0, sizeof(struct sg_io_hdr));
171     hdr.interface_id = 'S';
172     hdr.cmd_len = elem->out_sg[1].iov_len;
173     hdr.cmdp = elem->out_sg[1].iov_base;
174     hdr.dxfer_len = 0;
175 
176     if (elem->out_num > 2) {
177         /*
178          * If there are more than the minimally required 2 output segments
179          * there is write payload starting from the third iovec.
180          */
181         hdr.dxfer_direction = SG_DXFER_TO_DEV;
182         hdr.iovec_count = elem->out_num - 2;
183 
184         for (i = 0; i < hdr.iovec_count; i++)
185             hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
186 
187         hdr.dxferp = elem->out_sg + 2;
188 
189     } else if (elem->in_num > 3) {
190         /*
191          * If we have more than 3 input segments the guest wants to actually
192          * read data.
193          */
194         hdr.dxfer_direction = SG_DXFER_FROM_DEV;
195         hdr.iovec_count = elem->in_num - 3;
196         for (i = 0; i < hdr.iovec_count; i++)
197             hdr.dxfer_len += elem->in_sg[i].iov_len;
198 
199         hdr.dxferp = elem->in_sg;
200     } else {
201         /*
202          * Some SCSI commands don't actually transfer any data.
203          */
204         hdr.dxfer_direction = SG_DXFER_NONE;
205     }
206 
207     hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
208     hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
209 
210     status = bdrv_ioctl(blk->bs, SG_IO, &hdr);
211     if (status) {
212         status = VIRTIO_BLK_S_UNSUPP;
213         goto fail;
214     }
215 
216     /*
217      * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
218      * clear the masked_status field [hence status gets cleared too, see
219      * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
220      * status has occurred.  However they do set DRIVER_SENSE in driver_status
221      * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
222      */
223     if (hdr.status == 0 && hdr.sb_len_wr > 0) {
224         hdr.status = CHECK_CONDITION;
225     }
226 
227     stl_p(&scsi->errors,
228           hdr.status | (hdr.msg_status << 8) |
229           (hdr.host_status << 16) | (hdr.driver_status << 24));
230     stl_p(&scsi->residual, hdr.resid);
231     stl_p(&scsi->sense_len, hdr.sb_len_wr);
232     stl_p(&scsi->data_len, hdr.dxfer_len);
233 
234     return status;
235 #else
236     abort();
237 #endif
238 
239 fail:
240     /* Just put anything nonzero so that the ioctl fails in the guest.  */
241     if (scsi) {
242         stl_p(&scsi->errors, 255);
243     }
244     return status;
245 }
246 
247 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
248 {
249     int status;
250 
251     status = virtio_blk_handle_scsi_req(req->dev, req->elem);
252     virtio_blk_req_complete(req, status);
253     virtio_blk_free_request(req);
254 }
255 
256 void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
257 {
258     int i, ret;
259 
260     if (!mrb->num_writes) {
261         return;
262     }
263 
264     ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
265     if (ret != 0) {
266         for (i = 0; i < mrb->num_writes; i++) {
267             if (mrb->blkreq[i].error) {
268                 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
269             }
270         }
271     }
272 
273     mrb->num_writes = 0;
274 }
275 
276 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
277 {
278     bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
279 
280     /*
281      * Make sure all outstanding writes are posted to the backing device.
282      */
283     virtio_submit_multiwrite(req->dev->bs, mrb);
284     bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
285 }
286 
287 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
288 {
289     BlockRequest *blkreq;
290     uint64_t sector;
291 
292     sector = ldq_p(&req->out.sector);
293 
294     bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
295 
296     trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
297 
298     if (sector & req->dev->sector_mask) {
299         virtio_blk_rw_complete(req, -EIO);
300         return;
301     }
302     if (req->qiov.size % req->dev->conf->logical_block_size) {
303         virtio_blk_rw_complete(req, -EIO);
304         return;
305     }
306 
307     if (mrb->num_writes == 32) {
308         virtio_submit_multiwrite(req->dev->bs, mrb);
309     }
310 
311     blkreq = &mrb->blkreq[mrb->num_writes];
312     blkreq->sector = sector;
313     blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
314     blkreq->qiov = &req->qiov;
315     blkreq->cb = virtio_blk_rw_complete;
316     blkreq->opaque = req;
317     blkreq->error = 0;
318 
319     mrb->num_writes++;
320 }
321 
322 static void virtio_blk_handle_read(VirtIOBlockReq *req)
323 {
324     uint64_t sector;
325 
326     sector = ldq_p(&req->out.sector);
327 
328     bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
329 
330     trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
331 
332     if (sector & req->dev->sector_mask) {
333         virtio_blk_rw_complete(req, -EIO);
334         return;
335     }
336     if (req->qiov.size % req->dev->conf->logical_block_size) {
337         virtio_blk_rw_complete(req, -EIO);
338         return;
339     }
340     bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
341                    req->qiov.size / BDRV_SECTOR_SIZE,
342                    virtio_blk_rw_complete, req);
343 }
344 
345 void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
346 {
347     uint32_t type;
348     struct iovec *in_iov = req->elem->in_sg;
349     struct iovec *iov = req->elem->out_sg;
350     unsigned in_num = req->elem->in_num;
351     unsigned out_num = req->elem->out_num;
352 
353     if (req->elem->out_num < 1 || req->elem->in_num < 1) {
354         error_report("virtio-blk missing headers");
355         exit(1);
356     }
357 
358     if (unlikely(iov_to_buf(iov, out_num, 0, &req->out,
359                             sizeof(req->out)) != sizeof(req->out))) {
360         error_report("virtio-blk request outhdr too short");
361         exit(1);
362     }
363 
364     iov_discard_front(&iov, &out_num, sizeof(req->out));
365 
366     if (in_num < 1 ||
367         in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
368         error_report("virtio-blk request inhdr too short");
369         exit(1);
370     }
371 
372     req->in = (void *)in_iov[in_num - 1].iov_base
373               + in_iov[in_num - 1].iov_len
374               - sizeof(struct virtio_blk_inhdr);
375     iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr));
376 
377     type = ldl_p(&req->out.type);
378 
379     if (type & VIRTIO_BLK_T_FLUSH) {
380         virtio_blk_handle_flush(req, mrb);
381     } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
382         virtio_blk_handle_scsi(req);
383     } else if (type & VIRTIO_BLK_T_GET_ID) {
384         VirtIOBlock *s = req->dev;
385 
386         /*
387          * NB: per existing s/n string convention the string is
388          * terminated by '\0' only when shorter than buffer.
389          */
390         strncpy(req->elem->in_sg[0].iov_base,
391                 s->blk.serial ? s->blk.serial : "",
392                 MIN(req->elem->in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
393         virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
394         virtio_blk_free_request(req);
395     } else if (type & VIRTIO_BLK_T_OUT) {
396         qemu_iovec_init_external(&req->qiov, &req->elem->out_sg[1],
397                                  req->elem->out_num - 1);
398         virtio_blk_handle_write(req, mrb);
399     } else if (type == VIRTIO_BLK_T_IN || type == VIRTIO_BLK_T_BARRIER) {
400         /* VIRTIO_BLK_T_IN is 0, so we can't just & it. */
401         qemu_iovec_init_external(&req->qiov, &req->elem->in_sg[0],
402                                  req->elem->in_num - 1);
403         virtio_blk_handle_read(req);
404     } else {
405         virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
406         virtio_blk_free_request(req);
407     }
408 }
409 
410 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
411 {
412     VirtIOBlock *s = VIRTIO_BLK(vdev);
413     VirtIOBlockReq *req;
414     MultiReqBuffer mrb = {
415         .num_writes = 0,
416     };
417 
418 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
419     /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
420      * dataplane here instead of waiting for .set_status().
421      */
422     if (s->dataplane) {
423         virtio_blk_data_plane_start(s->dataplane);
424         return;
425     }
426 #endif
427 
428     while ((req = virtio_blk_get_request(s))) {
429         virtio_blk_handle_request(req, &mrb);
430     }
431 
432     virtio_submit_multiwrite(s->bs, &mrb);
433 
434     /*
435      * FIXME: Want to check for completions before returning to guest mode,
436      * so cached reads and writes are reported as quickly as possible. But
437      * that should be done in the generic block layer.
438      */
439 }
440 
441 static void virtio_blk_dma_restart_bh(void *opaque)
442 {
443     VirtIOBlock *s = opaque;
444     VirtIOBlockReq *req = s->rq;
445     MultiReqBuffer mrb = {
446         .num_writes = 0,
447     };
448 
449     qemu_bh_delete(s->bh);
450     s->bh = NULL;
451 
452     s->rq = NULL;
453 
454     while (req) {
455         virtio_blk_handle_request(req, &mrb);
456         req = req->next;
457     }
458 
459     virtio_submit_multiwrite(s->bs, &mrb);
460 }
461 
462 static void virtio_blk_dma_restart_cb(void *opaque, int running,
463                                       RunState state)
464 {
465     VirtIOBlock *s = opaque;
466 
467     if (!running) {
468         return;
469     }
470 
471     if (!s->bh) {
472         s->bh = aio_bh_new(bdrv_get_aio_context(s->blk.conf.bs),
473                            virtio_blk_dma_restart_bh, s);
474         qemu_bh_schedule(s->bh);
475     }
476 }
477 
478 static void virtio_blk_reset(VirtIODevice *vdev)
479 {
480     VirtIOBlock *s = VIRTIO_BLK(vdev);
481 
482 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
483     if (s->dataplane) {
484         virtio_blk_data_plane_stop(s->dataplane);
485     }
486 #endif
487 
488     /*
489      * This should cancel pending requests, but can't do nicely until there
490      * are per-device request lists.
491      */
492     bdrv_drain_all();
493     bdrv_set_enable_write_cache(s->bs, s->original_wce);
494 }
495 
496 /* coalesce internal state, copy to pci i/o region 0
497  */
498 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
499 {
500     VirtIOBlock *s = VIRTIO_BLK(vdev);
501     struct virtio_blk_config blkcfg;
502     uint64_t capacity;
503     int blk_size = s->conf->logical_block_size;
504 
505     bdrv_get_geometry(s->bs, &capacity);
506     memset(&blkcfg, 0, sizeof(blkcfg));
507     stq_p(&blkcfg.capacity, capacity);
508     stl_p(&blkcfg.seg_max, 128 - 2);
509     stw_p(&blkcfg.cylinders, s->conf->cyls);
510     stl_p(&blkcfg.blk_size, blk_size);
511     stw_p(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
512     stw_p(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
513     blkcfg.heads = s->conf->heads;
514     /*
515      * We must ensure that the block device capacity is a multiple of
516      * the logical block size. If that is not the case, let's use
517      * sector_mask to adopt the geometry to have a correct picture.
518      * For those devices where the capacity is ok for the given geometry
519      * we don't touch the sector value of the geometry, since some devices
520      * (like s390 dasd) need a specific value. Here the capacity is already
521      * cyls*heads*secs*blk_size and the sector value is not block size
522      * divided by 512 - instead it is the amount of blk_size blocks
523      * per track (cylinder).
524      */
525     if (bdrv_getlength(s->bs) /  s->conf->heads / s->conf->secs % blk_size) {
526         blkcfg.sectors = s->conf->secs & ~s->sector_mask;
527     } else {
528         blkcfg.sectors = s->conf->secs;
529     }
530     blkcfg.size_max = 0;
531     blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
532     blkcfg.alignment_offset = 0;
533     blkcfg.wce = bdrv_enable_write_cache(s->bs);
534     memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
535 }
536 
537 static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
538 {
539     VirtIOBlock *s = VIRTIO_BLK(vdev);
540     struct virtio_blk_config blkcfg;
541 
542     memcpy(&blkcfg, config, sizeof(blkcfg));
543 
544     aio_context_acquire(bdrv_get_aio_context(s->bs));
545     bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
546     aio_context_release(bdrv_get_aio_context(s->bs));
547 }
548 
549 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
550 {
551     VirtIOBlock *s = VIRTIO_BLK(vdev);
552 
553     features |= (1 << VIRTIO_BLK_F_SEG_MAX);
554     features |= (1 << VIRTIO_BLK_F_GEOMETRY);
555     features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
556     features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
557     features |= (1 << VIRTIO_BLK_F_SCSI);
558 
559     if (s->blk.config_wce) {
560         features |= (1 << VIRTIO_BLK_F_CONFIG_WCE);
561     }
562     if (bdrv_enable_write_cache(s->bs))
563         features |= (1 << VIRTIO_BLK_F_WCE);
564 
565     if (bdrv_is_read_only(s->bs))
566         features |= 1 << VIRTIO_BLK_F_RO;
567 
568     return features;
569 }
570 
571 static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
572 {
573     VirtIOBlock *s = VIRTIO_BLK(vdev);
574     uint32_t features;
575 
576 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
577     if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER |
578                                     VIRTIO_CONFIG_S_DRIVER_OK))) {
579         virtio_blk_data_plane_stop(s->dataplane);
580     }
581 #endif
582 
583     if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
584         return;
585     }
586 
587     features = vdev->guest_features;
588 
589     /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
590      * cache flushes.  Thus, the "auto writethrough" behavior is never
591      * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
592      * Leaving it enabled would break the following sequence:
593      *
594      *     Guest started with "-drive cache=writethrough"
595      *     Guest sets status to 0
596      *     Guest sets DRIVER bit in status field
597      *     Guest reads host features (WCE=0, CONFIG_WCE=1)
598      *     Guest writes guest features (WCE=0, CONFIG_WCE=1)
599      *     Guest writes 1 to the WCE configuration field (writeback mode)
600      *     Guest sets DRIVER_OK bit in status field
601      *
602      * s->bs would erroneously be placed in writethrough mode.
603      */
604     if (!(features & (1 << VIRTIO_BLK_F_CONFIG_WCE))) {
605         aio_context_acquire(bdrv_get_aio_context(s->bs));
606         bdrv_set_enable_write_cache(s->bs,
607                                     !!(features & (1 << VIRTIO_BLK_F_WCE)));
608         aio_context_release(bdrv_get_aio_context(s->bs));
609     }
610 }
611 
612 static void virtio_blk_save(QEMUFile *f, void *opaque)
613 {
614     VirtIOBlock *s = opaque;
615     VirtIODevice *vdev = VIRTIO_DEVICE(s);
616     VirtIOBlockReq *req = s->rq;
617 
618     virtio_save(vdev, f);
619 
620     while (req) {
621         qemu_put_sbyte(f, 1);
622         qemu_put_buffer(f, (unsigned char *)req->elem,
623                         sizeof(VirtQueueElement));
624         req = req->next;
625     }
626     qemu_put_sbyte(f, 0);
627 }
628 
629 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
630 {
631     VirtIOBlock *s = opaque;
632     VirtIODevice *vdev = VIRTIO_DEVICE(s);
633     int ret;
634 
635     if (version_id != 2)
636         return -EINVAL;
637 
638     ret = virtio_load(vdev, f, version_id);
639     if (ret) {
640         return ret;
641     }
642 
643     while (qemu_get_sbyte(f)) {
644         VirtIOBlockReq *req = virtio_blk_alloc_request(s);
645         qemu_get_buffer(f, (unsigned char *)req->elem,
646                         sizeof(VirtQueueElement));
647         req->next = s->rq;
648         s->rq = req;
649 
650         virtqueue_map_sg(req->elem->in_sg, req->elem->in_addr,
651             req->elem->in_num, 1);
652         virtqueue_map_sg(req->elem->out_sg, req->elem->out_addr,
653             req->elem->out_num, 0);
654     }
655 
656     return 0;
657 }
658 
659 static void virtio_blk_resize(void *opaque)
660 {
661     VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
662 
663     virtio_notify_config(vdev);
664 }
665 
666 static const BlockDevOps virtio_block_ops = {
667     .resize_cb = virtio_blk_resize,
668 };
669 
670 void virtio_blk_set_conf(DeviceState *dev, VirtIOBlkConf *blk)
671 {
672     VirtIOBlock *s = VIRTIO_BLK(dev);
673     memcpy(&(s->blk), blk, sizeof(struct VirtIOBlkConf));
674 }
675 
676 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
677 /* Disable dataplane thread during live migration since it does not
678  * update the dirty memory bitmap yet.
679  */
680 static void virtio_blk_migration_state_changed(Notifier *notifier, void *data)
681 {
682     VirtIOBlock *s = container_of(notifier, VirtIOBlock,
683                                   migration_state_notifier);
684     MigrationState *mig = data;
685     Error *err = NULL;
686 
687     if (migration_in_setup(mig)) {
688         if (!s->dataplane) {
689             return;
690         }
691         virtio_blk_data_plane_destroy(s->dataplane);
692         s->dataplane = NULL;
693     } else if (migration_has_finished(mig) ||
694                migration_has_failed(mig)) {
695         if (s->dataplane) {
696             return;
697         }
698         bdrv_drain_all(); /* complete in-flight non-dataplane requests */
699         virtio_blk_data_plane_create(VIRTIO_DEVICE(s), &s->blk,
700                                      &s->dataplane, &err);
701         if (err != NULL) {
702             error_report("%s", error_get_pretty(err));
703             error_free(err);
704         }
705     }
706 }
707 #endif /* CONFIG_VIRTIO_BLK_DATA_PLANE */
708 
709 static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
710 {
711     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
712     VirtIOBlock *s = VIRTIO_BLK(dev);
713     VirtIOBlkConf *blk = &(s->blk);
714 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
715     Error *err = NULL;
716 #endif
717     static int virtio_blk_id;
718 
719     if (!blk->conf.bs) {
720         error_setg(errp, "drive property not set");
721         return;
722     }
723     if (!bdrv_is_inserted(blk->conf.bs)) {
724         error_setg(errp, "Device needs media, but drive is empty");
725         return;
726     }
727 
728     blkconf_serial(&blk->conf, &blk->serial);
729     s->original_wce = bdrv_enable_write_cache(blk->conf.bs);
730     if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
731         error_setg(errp, "Error setting geometry");
732         return;
733     }
734 
735     virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
736                 sizeof(struct virtio_blk_config));
737 
738     s->bs = blk->conf.bs;
739     s->conf = &blk->conf;
740     s->rq = NULL;
741     s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
742 
743     s->vq = virtio_add_queue(vdev, 128, virtio_blk_handle_output);
744     s->complete_request = virtio_blk_complete_request;
745 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
746     virtio_blk_data_plane_create(vdev, blk, &s->dataplane, &err);
747     if (err != NULL) {
748         error_propagate(errp, err);
749         virtio_cleanup(vdev);
750         return;
751     }
752     s->migration_state_notifier.notify = virtio_blk_migration_state_changed;
753     add_migration_state_change_notifier(&s->migration_state_notifier);
754 #endif
755 
756     s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
757     register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
758                     virtio_blk_save, virtio_blk_load, s);
759     bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
760     bdrv_set_guest_block_size(s->bs, s->conf->logical_block_size);
761 
762     bdrv_iostatus_enable(s->bs);
763 
764     add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
765 }
766 
767 static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp)
768 {
769     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
770     VirtIOBlock *s = VIRTIO_BLK(dev);
771 
772 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
773     remove_migration_state_change_notifier(&s->migration_state_notifier);
774     virtio_blk_data_plane_destroy(s->dataplane);
775     s->dataplane = NULL;
776 #endif
777     qemu_del_vm_change_state_handler(s->change);
778     unregister_savevm(dev, "virtio-blk", s);
779     blockdev_mark_auto_del(s->bs);
780     virtio_cleanup(vdev);
781 }
782 
783 static Property virtio_blk_properties[] = {
784     DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlock, blk),
785     DEFINE_PROP_END_OF_LIST(),
786 };
787 
788 static void virtio_blk_class_init(ObjectClass *klass, void *data)
789 {
790     DeviceClass *dc = DEVICE_CLASS(klass);
791     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
792 
793     dc->props = virtio_blk_properties;
794     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
795     vdc->realize = virtio_blk_device_realize;
796     vdc->unrealize = virtio_blk_device_unrealize;
797     vdc->get_config = virtio_blk_update_config;
798     vdc->set_config = virtio_blk_set_config;
799     vdc->get_features = virtio_blk_get_features;
800     vdc->set_status = virtio_blk_set_status;
801     vdc->reset = virtio_blk_reset;
802 }
803 
804 static const TypeInfo virtio_device_info = {
805     .name = TYPE_VIRTIO_BLK,
806     .parent = TYPE_VIRTIO_DEVICE,
807     .instance_size = sizeof(VirtIOBlock),
808     .class_init = virtio_blk_class_init,
809 };
810 
811 static void virtio_register_types(void)
812 {
813     type_register_static(&virtio_device_info);
814 }
815 
816 type_init(virtio_register_types)
817