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/osdep.h"
15 #include "qapi/error.h"
17 #include "qemu/module.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
21 #include "hw/block/block.h"
22 #include "hw/qdev-properties.h"
23 #include "sysemu/blockdev.h"
24 #include "sysemu/sysemu.h"
25 #include "sysemu/runstate.h"
26 #include "hw/virtio/virtio-blk.h"
27 #include "dataplane/virtio-blk.h"
28 #include "scsi/constants.h"
32 #include "hw/virtio/virtio-bus.h"
33 #include "migration/qemu-file-types.h"
34 #include "hw/virtio/virtio-access.h"
36 /* Config size before the discard support (hide associated config fields) */
37 #define VIRTIO_BLK_CFG_SIZE offsetof(struct virtio_blk_config, \
40 * Starting from the discard feature, we can use this array to properly
41 * set the config size depending on the features enabled.
43 static VirtIOFeature feature_sizes
[] = {
44 {.flags
= 1ULL << VIRTIO_BLK_F_DISCARD
,
45 .end
= endof(struct virtio_blk_config
, discard_sector_alignment
)},
46 {.flags
= 1ULL << VIRTIO_BLK_F_WRITE_ZEROES
,
47 .end
= endof(struct virtio_blk_config
, write_zeroes_may_unmap
)},
51 static void virtio_blk_set_config_size(VirtIOBlock
*s
, uint64_t host_features
)
53 s
->config_size
= MAX(VIRTIO_BLK_CFG_SIZE
,
54 virtio_feature_get_config_size(feature_sizes
, host_features
));
56 assert(s
->config_size
<= sizeof(struct virtio_blk_config
));
59 static void virtio_blk_init_request(VirtIOBlock
*s
, VirtQueue
*vq
,
70 static void virtio_blk_free_request(VirtIOBlockReq
*req
)
75 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, unsigned char status
)
77 VirtIOBlock
*s
= req
->dev
;
78 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
80 trace_virtio_blk_req_complete(vdev
, req
, status
);
82 stb_p(&req
->in
->status
, status
);
83 virtqueue_push(req
->vq
, &req
->elem
, req
->in_len
);
84 if (s
->dataplane_started
&& !s
->dataplane_disabled
) {
85 virtio_blk_data_plane_notify(s
->dataplane
, req
->vq
);
87 virtio_notify(vdev
, req
->vq
);
91 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
92 bool is_read
, bool acct_failed
)
94 VirtIOBlock
*s
= req
->dev
;
95 BlockErrorAction action
= blk_get_error_action(s
->blk
, is_read
, error
);
97 if (action
== BLOCK_ERROR_ACTION_STOP
) {
98 /* Break the link as the next request is going to be parsed from the
99 * ring again. Otherwise we may end up doing a double completion! */
103 } else if (action
== BLOCK_ERROR_ACTION_REPORT
) {
104 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
106 block_acct_failed(blk_get_stats(s
->blk
), &req
->acct
);
108 virtio_blk_free_request(req
);
111 blk_error_action(s
->blk
, action
, is_read
, error
);
112 return action
!= BLOCK_ERROR_ACTION_IGNORE
;
115 static void virtio_blk_rw_complete(void *opaque
, int ret
)
117 VirtIOBlockReq
*next
= opaque
;
118 VirtIOBlock
*s
= next
->dev
;
119 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
121 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
123 VirtIOBlockReq
*req
= next
;
125 trace_virtio_blk_rw_complete(vdev
, req
, ret
);
127 if (req
->qiov
.nalloc
!= -1) {
128 /* If nalloc is != -1 req->qiov is a local copy of the original
129 * external iovec. It was allocated in submit_requests to be
130 * able to merge requests. */
131 qemu_iovec_destroy(&req
->qiov
);
135 int p
= virtio_ldl_p(VIRTIO_DEVICE(s
), &req
->out
.type
);
136 bool is_read
= !(p
& VIRTIO_BLK_T_OUT
);
137 /* Note that memory may be dirtied on read failure. If the
138 * virtio request is not completed here, as is the case for
139 * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
140 * correctly during live migration. While this is ugly,
141 * it is acceptable because the device is free to write to
142 * the memory until the request is completed (which will
143 * happen on the other side of the migration).
145 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
, true)) {
150 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
151 block_acct_done(blk_get_stats(s
->blk
), &req
->acct
);
152 virtio_blk_free_request(req
);
154 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
157 static void virtio_blk_flush_complete(void *opaque
, int ret
)
159 VirtIOBlockReq
*req
= opaque
;
160 VirtIOBlock
*s
= req
->dev
;
162 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
164 if (virtio_blk_handle_rw_error(req
, -ret
, 0, true)) {
169 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
170 block_acct_done(blk_get_stats(s
->blk
), &req
->acct
);
171 virtio_blk_free_request(req
);
174 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
177 static void virtio_blk_discard_write_zeroes_complete(void *opaque
, int ret
)
179 VirtIOBlockReq
*req
= opaque
;
180 VirtIOBlock
*s
= req
->dev
;
181 bool is_write_zeroes
= (virtio_ldl_p(VIRTIO_DEVICE(s
), &req
->out
.type
) &
182 ~VIRTIO_BLK_T_BARRIER
) == VIRTIO_BLK_T_WRITE_ZEROES
;
184 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
186 if (virtio_blk_handle_rw_error(req
, -ret
, false, is_write_zeroes
)) {
191 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
192 if (is_write_zeroes
) {
193 block_acct_done(blk_get_stats(s
->blk
), &req
->acct
);
195 virtio_blk_free_request(req
);
198 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
205 struct sg_io_hdr hdr
;
206 } VirtIOBlockIoctlReq
;
208 static void virtio_blk_ioctl_complete(void *opaque
, int status
)
210 VirtIOBlockIoctlReq
*ioctl_req
= opaque
;
211 VirtIOBlockReq
*req
= ioctl_req
->req
;
212 VirtIOBlock
*s
= req
->dev
;
213 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
214 struct virtio_scsi_inhdr
*scsi
;
215 struct sg_io_hdr
*hdr
;
217 scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
220 status
= VIRTIO_BLK_S_UNSUPP
;
221 virtio_stl_p(vdev
, &scsi
->errors
, 255);
225 hdr
= &ioctl_req
->hdr
;
227 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
228 * clear the masked_status field [hence status gets cleared too, see
229 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
230 * status has occurred. However they do set DRIVER_SENSE in driver_status
231 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
233 if (hdr
->status
== 0 && hdr
->sb_len_wr
> 0) {
234 hdr
->status
= CHECK_CONDITION
;
237 virtio_stl_p(vdev
, &scsi
->errors
,
238 hdr
->status
| (hdr
->msg_status
<< 8) |
239 (hdr
->host_status
<< 16) | (hdr
->driver_status
<< 24));
240 virtio_stl_p(vdev
, &scsi
->residual
, hdr
->resid
);
241 virtio_stl_p(vdev
, &scsi
->sense_len
, hdr
->sb_len_wr
);
242 virtio_stl_p(vdev
, &scsi
->data_len
, hdr
->dxfer_len
);
245 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
246 virtio_blk_req_complete(req
, status
);
247 virtio_blk_free_request(req
);
248 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
254 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
, VirtQueue
*vq
)
256 VirtIOBlockReq
*req
= virtqueue_pop(vq
, sizeof(VirtIOBlockReq
));
259 virtio_blk_init_request(s
, vq
, req
);
264 static int virtio_blk_handle_scsi_req(VirtIOBlockReq
*req
)
266 int status
= VIRTIO_BLK_S_OK
;
267 struct virtio_scsi_inhdr
*scsi
= NULL
;
268 VirtIOBlock
*blk
= req
->dev
;
269 VirtIODevice
*vdev
= VIRTIO_DEVICE(blk
);
270 VirtQueueElement
*elem
= &req
->elem
;
274 VirtIOBlockIoctlReq
*ioctl_req
;
279 * We require at least one output segment each for the virtio_blk_outhdr
280 * and the SCSI command block.
282 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
283 * and the sense buffer pointer in the input segments.
285 if (elem
->out_num
< 2 || elem
->in_num
< 3) {
286 status
= VIRTIO_BLK_S_IOERR
;
291 * The scsi inhdr is placed in the second-to-last input segment, just
292 * before the regular inhdr.
294 scsi
= (void *)elem
->in_sg
[elem
->in_num
- 2].iov_base
;
296 if (!virtio_has_feature(blk
->host_features
, VIRTIO_BLK_F_SCSI
)) {
297 status
= VIRTIO_BLK_S_UNSUPP
;
302 * No support for bidirection commands yet.
304 if (elem
->out_num
> 2 && elem
->in_num
> 3) {
305 status
= VIRTIO_BLK_S_UNSUPP
;
310 ioctl_req
= g_new0(VirtIOBlockIoctlReq
, 1);
311 ioctl_req
->req
= req
;
312 ioctl_req
->hdr
.interface_id
= 'S';
313 ioctl_req
->hdr
.cmd_len
= elem
->out_sg
[1].iov_len
;
314 ioctl_req
->hdr
.cmdp
= elem
->out_sg
[1].iov_base
;
315 ioctl_req
->hdr
.dxfer_len
= 0;
317 if (elem
->out_num
> 2) {
319 * If there are more than the minimally required 2 output segments
320 * there is write payload starting from the third iovec.
322 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
323 ioctl_req
->hdr
.iovec_count
= elem
->out_num
- 2;
325 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
326 ioctl_req
->hdr
.dxfer_len
+= elem
->out_sg
[i
+ 2].iov_len
;
329 ioctl_req
->hdr
.dxferp
= elem
->out_sg
+ 2;
331 } else if (elem
->in_num
> 3) {
333 * If we have more than 3 input segments the guest wants to actually
336 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
337 ioctl_req
->hdr
.iovec_count
= elem
->in_num
- 3;
338 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
339 ioctl_req
->hdr
.dxfer_len
+= elem
->in_sg
[i
].iov_len
;
342 ioctl_req
->hdr
.dxferp
= elem
->in_sg
;
345 * Some SCSI commands don't actually transfer any data.
347 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_NONE
;
350 ioctl_req
->hdr
.sbp
= elem
->in_sg
[elem
->in_num
- 3].iov_base
;
351 ioctl_req
->hdr
.mx_sb_len
= elem
->in_sg
[elem
->in_num
- 3].iov_len
;
353 acb
= blk_aio_ioctl(blk
->blk
, SG_IO
, &ioctl_req
->hdr
,
354 virtio_blk_ioctl_complete
, ioctl_req
);
357 status
= VIRTIO_BLK_S_UNSUPP
;
366 /* Just put anything nonzero so that the ioctl fails in the guest. */
368 virtio_stl_p(vdev
, &scsi
->errors
, 255);
373 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
377 status
= virtio_blk_handle_scsi_req(req
);
378 if (status
!= -EINPROGRESS
) {
379 virtio_blk_req_complete(req
, status
);
380 virtio_blk_free_request(req
);
384 static inline void submit_requests(BlockBackend
*blk
, MultiReqBuffer
*mrb
,
385 int start
, int num_reqs
, int niov
)
387 QEMUIOVector
*qiov
= &mrb
->reqs
[start
]->qiov
;
388 int64_t sector_num
= mrb
->reqs
[start
]->sector_num
;
389 bool is_write
= mrb
->is_write
;
393 struct iovec
*tmp_iov
= qiov
->iov
;
394 int tmp_niov
= qiov
->niov
;
396 /* mrb->reqs[start]->qiov was initialized from external so we can't
397 * modify it here. We need to initialize it locally and then add the
398 * external iovecs. */
399 qemu_iovec_init(qiov
, niov
);
401 for (i
= 0; i
< tmp_niov
; i
++) {
402 qemu_iovec_add(qiov
, tmp_iov
[i
].iov_base
, tmp_iov
[i
].iov_len
);
405 for (i
= start
+ 1; i
< start
+ num_reqs
; i
++) {
406 qemu_iovec_concat(qiov
, &mrb
->reqs
[i
]->qiov
, 0,
407 mrb
->reqs
[i
]->qiov
.size
);
408 mrb
->reqs
[i
- 1]->mr_next
= mrb
->reqs
[i
];
411 trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb
->reqs
[start
]->dev
),
412 mrb
, start
, num_reqs
,
413 sector_num
<< BDRV_SECTOR_BITS
,
414 qiov
->size
, is_write
);
415 block_acct_merge_done(blk_get_stats(blk
),
416 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
,
421 blk_aio_pwritev(blk
, sector_num
<< BDRV_SECTOR_BITS
, qiov
, 0,
422 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
424 blk_aio_preadv(blk
, sector_num
<< BDRV_SECTOR_BITS
, qiov
, 0,
425 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
429 static int multireq_compare(const void *a
, const void *b
)
431 const VirtIOBlockReq
*req1
= *(VirtIOBlockReq
**)a
,
432 *req2
= *(VirtIOBlockReq
**)b
;
435 * Note that we can't simply subtract sector_num1 from sector_num2
436 * here as that could overflow the return value.
438 if (req1
->sector_num
> req2
->sector_num
) {
440 } else if (req1
->sector_num
< req2
->sector_num
) {
447 static void virtio_blk_submit_multireq(BlockBackend
*blk
, MultiReqBuffer
*mrb
)
449 int i
= 0, start
= 0, num_reqs
= 0, niov
= 0, nb_sectors
= 0;
450 uint32_t max_transfer
;
451 int64_t sector_num
= 0;
453 if (mrb
->num_reqs
== 1) {
454 submit_requests(blk
, mrb
, 0, 1, -1);
459 max_transfer
= blk_get_max_transfer(mrb
->reqs
[0]->dev
->blk
);
461 qsort(mrb
->reqs
, mrb
->num_reqs
, sizeof(*mrb
->reqs
),
464 for (i
= 0; i
< mrb
->num_reqs
; i
++) {
465 VirtIOBlockReq
*req
= mrb
->reqs
[i
];
468 * NOTE: We cannot merge the requests in below situations:
469 * 1. requests are not sequential
470 * 2. merge would exceed maximum number of IOVs
471 * 3. merge would exceed maximum transfer length of backend device
473 if (sector_num
+ nb_sectors
!= req
->sector_num
||
474 niov
> blk_get_max_iov(blk
) - req
->qiov
.niov
||
475 req
->qiov
.size
> max_transfer
||
476 nb_sectors
> (max_transfer
-
477 req
->qiov
.size
) / BDRV_SECTOR_SIZE
) {
478 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
484 sector_num
= req
->sector_num
;
485 nb_sectors
= niov
= 0;
489 nb_sectors
+= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
490 niov
+= req
->qiov
.niov
;
494 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
498 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
500 VirtIOBlock
*s
= req
->dev
;
502 block_acct_start(blk_get_stats(s
->blk
), &req
->acct
, 0,
506 * Make sure all outstanding writes are posted to the backing device.
508 if (mrb
->is_write
&& mrb
->num_reqs
> 0) {
509 virtio_blk_submit_multireq(s
->blk
, mrb
);
511 blk_aio_flush(s
->blk
, virtio_blk_flush_complete
, req
);
514 static bool virtio_blk_sect_range_ok(VirtIOBlock
*dev
,
515 uint64_t sector
, size_t size
)
517 uint64_t nb_sectors
= size
>> BDRV_SECTOR_BITS
;
518 uint64_t total_sectors
;
520 if (nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
523 if (sector
& dev
->sector_mask
) {
526 if (size
% dev
->conf
.conf
.logical_block_size
) {
529 blk_get_geometry(dev
->blk
, &total_sectors
);
530 if (sector
> total_sectors
|| nb_sectors
> total_sectors
- sector
) {
536 static uint8_t virtio_blk_handle_discard_write_zeroes(VirtIOBlockReq
*req
,
537 struct virtio_blk_discard_write_zeroes
*dwz_hdr
, bool is_write_zeroes
)
539 VirtIOBlock
*s
= req
->dev
;
540 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
542 uint32_t num_sectors
, flags
, max_sectors
;
546 sector
= virtio_ldq_p(vdev
, &dwz_hdr
->sector
);
547 num_sectors
= virtio_ldl_p(vdev
, &dwz_hdr
->num_sectors
);
548 flags
= virtio_ldl_p(vdev
, &dwz_hdr
->flags
);
549 max_sectors
= is_write_zeroes
? s
->conf
.max_write_zeroes_sectors
:
550 s
->conf
.max_discard_sectors
;
553 * max_sectors is at most BDRV_REQUEST_MAX_SECTORS, this check
554 * make us sure that "num_sectors << BDRV_SECTOR_BITS" can fit in
555 * the integer variable.
557 if (unlikely(num_sectors
> max_sectors
)) {
558 err_status
= VIRTIO_BLK_S_IOERR
;
562 bytes
= num_sectors
<< BDRV_SECTOR_BITS
;
564 if (unlikely(!virtio_blk_sect_range_ok(s
, sector
, bytes
))) {
565 err_status
= VIRTIO_BLK_S_IOERR
;
570 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard
571 * and write zeroes commands if any unknown flag is set.
573 if (unlikely(flags
& ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP
)) {
574 err_status
= VIRTIO_BLK_S_UNSUPP
;
578 if (is_write_zeroes
) { /* VIRTIO_BLK_T_WRITE_ZEROES */
579 int blk_aio_flags
= 0;
581 if (flags
& VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP
) {
582 blk_aio_flags
|= BDRV_REQ_MAY_UNMAP
;
585 block_acct_start(blk_get_stats(s
->blk
), &req
->acct
, bytes
,
588 blk_aio_pwrite_zeroes(s
->blk
, sector
<< BDRV_SECTOR_BITS
,
589 bytes
, blk_aio_flags
,
590 virtio_blk_discard_write_zeroes_complete
, req
);
591 } else { /* VIRTIO_BLK_T_DISCARD */
593 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for
594 * discard commands if the unmap flag is set.
596 if (unlikely(flags
& VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP
)) {
597 err_status
= VIRTIO_BLK_S_UNSUPP
;
601 blk_aio_pdiscard(s
->blk
, sector
<< BDRV_SECTOR_BITS
, bytes
,
602 virtio_blk_discard_write_zeroes_complete
, req
);
605 return VIRTIO_BLK_S_OK
;
608 if (is_write_zeroes
) {
609 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_WRITE
);
614 static int virtio_blk_handle_request(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
617 struct iovec
*in_iov
= req
->elem
.in_sg
;
618 struct iovec
*out_iov
= req
->elem
.out_sg
;
619 unsigned in_num
= req
->elem
.in_num
;
620 unsigned out_num
= req
->elem
.out_num
;
621 VirtIOBlock
*s
= req
->dev
;
622 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
624 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
625 virtio_error(vdev
, "virtio-blk missing headers");
629 if (unlikely(iov_to_buf(out_iov
, out_num
, 0, &req
->out
,
630 sizeof(req
->out
)) != sizeof(req
->out
))) {
631 virtio_error(vdev
, "virtio-blk request outhdr too short");
635 iov_discard_front(&out_iov
, &out_num
, sizeof(req
->out
));
637 if (in_iov
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
638 virtio_error(vdev
, "virtio-blk request inhdr too short");
642 /* We always touch the last byte, so just see how big in_iov is. */
643 req
->in_len
= iov_size(in_iov
, in_num
);
644 req
->in
= (void *)in_iov
[in_num
- 1].iov_base
645 + in_iov
[in_num
- 1].iov_len
646 - sizeof(struct virtio_blk_inhdr
);
647 iov_discard_back(in_iov
, &in_num
, sizeof(struct virtio_blk_inhdr
));
649 type
= virtio_ldl_p(vdev
, &req
->out
.type
);
651 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
652 * is an optional flag. Although a guest should not send this flag if
653 * not negotiated we ignored it in the past. So keep ignoring it. */
654 switch (type
& ~(VIRTIO_BLK_T_OUT
| VIRTIO_BLK_T_BARRIER
)) {
655 case VIRTIO_BLK_T_IN
:
657 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
658 req
->sector_num
= virtio_ldq_p(vdev
, &req
->out
.sector
);
661 qemu_iovec_init_external(&req
->qiov
, out_iov
, out_num
);
662 trace_virtio_blk_handle_write(vdev
, req
, req
->sector_num
,
663 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
665 qemu_iovec_init_external(&req
->qiov
, in_iov
, in_num
);
666 trace_virtio_blk_handle_read(vdev
, req
, req
->sector_num
,
667 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
670 if (!virtio_blk_sect_range_ok(s
, req
->sector_num
, req
->qiov
.size
)) {
671 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
672 block_acct_invalid(blk_get_stats(s
->blk
),
673 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
674 virtio_blk_free_request(req
);
678 block_acct_start(blk_get_stats(s
->blk
), &req
->acct
, req
->qiov
.size
,
679 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
681 /* merge would exceed maximum number of requests or IO direction
683 if (mrb
->num_reqs
> 0 && (mrb
->num_reqs
== VIRTIO_BLK_MAX_MERGE_REQS
||
684 is_write
!= mrb
->is_write
||
685 !s
->conf
.request_merging
)) {
686 virtio_blk_submit_multireq(s
->blk
, mrb
);
689 assert(mrb
->num_reqs
< VIRTIO_BLK_MAX_MERGE_REQS
);
690 mrb
->reqs
[mrb
->num_reqs
++] = req
;
691 mrb
->is_write
= is_write
;
694 case VIRTIO_BLK_T_FLUSH
:
695 virtio_blk_handle_flush(req
, mrb
);
697 case VIRTIO_BLK_T_SCSI_CMD
:
698 virtio_blk_handle_scsi(req
);
700 case VIRTIO_BLK_T_GET_ID
:
703 * NB: per existing s/n string convention the string is
704 * terminated by '\0' only when shorter than buffer.
706 const char *serial
= s
->conf
.serial
? s
->conf
.serial
: "";
707 size_t size
= MIN(strlen(serial
) + 1,
708 MIN(iov_size(in_iov
, in_num
),
709 VIRTIO_BLK_ID_BYTES
));
710 iov_from_buf(in_iov
, in_num
, 0, serial
, size
);
711 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
712 virtio_blk_free_request(req
);
716 * VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES are defined with
717 * VIRTIO_BLK_T_OUT flag set. We masked this flag in the switch statement,
718 * so we must mask it for these requests, then we will check if it is set.
720 case VIRTIO_BLK_T_DISCARD
& ~VIRTIO_BLK_T_OUT
:
721 case VIRTIO_BLK_T_WRITE_ZEROES
& ~VIRTIO_BLK_T_OUT
:
723 struct virtio_blk_discard_write_zeroes dwz_hdr
;
724 size_t out_len
= iov_size(out_iov
, out_num
);
725 bool is_write_zeroes
= (type
& ~VIRTIO_BLK_T_BARRIER
) ==
726 VIRTIO_BLK_T_WRITE_ZEROES
;
730 * Unsupported if VIRTIO_BLK_T_OUT is not set or the request contains
731 * more than one segment.
733 if (unlikely(!(type
& VIRTIO_BLK_T_OUT
) ||
734 out_len
> sizeof(dwz_hdr
))) {
735 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
736 virtio_blk_free_request(req
);
740 if (unlikely(iov_to_buf(out_iov
, out_num
, 0, &dwz_hdr
,
741 sizeof(dwz_hdr
)) != sizeof(dwz_hdr
))) {
742 virtio_error(vdev
, "virtio-blk discard/write_zeroes header"
747 err_status
= virtio_blk_handle_discard_write_zeroes(req
, &dwz_hdr
,
749 if (err_status
!= VIRTIO_BLK_S_OK
) {
750 virtio_blk_req_complete(req
, err_status
);
751 virtio_blk_free_request(req
);
757 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
758 virtio_blk_free_request(req
);
763 bool virtio_blk_handle_vq(VirtIOBlock
*s
, VirtQueue
*vq
)
766 MultiReqBuffer mrb
= {};
767 bool suppress_notifications
= virtio_queue_get_notification(vq
);
768 bool progress
= false;
770 aio_context_acquire(blk_get_aio_context(s
->blk
));
774 if (suppress_notifications
) {
775 virtio_queue_set_notification(vq
, 0);
778 while ((req
= virtio_blk_get_request(s
, vq
))) {
780 if (virtio_blk_handle_request(req
, &mrb
)) {
781 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
782 virtio_blk_free_request(req
);
787 if (suppress_notifications
) {
788 virtio_queue_set_notification(vq
, 1);
790 } while (!virtio_queue_empty(vq
));
793 virtio_blk_submit_multireq(s
->blk
, &mrb
);
796 blk_io_unplug(s
->blk
);
797 aio_context_release(blk_get_aio_context(s
->blk
));
801 static void virtio_blk_handle_output_do(VirtIOBlock
*s
, VirtQueue
*vq
)
803 virtio_blk_handle_vq(s
, vq
);
806 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
808 VirtIOBlock
*s
= (VirtIOBlock
*)vdev
;
811 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
812 * dataplane here instead of waiting for .set_status().
814 virtio_device_start_ioeventfd(vdev
);
815 if (!s
->dataplane_disabled
) {
819 virtio_blk_handle_output_do(s
, vq
);
822 void virtio_blk_process_queued_requests(VirtIOBlock
*s
, bool is_bh
)
824 VirtIOBlockReq
*req
= s
->rq
;
825 MultiReqBuffer mrb
= {};
829 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
831 VirtIOBlockReq
*next
= req
->next
;
832 if (virtio_blk_handle_request(req
, &mrb
)) {
833 /* Device is now broken and won't do any processing until it gets
834 * reset. Already queued requests will be lost: let's purge them.
838 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
839 virtio_blk_free_request(req
);
848 virtio_blk_submit_multireq(s
->blk
, &mrb
);
851 blk_dec_in_flight(s
->conf
.conf
.blk
);
853 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
856 static void virtio_blk_dma_restart_bh(void *opaque
)
858 VirtIOBlock
*s
= opaque
;
860 qemu_bh_delete(s
->bh
);
863 virtio_blk_process_queued_requests(s
, true);
866 static void virtio_blk_dma_restart_cb(void *opaque
, int running
,
869 VirtIOBlock
*s
= opaque
;
870 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
)));
871 VirtioBusState
*bus
= VIRTIO_BUS(qbus
);
878 * If ioeventfd is enabled, don't schedule the BH here as queued
879 * requests will be processed while starting the data plane.
881 if (!s
->bh
&& !virtio_bus_ioeventfd_enabled(bus
)) {
882 s
->bh
= aio_bh_new(blk_get_aio_context(s
->conf
.conf
.blk
),
883 virtio_blk_dma_restart_bh
, s
);
884 blk_inc_in_flight(s
->conf
.conf
.blk
);
885 qemu_bh_schedule(s
->bh
);
889 static void virtio_blk_reset(VirtIODevice
*vdev
)
891 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
895 ctx
= blk_get_aio_context(s
->blk
);
896 aio_context_acquire(ctx
);
899 /* We drop queued requests after blk_drain() because blk_drain() itself can
904 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
905 virtio_blk_free_request(req
);
908 aio_context_release(ctx
);
910 assert(!s
->dataplane_started
);
911 blk_set_enable_write_cache(s
->blk
, s
->original_wce
);
914 /* coalesce internal state, copy to pci i/o region 0
916 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
918 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
919 BlockConf
*conf
= &s
->conf
.conf
;
920 struct virtio_blk_config blkcfg
;
923 int blk_size
= conf
->logical_block_size
;
925 blk_get_geometry(s
->blk
, &capacity
);
926 memset(&blkcfg
, 0, sizeof(blkcfg
));
927 virtio_stq_p(vdev
, &blkcfg
.capacity
, capacity
);
928 virtio_stl_p(vdev
, &blkcfg
.seg_max
,
929 s
->conf
.seg_max_adjust
? s
->conf
.queue_size
- 2 : 128 - 2);
930 virtio_stw_p(vdev
, &blkcfg
.geometry
.cylinders
, conf
->cyls
);
931 virtio_stl_p(vdev
, &blkcfg
.blk_size
, blk_size
);
932 virtio_stw_p(vdev
, &blkcfg
.min_io_size
, conf
->min_io_size
/ blk_size
);
933 virtio_stl_p(vdev
, &blkcfg
.opt_io_size
, conf
->opt_io_size
/ blk_size
);
934 blkcfg
.geometry
.heads
= conf
->heads
;
936 * We must ensure that the block device capacity is a multiple of
937 * the logical block size. If that is not the case, let's use
938 * sector_mask to adopt the geometry to have a correct picture.
939 * For those devices where the capacity is ok for the given geometry
940 * we don't touch the sector value of the geometry, since some devices
941 * (like s390 dasd) need a specific value. Here the capacity is already
942 * cyls*heads*secs*blk_size and the sector value is not block size
943 * divided by 512 - instead it is the amount of blk_size blocks
944 * per track (cylinder).
946 length
= blk_getlength(s
->blk
);
947 if (length
> 0 && length
/ conf
->heads
/ conf
->secs
% blk_size
) {
948 blkcfg
.geometry
.sectors
= conf
->secs
& ~s
->sector_mask
;
950 blkcfg
.geometry
.sectors
= conf
->secs
;
953 blkcfg
.physical_block_exp
= get_physical_block_exp(conf
);
954 blkcfg
.alignment_offset
= 0;
955 blkcfg
.wce
= blk_enable_write_cache(s
->blk
);
956 virtio_stw_p(vdev
, &blkcfg
.num_queues
, s
->conf
.num_queues
);
957 if (virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_DISCARD
)) {
958 virtio_stl_p(vdev
, &blkcfg
.max_discard_sectors
,
959 s
->conf
.max_discard_sectors
);
960 virtio_stl_p(vdev
, &blkcfg
.discard_sector_alignment
,
961 blk_size
>> BDRV_SECTOR_BITS
);
963 * We support only one segment per request since multiple segments
964 * are not widely used and there are no userspace APIs that allow
965 * applications to submit multiple segments in a single call.
967 virtio_stl_p(vdev
, &blkcfg
.max_discard_seg
, 1);
969 if (virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_WRITE_ZEROES
)) {
970 virtio_stl_p(vdev
, &blkcfg
.max_write_zeroes_sectors
,
971 s
->conf
.max_write_zeroes_sectors
);
972 blkcfg
.write_zeroes_may_unmap
= 1;
973 virtio_stl_p(vdev
, &blkcfg
.max_write_zeroes_seg
, 1);
975 memcpy(config
, &blkcfg
, s
->config_size
);
978 static void virtio_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
980 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
981 struct virtio_blk_config blkcfg
;
983 memcpy(&blkcfg
, config
, s
->config_size
);
985 aio_context_acquire(blk_get_aio_context(s
->blk
));
986 blk_set_enable_write_cache(s
->blk
, blkcfg
.wce
!= 0);
987 aio_context_release(blk_get_aio_context(s
->blk
));
990 static uint64_t virtio_blk_get_features(VirtIODevice
*vdev
, uint64_t features
,
993 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
995 /* Firstly sync all virtio-blk possible supported features */
996 features
|= s
->host_features
;
998 virtio_add_feature(&features
, VIRTIO_BLK_F_SEG_MAX
);
999 virtio_add_feature(&features
, VIRTIO_BLK_F_GEOMETRY
);
1000 virtio_add_feature(&features
, VIRTIO_BLK_F_TOPOLOGY
);
1001 virtio_add_feature(&features
, VIRTIO_BLK_F_BLK_SIZE
);
1002 if (virtio_has_feature(features
, VIRTIO_F_VERSION_1
)) {
1003 if (virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_SCSI
)) {
1004 error_setg(errp
, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
1008 virtio_clear_feature(&features
, VIRTIO_F_ANY_LAYOUT
);
1009 virtio_add_feature(&features
, VIRTIO_BLK_F_SCSI
);
1012 if (blk_enable_write_cache(s
->blk
) ||
1013 (s
->conf
.x_enable_wce_if_config_wce
&&
1014 virtio_has_feature(features
, VIRTIO_BLK_F_CONFIG_WCE
))) {
1015 virtio_add_feature(&features
, VIRTIO_BLK_F_WCE
);
1017 if (blk_is_read_only(s
->blk
)) {
1018 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
1020 if (s
->conf
.num_queues
> 1) {
1021 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
1027 static void virtio_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
1029 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1031 if (!(status
& (VIRTIO_CONFIG_S_DRIVER
| VIRTIO_CONFIG_S_DRIVER_OK
))) {
1032 assert(!s
->dataplane_started
);
1035 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1039 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
1040 * cache flushes. Thus, the "auto writethrough" behavior is never
1041 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
1042 * Leaving it enabled would break the following sequence:
1044 * Guest started with "-drive cache=writethrough"
1045 * Guest sets status to 0
1046 * Guest sets DRIVER bit in status field
1047 * Guest reads host features (WCE=0, CONFIG_WCE=1)
1048 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
1049 * Guest writes 1 to the WCE configuration field (writeback mode)
1050 * Guest sets DRIVER_OK bit in status field
1052 * s->blk would erroneously be placed in writethrough mode.
1054 if (!virtio_vdev_has_feature(vdev
, VIRTIO_BLK_F_CONFIG_WCE
)) {
1055 aio_context_acquire(blk_get_aio_context(s
->blk
));
1056 blk_set_enable_write_cache(s
->blk
,
1057 virtio_vdev_has_feature(vdev
,
1059 aio_context_release(blk_get_aio_context(s
->blk
));
1063 static void virtio_blk_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
1065 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1066 VirtIOBlockReq
*req
= s
->rq
;
1069 qemu_put_sbyte(f
, 1);
1071 if (s
->conf
.num_queues
> 1) {
1072 qemu_put_be32(f
, virtio_get_queue_index(req
->vq
));
1075 qemu_put_virtqueue_element(vdev
, f
, &req
->elem
);
1078 qemu_put_sbyte(f
, 0);
1081 static int virtio_blk_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
1084 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1086 while (qemu_get_sbyte(f
)) {
1087 unsigned nvqs
= s
->conf
.num_queues
;
1088 unsigned vq_idx
= 0;
1089 VirtIOBlockReq
*req
;
1092 vq_idx
= qemu_get_be32(f
);
1094 if (vq_idx
>= nvqs
) {
1095 error_report("Invalid virtqueue index in request list: %#x",
1101 req
= qemu_get_virtqueue_element(vdev
, f
, sizeof(VirtIOBlockReq
));
1102 virtio_blk_init_request(s
, virtio_get_queue(vdev
, vq_idx
), req
);
1110 static void virtio_resize_cb(void *opaque
)
1112 VirtIODevice
*vdev
= opaque
;
1114 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1115 virtio_notify_config(vdev
);
1118 static void virtio_blk_resize(void *opaque
)
1120 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
1123 * virtio_notify_config() needs to acquire the global mutex,
1124 * so it can't be called from an iothread. Instead, schedule
1125 * it to be run in the main context BH.
1127 aio_bh_schedule_oneshot(qemu_get_aio_context(), virtio_resize_cb
, vdev
);
1130 static const BlockDevOps virtio_block_ops
= {
1131 .resize_cb
= virtio_blk_resize
,
1134 static void virtio_blk_device_realize(DeviceState
*dev
, Error
**errp
)
1136 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1137 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
1138 VirtIOBlkConf
*conf
= &s
->conf
;
1142 if (!conf
->conf
.blk
) {
1143 error_setg(errp
, "drive property not set");
1146 if (!blk_is_inserted(conf
->conf
.blk
)) {
1147 error_setg(errp
, "Device needs media, but drive is empty");
1150 if (!conf
->num_queues
) {
1151 error_setg(errp
, "num-queues property must be larger than 0");
1154 if (conf
->queue_size
<= 2) {
1155 error_setg(errp
, "invalid queue-size property (%" PRIu16
"), "
1156 "must be > 2", conf
->queue_size
);
1159 if (!is_power_of_2(conf
->queue_size
) ||
1160 conf
->queue_size
> VIRTQUEUE_MAX_SIZE
) {
1161 error_setg(errp
, "invalid queue-size property (%" PRIu16
"), "
1162 "must be a power of 2 (max %d)",
1163 conf
->queue_size
, VIRTQUEUE_MAX_SIZE
);
1167 if (!blkconf_apply_backend_options(&conf
->conf
,
1168 blk_is_read_only(conf
->conf
.blk
), true,
1172 s
->original_wce
= blk_enable_write_cache(conf
->conf
.blk
);
1173 if (!blkconf_geometry(&conf
->conf
, NULL
, 65535, 255, 255, errp
)) {
1177 if (!blkconf_blocksizes(&conf
->conf
, errp
)) {
1181 if (virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_DISCARD
) &&
1182 (!conf
->max_discard_sectors
||
1183 conf
->max_discard_sectors
> BDRV_REQUEST_MAX_SECTORS
)) {
1184 error_setg(errp
, "invalid max-discard-sectors property (%" PRIu32
")"
1185 ", must be between 1 and %d",
1186 conf
->max_discard_sectors
, (int)BDRV_REQUEST_MAX_SECTORS
);
1190 if (virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_WRITE_ZEROES
) &&
1191 (!conf
->max_write_zeroes_sectors
||
1192 conf
->max_write_zeroes_sectors
> BDRV_REQUEST_MAX_SECTORS
)) {
1193 error_setg(errp
, "invalid max-write-zeroes-sectors property (%" PRIu32
1194 "), must be between 1 and %d",
1195 conf
->max_write_zeroes_sectors
,
1196 (int)BDRV_REQUEST_MAX_SECTORS
);
1200 virtio_blk_set_config_size(s
, s
->host_features
);
1202 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
, s
->config_size
);
1204 s
->blk
= conf
->conf
.blk
;
1206 s
->sector_mask
= (s
->conf
.conf
.logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
1208 for (i
= 0; i
< conf
->num_queues
; i
++) {
1209 virtio_add_queue(vdev
, conf
->queue_size
, virtio_blk_handle_output
);
1211 virtio_blk_data_plane_create(vdev
, conf
, &s
->dataplane
, &err
);
1213 error_propagate(errp
, err
);
1214 for (i
= 0; i
< conf
->num_queues
; i
++) {
1215 virtio_del_queue(vdev
, i
);
1217 virtio_cleanup(vdev
);
1221 s
->change
= qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
1222 blk_set_dev_ops(s
->blk
, &virtio_block_ops
, s
);
1223 blk_set_guest_block_size(s
->blk
, s
->conf
.conf
.logical_block_size
);
1225 blk_iostatus_enable(s
->blk
);
1227 add_boot_device_lchs(dev
, "/disk@0,0",
1233 static void virtio_blk_device_unrealize(DeviceState
*dev
)
1235 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1236 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
1237 VirtIOBlkConf
*conf
= &s
->conf
;
1241 del_boot_device_lchs(dev
, "/disk@0,0");
1242 virtio_blk_data_plane_destroy(s
->dataplane
);
1243 s
->dataplane
= NULL
;
1244 for (i
= 0; i
< conf
->num_queues
; i
++) {
1245 virtio_del_queue(vdev
, i
);
1247 qemu_del_vm_change_state_handler(s
->change
);
1248 blockdev_mark_auto_del(s
->blk
);
1249 virtio_cleanup(vdev
);
1252 static void virtio_blk_instance_init(Object
*obj
)
1254 VirtIOBlock
*s
= VIRTIO_BLK(obj
);
1256 device_add_bootindex_property(obj
, &s
->conf
.conf
.bootindex
,
1257 "bootindex", "/disk@0,0",
1261 static const VMStateDescription vmstate_virtio_blk
= {
1262 .name
= "virtio-blk",
1263 .minimum_version_id
= 2,
1265 .fields
= (VMStateField
[]) {
1266 VMSTATE_VIRTIO_DEVICE
,
1267 VMSTATE_END_OF_LIST()
1271 static Property virtio_blk_properties
[] = {
1272 DEFINE_BLOCK_PROPERTIES(VirtIOBlock
, conf
.conf
),
1273 DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock
, conf
.conf
),
1274 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock
, conf
.conf
),
1275 DEFINE_PROP_STRING("serial", VirtIOBlock
, conf
.serial
),
1276 DEFINE_PROP_BIT64("config-wce", VirtIOBlock
, host_features
,
1277 VIRTIO_BLK_F_CONFIG_WCE
, true),
1279 DEFINE_PROP_BIT64("scsi", VirtIOBlock
, host_features
,
1280 VIRTIO_BLK_F_SCSI
, false),
1282 DEFINE_PROP_BIT("request-merging", VirtIOBlock
, conf
.request_merging
, 0,
1284 DEFINE_PROP_UINT16("num-queues", VirtIOBlock
, conf
.num_queues
, 1),
1285 DEFINE_PROP_UINT16("queue-size", VirtIOBlock
, conf
.queue_size
, 256),
1286 DEFINE_PROP_BOOL("seg-max-adjust", VirtIOBlock
, conf
.seg_max_adjust
, true),
1287 DEFINE_PROP_LINK("iothread", VirtIOBlock
, conf
.iothread
, TYPE_IOTHREAD
,
1289 DEFINE_PROP_BIT64("discard", VirtIOBlock
, host_features
,
1290 VIRTIO_BLK_F_DISCARD
, true),
1291 DEFINE_PROP_BIT64("write-zeroes", VirtIOBlock
, host_features
,
1292 VIRTIO_BLK_F_WRITE_ZEROES
, true),
1293 DEFINE_PROP_UINT32("max-discard-sectors", VirtIOBlock
,
1294 conf
.max_discard_sectors
, BDRV_REQUEST_MAX_SECTORS
),
1295 DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock
,
1296 conf
.max_write_zeroes_sectors
, BDRV_REQUEST_MAX_SECTORS
),
1297 DEFINE_PROP_BOOL("x-enable-wce-if-config-wce", VirtIOBlock
,
1298 conf
.x_enable_wce_if_config_wce
, true),
1299 DEFINE_PROP_END_OF_LIST(),
1302 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
1304 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1305 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1307 device_class_set_props(dc
, virtio_blk_properties
);
1308 dc
->vmsd
= &vmstate_virtio_blk
;
1309 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1310 vdc
->realize
= virtio_blk_device_realize
;
1311 vdc
->unrealize
= virtio_blk_device_unrealize
;
1312 vdc
->get_config
= virtio_blk_update_config
;
1313 vdc
->set_config
= virtio_blk_set_config
;
1314 vdc
->get_features
= virtio_blk_get_features
;
1315 vdc
->set_status
= virtio_blk_set_status
;
1316 vdc
->reset
= virtio_blk_reset
;
1317 vdc
->save
= virtio_blk_save_device
;
1318 vdc
->load
= virtio_blk_load_device
;
1319 vdc
->start_ioeventfd
= virtio_blk_data_plane_start
;
1320 vdc
->stop_ioeventfd
= virtio_blk_data_plane_stop
;
1323 static const TypeInfo virtio_blk_info
= {
1324 .name
= TYPE_VIRTIO_BLK
,
1325 .parent
= TYPE_VIRTIO_DEVICE
,
1326 .instance_size
= sizeof(VirtIOBlock
),
1327 .instance_init
= virtio_blk_instance_init
,
1328 .class_init
= virtio_blk_class_init
,
1331 static void virtio_register_types(void)
1333 type_register_static(&virtio_blk_info
);
1336 type_init(virtio_register_types
)