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