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"
16 #include "virtio-blk.h"
21 typedef struct VirtIOBlock
29 unsigned short sector_mask
;
30 char sn
[BLOCK_SERIAL_STRLEN
];
33 static VirtIOBlock
*to_virtio_blk(VirtIODevice
*vdev
)
35 return (VirtIOBlock
*)vdev
;
38 typedef struct VirtIOBlockReq
41 VirtQueueElement elem
;
42 struct virtio_blk_inhdr
*in
;
43 struct virtio_blk_outhdr
*out
;
44 struct virtio_scsi_inhdr
*scsi
;
46 struct VirtIOBlockReq
*next
;
49 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, int status
)
51 VirtIOBlock
*s
= req
->dev
;
53 req
->in
->status
= status
;
54 virtqueue_push(s
->vq
, &req
->elem
, req
->qiov
.size
+ sizeof(*req
->in
));
55 virtio_notify(&s
->vdev
, s
->vq
);
60 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
63 BlockErrorAction action
= bdrv_get_on_error(req
->dev
->bs
, is_read
);
64 VirtIOBlock
*s
= req
->dev
;
66 if (action
== BLOCK_ERR_IGNORE
) {
67 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
71 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
72 || action
== BLOCK_ERR_STOP_ANY
) {
75 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
78 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
79 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
85 static void virtio_blk_rw_complete(void *opaque
, int ret
)
87 VirtIOBlockReq
*req
= opaque
;
90 int is_read
= !(req
->out
->type
& VIRTIO_BLK_T_OUT
);
91 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
))
95 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
98 static void virtio_blk_flush_complete(void *opaque
, int ret
)
100 VirtIOBlockReq
*req
= opaque
;
102 virtio_blk_req_complete(req
, ret
? VIRTIO_BLK_S_IOERR
: VIRTIO_BLK_S_OK
);
105 static VirtIOBlockReq
*virtio_blk_alloc_request(VirtIOBlock
*s
)
107 VirtIOBlockReq
*req
= qemu_malloc(sizeof(*req
));
114 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
)
116 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
119 if (!virtqueue_pop(s
->vq
, &req
->elem
)) {
129 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
131 struct sg_io_hdr hdr
;
137 * We require at least one output segment each for the virtio_blk_outhdr
138 * and the SCSI command block.
140 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
141 * and the sense buffer pointer in the input segments.
143 if (req
->elem
.out_num
< 2 || req
->elem
.in_num
< 3) {
144 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
149 * No support for bidirection commands yet.
151 if (req
->elem
.out_num
> 2 && req
->elem
.in_num
> 3) {
152 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
157 * The scsi inhdr is placed in the second-to-last input segment, just
158 * before the regular inhdr.
160 req
->scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
162 memset(&hdr
, 0, sizeof(struct sg_io_hdr
));
163 hdr
.interface_id
= 'S';
164 hdr
.cmd_len
= req
->elem
.out_sg
[1].iov_len
;
165 hdr
.cmdp
= req
->elem
.out_sg
[1].iov_base
;
168 if (req
->elem
.out_num
> 2) {
170 * If there are more than the minimally required 2 output segments
171 * there is write payload starting from the third iovec.
173 hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
174 hdr
.iovec_count
= req
->elem
.out_num
- 2;
176 for (i
= 0; i
< hdr
.iovec_count
; i
++)
177 hdr
.dxfer_len
+= req
->elem
.out_sg
[i
+ 2].iov_len
;
179 hdr
.dxferp
= req
->elem
.out_sg
+ 2;
181 } else if (req
->elem
.in_num
> 3) {
183 * If we have more than 3 input segments the guest wants to actually
186 hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
187 hdr
.iovec_count
= req
->elem
.in_num
- 3;
188 for (i
= 0; i
< hdr
.iovec_count
; i
++)
189 hdr
.dxfer_len
+= req
->elem
.in_sg
[i
].iov_len
;
191 hdr
.dxferp
= req
->elem
.in_sg
;
194 * Some SCSI commands don't actually transfer any data.
196 hdr
.dxfer_direction
= SG_DXFER_NONE
;
199 hdr
.sbp
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_base
;
200 hdr
.mx_sb_len
= req
->elem
.in_sg
[req
->elem
.in_num
- 3].iov_len
;
202 ret
= bdrv_ioctl(req
->dev
->bs
, SG_IO
, &hdr
);
204 status
= VIRTIO_BLK_S_UNSUPP
;
206 hdr
.resid
= hdr
.dxfer_len
;
207 } else if (hdr
.status
) {
208 status
= VIRTIO_BLK_S_IOERR
;
210 status
= VIRTIO_BLK_S_OK
;
213 req
->scsi
->errors
= hdr
.status
;
214 req
->scsi
->residual
= hdr
.resid
;
215 req
->scsi
->sense_len
= hdr
.sb_len_wr
;
216 req
->scsi
->data_len
= hdr
.dxfer_len
;
218 virtio_blk_req_complete(req
, status
);
221 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
223 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
225 #endif /* __linux__ */
227 typedef struct MultiReqBuffer
{
228 BlockRequest blkreq
[32];
229 unsigned int num_writes
;
232 static void virtio_submit_multiwrite(BlockDriverState
*bs
, MultiReqBuffer
*mrb
)
236 if (!mrb
->num_writes
) {
240 ret
= bdrv_aio_multiwrite(bs
, mrb
->blkreq
, mrb
->num_writes
);
242 for (i
= 0; i
< mrb
->num_writes
; i
++) {
243 if (mrb
->blkreq
[i
].error
) {
244 virtio_blk_rw_complete(mrb
->blkreq
[i
].opaque
, -EIO
);
252 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
254 BlockDriverAIOCB
*acb
;
257 * Make sure all outstanding writes are posted to the backing device.
259 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
261 acb
= bdrv_aio_flush(req
->dev
->bs
, virtio_blk_flush_complete
, req
);
263 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
267 static void virtio_blk_handle_write(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
269 BlockRequest
*blkreq
;
271 if (req
->out
->sector
& req
->dev
->sector_mask
) {
272 virtio_blk_rw_complete(req
, -EIO
);
276 if (mrb
->num_writes
== 32) {
277 virtio_submit_multiwrite(req
->dev
->bs
, mrb
);
280 blkreq
= &mrb
->blkreq
[mrb
->num_writes
];
281 blkreq
->sector
= req
->out
->sector
;
282 blkreq
->nb_sectors
= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
283 blkreq
->qiov
= &req
->qiov
;
284 blkreq
->cb
= virtio_blk_rw_complete
;
285 blkreq
->opaque
= req
;
291 static void virtio_blk_handle_read(VirtIOBlockReq
*req
)
293 BlockDriverAIOCB
*acb
;
295 if (req
->out
->sector
& req
->dev
->sector_mask
) {
296 virtio_blk_rw_complete(req
, -EIO
);
300 acb
= bdrv_aio_readv(req
->dev
->bs
, req
->out
->sector
, &req
->qiov
,
301 req
->qiov
.size
/ BDRV_SECTOR_SIZE
,
302 virtio_blk_rw_complete
, req
);
304 virtio_blk_rw_complete(req
, -EIO
);
308 static void virtio_blk_handle_request(VirtIOBlockReq
*req
,
311 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
312 fprintf(stderr
, "virtio-blk missing headers\n");
316 if (req
->elem
.out_sg
[0].iov_len
< sizeof(*req
->out
) ||
317 req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_len
< sizeof(*req
->in
)) {
318 fprintf(stderr
, "virtio-blk header not in correct element\n");
322 req
->out
= (void *)req
->elem
.out_sg
[0].iov_base
;
323 req
->in
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 1].iov_base
;
325 if (req
->out
->type
& VIRTIO_BLK_T_FLUSH
) {
326 virtio_blk_handle_flush(req
, mrb
);
327 } else if (req
->out
->type
& VIRTIO_BLK_T_SCSI_CMD
) {
328 virtio_blk_handle_scsi(req
);
329 } else if (req
->out
->type
& VIRTIO_BLK_T_GET_ID
) {
330 VirtIOBlock
*s
= req
->dev
;
332 memcpy(req
->elem
.in_sg
[0].iov_base
, s
->sn
,
333 MIN(req
->elem
.in_sg
[0].iov_len
, sizeof(s
->sn
)));
334 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
335 } else if (req
->out
->type
& VIRTIO_BLK_T_OUT
) {
336 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.out_sg
[1],
337 req
->elem
.out_num
- 1);
338 virtio_blk_handle_write(req
, mrb
);
340 qemu_iovec_init_external(&req
->qiov
, &req
->elem
.in_sg
[0],
341 req
->elem
.in_num
- 1);
342 virtio_blk_handle_read(req
);
346 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
348 VirtIOBlock
*s
= to_virtio_blk(vdev
);
350 MultiReqBuffer mrb
= {
354 while ((req
= virtio_blk_get_request(s
))) {
355 virtio_blk_handle_request(req
, &mrb
);
358 virtio_submit_multiwrite(s
->bs
, &mrb
);
361 * FIXME: Want to check for completions before returning to guest mode,
362 * so cached reads and writes are reported as quickly as possible. But
363 * that should be done in the generic block layer.
367 static void virtio_blk_dma_restart_bh(void *opaque
)
369 VirtIOBlock
*s
= opaque
;
370 VirtIOBlockReq
*req
= s
->rq
;
371 MultiReqBuffer mrb
= {
375 qemu_bh_delete(s
->bh
);
381 virtio_blk_handle_request(req
, &mrb
);
385 virtio_submit_multiwrite(s
->bs
, &mrb
);
388 static void virtio_blk_dma_restart_cb(void *opaque
, int running
, int reason
)
390 VirtIOBlock
*s
= opaque
;
396 s
->bh
= qemu_bh_new(virtio_blk_dma_restart_bh
, s
);
397 qemu_bh_schedule(s
->bh
);
401 static void virtio_blk_reset(VirtIODevice
*vdev
)
404 * This should cancel pending requests, but can't do nicely until there
405 * are per-device request lists.
410 /* coalesce internal state, copy to pci i/o region 0
412 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
414 VirtIOBlock
*s
= to_virtio_blk(vdev
);
415 struct virtio_blk_config blkcfg
;
417 int cylinders
, heads
, secs
;
419 bdrv_get_geometry(s
->bs
, &capacity
);
420 bdrv_get_geometry_hint(s
->bs
, &cylinders
, &heads
, &secs
);
421 memset(&blkcfg
, 0, sizeof(blkcfg
));
422 stq_raw(&blkcfg
.capacity
, capacity
);
423 stl_raw(&blkcfg
.seg_max
, 128 - 2);
424 stw_raw(&blkcfg
.cylinders
, cylinders
);
425 blkcfg
.heads
= heads
;
426 blkcfg
.sectors
= secs
& ~s
->sector_mask
;
427 blkcfg
.blk_size
= s
->conf
->logical_block_size
;
429 blkcfg
.physical_block_exp
= get_physical_block_exp(s
->conf
);
430 blkcfg
.alignment_offset
= 0;
431 blkcfg
.min_io_size
= s
->conf
->min_io_size
/ blkcfg
.blk_size
;
432 blkcfg
.opt_io_size
= s
->conf
->opt_io_size
/ blkcfg
.blk_size
;
433 memcpy(config
, &blkcfg
, sizeof(struct virtio_blk_config
));
436 static uint32_t virtio_blk_get_features(VirtIODevice
*vdev
, uint32_t features
)
438 VirtIOBlock
*s
= to_virtio_blk(vdev
);
440 features
|= (1 << VIRTIO_BLK_F_SEG_MAX
);
441 features
|= (1 << VIRTIO_BLK_F_GEOMETRY
);
442 features
|= (1 << VIRTIO_BLK_F_TOPOLOGY
);
443 features
|= (1 << VIRTIO_BLK_F_BLK_SIZE
);
445 if (bdrv_enable_write_cache(s
->bs
))
446 features
|= (1 << VIRTIO_BLK_F_WCACHE
);
448 if (bdrv_is_read_only(s
->bs
))
449 features
|= 1 << VIRTIO_BLK_F_RO
;
454 static void virtio_blk_save(QEMUFile
*f
, void *opaque
)
456 VirtIOBlock
*s
= opaque
;
457 VirtIOBlockReq
*req
= s
->rq
;
459 virtio_save(&s
->vdev
, f
);
462 qemu_put_sbyte(f
, 1);
463 qemu_put_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
466 qemu_put_sbyte(f
, 0);
469 static int virtio_blk_load(QEMUFile
*f
, void *opaque
, int version_id
)
471 VirtIOBlock
*s
= opaque
;
476 virtio_load(&s
->vdev
, f
);
477 while (qemu_get_sbyte(f
)) {
478 VirtIOBlockReq
*req
= virtio_blk_alloc_request(s
);
479 qemu_get_buffer(f
, (unsigned char*)&req
->elem
, sizeof(req
->elem
));
487 VirtIODevice
*virtio_blk_init(DeviceState
*dev
, BlockConf
*conf
)
490 int cylinders
, heads
, secs
;
491 static int virtio_blk_id
;
495 error_report("virtio-blk-pci: drive property not set");
498 if (!bdrv_is_inserted(conf
->bs
)) {
499 error_report("Device needs media, but drive is empty");
503 s
= (VirtIOBlock
*)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK
,
504 sizeof(struct virtio_blk_config
),
505 sizeof(VirtIOBlock
));
507 s
->vdev
.get_config
= virtio_blk_update_config
;
508 s
->vdev
.get_features
= virtio_blk_get_features
;
509 s
->vdev
.reset
= virtio_blk_reset
;
513 s
->sector_mask
= (s
->conf
->logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
514 bdrv_guess_geometry(s
->bs
, &cylinders
, &heads
, &secs
);
516 /* NB: per existing s/n string convention the string is terminated
517 * by '\0' only when less than sizeof (s->sn)
519 dinfo
= drive_get_by_blockdev(s
->bs
);
520 strncpy(s
->sn
, dinfo
->serial
, sizeof (s
->sn
));
522 s
->vq
= virtio_add_queue(&s
->vdev
, 128, virtio_blk_handle_output
);
524 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb
, s
);
525 register_savevm(dev
, "virtio-blk", virtio_blk_id
++, 2,
526 virtio_blk_save
, virtio_blk_load
, s
);
527 bdrv_set_removable(s
->bs
, 0);