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"
15 #include "qemu-error.h"
18 #include "virtio-blk.h"
19 #include "scsi-defs.h"
24 typedef struct VirtIOBlock
33 unsigned short sector_mask
;
37 static VirtIOBlock
*to_virtio_blk(VirtIODevice
*vdev
)
39 return (VirtIOBlock
*)vdev
;
42 typedef struct VirtIOBlockReq
45 VirtQueueElement elem
;
46 struct virtio_blk_inhdr
*in
;
47 struct virtio_blk_outhdr
*out
;
48 struct virtio_scsi_inhdr
*scsi
;
50 struct VirtIOBlockReq
*next
;
54 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, int status
)
56 VirtIOBlock
*s
= req
->dev
;
58 trace_virtio_blk_req_complete(req
, status
);
60 stb_p(&req
->in
->status
, status
);
61 virtqueue_push(s
->vq
, &req
->elem
, req
->qiov
.size
+ sizeof(*req
->in
));
62 virtio_notify(&s
->vdev
, s
->vq
);
65 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
68 BlockErrorAction action
= bdrv_get_on_error(req
->dev
->bs
, is_read
);
69 VirtIOBlock
*s
= req
->dev
;
71 if (action
== BLOCK_ERR_IGNORE
) {
72 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
76 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
77 || action
== BLOCK_ERR_STOP_ANY
) {
80 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
81 vm_stop(RUN_STATE_IO_ERROR
);
82 bdrv_iostatus_set_err(s
->bs
, error
);
84 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
85 bdrv_acct_done(s
->bs
, &req
->acct
);
87 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
93 static void virtio_blk_rw_complete(void *opaque
, int ret
)
95 VirtIOBlockReq
*req
= opaque
;
97 trace_virtio_blk_rw_complete(req
, ret
);
100 int is_read
= !(ldl_p(&req
->out
->type
) & VIRTIO_BLK_T_OUT
);
101 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
))
105 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
106 bdrv_acct_done(req
->dev
->bs
, &req
->acct
);
110 static void virtio_blk_flush_complete(void *opaque
, int ret
)
112 VirtIOBlockReq
*req
= opaque
;
115 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
120 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
121 bdrv_acct_done(req
->dev
->bs
, &req
->acct
);
125 static VirtIOBlockReq
*virtio_blk_alloc_request(VirtIOBlock
*s
)
127 VirtIOBlockReq
*req
= g_malloc(sizeof(*req
));
134 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
136 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
139 if (!virtqueue_pop(s
->vq
, &req
->elem
)) {
149 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
151 struct sg_io_hdr hdr
;
157 * We require at least one output segment each for the virtio_blk_outhdr
158 * and the SCSI command block.
160 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
161 * and the sense buffer pointer in the input segments.
163 if (req
->elem
.out_num
< 2 || req
->elem
.in_num
< 3) {
164 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
170 * No support for bidirection commands yet.
172 if (req
->elem
.out_num
> 2 && req
->elem
.in_num
> 3) {
173 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
179 * The scsi inhdr is placed in the second-to-last input segment, just
180 * before the regular inhdr.
182 req
->scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
184 memset(&hdr
, 0, sizeof(struct sg_io_hdr
));
185 hdr
.interface_id
= 'S';
186 hdr
.cmd_len
= req
->elem
.out_sg
[1].iov_len
;
187 hdr
.cmdp
= req
->elem
.out_sg
[1].iov_base
;
190 if (req
->elem
.out_num
> 2) {
192 * If there are more than the minimally required 2 output segments
193 * there is write payload starting from the third iovec.
195 hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
196 hdr
.iovec_count
= req
->elem
.out_num
- 2;
198 for (i
= 0; i
< hdr
.iovec_count
; i
++)
199 hdr
.dxfer_len
+= req
->elem
.out_sg
[i
+ 2].iov_len
;
201 hdr
.dxferp
= req
->elem
.out_sg
+ 2;
203 } else if (req
->elem
.in_num
> 3) {
205 * If we have more than 3 input segments the guest wants to actually
208 hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
209 hdr
.iovec_count
= req
->elem
.in_num
- 3;
210 for (i
= 0; i
< hdr
.iovec_count
; i
++)
211 hdr
.dxfer_len
+= req
->elem
.in_sg
[i
].iov_len
;
213 hdr
.dxferp
= req
->elem
.in_sg
;
216 * Some SCSI commands don't actually transfer any data.
218 hdr
.dxfer_direction
= SG_DXFER_NONE
;
221 hdr
.sbp
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_base
;
222 hdr
.mx_sb_len
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_len
;
224 ret
= bdrv_ioctl(req
->dev
->bs
, SG_IO
, &hdr
);
226 status
= VIRTIO_BLK_S_UNSUPP
;
228 hdr
.resid
= hdr
.dxfer_len
;
229 } else if (hdr
.status
) {
230 status
= VIRTIO_BLK_S_IOERR
;
232 status
= VIRTIO_BLK_S_OK
;
236 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
237 * clear the masked_status field [hence status gets cleared too, see
238 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
239 * status has occurred. However they do set DRIVER_SENSE in driver_status
240 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
242 if (hdr
.status
== 0 && hdr
.sb_len_wr
> 0) {
243 hdr
.status
= CHECK_CONDITION
;
246 stl_p(&req
->scsi
->errors
,
247 hdr
.status
| (hdr
.msg_status
<< 8) |
248 (hdr
.host_status
<< 16) | (hdr
.driver_status
<< 24));
249 stl_p(&req
->scsi
->residual
, hdr
.resid
);
250 stl_p(&req
->scsi
->sense_len
, hdr
.sb_len_wr
);
251 stl_p(&req
->scsi
->data_len
, hdr
.dxfer_len
);
253 virtio_blk_req_complete(req
, status
);
257 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
259 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
262 #endif /* __linux__ */
264 typedef struct MultiReqBuffer
{
265 BlockRequest blkreq
[32];
266 unsigned int num_writes
;
269 static void virtio_submit_multiwrite(BlockDriverState
*bs
, MultiReqBuffer
*mrb
)
273 if (!mrb
->num_writes
) {
277 ret
= bdrv_aio_multiwrite(bs
, mrb
->blkreq
, mrb
->num_writes
);
279 for (i
= 0; i
< mrb
->num_writes
; i
++) {
280 if (mrb
->blkreq
[i
].error
) {
281 virtio_blk_rw_complete(mrb
->blkreq
[i
].opaque
, -EIO
);
289 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
291 BlockDriverAIOCB
*acb
;
293 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, 0, BDRV_ACCT_FLUSH
);
296 * Make sure all outstanding writes are posted to the backing device.
298 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
300 acb
= bdrv_aio_flush(req
->dev
->bs
, virtio_blk_flush_complete
, req
);
302 virtio_blk_flush_complete(req
, -EIO
);
306 static void virtio_blk_handle_write(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
308 BlockRequest
*blkreq
;
311 sector
= ldq_p(&req
->out
->sector
);
313 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, req
->qiov
.size
, BDRV_ACCT_WRITE
);
315 trace_virtio_blk_handle_write(req
, sector
, req
->qiov
.size
/ 512);
317 if (sector
& req
->dev
->sector_mask
) {
318 virtio_blk_rw_complete(req
, -EIO
);
321 if (req
->qiov
.size
% req
->dev
->conf
->logical_block_size
) {
322 virtio_blk_rw_complete(req
, -EIO
);
326 if (mrb
->num_writes
== 32) {
327 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
330 blkreq
= &mrb
->blkreq
[mrb
->num_writes
];
331 blkreq
->sector
= sector
;
332 blkreq
->nb_sectors
= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
333 blkreq
->qiov
= &req
->qiov
;
334 blkreq
->cb
= virtio_blk_rw_complete
;
335 blkreq
->opaque
= req
;
341 static void virtio_blk_handle_read(VirtIOBlockReq
*req
)
343 BlockDriverAIOCB
*acb
;
346 sector
= ldq_p(&req
->out
->sector
);
348 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, req
->qiov
.size
, BDRV_ACCT_READ
);
350 if (sector
& req
->dev
->sector_mask
) {
351 virtio_blk_rw_complete(req
, -EIO
);
354 if (req
->qiov
.size
% req
->dev
->conf
->logical_block_size
) {
355 virtio_blk_rw_complete(req
, -EIO
);
359 acb
= bdrv_aio_readv(req
->dev
->bs
, sector
, &req
->qiov
,
360 req
->qiov
.size
/ BDRV_SECTOR_SIZE
,
361 virtio_blk_rw_complete
, req
);
363 virtio_blk_rw_complete(req
, -EIO
);
367 static void virtio_blk_handle_request(VirtIOBlockReq
*req
,
372 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
373 error_report("virtio-blk missing headers");
377 if (req
->elem
.out_sg
[0].iov_len
< sizeof(*req
->out
) ||
378 req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_len
< sizeof(*req
->in
)) {
379 error_report("virtio-blk header not in correct element");
383 req
->out
= (void *)req
->elem
.out_sg
[0].iov_base
;
384 req
->in
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_base
;
386 type
= ldl_p(&req
->out
->type
);
388 if (type
& VIRTIO_BLK_T_FLUSH
) {
389 virtio_blk_handle_flush(req
, mrb
);
390 } else if (type
& VIRTIO_BLK_T_SCSI_CMD
) {
391 virtio_blk_handle_scsi(req
);
392 } else if (type
& VIRTIO_BLK_T_GET_ID
) {
393 VirtIOBlock
*s
= req
->dev
;
396 * NB: per existing s/n string convention the string is
397 * terminated by '\0' only when shorter than buffer.
399 strncpy(req
->elem
.in_sg
[0].iov_base
,
400 s
->serial
? s
->serial
: "",
401 MIN(req
->elem
.in_sg
[0].iov_len
, VIRTIO_BLK_ID_BYTES
));
402 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
404 } else if (type
& VIRTIO_BLK_T_OUT
) {
405 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.out_sg
[1],
406 req
->elem
.out_num
- 1);
407 virtio_blk_handle_write(req
, mrb
);
409 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.in_sg
[0],
410 req
->elem
.in_num
- 1);
411 virtio_blk_handle_read(req
);
415 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
417 VirtIOBlock
*s
= to_virtio_blk(vdev
);
419 MultiReqBuffer mrb
= {
423 while ((req
= virtio_blk_get_request(s
))) {
424 virtio_blk_handle_request(req
, &mrb
);
427 virtio_submit_multiwrite(s
->bs
, &mrb
);
430 * FIXME: Want to check for completions before returning to guest mode,
431 * so cached reads and writes are reported as quickly as possible. But
432 * that should be done in the generic block layer.
436 static void virtio_blk_dma_restart_bh(void *opaque
)
438 VirtIOBlock
*s
= opaque
;
439 VirtIOBlockReq
*req
= s
->rq
;
440 MultiReqBuffer mrb
= {
444 qemu_bh_delete(s
->bh
);
450 virtio_blk_handle_request(req
, &mrb
);
454 virtio_submit_multiwrite(s
->bs
, &mrb
);
457 static void virtio_blk_dma_restart_cb(void *opaque
, int running
,
460 VirtIOBlock
*s
= opaque
;
466 s
->bh
= qemu_bh_new(virtio_blk_dma_restart_bh
, s
);
467 qemu_bh_schedule(s
->bh
);
471 static void virtio_blk_reset(VirtIODevice
*vdev
)
474 * This should cancel pending requests, but can't do nicely until there
475 * are per-device request lists.
480 /* coalesce internal state, copy to pci i/o region 0
482 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
484 VirtIOBlock
*s
= to_virtio_blk(vdev
);
485 struct virtio_blk_config blkcfg
;
487 int cylinders
, heads
, secs
;
488 int blk_size
= s
->conf
->logical_block_size
;
490 bdrv_get_geometry(s
->bs
, &capacity
);
491 bdrv_get_geometry_hint(s
->bs
, &cylinders
, &heads
, &secs
);
492 memset(&blkcfg
, 0, sizeof(blkcfg
));
493 stq_raw(&blkcfg
.capacity
, capacity
);
494 stl_raw(&blkcfg
.seg_max
, 128 - 2);
495 stw_raw(&blkcfg
.cylinders
, cylinders
);
496 stl_raw(&blkcfg
.blk_size
, blk_size
);
497 stw_raw(&blkcfg
.min_io_size
, s
->conf
->min_io_size
/ blk_size
);
498 stw_raw(&blkcfg
.opt_io_size
, s
->conf
->opt_io_size
/ blk_size
);
499 blkcfg
.heads
= heads
;
500 blkcfg
.sectors
= secs
& ~s
->sector_mask
;
502 blkcfg
.physical_block_exp
= get_physical_block_exp(s
->conf
);
503 blkcfg
.alignment_offset
= 0;
504 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
507 static uint32_t virtio_blk_get_features(VirtIODevice
*vdev
, uint32_t features
)
509 VirtIOBlock
*s
= to_virtio_blk(vdev
);
511 features
|= (1 << VIRTIO_BLK_F_SEG_MAX
);
512 features
|= (1 << VIRTIO_BLK_F_GEOMETRY
);
513 features
|= (1 << VIRTIO_BLK_F_TOPOLOGY
);
514 features
|= (1 << VIRTIO_BLK_F_BLK_SIZE
);
516 if (bdrv_enable_write_cache(s
->bs
))
517 features
|= (1 << VIRTIO_BLK_F_WCACHE
);
519 if (bdrv_is_read_only(s
->bs
))
520 features
|= 1 << VIRTIO_BLK_F_RO
;
525 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
527 VirtIOBlock
*s
= opaque
;
528 VirtIOBlockReq
*req
= s
->rq
;
530 virtio_save(&s
->vdev
, f
);
533 qemu_put_sbyte(f
, 1);
534 qemu_put_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
537 qemu_put_sbyte(f
, 0);
540 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
542 VirtIOBlock
*s
= opaque
;
547 virtio_load(&s
->vdev
, f
);
548 while (qemu_get_sbyte(f
)) {
549 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
550 qemu_get_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
554 virtqueue_map_sg(req
->elem
.in_sg
, req
->elem
.in_addr
,
555 req
->elem
.in_num
, 1);
556 virtqueue_map_sg(req
->elem
.out_sg
, req
->elem
.out_addr
,
557 req
->elem
.out_num
, 0);
563 static void virtio_blk_resize(void *opaque
)
565 VirtIOBlock
*s
= opaque
;
567 virtio_notify_config(&s
->vdev
);
570 static const BlockDevOps virtio_block_ops
= {
571 .resize_cb
= virtio_blk_resize
,
574 VirtIODevice
*virtio_blk_init(DeviceState
*dev
, BlockConf
*conf
,
578 int cylinders
, heads
, secs
;
579 static int virtio_blk_id
;
583 error_report("virtio-blk-pci: drive property not set");
586 if (!bdrv_is_inserted(conf
->bs
)) {
587 error_report("Device needs media, but drive is empty");
592 /* try to fall back to value set with legacy -drive serial=... */
593 dinfo
= drive_get_by_blockdev(conf
->bs
);
594 if (*dinfo
->serial
) {
595 *serial
= strdup(dinfo
->serial
);
599 s
= (VirtIOBlock
*)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK
,
600 sizeof(struct virtio_blk_config
),
601 sizeof(VirtIOBlock
));
603 s
->vdev
.get_config
= virtio_blk_update_config
;
604 s
->vdev
.get_features
= virtio_blk_get_features
;
605 s
->vdev
.reset
= virtio_blk_reset
;
610 s
->sector_mask
= (s
->conf
->logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
611 bdrv_guess_geometry(s
->bs
, &cylinders
, &heads
, &secs
);
613 s
->vq
= virtio_add_queue(&s
->vdev
, 128, virtio_blk_handle_output
);
615 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
617 register_savevm(dev
, "virtio-blk", virtio_blk_id
++, 2,
618 virtio_blk_save
, virtio_blk_load
, s
);
619 bdrv_set_dev_ops(s
->bs
, &virtio_block_ops
, s
);
620 bdrv_set_buffer_alignment(s
->bs
, conf
->logical_block_size
);
622 bdrv_iostatus_enable(s
->bs
);
623 add_boot_device_path(conf
->bootindex
, dev
, "/disk@0,0");
628 void virtio_blk_exit(VirtIODevice
*vdev
)
630 VirtIOBlock
*s
= to_virtio_blk(vdev
);
631 unregister_savevm(s
->qdev
, "virtio-blk", s
);
632 virtio_cleanup(vdev
);