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 "virtio-blk.h"
20 typedef struct VirtIOBlock
28 unsigned short sector_mask
;
31 static VirtIOBlock
*to_virtio_blk(VirtIODevice
*vdev
)
33 return (VirtIOBlock
*)vdev
;
36 typedef struct VirtIOBlockReq
39 VirtQueueElement elem
;
40 struct virtio_blk_inhdr
*in
;
41 struct virtio_blk_outhdr
*out
;
42 struct virtio_scsi_inhdr
*scsi
;
44 struct VirtIOBlockReq
*next
;
47 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, int status
)
49 VirtIOBlock
*s
= req
->dev
;
51 req
->in
->status
= status
;
52 virtqueue_push(s
->vq
, &req
->elem
, req
->qiov
.size
+ sizeof(*req
->in
));
53 virtio_notify(&s
->vdev
, s
->vq
);
58 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
61 BlockInterfaceErrorAction action
=
62 drive_get_on_error(req
->dev
->bs
, is_read
);
63 VirtIOBlock
*s
= req
->dev
;
65 if (action
== BLOCK_ERR_IGNORE
) {
66 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
70 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
71 || action
== BLOCK_ERR_STOP_ANY
) {
74 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
77 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
78 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
84 static void virtio_blk_rw_complete(void *opaque
, int ret
)
86 VirtIOBlockReq
*req
= opaque
;
89 int is_read
= !(req
->out
->type
& VIRTIO_BLK_T_OUT
);
90 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
))
94 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
97 static void virtio_blk_flush_complete(void *opaque
, int ret
)
99 VirtIOBlockReq
*req
= opaque
;
101 virtio_blk_req_complete(req
, ret
? VIRTIO_BLK_S_IOERR
: VIRTIO_BLK_S_OK
);
104 static VirtIOBlockReq
*virtio_blk_alloc_request(VirtIOBlock
*s
)
106 VirtIOBlockReq
*req
= qemu_malloc(sizeof(*req
));
113 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
115 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
118 if (!virtqueue_pop(s
->vq
, &req
->elem
)) {
128 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
130 struct sg_io_hdr hdr
;
136 * We require at least one output segment each for the virtio_blk_outhdr
137 * and the SCSI command block.
139 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
140 * and the sense buffer pointer in the input segments.
142 if (req
->elem
.out_num
< 2 || req
->elem
.in_num
< 3) {
143 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
148 * No support for bidirection commands yet.
150 if (req
->elem
.out_num
> 2 && req
->elem
.in_num
> 3) {
151 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
156 * The scsi inhdr is placed in the second-to-last input segment, just
157 * before the regular inhdr.
159 req
->scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
161 memset(&hdr
, 0, sizeof(struct sg_io_hdr
));
162 hdr
.interface_id
= 'S';
163 hdr
.cmd_len
= req
->elem
.out_sg
[1].iov_len
;
164 hdr
.cmdp
= req
->elem
.out_sg
[1].iov_base
;
167 if (req
->elem
.out_num
> 2) {
169 * If there are more than the minimally required 2 output segments
170 * there is write payload starting from the third iovec.
172 hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
173 hdr
.iovec_count
= req
->elem
.out_num
- 2;
175 for (i
= 0; i
< hdr
.iovec_count
; i
++)
176 hdr
.dxfer_len
+= req
->elem
.out_sg
[i
+ 2].iov_len
;
178 hdr
.dxferp
= req
->elem
.out_sg
+ 2;
180 } else if (req
->elem
.in_num
> 3) {
182 * If we have more than 3 input segments the guest wants to actually
185 hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
186 hdr
.iovec_count
= req
->elem
.in_num
- 3;
187 for (i
= 0; i
< hdr
.iovec_count
; i
++)
188 hdr
.dxfer_len
+= req
->elem
.in_sg
[i
].iov_len
;
190 hdr
.dxferp
= req
->elem
.in_sg
;
193 * Some SCSI commands don't actually transfer any data.
195 hdr
.dxfer_direction
= SG_DXFER_NONE
;
198 hdr
.sbp
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_base
;
199 hdr
.mx_sb_len
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_len
;
201 ret
= bdrv_ioctl(req
->dev
->bs
, SG_IO
, &hdr
);
203 status
= VIRTIO_BLK_S_UNSUPP
;
205 hdr
.resid
= hdr
.dxfer_len
;
206 } else if (hdr
.status
) {
207 status
= VIRTIO_BLK_S_IOERR
;
209 status
= VIRTIO_BLK_S_OK
;
212 req
->scsi
->errors
= hdr
.status
;
213 req
->scsi
->residual
= hdr
.resid
;
214 req
->scsi
->sense_len
= hdr
.sb_len_wr
;
215 req
->scsi
->data_len
= hdr
.dxfer_len
;
217 virtio_blk_req_complete(req
, status
);
220 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
222 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
224 #endif /* __linux__ */
226 typedef struct MultiReqBuffer
{
227 BlockRequest blkreq
[32];
228 unsigned int num_writes
;
231 static void virtio_submit_multiwrite(BlockDriverState
*bs
, MultiReqBuffer
*mrb
)
235 if (!mrb
->num_writes
) {
239 ret
= bdrv_aio_multiwrite(bs
, mrb
->blkreq
, mrb
->num_writes
);
241 for (i
= 0; i
< mrb
->num_writes
; i
++) {
242 if (mrb
->blkreq
[i
].error
) {
243 virtio_blk_rw_complete(mrb
->blkreq
[i
].opaque
, -EIO
);
251 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
253 BlockDriverAIOCB
*acb
;
256 * Make sure all outstanding writes are posted to the backing device.
258 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
260 acb
= bdrv_aio_flush(req
->dev
->bs
, virtio_blk_flush_complete
, req
);
262 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
266 static void virtio_blk_handle_write(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
268 BlockRequest
*blkreq
;
270 if (req
->out
->sector
& req
->dev
->sector_mask
) {
271 virtio_blk_rw_complete(req
, -EIO
);
275 if (mrb
->num_writes
== 32) {
276 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
279 blkreq
= &mrb
->blkreq
[mrb
->num_writes
];
280 blkreq
->sector
= req
->out
->sector
;
281 blkreq
->nb_sectors
= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
282 blkreq
->qiov
= &req
->qiov
;
283 blkreq
->cb
= virtio_blk_rw_complete
;
284 blkreq
->opaque
= req
;
290 static void virtio_blk_handle_read(VirtIOBlockReq
*req
)
292 BlockDriverAIOCB
*acb
;
294 if (req
->out
->sector
& req
->dev
->sector_mask
) {
295 virtio_blk_rw_complete(req
, -EIO
);
299 acb
= bdrv_aio_readv(req
->dev
->bs
, req
->out
->sector
, &req
->qiov
,
300 req
->qiov
.size
/ BDRV_SECTOR_SIZE
,
301 virtio_blk_rw_complete
, req
);
303 virtio_blk_rw_complete(req
, -EIO
);
307 static void virtio_blk_handle_request(VirtIOBlockReq
*req
,
310 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
311 fprintf(stderr
, "virtio-blk missing headers\n");
315 if (req
->elem
.out_sg
[0].iov_len
< sizeof(*req
->out
) ||
316 req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_len
< sizeof(*req
->in
)) {
317 fprintf(stderr
, "virtio-blk header not in correct element\n");
321 req
->out
= (void *)req
->elem
.out_sg
[0].iov_base
;
322 req
->in
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_base
;
324 if (req
->out
->type
& VIRTIO_BLK_T_FLUSH
) {
325 virtio_blk_handle_flush(req
, mrb
);
326 } else if (req
->out
->type
& VIRTIO_BLK_T_SCSI_CMD
) {
327 virtio_blk_handle_scsi(req
);
328 } else if (req
->out
->type
& VIRTIO_BLK_T_OUT
) {
329 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.out_sg
[1],
330 req
->elem
.out_num
- 1);
331 virtio_blk_handle_write(req
, mrb
);
333 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.in_sg
[0],
334 req
->elem
.in_num
- 1);
335 virtio_blk_handle_read(req
);
339 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
341 VirtIOBlock
*s
= to_virtio_blk(vdev
);
343 MultiReqBuffer mrb
= {
347 while ((req
= virtio_blk_get_request(s
))) {
348 virtio_blk_handle_request(req
, &mrb
);
351 virtio_submit_multiwrite(s
->bs
, &mrb
);
354 * FIXME: Want to check for completions before returning to guest mode,
355 * so cached reads and writes are reported as quickly as possible. But
356 * that should be done in the generic block layer.
360 static void virtio_blk_dma_restart_bh(void *opaque
)
362 VirtIOBlock
*s
= opaque
;
363 VirtIOBlockReq
*req
= s
->rq
;
364 MultiReqBuffer mrb
= {
368 qemu_bh_delete(s
->bh
);
374 virtio_blk_handle_request(req
, &mrb
);
378 virtio_submit_multiwrite(s
->bs
, &mrb
);
381 static void virtio_blk_dma_restart_cb(void *opaque
, int running
, int reason
)
383 VirtIOBlock
*s
= opaque
;
389 s
->bh
= qemu_bh_new(virtio_blk_dma_restart_bh
, s
);
390 qemu_bh_schedule(s
->bh
);
394 static void virtio_blk_reset(VirtIODevice
*vdev
)
397 * This should cancel pending requests, but can't do nicely until there
398 * are per-device request lists.
403 /* coalesce internal state, copy to pci i/o region 0
405 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
407 VirtIOBlock
*s
= to_virtio_blk(vdev
);
408 struct virtio_blk_config blkcfg
;
410 int cylinders
, heads
, secs
;
412 bdrv_get_geometry(s
->bs
, &capacity
);
413 bdrv_get_geometry_hint(s
->bs
, &cylinders
, &heads
, &secs
);
414 memset(&blkcfg
, 0, sizeof(blkcfg
));
415 stq_raw(&blkcfg
.capacity
, capacity
);
416 stl_raw(&blkcfg
.seg_max
, 128 - 2);
417 stw_raw(&blkcfg
.cylinders
, cylinders
);
418 blkcfg
.heads
= heads
;
419 blkcfg
.sectors
= secs
& ~s
->sector_mask
;
420 blkcfg
.blk_size
= s
->conf
->logical_block_size
;
422 blkcfg
.physical_block_exp
= get_physical_block_exp(s
->conf
);
423 blkcfg
.alignment_offset
= 0;
424 blkcfg
.min_io_size
= s
->conf
->min_io_size
/ blkcfg
.blk_size
;
425 blkcfg
.opt_io_size
= s
->conf
->opt_io_size
/ blkcfg
.blk_size
;
426 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
429 static uint32_t virtio_blk_get_features(VirtIODevice
*vdev
, uint32_t features
)
431 VirtIOBlock
*s
= to_virtio_blk(vdev
);
433 features
|= (1 << VIRTIO_BLK_F_SEG_MAX
);
434 features
|= (1 << VIRTIO_BLK_F_GEOMETRY
);
435 features
|= (1 << VIRTIO_BLK_F_TOPOLOGY
);
436 features
|= (1 << VIRTIO_BLK_F_BLK_SIZE
);
438 if (bdrv_enable_write_cache(s
->bs
))
439 features
|= (1 << VIRTIO_BLK_F_WCACHE
);
441 if (bdrv_is_read_only(s
->bs
))
442 features
|= 1 << VIRTIO_BLK_F_RO
;
447 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
449 VirtIOBlock
*s
= opaque
;
450 VirtIOBlockReq
*req
= s
->rq
;
452 virtio_save(&s
->vdev
, f
);
455 qemu_put_sbyte(f
, 1);
456 qemu_put_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
459 qemu_put_sbyte(f
, 0);
462 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
464 VirtIOBlock
*s
= opaque
;
469 virtio_load(&s
->vdev
, f
);
470 while (qemu_get_sbyte(f
)) {
471 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
472 qemu_get_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
480 VirtIODevice
*virtio_blk_init(DeviceState
*dev
, BlockConf
*conf
)
483 int cylinders
, heads
, secs
;
484 static int virtio_blk_id
;
486 s
= (VirtIOBlock
*)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK
,
487 sizeof(struct virtio_blk_config
),
488 sizeof(VirtIOBlock
));
490 s
->vdev
.get_config
= virtio_blk_update_config
;
491 s
->vdev
.get_features
= virtio_blk_get_features
;
492 s
->vdev
.reset
= virtio_blk_reset
;
493 s
->bs
= conf
->dinfo
->bdrv
;
496 s
->sector_mask
= (s
->conf
->logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
497 bdrv_guess_geometry(s
->bs
, &cylinders
, &heads
, &secs
);
499 s
->vq
= virtio_add_queue(&s
->vdev
, 128, virtio_blk_handle_output
);
501 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
502 register_savevm("virtio-blk", virtio_blk_id
++, 2,
503 virtio_blk_save
, virtio_blk_load
, s
);