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
;
36 struct virtio_scsi_inhdr
*scsi
;
38 struct VirtIOBlockReq
*next
;
42 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, int status
)
44 VirtIOBlock
*s
= req
->dev
;
45 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
47 trace_virtio_blk_req_complete(req
, status
);
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
);
54 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
57 BlockErrorAction action
= bdrv_get_error_action(req
->dev
->bs
, is_read
, error
);
58 VirtIOBlock
*s
= req
->dev
;
60 if (action
== BDRV_ACTION_STOP
) {
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
);
69 bdrv_error_action(s
->bs
, action
, is_read
, error
);
70 return action
!= BDRV_ACTION_IGNORE
;
73 static void virtio_blk_rw_complete(void *opaque
, int ret
)
75 VirtIOBlockReq
*req
= opaque
;
77 trace_virtio_blk_rw_complete(req
, 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
))
85 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
86 bdrv_acct_done(req
->dev
->bs
, &req
->acct
);
90 static void virtio_blk_flush_complete(void *opaque
, int ret
)
92 VirtIOBlockReq
*req
= opaque
;
95 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
100 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
101 bdrv_acct_done(req
->dev
->bs
, &req
->acct
);
105 static VirtIOBlockReq
*virtio_blk_alloc_request(VirtIOBlock
*s
)
107 VirtIOBlockReq
*req
= g_malloc(sizeof(*req
));
114 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
116 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
119 if (!virtqueue_pop(s
->vq
, &req
->elem
)) {
128 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
134 int status
= VIRTIO_BLK_S_OK
;
137 * We require at least one output segment each for the virtio_blk_outhdr
138 * and the SCSI command block.
140 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
141 * and the sense buffer pointer in the input segments.
143 if (req
->elem
.out_num
< 2 || req
->elem
.in_num
< 3) {
144 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
150 * The scsi inhdr is placed in the second-to-last input segment, just
151 * before the regular inhdr.
153 req
->scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
155 if (!req
->dev
->blk
.scsi
) {
156 status
= VIRTIO_BLK_S_UNSUPP
;
161 * No support for bidirection commands yet.
163 if (req
->elem
.out_num
> 2 && req
->elem
.in_num
> 3) {
164 status
= VIRTIO_BLK_S_UNSUPP
;
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
;
176 if (req
->elem
.out_num
> 2) {
178 * If there are more than the minimally required 2 output segments
179 * there is write payload starting from the third iovec.
181 hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
182 hdr
.iovec_count
= req
->elem
.out_num
- 2;
184 for (i
= 0; i
< hdr
.iovec_count
; i
++)
185 hdr
.dxfer_len
+= req
->elem
.out_sg
[i
+ 2].iov_len
;
187 hdr
.dxferp
= req
->elem
.out_sg
+ 2;
189 } else if (req
->elem
.in_num
> 3) {
191 * If we have more than 3 input segments the guest wants to actually
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
;
199 hdr
.dxferp
= req
->elem
.in_sg
;
202 * Some SCSI commands don't actually transfer any data.
204 hdr
.dxfer_direction
= SG_DXFER_NONE
;
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
;
210 ret
= bdrv_ioctl(req
->dev
->bs
, SG_IO
, &hdr
);
212 status
= VIRTIO_BLK_S_UNSUPP
;
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.
223 if (hdr
.status
== 0 && hdr
.sb_len_wr
> 0) {
224 hdr
.status
= CHECK_CONDITION
;
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
);
234 virtio_blk_req_complete(req
, status
);
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
);
248 typedef struct MultiReqBuffer
{
249 BlockRequest blkreq
[32];
250 unsigned int num_writes
;
253 static void virtio_submit_multiwrite(BlockDriverState
*bs
, MultiReqBuffer
*mrb
)
257 if (!mrb
->num_writes
) {
261 ret
= bdrv_aio_multiwrite(bs
, mrb
->blkreq
, mrb
->num_writes
);
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
);
273 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
275 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, 0, BDRV_ACCT_FLUSH
);
278 * Make sure all outstanding writes are posted to the backing device.
280 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
281 bdrv_aio_flush(req
->dev
->bs
, virtio_blk_flush_complete
, req
);
284 static void virtio_blk_handle_write(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
286 BlockRequest
*blkreq
;
289 sector
= ldq_p(&req
->out
->sector
);
291 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, req
->qiov
.size
, BDRV_ACCT_WRITE
);
293 trace_virtio_blk_handle_write(req
, sector
, req
->qiov
.size
/ 512);
295 if (sector
& req
->dev
->sector_mask
) {
296 virtio_blk_rw_complete(req
, -EIO
);
299 if (req
->qiov
.size
% req
->dev
->conf
->logical_block_size
) {
300 virtio_blk_rw_complete(req
, -EIO
);
304 if (mrb
->num_writes
== 32) {
305 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
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
;
319 static void virtio_blk_handle_read(VirtIOBlockReq
*req
)
323 sector
= ldq_p(&req
->out
->sector
);
325 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, req
->qiov
.size
, BDRV_ACCT_READ
);
327 trace_virtio_blk_handle_read(req
, sector
, req
->qiov
.size
/ 512);
329 if (sector
& req
->dev
->sector_mask
) {
330 virtio_blk_rw_complete(req
, -EIO
);
333 if (req
->qiov
.size
% req
->dev
->conf
->logical_block_size
) {
334 virtio_blk_rw_complete(req
, -EIO
);
337 bdrv_aio_readv(req
->dev
->bs
, sector
, &req
->qiov
,
338 req
->qiov
.size
/ BDRV_SECTOR_SIZE
,
339 virtio_blk_rw_complete
, req
);
342 static void virtio_blk_handle_request(VirtIOBlockReq
*req
,
347 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
348 error_report("virtio-blk missing headers");
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");
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
;
361 type
= ldl_p(&req
->out
->type
);
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
;
371 * NB: per existing s/n string convention the string is
372 * terminated by '\0' only when shorter than buffer.
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
);
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
);
389 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
394 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
396 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
398 MultiReqBuffer mrb
= {
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().
407 virtio_blk_data_plane_start(s
->dataplane
);
412 while ((req
= virtio_blk_get_request(s
))) {
413 virtio_blk_handle_request(req
, &mrb
);
416 virtio_submit_multiwrite(s
->bs
, &mrb
);
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.
425 static void virtio_blk_dma_restart_bh(void *opaque
)
427 VirtIOBlock
*s
= opaque
;
428 VirtIOBlockReq
*req
= s
->rq
;
429 MultiReqBuffer mrb
= {
433 qemu_bh_delete(s
->bh
);
439 virtio_blk_handle_request(req
, &mrb
);
443 virtio_submit_multiwrite(s
->bs
, &mrb
);
446 static void virtio_blk_dma_restart_cb(void *opaque
, int running
,
449 VirtIOBlock
*s
= opaque
;
456 s
->bh
= qemu_bh_new(virtio_blk_dma_restart_bh
, s
);
457 qemu_bh_schedule(s
->bh
);
461 static void virtio_blk_reset(VirtIODevice
*vdev
)
463 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
465 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
467 virtio_blk_data_plane_stop(s
->dataplane
);
472 * This should cancel pending requests, but can't do nicely until there
473 * are per-device request lists.
476 bdrv_set_enable_write_cache(s
->bs
, s
->original_wce
);
479 /* coalesce internal state, copy to pci i/o region 0
481 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
483 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
484 struct virtio_blk_config blkcfg
;
486 int blk_size
= s
->conf
->logical_block_size
;
488 bdrv_get_geometry(s
->bs
, &capacity
);
489 memset(&blkcfg
, 0, sizeof(blkcfg
));
490 stq_raw(&blkcfg
.capacity
, capacity
);
491 stl_raw(&blkcfg
.seg_max
, 128 - 2);
492 stw_raw(&blkcfg
.cylinders
, s
->conf
->cyls
);
493 stl_raw(&blkcfg
.blk_size
, blk_size
);
494 stw_raw(&blkcfg
.min_io_size
, s
->conf
->min_io_size
/ blk_size
);
495 stw_raw(&blkcfg
.opt_io_size
, s
->conf
->opt_io_size
/ blk_size
);
496 blkcfg
.heads
= s
->conf
->heads
;
498 * We must ensure that the block device capacity is a multiple of
499 * the logical block size. If that is not the case, let's use
500 * sector_mask to adopt the geometry to have a correct picture.
501 * For those devices where the capacity is ok for the given geometry
502 * we don't touch the sector value of the geometry, since some devices
503 * (like s390 dasd) need a specific value. Here the capacity is already
504 * cyls*heads*secs*blk_size and the sector value is not block size
505 * divided by 512 - instead it is the amount of blk_size blocks
506 * per track (cylinder).
508 if (bdrv_getlength(s
->bs
) / s
->conf
->heads
/ s
->conf
->secs
% blk_size
) {
509 blkcfg
.sectors
= s
->conf
->secs
& ~s
->sector_mask
;
511 blkcfg
.sectors
= s
->conf
->secs
;
514 blkcfg
.physical_block_exp
= get_physical_block_exp(s
->conf
);
515 blkcfg
.alignment_offset
= 0;
516 blkcfg
.wce
= bdrv_enable_write_cache(s
->bs
);
517 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
520 static void virtio_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
522 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
523 struct virtio_blk_config blkcfg
;
525 memcpy(&blkcfg
, config
, sizeof(blkcfg
));
526 bdrv_set_enable_write_cache(s
->bs
, blkcfg
.wce
!= 0);
529 static uint32_t virtio_blk_get_features(VirtIODevice
*vdev
, uint32_t features
)
531 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
533 features
|= (1 << VIRTIO_BLK_F_SEG_MAX
);
534 features
|= (1 << VIRTIO_BLK_F_GEOMETRY
);
535 features
|= (1 << VIRTIO_BLK_F_TOPOLOGY
);
536 features
|= (1 << VIRTIO_BLK_F_BLK_SIZE
);
537 features
|= (1 << VIRTIO_BLK_F_SCSI
);
539 if (s
->blk
.config_wce
) {
540 features
|= (1 << VIRTIO_BLK_F_CONFIG_WCE
);
542 if (bdrv_enable_write_cache(s
->bs
))
543 features
|= (1 << VIRTIO_BLK_F_WCE
);
545 if (bdrv_is_read_only(s
->bs
))
546 features
|= 1 << VIRTIO_BLK_F_RO
;
551 static void virtio_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
553 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
556 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
557 if (s
->dataplane
&& !(status
& (VIRTIO_CONFIG_S_DRIVER
|
558 VIRTIO_CONFIG_S_DRIVER_OK
))) {
559 virtio_blk_data_plane_stop(s
->dataplane
);
563 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
567 features
= vdev
->guest_features
;
569 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
570 * cache flushes. Thus, the "auto writethrough" behavior is never
571 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
572 * Leaving it enabled would break the following sequence:
574 * Guest started with "-drive cache=writethrough"
575 * Guest sets status to 0
576 * Guest sets DRIVER bit in status field
577 * Guest reads host features (WCE=0, CONFIG_WCE=1)
578 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
579 * Guest writes 1 to the WCE configuration field (writeback mode)
580 * Guest sets DRIVER_OK bit in status field
582 * s->bs would erroneously be placed in writethrough mode.
584 if (!(features
& (1 << VIRTIO_BLK_F_CONFIG_WCE
))) {
585 bdrv_set_enable_write_cache(s
->bs
, !!(features
& (1 << VIRTIO_BLK_F_WCE
)));
589 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
591 VirtIOBlock
*s
= opaque
;
592 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
593 VirtIOBlockReq
*req
= s
->rq
;
595 virtio_save(vdev
, f
);
598 qemu_put_sbyte(f
, 1);
599 qemu_put_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
602 qemu_put_sbyte(f
, 0);
605 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
607 VirtIOBlock
*s
= opaque
;
608 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
614 ret
= virtio_load(vdev
, f
);
619 while (qemu_get_sbyte(f
)) {
620 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
621 qemu_get_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
625 virtqueue_map_sg(req
->elem
.in_sg
, req
->elem
.in_addr
,
626 req
->elem
.in_num
, 1);
627 virtqueue_map_sg(req
->elem
.out_sg
, req
->elem
.out_addr
,
628 req
->elem
.out_num
, 0);
634 static void virtio_blk_resize(void *opaque
)
636 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
638 virtio_notify_config(vdev
);
641 static const BlockDevOps virtio_block_ops
= {
642 .resize_cb
= virtio_blk_resize
,
645 void virtio_blk_set_conf(DeviceState
*dev
, VirtIOBlkConf
*blk
)
647 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
648 memcpy(&(s
->blk
), blk
, sizeof(struct VirtIOBlkConf
));
651 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
652 /* Disable dataplane thread during live migration since it does not
653 * update the dirty memory bitmap yet.
655 static void virtio_blk_migration_state_changed(Notifier
*notifier
, void *data
)
657 VirtIOBlock
*s
= container_of(notifier
, VirtIOBlock
,
658 migration_state_notifier
);
659 MigrationState
*mig
= data
;
661 if (migration_in_setup(mig
)) {
665 virtio_blk_data_plane_destroy(s
->dataplane
);
667 } else if (migration_has_finished(mig
) ||
668 migration_has_failed(mig
)) {
672 bdrv_drain_all(); /* complete in-flight non-dataplane requests */
673 virtio_blk_data_plane_create(VIRTIO_DEVICE(s
), &s
->blk
,
677 #endif /* CONFIG_VIRTIO_BLK_DATA_PLANE */
679 static int virtio_blk_device_init(VirtIODevice
*vdev
)
681 DeviceState
*qdev
= DEVICE(vdev
);
682 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
683 VirtIOBlkConf
*blk
= &(s
->blk
);
684 static int virtio_blk_id
;
687 error_report("drive property not set");
690 if (!bdrv_is_inserted(blk
->conf
.bs
)) {
691 error_report("Device needs media, but drive is empty");
695 blkconf_serial(&blk
->conf
, &blk
->serial
);
696 s
->original_wce
= bdrv_enable_write_cache(blk
->conf
.bs
);
697 if (blkconf_geometry(&blk
->conf
, NULL
, 65535, 255, 255) < 0) {
701 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
702 sizeof(struct virtio_blk_config
));
704 s
->bs
= blk
->conf
.bs
;
705 s
->conf
= &blk
->conf
;
707 s
->sector_mask
= (s
->conf
->logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
709 s
->vq
= virtio_add_queue(vdev
, 128, virtio_blk_handle_output
);
710 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
711 if (!virtio_blk_data_plane_create(vdev
, blk
, &s
->dataplane
)) {
712 virtio_cleanup(vdev
);
715 s
->migration_state_notifier
.notify
= virtio_blk_migration_state_changed
;
716 add_migration_state_change_notifier(&s
->migration_state_notifier
);
719 s
->change
= qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
720 register_savevm(qdev
, "virtio-blk", virtio_blk_id
++, 2,
721 virtio_blk_save
, virtio_blk_load
, s
);
722 bdrv_set_dev_ops(s
->bs
, &virtio_block_ops
, s
);
723 bdrv_set_buffer_alignment(s
->bs
, s
->conf
->logical_block_size
);
725 bdrv_iostatus_enable(s
->bs
);
727 add_boot_device_path(s
->conf
->bootindex
, qdev
, "/disk@0,0");
731 static int virtio_blk_device_exit(DeviceState
*dev
)
733 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
734 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
735 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
736 remove_migration_state_change_notifier(&s
->migration_state_notifier
);
737 virtio_blk_data_plane_destroy(s
->dataplane
);
740 qemu_del_vm_change_state_handler(s
->change
);
741 unregister_savevm(dev
, "virtio-blk", s
);
742 blockdev_mark_auto_del(s
->bs
);
743 virtio_cleanup(vdev
);
747 static Property virtio_blk_properties
[] = {
748 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlock
, blk
),
749 DEFINE_PROP_END_OF_LIST(),
752 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
754 DeviceClass
*dc
= DEVICE_CLASS(klass
);
755 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
756 dc
->exit
= virtio_blk_device_exit
;
757 dc
->props
= virtio_blk_properties
;
758 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
759 vdc
->init
= virtio_blk_device_init
;
760 vdc
->get_config
= virtio_blk_update_config
;
761 vdc
->set_config
= virtio_blk_set_config
;
762 vdc
->get_features
= virtio_blk_get_features
;
763 vdc
->set_status
= virtio_blk_set_status
;
764 vdc
->reset
= virtio_blk_reset
;
767 static const TypeInfo virtio_device_info
= {
768 .name
= TYPE_VIRTIO_BLK
,
769 .parent
= TYPE_VIRTIO_DEVICE
,
770 .instance_size
= sizeof(VirtIOBlock
),
771 .class_init
= virtio_blk_class_init
,
774 static void virtio_register_types(void)
776 type_register_static(&virtio_device_info
);
779 type_init(virtio_register_types
)