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"
23 typedef struct VirtIOBlock
31 unsigned short sector_mask
;
32 char sn
[BLOCK_SERIAL_STRLEN
];
36 static VirtIOBlock
*to_virtio_blk(VirtIODevice
*vdev
)
38 return (VirtIOBlock
*)vdev
;
41 typedef struct VirtIOBlockReq
44 VirtQueueElement elem
;
45 struct virtio_blk_inhdr
*in
;
46 struct virtio_blk_outhdr
*out
;
47 struct virtio_scsi_inhdr
*scsi
;
49 struct VirtIOBlockReq
*next
;
52 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, int status
)
54 VirtIOBlock
*s
= req
->dev
;
56 trace_virtio_blk_req_complete(req
, status
);
58 stb_p(&req
->in
->status
, status
);
59 virtqueue_push(s
->vq
, &req
->elem
, req
->qiov
.size
+ sizeof(*req
->in
));
60 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(VMSTOP_DISKFULL
);
83 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
84 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
90 static void virtio_blk_rw_complete(void *opaque
, int ret
)
92 VirtIOBlockReq
*req
= opaque
;
94 trace_virtio_blk_rw_complete(req
, ret
);
97 int is_read
= !(ldl_p(&req
->out
->type
) & VIRTIO_BLK_T_OUT
);
98 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
))
102 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
105 static void virtio_blk_flush_complete(void *opaque
, int ret
)
107 VirtIOBlockReq
*req
= opaque
;
110 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
115 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
118 static VirtIOBlockReq
*virtio_blk_alloc_request(VirtIOBlock
*s
)
120 VirtIOBlockReq
*req
= qemu_malloc(sizeof(*req
));
127 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
129 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
132 if (!virtqueue_pop(s
->vq
, &req
->elem
)) {
142 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
144 struct sg_io_hdr hdr
;
150 * We require at least one output segment each for the virtio_blk_outhdr
151 * and the SCSI command block.
153 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
154 * and the sense buffer pointer in the input segments.
156 if (req
->elem
.out_num
< 2 || req
->elem
.in_num
< 3) {
157 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
162 * No support for bidirection commands yet.
164 if (req
->elem
.out_num
> 2 && req
->elem
.in_num
> 3) {
165 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
170 * The scsi inhdr is placed in the second-to-last input segment, just
171 * before the regular inhdr.
173 req
->scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
175 memset(&hdr
, 0, sizeof(struct sg_io_hdr
));
176 hdr
.interface_id
= 'S';
177 hdr
.cmd_len
= req
->elem
.out_sg
[1].iov_len
;
178 hdr
.cmdp
= req
->elem
.out_sg
[1].iov_base
;
181 if (req
->elem
.out_num
> 2) {
183 * If there are more than the minimally required 2 output segments
184 * there is write payload starting from the third iovec.
186 hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
187 hdr
.iovec_count
= req
->elem
.out_num
- 2;
189 for (i
= 0; i
< hdr
.iovec_count
; i
++)
190 hdr
.dxfer_len
+= req
->elem
.out_sg
[i
+ 2].iov_len
;
192 hdr
.dxferp
= req
->elem
.out_sg
+ 2;
194 } else if (req
->elem
.in_num
> 3) {
196 * If we have more than 3 input segments the guest wants to actually
199 hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
200 hdr
.iovec_count
= req
->elem
.in_num
- 3;
201 for (i
= 0; i
< hdr
.iovec_count
; i
++)
202 hdr
.dxfer_len
+= req
->elem
.in_sg
[i
].iov_len
;
204 hdr
.dxferp
= req
->elem
.in_sg
;
207 * Some SCSI commands don't actually transfer any data.
209 hdr
.dxfer_direction
= SG_DXFER_NONE
;
212 hdr
.sbp
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_base
;
213 hdr
.mx_sb_len
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_len
;
215 ret
= bdrv_ioctl(req
->dev
->bs
, SG_IO
, &hdr
);
217 status
= VIRTIO_BLK_S_UNSUPP
;
219 hdr
.resid
= hdr
.dxfer_len
;
220 } else if (hdr
.status
) {
221 status
= VIRTIO_BLK_S_IOERR
;
223 status
= VIRTIO_BLK_S_OK
;
226 stl_p(&req
->scsi
->errors
, hdr
.status
);
227 stl_p(&req
->scsi
->residual
, hdr
.resid
);
228 stl_p(&req
->scsi
->sense_len
, hdr
.sb_len_wr
);
229 stl_p(&req
->scsi
->data_len
, hdr
.dxfer_len
);
231 virtio_blk_req_complete(req
, status
);
234 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
236 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
238 #endif /* __linux__ */
240 typedef struct MultiReqBuffer
{
241 BlockRequest blkreq
[32];
242 unsigned int num_writes
;
245 static void virtio_submit_multiwrite(BlockDriverState
*bs
, MultiReqBuffer
*mrb
)
249 if (!mrb
->num_writes
) {
253 ret
= bdrv_aio_multiwrite(bs
, mrb
->blkreq
, mrb
->num_writes
);
255 for (i
= 0; i
< mrb
->num_writes
; i
++) {
256 if (mrb
->blkreq
[i
].error
) {
257 virtio_blk_rw_complete(mrb
->blkreq
[i
].opaque
, -EIO
);
265 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
267 BlockDriverAIOCB
*acb
;
270 * Make sure all outstanding writes are posted to the backing device.
272 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
274 acb
= bdrv_aio_flush(req
->dev
->bs
, virtio_blk_flush_complete
, req
);
276 virtio_blk_flush_complete(req
, -EIO
);
280 static void virtio_blk_handle_write(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
282 BlockRequest
*blkreq
;
285 sector
= ldq_p(&req
->out
->sector
);
287 trace_virtio_blk_handle_write(req
, sector
, req
->qiov
.size
/ 512);
289 if (sector
& req
->dev
->sector_mask
) {
290 virtio_blk_rw_complete(req
, -EIO
);
294 if (mrb
->num_writes
== 32) {
295 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
298 blkreq
= &mrb
->blkreq
[mrb
->num_writes
];
299 blkreq
->sector
= sector
;
300 blkreq
->nb_sectors
= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
301 blkreq
->qiov
= &req
->qiov
;
302 blkreq
->cb
= virtio_blk_rw_complete
;
303 blkreq
->opaque
= req
;
309 static void virtio_blk_handle_read(VirtIOBlockReq
*req
)
311 BlockDriverAIOCB
*acb
;
314 sector
= ldq_p(&req
->out
->sector
);
316 if (sector
& req
->dev
->sector_mask
) {
317 virtio_blk_rw_complete(req
, -EIO
);
321 acb
= bdrv_aio_readv(req
->dev
->bs
, sector
, &req
->qiov
,
322 req
->qiov
.size
/ BDRV_SECTOR_SIZE
,
323 virtio_blk_rw_complete
, req
);
325 virtio_blk_rw_complete(req
, -EIO
);
329 static void virtio_blk_handle_request(VirtIOBlockReq
*req
,
334 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
335 error_report("virtio-blk missing headers");
339 if (req
->elem
.out_sg
[0].iov_len
< sizeof(*req
->out
) ||
340 req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_len
< sizeof(*req
->in
)) {
341 error_report("virtio-blk header not in correct element");
345 req
->out
= (void *)req
->elem
.out_sg
[0].iov_base
;
346 req
->in
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_base
;
348 type
= ldl_p(&req
->out
->type
);
350 if (type
& VIRTIO_BLK_T_FLUSH
) {
351 virtio_blk_handle_flush(req
, mrb
);
352 } else if (type
& VIRTIO_BLK_T_SCSI_CMD
) {
353 virtio_blk_handle_scsi(req
);
354 } else if (type
& VIRTIO_BLK_T_GET_ID
) {
355 VirtIOBlock
*s
= req
->dev
;
357 memcpy(req
->elem
.in_sg
[0].iov_base
, s
->sn
,
358 MIN(req
->elem
.in_sg
[0].iov_len
, sizeof(s
->sn
)));
359 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
360 } else if (type
& VIRTIO_BLK_T_OUT
) {
361 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.out_sg
[1],
362 req
->elem
.out_num
- 1);
363 virtio_blk_handle_write(req
, mrb
);
365 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.in_sg
[0],
366 req
->elem
.in_num
- 1);
367 virtio_blk_handle_read(req
);
371 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
373 VirtIOBlock
*s
= to_virtio_blk(vdev
);
375 MultiReqBuffer mrb
= {
379 while ((req
= virtio_blk_get_request(s
))) {
380 virtio_blk_handle_request(req
, &mrb
);
383 virtio_submit_multiwrite(s
->bs
, &mrb
);
386 * FIXME: Want to check for completions before returning to guest mode,
387 * so cached reads and writes are reported as quickly as possible. But
388 * that should be done in the generic block layer.
392 static void virtio_blk_dma_restart_bh(void *opaque
)
394 VirtIOBlock
*s
= opaque
;
395 VirtIOBlockReq
*req
= s
->rq
;
396 MultiReqBuffer mrb
= {
400 qemu_bh_delete(s
->bh
);
406 virtio_blk_handle_request(req
, &mrb
);
410 virtio_submit_multiwrite(s
->bs
, &mrb
);
413 static void virtio_blk_dma_restart_cb(void *opaque
, int running
, int reason
)
415 VirtIOBlock
*s
= opaque
;
421 s
->bh
= qemu_bh_new(virtio_blk_dma_restart_bh
, s
);
422 qemu_bh_schedule(s
->bh
);
426 static void virtio_blk_reset(VirtIODevice
*vdev
)
429 * This should cancel pending requests, but can't do nicely until there
430 * are per-device request lists.
435 /* coalesce internal state, copy to pci i/o region 0
437 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
439 VirtIOBlock
*s
= to_virtio_blk(vdev
);
440 struct virtio_blk_config blkcfg
;
442 int cylinders
, heads
, secs
;
444 bdrv_get_geometry(s
->bs
, &capacity
);
445 bdrv_get_geometry_hint(s
->bs
, &cylinders
, &heads
, &secs
);
446 memset(&blkcfg
, 0, sizeof(blkcfg
));
447 stq_raw(&blkcfg
.capacity
, capacity
);
448 stl_raw(&blkcfg
.seg_max
, 128 - 2);
449 stw_raw(&blkcfg
.cylinders
, cylinders
);
450 blkcfg
.heads
= heads
;
451 blkcfg
.sectors
= secs
& ~s
->sector_mask
;
452 blkcfg
.blk_size
= s
->conf
->logical_block_size
;
454 blkcfg
.physical_block_exp
= get_physical_block_exp(s
->conf
);
455 blkcfg
.alignment_offset
= 0;
456 blkcfg
.min_io_size
= s
->conf
->min_io_size
/ blkcfg
.blk_size
;
457 blkcfg
.opt_io_size
= s
->conf
->opt_io_size
/ blkcfg
.blk_size
;
458 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
461 static uint32_t virtio_blk_get_features(VirtIODevice
*vdev
, uint32_t features
)
463 VirtIOBlock
*s
= to_virtio_blk(vdev
);
465 features
|= (1 << VIRTIO_BLK_F_SEG_MAX
);
466 features
|= (1 << VIRTIO_BLK_F_GEOMETRY
);
467 features
|= (1 << VIRTIO_BLK_F_TOPOLOGY
);
468 features
|= (1 << VIRTIO_BLK_F_BLK_SIZE
);
470 if (bdrv_enable_write_cache(s
->bs
))
471 features
|= (1 << VIRTIO_BLK_F_WCACHE
);
473 if (bdrv_is_read_only(s
->bs
))
474 features
|= 1 << VIRTIO_BLK_F_RO
;
479 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
481 VirtIOBlock
*s
= opaque
;
482 VirtIOBlockReq
*req
= s
->rq
;
484 virtio_save(&s
->vdev
, f
);
487 qemu_put_sbyte(f
, 1);
488 qemu_put_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
491 qemu_put_sbyte(f
, 0);
494 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
496 VirtIOBlock
*s
= opaque
;
501 virtio_load(&s
->vdev
, f
);
502 while (qemu_get_sbyte(f
)) {
503 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
504 qemu_get_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
508 virtqueue_map_sg(req
->elem
.in_sg
, req
->elem
.in_addr
,
509 req
->elem
.in_num
, 1);
510 virtqueue_map_sg(req
->elem
.out_sg
, req
->elem
.out_addr
,
511 req
->elem
.out_num
, 0);
517 static void virtio_blk_change_cb(void *opaque
, int reason
)
519 VirtIOBlock
*s
= opaque
;
521 if (reason
& CHANGE_SIZE
) {
522 virtio_notify_config(&s
->vdev
);
526 VirtIODevice
*virtio_blk_init(DeviceState
*dev
, BlockConf
*conf
)
529 int cylinders
, heads
, secs
;
530 static int virtio_blk_id
;
534 error_report("virtio-blk-pci: drive property not set");
537 if (!bdrv_is_inserted(conf
->bs
)) {
538 error_report("Device needs media, but drive is empty");
542 s
= (VirtIOBlock
*)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK
,
543 sizeof(struct virtio_blk_config
),
544 sizeof(VirtIOBlock
));
546 s
->vdev
.get_config
= virtio_blk_update_config
;
547 s
->vdev
.get_features
= virtio_blk_get_features
;
548 s
->vdev
.reset
= virtio_blk_reset
;
552 s
->sector_mask
= (s
->conf
->logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
553 bdrv_guess_geometry(s
->bs
, &cylinders
, &heads
, &secs
);
555 /* NB: per existing s/n string convention the string is terminated
556 * by '\0' only when less than sizeof (s->sn)
558 dinfo
= drive_get_by_blockdev(s
->bs
);
559 strncpy(s
->sn
, dinfo
->serial
, sizeof (s
->sn
));
561 s
->vq
= virtio_add_queue(&s
->vdev
, 128, virtio_blk_handle_output
);
563 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
565 register_savevm(dev
, "virtio-blk", virtio_blk_id
++, 2,
566 virtio_blk_save
, virtio_blk_load
, s
);
567 bdrv_set_removable(s
->bs
, 0);
568 bdrv_set_change_cb(s
->bs
, virtio_blk_change_cb
, s
);
569 s
->bs
->buffer_alignment
= conf
->logical_block_size
;
571 add_boot_device_path(conf
->bootindex
, dev
, "/disk@0,0");
576 void virtio_blk_exit(VirtIODevice
*vdev
)
578 VirtIOBlock
*s
= to_virtio_blk(vdev
);
579 unregister_savevm(s
->qdev
, "virtio-blk", s
);