2008-12-04 20:52:44 +01:00
|
|
|
/*
|
|
|
|
* Virtio Block Device
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2007
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-01-22 20:52:25 +01:00
|
|
|
#include <qemu-common.h>
|
|
|
|
#include <sysemu.h>
|
2008-12-04 20:52:44 +01:00
|
|
|
#include "virtio-blk.h"
|
|
|
|
#include "block_int.h"
|
|
|
|
|
|
|
|
typedef struct VirtIOBlock
|
|
|
|
{
|
|
|
|
VirtIODevice vdev;
|
|
|
|
BlockDriverState *bs;
|
|
|
|
VirtQueue *vq;
|
2009-01-22 20:52:25 +01:00
|
|
|
void *rq;
|
2008-12-04 20:52:44 +01:00
|
|
|
} VirtIOBlock;
|
|
|
|
|
|
|
|
static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
|
|
|
|
{
|
|
|
|
return (VirtIOBlock *)vdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct VirtIOBlockReq
|
|
|
|
{
|
|
|
|
VirtIOBlock *dev;
|
|
|
|
VirtQueueElement elem;
|
|
|
|
struct virtio_blk_inhdr *in;
|
|
|
|
struct virtio_blk_outhdr *out;
|
2009-03-28 18:46:14 +01:00
|
|
|
QEMUIOVector qiov;
|
2009-01-22 20:52:25 +01:00
|
|
|
struct VirtIOBlockReq *next;
|
2008-12-04 20:52:44 +01:00
|
|
|
} VirtIOBlockReq;
|
|
|
|
|
2009-01-22 20:52:25 +01:00
|
|
|
static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
|
|
|
|
{
|
|
|
|
VirtIOBlock *s = req->dev;
|
|
|
|
|
|
|
|
req->in->status = status;
|
2009-03-28 18:46:14 +01:00
|
|
|
virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
|
2009-01-22 20:52:25 +01:00
|
|
|
virtio_notify(&s->vdev, s->vq);
|
|
|
|
|
|
|
|
qemu_free(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtio_blk_handle_write_error(VirtIOBlockReq *req, int error)
|
|
|
|
{
|
|
|
|
BlockInterfaceErrorAction action = drive_get_onerror(req->dev->bs);
|
|
|
|
VirtIOBlock *s = req->dev;
|
|
|
|
|
|
|
|
if (action == BLOCK_ERR_IGNORE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
|
|
|
|
|| action == BLOCK_ERR_STOP_ANY) {
|
|
|
|
req->next = s->rq;
|
|
|
|
s->rq = req;
|
|
|
|
vm_stop(0);
|
|
|
|
} else {
|
|
|
|
virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-12-04 20:52:44 +01:00
|
|
|
static void virtio_blk_rw_complete(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
VirtIOBlockReq *req = opaque;
|
|
|
|
|
2009-03-28 18:46:14 +01:00
|
|
|
if (ret && (req->out->type & VIRTIO_BLK_T_OUT)) {
|
2009-01-22 20:52:25 +01:00
|
|
|
if (virtio_blk_handle_write_error(req, -ret))
|
|
|
|
return;
|
2008-12-04 20:52:44 +01:00
|
|
|
}
|
|
|
|
|
2009-01-22 20:52:25 +01:00
|
|
|
virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
|
|
|
|
}
|
2008-12-04 20:52:44 +01:00
|
|
|
|
2009-01-22 20:52:25 +01:00
|
|
|
static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
|
|
|
|
{
|
|
|
|
VirtIOBlockReq *req = qemu_mallocz(sizeof(*req));
|
2009-02-05 23:06:05 +01:00
|
|
|
req->dev = s;
|
2009-01-22 20:52:25 +01:00
|
|
|
return req;
|
2008-12-04 20:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
|
|
|
|
{
|
2009-01-22 20:52:25 +01:00
|
|
|
VirtIOBlockReq *req = virtio_blk_alloc_request(s);
|
2008-12-04 20:52:44 +01:00
|
|
|
|
2009-01-22 20:52:25 +01:00
|
|
|
if (req != NULL) {
|
|
|
|
if (!virtqueue_pop(s->vq, &req->elem)) {
|
|
|
|
qemu_free(req);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-04 20:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2009-03-28 18:46:14 +01:00
|
|
|
static void virtio_blk_handle_write(VirtIOBlockReq *req)
|
2009-01-22 20:52:25 +01:00
|
|
|
{
|
2009-03-28 18:46:14 +01:00
|
|
|
bdrv_aio_writev(req->dev->bs, req->out->sector, &req->qiov,
|
|
|
|
req->qiov.size / 512, virtio_blk_rw_complete, req);
|
|
|
|
}
|
2009-01-22 20:52:25 +01:00
|
|
|
|
2009-03-28 18:46:14 +01:00
|
|
|
static void virtio_blk_handle_read(VirtIOBlockReq *req)
|
|
|
|
{
|
|
|
|
bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
|
|
|
|
req->qiov.size / 512, virtio_blk_rw_complete, req);
|
2009-01-22 20:52:25 +01:00
|
|
|
}
|
|
|
|
|
2008-12-04 20:52:44 +01:00
|
|
|
static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
|
|
|
|
{
|
|
|
|
VirtIOBlock *s = to_virtio_blk(vdev);
|
|
|
|
VirtIOBlockReq *req;
|
|
|
|
|
|
|
|
while ((req = virtio_blk_get_request(s))) {
|
|
|
|
if (req->elem.out_num < 1 || req->elem.in_num < 1) {
|
|
|
|
fprintf(stderr, "virtio-blk missing headers\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
|
|
|
|
req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
|
|
|
|
fprintf(stderr, "virtio-blk header not in correct element\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
req->out = (void *)req->elem.out_sg[0].iov_base;
|
|
|
|
req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
|
|
|
|
|
|
|
|
if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
|
|
|
|
unsigned int len = sizeof(*req->in);
|
|
|
|
|
|
|
|
req->in->status = VIRTIO_BLK_S_UNSUPP;
|
|
|
|
virtqueue_push(vq, &req->elem, len);
|
|
|
|
virtio_notify(vdev, vq);
|
|
|
|
qemu_free(req);
|
|
|
|
} else if (req->out->type & VIRTIO_BLK_T_OUT) {
|
2009-03-28 18:46:14 +01:00
|
|
|
qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
|
|
|
|
req->elem.out_num - 1);
|
|
|
|
virtio_blk_handle_write(req);
|
2008-12-04 20:52:44 +01:00
|
|
|
} else {
|
2009-03-28 18:46:14 +01:00
|
|
|
qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
|
|
|
|
req->elem.in_num - 1);
|
|
|
|
virtio_blk_handle_read(req);
|
2008-12-04 20:52:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* FIXME: Want to check for completions before returning to guest mode,
|
|
|
|
* so cached reads and writes are reported as quickly as possible. But
|
|
|
|
* that should be done in the generic block layer.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2009-01-22 20:52:25 +01:00
|
|
|
static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
|
|
|
|
{
|
|
|
|
VirtIOBlock *s = opaque;
|
|
|
|
VirtIOBlockReq *req = s->rq;
|
|
|
|
|
|
|
|
if (!running)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s->rq = NULL;
|
|
|
|
|
|
|
|
while (req) {
|
|
|
|
virtio_blk_handle_write(req);
|
|
|
|
req = req->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-04 20:52:44 +01:00
|
|
|
static void virtio_blk_reset(VirtIODevice *vdev)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This should cancel pending requests, but can't do nicely until there
|
|
|
|
* are per-device request lists.
|
|
|
|
*/
|
|
|
|
qemu_aio_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
|
|
|
|
{
|
|
|
|
VirtIOBlock *s = to_virtio_blk(vdev);
|
|
|
|
struct virtio_blk_config blkcfg;
|
|
|
|
uint64_t capacity;
|
|
|
|
int cylinders, heads, secs;
|
|
|
|
|
|
|
|
bdrv_get_geometry(s->bs, &capacity);
|
|
|
|
bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
|
|
|
|
stq_raw(&blkcfg.capacity, capacity);
|
|
|
|
stl_raw(&blkcfg.seg_max, 128 - 2);
|
|
|
|
stw_raw(&blkcfg.cylinders, cylinders);
|
|
|
|
blkcfg.heads = heads;
|
|
|
|
blkcfg.sectors = secs;
|
|
|
|
memcpy(config, &blkcfg, sizeof(blkcfg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t virtio_blk_get_features(VirtIODevice *vdev)
|
|
|
|
{
|
|
|
|
return (1 << VIRTIO_BLK_F_SEG_MAX | 1 << VIRTIO_BLK_F_GEOMETRY);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_blk_save(QEMUFile *f, void *opaque)
|
|
|
|
{
|
|
|
|
VirtIOBlock *s = opaque;
|
2009-01-22 20:52:25 +01:00
|
|
|
VirtIOBlockReq *req = s->rq;
|
|
|
|
|
2008-12-04 20:52:44 +01:00
|
|
|
virtio_save(&s->vdev, f);
|
2009-01-22 20:52:25 +01:00
|
|
|
|
|
|
|
while (req) {
|
|
|
|
qemu_put_sbyte(f, 1);
|
|
|
|
qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
|
|
|
|
req = req->next;
|
|
|
|
}
|
|
|
|
qemu_put_sbyte(f, 0);
|
2008-12-04 20:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
VirtIOBlock *s = opaque;
|
|
|
|
|
2009-01-22 20:52:25 +01:00
|
|
|
if (version_id != 2)
|
2008-12-04 20:52:44 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
virtio_load(&s->vdev, f);
|
2009-01-22 20:52:25 +01:00
|
|
|
while (qemu_get_sbyte(f)) {
|
|
|
|
VirtIOBlockReq *req = virtio_blk_alloc_request(s);
|
|
|
|
qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
|
|
|
|
req->next = s->rq;
|
|
|
|
s->rq = req->next;
|
|
|
|
}
|
2008-12-04 20:52:44 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-11 22:20:03 +01:00
|
|
|
void *virtio_blk_init(PCIBus *bus, BlockDriverState *bs)
|
2008-12-04 20:52:44 +01:00
|
|
|
{
|
|
|
|
VirtIOBlock *s;
|
|
|
|
int cylinders, heads, secs;
|
|
|
|
static int virtio_blk_id;
|
|
|
|
|
2008-12-11 22:20:03 +01:00
|
|
|
s = (VirtIOBlock *)virtio_init_pci(bus, "virtio-blk",
|
|
|
|
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
|
|
|
PCI_DEVICE_ID_VIRTIO_BLOCK,
|
2009-01-26 16:22:57 +01:00
|
|
|
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
|
|
|
VIRTIO_ID_BLOCK,
|
2009-02-01 20:26:20 +01:00
|
|
|
PCI_CLASS_STORAGE_OTHER, 0x00,
|
2008-12-04 20:52:44 +01:00
|
|
|
sizeof(struct virtio_blk_config), sizeof(VirtIOBlock));
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s->vdev.get_config = virtio_blk_update_config;
|
|
|
|
s->vdev.get_features = virtio_blk_get_features;
|
|
|
|
s->vdev.reset = virtio_blk_reset;
|
|
|
|
s->bs = bs;
|
2009-01-22 20:52:25 +01:00
|
|
|
s->rq = NULL;
|
2009-02-11 16:20:29 +01:00
|
|
|
bs->private = &s->vdev.pci_dev;
|
2008-12-04 20:52:44 +01:00
|
|
|
bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
|
|
|
|
bdrv_set_geometry_hint(s->bs, cylinders, heads, secs);
|
|
|
|
|
|
|
|
s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
|
|
|
|
|
2009-01-22 20:52:25 +01:00
|
|
|
qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
|
|
|
|
register_savevm("virtio-blk", virtio_blk_id++, 2,
|
2008-12-04 20:52:44 +01:00
|
|
|
virtio_blk_save, virtio_blk_load, s);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|