4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
15 #include "qemu/error-report.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"
24 #include "block/scsi.h"
28 #include "hw/virtio/virtio-bus.h"
30 typedef struct VirtIOBlockReq
33 VirtQueueElement elem
;
34 struct virtio_blk_inhdr
*in
;
35 struct virtio_blk_outhdr
*out
;
37 struct VirtIOBlockReq
*next
;
41 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, int status
)
43 VirtIOBlock
*s
= req
->dev
;
44 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
46 trace_virtio_blk_req_complete(req
, status
);
48 stb_p(&req
->in
->status
, status
);
49 virtqueue_push(s
->vq
, &req
->elem
, req
->qiov
.size
+ sizeof(*req
->in
));
50 virtio_notify(vdev
, s
->vq
);
53 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
56 BlockErrorAction action
= bdrv_get_error_action(req
->dev
->bs
, is_read
, error
);
57 VirtIOBlock
*s
= req
->dev
;
59 if (action
== BDRV_ACTION_STOP
) {
62 } else if (action
== BDRV_ACTION_REPORT
) {
63 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
64 bdrv_acct_done(s
->bs
, &req
->acct
);
68 bdrv_error_action(s
->bs
, action
, is_read
, error
);
69 return action
!= BDRV_ACTION_IGNORE
;
72 static void virtio_blk_rw_complete(void *opaque
, int ret
)
74 VirtIOBlockReq
*req
= opaque
;
76 trace_virtio_blk_rw_complete(req
, ret
);
79 bool is_read
= !(ldl_p(&req
->out
->type
) & VIRTIO_BLK_T_OUT
);
80 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
))
84 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
85 bdrv_acct_done(req
->dev
->bs
, &req
->acct
);
89 static void virtio_blk_flush_complete(void *opaque
, int ret
)
91 VirtIOBlockReq
*req
= opaque
;
94 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
99 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
100 bdrv_acct_done(req
->dev
->bs
, &req
->acct
);
104 static VirtIOBlockReq
*virtio_blk_alloc_request(VirtIOBlock
*s
)
106 VirtIOBlockReq
*req
= g_malloc(sizeof(*req
));
113 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
115 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
118 if (!virtqueue_pop(s
->vq
, &req
->elem
)) {
127 int virtio_blk_handle_scsi_req(VirtIOBlock
*blk
,
128 VirtQueueElement
*elem
)
130 int status
= VIRTIO_BLK_S_OK
;
131 struct virtio_scsi_inhdr
*scsi
= NULL
;
134 struct sg_io_hdr hdr
;
138 * We require at least one output segment each for the virtio_blk_outhdr
139 * and the SCSI command block.
141 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
142 * and the sense buffer pointer in the input segments.
144 if (elem
->out_num
< 2 || elem
->in_num
< 3) {
145 status
= VIRTIO_BLK_S_IOERR
;
150 * The scsi inhdr is placed in the second-to-last input segment, just
151 * before the regular inhdr.
153 scsi
= (void *)elem
->in_sg
[elem
->in_num
- 2].iov_base
;
155 if (!blk
->blk
.scsi
) {
156 status
= VIRTIO_BLK_S_UNSUPP
;
161 * No support for bidirection commands yet.
163 if (elem
->out_num
> 2 && elem
->in_num
> 3) {
164 status
= VIRTIO_BLK_S_UNSUPP
;
169 memset(&hdr
, 0, sizeof(struct sg_io_hdr
));
170 hdr
.interface_id
= 'S';
171 hdr
.cmd_len
= elem
->out_sg
[1].iov_len
;
172 hdr
.cmdp
= elem
->out_sg
[1].iov_base
;
175 if (elem
->out_num
> 2) {
177 * If there are more than the minimally required 2 output segments
178 * there is write payload starting from the third iovec.
180 hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
181 hdr
.iovec_count
= elem
->out_num
- 2;
183 for (i
= 0; i
< hdr
.iovec_count
; i
++)
184 hdr
.dxfer_len
+= elem
->out_sg
[i
+ 2].iov_len
;
186 hdr
.dxferp
= elem
->out_sg
+ 2;
188 } else if (elem
->in_num
> 3) {
190 * If we have more than 3 input segments the guest wants to actually
193 hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
194 hdr
.iovec_count
= elem
->in_num
- 3;
195 for (i
= 0; i
< hdr
.iovec_count
; i
++)
196 hdr
.dxfer_len
+= elem
->in_sg
[i
].iov_len
;
198 hdr
.dxferp
= elem
->in_sg
;
201 * Some SCSI commands don't actually transfer any data.
203 hdr
.dxfer_direction
= SG_DXFER_NONE
;
206 hdr
.sbp
= elem
->in_sg
[elem
->in_num
- 3].iov_base
;
207 hdr
.mx_sb_len
= elem
->in_sg
[elem
->in_num
- 3].iov_len
;
209 status
= bdrv_ioctl(blk
->bs
, SG_IO
, &hdr
);
211 status
= VIRTIO_BLK_S_UNSUPP
;
216 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
217 * clear the masked_status field [hence status gets cleared too, see
218 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
219 * status has occurred. However they do set DRIVER_SENSE in driver_status
220 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
222 if (hdr
.status
== 0 && hdr
.sb_len_wr
> 0) {
223 hdr
.status
= CHECK_CONDITION
;
227 hdr
.status
| (hdr
.msg_status
<< 8) |
228 (hdr
.host_status
<< 16) | (hdr
.driver_status
<< 24));
229 stl_p(&scsi
->residual
, hdr
.resid
);
230 stl_p(&scsi
->sense_len
, hdr
.sb_len_wr
);
231 stl_p(&scsi
->data_len
, hdr
.dxfer_len
);
239 /* Just put anything nonzero so that the ioctl fails in the guest. */
241 stl_p(&scsi
->errors
, 255);
246 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
250 status
= virtio_blk_handle_scsi_req(req
->dev
, &req
->elem
);
251 virtio_blk_req_complete(req
, status
);
255 typedef struct MultiReqBuffer
{
256 BlockRequest blkreq
[32];
257 unsigned int num_writes
;
260 static void virtio_submit_multiwrite(BlockDriverState
*bs
, MultiReqBuffer
*mrb
)
264 if (!mrb
->num_writes
) {
268 ret
= bdrv_aio_multiwrite(bs
, mrb
->blkreq
, mrb
->num_writes
);
270 for (i
= 0; i
< mrb
->num_writes
; i
++) {
271 if (mrb
->blkreq
[i
].error
) {
272 virtio_blk_rw_complete(mrb
->blkreq
[i
].opaque
, -EIO
);
280 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
282 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, 0, BDRV_ACCT_FLUSH
);
285 * Make sure all outstanding writes are posted to the backing device.
287 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
288 bdrv_aio_flush(req
->dev
->bs
, virtio_blk_flush_complete
, req
);
291 static void virtio_blk_handle_write(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
293 BlockRequest
*blkreq
;
296 sector
= ldq_p(&req
->out
->sector
);
298 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, req
->qiov
.size
, BDRV_ACCT_WRITE
);
300 trace_virtio_blk_handle_write(req
, sector
, req
->qiov
.size
/ 512);
302 if (sector
& req
->dev
->sector_mask
) {
303 virtio_blk_rw_complete(req
, -EIO
);
306 if (req
->qiov
.size
% req
->dev
->conf
->logical_block_size
) {
307 virtio_blk_rw_complete(req
, -EIO
);
311 if (mrb
->num_writes
== 32) {
312 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
315 blkreq
= &mrb
->blkreq
[mrb
->num_writes
];
316 blkreq
->sector
= sector
;
317 blkreq
->nb_sectors
= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
318 blkreq
->qiov
= &req
->qiov
;
319 blkreq
->cb
= virtio_blk_rw_complete
;
320 blkreq
->opaque
= req
;
326 static void virtio_blk_handle_read(VirtIOBlockReq
*req
)
330 sector
= ldq_p(&req
->out
->sector
);
332 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, req
->qiov
.size
, BDRV_ACCT_READ
);
334 trace_virtio_blk_handle_read(req
, sector
, req
->qiov
.size
/ 512);
336 if (sector
& req
->dev
->sector_mask
) {
337 virtio_blk_rw_complete(req
, -EIO
);
340 if (req
->qiov
.size
% req
->dev
->conf
->logical_block_size
) {
341 virtio_blk_rw_complete(req
, -EIO
);
344 bdrv_aio_readv(req
->dev
->bs
, sector
, &req
->qiov
,
345 req
->qiov
.size
/ BDRV_SECTOR_SIZE
,
346 virtio_blk_rw_complete
, req
);
349 static void virtio_blk_handle_request(VirtIOBlockReq
*req
,
354 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
355 error_report("virtio-blk missing headers");
359 if (req
->elem
.out_sg
[0].iov_len
< sizeof(*req
->out
) ||
360 req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_len
< sizeof(*req
->in
)) {
361 error_report("virtio-blk header not in correct element");
365 req
->out
= (void *)req
->elem
.out_sg
[0].iov_base
;
366 req
->in
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_base
;
368 type
= ldl_p(&req
->out
->type
);
370 if (type
& VIRTIO_BLK_T_FLUSH
) {
371 virtio_blk_handle_flush(req
, mrb
);
372 } else if (type
& VIRTIO_BLK_T_SCSI_CMD
) {
373 virtio_blk_handle_scsi(req
);
374 } else if (type
& VIRTIO_BLK_T_GET_ID
) {
375 VirtIOBlock
*s
= req
->dev
;
378 * NB: per existing s/n string convention the string is
379 * terminated by '\0' only when shorter than buffer.
381 strncpy(req
->elem
.in_sg
[0].iov_base
,
382 s
->blk
.serial
? s
->blk
.serial
: "",
383 MIN(req
->elem
.in_sg
[0].iov_len
, VIRTIO_BLK_ID_BYTES
));
384 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
386 } else if (type
& VIRTIO_BLK_T_OUT
) {
387 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.out_sg
[1],
388 req
->elem
.out_num
- 1);
389 virtio_blk_handle_write(req
, mrb
);
390 } else if (type
== VIRTIO_BLK_T_IN
|| type
== VIRTIO_BLK_T_BARRIER
) {
391 /* VIRTIO_BLK_T_IN is 0, so we can't just & it. */
392 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.in_sg
[0],
393 req
->elem
.in_num
- 1);
394 virtio_blk_handle_read(req
);
396 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
401 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
403 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
405 MultiReqBuffer mrb
= {
409 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
410 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
411 * dataplane here instead of waiting for .set_status().
414 virtio_blk_data_plane_start(s
->dataplane
);
419 while ((req
= virtio_blk_get_request(s
))) {
420 virtio_blk_handle_request(req
, &mrb
);
423 virtio_submit_multiwrite(s
->bs
, &mrb
);
426 * FIXME: Want to check for completions before returning to guest mode,
427 * so cached reads and writes are reported as quickly as possible. But
428 * that should be done in the generic block layer.
432 static void virtio_blk_dma_restart_bh(void *opaque
)
434 VirtIOBlock
*s
= opaque
;
435 VirtIOBlockReq
*req
= s
->rq
;
436 MultiReqBuffer mrb
= {
440 qemu_bh_delete(s
->bh
);
446 virtio_blk_handle_request(req
, &mrb
);
450 virtio_submit_multiwrite(s
->bs
, &mrb
);
453 static void virtio_blk_dma_restart_cb(void *opaque
, int running
,
456 VirtIOBlock
*s
= opaque
;
463 s
->bh
= qemu_bh_new(virtio_blk_dma_restart_bh
, s
);
464 qemu_bh_schedule(s
->bh
);
468 static void virtio_blk_reset(VirtIODevice
*vdev
)
470 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
472 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
474 virtio_blk_data_plane_stop(s
->dataplane
);
479 * This should cancel pending requests, but can't do nicely until there
480 * are per-device request lists.
483 bdrv_set_enable_write_cache(s
->bs
, s
->original_wce
);
486 /* coalesce internal state, copy to pci i/o region 0
488 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
490 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
491 struct virtio_blk_config blkcfg
;
493 int blk_size
= s
->conf
->logical_block_size
;
495 bdrv_get_geometry(s
->bs
, &capacity
);
496 memset(&blkcfg
, 0, sizeof(blkcfg
));
497 stq_p(&blkcfg
.capacity
, capacity
);
498 stl_p(&blkcfg
.seg_max
, 128 - 2);
499 stw_p(&blkcfg
.cylinders
, s
->conf
->cyls
);
500 stl_p(&blkcfg
.blk_size
, blk_size
);
501 stw_p(&blkcfg
.min_io_size
, s
->conf
->min_io_size
/ blk_size
);
502 stw_p(&blkcfg
.opt_io_size
, s
->conf
->opt_io_size
/ blk_size
);
503 blkcfg
.heads
= s
->conf
->heads
;
505 * We must ensure that the block device capacity is a multiple of
506 * the logical block size. If that is not the case, let's use
507 * sector_mask to adopt the geometry to have a correct picture.
508 * For those devices where the capacity is ok for the given geometry
509 * we don't touch the sector value of the geometry, since some devices
510 * (like s390 dasd) need a specific value. Here the capacity is already
511 * cyls*heads*secs*blk_size and the sector value is not block size
512 * divided by 512 - instead it is the amount of blk_size blocks
513 * per track (cylinder).
515 if (bdrv_getlength(s
->bs
) / s
->conf
->heads
/ s
->conf
->secs
% blk_size
) {
516 blkcfg
.sectors
= s
->conf
->secs
& ~s
->sector_mask
;
518 blkcfg
.sectors
= s
->conf
->secs
;
521 blkcfg
.physical_block_exp
= get_physical_block_exp(s
->conf
);
522 blkcfg
.alignment_offset
= 0;
523 blkcfg
.wce
= bdrv_enable_write_cache(s
->bs
);
524 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
527 static void virtio_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
529 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
530 struct virtio_blk_config blkcfg
;
532 memcpy(&blkcfg
, config
, sizeof(blkcfg
));
534 aio_context_acquire(bdrv_get_aio_context(s
->bs
));
535 bdrv_set_enable_write_cache(s
->bs
, blkcfg
.wce
!= 0);
536 aio_context_release(bdrv_get_aio_context(s
->bs
));
539 static uint32_t virtio_blk_get_features(VirtIODevice
*vdev
, uint32_t features
)
541 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
543 features
|= (1 << VIRTIO_BLK_F_SEG_MAX
);
544 features
|= (1 << VIRTIO_BLK_F_GEOMETRY
);
545 features
|= (1 << VIRTIO_BLK_F_TOPOLOGY
);
546 features
|= (1 << VIRTIO_BLK_F_BLK_SIZE
);
547 features
|= (1 << VIRTIO_BLK_F_SCSI
);
549 if (s
->blk
.config_wce
) {
550 features
|= (1 << VIRTIO_BLK_F_CONFIG_WCE
);
552 if (bdrv_enable_write_cache(s
->bs
))
553 features
|= (1 << VIRTIO_BLK_F_WCE
);
555 if (bdrv_is_read_only(s
->bs
))
556 features
|= 1 << VIRTIO_BLK_F_RO
;
561 static void virtio_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
563 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
566 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
567 if (s
->dataplane
&& !(status
& (VIRTIO_CONFIG_S_DRIVER
|
568 VIRTIO_CONFIG_S_DRIVER_OK
))) {
569 virtio_blk_data_plane_stop(s
->dataplane
);
573 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
577 features
= vdev
->guest_features
;
579 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
580 * cache flushes. Thus, the "auto writethrough" behavior is never
581 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
582 * Leaving it enabled would break the following sequence:
584 * Guest started with "-drive cache=writethrough"
585 * Guest sets status to 0
586 * Guest sets DRIVER bit in status field
587 * Guest reads host features (WCE=0, CONFIG_WCE=1)
588 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
589 * Guest writes 1 to the WCE configuration field (writeback mode)
590 * Guest sets DRIVER_OK bit in status field
592 * s->bs would erroneously be placed in writethrough mode.
594 if (!(features
& (1 << VIRTIO_BLK_F_CONFIG_WCE
))) {
595 aio_context_acquire(bdrv_get_aio_context(s
->bs
));
596 bdrv_set_enable_write_cache(s
->bs
,
597 !!(features
& (1 << VIRTIO_BLK_F_WCE
)));
598 aio_context_release(bdrv_get_aio_context(s
->bs
));
602 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
604 VirtIOBlock
*s
= opaque
;
605 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
606 VirtIOBlockReq
*req
= s
->rq
;
608 virtio_save(vdev
, f
);
611 qemu_put_sbyte(f
, 1);
612 qemu_put_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
615 qemu_put_sbyte(f
, 0);
618 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
620 VirtIOBlock
*s
= opaque
;
621 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
627 ret
= virtio_load(vdev
, f
);
632 while (qemu_get_sbyte(f
)) {
633 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
634 qemu_get_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
638 virtqueue_map_sg(req
->elem
.in_sg
, req
->elem
.in_addr
,
639 req
->elem
.in_num
, 1);
640 virtqueue_map_sg(req
->elem
.out_sg
, req
->elem
.out_addr
,
641 req
->elem
.out_num
, 0);
647 static void virtio_blk_resize(void *opaque
)
649 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
651 virtio_notify_config(vdev
);
654 static const BlockDevOps virtio_block_ops
= {
655 .resize_cb
= virtio_blk_resize
,
658 void virtio_blk_set_conf(DeviceState
*dev
, VirtIOBlkConf
*blk
)
660 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
661 memcpy(&(s
->blk
), blk
, sizeof(struct VirtIOBlkConf
));
664 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
665 /* Disable dataplane thread during live migration since it does not
666 * update the dirty memory bitmap yet.
668 static void virtio_blk_migration_state_changed(Notifier
*notifier
, void *data
)
670 VirtIOBlock
*s
= container_of(notifier
, VirtIOBlock
,
671 migration_state_notifier
);
672 MigrationState
*mig
= data
;
675 if (migration_in_setup(mig
)) {
679 virtio_blk_data_plane_destroy(s
->dataplane
);
681 } else if (migration_has_finished(mig
) ||
682 migration_has_failed(mig
)) {
686 bdrv_drain_all(); /* complete in-flight non-dataplane requests */
687 virtio_blk_data_plane_create(VIRTIO_DEVICE(s
), &s
->blk
,
688 &s
->dataplane
, &err
);
690 error_report("%s", error_get_pretty(err
));
695 #endif /* CONFIG_VIRTIO_BLK_DATA_PLANE */
697 static void virtio_blk_device_realize(DeviceState
*dev
, Error
**errp
)
699 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
700 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
701 VirtIOBlkConf
*blk
= &(s
->blk
);
702 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
705 static int virtio_blk_id
;
708 error_setg(errp
, "drive property not set");
711 if (!bdrv_is_inserted(blk
->conf
.bs
)) {
712 error_setg(errp
, "Device needs media, but drive is empty");
716 blkconf_serial(&blk
->conf
, &blk
->serial
);
717 s
->original_wce
= bdrv_enable_write_cache(blk
->conf
.bs
);
718 if (blkconf_geometry(&blk
->conf
, NULL
, 65535, 255, 255) < 0) {
719 error_setg(errp
, "Error setting geometry");
723 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
724 sizeof(struct virtio_blk_config
));
726 s
->bs
= blk
->conf
.bs
;
727 s
->conf
= &blk
->conf
;
729 s
->sector_mask
= (s
->conf
->logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
731 s
->vq
= virtio_add_queue(vdev
, 128, virtio_blk_handle_output
);
732 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
733 virtio_blk_data_plane_create(vdev
, blk
, &s
->dataplane
, &err
);
735 error_propagate(errp
, err
);
736 virtio_cleanup(vdev
);
739 s
->migration_state_notifier
.notify
= virtio_blk_migration_state_changed
;
740 add_migration_state_change_notifier(&s
->migration_state_notifier
);
743 s
->change
= qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
744 register_savevm(dev
, "virtio-blk", virtio_blk_id
++, 2,
745 virtio_blk_save
, virtio_blk_load
, s
);
746 bdrv_set_dev_ops(s
->bs
, &virtio_block_ops
, s
);
747 bdrv_set_guest_block_size(s
->bs
, s
->conf
->logical_block_size
);
749 bdrv_iostatus_enable(s
->bs
);
751 add_boot_device_path(s
->conf
->bootindex
, dev
, "/disk@0,0");
754 static void virtio_blk_device_unrealize(DeviceState
*dev
, Error
**errp
)
756 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
757 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
759 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
760 remove_migration_state_change_notifier(&s
->migration_state_notifier
);
761 virtio_blk_data_plane_destroy(s
->dataplane
);
764 qemu_del_vm_change_state_handler(s
->change
);
765 unregister_savevm(dev
, "virtio-blk", s
);
766 blockdev_mark_auto_del(s
->bs
);
767 virtio_cleanup(vdev
);
770 static Property virtio_blk_properties
[] = {
771 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlock
, blk
),
772 DEFINE_PROP_END_OF_LIST(),
775 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
777 DeviceClass
*dc
= DEVICE_CLASS(klass
);
778 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
780 dc
->props
= virtio_blk_properties
;
781 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
782 vdc
->realize
= virtio_blk_device_realize
;
783 vdc
->unrealize
= virtio_blk_device_unrealize
;
784 vdc
->get_config
= virtio_blk_update_config
;
785 vdc
->set_config
= virtio_blk_set_config
;
786 vdc
->get_features
= virtio_blk_get_features
;
787 vdc
->set_status
= virtio_blk_set_status
;
788 vdc
->reset
= virtio_blk_reset
;
791 static const TypeInfo virtio_device_info
= {
792 .name
= TYPE_VIRTIO_BLK
,
793 .parent
= TYPE_VIRTIO_DEVICE
,
794 .instance_size
= sizeof(VirtIOBlock
),
795 .class_init
= virtio_blk_class_init
,
798 static void virtio_register_types(void)
800 type_register_static(&virtio_device_info
);
803 type_init(virtio_register_types
)