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/blockdev.h"
22 #include "hw/virtio/virtio-blk.h"
23 #include "dataplane/virtio-blk.h"
24 #include "scsi/constants.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/virtio/virtio-access.h"
31 static void virtio_blk_init_request(VirtIOBlock
*s
, VirtQueue
*vq
,
42 static void virtio_blk_free_request(VirtIOBlockReq
*req
)
47 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, unsigned char status
)
49 VirtIOBlock
*s
= req
->dev
;
50 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
52 trace_virtio_blk_req_complete(vdev
, req
, status
);
54 stb_p(&req
->in
->status
, status
);
55 virtqueue_push(req
->vq
, &req
->elem
, req
->in_len
);
56 if (s
->dataplane_started
&& !s
->dataplane_disabled
) {
57 virtio_blk_data_plane_notify(s
->dataplane
, req
->vq
);
59 virtio_notify(vdev
, req
->vq
);
63 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
66 VirtIOBlock
*s
= req
->dev
;
67 BlockErrorAction action
= blk_get_error_action(s
->blk
, is_read
, error
);
69 if (action
== BLOCK_ERROR_ACTION_STOP
) {
70 /* Break the link as the next request is going to be parsed from the
71 * ring again. Otherwise we may end up doing a double completion! */
75 } else if (action
== BLOCK_ERROR_ACTION_REPORT
) {
76 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
77 block_acct_failed(blk_get_stats(s
->blk
), &req
->acct
);
78 virtio_blk_free_request(req
);
81 blk_error_action(s
->blk
, action
, is_read
, error
);
82 return action
!= BLOCK_ERROR_ACTION_IGNORE
;
85 static void virtio_blk_rw_complete(void *opaque
, int ret
)
87 VirtIOBlockReq
*next
= opaque
;
88 VirtIOBlock
*s
= next
->dev
;
89 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
91 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
93 VirtIOBlockReq
*req
= next
;
95 trace_virtio_blk_rw_complete(vdev
, req
, ret
);
97 if (req
->qiov
.nalloc
!= -1) {
98 /* If nalloc is != -1 req->qiov is a local copy of the original
99 * external iovec. It was allocated in submit_requests to be
100 * able to merge requests. */
101 qemu_iovec_destroy(&req
->qiov
);
105 int p
= virtio_ldl_p(VIRTIO_DEVICE(req
->dev
), &req
->out
.type
);
106 bool is_read
= !(p
& VIRTIO_BLK_T_OUT
);
107 /* Note that memory may be dirtied on read failure. If the
108 * virtio request is not completed here, as is the case for
109 * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
110 * correctly during live migration. While this is ugly,
111 * it is acceptable because the device is free to write to
112 * the memory until the request is completed (which will
113 * happen on the other side of the migration).
115 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
)) {
120 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
121 block_acct_done(blk_get_stats(req
->dev
->blk
), &req
->acct
);
122 virtio_blk_free_request(req
);
124 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
127 static void virtio_blk_flush_complete(void *opaque
, int ret
)
129 VirtIOBlockReq
*req
= opaque
;
130 VirtIOBlock
*s
= req
->dev
;
132 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
134 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
139 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
140 block_acct_done(blk_get_stats(s
->blk
), &req
->acct
);
141 virtio_blk_free_request(req
);
144 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
151 struct sg_io_hdr hdr
;
152 } VirtIOBlockIoctlReq
;
154 static void virtio_blk_ioctl_complete(void *opaque
, int status
)
156 VirtIOBlockIoctlReq
*ioctl_req
= opaque
;
157 VirtIOBlockReq
*req
= ioctl_req
->req
;
158 VirtIOBlock
*s
= req
->dev
;
159 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
160 struct virtio_scsi_inhdr
*scsi
;
161 struct sg_io_hdr
*hdr
;
163 scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
166 status
= VIRTIO_BLK_S_UNSUPP
;
167 virtio_stl_p(vdev
, &scsi
->errors
, 255);
171 hdr
= &ioctl_req
->hdr
;
173 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
174 * clear the masked_status field [hence status gets cleared too, see
175 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
176 * status has occurred. However they do set DRIVER_SENSE in driver_status
177 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
179 if (hdr
->status
== 0 && hdr
->sb_len_wr
> 0) {
180 hdr
->status
= CHECK_CONDITION
;
183 virtio_stl_p(vdev
, &scsi
->errors
,
184 hdr
->status
| (hdr
->msg_status
<< 8) |
185 (hdr
->host_status
<< 16) | (hdr
->driver_status
<< 24));
186 virtio_stl_p(vdev
, &scsi
->residual
, hdr
->resid
);
187 virtio_stl_p(vdev
, &scsi
->sense_len
, hdr
->sb_len_wr
);
188 virtio_stl_p(vdev
, &scsi
->data_len
, hdr
->dxfer_len
);
191 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
192 virtio_blk_req_complete(req
, status
);
193 virtio_blk_free_request(req
);
194 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
200 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
, VirtQueue
*vq
)
202 VirtIOBlockReq
*req
= virtqueue_pop(vq
, sizeof(VirtIOBlockReq
));
205 virtio_blk_init_request(s
, vq
, req
);
210 static int virtio_blk_handle_scsi_req(VirtIOBlockReq
*req
)
212 int status
= VIRTIO_BLK_S_OK
;
213 struct virtio_scsi_inhdr
*scsi
= NULL
;
214 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
215 VirtQueueElement
*elem
= &req
->elem
;
216 VirtIOBlock
*blk
= req
->dev
;
220 VirtIOBlockIoctlReq
*ioctl_req
;
225 * We require at least one output segment each for the virtio_blk_outhdr
226 * and the SCSI command block.
228 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
229 * and the sense buffer pointer in the input segments.
231 if (elem
->out_num
< 2 || elem
->in_num
< 3) {
232 status
= VIRTIO_BLK_S_IOERR
;
237 * The scsi inhdr is placed in the second-to-last input segment, just
238 * before the regular inhdr.
240 scsi
= (void *)elem
->in_sg
[elem
->in_num
- 2].iov_base
;
242 if (!blk
->conf
.scsi
) {
243 status
= VIRTIO_BLK_S_UNSUPP
;
248 * No support for bidirection commands yet.
250 if (elem
->out_num
> 2 && elem
->in_num
> 3) {
251 status
= VIRTIO_BLK_S_UNSUPP
;
256 ioctl_req
= g_new0(VirtIOBlockIoctlReq
, 1);
257 ioctl_req
->req
= req
;
258 ioctl_req
->hdr
.interface_id
= 'S';
259 ioctl_req
->hdr
.cmd_len
= elem
->out_sg
[1].iov_len
;
260 ioctl_req
->hdr
.cmdp
= elem
->out_sg
[1].iov_base
;
261 ioctl_req
->hdr
.dxfer_len
= 0;
263 if (elem
->out_num
> 2) {
265 * If there are more than the minimally required 2 output segments
266 * there is write payload starting from the third iovec.
268 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
269 ioctl_req
->hdr
.iovec_count
= elem
->out_num
- 2;
271 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
272 ioctl_req
->hdr
.dxfer_len
+= elem
->out_sg
[i
+ 2].iov_len
;
275 ioctl_req
->hdr
.dxferp
= elem
->out_sg
+ 2;
277 } else if (elem
->in_num
> 3) {
279 * If we have more than 3 input segments the guest wants to actually
282 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
283 ioctl_req
->hdr
.iovec_count
= elem
->in_num
- 3;
284 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
285 ioctl_req
->hdr
.dxfer_len
+= elem
->in_sg
[i
].iov_len
;
288 ioctl_req
->hdr
.dxferp
= elem
->in_sg
;
291 * Some SCSI commands don't actually transfer any data.
293 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_NONE
;
296 ioctl_req
->hdr
.sbp
= elem
->in_sg
[elem
->in_num
- 3].iov_base
;
297 ioctl_req
->hdr
.mx_sb_len
= elem
->in_sg
[elem
->in_num
- 3].iov_len
;
299 acb
= blk_aio_ioctl(blk
->blk
, SG_IO
, &ioctl_req
->hdr
,
300 virtio_blk_ioctl_complete
, ioctl_req
);
303 status
= VIRTIO_BLK_S_UNSUPP
;
312 /* Just put anything nonzero so that the ioctl fails in the guest. */
314 virtio_stl_p(vdev
, &scsi
->errors
, 255);
319 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
323 status
= virtio_blk_handle_scsi_req(req
);
324 if (status
!= -EINPROGRESS
) {
325 virtio_blk_req_complete(req
, status
);
326 virtio_blk_free_request(req
);
330 static inline void submit_requests(BlockBackend
*blk
, MultiReqBuffer
*mrb
,
331 int start
, int num_reqs
, int niov
)
333 QEMUIOVector
*qiov
= &mrb
->reqs
[start
]->qiov
;
334 int64_t sector_num
= mrb
->reqs
[start
]->sector_num
;
335 bool is_write
= mrb
->is_write
;
339 struct iovec
*tmp_iov
= qiov
->iov
;
340 int tmp_niov
= qiov
->niov
;
342 /* mrb->reqs[start]->qiov was initialized from external so we can't
343 * modify it here. We need to initialize it locally and then add the
344 * external iovecs. */
345 qemu_iovec_init(qiov
, niov
);
347 for (i
= 0; i
< tmp_niov
; i
++) {
348 qemu_iovec_add(qiov
, tmp_iov
[i
].iov_base
, tmp_iov
[i
].iov_len
);
351 for (i
= start
+ 1; i
< start
+ num_reqs
; i
++) {
352 qemu_iovec_concat(qiov
, &mrb
->reqs
[i
]->qiov
, 0,
353 mrb
->reqs
[i
]->qiov
.size
);
354 mrb
->reqs
[i
- 1]->mr_next
= mrb
->reqs
[i
];
357 trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb
->reqs
[start
]->dev
),
358 mrb
, start
, num_reqs
,
359 sector_num
<< BDRV_SECTOR_BITS
,
360 qiov
->size
, is_write
);
361 block_acct_merge_done(blk_get_stats(blk
),
362 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
,
367 blk_aio_pwritev(blk
, sector_num
<< BDRV_SECTOR_BITS
, qiov
, 0,
368 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
370 blk_aio_preadv(blk
, sector_num
<< BDRV_SECTOR_BITS
, qiov
, 0,
371 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
375 static int multireq_compare(const void *a
, const void *b
)
377 const VirtIOBlockReq
*req1
= *(VirtIOBlockReq
**)a
,
378 *req2
= *(VirtIOBlockReq
**)b
;
381 * Note that we can't simply subtract sector_num1 from sector_num2
382 * here as that could overflow the return value.
384 if (req1
->sector_num
> req2
->sector_num
) {
386 } else if (req1
->sector_num
< req2
->sector_num
) {
393 static void virtio_blk_submit_multireq(BlockBackend
*blk
, MultiReqBuffer
*mrb
)
395 int i
= 0, start
= 0, num_reqs
= 0, niov
= 0, nb_sectors
= 0;
396 uint32_t max_transfer
;
397 int64_t sector_num
= 0;
399 if (mrb
->num_reqs
== 1) {
400 submit_requests(blk
, mrb
, 0, 1, -1);
405 max_transfer
= blk_get_max_transfer(mrb
->reqs
[0]->dev
->blk
);
407 qsort(mrb
->reqs
, mrb
->num_reqs
, sizeof(*mrb
->reqs
),
410 for (i
= 0; i
< mrb
->num_reqs
; i
++) {
411 VirtIOBlockReq
*req
= mrb
->reqs
[i
];
414 * NOTE: We cannot merge the requests in below situations:
415 * 1. requests are not sequential
416 * 2. merge would exceed maximum number of IOVs
417 * 3. merge would exceed maximum transfer length of backend device
419 if (sector_num
+ nb_sectors
!= req
->sector_num
||
420 niov
> blk_get_max_iov(blk
) - req
->qiov
.niov
||
421 req
->qiov
.size
> max_transfer
||
422 nb_sectors
> (max_transfer
-
423 req
->qiov
.size
) / BDRV_SECTOR_SIZE
) {
424 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
430 sector_num
= req
->sector_num
;
431 nb_sectors
= niov
= 0;
435 nb_sectors
+= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
436 niov
+= req
->qiov
.niov
;
440 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
444 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
446 block_acct_start(blk_get_stats(req
->dev
->blk
), &req
->acct
, 0,
450 * Make sure all outstanding writes are posted to the backing device.
452 if (mrb
->is_write
&& mrb
->num_reqs
> 0) {
453 virtio_blk_submit_multireq(req
->dev
->blk
, mrb
);
455 blk_aio_flush(req
->dev
->blk
, virtio_blk_flush_complete
, req
);
458 static bool virtio_blk_sect_range_ok(VirtIOBlock
*dev
,
459 uint64_t sector
, size_t size
)
461 uint64_t nb_sectors
= size
>> BDRV_SECTOR_BITS
;
462 uint64_t total_sectors
;
464 if (nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
467 if (sector
& dev
->sector_mask
) {
470 if (size
% dev
->conf
.conf
.logical_block_size
) {
473 blk_get_geometry(dev
->blk
, &total_sectors
);
474 if (sector
> total_sectors
|| nb_sectors
> total_sectors
- sector
) {
480 static int virtio_blk_handle_request(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
483 struct iovec
*in_iov
= req
->elem
.in_sg
;
484 struct iovec
*out_iov
= req
->elem
.out_sg
;
485 unsigned in_num
= req
->elem
.in_num
;
486 unsigned out_num
= req
->elem
.out_num
;
487 VirtIOBlock
*s
= req
->dev
;
488 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
490 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
491 virtio_error(vdev
, "virtio-blk missing headers");
495 if (unlikely(iov_to_buf(out_iov
, out_num
, 0, &req
->out
,
496 sizeof(req
->out
)) != sizeof(req
->out
))) {
497 virtio_error(vdev
, "virtio-blk request outhdr too short");
501 iov_discard_front(&out_iov
, &out_num
, sizeof(req
->out
));
503 if (in_iov
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
504 virtio_error(vdev
, "virtio-blk request inhdr too short");
508 /* We always touch the last byte, so just see how big in_iov is. */
509 req
->in_len
= iov_size(in_iov
, in_num
);
510 req
->in
= (void *)in_iov
[in_num
- 1].iov_base
511 + in_iov
[in_num
- 1].iov_len
512 - sizeof(struct virtio_blk_inhdr
);
513 iov_discard_back(in_iov
, &in_num
, sizeof(struct virtio_blk_inhdr
));
515 type
= virtio_ldl_p(vdev
, &req
->out
.type
);
517 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
518 * is an optional flag. Although a guest should not send this flag if
519 * not negotiated we ignored it in the past. So keep ignoring it. */
520 switch (type
& ~(VIRTIO_BLK_T_OUT
| VIRTIO_BLK_T_BARRIER
)) {
521 case VIRTIO_BLK_T_IN
:
523 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
524 req
->sector_num
= virtio_ldq_p(vdev
, &req
->out
.sector
);
527 qemu_iovec_init_external(&req
->qiov
, out_iov
, out_num
);
528 trace_virtio_blk_handle_write(vdev
, req
, req
->sector_num
,
529 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
531 qemu_iovec_init_external(&req
->qiov
, in_iov
, in_num
);
532 trace_virtio_blk_handle_read(vdev
, req
, req
->sector_num
,
533 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
536 if (!virtio_blk_sect_range_ok(s
, req
->sector_num
, req
->qiov
.size
)) {
537 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
538 block_acct_invalid(blk_get_stats(s
->blk
),
539 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
540 virtio_blk_free_request(req
);
544 block_acct_start(blk_get_stats(s
->blk
), &req
->acct
, req
->qiov
.size
,
545 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
547 /* merge would exceed maximum number of requests or IO direction
549 if (mrb
->num_reqs
> 0 && (mrb
->num_reqs
== VIRTIO_BLK_MAX_MERGE_REQS
||
550 is_write
!= mrb
->is_write
||
551 !s
->conf
.request_merging
)) {
552 virtio_blk_submit_multireq(s
->blk
, mrb
);
555 assert(mrb
->num_reqs
< VIRTIO_BLK_MAX_MERGE_REQS
);
556 mrb
->reqs
[mrb
->num_reqs
++] = req
;
557 mrb
->is_write
= is_write
;
560 case VIRTIO_BLK_T_FLUSH
:
561 virtio_blk_handle_flush(req
, mrb
);
563 case VIRTIO_BLK_T_SCSI_CMD
:
564 virtio_blk_handle_scsi(req
);
566 case VIRTIO_BLK_T_GET_ID
:
569 * NB: per existing s/n string convention the string is
570 * terminated by '\0' only when shorter than buffer.
572 const char *serial
= s
->conf
.serial
? s
->conf
.serial
: "";
573 size_t size
= MIN(strlen(serial
) + 1,
574 MIN(iov_size(in_iov
, in_num
),
575 VIRTIO_BLK_ID_BYTES
));
576 iov_from_buf(in_iov
, in_num
, 0, serial
, size
);
577 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
578 virtio_blk_free_request(req
);
582 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
583 virtio_blk_free_request(req
);
588 bool virtio_blk_handle_vq(VirtIOBlock
*s
, VirtQueue
*vq
)
591 MultiReqBuffer mrb
= {};
592 bool progress
= false;
594 aio_context_acquire(blk_get_aio_context(s
->blk
));
598 virtio_queue_set_notification(vq
, 0);
600 while ((req
= virtio_blk_get_request(s
, vq
))) {
602 if (virtio_blk_handle_request(req
, &mrb
)) {
603 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
604 virtio_blk_free_request(req
);
609 virtio_queue_set_notification(vq
, 1);
610 } while (!virtio_queue_empty(vq
));
613 virtio_blk_submit_multireq(s
->blk
, &mrb
);
616 blk_io_unplug(s
->blk
);
617 aio_context_release(blk_get_aio_context(s
->blk
));
621 static void virtio_blk_handle_output_do(VirtIOBlock
*s
, VirtQueue
*vq
)
623 virtio_blk_handle_vq(s
, vq
);
626 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
628 VirtIOBlock
*s
= (VirtIOBlock
*)vdev
;
631 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
632 * dataplane here instead of waiting for .set_status().
634 virtio_device_start_ioeventfd(vdev
);
635 if (!s
->dataplane_disabled
) {
639 virtio_blk_handle_output_do(s
, vq
);
642 static void virtio_blk_dma_restart_bh(void *opaque
)
644 VirtIOBlock
*s
= opaque
;
645 VirtIOBlockReq
*req
= s
->rq
;
646 MultiReqBuffer mrb
= {};
648 qemu_bh_delete(s
->bh
);
653 aio_context_acquire(blk_get_aio_context(s
->conf
.conf
.blk
));
655 VirtIOBlockReq
*next
= req
->next
;
656 if (virtio_blk_handle_request(req
, &mrb
)) {
657 /* Device is now broken and won't do any processing until it gets
658 * reset. Already queued requests will be lost: let's purge them.
662 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
663 virtio_blk_free_request(req
);
672 virtio_blk_submit_multireq(s
->blk
, &mrb
);
674 aio_context_release(blk_get_aio_context(s
->conf
.conf
.blk
));
677 static void virtio_blk_dma_restart_cb(void *opaque
, int running
,
680 VirtIOBlock
*s
= opaque
;
687 s
->bh
= aio_bh_new(blk_get_aio_context(s
->conf
.conf
.blk
),
688 virtio_blk_dma_restart_bh
, s
);
689 qemu_bh_schedule(s
->bh
);
693 static void virtio_blk_reset(VirtIODevice
*vdev
)
695 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
699 ctx
= blk_get_aio_context(s
->blk
);
700 aio_context_acquire(ctx
);
703 /* We drop queued requests after blk_drain() because blk_drain() itself can
708 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
709 virtio_blk_free_request(req
);
712 aio_context_release(ctx
);
714 assert(!s
->dataplane_started
);
715 blk_set_enable_write_cache(s
->blk
, s
->original_wce
);
718 /* coalesce internal state, copy to pci i/o region 0
720 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
722 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
723 BlockConf
*conf
= &s
->conf
.conf
;
724 struct virtio_blk_config blkcfg
;
727 int blk_size
= conf
->logical_block_size
;
729 blk_get_geometry(s
->blk
, &capacity
);
730 memset(&blkcfg
, 0, sizeof(blkcfg
));
731 virtio_stq_p(vdev
, &blkcfg
.capacity
, capacity
);
732 virtio_stl_p(vdev
, &blkcfg
.seg_max
, 128 - 2);
733 virtio_stw_p(vdev
, &blkcfg
.geometry
.cylinders
, conf
->cyls
);
734 virtio_stl_p(vdev
, &blkcfg
.blk_size
, blk_size
);
735 virtio_stw_p(vdev
, &blkcfg
.min_io_size
, conf
->min_io_size
/ blk_size
);
736 virtio_stw_p(vdev
, &blkcfg
.opt_io_size
, conf
->opt_io_size
/ blk_size
);
737 blkcfg
.geometry
.heads
= conf
->heads
;
739 * We must ensure that the block device capacity is a multiple of
740 * the logical block size. If that is not the case, let's use
741 * sector_mask to adopt the geometry to have a correct picture.
742 * For those devices where the capacity is ok for the given geometry
743 * we don't touch the sector value of the geometry, since some devices
744 * (like s390 dasd) need a specific value. Here the capacity is already
745 * cyls*heads*secs*blk_size and the sector value is not block size
746 * divided by 512 - instead it is the amount of blk_size blocks
747 * per track (cylinder).
749 length
= blk_getlength(s
->blk
);
750 if (length
> 0 && length
/ conf
->heads
/ conf
->secs
% blk_size
) {
751 blkcfg
.geometry
.sectors
= conf
->secs
& ~s
->sector_mask
;
753 blkcfg
.geometry
.sectors
= conf
->secs
;
756 blkcfg
.physical_block_exp
= get_physical_block_exp(conf
);
757 blkcfg
.alignment_offset
= 0;
758 blkcfg
.wce
= blk_enable_write_cache(s
->blk
);
759 virtio_stw_p(vdev
, &blkcfg
.num_queues
, s
->conf
.num_queues
);
760 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
763 static void virtio_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
765 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
766 struct virtio_blk_config blkcfg
;
768 memcpy(&blkcfg
, config
, sizeof(blkcfg
));
770 aio_context_acquire(blk_get_aio_context(s
->blk
));
771 blk_set_enable_write_cache(s
->blk
, blkcfg
.wce
!= 0);
772 aio_context_release(blk_get_aio_context(s
->blk
));
775 static uint64_t virtio_blk_get_features(VirtIODevice
*vdev
, uint64_t features
,
778 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
780 virtio_add_feature(&features
, VIRTIO_BLK_F_SEG_MAX
);
781 virtio_add_feature(&features
, VIRTIO_BLK_F_GEOMETRY
);
782 virtio_add_feature(&features
, VIRTIO_BLK_F_TOPOLOGY
);
783 virtio_add_feature(&features
, VIRTIO_BLK_F_BLK_SIZE
);
784 if (virtio_has_feature(features
, VIRTIO_F_VERSION_1
)) {
786 error_setg(errp
, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
790 virtio_clear_feature(&features
, VIRTIO_F_ANY_LAYOUT
);
791 virtio_add_feature(&features
, VIRTIO_BLK_F_SCSI
);
794 if (s
->conf
.config_wce
) {
795 virtio_add_feature(&features
, VIRTIO_BLK_F_CONFIG_WCE
);
797 if (blk_enable_write_cache(s
->blk
)) {
798 virtio_add_feature(&features
, VIRTIO_BLK_F_WCE
);
800 if (blk_is_read_only(s
->blk
)) {
801 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
803 if (s
->conf
.num_queues
> 1) {
804 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
810 static void virtio_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
812 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
814 if (!(status
& (VIRTIO_CONFIG_S_DRIVER
| VIRTIO_CONFIG_S_DRIVER_OK
))) {
815 assert(!s
->dataplane_started
);
818 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
822 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
823 * cache flushes. Thus, the "auto writethrough" behavior is never
824 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
825 * Leaving it enabled would break the following sequence:
827 * Guest started with "-drive cache=writethrough"
828 * Guest sets status to 0
829 * Guest sets DRIVER bit in status field
830 * Guest reads host features (WCE=0, CONFIG_WCE=1)
831 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
832 * Guest writes 1 to the WCE configuration field (writeback mode)
833 * Guest sets DRIVER_OK bit in status field
835 * s->blk would erroneously be placed in writethrough mode.
837 if (!virtio_vdev_has_feature(vdev
, VIRTIO_BLK_F_CONFIG_WCE
)) {
838 aio_context_acquire(blk_get_aio_context(s
->blk
));
839 blk_set_enable_write_cache(s
->blk
,
840 virtio_vdev_has_feature(vdev
,
842 aio_context_release(blk_get_aio_context(s
->blk
));
846 static void virtio_blk_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
848 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
849 VirtIOBlockReq
*req
= s
->rq
;
852 qemu_put_sbyte(f
, 1);
854 if (s
->conf
.num_queues
> 1) {
855 qemu_put_be32(f
, virtio_get_queue_index(req
->vq
));
858 qemu_put_virtqueue_element(f
, &req
->elem
);
861 qemu_put_sbyte(f
, 0);
864 static int virtio_blk_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
867 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
869 while (qemu_get_sbyte(f
)) {
870 unsigned nvqs
= s
->conf
.num_queues
;
875 vq_idx
= qemu_get_be32(f
);
877 if (vq_idx
>= nvqs
) {
878 error_report("Invalid virtqueue index in request list: %#x",
884 req
= qemu_get_virtqueue_element(vdev
, f
, sizeof(VirtIOBlockReq
));
885 virtio_blk_init_request(s
, virtio_get_queue(vdev
, vq_idx
), req
);
893 static void virtio_blk_resize(void *opaque
)
895 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
897 virtio_notify_config(vdev
);
900 static const BlockDevOps virtio_block_ops
= {
901 .resize_cb
= virtio_blk_resize
,
904 static void virtio_blk_device_realize(DeviceState
*dev
, Error
**errp
)
906 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
907 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
908 VirtIOBlkConf
*conf
= &s
->conf
;
912 if (!conf
->conf
.blk
) {
913 error_setg(errp
, "drive property not set");
916 if (!blk_is_inserted(conf
->conf
.blk
)) {
917 error_setg(errp
, "Device needs media, but drive is empty");
920 if (!conf
->num_queues
) {
921 error_setg(errp
, "num-queues property must be larger than 0");
924 if (!is_power_of_2(conf
->queue_size
) ||
925 conf
->queue_size
> VIRTQUEUE_MAX_SIZE
) {
926 error_setg(errp
, "invalid queue-size property (%" PRIu16
"), "
927 "must be a power of 2 (max %d)",
928 conf
->queue_size
, VIRTQUEUE_MAX_SIZE
);
932 if (!blkconf_apply_backend_options(&conf
->conf
,
933 blk_is_read_only(conf
->conf
.blk
), true,
937 s
->original_wce
= blk_enable_write_cache(conf
->conf
.blk
);
938 if (!blkconf_geometry(&conf
->conf
, NULL
, 65535, 255, 255, errp
)) {
942 blkconf_blocksizes(&conf
->conf
);
944 if (conf
->conf
.logical_block_size
>
945 conf
->conf
.physical_block_size
) {
947 "logical_block_size > physical_block_size not supported");
951 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
952 sizeof(struct virtio_blk_config
));
954 s
->blk
= conf
->conf
.blk
;
956 s
->sector_mask
= (s
->conf
.conf
.logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
958 for (i
= 0; i
< conf
->num_queues
; i
++) {
959 virtio_add_queue(vdev
, conf
->queue_size
, virtio_blk_handle_output
);
961 virtio_blk_data_plane_create(vdev
, conf
, &s
->dataplane
, &err
);
963 error_propagate(errp
, err
);
964 virtio_cleanup(vdev
);
968 s
->change
= qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
969 blk_set_dev_ops(s
->blk
, &virtio_block_ops
, s
);
970 blk_set_guest_block_size(s
->blk
, s
->conf
.conf
.logical_block_size
);
972 blk_iostatus_enable(s
->blk
);
975 static void virtio_blk_device_unrealize(DeviceState
*dev
, Error
**errp
)
977 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
978 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
980 virtio_blk_data_plane_destroy(s
->dataplane
);
982 qemu_del_vm_change_state_handler(s
->change
);
983 blockdev_mark_auto_del(s
->blk
);
984 virtio_cleanup(vdev
);
987 static void virtio_blk_instance_init(Object
*obj
)
989 VirtIOBlock
*s
= VIRTIO_BLK(obj
);
991 device_add_bootindex_property(obj
, &s
->conf
.conf
.bootindex
,
992 "bootindex", "/disk@0,0",
996 static const VMStateDescription vmstate_virtio_blk
= {
997 .name
= "virtio-blk",
998 .minimum_version_id
= 2,
1000 .fields
= (VMStateField
[]) {
1001 VMSTATE_VIRTIO_DEVICE
,
1002 VMSTATE_END_OF_LIST()
1006 static Property virtio_blk_properties
[] = {
1007 DEFINE_BLOCK_PROPERTIES(VirtIOBlock
, conf
.conf
),
1008 DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock
, conf
.conf
),
1009 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock
, conf
.conf
),
1010 DEFINE_PROP_STRING("serial", VirtIOBlock
, conf
.serial
),
1011 DEFINE_PROP_BIT("config-wce", VirtIOBlock
, conf
.config_wce
, 0, true),
1013 DEFINE_PROP_BIT("scsi", VirtIOBlock
, conf
.scsi
, 0, false),
1015 DEFINE_PROP_BIT("request-merging", VirtIOBlock
, conf
.request_merging
, 0,
1017 DEFINE_PROP_UINT16("num-queues", VirtIOBlock
, conf
.num_queues
, 1),
1018 DEFINE_PROP_UINT16("queue-size", VirtIOBlock
, conf
.queue_size
, 128),
1019 DEFINE_PROP_LINK("iothread", VirtIOBlock
, conf
.iothread
, TYPE_IOTHREAD
,
1021 DEFINE_PROP_END_OF_LIST(),
1024 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
1026 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1027 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1029 dc
->props
= virtio_blk_properties
;
1030 dc
->vmsd
= &vmstate_virtio_blk
;
1031 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1032 vdc
->realize
= virtio_blk_device_realize
;
1033 vdc
->unrealize
= virtio_blk_device_unrealize
;
1034 vdc
->get_config
= virtio_blk_update_config
;
1035 vdc
->set_config
= virtio_blk_set_config
;
1036 vdc
->get_features
= virtio_blk_get_features
;
1037 vdc
->set_status
= virtio_blk_set_status
;
1038 vdc
->reset
= virtio_blk_reset
;
1039 vdc
->save
= virtio_blk_save_device
;
1040 vdc
->load
= virtio_blk_load_device
;
1041 vdc
->start_ioeventfd
= virtio_blk_data_plane_start
;
1042 vdc
->stop_ioeventfd
= virtio_blk_data_plane_stop
;
1045 static const TypeInfo virtio_blk_info
= {
1046 .name
= TYPE_VIRTIO_BLK
,
1047 .parent
= TYPE_VIRTIO_DEVICE
,
1048 .instance_size
= sizeof(VirtIOBlock
),
1049 .instance_init
= virtio_blk_instance_init
,
1050 .class_init
= virtio_blk_class_init
,
1053 static void virtio_register_types(void)
1055 type_register_static(&virtio_blk_info
);
1058 type_init(virtio_register_types
)