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
32 unsigned short sector_mask
;
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
;
53 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, int status
)
55 VirtIOBlock
*s
= req
->dev
;
57 trace_virtio_blk_req_complete(req
, status
);
59 stb_p(&req
->in
->status
, status
);
60 virtqueue_push(s
->vq
, &req
->elem
, req
->qiov
.size
+ sizeof(*req
->in
));
61 virtio_notify(&s
->vdev
, s
->vq
);
64 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
67 BlockErrorAction action
= bdrv_get_on_error(req
->dev
->bs
, is_read
);
68 VirtIOBlock
*s
= req
->dev
;
70 if (action
== BLOCK_ERR_IGNORE
) {
71 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
75 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
76 || action
== BLOCK_ERR_STOP_ANY
) {
79 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
80 vm_stop(VMSTOP_DISKFULL
);
82 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
83 bdrv_acct_done(s
->bs
, &req
->acct
);
85 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
91 static void virtio_blk_rw_complete(void *opaque
, int ret
)
93 VirtIOBlockReq
*req
= opaque
;
95 trace_virtio_blk_rw_complete(req
, ret
);
98 int is_read
= !(ldl_p(&req
->out
->type
) & VIRTIO_BLK_T_OUT
);
99 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
))
103 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
104 bdrv_acct_done(req
->dev
->bs
, &req
->acct
);
108 static void virtio_blk_flush_complete(void *opaque
, int ret
)
110 VirtIOBlockReq
*req
= opaque
;
113 if (virtio_blk_handle_rw_error(req
, -ret
, 0)) {
118 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
119 bdrv_acct_done(req
->dev
->bs
, &req
->acct
);
123 static VirtIOBlockReq
*virtio_blk_alloc_request(VirtIOBlock
*s
)
125 VirtIOBlockReq
*req
= g_malloc(sizeof(*req
));
132 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
134 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
137 if (!virtqueue_pop(s
->vq
, &req
->elem
)) {
147 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
149 struct sg_io_hdr hdr
;
155 * We require at least one output segment each for the virtio_blk_outhdr
156 * and the SCSI command block.
158 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
159 * and the sense buffer pointer in the input segments.
161 if (req
->elem
.out_num
< 2 || req
->elem
.in_num
< 3) {
162 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
168 * No support for bidirection commands yet.
170 if (req
->elem
.out_num
> 2 && req
->elem
.in_num
> 3) {
171 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
177 * The scsi inhdr is placed in the second-to-last input segment, just
178 * before the regular inhdr.
180 req
->scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
182 memset(&hdr
, 0, sizeof(struct sg_io_hdr
));
183 hdr
.interface_id
= 'S';
184 hdr
.cmd_len
= req
->elem
.out_sg
[1].iov_len
;
185 hdr
.cmdp
= req
->elem
.out_sg
[1].iov_base
;
188 if (req
->elem
.out_num
> 2) {
190 * If there are more than the minimally required 2 output segments
191 * there is write payload starting from the third iovec.
193 hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
194 hdr
.iovec_count
= req
->elem
.out_num
- 2;
196 for (i
= 0; i
< hdr
.iovec_count
; i
++)
197 hdr
.dxfer_len
+= req
->elem
.out_sg
[i
+ 2].iov_len
;
199 hdr
.dxferp
= req
->elem
.out_sg
+ 2;
201 } else if (req
->elem
.in_num
> 3) {
203 * If we have more than 3 input segments the guest wants to actually
206 hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
207 hdr
.iovec_count
= req
->elem
.in_num
- 3;
208 for (i
= 0; i
< hdr
.iovec_count
; i
++)
209 hdr
.dxfer_len
+= req
->elem
.in_sg
[i
].iov_len
;
211 hdr
.dxferp
= req
->elem
.in_sg
;
214 * Some SCSI commands don't actually transfer any data.
216 hdr
.dxfer_direction
= SG_DXFER_NONE
;
219 hdr
.sbp
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_base
;
220 hdr
.mx_sb_len
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_len
;
222 ret
= bdrv_ioctl(req
->dev
->bs
, SG_IO
, &hdr
);
224 status
= VIRTIO_BLK_S_UNSUPP
;
226 hdr
.resid
= hdr
.dxfer_len
;
227 } else if (hdr
.status
) {
228 status
= VIRTIO_BLK_S_IOERR
;
230 status
= VIRTIO_BLK_S_OK
;
233 stl_p(&req
->scsi
->errors
, hdr
.status
);
234 stl_p(&req
->scsi
->residual
, hdr
.resid
);
235 stl_p(&req
->scsi
->sense_len
, hdr
.sb_len_wr
);
236 stl_p(&req
->scsi
->data_len
, hdr
.dxfer_len
);
238 virtio_blk_req_complete(req
, status
);
242 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
244 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
247 #endif /* __linux__ */
249 typedef struct MultiReqBuffer
{
250 BlockRequest blkreq
[32];
251 unsigned int num_writes
;
254 static void virtio_submit_multiwrite(BlockDriverState
*bs
, MultiReqBuffer
*mrb
)
258 if (!mrb
->num_writes
) {
262 ret
= bdrv_aio_multiwrite(bs
, mrb
->blkreq
, mrb
->num_writes
);
264 for (i
= 0; i
< mrb
->num_writes
; i
++) {
265 if (mrb
->blkreq
[i
].error
) {
266 virtio_blk_rw_complete(mrb
->blkreq
[i
].opaque
, -EIO
);
274 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
276 BlockDriverAIOCB
*acb
;
278 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, 0, BDRV_ACCT_FLUSH
);
281 * Make sure all outstanding writes are posted to the backing device.
283 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
285 acb
= bdrv_aio_flush(req
->dev
->bs
, virtio_blk_flush_complete
, req
);
287 virtio_blk_flush_complete(req
, -EIO
);
291 static void virtio_blk_handle_write(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
293 BlockRequest
*blkreq
;
296 sector
= ldq_p(&req
->out
->sector
);
298 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, req
->qiov
.size
, BDRV_ACCT_WRITE
);
300 trace_virtio_blk_handle_write(req
, sector
, req
->qiov
.size
/ 512);
302 if (sector
& req
->dev
->sector_mask
) {
303 virtio_blk_rw_complete(req
, -EIO
);
306 if (req
->qiov
.size
% req
->dev
->conf
->logical_block_size
) {
307 virtio_blk_rw_complete(req
, -EIO
);
311 if (mrb
->num_writes
== 32) {
312 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
315 blkreq
= &mrb
->blkreq
[mrb
->num_writes
];
316 blkreq
->sector
= sector
;
317 blkreq
->nb_sectors
= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
318 blkreq
->qiov
= &req
->qiov
;
319 blkreq
->cb
= virtio_blk_rw_complete
;
320 blkreq
->opaque
= req
;
326 static void virtio_blk_handle_read(VirtIOBlockReq
*req
)
328 BlockDriverAIOCB
*acb
;
331 sector
= ldq_p(&req
->out
->sector
);
333 bdrv_acct_start(req
->dev
->bs
, &req
->acct
, req
->qiov
.size
, BDRV_ACCT_READ
);
335 if (sector
& req
->dev
->sector_mask
) {
336 virtio_blk_rw_complete(req
, -EIO
);
339 if (req
->qiov
.size
% req
->dev
->conf
->logical_block_size
) {
340 virtio_blk_rw_complete(req
, -EIO
);
344 acb
= bdrv_aio_readv(req
->dev
->bs
, sector
, &req
->qiov
,
345 req
->qiov
.size
/ BDRV_SECTOR_SIZE
,
346 virtio_blk_rw_complete
, req
);
348 virtio_blk_rw_complete(req
, -EIO
);
352 static void virtio_blk_handle_request(VirtIOBlockReq
*req
,
357 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
358 error_report("virtio-blk missing headers");
362 if (req
->elem
.out_sg
[0].iov_len
< sizeof(*req
->out
) ||
363 req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_len
< sizeof(*req
->in
)) {
364 error_report("virtio-blk header not in correct element");
368 req
->out
= (void *)req
->elem
.out_sg
[0].iov_base
;
369 req
->in
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_base
;
371 type
= ldl_p(&req
->out
->type
);
373 if (type
& VIRTIO_BLK_T_FLUSH
) {
374 virtio_blk_handle_flush(req
, mrb
);
375 } else if (type
& VIRTIO_BLK_T_SCSI_CMD
) {
376 virtio_blk_handle_scsi(req
);
377 } else if (type
& VIRTIO_BLK_T_GET_ID
) {
378 VirtIOBlock
*s
= req
->dev
;
381 * NB: per existing s/n string convention the string is
382 * terminated by '\0' only when shorter than buffer.
384 strncpy(req
->elem
.in_sg
[0].iov_base
,
385 s
->serial
? s
->serial
: "",
386 MIN(req
->elem
.in_sg
[0].iov_len
, VIRTIO_BLK_ID_BYTES
));
387 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
389 } else if (type
& VIRTIO_BLK_T_OUT
) {
390 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.out_sg
[1],
391 req
->elem
.out_num
- 1);
392 virtio_blk_handle_write(req
, mrb
);
394 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.in_sg
[0],
395 req
->elem
.in_num
- 1);
396 virtio_blk_handle_read(req
);
400 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
402 VirtIOBlock
*s
= to_virtio_blk(vdev
);
404 MultiReqBuffer mrb
= {
408 while ((req
= virtio_blk_get_request(s
))) {
409 virtio_blk_handle_request(req
, &mrb
);
412 virtio_submit_multiwrite(s
->bs
, &mrb
);
415 * FIXME: Want to check for completions before returning to guest mode,
416 * so cached reads and writes are reported as quickly as possible. But
417 * that should be done in the generic block layer.
421 static void virtio_blk_dma_restart_bh(void *opaque
)
423 VirtIOBlock
*s
= opaque
;
424 VirtIOBlockReq
*req
= s
->rq
;
425 MultiReqBuffer mrb
= {
429 qemu_bh_delete(s
->bh
);
435 virtio_blk_handle_request(req
, &mrb
);
439 virtio_submit_multiwrite(s
->bs
, &mrb
);
442 static void virtio_blk_dma_restart_cb(void *opaque
, int running
, int reason
)
444 VirtIOBlock
*s
= opaque
;
450 s
->bh
= qemu_bh_new(virtio_blk_dma_restart_bh
, s
);
451 qemu_bh_schedule(s
->bh
);
455 static void virtio_blk_reset(VirtIODevice
*vdev
)
458 * This should cancel pending requests, but can't do nicely until there
459 * are per-device request lists.
464 /* coalesce internal state, copy to pci i/o region 0
466 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
468 VirtIOBlock
*s
= to_virtio_blk(vdev
);
469 struct virtio_blk_config blkcfg
;
471 int cylinders
, heads
, secs
;
473 bdrv_get_geometry(s
->bs
, &capacity
);
474 bdrv_get_geometry_hint(s
->bs
, &cylinders
, &heads
, &secs
);
475 memset(&blkcfg
, 0, sizeof(blkcfg
));
476 stq_raw(&blkcfg
.capacity
, capacity
);
477 stl_raw(&blkcfg
.seg_max
, 128 - 2);
478 stw_raw(&blkcfg
.cylinders
, cylinders
);
479 blkcfg
.heads
= heads
;
480 blkcfg
.sectors
= secs
& ~s
->sector_mask
;
481 blkcfg
.blk_size
= s
->conf
->logical_block_size
;
483 blkcfg
.physical_block_exp
= get_physical_block_exp(s
->conf
);
484 blkcfg
.alignment_offset
= 0;
485 blkcfg
.min_io_size
= s
->conf
->min_io_size
/ blkcfg
.blk_size
;
486 blkcfg
.opt_io_size
= s
->conf
->opt_io_size
/ blkcfg
.blk_size
;
487 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
490 static uint32_t virtio_blk_get_features(VirtIODevice
*vdev
, uint32_t features
)
492 VirtIOBlock
*s
= to_virtio_blk(vdev
);
494 features
|= (1 << VIRTIO_BLK_F_SEG_MAX
);
495 features
|= (1 << VIRTIO_BLK_F_GEOMETRY
);
496 features
|= (1 << VIRTIO_BLK_F_TOPOLOGY
);
497 features
|= (1 << VIRTIO_BLK_F_BLK_SIZE
);
499 if (bdrv_enable_write_cache(s
->bs
))
500 features
|= (1 << VIRTIO_BLK_F_WCACHE
);
502 if (bdrv_is_read_only(s
->bs
))
503 features
|= 1 << VIRTIO_BLK_F_RO
;
508 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
510 VirtIOBlock
*s
= opaque
;
511 VirtIOBlockReq
*req
= s
->rq
;
513 virtio_save(&s
->vdev
, f
);
516 qemu_put_sbyte(f
, 1);
517 qemu_put_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
520 qemu_put_sbyte(f
, 0);
523 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
525 VirtIOBlock
*s
= opaque
;
530 virtio_load(&s
->vdev
, f
);
531 while (qemu_get_sbyte(f
)) {
532 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
533 qemu_get_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
537 virtqueue_map_sg(req
->elem
.in_sg
, req
->elem
.in_addr
,
538 req
->elem
.in_num
, 1);
539 virtqueue_map_sg(req
->elem
.out_sg
, req
->elem
.out_addr
,
540 req
->elem
.out_num
, 0);
546 static void virtio_blk_resize(void *opaque
)
548 VirtIOBlock
*s
= opaque
;
550 virtio_notify_config(&s
->vdev
);
553 static const BlockDevOps virtio_block_ops
= {
554 .resize_cb
= virtio_blk_resize
,
557 VirtIODevice
*virtio_blk_init(DeviceState
*dev
, BlockConf
*conf
,
561 int cylinders
, heads
, secs
;
562 static int virtio_blk_id
;
566 error_report("virtio-blk-pci: drive property not set");
569 if (!bdrv_is_inserted(conf
->bs
)) {
570 error_report("Device needs media, but drive is empty");
575 /* try to fall back to value set with legacy -drive serial=... */
576 dinfo
= drive_get_by_blockdev(conf
->bs
);
577 if (*dinfo
->serial
) {
578 *serial
= strdup(dinfo
->serial
);
582 s
= (VirtIOBlock
*)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK
,
583 sizeof(struct virtio_blk_config
),
584 sizeof(VirtIOBlock
));
586 s
->vdev
.get_config
= virtio_blk_update_config
;
587 s
->vdev
.get_features
= virtio_blk_get_features
;
588 s
->vdev
.reset
= virtio_blk_reset
;
593 s
->sector_mask
= (s
->conf
->logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
594 bdrv_guess_geometry(s
->bs
, &cylinders
, &heads
, &secs
);
596 s
->vq
= virtio_add_queue(&s
->vdev
, 128, virtio_blk_handle_output
);
598 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
600 register_savevm(dev
, "virtio-blk", virtio_blk_id
++, 2,
601 virtio_blk_save
, virtio_blk_load
, s
);
602 bdrv_set_removable(s
->bs
, 0);
603 bdrv_set_dev_ops(s
->bs
, &virtio_block_ops
, s
);
604 s
->bs
->buffer_alignment
= conf
->logical_block_size
;
606 add_boot_device_path(conf
->bootindex
, dev
, "/disk@0,0");
611 void virtio_blk_exit(VirtIODevice
*vdev
)
613 VirtIOBlock
*s
= to_virtio_blk(vdev
);
614 unregister_savevm(s
->qdev
, "virtio-blk", s
);
615 virtio_cleanup(vdev
);