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_slice_new(VirtIOBlockReq
);
41 void virtio_blk_free_request(VirtIOBlockReq
*req
)
44 g_slice_free(VirtIOBlockReq
, req
);
48 static void virtio_blk_complete_request(VirtIOBlockReq
*req
,
51 VirtIOBlock
*s
= req
->dev
;
52 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
54 trace_virtio_blk_req_complete(req
, status
);
56 stb_p(&req
->in
->status
, status
);
57 virtqueue_push(s
->vq
, &req
->elem
, req
->qiov
.size
+ sizeof(*req
->in
));
58 virtio_notify(vdev
, s
->vq
);
61 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, unsigned char status
)
63 req
->dev
->complete_request(req
, status
);
66 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
69 BlockErrorAction action
= blk_get_error_action(req
->dev
->blk
,
71 VirtIOBlock
*s
= req
->dev
;
73 if (action
== BLOCK_ERROR_ACTION_STOP
) {
76 } else if (action
== BLOCK_ERROR_ACTION_REPORT
) {
77 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
78 block_acct_done(blk_get_stats(s
->blk
), &req
->acct
);
79 virtio_blk_free_request(req
);
82 blk_error_action(s
->blk
, action
, is_read
, error
);
83 return action
!= BLOCK_ERROR_ACTION_IGNORE
;
86 static void virtio_blk_rw_complete(void *opaque
, int ret
)
88 VirtIOBlockReq
*next
= opaque
;
91 VirtIOBlockReq
*req
= next
;
93 trace_virtio_blk_rw_complete(req
, ret
);
95 if (req
->qiov
.nalloc
!= -1) {
96 /* If nalloc is != 1 req->qiov is a local copy of the original
97 * external iovec. It was allocated in submit_merged_requests
98 * to be able to merge requests. */
99 qemu_iovec_destroy(&req
->qiov
);
103 int p
= virtio_ldl_p(VIRTIO_DEVICE(req
->dev
), &req
->out
.type
);
104 bool is_read
= !(p
& VIRTIO_BLK_T_OUT
);
105 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
)) {
110 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
111 block_acct_done(blk_get_stats(req
->dev
->blk
), &req
->acct
);
112 virtio_blk_free_request(req
);
116 static void virtio_blk_flush_complete(void *opaque
, int ret
)
118 VirtIOBlockReq
*req
= opaque
;
121 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
126 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
127 block_acct_done(blk_get_stats(req
->dev
->blk
), &req
->acct
);
128 virtio_blk_free_request(req
);
135 struct sg_io_hdr hdr
;
136 } VirtIOBlockIoctlReq
;
138 static void virtio_blk_ioctl_complete(void *opaque
, int status
)
140 VirtIOBlockIoctlReq
*ioctl_req
= opaque
;
141 VirtIOBlockReq
*req
= ioctl_req
->req
;
142 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
143 struct virtio_scsi_inhdr
*scsi
;
144 struct sg_io_hdr
*hdr
;
146 scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
149 status
= VIRTIO_BLK_S_UNSUPP
;
150 virtio_stl_p(vdev
, &scsi
->errors
, 255);
154 hdr
= &ioctl_req
->hdr
;
156 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
157 * clear the masked_status field [hence status gets cleared too, see
158 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
159 * status has occurred. However they do set DRIVER_SENSE in driver_status
160 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
162 if (hdr
->status
== 0 && hdr
->sb_len_wr
> 0) {
163 hdr
->status
= CHECK_CONDITION
;
166 virtio_stl_p(vdev
, &scsi
->errors
,
167 hdr
->status
| (hdr
->msg_status
<< 8) |
168 (hdr
->host_status
<< 16) | (hdr
->driver_status
<< 24));
169 virtio_stl_p(vdev
, &scsi
->residual
, hdr
->resid
);
170 virtio_stl_p(vdev
, &scsi
->sense_len
, hdr
->sb_len_wr
);
171 virtio_stl_p(vdev
, &scsi
->data_len
, hdr
->dxfer_len
);
174 virtio_blk_req_complete(req
, status
);
175 virtio_blk_free_request(req
);
181 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
183 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
185 if (!virtqueue_pop(s
->vq
, &req
->elem
)) {
186 virtio_blk_free_request(req
);
193 static int virtio_blk_handle_scsi_req(VirtIOBlockReq
*req
)
195 int status
= VIRTIO_BLK_S_OK
;
196 struct virtio_scsi_inhdr
*scsi
= NULL
;
197 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
198 VirtQueueElement
*elem
= &req
->elem
;
199 VirtIOBlock
*blk
= req
->dev
;
203 VirtIOBlockIoctlReq
*ioctl_req
;
207 * We require at least one output segment each for the virtio_blk_outhdr
208 * and the SCSI command block.
210 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
211 * and the sense buffer pointer in the input segments.
213 if (elem
->out_num
< 2 || elem
->in_num
< 3) {
214 status
= VIRTIO_BLK_S_IOERR
;
219 * The scsi inhdr is placed in the second-to-last input segment, just
220 * before the regular inhdr.
222 scsi
= (void *)elem
->in_sg
[elem
->in_num
- 2].iov_base
;
224 if (!blk
->conf
.scsi
) {
225 status
= VIRTIO_BLK_S_UNSUPP
;
230 * No support for bidirection commands yet.
232 if (elem
->out_num
> 2 && elem
->in_num
> 3) {
233 status
= VIRTIO_BLK_S_UNSUPP
;
238 ioctl_req
= g_new0(VirtIOBlockIoctlReq
, 1);
239 ioctl_req
->req
= req
;
240 ioctl_req
->hdr
.interface_id
= 'S';
241 ioctl_req
->hdr
.cmd_len
= elem
->out_sg
[1].iov_len
;
242 ioctl_req
->hdr
.cmdp
= elem
->out_sg
[1].iov_base
;
243 ioctl_req
->hdr
.dxfer_len
= 0;
245 if (elem
->out_num
> 2) {
247 * If there are more than the minimally required 2 output segments
248 * there is write payload starting from the third iovec.
250 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
251 ioctl_req
->hdr
.iovec_count
= elem
->out_num
- 2;
253 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
254 ioctl_req
->hdr
.dxfer_len
+= elem
->out_sg
[i
+ 2].iov_len
;
257 ioctl_req
->hdr
.dxferp
= elem
->out_sg
+ 2;
259 } else if (elem
->in_num
> 3) {
261 * If we have more than 3 input segments the guest wants to actually
264 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
265 ioctl_req
->hdr
.iovec_count
= elem
->in_num
- 3;
266 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
267 ioctl_req
->hdr
.dxfer_len
+= elem
->in_sg
[i
].iov_len
;
270 ioctl_req
->hdr
.dxferp
= elem
->in_sg
;
273 * Some SCSI commands don't actually transfer any data.
275 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_NONE
;
278 ioctl_req
->hdr
.sbp
= elem
->in_sg
[elem
->in_num
- 3].iov_base
;
279 ioctl_req
->hdr
.mx_sb_len
= elem
->in_sg
[elem
->in_num
- 3].iov_len
;
281 blk_aio_ioctl(blk
->blk
, SG_IO
, &ioctl_req
->hdr
,
282 virtio_blk_ioctl_complete
, ioctl_req
);
289 /* Just put anything nonzero so that the ioctl fails in the guest. */
291 virtio_stl_p(vdev
, &scsi
->errors
, 255);
296 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
300 status
= virtio_blk_handle_scsi_req(req
);
301 if (status
!= -EINPROGRESS
) {
302 virtio_blk_req_complete(req
, status
);
303 virtio_blk_free_request(req
);
307 static inline void submit_requests(BlockBackend
*blk
, MultiReqBuffer
*mrb
,
308 int start
, int num_reqs
, int niov
)
310 QEMUIOVector
*qiov
= &mrb
->reqs
[start
]->qiov
;
311 int64_t sector_num
= mrb
->reqs
[start
]->sector_num
;
312 int nb_sectors
= mrb
->reqs
[start
]->qiov
.size
/ BDRV_SECTOR_SIZE
;
313 bool is_write
= mrb
->is_write
;
317 struct iovec
*tmp_iov
= qiov
->iov
;
318 int tmp_niov
= qiov
->niov
;
320 /* mrb->reqs[start]->qiov was initialized from external so we can't
321 * modifiy it here. We need to initialize it locally and then add the
322 * external iovecs. */
323 qemu_iovec_init(qiov
, niov
);
325 for (i
= 0; i
< tmp_niov
; i
++) {
326 qemu_iovec_add(qiov
, tmp_iov
[i
].iov_base
, tmp_iov
[i
].iov_len
);
329 for (i
= start
+ 1; i
< start
+ num_reqs
; i
++) {
330 qemu_iovec_concat(qiov
, &mrb
->reqs
[i
]->qiov
, 0,
331 mrb
->reqs
[i
]->qiov
.size
);
332 mrb
->reqs
[i
- 1]->mr_next
= mrb
->reqs
[i
];
333 nb_sectors
+= mrb
->reqs
[i
]->qiov
.size
/ BDRV_SECTOR_SIZE
;
335 assert(nb_sectors
== qiov
->size
/ BDRV_SECTOR_SIZE
);
337 trace_virtio_blk_submit_multireq(mrb
, start
, num_reqs
, sector_num
,
338 nb_sectors
, is_write
);
339 block_acct_merge_done(blk_get_stats(blk
),
340 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
,
345 blk_aio_writev(blk
, sector_num
, qiov
, nb_sectors
,
346 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
348 blk_aio_readv(blk
, sector_num
, qiov
, nb_sectors
,
349 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
353 static int multireq_compare(const void *a
, const void *b
)
355 const VirtIOBlockReq
*req1
= *(VirtIOBlockReq
**)a
,
356 *req2
= *(VirtIOBlockReq
**)b
;
359 * Note that we can't simply subtract sector_num1 from sector_num2
360 * here as that could overflow the return value.
362 if (req1
->sector_num
> req2
->sector_num
) {
364 } else if (req1
->sector_num
< req2
->sector_num
) {
371 void virtio_blk_submit_multireq(BlockBackend
*blk
, MultiReqBuffer
*mrb
)
373 int i
= 0, start
= 0, num_reqs
= 0, niov
= 0, nb_sectors
= 0;
374 int max_xfer_len
= 0;
375 int64_t sector_num
= 0;
377 if (mrb
->num_reqs
== 1) {
378 submit_requests(blk
, mrb
, 0, 1, -1);
383 max_xfer_len
= blk_get_max_transfer_length(mrb
->reqs
[0]->dev
->blk
);
384 max_xfer_len
= MIN_NON_ZERO(max_xfer_len
, INT_MAX
);
386 qsort(mrb
->reqs
, mrb
->num_reqs
, sizeof(*mrb
->reqs
),
389 for (i
= 0; i
< mrb
->num_reqs
; i
++) {
390 VirtIOBlockReq
*req
= mrb
->reqs
[i
];
394 /* merge would exceed maximum number of IOVs */
395 if (niov
+ req
->qiov
.niov
> IOV_MAX
) {
399 /* merge would exceed maximum transfer length of backend device */
400 if (req
->qiov
.size
/ BDRV_SECTOR_SIZE
+ nb_sectors
> max_xfer_len
) {
404 /* requests are not sequential */
405 if (sector_num
+ nb_sectors
!= req
->sector_num
) {
410 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
416 sector_num
= req
->sector_num
;
417 nb_sectors
= niov
= 0;
421 nb_sectors
+= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
422 niov
+= req
->qiov
.niov
;
426 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
430 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
432 block_acct_start(blk_get_stats(req
->dev
->blk
), &req
->acct
, 0,
436 * Make sure all outstanding writes are posted to the backing device.
438 if (mrb
->is_write
&& mrb
->num_reqs
> 0) {
439 virtio_blk_submit_multireq(req
->dev
->blk
, mrb
);
441 blk_aio_flush(req
->dev
->blk
, virtio_blk_flush_complete
, req
);
444 static bool virtio_blk_sect_range_ok(VirtIOBlock
*dev
,
445 uint64_t sector
, size_t size
)
447 uint64_t nb_sectors
= size
>> BDRV_SECTOR_BITS
;
448 uint64_t total_sectors
;
450 if (nb_sectors
> INT_MAX
) {
453 if (sector
& dev
->sector_mask
) {
456 if (size
% dev
->conf
.conf
.logical_block_size
) {
459 blk_get_geometry(dev
->blk
, &total_sectors
);
460 if (sector
> total_sectors
|| nb_sectors
> total_sectors
- sector
) {
466 void virtio_blk_handle_request(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
469 struct iovec
*in_iov
= req
->elem
.in_sg
;
470 struct iovec
*iov
= req
->elem
.out_sg
;
471 unsigned in_num
= req
->elem
.in_num
;
472 unsigned out_num
= req
->elem
.out_num
;
474 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
475 error_report("virtio-blk missing headers");
479 if (unlikely(iov_to_buf(iov
, out_num
, 0, &req
->out
,
480 sizeof(req
->out
)) != sizeof(req
->out
))) {
481 error_report("virtio-blk request outhdr too short");
485 iov_discard_front(&iov
, &out_num
, sizeof(req
->out
));
488 in_iov
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
489 error_report("virtio-blk request inhdr too short");
493 req
->in
= (void *)in_iov
[in_num
- 1].iov_base
494 + in_iov
[in_num
- 1].iov_len
495 - sizeof(struct virtio_blk_inhdr
);
496 iov_discard_back(in_iov
, &in_num
, sizeof(struct virtio_blk_inhdr
));
498 type
= virtio_ldl_p(VIRTIO_DEVICE(req
->dev
), &req
->out
.type
);
500 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
501 * is an optional flag. Altough a guest should not send this flag if
502 * not negotiated we ignored it in the past. So keep ignoring it. */
503 switch (type
& ~(VIRTIO_BLK_T_OUT
| VIRTIO_BLK_T_BARRIER
)) {
504 case VIRTIO_BLK_T_IN
:
506 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
507 req
->sector_num
= virtio_ldq_p(VIRTIO_DEVICE(req
->dev
),
511 qemu_iovec_init_external(&req
->qiov
, iov
, out_num
);
512 trace_virtio_blk_handle_write(req
, req
->sector_num
,
513 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
515 qemu_iovec_init_external(&req
->qiov
, in_iov
, in_num
);
516 trace_virtio_blk_handle_read(req
, req
->sector_num
,
517 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
520 if (!virtio_blk_sect_range_ok(req
->dev
, req
->sector_num
,
522 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
523 virtio_blk_free_request(req
);
527 block_acct_start(blk_get_stats(req
->dev
->blk
),
528 &req
->acct
, req
->qiov
.size
,
529 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
531 /* merge would exceed maximum number of requests or IO direction
533 if (mrb
->num_reqs
> 0 && (mrb
->num_reqs
== VIRTIO_BLK_MAX_MERGE_REQS
||
534 is_write
!= mrb
->is_write
||
535 !req
->dev
->conf
.request_merging
)) {
536 virtio_blk_submit_multireq(req
->dev
->blk
, mrb
);
539 assert(mrb
->num_reqs
< VIRTIO_BLK_MAX_MERGE_REQS
);
540 mrb
->reqs
[mrb
->num_reqs
++] = req
;
541 mrb
->is_write
= is_write
;
544 case VIRTIO_BLK_T_FLUSH
:
545 virtio_blk_handle_flush(req
, mrb
);
547 case VIRTIO_BLK_T_SCSI_CMD
:
548 virtio_blk_handle_scsi(req
);
550 case VIRTIO_BLK_T_GET_ID
:
552 VirtIOBlock
*s
= req
->dev
;
555 * NB: per existing s/n string convention the string is
556 * terminated by '\0' only when shorter than buffer.
558 const char *serial
= s
->conf
.serial
? s
->conf
.serial
: "";
559 size_t size
= MIN(strlen(serial
) + 1,
560 MIN(iov_size(in_iov
, in_num
),
561 VIRTIO_BLK_ID_BYTES
));
562 iov_from_buf(in_iov
, in_num
, 0, serial
, size
);
563 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
564 virtio_blk_free_request(req
);
568 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
569 virtio_blk_free_request(req
);
573 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
575 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
577 MultiReqBuffer mrb
= {};
579 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
580 * dataplane here instead of waiting for .set_status().
583 virtio_blk_data_plane_start(s
->dataplane
);
587 while ((req
= virtio_blk_get_request(s
))) {
588 virtio_blk_handle_request(req
, &mrb
);
592 virtio_blk_submit_multireq(s
->blk
, &mrb
);
596 * FIXME: Want to check for completions before returning to guest mode,
597 * so cached reads and writes are reported as quickly as possible. But
598 * that should be done in the generic block layer.
602 static void virtio_blk_dma_restart_bh(void *opaque
)
604 VirtIOBlock
*s
= opaque
;
605 VirtIOBlockReq
*req
= s
->rq
;
606 MultiReqBuffer mrb
= {};
608 qemu_bh_delete(s
->bh
);
614 VirtIOBlockReq
*next
= req
->next
;
615 virtio_blk_handle_request(req
, &mrb
);
620 virtio_blk_submit_multireq(s
->blk
, &mrb
);
624 static void virtio_blk_dma_restart_cb(void *opaque
, int running
,
627 VirtIOBlock
*s
= opaque
;
634 s
->bh
= aio_bh_new(blk_get_aio_context(s
->conf
.conf
.blk
),
635 virtio_blk_dma_restart_bh
, s
);
636 qemu_bh_schedule(s
->bh
);
640 static void virtio_blk_reset(VirtIODevice
*vdev
)
642 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
645 virtio_blk_data_plane_stop(s
->dataplane
);
649 * This should cancel pending requests, but can't do nicely until there
650 * are per-device request lists.
653 blk_set_enable_write_cache(s
->blk
, s
->original_wce
);
656 /* coalesce internal state, copy to pci i/o region 0
658 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
660 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
661 BlockConf
*conf
= &s
->conf
.conf
;
662 struct virtio_blk_config blkcfg
;
664 int blk_size
= conf
->logical_block_size
;
666 blk_get_geometry(s
->blk
, &capacity
);
667 memset(&blkcfg
, 0, sizeof(blkcfg
));
668 virtio_stq_p(vdev
, &blkcfg
.capacity
, capacity
);
669 virtio_stl_p(vdev
, &blkcfg
.seg_max
, 128 - 2);
670 virtio_stw_p(vdev
, &blkcfg
.cylinders
, conf
->cyls
);
671 virtio_stl_p(vdev
, &blkcfg
.blk_size
, blk_size
);
672 virtio_stw_p(vdev
, &blkcfg
.min_io_size
, conf
->min_io_size
/ blk_size
);
673 virtio_stw_p(vdev
, &blkcfg
.opt_io_size
, conf
->opt_io_size
/ blk_size
);
674 blkcfg
.heads
= conf
->heads
;
676 * We must ensure that the block device capacity is a multiple of
677 * the logical block size. If that is not the case, let's use
678 * sector_mask to adopt the geometry to have a correct picture.
679 * For those devices where the capacity is ok for the given geometry
680 * we don't touch the sector value of the geometry, since some devices
681 * (like s390 dasd) need a specific value. Here the capacity is already
682 * cyls*heads*secs*blk_size and the sector value is not block size
683 * divided by 512 - instead it is the amount of blk_size blocks
684 * per track (cylinder).
686 if (blk_getlength(s
->blk
) / conf
->heads
/ conf
->secs
% blk_size
) {
687 blkcfg
.sectors
= conf
->secs
& ~s
->sector_mask
;
689 blkcfg
.sectors
= conf
->secs
;
692 blkcfg
.physical_block_exp
= get_physical_block_exp(conf
);
693 blkcfg
.alignment_offset
= 0;
694 blkcfg
.wce
= blk_enable_write_cache(s
->blk
);
695 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
698 static void virtio_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
700 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
701 struct virtio_blk_config blkcfg
;
703 memcpy(&blkcfg
, config
, sizeof(blkcfg
));
705 aio_context_acquire(blk_get_aio_context(s
->blk
));
706 blk_set_enable_write_cache(s
->blk
, blkcfg
.wce
!= 0);
707 aio_context_release(blk_get_aio_context(s
->blk
));
710 static uint32_t virtio_blk_get_features(VirtIODevice
*vdev
, uint32_t features
)
712 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
714 features
|= (1 << VIRTIO_BLK_F_SEG_MAX
);
715 features
|= (1 << VIRTIO_BLK_F_GEOMETRY
);
716 features
|= (1 << VIRTIO_BLK_F_TOPOLOGY
);
717 features
|= (1 << VIRTIO_BLK_F_BLK_SIZE
);
718 features
|= (1 << VIRTIO_BLK_F_SCSI
);
720 if (s
->conf
.config_wce
) {
721 features
|= (1 << VIRTIO_BLK_F_CONFIG_WCE
);
723 if (blk_enable_write_cache(s
->blk
)) {
724 features
|= (1 << VIRTIO_BLK_F_WCE
);
726 if (blk_is_read_only(s
->blk
)) {
727 features
|= 1 << VIRTIO_BLK_F_RO
;
733 static void virtio_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
735 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
738 if (s
->dataplane
&& !(status
& (VIRTIO_CONFIG_S_DRIVER
|
739 VIRTIO_CONFIG_S_DRIVER_OK
))) {
740 virtio_blk_data_plane_stop(s
->dataplane
);
743 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
747 features
= vdev
->guest_features
;
749 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
750 * cache flushes. Thus, the "auto writethrough" behavior is never
751 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
752 * Leaving it enabled would break the following sequence:
754 * Guest started with "-drive cache=writethrough"
755 * Guest sets status to 0
756 * Guest sets DRIVER bit in status field
757 * Guest reads host features (WCE=0, CONFIG_WCE=1)
758 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
759 * Guest writes 1 to the WCE configuration field (writeback mode)
760 * Guest sets DRIVER_OK bit in status field
762 * s->blk would erroneously be placed in writethrough mode.
764 if (!(features
& (1 << VIRTIO_BLK_F_CONFIG_WCE
))) {
765 aio_context_acquire(blk_get_aio_context(s
->blk
));
766 blk_set_enable_write_cache(s
->blk
,
767 !!(features
& (1 << VIRTIO_BLK_F_WCE
)));
768 aio_context_release(blk_get_aio_context(s
->blk
));
772 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
774 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
776 virtio_save(vdev
, f
);
779 static void virtio_blk_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
781 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
782 VirtIOBlockReq
*req
= s
->rq
;
785 qemu_put_sbyte(f
, 1);
786 qemu_put_buffer(f
, (unsigned char *)&req
->elem
,
787 sizeof(VirtQueueElement
));
790 qemu_put_sbyte(f
, 0);
793 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
795 VirtIOBlock
*s
= opaque
;
796 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
801 return virtio_load(vdev
, f
, version_id
);
804 static int virtio_blk_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
807 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
809 while (qemu_get_sbyte(f
)) {
810 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
811 qemu_get_buffer(f
, (unsigned char *)&req
->elem
,
812 sizeof(VirtQueueElement
));
816 virtqueue_map_sg(req
->elem
.in_sg
, req
->elem
.in_addr
,
817 req
->elem
.in_num
, 1);
818 virtqueue_map_sg(req
->elem
.out_sg
, req
->elem
.out_addr
,
819 req
->elem
.out_num
, 0);
825 static void virtio_blk_resize(void *opaque
)
827 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
829 virtio_notify_config(vdev
);
832 static const BlockDevOps virtio_block_ops
= {
833 .resize_cb
= virtio_blk_resize
,
836 /* Disable dataplane thread during live migration since it does not
837 * update the dirty memory bitmap yet.
839 static void virtio_blk_migration_state_changed(Notifier
*notifier
, void *data
)
841 VirtIOBlock
*s
= container_of(notifier
, VirtIOBlock
,
842 migration_state_notifier
);
843 MigrationState
*mig
= data
;
846 if (migration_in_setup(mig
)) {
850 virtio_blk_data_plane_destroy(s
->dataplane
);
852 } else if (migration_has_finished(mig
) ||
853 migration_has_failed(mig
)) {
857 blk_drain_all(); /* complete in-flight non-dataplane requests */
858 virtio_blk_data_plane_create(VIRTIO_DEVICE(s
), &s
->conf
,
859 &s
->dataplane
, &err
);
861 error_report("%s", error_get_pretty(err
));
867 static void virtio_blk_device_realize(DeviceState
*dev
, Error
**errp
)
869 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
870 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
871 VirtIOBlkConf
*conf
= &s
->conf
;
873 static int virtio_blk_id
;
875 if (!conf
->conf
.blk
) {
876 error_setg(errp
, "drive property not set");
879 if (!blk_is_inserted(conf
->conf
.blk
)) {
880 error_setg(errp
, "Device needs media, but drive is empty");
884 blkconf_serial(&conf
->conf
, &conf
->serial
);
885 s
->original_wce
= blk_enable_write_cache(conf
->conf
.blk
);
886 blkconf_geometry(&conf
->conf
, NULL
, 65535, 255, 255, &err
);
888 error_propagate(errp
, err
);
892 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
893 sizeof(struct virtio_blk_config
));
895 s
->blk
= conf
->conf
.blk
;
897 s
->sector_mask
= (s
->conf
.conf
.logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
899 s
->vq
= virtio_add_queue(vdev
, 128, virtio_blk_handle_output
);
900 s
->complete_request
= virtio_blk_complete_request
;
901 virtio_blk_data_plane_create(vdev
, conf
, &s
->dataplane
, &err
);
903 error_propagate(errp
, err
);
904 virtio_cleanup(vdev
);
907 s
->migration_state_notifier
.notify
= virtio_blk_migration_state_changed
;
908 add_migration_state_change_notifier(&s
->migration_state_notifier
);
910 s
->change
= qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
911 register_savevm(dev
, "virtio-blk", virtio_blk_id
++, 2,
912 virtio_blk_save
, virtio_blk_load
, s
);
913 blk_set_dev_ops(s
->blk
, &virtio_block_ops
, s
);
914 blk_set_guest_block_size(s
->blk
, s
->conf
.conf
.logical_block_size
);
916 blk_iostatus_enable(s
->blk
);
919 static void virtio_blk_device_unrealize(DeviceState
*dev
, Error
**errp
)
921 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
922 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
924 remove_migration_state_change_notifier(&s
->migration_state_notifier
);
925 virtio_blk_data_plane_destroy(s
->dataplane
);
927 qemu_del_vm_change_state_handler(s
->change
);
928 unregister_savevm(dev
, "virtio-blk", s
);
929 blockdev_mark_auto_del(s
->blk
);
930 virtio_cleanup(vdev
);
933 static void virtio_blk_instance_init(Object
*obj
)
935 VirtIOBlock
*s
= VIRTIO_BLK(obj
);
937 object_property_add_link(obj
, "iothread", TYPE_IOTHREAD
,
938 (Object
**)&s
->conf
.iothread
,
939 qdev_prop_allow_set_link_before_realize
,
940 OBJ_PROP_LINK_UNREF_ON_RELEASE
, NULL
);
941 device_add_bootindex_property(obj
, &s
->conf
.conf
.bootindex
,
942 "bootindex", "/disk@0,0",
946 static Property virtio_blk_properties
[] = {
947 DEFINE_BLOCK_PROPERTIES(VirtIOBlock
, conf
.conf
),
948 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock
, conf
.conf
),
949 DEFINE_PROP_STRING("serial", VirtIOBlock
, conf
.serial
),
950 DEFINE_PROP_BIT("config-wce", VirtIOBlock
, conf
.config_wce
, 0, true),
952 DEFINE_PROP_BIT("scsi", VirtIOBlock
, conf
.scsi
, 0, true),
954 DEFINE_PROP_BIT("request-merging", VirtIOBlock
, conf
.request_merging
, 0,
956 DEFINE_PROP_BIT("x-data-plane", VirtIOBlock
, conf
.data_plane
, 0, false),
957 DEFINE_PROP_END_OF_LIST(),
960 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
962 DeviceClass
*dc
= DEVICE_CLASS(klass
);
963 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
965 dc
->props
= virtio_blk_properties
;
966 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
967 vdc
->realize
= virtio_blk_device_realize
;
968 vdc
->unrealize
= virtio_blk_device_unrealize
;
969 vdc
->get_config
= virtio_blk_update_config
;
970 vdc
->set_config
= virtio_blk_set_config
;
971 vdc
->get_features
= virtio_blk_get_features
;
972 vdc
->set_status
= virtio_blk_set_status
;
973 vdc
->reset
= virtio_blk_reset
;
974 vdc
->save
= virtio_blk_save_device
;
975 vdc
->load
= virtio_blk_load_device
;
978 static const TypeInfo virtio_device_info
= {
979 .name
= TYPE_VIRTIO_BLK
,
980 .parent
= TYPE_VIRTIO_DEVICE
,
981 .instance_size
= sizeof(VirtIOBlock
),
982 .instance_init
= virtio_blk_instance_init
,
983 .class_init
= virtio_blk_class_init
,
986 static void virtio_register_types(void)
988 type_register_static(&virtio_device_info
);
991 type_init(virtio_register_types
)