[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 4/5] virtio block driver (v3)
From: |
Anthony Liguori |
Subject: |
[Qemu-devel] [PATCH 4/5] virtio block driver (v3) |
Date: |
Tue, 15 Apr 2008 17:11:30 -0500 |
This patch implements the virtio block driver backend.
Signed-off-by: Anthony Liguori <address@hidden>
diff --git a/Makefile.target b/Makefile.target
index ea632fa..4d695c7 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -535,7 +535,7 @@ OBJS += rtl8139.o
OBJS += e1000.o
# virtio devices
-OBJS += virtio.o virtio-net.o
+OBJS += virtio.o virtio-net.o virtio-blk.o
ifeq ($(TARGET_BASE_ARCH), i386)
# Hardware support
diff --git a/hw/pc.c b/hw/pc.c
index 4fec2d4..2da9413 100644
--- a/hw/pc.c
+++ b/hw/pc.c
@@ -1011,6 +1011,18 @@ static void pc_init1(int ram_size, int vga_ram_size,
}
}
}
+
+ /* Add virtio block devices */
+ if (pci_enabled) {
+ int index;
+ int unit_id = 0;
+
+ while ((index = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
+ virtio_blk_init(pci_bus, drives_table[index].bdrv);
+ unit_id++;
+ }
+ }
+
}
static void pc_init_pci(int ram_size, int vga_ram_size,
diff --git a/hw/pc.h b/hw/pc.h
index 9f83050..c828cda 100644
--- a/hw/pc.h
+++ b/hw/pc.h
@@ -143,4 +143,7 @@ void pci_piix4_ide_init(PCIBus *bus, BlockDriverState
**hd_table, int devfn,
void isa_ne2000_init(int base, qemu_irq irq, NICInfo *nd);
+/* virtio-blk.c */
+void *virtio_blk_init(PCIBus *bus, BlockDriverState *bs);
+
#endif
diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c
new file mode 100644
index 0000000..534e819
--- /dev/null
+++ b/hw/virtio-blk.c
@@ -0,0 +1,112 @@
+/*
+ * Virtio Block Device
+ *
+ * Copyright IBM, Corp. 2007
+ *
+ * Authors:
+ * Anthony Liguori <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "virtio.h"
+#include "block.h"
+#include "block_int.h"
+#include "pc.h"
+#include "virtio-blk.h"
+
+typedef struct VirtIOBlock
+{
+ VirtIODevice vdev;
+ BlockDriverState *bs;
+} VirtIOBlock;
+
+static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
+{
+ return (VirtIOBlock *)vdev;
+}
+
+static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
+{
+ VirtIOBlock *s = to_virtio_blk(vdev);
+ VirtQueueElement *elem;
+
+ while ((elem = virtqueue_pop(vq)) != 0) {
+ struct virtio_blk_outhdr out;
+ struct virtio_blk_inhdr in;
+ unsigned int wlen;
+ size_t in_size, out_size;
+
+ out_size = iovector_size(elem->out);
+ in_size = iovector_size(elem->in);
+
+ memcpy_from_iovector(&out, 0, sizeof(out), elem->out);
+
+ if (out.type & VIRTIO_BLK_T_SCSI_CMD) {
+ wlen = sizeof(in);
+ in.status = VIRTIO_BLK_S_UNSUPP;
+ } else if (out.type & VIRTIO_BLK_T_OUT) {
+ IOVector *sg;
+
+ sg = iovector_trim(elem->out, sizeof(out),
+ out_size - sizeof(out));
+ bdrv_writev(s->bs, out.sector, sg);
+ iovector_free(sg);
+
+ wlen = sizeof(in);
+ in.status = VIRTIO_BLK_S_OK;
+ } else {
+ IOVector *sg;
+
+ sg = iovector_trim(elem->in, 0, in_size - sizeof(in));
+ bdrv_readv(s->bs, out.sector, sg);
+ iovector_free(sg);
+
+ wlen = in_size;
+ in.status = VIRTIO_BLK_S_OK;
+ }
+
+ memcpy_to_iovector(elem->in, in_size - sizeof(in),
+ sizeof(in), &in);
+
+ virtqueue_push(vq, elem, wlen);
+ virtio_notify(vdev, vq);
+ }
+}
+
+static void virtio_blk_get_config(VirtIODevice *vdev, uint8_t *config)
+{
+ VirtIOBlock *s = to_virtio_blk(vdev);
+ struct virtio_blk_config blkcfg;
+ int64_t capacity;
+
+ bdrv_get_geometry(s->bs, &capacity);
+ blkcfg.capacity = cpu_to_le64(capacity);
+ blkcfg.seg_max = cpu_to_le32(128 - 2);
+ memcpy(config, &blkcfg, sizeof(blkcfg));
+}
+
+static uint32_t virtio_blk_get_features(VirtIODevice *vdev)
+{
+ return (1 << VIRTIO_BLK_F_SEG_MAX);
+}
+
+void *virtio_blk_init(PCIBus *bus, BlockDriverState *bs)
+{
+ VirtIOBlock *s;
+
+ s = (VirtIOBlock *)virtio_init_pci(bus, "virtio-blk", 6900, 0x1001,
+ 0, VIRTIO_ID_BLOCK,
+ 0x01, 0x80, 0x00,
+ 16, sizeof(VirtIOBlock));
+
+ s->vdev.get_config = virtio_blk_get_config;
+ s->vdev.get_features = virtio_blk_get_features;
+ s->bs = bs;
+
+ virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
+
+ return s;
+}
diff --git a/hw/virtio-blk.h b/hw/virtio-blk.h
new file mode 100644
index 0000000..290ff5b
--- /dev/null
+++ b/hw/virtio-blk.h
@@ -0,0 +1,66 @@
+/*
+ * Virtio Support
+ *
+ * Copyright IBM, Corp. 2007-2008
+ *
+ * Authors:
+ * Anthony Liguori <address@hidden>
+ * Rusty Russell <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef _QEMU_VIRTIO_BLK_H
+#define _QEMU_VIRTIO_BLK_H
+
+/* from Linux's linux/virtio_blk.h */
+
+/* The ID for virtio_block */
+#define VIRTIO_ID_BLOCK 2
+
+/* Feature bits */
+#define VIRTIO_BLK_F_BARRIER 0 /* Does host support barriers? */
+#define VIRTIO_BLK_F_SIZE_MAX 1 /* Indicates maximum segment size */
+#define VIRTIO_BLK_F_SEG_MAX 2 /* Indicates maximum # of segments */
+
+struct virtio_blk_config
+{
+ uint64_t capacity;
+ uint32_t size_max;
+ uint32_t seg_max;
+};
+
+/* These two define direction. */
+#define VIRTIO_BLK_T_IN 0
+#define VIRTIO_BLK_T_OUT 1
+
+/* This bit says it's a scsi command, not an actual read or write. */
+#define VIRTIO_BLK_T_SCSI_CMD 2
+
+/* Barrier before this op. */
+#define VIRTIO_BLK_T_BARRIER 0x80000000
+
+/* This is the first element of the read scatter-gather list. */
+struct virtio_blk_outhdr
+{
+ /* VIRTIO_BLK_T* */
+ uint32_t type;
+ /* io priority. */
+ uint32_t ioprio;
+ /* Sector (ie. 512 byte offset) */
+ uint64_t sector;
+};
+
+#define VIRTIO_BLK_S_OK 0
+#define VIRTIO_BLK_S_IOERR 1
+#define VIRTIO_BLK_S_UNSUPP 2
+
+/* This is the first element of the write scatter-gather list */
+struct virtio_blk_inhdr
+{
+ unsigned char status;
+};
+
+#endif
diff --git a/sysemu.h b/sysemu.h
index 0f18e04..0078190 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -119,7 +119,7 @@ extern unsigned int nb_prom_envs;
#endif
typedef enum {
- IF_IDE, IF_SCSI, IF_FLOPPY, IF_PFLASH, IF_MTD, IF_SD
+ IF_IDE, IF_SCSI, IF_FLOPPY, IF_PFLASH, IF_MTD, IF_SD, IF_VIRTIO
} BlockInterfaceType;
typedef struct DriveInfo {
diff --git a/vl.c b/vl.c
index 821c05d..4c11be6 100644
--- a/vl.c
+++ b/vl.c
@@ -5050,6 +5050,9 @@ static int drive_init(struct drive_opt *arg, int snapshot,
} else if (!strcmp(buf, "sd")) {
type = IF_SD;
max_devs = 0;
+ } else if (!strcmp(buf, "virtio")) {
+ type = IF_VIRTIO;
+ max_devs = 0;
} else {
fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str,
buf);
return -1;
@@ -5241,6 +5244,7 @@ static int drive_init(struct drive_opt *arg, int snapshot,
break;
case IF_PFLASH:
case IF_MTD:
+ case IF_VIRTIO:
break;
}
if (!file[0])
- [Qemu-devel] [PATCH 1/5] PCI DMA API (v3), Anthony Liguori, 2008/04/15
- [Qemu-devel] [PATCH 5/5] virtio balloon driver (v3), Anthony Liguori, 2008/04/15
- [Qemu-devel] [PATCH 2/5] virtio for QEMU (v3), Anthony Liguori, 2008/04/15
- [Qemu-devel] [PATCH 3/5] virtio network driver (v3), Anthony Liguori, 2008/04/15
- [Qemu-devel] [PATCH 4/5] virtio block driver (v3),
Anthony Liguori <=
- Re: [Qemu-devel] [PATCH 1/5] PCI DMA API (v3), Blue Swirl, 2008/04/16
- Re: [Qemu-devel] [PATCH 1/5] PCI DMA API (v3), Anthony Liguori, 2008/04/16
- Re: [Qemu-devel] [PATCH 1/5] PCI DMA API (v3), Blue Swirl, 2008/04/17
- Re: [Qemu-devel] [PATCH 1/5] PCI DMA API (v3), Anthony Liguori, 2008/04/17
- Re: [Qemu-devel] [PATCH 1/5] PCI DMA API (v3), Blue Swirl, 2008/04/19
- Re: [kvm-devel] [Qemu-devel] [PATCH 1/5] PCI DMA API (v3), Anthony Liguori, 2008/04/19
- Re: [kvm-devel] [Qemu-devel] [PATCH 1/5] PCI DMA API (v3), Blue Swirl, 2008/04/20
- Re: [kvm-devel] [Qemu-devel] [PATCH 1/5] PCI DMA API (v3), Anthony Liguori, 2008/04/20