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 "block/scsi.h"
29 #include "hw/virtio/virtio-bus.h"
30 #include "hw/virtio/virtio-access.h"
32 void virtio_blk_init_request(VirtIOBlock
*s
, VirtIOBlockReq
*req
)
41 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(req
, status
);
55 stb_p(&req
->in
->status
, status
);
56 virtqueue_push(s
->vq
, &req
->elem
, req
->in_len
);
58 virtio_blk_data_plane_notify(s
->dataplane
);
60 virtio_notify(vdev
, s
->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
;
92 VirtIOBlockReq
*req
= next
;
94 trace_virtio_blk_rw_complete(req
, ret
);
96 if (req
->qiov
.nalloc
!= -1) {
97 /* If nalloc is != 1 req->qiov is a local copy of the original
98 * external iovec. It was allocated in submit_merged_requests
99 * to be able to merge requests. */
100 qemu_iovec_destroy(&req
->qiov
);
104 int p
= virtio_ldl_p(VIRTIO_DEVICE(req
->dev
), &req
->out
.type
);
105 bool is_read
= !(p
& VIRTIO_BLK_T_OUT
);
106 /* Note that memory may be dirtied on read failure. If the
107 * virtio request is not completed here, as is the case for
108 * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
109 * correctly during live migration. While this is ugly,
110 * it is acceptable because the device is free to write to
111 * the memory until the request is completed (which will
112 * happen on the other side of the migration).
114 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
)) {
119 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
120 block_acct_done(blk_get_stats(req
->dev
->blk
), &req
->acct
);
121 virtio_blk_free_request(req
);
125 static void virtio_blk_flush_complete(void *opaque
, int ret
)
127 VirtIOBlockReq
*req
= opaque
;
130 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
135 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
136 block_acct_done(blk_get_stats(req
->dev
->blk
), &req
->acct
);
137 virtio_blk_free_request(req
);
144 struct sg_io_hdr hdr
;
145 } VirtIOBlockIoctlReq
;
147 static void virtio_blk_ioctl_complete(void *opaque
, int status
)
149 VirtIOBlockIoctlReq
*ioctl_req
= opaque
;
150 VirtIOBlockReq
*req
= ioctl_req
->req
;
151 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
152 struct virtio_scsi_inhdr
*scsi
;
153 struct sg_io_hdr
*hdr
;
155 scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
158 status
= VIRTIO_BLK_S_UNSUPP
;
159 virtio_stl_p(vdev
, &scsi
->errors
, 255);
163 hdr
= &ioctl_req
->hdr
;
165 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
166 * clear the masked_status field [hence status gets cleared too, see
167 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
168 * status has occurred. However they do set DRIVER_SENSE in driver_status
169 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
171 if (hdr
->status
== 0 && hdr
->sb_len_wr
> 0) {
172 hdr
->status
= CHECK_CONDITION
;
175 virtio_stl_p(vdev
, &scsi
->errors
,
176 hdr
->status
| (hdr
->msg_status
<< 8) |
177 (hdr
->host_status
<< 16) | (hdr
->driver_status
<< 24));
178 virtio_stl_p(vdev
, &scsi
->residual
, hdr
->resid
);
179 virtio_stl_p(vdev
, &scsi
->sense_len
, hdr
->sb_len_wr
);
180 virtio_stl_p(vdev
, &scsi
->data_len
, hdr
->dxfer_len
);
183 virtio_blk_req_complete(req
, status
);
184 virtio_blk_free_request(req
);
190 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
192 VirtIOBlockReq
*req
= virtqueue_pop(s
->vq
, sizeof(VirtIOBlockReq
));
195 virtio_blk_init_request(s
, req
);
200 static int virtio_blk_handle_scsi_req(VirtIOBlockReq
*req
)
202 int status
= VIRTIO_BLK_S_OK
;
203 struct virtio_scsi_inhdr
*scsi
= NULL
;
204 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
205 VirtQueueElement
*elem
= &req
->elem
;
206 VirtIOBlock
*blk
= req
->dev
;
210 VirtIOBlockIoctlReq
*ioctl_req
;
215 * We require at least one output segment each for the virtio_blk_outhdr
216 * and the SCSI command block.
218 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
219 * and the sense buffer pointer in the input segments.
221 if (elem
->out_num
< 2 || elem
->in_num
< 3) {
222 status
= VIRTIO_BLK_S_IOERR
;
227 * The scsi inhdr is placed in the second-to-last input segment, just
228 * before the regular inhdr.
230 scsi
= (void *)elem
->in_sg
[elem
->in_num
- 2].iov_base
;
232 if (!blk
->conf
.scsi
) {
233 status
= VIRTIO_BLK_S_UNSUPP
;
238 * No support for bidirection commands yet.
240 if (elem
->out_num
> 2 && elem
->in_num
> 3) {
241 status
= VIRTIO_BLK_S_UNSUPP
;
246 ioctl_req
= g_new0(VirtIOBlockIoctlReq
, 1);
247 ioctl_req
->req
= req
;
248 ioctl_req
->hdr
.interface_id
= 'S';
249 ioctl_req
->hdr
.cmd_len
= elem
->out_sg
[1].iov_len
;
250 ioctl_req
->hdr
.cmdp
= elem
->out_sg
[1].iov_base
;
251 ioctl_req
->hdr
.dxfer_len
= 0;
253 if (elem
->out_num
> 2) {
255 * If there are more than the minimally required 2 output segments
256 * there is write payload starting from the third iovec.
258 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
259 ioctl_req
->hdr
.iovec_count
= elem
->out_num
- 2;
261 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
262 ioctl_req
->hdr
.dxfer_len
+= elem
->out_sg
[i
+ 2].iov_len
;
265 ioctl_req
->hdr
.dxferp
= elem
->out_sg
+ 2;
267 } else if (elem
->in_num
> 3) {
269 * If we have more than 3 input segments the guest wants to actually
272 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
273 ioctl_req
->hdr
.iovec_count
= elem
->in_num
- 3;
274 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
275 ioctl_req
->hdr
.dxfer_len
+= elem
->in_sg
[i
].iov_len
;
278 ioctl_req
->hdr
.dxferp
= elem
->in_sg
;
281 * Some SCSI commands don't actually transfer any data.
283 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_NONE
;
286 ioctl_req
->hdr
.sbp
= elem
->in_sg
[elem
->in_num
- 3].iov_base
;
287 ioctl_req
->hdr
.mx_sb_len
= elem
->in_sg
[elem
->in_num
- 3].iov_len
;
289 acb
= blk_aio_ioctl(blk
->blk
, SG_IO
, &ioctl_req
->hdr
,
290 virtio_blk_ioctl_complete
, ioctl_req
);
293 status
= VIRTIO_BLK_S_UNSUPP
;
302 /* Just put anything nonzero so that the ioctl fails in the guest. */
304 virtio_stl_p(vdev
, &scsi
->errors
, 255);
309 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
313 status
= virtio_blk_handle_scsi_req(req
);
314 if (status
!= -EINPROGRESS
) {
315 virtio_blk_req_complete(req
, status
);
316 virtio_blk_free_request(req
);
320 static inline void submit_requests(BlockBackend
*blk
, MultiReqBuffer
*mrb
,
321 int start
, int num_reqs
, int niov
)
323 QEMUIOVector
*qiov
= &mrb
->reqs
[start
]->qiov
;
324 int64_t sector_num
= mrb
->reqs
[start
]->sector_num
;
325 int nb_sectors
= mrb
->reqs
[start
]->qiov
.size
/ BDRV_SECTOR_SIZE
;
326 bool is_write
= mrb
->is_write
;
330 struct iovec
*tmp_iov
= qiov
->iov
;
331 int tmp_niov
= qiov
->niov
;
333 /* mrb->reqs[start]->qiov was initialized from external so we can't
334 * modifiy it here. We need to initialize it locally and then add the
335 * external iovecs. */
336 qemu_iovec_init(qiov
, niov
);
338 for (i
= 0; i
< tmp_niov
; i
++) {
339 qemu_iovec_add(qiov
, tmp_iov
[i
].iov_base
, tmp_iov
[i
].iov_len
);
342 for (i
= start
+ 1; i
< start
+ num_reqs
; i
++) {
343 qemu_iovec_concat(qiov
, &mrb
->reqs
[i
]->qiov
, 0,
344 mrb
->reqs
[i
]->qiov
.size
);
345 mrb
->reqs
[i
- 1]->mr_next
= mrb
->reqs
[i
];
346 nb_sectors
+= mrb
->reqs
[i
]->qiov
.size
/ BDRV_SECTOR_SIZE
;
348 assert(nb_sectors
== qiov
->size
/ BDRV_SECTOR_SIZE
);
350 trace_virtio_blk_submit_multireq(mrb
, start
, num_reqs
, sector_num
,
351 nb_sectors
, is_write
);
352 block_acct_merge_done(blk_get_stats(blk
),
353 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
,
358 blk_aio_writev(blk
, sector_num
, qiov
, nb_sectors
,
359 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
361 blk_aio_readv(blk
, sector_num
, qiov
, nb_sectors
,
362 virtio_blk_rw_complete
, mrb
->reqs
[start
]);
366 static int multireq_compare(const void *a
, const void *b
)
368 const VirtIOBlockReq
*req1
= *(VirtIOBlockReq
**)a
,
369 *req2
= *(VirtIOBlockReq
**)b
;
372 * Note that we can't simply subtract sector_num1 from sector_num2
373 * here as that could overflow the return value.
375 if (req1
->sector_num
> req2
->sector_num
) {
377 } else if (req1
->sector_num
< req2
->sector_num
) {
384 void virtio_blk_submit_multireq(BlockBackend
*blk
, MultiReqBuffer
*mrb
)
386 int i
= 0, start
= 0, num_reqs
= 0, niov
= 0, nb_sectors
= 0;
387 int max_xfer_len
= 0;
388 int64_t sector_num
= 0;
390 if (mrb
->num_reqs
== 1) {
391 submit_requests(blk
, mrb
, 0, 1, -1);
396 max_xfer_len
= blk_get_max_transfer_length(mrb
->reqs
[0]->dev
->blk
);
397 max_xfer_len
= MIN_NON_ZERO(max_xfer_len
, BDRV_REQUEST_MAX_SECTORS
);
399 qsort(mrb
->reqs
, mrb
->num_reqs
, sizeof(*mrb
->reqs
),
402 for (i
= 0; i
< mrb
->num_reqs
; i
++) {
403 VirtIOBlockReq
*req
= mrb
->reqs
[i
];
406 * NOTE: We cannot merge the requests in below situations:
407 * 1. requests are not sequential
408 * 2. merge would exceed maximum number of IOVs
409 * 3. merge would exceed maximum transfer length of backend device
411 if (sector_num
+ nb_sectors
!= req
->sector_num
||
412 niov
> blk_get_max_iov(blk
) - req
->qiov
.niov
||
413 req
->qiov
.size
/ BDRV_SECTOR_SIZE
> max_xfer_len
||
414 nb_sectors
> max_xfer_len
- req
->qiov
.size
/ BDRV_SECTOR_SIZE
) {
415 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
421 sector_num
= req
->sector_num
;
422 nb_sectors
= niov
= 0;
426 nb_sectors
+= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
427 niov
+= req
->qiov
.niov
;
431 submit_requests(blk
, mrb
, start
, num_reqs
, niov
);
435 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
437 block_acct_start(blk_get_stats(req
->dev
->blk
), &req
->acct
, 0,
441 * Make sure all outstanding writes are posted to the backing device.
443 if (mrb
->is_write
&& mrb
->num_reqs
> 0) {
444 virtio_blk_submit_multireq(req
->dev
->blk
, mrb
);
446 blk_aio_flush(req
->dev
->blk
, virtio_blk_flush_complete
, req
);
449 static bool virtio_blk_sect_range_ok(VirtIOBlock
*dev
,
450 uint64_t sector
, size_t size
)
452 uint64_t nb_sectors
= size
>> BDRV_SECTOR_BITS
;
453 uint64_t total_sectors
;
455 if (nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
458 if (sector
& dev
->sector_mask
) {
461 if (size
% dev
->conf
.conf
.logical_block_size
) {
464 blk_get_geometry(dev
->blk
, &total_sectors
);
465 if (sector
> total_sectors
|| nb_sectors
> total_sectors
- sector
) {
471 void virtio_blk_handle_request(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
474 struct iovec
*in_iov
= req
->elem
.in_sg
;
475 struct iovec
*iov
= req
->elem
.out_sg
;
476 unsigned in_num
= req
->elem
.in_num
;
477 unsigned out_num
= req
->elem
.out_num
;
479 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
480 error_report("virtio-blk missing headers");
484 if (unlikely(iov_to_buf(iov
, out_num
, 0, &req
->out
,
485 sizeof(req
->out
)) != sizeof(req
->out
))) {
486 error_report("virtio-blk request outhdr too short");
490 iov_discard_front(&iov
, &out_num
, sizeof(req
->out
));
492 if (in_iov
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
493 error_report("virtio-blk request inhdr too short");
497 /* We always touch the last byte, so just see how big in_iov is. */
498 req
->in_len
= iov_size(in_iov
, in_num
);
499 req
->in
= (void *)in_iov
[in_num
- 1].iov_base
500 + in_iov
[in_num
- 1].iov_len
501 - sizeof(struct virtio_blk_inhdr
);
502 iov_discard_back(in_iov
, &in_num
, sizeof(struct virtio_blk_inhdr
));
504 type
= virtio_ldl_p(VIRTIO_DEVICE(req
->dev
), &req
->out
.type
);
506 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
507 * is an optional flag. Although a guest should not send this flag if
508 * not negotiated we ignored it in the past. So keep ignoring it. */
509 switch (type
& ~(VIRTIO_BLK_T_OUT
| VIRTIO_BLK_T_BARRIER
)) {
510 case VIRTIO_BLK_T_IN
:
512 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
513 req
->sector_num
= virtio_ldq_p(VIRTIO_DEVICE(req
->dev
),
517 qemu_iovec_init_external(&req
->qiov
, iov
, out_num
);
518 trace_virtio_blk_handle_write(req
, req
->sector_num
,
519 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
521 qemu_iovec_init_external(&req
->qiov
, in_iov
, in_num
);
522 trace_virtio_blk_handle_read(req
, req
->sector_num
,
523 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
526 if (!virtio_blk_sect_range_ok(req
->dev
, req
->sector_num
,
528 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
529 block_acct_invalid(blk_get_stats(req
->dev
->blk
),
530 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
531 virtio_blk_free_request(req
);
535 block_acct_start(blk_get_stats(req
->dev
->blk
),
536 &req
->acct
, req
->qiov
.size
,
537 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
539 /* merge would exceed maximum number of requests or IO direction
541 if (mrb
->num_reqs
> 0 && (mrb
->num_reqs
== VIRTIO_BLK_MAX_MERGE_REQS
||
542 is_write
!= mrb
->is_write
||
543 !req
->dev
->conf
.request_merging
)) {
544 virtio_blk_submit_multireq(req
->dev
->blk
, mrb
);
547 assert(mrb
->num_reqs
< VIRTIO_BLK_MAX_MERGE_REQS
);
548 mrb
->reqs
[mrb
->num_reqs
++] = req
;
549 mrb
->is_write
= is_write
;
552 case VIRTIO_BLK_T_FLUSH
:
553 virtio_blk_handle_flush(req
, mrb
);
555 case VIRTIO_BLK_T_SCSI_CMD
:
556 virtio_blk_handle_scsi(req
);
558 case VIRTIO_BLK_T_GET_ID
:
560 VirtIOBlock
*s
= req
->dev
;
563 * NB: per existing s/n string convention the string is
564 * terminated by '\0' only when shorter than buffer.
566 const char *serial
= s
->conf
.serial
? s
->conf
.serial
: "";
567 size_t size
= MIN(strlen(serial
) + 1,
568 MIN(iov_size(in_iov
, in_num
),
569 VIRTIO_BLK_ID_BYTES
));
570 iov_from_buf(in_iov
, in_num
, 0, serial
, size
);
571 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
572 virtio_blk_free_request(req
);
576 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
577 virtio_blk_free_request(req
);
581 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
583 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
585 MultiReqBuffer mrb
= {};
587 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
588 * dataplane here instead of waiting for .set_status().
590 if (s
->dataplane
&& !s
->dataplane_started
) {
591 virtio_blk_data_plane_start(s
->dataplane
);
597 while ((req
= virtio_blk_get_request(s
))) {
598 virtio_blk_handle_request(req
, &mrb
);
602 virtio_blk_submit_multireq(s
->blk
, &mrb
);
605 blk_io_unplug(s
->blk
);
608 static void virtio_blk_dma_restart_bh(void *opaque
)
610 VirtIOBlock
*s
= opaque
;
611 VirtIOBlockReq
*req
= s
->rq
;
612 MultiReqBuffer mrb
= {};
614 qemu_bh_delete(s
->bh
);
620 VirtIOBlockReq
*next
= req
->next
;
621 virtio_blk_handle_request(req
, &mrb
);
626 virtio_blk_submit_multireq(s
->blk
, &mrb
);
630 static void virtio_blk_dma_restart_cb(void *opaque
, int running
,
633 VirtIOBlock
*s
= opaque
;
640 s
->bh
= aio_bh_new(blk_get_aio_context(s
->conf
.conf
.blk
),
641 virtio_blk_dma_restart_bh
, s
);
642 qemu_bh_schedule(s
->bh
);
646 static void virtio_blk_reset(VirtIODevice
*vdev
)
648 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
652 * This should cancel pending requests, but can't do nicely until there
653 * are per-device request lists.
655 ctx
= blk_get_aio_context(s
->blk
);
656 aio_context_acquire(ctx
);
660 virtio_blk_data_plane_stop(s
->dataplane
);
662 aio_context_release(ctx
);
664 blk_set_enable_write_cache(s
->blk
, s
->original_wce
);
667 /* coalesce internal state, copy to pci i/o region 0
669 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
671 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
672 BlockConf
*conf
= &s
->conf
.conf
;
673 struct virtio_blk_config blkcfg
;
675 int blk_size
= conf
->logical_block_size
;
677 blk_get_geometry(s
->blk
, &capacity
);
678 memset(&blkcfg
, 0, sizeof(blkcfg
));
679 virtio_stq_p(vdev
, &blkcfg
.capacity
, capacity
);
680 virtio_stl_p(vdev
, &blkcfg
.seg_max
, 128 - 2);
681 virtio_stw_p(vdev
, &blkcfg
.geometry
.cylinders
, conf
->cyls
);
682 virtio_stl_p(vdev
, &blkcfg
.blk_size
, blk_size
);
683 virtio_stw_p(vdev
, &blkcfg
.min_io_size
, conf
->min_io_size
/ blk_size
);
684 virtio_stw_p(vdev
, &blkcfg
.opt_io_size
, conf
->opt_io_size
/ blk_size
);
685 blkcfg
.geometry
.heads
= conf
->heads
;
687 * We must ensure that the block device capacity is a multiple of
688 * the logical block size. If that is not the case, let's use
689 * sector_mask to adopt the geometry to have a correct picture.
690 * For those devices where the capacity is ok for the given geometry
691 * we don't touch the sector value of the geometry, since some devices
692 * (like s390 dasd) need a specific value. Here the capacity is already
693 * cyls*heads*secs*blk_size and the sector value is not block size
694 * divided by 512 - instead it is the amount of blk_size blocks
695 * per track (cylinder).
697 if (blk_getlength(s
->blk
) / conf
->heads
/ conf
->secs
% blk_size
) {
698 blkcfg
.geometry
.sectors
= conf
->secs
& ~s
->sector_mask
;
700 blkcfg
.geometry
.sectors
= conf
->secs
;
703 blkcfg
.physical_block_exp
= get_physical_block_exp(conf
);
704 blkcfg
.alignment_offset
= 0;
705 blkcfg
.wce
= blk_enable_write_cache(s
->blk
);
706 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
709 static void virtio_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
711 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
712 struct virtio_blk_config blkcfg
;
714 memcpy(&blkcfg
, config
, sizeof(blkcfg
));
716 aio_context_acquire(blk_get_aio_context(s
->blk
));
717 blk_set_enable_write_cache(s
->blk
, blkcfg
.wce
!= 0);
718 aio_context_release(blk_get_aio_context(s
->blk
));
721 static uint64_t virtio_blk_get_features(VirtIODevice
*vdev
, uint64_t features
,
724 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
726 virtio_add_feature(&features
, VIRTIO_BLK_F_SEG_MAX
);
727 virtio_add_feature(&features
, VIRTIO_BLK_F_GEOMETRY
);
728 virtio_add_feature(&features
, VIRTIO_BLK_F_TOPOLOGY
);
729 virtio_add_feature(&features
, VIRTIO_BLK_F_BLK_SIZE
);
730 if (virtio_has_feature(features
, VIRTIO_F_VERSION_1
)) {
732 error_setg(errp
, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
736 virtio_clear_feature(&features
, VIRTIO_F_ANY_LAYOUT
);
737 virtio_add_feature(&features
, VIRTIO_BLK_F_SCSI
);
740 if (s
->conf
.config_wce
) {
741 virtio_add_feature(&features
, VIRTIO_BLK_F_CONFIG_WCE
);
743 if (blk_enable_write_cache(s
->blk
)) {
744 virtio_add_feature(&features
, VIRTIO_BLK_F_WCE
);
746 if (blk_is_read_only(s
->blk
)) {
747 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
753 static void virtio_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
755 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
757 if (s
->dataplane
&& !(status
& (VIRTIO_CONFIG_S_DRIVER
|
758 VIRTIO_CONFIG_S_DRIVER_OK
))) {
759 virtio_blk_data_plane_stop(s
->dataplane
);
762 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
766 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
767 * cache flushes. Thus, the "auto writethrough" behavior is never
768 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
769 * Leaving it enabled would break the following sequence:
771 * Guest started with "-drive cache=writethrough"
772 * Guest sets status to 0
773 * Guest sets DRIVER bit in status field
774 * Guest reads host features (WCE=0, CONFIG_WCE=1)
775 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
776 * Guest writes 1 to the WCE configuration field (writeback mode)
777 * Guest sets DRIVER_OK bit in status field
779 * s->blk would erroneously be placed in writethrough mode.
781 if (!virtio_vdev_has_feature(vdev
, VIRTIO_BLK_F_CONFIG_WCE
)) {
782 aio_context_acquire(blk_get_aio_context(s
->blk
));
783 blk_set_enable_write_cache(s
->blk
,
784 virtio_vdev_has_feature(vdev
,
786 aio_context_release(blk_get_aio_context(s
->blk
));
790 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
792 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
793 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
796 virtio_blk_data_plane_stop(s
->dataplane
);
799 virtio_save(vdev
, f
);
802 static void virtio_blk_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
804 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
805 VirtIOBlockReq
*req
= s
->rq
;
808 qemu_put_sbyte(f
, 1);
809 qemu_put_virtqueue_element(f
, &req
->elem
);
812 qemu_put_sbyte(f
, 0);
815 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
817 VirtIOBlock
*s
= opaque
;
818 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
823 return virtio_load(vdev
, f
, version_id
);
826 static int virtio_blk_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
829 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
831 while (qemu_get_sbyte(f
)) {
833 req
= qemu_get_virtqueue_element(f
, sizeof(VirtIOBlockReq
));
834 virtio_blk_init_request(s
, req
);
842 static void virtio_blk_resize(void *opaque
)
844 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
846 virtio_notify_config(vdev
);
849 static const BlockDevOps virtio_block_ops
= {
850 .resize_cb
= virtio_blk_resize
,
853 static void virtio_blk_device_realize(DeviceState
*dev
, Error
**errp
)
855 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
856 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
857 VirtIOBlkConf
*conf
= &s
->conf
;
859 static int virtio_blk_id
;
861 if (!conf
->conf
.blk
) {
862 error_setg(errp
, "drive property not set");
865 if (!blk_is_inserted(conf
->conf
.blk
)) {
866 error_setg(errp
, "Device needs media, but drive is empty");
870 blkconf_serial(&conf
->conf
, &conf
->serial
);
871 s
->original_wce
= blk_enable_write_cache(conf
->conf
.blk
);
872 blkconf_geometry(&conf
->conf
, NULL
, 65535, 255, 255, &err
);
874 error_propagate(errp
, err
);
877 blkconf_blocksizes(&conf
->conf
);
879 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
880 sizeof(struct virtio_blk_config
));
882 s
->blk
= conf
->conf
.blk
;
884 s
->sector_mask
= (s
->conf
.conf
.logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
886 s
->vq
= virtio_add_queue(vdev
, 128, virtio_blk_handle_output
);
887 virtio_blk_data_plane_create(vdev
, conf
, &s
->dataplane
, &err
);
889 error_propagate(errp
, err
);
890 virtio_cleanup(vdev
);
894 s
->change
= qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
895 register_savevm(dev
, "virtio-blk", virtio_blk_id
++, 2,
896 virtio_blk_save
, virtio_blk_load
, s
);
897 blk_set_dev_ops(s
->blk
, &virtio_block_ops
, s
);
898 blk_set_guest_block_size(s
->blk
, s
->conf
.conf
.logical_block_size
);
900 blk_iostatus_enable(s
->blk
);
903 static void virtio_blk_device_unrealize(DeviceState
*dev
, Error
**errp
)
905 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
906 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
908 virtio_blk_data_plane_destroy(s
->dataplane
);
910 qemu_del_vm_change_state_handler(s
->change
);
911 unregister_savevm(dev
, "virtio-blk", s
);
912 blockdev_mark_auto_del(s
->blk
);
913 virtio_cleanup(vdev
);
916 static void virtio_blk_instance_init(Object
*obj
)
918 VirtIOBlock
*s
= VIRTIO_BLK(obj
);
920 object_property_add_link(obj
, "iothread", TYPE_IOTHREAD
,
921 (Object
**)&s
->conf
.iothread
,
922 qdev_prop_allow_set_link_before_realize
,
923 OBJ_PROP_LINK_UNREF_ON_RELEASE
, NULL
);
924 device_add_bootindex_property(obj
, &s
->conf
.conf
.bootindex
,
925 "bootindex", "/disk@0,0",
929 static Property virtio_blk_properties
[] = {
930 DEFINE_BLOCK_PROPERTIES(VirtIOBlock
, conf
.conf
),
931 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock
, conf
.conf
),
932 DEFINE_PROP_STRING("serial", VirtIOBlock
, conf
.serial
),
933 DEFINE_PROP_BIT("config-wce", VirtIOBlock
, conf
.config_wce
, 0, true),
935 DEFINE_PROP_BIT("scsi", VirtIOBlock
, conf
.scsi
, 0, false),
937 DEFINE_PROP_BIT("request-merging", VirtIOBlock
, conf
.request_merging
, 0,
939 DEFINE_PROP_END_OF_LIST(),
942 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
944 DeviceClass
*dc
= DEVICE_CLASS(klass
);
945 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
947 dc
->props
= virtio_blk_properties
;
948 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
949 vdc
->realize
= virtio_blk_device_realize
;
950 vdc
->unrealize
= virtio_blk_device_unrealize
;
951 vdc
->get_config
= virtio_blk_update_config
;
952 vdc
->set_config
= virtio_blk_set_config
;
953 vdc
->get_features
= virtio_blk_get_features
;
954 vdc
->set_status
= virtio_blk_set_status
;
955 vdc
->reset
= virtio_blk_reset
;
956 vdc
->save
= virtio_blk_save_device
;
957 vdc
->load
= virtio_blk_load_device
;
960 static const TypeInfo virtio_device_info
= {
961 .name
= TYPE_VIRTIO_BLK
,
962 .parent
= TYPE_VIRTIO_DEVICE
,
963 .instance_size
= sizeof(VirtIOBlock
),
964 .instance_init
= virtio_blk_instance_init
,
965 .class_init
= virtio_blk_class_init
,
968 static void virtio_register_types(void)
970 type_register_static(&virtio_device_info
);
973 type_init(virtio_register_types
)