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"
16 #include "qemu-common.h"
18 #include "qemu/error-report.h"
20 #include "hw/block/block.h"
21 #include "sysemu/block-backend.h"
22 #include "sysemu/blockdev.h"
23 #include "hw/virtio/virtio-blk.h"
24 #include "dataplane/virtio-blk.h"
25 #include "scsi/constants.h"
29 #include "hw/virtio/virtio-bus.h"
30 #include "hw/virtio/virtio-access.h"
32 static void virtio_blk_init_request(VirtIOBlock
*s
, VirtQueue
*vq
,
43 static void virtio_blk_free_request(VirtIOBlockReq
*req
)
48 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, unsigned char status
)
50 VirtIOBlock
*s
= req
->dev
;
51 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
53 trace_virtio_blk_req_complete(vdev
, req
, status
);
55 stb_p(&req
->in
->status
, status
);
56 virtqueue_push(req
->vq
, &req
->elem
, req
->in_len
);
57 if (s
->dataplane_started
&& !s
->dataplane_disabled
) {
58 virtio_blk_data_plane_notify(s
->dataplane
, req
->vq
);
60 virtio_notify(vdev
, req
->vq
);
64 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
67 BlockErrorAction action
= blk_get_error_action(req
->dev
->blk
,
69 VirtIOBlock
*s
= req
->dev
;
71 if (action
== BLOCK_ERROR_ACTION_STOP
) {
72 /* Break the link as the next request is going to be parsed from the
73 * ring again. Otherwise we may end up doing a double completion! */
77 } else if (action
== BLOCK_ERROR_ACTION_REPORT
) {
78 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
79 block_acct_failed(blk_get_stats(s
->blk
), &req
->acct
);
80 virtio_blk_free_request(req
);
83 blk_error_action(s
->blk
, action
, is_read
, error
);
84 return action
!= BLOCK_ERROR_ACTION_IGNORE
;
87 static void virtio_blk_rw_complete(void *opaque
, int ret
)
89 VirtIOBlockReq
*next
= opaque
;
90 VirtIOBlock
*s
= next
->dev
;
91 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
93 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
95 VirtIOBlockReq
*req
= next
;
97 trace_virtio_blk_rw_complete(vdev
, req
, ret
);
99 if (req
->qiov
.nalloc
!= -1) {
100 /* If nalloc is != 1 req->qiov is a local copy of the original
101 * external iovec. It was allocated in submit_merged_requests
102 * to be able to merge requests. */
103 qemu_iovec_destroy(&req
->qiov
);
107 int p
= virtio_ldl_p(VIRTIO_DEVICE(req
->dev
), &req
->out
.type
);
108 bool is_read
= !(p
& VIRTIO_BLK_T_OUT
);
109 /* Note that memory may be dirtied on read failure. If the
110 * virtio request is not completed here, as is the case for
111 * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
112 * correctly during live migration. While this is ugly,
113 * it is acceptable because the device is free to write to
114 * the memory until the request is completed (which will
115 * happen on the other side of the migration).
117 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
)) {
122 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
123 block_acct_done(blk_get_stats(req
->dev
->blk
), &req
->acct
);
124 virtio_blk_free_request(req
);
126 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
129 static void virtio_blk_flush_complete(void *opaque
, int ret
)
131 VirtIOBlockReq
*req
= opaque
;
132 VirtIOBlock
*s
= req
->dev
;
134 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
136 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
141 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
142 block_acct_done(blk_get_stats(req
->dev
->blk
), &req
->acct
);
143 virtio_blk_free_request(req
);
146 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
153 struct sg_io_hdr hdr
;
154 } VirtIOBlockIoctlReq
;
156 static void virtio_blk_ioctl_complete(void *opaque
, int status
)
158 VirtIOBlockIoctlReq
*ioctl_req
= opaque
;
159 VirtIOBlockReq
*req
= ioctl_req
->req
;
160 VirtIOBlock
*s
= req
->dev
;
161 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
162 struct virtio_scsi_inhdr
*scsi
;
163 struct sg_io_hdr
*hdr
;
165 scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
168 status
= VIRTIO_BLK_S_UNSUPP
;
169 virtio_stl_p(vdev
, &scsi
->errors
, 255);
173 hdr
= &ioctl_req
->hdr
;
175 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
176 * clear the masked_status field [hence status gets cleared too, see
177 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
178 * status has occurred. However they do set DRIVER_SENSE in driver_status
179 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
181 if (hdr
->status
== 0 && hdr
->sb_len_wr
> 0) {
182 hdr
->status
= CHECK_CONDITION
;
185 virtio_stl_p(vdev
, &scsi
->errors
,
186 hdr
->status
| (hdr
->msg_status
<< 8) |
187 (hdr
->host_status
<< 16) | (hdr
->driver_status
<< 24));
188 virtio_stl_p(vdev
, &scsi
->residual
, hdr
->resid
);
189 virtio_stl_p(vdev
, &scsi
->sense_len
, hdr
->sb_len_wr
);
190 virtio_stl_p(vdev
, &scsi
->data_len
, hdr
->dxfer_len
);
193 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
194 virtio_blk_req_complete(req
, status
);
195 virtio_blk_free_request(req
);
196 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
202 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
, VirtQueue
*vq
)
204 VirtIOBlockReq
*req
= virtqueue_pop(vq
, sizeof(VirtIOBlockReq
));
207 virtio_blk_init_request(s
, vq
, req
);
212 static int virtio_blk_handle_scsi_req(VirtIOBlockReq
*req
)
214 int status
= VIRTIO_BLK_S_OK
;
215 struct virtio_scsi_inhdr
*scsi
= NULL
;
216 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
217 VirtQueueElement
*elem
= &req
->elem
;
218 VirtIOBlock
*blk
= req
->dev
;
222 VirtIOBlockIoctlReq
*ioctl_req
;
227 * We require at least one output segment each for the virtio_blk_outhdr
228 * and the SCSI command block.
230 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
231 * and the sense buffer pointer in the input segments.
233 if (elem
->out_num
< 2 || elem
->in_num
< 3) {
234 status
= VIRTIO_BLK_S_IOERR
;
239 * The scsi inhdr is placed in the second-to-last input segment, just
240 * before the regular inhdr.
242 scsi
= (void *)elem
->in_sg
[elem
->in_num
- 2].iov_base
;
244 if (!blk
->conf
.scsi
) {
245 status
= VIRTIO_BLK_S_UNSUPP
;
250 * No support for bidirection commands yet.
252 if (elem
->out_num
> 2 && elem
->in_num
> 3) {
253 status
= VIRTIO_BLK_S_UNSUPP
;
258 ioctl_req
= g_new0(VirtIOBlockIoctlReq
, 1);
259 ioctl_req
->req
= req
;
260 ioctl_req
->hdr
.interface_id
= 'S';
261 ioctl_req
->hdr
.cmd_len
= elem
->out_sg
[1].iov_len
;
262 ioctl_req
->hdr
.cmdp
= elem
->out_sg
[1].iov_base
;
263 ioctl_req
->hdr
.dxfer_len
= 0;
265 if (elem
->out_num
> 2) {
267 * If there are more than the minimally required 2 output segments
268 * there is write payload starting from the third iovec.
270 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
271 ioctl_req
->hdr
.iovec_count
= elem
->out_num
- 2;
273 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
274 ioctl_req
->hdr
.dxfer_len
+= elem
->out_sg
[i
+ 2].iov_len
;
277 ioctl_req
->hdr
.dxferp
= elem
->out_sg
+ 2;
279 } else if (elem
->in_num
> 3) {
281 * If we have more than 3 input segments the guest wants to actually
284 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
285 ioctl_req
->hdr
.iovec_count
= elem
->in_num
- 3;
286 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
287 ioctl_req
->hdr
.dxfer_len
+= elem
->in_sg
[i
].iov_len
;
290 ioctl_req
->hdr
.dxferp
= elem
->in_sg
;
293 * Some SCSI commands don't actually transfer any data.
295 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_NONE
;
298 ioctl_req
->hdr
.sbp
= elem
->in_sg
[elem
->in_num
- 3].iov_base
;
299 ioctl_req
->hdr
.mx_sb_len
= elem
->in_sg
[elem
->in_num
- 3].iov_len
;
301 acb
= blk_aio_ioctl(blk
->blk
, SG_IO
, &ioctl_req
->hdr
,
302 virtio_blk_ioctl_complete
, ioctl_req
);
305 status
= VIRTIO_BLK_S_UNSUPP
;
314 /* Just put anything nonzero so that the ioctl fails in the guest. */
316 virtio_stl_p(vdev
, &scsi
->errors
, 255);
321 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
325 status
= virtio_blk_handle_scsi_req(req
);
326 if (status
!= -EINPROGRESS
) {
327 virtio_blk_req_complete(req
, status
);
328 virtio_blk_free_request(req
);
332 static inline void submit_requests(BlockBackend
*blk
, MultiReqBuffer
*mrb
,
333 int start
, int num_reqs
, int niov
)
335 QEMUIOVector
*qiov
= &mrb
->reqs
[start
]->qiov
;
336 int64_t sector_num
= mrb
->reqs
[start
]->sector_num
;
337 bool is_write
= mrb
->is_write
;
341 struct iovec
*tmp_iov
= qiov
->iov
;
342 int tmp_niov
= qiov
->niov
;
344 /* mrb->reqs[start]->qiov was initialized from external so we can't
345 * modify it here. We need to initialize it locally and then add the
346 * external iovecs. */
347 qemu_iovec_init(qiov
, niov
);
349 for (i
= 0; i
< tmp_niov
; i
++) {
350 qemu_iovec_add(qiov
, tmp_iov
[i
].iov_base
, tmp_iov
[i
].iov_len
);
353 for (i
= start
+ 1; i
< start
+ num_reqs
; i
++) {
354 qemu_iovec_concat(qiov
, &mrb
->reqs
[i
]->qiov
, 0,
355 mrb
->reqs
[i
]->qiov
.size
);
356 mrb
->reqs
[i
- 1]->mr_next
= mrb
->reqs
[i
];
359 trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb
->reqs
[start
]->dev
),
360 mrb
, start
, num_reqs
,
361 sector_num
<< BDRV_SECTOR_BITS
,
362 qiov
->size
, is_write
);
363 block_acct_merge_done(blk_get_stats(blk
),
364 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
,
369 blk_aio_pwritev(blk
, sector_num
<< BDRV_SECTOR_BITS
, qiov
, 0,
370 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
372 blk_aio_preadv(blk
, sector_num
<< BDRV_SECTOR_BITS
, qiov
, 0,
373 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
377 static int multireq_compare(const void *a
, const void *b
)
379 const VirtIOBlockReq
*req1
= *(VirtIOBlockReq
**)a
,
380 *req2
= *(VirtIOBlockReq
**)b
;
383 * Note that we can't simply subtract sector_num1 from sector_num2
384 * here as that could overflow the return value.
386 if (req1
->sector_num
> req2
->sector_num
) {
388 } else if (req1
->sector_num
< req2
->sector_num
) {
395 static void virtio_blk_submit_multireq(BlockBackend
*blk
, MultiReqBuffer
*mrb
)
397 int i
= 0, start
= 0, num_reqs
= 0, niov
= 0, nb_sectors
= 0;
398 uint32_t max_transfer
;
399 int64_t sector_num
= 0;
401 if (mrb
->num_reqs
== 1) {
402 submit_requests(blk
, mrb
, 0, 1, -1);
407 max_transfer
= blk_get_max_transfer(mrb
->reqs
[0]->dev
->blk
);
409 qsort(mrb
->reqs
, mrb
->num_reqs
, sizeof(*mrb
->reqs
),
412 for (i
= 0; i
< mrb
->num_reqs
; i
++) {
413 VirtIOBlockReq
*req
= mrb
->reqs
[i
];
416 * NOTE: We cannot merge the requests in below situations:
417 * 1. requests are not sequential
418 * 2. merge would exceed maximum number of IOVs
419 * 3. merge would exceed maximum transfer length of backend device
421 if (sector_num
+ nb_sectors
!= req
->sector_num
||
422 niov
> blk_get_max_iov(blk
) - req
->qiov
.niov
||
423 req
->qiov
.size
> max_transfer
||
424 nb_sectors
> (max_transfer
-
425 req
->qiov
.size
) / BDRV_SECTOR_SIZE
) {
426 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
432 sector_num
= req
->sector_num
;
433 nb_sectors
= niov
= 0;
437 nb_sectors
+= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
438 niov
+= req
->qiov
.niov
;
442 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
446 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
448 block_acct_start(blk_get_stats(req
->dev
->blk
), &req
->acct
, 0,
452 * Make sure all outstanding writes are posted to the backing device.
454 if (mrb
->is_write
&& mrb
->num_reqs
> 0) {
455 virtio_blk_submit_multireq(req
->dev
->blk
, mrb
);
457 blk_aio_flush(req
->dev
->blk
, virtio_blk_flush_complete
, req
);
460 static bool virtio_blk_sect_range_ok(VirtIOBlock
*dev
,
461 uint64_t sector
, size_t size
)
463 uint64_t nb_sectors
= size
>> BDRV_SECTOR_BITS
;
464 uint64_t total_sectors
;
466 if (nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
469 if (sector
& dev
->sector_mask
) {
472 if (size
% dev
->conf
.conf
.logical_block_size
) {
475 blk_get_geometry(dev
->blk
, &total_sectors
);
476 if (sector
> total_sectors
|| nb_sectors
> total_sectors
- sector
) {
482 static int virtio_blk_handle_request(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
485 struct iovec
*in_iov
= req
->elem
.in_sg
;
486 struct iovec
*iov
= req
->elem
.out_sg
;
487 unsigned in_num
= req
->elem
.in_num
;
488 unsigned out_num
= req
->elem
.out_num
;
489 VirtIOBlock
*s
= req
->dev
;
490 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
492 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
493 virtio_error(vdev
, "virtio-blk missing headers");
497 if (unlikely(iov_to_buf(iov
, out_num
, 0, &req
->out
,
498 sizeof(req
->out
)) != sizeof(req
->out
))) {
499 virtio_error(vdev
, "virtio-blk request outhdr too short");
503 iov_discard_front(&iov
, &out_num
, sizeof(req
->out
));
505 if (in_iov
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
506 virtio_error(vdev
, "virtio-blk request inhdr too short");
510 /* We always touch the last byte, so just see how big in_iov is. */
511 req
->in_len
= iov_size(in_iov
, in_num
);
512 req
->in
= (void *)in_iov
[in_num
- 1].iov_base
513 + in_iov
[in_num
- 1].iov_len
514 - sizeof(struct virtio_blk_inhdr
);
515 iov_discard_back(in_iov
, &in_num
, sizeof(struct virtio_blk_inhdr
));
517 type
= virtio_ldl_p(VIRTIO_DEVICE(req
->dev
), &req
->out
.type
);
519 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
520 * is an optional flag. Although a guest should not send this flag if
521 * not negotiated we ignored it in the past. So keep ignoring it. */
522 switch (type
& ~(VIRTIO_BLK_T_OUT
| VIRTIO_BLK_T_BARRIER
)) {
523 case VIRTIO_BLK_T_IN
:
525 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
526 req
->sector_num
= virtio_ldq_p(VIRTIO_DEVICE(req
->dev
),
530 qemu_iovec_init_external(&req
->qiov
, iov
, out_num
);
531 trace_virtio_blk_handle_write(vdev
, req
, req
->sector_num
,
532 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
534 qemu_iovec_init_external(&req
->qiov
, in_iov
, in_num
);
535 trace_virtio_blk_handle_read(vdev
, req
, req
->sector_num
,
536 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
539 if (!virtio_blk_sect_range_ok(req
->dev
, req
->sector_num
,
541 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
542 block_acct_invalid(blk_get_stats(req
->dev
->blk
),
543 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
544 virtio_blk_free_request(req
);
548 block_acct_start(blk_get_stats(req
->dev
->blk
),
549 &req
->acct
, req
->qiov
.size
,
550 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
552 /* merge would exceed maximum number of requests or IO direction
554 if (mrb
->num_reqs
> 0 && (mrb
->num_reqs
== VIRTIO_BLK_MAX_MERGE_REQS
||
555 is_write
!= mrb
->is_write
||
556 !req
->dev
->conf
.request_merging
)) {
557 virtio_blk_submit_multireq(req
->dev
->blk
, mrb
);
560 assert(mrb
->num_reqs
< VIRTIO_BLK_MAX_MERGE_REQS
);
561 mrb
->reqs
[mrb
->num_reqs
++] = req
;
562 mrb
->is_write
= is_write
;
565 case VIRTIO_BLK_T_FLUSH
:
566 virtio_blk_handle_flush(req
, mrb
);
568 case VIRTIO_BLK_T_SCSI_CMD
:
569 virtio_blk_handle_scsi(req
);
571 case VIRTIO_BLK_T_GET_ID
:
573 VirtIOBlock
*s
= req
->dev
;
576 * NB: per existing s/n string convention the string is
577 * terminated by '\0' only when shorter than buffer.
579 const char *serial
= s
->conf
.serial
? s
->conf
.serial
: "";
580 size_t size
= MIN(strlen(serial
) + 1,
581 MIN(iov_size(in_iov
, in_num
),
582 VIRTIO_BLK_ID_BYTES
));
583 iov_from_buf(in_iov
, in_num
, 0, serial
, size
);
584 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
585 virtio_blk_free_request(req
);
589 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
590 virtio_blk_free_request(req
);
595 bool virtio_blk_handle_vq(VirtIOBlock
*s
, VirtQueue
*vq
)
598 MultiReqBuffer mrb
= {};
599 bool progress
= false;
601 aio_context_acquire(blk_get_aio_context(s
->blk
));
605 virtio_queue_set_notification(vq
, 0);
607 while ((req
= virtio_blk_get_request(s
, vq
))) {
609 if (virtio_blk_handle_request(req
, &mrb
)) {
610 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
611 virtio_blk_free_request(req
);
616 virtio_queue_set_notification(vq
, 1);
617 } while (!virtio_queue_empty(vq
));
620 virtio_blk_submit_multireq(s
->blk
, &mrb
);
623 blk_io_unplug(s
->blk
);
624 aio_context_release(blk_get_aio_context(s
->blk
));
628 static void virtio_blk_handle_output_do(VirtIOBlock
*s
, VirtQueue
*vq
)
630 virtio_blk_handle_vq(s
, vq
);
633 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
635 VirtIOBlock
*s
= (VirtIOBlock
*)vdev
;
638 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
639 * dataplane here instead of waiting for .set_status().
641 virtio_device_start_ioeventfd(vdev
);
642 if (!s
->dataplane_disabled
) {
646 virtio_blk_handle_output_do(s
, vq
);
649 static void virtio_blk_dma_restart_bh(void *opaque
)
651 VirtIOBlock
*s
= opaque
;
652 VirtIOBlockReq
*req
= s
->rq
;
653 MultiReqBuffer mrb
= {};
655 qemu_bh_delete(s
->bh
);
660 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
662 VirtIOBlockReq
*next
= req
->next
;
663 if (virtio_blk_handle_request(req
, &mrb
)) {
664 /* Device is now broken and won't do any processing until it gets
665 * reset. Already queued requests will be lost: let's purge them.
669 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
670 virtio_blk_free_request(req
);
679 virtio_blk_submit_multireq(s
->blk
, &mrb
);
681 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
684 static void virtio_blk_dma_restart_cb(void *opaque
, int running
,
687 VirtIOBlock
*s
= opaque
;
694 s
->bh
= aio_bh_new(blk_get_aio_context(s
->conf
.conf
.blk
),
695 virtio_blk_dma_restart_bh
, s
);
696 qemu_bh_schedule(s
->bh
);
700 static void virtio_blk_reset(VirtIODevice
*vdev
)
702 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
706 ctx
= blk_get_aio_context(s
->blk
);
707 aio_context_acquire(ctx
);
710 /* We drop queued requests after blk_drain() because blk_drain() itself can
715 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
716 virtio_blk_free_request(req
);
719 aio_context_release(ctx
);
721 assert(!s
->dataplane_started
);
722 blk_set_enable_write_cache(s
->blk
, s
->original_wce
);
725 /* coalesce internal state, copy to pci i/o region 0
727 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
729 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
730 BlockConf
*conf
= &s
->conf
.conf
;
731 struct virtio_blk_config blkcfg
;
734 int blk_size
= conf
->logical_block_size
;
736 blk_get_geometry(s
->blk
, &capacity
);
737 memset(&blkcfg
, 0, sizeof(blkcfg
));
738 virtio_stq_p(vdev
, &blkcfg
.capacity
, capacity
);
739 virtio_stl_p(vdev
, &blkcfg
.seg_max
, 128 - 2);
740 virtio_stw_p(vdev
, &blkcfg
.geometry
.cylinders
, conf
->cyls
);
741 virtio_stl_p(vdev
, &blkcfg
.blk_size
, blk_size
);
742 virtio_stw_p(vdev
, &blkcfg
.min_io_size
, conf
->min_io_size
/ blk_size
);
743 virtio_stw_p(vdev
, &blkcfg
.opt_io_size
, conf
->opt_io_size
/ blk_size
);
744 blkcfg
.geometry
.heads
= conf
->heads
;
746 * We must ensure that the block device capacity is a multiple of
747 * the logical block size. If that is not the case, let's use
748 * sector_mask to adopt the geometry to have a correct picture.
749 * For those devices where the capacity is ok for the given geometry
750 * we don't touch the sector value of the geometry, since some devices
751 * (like s390 dasd) need a specific value. Here the capacity is already
752 * cyls*heads*secs*blk_size and the sector value is not block size
753 * divided by 512 - instead it is the amount of blk_size blocks
754 * per track (cylinder).
756 length
= blk_getlength(s
->blk
);
757 if (length
> 0 && length
/ conf
->heads
/ conf
->secs
% blk_size
) {
758 blkcfg
.geometry
.sectors
= conf
->secs
& ~s
->sector_mask
;
760 blkcfg
.geometry
.sectors
= conf
->secs
;
763 blkcfg
.physical_block_exp
= get_physical_block_exp(conf
);
764 blkcfg
.alignment_offset
= 0;
765 blkcfg
.wce
= blk_enable_write_cache(s
->blk
);
766 virtio_stw_p(vdev
, &blkcfg
.num_queues
, s
->conf
.num_queues
);
767 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
770 static void virtio_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
772 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
773 struct virtio_blk_config blkcfg
;
775 memcpy(&blkcfg
, config
, sizeof(blkcfg
));
777 aio_context_acquire(blk_get_aio_context(s
->blk
));
778 blk_set_enable_write_cache(s
->blk
, blkcfg
.wce
!= 0);
779 aio_context_release(blk_get_aio_context(s
->blk
));
782 static uint64_t virtio_blk_get_features(VirtIODevice
*vdev
, uint64_t features
,
785 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
787 virtio_add_feature(&features
, VIRTIO_BLK_F_SEG_MAX
);
788 virtio_add_feature(&features
, VIRTIO_BLK_F_GEOMETRY
);
789 virtio_add_feature(&features
, VIRTIO_BLK_F_TOPOLOGY
);
790 virtio_add_feature(&features
, VIRTIO_BLK_F_BLK_SIZE
);
791 if (virtio_has_feature(features
, VIRTIO_F_VERSION_1
)) {
793 error_setg(errp
, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
797 virtio_clear_feature(&features
, VIRTIO_F_ANY_LAYOUT
);
798 virtio_add_feature(&features
, VIRTIO_BLK_F_SCSI
);
801 if (s
->conf
.config_wce
) {
802 virtio_add_feature(&features
, VIRTIO_BLK_F_CONFIG_WCE
);
804 if (blk_enable_write_cache(s
->blk
)) {
805 virtio_add_feature(&features
, VIRTIO_BLK_F_WCE
);
807 if (blk_is_read_only(s
->blk
)) {
808 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
810 if (s
->conf
.num_queues
> 1) {
811 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
817 static void virtio_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
819 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
821 if (!(status
& (VIRTIO_CONFIG_S_DRIVER
| VIRTIO_CONFIG_S_DRIVER_OK
))) {
822 assert(!s
->dataplane_started
);
825 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
829 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
830 * cache flushes. Thus, the "auto writethrough" behavior is never
831 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
832 * Leaving it enabled would break the following sequence:
834 * Guest started with "-drive cache=writethrough"
835 * Guest sets status to 0
836 * Guest sets DRIVER bit in status field
837 * Guest reads host features (WCE=0, CONFIG_WCE=1)
838 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
839 * Guest writes 1 to the WCE configuration field (writeback mode)
840 * Guest sets DRIVER_OK bit in status field
842 * s->blk would erroneously be placed in writethrough mode.
844 if (!virtio_vdev_has_feature(vdev
, VIRTIO_BLK_F_CONFIG_WCE
)) {
845 aio_context_acquire(blk_get_aio_context(s
->blk
));
846 blk_set_enable_write_cache(s
->blk
,
847 virtio_vdev_has_feature(vdev
,
849 aio_context_release(blk_get_aio_context(s
->blk
));
853 static void virtio_blk_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
855 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
856 VirtIOBlockReq
*req
= s
->rq
;
859 qemu_put_sbyte(f
, 1);
861 if (s
->conf
.num_queues
> 1) {
862 qemu_put_be32(f
, virtio_get_queue_index(req
->vq
));
865 qemu_put_virtqueue_element(f
, &req
->elem
);
868 qemu_put_sbyte(f
, 0);
871 static int virtio_blk_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
874 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
876 while (qemu_get_sbyte(f
)) {
877 unsigned nvqs
= s
->conf
.num_queues
;
882 vq_idx
= qemu_get_be32(f
);
884 if (vq_idx
>= nvqs
) {
885 error_report("Invalid virtqueue index in request list: %#x",
891 req
= qemu_get_virtqueue_element(vdev
, f
, sizeof(VirtIOBlockReq
));
892 virtio_blk_init_request(s
, virtio_get_queue(vdev
, vq_idx
), req
);
900 static void virtio_blk_resize(void *opaque
)
902 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
904 virtio_notify_config(vdev
);
907 static const BlockDevOps virtio_block_ops
= {
908 .resize_cb
= virtio_blk_resize
,
911 static void virtio_blk_device_realize(DeviceState
*dev
, Error
**errp
)
913 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
914 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
915 VirtIOBlkConf
*conf
= &s
->conf
;
919 if (!conf
->conf
.blk
) {
920 error_setg(errp
, "drive property not set");
923 if (!blk_is_inserted(conf
->conf
.blk
)) {
924 error_setg(errp
, "Device needs media, but drive is empty");
927 if (!conf
->num_queues
) {
928 error_setg(errp
, "num-queues property must be larger than 0");
931 if (!is_power_of_2(conf
->queue_size
) ||
932 conf
->queue_size
> VIRTQUEUE_MAX_SIZE
) {
933 error_setg(errp
, "invalid queue-size property (%" PRIu16
"), "
934 "must be a power of 2 (max %d)",
935 conf
->queue_size
, VIRTQUEUE_MAX_SIZE
);
939 blkconf_serial(&conf
->conf
, &conf
->serial
);
940 if (!blkconf_apply_backend_options(&conf
->conf
,
941 blk_is_read_only(conf
->conf
.blk
), true,
945 s
->original_wce
= blk_enable_write_cache(conf
->conf
.blk
);
946 if (!blkconf_geometry(&conf
->conf
, NULL
, 65535, 255, 255, errp
)) {
950 blkconf_blocksizes(&conf
->conf
);
952 if (conf
->conf
.logical_block_size
>
953 conf
->conf
.physical_block_size
) {
955 "logical_block_size > physical_block_size not supported");
959 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
960 sizeof(struct virtio_blk_config
));
962 s
->blk
= conf
->conf
.blk
;
964 s
->sector_mask
= (s
->conf
.conf
.logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
966 for (i
= 0; i
< conf
->num_queues
; i
++) {
967 virtio_add_queue(vdev
, conf
->queue_size
, virtio_blk_handle_output
);
969 virtio_blk_data_plane_create(vdev
, conf
, &s
->dataplane
, &err
);
971 error_propagate(errp
, err
);
972 virtio_cleanup(vdev
);
976 s
->change
= qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
977 blk_set_dev_ops(s
->blk
, &virtio_block_ops
, s
);
978 blk_set_guest_block_size(s
->blk
, s
->conf
.conf
.logical_block_size
);
980 blk_iostatus_enable(s
->blk
);
983 static void virtio_blk_device_unrealize(DeviceState
*dev
, Error
**errp
)
985 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
986 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
988 virtio_blk_data_plane_destroy(s
->dataplane
);
990 qemu_del_vm_change_state_handler(s
->change
);
991 blockdev_mark_auto_del(s
->blk
);
992 virtio_cleanup(vdev
);
995 static void virtio_blk_instance_init(Object
*obj
)
997 VirtIOBlock
*s
= VIRTIO_BLK(obj
);
999 device_add_bootindex_property(obj
, &s
->conf
.conf
.bootindex
,
1000 "bootindex", "/disk@0,0",
1004 static const VMStateDescription vmstate_virtio_blk
= {
1005 .name
= "virtio-blk",
1006 .minimum_version_id
= 2,
1008 .fields
= (VMStateField
[]) {
1009 VMSTATE_VIRTIO_DEVICE
,
1010 VMSTATE_END_OF_LIST()
1014 static Property virtio_blk_properties
[] = {
1015 DEFINE_BLOCK_PROPERTIES(VirtIOBlock
, conf
.conf
),
1016 DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock
, conf
.conf
),
1017 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock
, conf
.conf
),
1018 DEFINE_PROP_STRING("serial", VirtIOBlock
, conf
.serial
),
1019 DEFINE_PROP_BIT("config-wce", VirtIOBlock
, conf
.config_wce
, 0, true),
1021 DEFINE_PROP_BIT("scsi", VirtIOBlock
, conf
.scsi
, 0, false),
1023 DEFINE_PROP_BIT("request-merging", VirtIOBlock
, conf
.request_merging
, 0,
1025 DEFINE_PROP_UINT16("num-queues", VirtIOBlock
, conf
.num_queues
, 1),
1026 DEFINE_PROP_UINT16("queue-size", VirtIOBlock
, conf
.queue_size
, 128),
1027 DEFINE_PROP_LINK("iothread", VirtIOBlock
, conf
.iothread
, TYPE_IOTHREAD
,
1029 DEFINE_PROP_END_OF_LIST(),
1032 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
1034 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1035 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1037 dc
->props
= virtio_blk_properties
;
1038 dc
->vmsd
= &vmstate_virtio_blk
;
1039 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1040 vdc
->realize
= virtio_blk_device_realize
;
1041 vdc
->unrealize
= virtio_blk_device_unrealize
;
1042 vdc
->get_config
= virtio_blk_update_config
;
1043 vdc
->set_config
= virtio_blk_set_config
;
1044 vdc
->get_features
= virtio_blk_get_features
;
1045 vdc
->set_status
= virtio_blk_set_status
;
1046 vdc
->reset
= virtio_blk_reset
;
1047 vdc
->save
= virtio_blk_save_device
;
1048 vdc
->load
= virtio_blk_load_device
;
1049 vdc
->start_ioeventfd
= virtio_blk_data_plane_start
;
1050 vdc
->stop_ioeventfd
= virtio_blk_data_plane_stop
;
1053 static const TypeInfo virtio_blk_info
= {
1054 .name
= TYPE_VIRTIO_BLK
,
1055 .parent
= TYPE_VIRTIO_DEVICE
,
1056 .instance_size
= sizeof(VirtIOBlock
),
1057 .instance_init
= virtio_blk_instance_init
,
1058 .class_init
= virtio_blk_class_init
,
1061 static void virtio_register_types(void)
1063 type_register_static(&virtio_blk_info
);
1066 type_init(virtio_register_types
)