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"
16 #include "qemu/error-report.h"
18 #include "hw/block/block.h"
19 #include "sysemu/block-backend.h"
20 #include "sysemu/blockdev.h"
21 #include "hw/virtio/virtio-blk.h"
22 #include "dataplane/virtio-blk.h"
23 #include "migration/migration.h"
24 #include "block/scsi.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/virtio/virtio-access.h"
31 VirtIOBlockReq
*virtio_blk_alloc_request(VirtIOBlock
*s
)
33 VirtIOBlockReq
*req
= g_new(VirtIOBlockReq
, 1);
42 void virtio_blk_free_request(VirtIOBlockReq
*req
)
49 static void virtio_blk_complete_request(VirtIOBlockReq
*req
,
52 VirtIOBlock
*s
= req
->dev
;
53 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
55 trace_virtio_blk_req_complete(req
, status
);
57 stb_p(&req
->in
->status
, status
);
58 virtqueue_push(s
->vq
, &req
->elem
, req
->in_len
);
59 virtio_notify(vdev
, s
->vq
);
62 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, unsigned char status
)
64 req
->dev
->complete_request(req
, status
);
67 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
70 BlockErrorAction action
= blk_get_error_action(req
->dev
->blk
,
72 VirtIOBlock
*s
= req
->dev
;
74 if (action
== BLOCK_ERROR_ACTION_STOP
) {
75 /* Break the link as the next request is going to be parsed from the
76 * ring again. Otherwise we may end up doing a double completion! */
80 } else if (action
== BLOCK_ERROR_ACTION_REPORT
) {
81 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
82 block_acct_failed(blk_get_stats(s
->blk
), &req
->acct
);
83 virtio_blk_free_request(req
);
86 blk_error_action(s
->blk
, action
, is_read
, error
);
87 return action
!= BLOCK_ERROR_ACTION_IGNORE
;
90 static void virtio_blk_rw_complete(void *opaque
, int ret
)
92 VirtIOBlockReq
*next
= opaque
;
95 VirtIOBlockReq
*req
= next
;
97 trace_virtio_blk_rw_complete(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
);
128 static void virtio_blk_flush_complete(void *opaque
, int ret
)
130 VirtIOBlockReq
*req
= opaque
;
133 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
138 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
139 block_acct_done(blk_get_stats(req
->dev
->blk
), &req
->acct
);
140 virtio_blk_free_request(req
);
147 struct sg_io_hdr hdr
;
148 } VirtIOBlockIoctlReq
;
150 static void virtio_blk_ioctl_complete(void *opaque
, int status
)
152 VirtIOBlockIoctlReq
*ioctl_req
= opaque
;
153 VirtIOBlockReq
*req
= ioctl_req
->req
;
154 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
155 struct virtio_scsi_inhdr
*scsi
;
156 struct sg_io_hdr
*hdr
;
158 scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
161 status
= VIRTIO_BLK_S_UNSUPP
;
162 virtio_stl_p(vdev
, &scsi
->errors
, 255);
166 hdr
= &ioctl_req
->hdr
;
168 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
169 * clear the masked_status field [hence status gets cleared too, see
170 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
171 * status has occurred. However they do set DRIVER_SENSE in driver_status
172 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
174 if (hdr
->status
== 0 && hdr
->sb_len_wr
> 0) {
175 hdr
->status
= CHECK_CONDITION
;
178 virtio_stl_p(vdev
, &scsi
->errors
,
179 hdr
->status
| (hdr
->msg_status
<< 8) |
180 (hdr
->host_status
<< 16) | (hdr
->driver_status
<< 24));
181 virtio_stl_p(vdev
, &scsi
->residual
, hdr
->resid
);
182 virtio_stl_p(vdev
, &scsi
->sense_len
, hdr
->sb_len_wr
);
183 virtio_stl_p(vdev
, &scsi
->data_len
, hdr
->dxfer_len
);
186 virtio_blk_req_complete(req
, status
);
187 virtio_blk_free_request(req
);
193 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
195 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
197 if (!virtqueue_pop(s
->vq
, &req
->elem
)) {
198 virtio_blk_free_request(req
);
205 static int virtio_blk_handle_scsi_req(VirtIOBlockReq
*req
)
207 int status
= VIRTIO_BLK_S_OK
;
208 struct virtio_scsi_inhdr
*scsi
= NULL
;
209 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
210 VirtQueueElement
*elem
= &req
->elem
;
211 VirtIOBlock
*blk
= req
->dev
;
215 VirtIOBlockIoctlReq
*ioctl_req
;
220 * We require at least one output segment each for the virtio_blk_outhdr
221 * and the SCSI command block.
223 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
224 * and the sense buffer pointer in the input segments.
226 if (elem
->out_num
< 2 || elem
->in_num
< 3) {
227 status
= VIRTIO_BLK_S_IOERR
;
232 * The scsi inhdr is placed in the second-to-last input segment, just
233 * before the regular inhdr.
235 scsi
= (void *)elem
->in_sg
[elem
->in_num
- 2].iov_base
;
237 if (!blk
->conf
.scsi
) {
238 status
= VIRTIO_BLK_S_UNSUPP
;
243 * No support for bidirection commands yet.
245 if (elem
->out_num
> 2 && elem
->in_num
> 3) {
246 status
= VIRTIO_BLK_S_UNSUPP
;
251 ioctl_req
= g_new0(VirtIOBlockIoctlReq
, 1);
252 ioctl_req
->req
= req
;
253 ioctl_req
->hdr
.interface_id
= 'S';
254 ioctl_req
->hdr
.cmd_len
= elem
->out_sg
[1].iov_len
;
255 ioctl_req
->hdr
.cmdp
= elem
->out_sg
[1].iov_base
;
256 ioctl_req
->hdr
.dxfer_len
= 0;
258 if (elem
->out_num
> 2) {
260 * If there are more than the minimally required 2 output segments
261 * there is write payload starting from the third iovec.
263 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
264 ioctl_req
->hdr
.iovec_count
= elem
->out_num
- 2;
266 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
267 ioctl_req
->hdr
.dxfer_len
+= elem
->out_sg
[i
+ 2].iov_len
;
270 ioctl_req
->hdr
.dxferp
= elem
->out_sg
+ 2;
272 } else if (elem
->in_num
> 3) {
274 * If we have more than 3 input segments the guest wants to actually
277 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
278 ioctl_req
->hdr
.iovec_count
= elem
->in_num
- 3;
279 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
280 ioctl_req
->hdr
.dxfer_len
+= elem
->in_sg
[i
].iov_len
;
283 ioctl_req
->hdr
.dxferp
= elem
->in_sg
;
286 * Some SCSI commands don't actually transfer any data.
288 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_NONE
;
291 ioctl_req
->hdr
.sbp
= elem
->in_sg
[elem
->in_num
- 3].iov_base
;
292 ioctl_req
->hdr
.mx_sb_len
= elem
->in_sg
[elem
->in_num
- 3].iov_len
;
294 acb
= blk_aio_ioctl(blk
->blk
, SG_IO
, &ioctl_req
->hdr
,
295 virtio_blk_ioctl_complete
, ioctl_req
);
298 status
= VIRTIO_BLK_S_UNSUPP
;
307 /* Just put anything nonzero so that the ioctl fails in the guest. */
309 virtio_stl_p(vdev
, &scsi
->errors
, 255);
314 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
318 status
= virtio_blk_handle_scsi_req(req
);
319 if (status
!= -EINPROGRESS
) {
320 virtio_blk_req_complete(req
, status
);
321 virtio_blk_free_request(req
);
325 static inline void submit_requests(BlockBackend
*blk
, MultiReqBuffer
*mrb
,
326 int start
, int num_reqs
, int niov
)
328 QEMUIOVector
*qiov
= &mrb
->reqs
[start
]->qiov
;
329 int64_t sector_num
= mrb
->reqs
[start
]->sector_num
;
330 int nb_sectors
= mrb
->reqs
[start
]->qiov
.size
/ BDRV_SECTOR_SIZE
;
331 bool is_write
= mrb
->is_write
;
335 struct iovec
*tmp_iov
= qiov
->iov
;
336 int tmp_niov
= qiov
->niov
;
338 /* mrb->reqs[start]->qiov was initialized from external so we can't
339 * modifiy it here. We need to initialize it locally and then add the
340 * external iovecs. */
341 qemu_iovec_init(qiov
, niov
);
343 for (i
= 0; i
< tmp_niov
; i
++) {
344 qemu_iovec_add(qiov
, tmp_iov
[i
].iov_base
, tmp_iov
[i
].iov_len
);
347 for (i
= start
+ 1; i
< start
+ num_reqs
; i
++) {
348 qemu_iovec_concat(qiov
, &mrb
->reqs
[i
]->qiov
, 0,
349 mrb
->reqs
[i
]->qiov
.size
);
350 mrb
->reqs
[i
- 1]->mr_next
= mrb
->reqs
[i
];
351 nb_sectors
+= mrb
->reqs
[i
]->qiov
.size
/ BDRV_SECTOR_SIZE
;
353 assert(nb_sectors
== qiov
->size
/ BDRV_SECTOR_SIZE
);
355 trace_virtio_blk_submit_multireq(mrb
, start
, num_reqs
, sector_num
,
356 nb_sectors
, is_write
);
357 block_acct_merge_done(blk_get_stats(blk
),
358 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
,
363 blk_aio_writev(blk
, sector_num
, qiov
, nb_sectors
,
364 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
366 blk_aio_readv(blk
, sector_num
, qiov
, nb_sectors
,
367 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
371 static int multireq_compare(const void *a
, const void *b
)
373 const VirtIOBlockReq
*req1
= *(VirtIOBlockReq
**)a
,
374 *req2
= *(VirtIOBlockReq
**)b
;
377 * Note that we can't simply subtract sector_num1 from sector_num2
378 * here as that could overflow the return value.
380 if (req1
->sector_num
> req2
->sector_num
) {
382 } else if (req1
->sector_num
< req2
->sector_num
) {
389 void virtio_blk_submit_multireq(BlockBackend
*blk
, MultiReqBuffer
*mrb
)
391 int i
= 0, start
= 0, num_reqs
= 0, niov
= 0, nb_sectors
= 0;
392 int max_xfer_len
= 0;
393 int64_t sector_num
= 0;
395 if (mrb
->num_reqs
== 1) {
396 submit_requests(blk
, mrb
, 0, 1, -1);
401 max_xfer_len
= blk_get_max_transfer_length(mrb
->reqs
[0]->dev
->blk
);
402 max_xfer_len
= MIN_NON_ZERO(max_xfer_len
, BDRV_REQUEST_MAX_SECTORS
);
404 qsort(mrb
->reqs
, mrb
->num_reqs
, sizeof(*mrb
->reqs
),
407 for (i
= 0; i
< mrb
->num_reqs
; i
++) {
408 VirtIOBlockReq
*req
= mrb
->reqs
[i
];
411 * NOTE: We cannot merge the requests in below situations:
412 * 1. requests are not sequential
413 * 2. merge would exceed maximum number of IOVs
414 * 3. merge would exceed maximum transfer length of backend device
416 if (sector_num
+ nb_sectors
!= req
->sector_num
||
417 niov
> blk_get_max_iov(blk
) - req
->qiov
.niov
||
418 req
->qiov
.size
/ BDRV_SECTOR_SIZE
> max_xfer_len
||
419 nb_sectors
> max_xfer_len
- req
->qiov
.size
/ BDRV_SECTOR_SIZE
) {
420 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
426 sector_num
= req
->sector_num
;
427 nb_sectors
= niov
= 0;
431 nb_sectors
+= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
432 niov
+= req
->qiov
.niov
;
436 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
440 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
442 block_acct_start(blk_get_stats(req
->dev
->blk
), &req
->acct
, 0,
446 * Make sure all outstanding writes are posted to the backing device.
448 if (mrb
->is_write
&& mrb
->num_reqs
> 0) {
449 virtio_blk_submit_multireq(req
->dev
->blk
, mrb
);
451 blk_aio_flush(req
->dev
->blk
, virtio_blk_flush_complete
, req
);
454 static bool virtio_blk_sect_range_ok(VirtIOBlock
*dev
,
455 uint64_t sector
, size_t size
)
457 uint64_t nb_sectors
= size
>> BDRV_SECTOR_BITS
;
458 uint64_t total_sectors
;
460 if (nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
463 if (sector
& dev
->sector_mask
) {
466 if (size
% dev
->conf
.conf
.logical_block_size
) {
469 blk_get_geometry(dev
->blk
, &total_sectors
);
470 if (sector
> total_sectors
|| nb_sectors
> total_sectors
- sector
) {
476 void virtio_blk_handle_request(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
479 struct iovec
*in_iov
= req
->elem
.in_sg
;
480 struct iovec
*iov
= req
->elem
.out_sg
;
481 unsigned in_num
= req
->elem
.in_num
;
482 unsigned out_num
= req
->elem
.out_num
;
484 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
485 error_report("virtio-blk missing headers");
489 if (unlikely(iov_to_buf(iov
, out_num
, 0, &req
->out
,
490 sizeof(req
->out
)) != sizeof(req
->out
))) {
491 error_report("virtio-blk request outhdr too short");
495 iov_discard_front(&iov
, &out_num
, sizeof(req
->out
));
497 if (in_iov
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
498 error_report("virtio-blk request inhdr too short");
502 /* We always touch the last byte, so just see how big in_iov is. */
503 req
->in_len
= iov_size(in_iov
, in_num
);
504 req
->in
= (void *)in_iov
[in_num
- 1].iov_base
505 + in_iov
[in_num
- 1].iov_len
506 - sizeof(struct virtio_blk_inhdr
);
507 iov_discard_back(in_iov
, &in_num
, sizeof(struct virtio_blk_inhdr
));
509 type
= virtio_ldl_p(VIRTIO_DEVICE(req
->dev
), &req
->out
.type
);
511 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
512 * is an optional flag. Although a guest should not send this flag if
513 * not negotiated we ignored it in the past. So keep ignoring it. */
514 switch (type
& ~(VIRTIO_BLK_T_OUT
| VIRTIO_BLK_T_BARRIER
)) {
515 case VIRTIO_BLK_T_IN
:
517 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
518 req
->sector_num
= virtio_ldq_p(VIRTIO_DEVICE(req
->dev
),
522 qemu_iovec_init_external(&req
->qiov
, iov
, out_num
);
523 trace_virtio_blk_handle_write(req
, req
->sector_num
,
524 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
526 qemu_iovec_init_external(&req
->qiov
, in_iov
, in_num
);
527 trace_virtio_blk_handle_read(req
, req
->sector_num
,
528 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
531 if (!virtio_blk_sect_range_ok(req
->dev
, req
->sector_num
,
533 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
534 block_acct_invalid(blk_get_stats(req
->dev
->blk
),
535 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
536 virtio_blk_free_request(req
);
540 block_acct_start(blk_get_stats(req
->dev
->blk
),
541 &req
->acct
, req
->qiov
.size
,
542 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
544 /* merge would exceed maximum number of requests or IO direction
546 if (mrb
->num_reqs
> 0 && (mrb
->num_reqs
== VIRTIO_BLK_MAX_MERGE_REQS
||
547 is_write
!= mrb
->is_write
||
548 !req
->dev
->conf
.request_merging
)) {
549 virtio_blk_submit_multireq(req
->dev
->blk
, mrb
);
552 assert(mrb
->num_reqs
< VIRTIO_BLK_MAX_MERGE_REQS
);
553 mrb
->reqs
[mrb
->num_reqs
++] = req
;
554 mrb
->is_write
= is_write
;
557 case VIRTIO_BLK_T_FLUSH
:
558 virtio_blk_handle_flush(req
, mrb
);
560 case VIRTIO_BLK_T_SCSI_CMD
:
561 virtio_blk_handle_scsi(req
);
563 case VIRTIO_BLK_T_GET_ID
:
565 VirtIOBlock
*s
= req
->dev
;
568 * NB: per existing s/n string convention the string is
569 * terminated by '\0' only when shorter than buffer.
571 const char *serial
= s
->conf
.serial
? s
->conf
.serial
: "";
572 size_t size
= MIN(strlen(serial
) + 1,
573 MIN(iov_size(in_iov
, in_num
),
574 VIRTIO_BLK_ID_BYTES
));
575 iov_from_buf(in_iov
, in_num
, 0, serial
, size
);
576 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
577 virtio_blk_free_request(req
);
581 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
582 virtio_blk_free_request(req
);
586 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
588 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
590 MultiReqBuffer mrb
= {};
592 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
593 * dataplane here instead of waiting for .set_status().
596 virtio_blk_data_plane_start(s
->dataplane
);
602 while ((req
= virtio_blk_get_request(s
))) {
603 virtio_blk_handle_request(req
, &mrb
);
607 virtio_blk_submit_multireq(s
->blk
, &mrb
);
610 blk_io_unplug(s
->blk
);
613 static void virtio_blk_dma_restart_bh(void *opaque
)
615 VirtIOBlock
*s
= opaque
;
616 VirtIOBlockReq
*req
= s
->rq
;
617 MultiReqBuffer mrb
= {};
619 qemu_bh_delete(s
->bh
);
625 VirtIOBlockReq
*next
= req
->next
;
626 virtio_blk_handle_request(req
, &mrb
);
631 virtio_blk_submit_multireq(s
->blk
, &mrb
);
635 static void virtio_blk_dma_restart_cb(void *opaque
, int running
,
638 VirtIOBlock
*s
= opaque
;
645 s
->bh
= aio_bh_new(blk_get_aio_context(s
->conf
.conf
.blk
),
646 virtio_blk_dma_restart_bh
, s
);
647 qemu_bh_schedule(s
->bh
);
651 static void virtio_blk_reset(VirtIODevice
*vdev
)
653 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
657 * This should cancel pending requests, but can't do nicely until there
658 * are per-device request lists.
660 ctx
= blk_get_aio_context(s
->blk
);
661 aio_context_acquire(ctx
);
665 virtio_blk_data_plane_stop(s
->dataplane
);
667 aio_context_release(ctx
);
669 blk_set_enable_write_cache(s
->blk
, s
->original_wce
);
672 /* coalesce internal state, copy to pci i/o region 0
674 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
676 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
677 BlockConf
*conf
= &s
->conf
.conf
;
678 struct virtio_blk_config blkcfg
;
680 int blk_size
= conf
->logical_block_size
;
682 blk_get_geometry(s
->blk
, &capacity
);
683 memset(&blkcfg
, 0, sizeof(blkcfg
));
684 virtio_stq_p(vdev
, &blkcfg
.capacity
, capacity
);
685 virtio_stl_p(vdev
, &blkcfg
.seg_max
, 128 - 2);
686 virtio_stw_p(vdev
, &blkcfg
.geometry
.cylinders
, conf
->cyls
);
687 virtio_stl_p(vdev
, &blkcfg
.blk_size
, blk_size
);
688 virtio_stw_p(vdev
, &blkcfg
.min_io_size
, conf
->min_io_size
/ blk_size
);
689 virtio_stw_p(vdev
, &blkcfg
.opt_io_size
, conf
->opt_io_size
/ blk_size
);
690 blkcfg
.geometry
.heads
= conf
->heads
;
692 * We must ensure that the block device capacity is a multiple of
693 * the logical block size. If that is not the case, let's use
694 * sector_mask to adopt the geometry to have a correct picture.
695 * For those devices where the capacity is ok for the given geometry
696 * we don't touch the sector value of the geometry, since some devices
697 * (like s390 dasd) need a specific value. Here the capacity is already
698 * cyls*heads*secs*blk_size and the sector value is not block size
699 * divided by 512 - instead it is the amount of blk_size blocks
700 * per track (cylinder).
702 if (blk_getlength(s
->blk
) / conf
->heads
/ conf
->secs
% blk_size
) {
703 blkcfg
.geometry
.sectors
= conf
->secs
& ~s
->sector_mask
;
705 blkcfg
.geometry
.sectors
= conf
->secs
;
708 blkcfg
.physical_block_exp
= get_physical_block_exp(conf
);
709 blkcfg
.alignment_offset
= 0;
710 blkcfg
.wce
= blk_enable_write_cache(s
->blk
);
711 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
714 static void virtio_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
716 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
717 struct virtio_blk_config blkcfg
;
719 memcpy(&blkcfg
, config
, sizeof(blkcfg
));
721 aio_context_acquire(blk_get_aio_context(s
->blk
));
722 blk_set_enable_write_cache(s
->blk
, blkcfg
.wce
!= 0);
723 aio_context_release(blk_get_aio_context(s
->blk
));
726 static uint64_t virtio_blk_get_features(VirtIODevice
*vdev
, uint64_t features
,
729 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
731 virtio_add_feature(&features
, VIRTIO_BLK_F_SEG_MAX
);
732 virtio_add_feature(&features
, VIRTIO_BLK_F_GEOMETRY
);
733 virtio_add_feature(&features
, VIRTIO_BLK_F_TOPOLOGY
);
734 virtio_add_feature(&features
, VIRTIO_BLK_F_BLK_SIZE
);
735 if (virtio_has_feature(features
, VIRTIO_F_VERSION_1
)) {
737 error_setg(errp
, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
741 virtio_clear_feature(&features
, VIRTIO_F_ANY_LAYOUT
);
742 virtio_add_feature(&features
, VIRTIO_BLK_F_SCSI
);
745 if (s
->conf
.config_wce
) {
746 virtio_add_feature(&features
, VIRTIO_BLK_F_CONFIG_WCE
);
748 if (blk_enable_write_cache(s
->blk
)) {
749 virtio_add_feature(&features
, VIRTIO_BLK_F_WCE
);
751 if (blk_is_read_only(s
->blk
)) {
752 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
758 static void virtio_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
760 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
762 if (s
->dataplane
&& !(status
& (VIRTIO_CONFIG_S_DRIVER
|
763 VIRTIO_CONFIG_S_DRIVER_OK
))) {
764 virtio_blk_data_plane_stop(s
->dataplane
);
767 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
771 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
772 * cache flushes. Thus, the "auto writethrough" behavior is never
773 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
774 * Leaving it enabled would break the following sequence:
776 * Guest started with "-drive cache=writethrough"
777 * Guest sets status to 0
778 * Guest sets DRIVER bit in status field
779 * Guest reads host features (WCE=0, CONFIG_WCE=1)
780 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
781 * Guest writes 1 to the WCE configuration field (writeback mode)
782 * Guest sets DRIVER_OK bit in status field
784 * s->blk would erroneously be placed in writethrough mode.
786 if (!virtio_vdev_has_feature(vdev
, VIRTIO_BLK_F_CONFIG_WCE
)) {
787 aio_context_acquire(blk_get_aio_context(s
->blk
));
788 blk_set_enable_write_cache(s
->blk
,
789 virtio_vdev_has_feature(vdev
,
791 aio_context_release(blk_get_aio_context(s
->blk
));
795 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
797 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
798 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
801 virtio_blk_data_plane_stop(s
->dataplane
);
804 virtio_save(vdev
, f
);
807 static void virtio_blk_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
809 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
810 VirtIOBlockReq
*req
= s
->rq
;
813 qemu_put_sbyte(f
, 1);
814 qemu_put_buffer(f
, (unsigned char *)&req
->elem
,
815 sizeof(VirtQueueElement
));
818 qemu_put_sbyte(f
, 0);
821 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
823 VirtIOBlock
*s
= opaque
;
824 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
829 return virtio_load(vdev
, f
, version_id
);
832 static int virtio_blk_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
835 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
837 while (qemu_get_sbyte(f
)) {
838 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
839 qemu_get_buffer(f
, (unsigned char *)&req
->elem
,
840 sizeof(VirtQueueElement
));
844 virtqueue_map(&req
->elem
);
850 static void virtio_blk_resize(void *opaque
)
852 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
854 virtio_notify_config(vdev
);
857 static const BlockDevOps virtio_block_ops
= {
858 .resize_cb
= virtio_blk_resize
,
861 /* Disable dataplane thread during live migration since it does not
862 * update the dirty memory bitmap yet.
864 static void virtio_blk_migration_state_changed(Notifier
*notifier
, void *data
)
866 VirtIOBlock
*s
= container_of(notifier
, VirtIOBlock
,
867 migration_state_notifier
);
868 MigrationState
*mig
= data
;
871 if (migration_in_setup(mig
)) {
875 virtio_blk_data_plane_destroy(s
->dataplane
);
877 } else if (migration_has_finished(mig
) ||
878 migration_has_failed(mig
)) {
882 blk_drain_all(); /* complete in-flight non-dataplane requests */
883 virtio_blk_data_plane_create(VIRTIO_DEVICE(s
), &s
->conf
,
884 &s
->dataplane
, &err
);
886 error_report_err(err
);
891 static void virtio_blk_device_realize(DeviceState
*dev
, Error
**errp
)
893 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
894 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
895 VirtIOBlkConf
*conf
= &s
->conf
;
897 static int virtio_blk_id
;
899 if (!conf
->conf
.blk
) {
900 error_setg(errp
, "drive property not set");
903 if (!blk_is_inserted(conf
->conf
.blk
)) {
904 error_setg(errp
, "Device needs media, but drive is empty");
908 blkconf_serial(&conf
->conf
, &conf
->serial
);
909 s
->original_wce
= blk_enable_write_cache(conf
->conf
.blk
);
910 blkconf_geometry(&conf
->conf
, NULL
, 65535, 255, 255, &err
);
912 error_propagate(errp
, err
);
915 blkconf_blocksizes(&conf
->conf
);
917 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
918 sizeof(struct virtio_blk_config
));
920 s
->blk
= conf
->conf
.blk
;
922 s
->sector_mask
= (s
->conf
.conf
.logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
924 s
->vq
= virtio_add_queue(vdev
, 128, virtio_blk_handle_output
);
925 s
->complete_request
= virtio_blk_complete_request
;
926 virtio_blk_data_plane_create(vdev
, conf
, &s
->dataplane
, &err
);
928 error_propagate(errp
, err
);
929 virtio_cleanup(vdev
);
932 s
->migration_state_notifier
.notify
= virtio_blk_migration_state_changed
;
933 add_migration_state_change_notifier(&s
->migration_state_notifier
);
935 s
->change
= qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
936 register_savevm(dev
, "virtio-blk", virtio_blk_id
++, 2,
937 virtio_blk_save
, virtio_blk_load
, s
);
938 blk_set_dev_ops(s
->blk
, &virtio_block_ops
, s
);
939 blk_set_guest_block_size(s
->blk
, s
->conf
.conf
.logical_block_size
);
941 blk_iostatus_enable(s
->blk
);
944 static void virtio_blk_device_unrealize(DeviceState
*dev
, Error
**errp
)
946 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
947 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
949 remove_migration_state_change_notifier(&s
->migration_state_notifier
);
950 virtio_blk_data_plane_destroy(s
->dataplane
);
952 qemu_del_vm_change_state_handler(s
->change
);
953 unregister_savevm(dev
, "virtio-blk", s
);
954 blockdev_mark_auto_del(s
->blk
);
955 virtio_cleanup(vdev
);
958 static void virtio_blk_instance_init(Object
*obj
)
960 VirtIOBlock
*s
= VIRTIO_BLK(obj
);
962 object_property_add_link(obj
, "iothread", TYPE_IOTHREAD
,
963 (Object
**)&s
->conf
.iothread
,
964 qdev_prop_allow_set_link_before_realize
,
965 OBJ_PROP_LINK_UNREF_ON_RELEASE
, NULL
);
966 device_add_bootindex_property(obj
, &s
->conf
.conf
.bootindex
,
967 "bootindex", "/disk@0,0",
971 static Property virtio_blk_properties
[] = {
972 DEFINE_BLOCK_PROPERTIES(VirtIOBlock
, conf
.conf
),
973 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock
, conf
.conf
),
974 DEFINE_PROP_STRING("serial", VirtIOBlock
, conf
.serial
),
975 DEFINE_PROP_BIT("config-wce", VirtIOBlock
, conf
.config_wce
, 0, true),
977 DEFINE_PROP_BIT("scsi", VirtIOBlock
, conf
.scsi
, 0, false),
979 DEFINE_PROP_BIT("request-merging", VirtIOBlock
, conf
.request_merging
, 0,
981 DEFINE_PROP_END_OF_LIST(),
984 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
986 DeviceClass
*dc
= DEVICE_CLASS(klass
);
987 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
989 dc
->props
= virtio_blk_properties
;
990 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
991 vdc
->realize
= virtio_blk_device_realize
;
992 vdc
->unrealize
= virtio_blk_device_unrealize
;
993 vdc
->get_config
= virtio_blk_update_config
;
994 vdc
->set_config
= virtio_blk_set_config
;
995 vdc
->get_features
= virtio_blk_get_features
;
996 vdc
->set_status
= virtio_blk_set_status
;
997 vdc
->reset
= virtio_blk_reset
;
998 vdc
->save
= virtio_blk_save_device
;
999 vdc
->load
= virtio_blk_load_device
;
1002 static const TypeInfo virtio_device_info
= {
1003 .name
= TYPE_VIRTIO_BLK
,
1004 .parent
= TYPE_VIRTIO_DEVICE
,
1005 .instance_size
= sizeof(VirtIOBlock
),
1006 .instance_init
= virtio_blk_instance_init
,
1007 .class_init
= virtio_blk_class_init
,
1010 static void virtio_register_types(void)
1012 type_register_static(&virtio_device_info
);
1015 type_init(virtio_register_types
)