Patchwork [RFC,v2,3/3] virtio-blk : add the virtio-blk device.

login
register
mail settings
Submitter fred.konrad@greensocs.com
Date Nov. 22, 2012, 2:50 p.m.
Message ID <1353595852-30776-4-git-send-email-fred.konrad@greensocs.com>
Download mbox | patch
Permalink /patch/201063/
State New
Headers show

Comments

fred.konrad@greensocs.com - Nov. 22, 2012, 2:50 p.m.
From: KONRAD Frederic <fred.konrad@greensocs.com>

This patch just add the virtio-blk device which can connect on a Virtio-Bus. The
initialization fail if no free VirtioBus are present.

Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
---
 hw/virtio-blk.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio-blk.h | 10 +++++++
 2 files changed, 92 insertions(+)
Stefan Hajnoczi - Nov. 23, 2012, 12:32 p.m.
On Thu, Nov 22, 2012 at 03:50:52PM +0100, fred.konrad@greensocs.com wrote:
> @@ -656,3 +658,83 @@ void virtio_blk_exit(VirtIODevice *vdev)
>      blockdev_mark_auto_del(s->bs);
>      virtio_cleanup(vdev);
>  }
> +
> +static int virtio_blk_qdev_init(DeviceState *qdev)
> +{
> +    VirtIOBLKState *s = VIRTIO_BLK(qdev);

Please use "Block" or "Blk", not "BLK".

Patch

diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c
index e25cc96..d8263c3 100644
--- a/hw/virtio-blk.c
+++ b/hw/virtio-blk.c
@@ -17,6 +17,8 @@ 
 #include "hw/block-common.h"
 #include "blockdev.h"
 #include "virtio-blk.h"
+#include "virtio-bus.h"
+#include "hw/pci.h"         /* for PCI IDs */
 #include "scsi-defs.h"
 #ifdef __linux__
 # include <scsi/sg.h>
@@ -656,3 +658,83 @@  void virtio_blk_exit(VirtIODevice *vdev)
     blockdev_mark_auto_del(s->bs);
     virtio_cleanup(vdev);
 }
+
+static int virtio_blk_qdev_init(DeviceState *qdev)
+{
+    VirtIOBLKState *s = VIRTIO_BLK(qdev);
+    /*
+     * When the 'bus=' option is not set, the default bus is the last created.
+     * This is a problem because only one VirtIODevice can be plugged.
+     */
+    VirtioBus *bus = VIRTIO_BUS(qdev_get_parent_bus(qdev));
+
+    /* init the device. */
+    VirtIODevice *vdev = virtio_blk_init(qdev, &s->blk);
+    if (!vdev) {
+        return -1;
+    }
+
+    /* Plug the device */
+    if (virtio_bus_plug_device(bus, vdev) != 0) {
+        /* this can happen when the bus is not free */
+        return -1;
+    }
+
+    return 0;
+}
+
+static int virtio_blk_qdev_exit(DeviceState *qdev)
+{
+    VirtioBus *bus = DO_UPCAST(VirtioBus, qbus, qdev->parent_bus);
+    virtio_bus_exit_cb(bus);
+    virtio_blk_exit(bus->vdev);
+    return 0;
+}
+
+static Property virtio_blk_properties[] = {
+    DEFINE_BLOCK_PROPERTIES(VirtIOBLKState, blk.conf),
+    DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBLKState, blk.conf),
+    DEFINE_PROP_STRING("serial", VirtIOBLKState, blk.serial),
+#ifdef __linux__
+    DEFINE_PROP_BIT("scsi", VirtIOBLKState, blk.scsi, 0, true),
+#endif
+    DEFINE_PROP_BIT("config-wce", VirtIOBLKState, blk.config_wce, 0, true),
+    /*
+     * Theses one are PCI related.
+     * DEFINE_PROP_BIT("ioeventfd", VirtIOBLKState, flags,
+     *                 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
+     * DEFINE_PROP_UINT32("vectors", VirtIOBLKState, nvectors, 2),
+     */
+    DEFINE_VIRTIO_BLK_FEATURES(VirtIOBLKState, host_features),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void virtio_blk_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *k = DEVICE_CLASS(klass);
+    k->bus_type = TYPE_VIRTIO_BUS;
+    k->init = virtio_blk_qdev_init;
+    k->exit = virtio_blk_qdev_exit;
+    /*
+     * k->unplug = ?
+     */
+    k->props = virtio_blk_properties;
+}
+
+static TypeInfo virtio_blk_info = {
+    .name = "virtio-blk",
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(VirtIOBLKState),
+    /*
+     * .abstract = true,
+     * .class_size = sizeof(?),
+     */
+    .class_init = virtio_blk_class_init,
+};
+
+static void virtio_blk_register_types(void)
+{
+    type_register_static(&virtio_blk_info);
+}
+
+type_init(virtio_blk_register_types)
diff --git a/hw/virtio-blk.h b/hw/virtio-blk.h
index f0740d0..cb2ea8c 100644
--- a/hw/virtio-blk.h
+++ b/hw/virtio-blk.h
@@ -16,6 +16,7 @@ 
 
 #include "virtio.h"
 #include "hw/block-common.h"
+#include "virtio-bus.h"
 
 /* from Linux's linux/virtio_blk.h */
 
@@ -107,6 +108,15 @@  struct VirtIOBlkConf
     uint32_t config_wce;
 };
 
+typedef struct {
+    DeviceState qdev;
+    uint32_t host_features;
+    VirtIOBlkConf blk;
+} VirtIOBLKState;
+
+#define TYPE_VIRTIO_BLK "virtio-blk"
+#define VIRTIO_BLK(obj) OBJECT_CHECK(VirtIOBLKState, (obj), TYPE_VIRTIO_BLK)
+
 #define DEFINE_VIRTIO_BLK_FEATURES(_state, _field) \
         DEFINE_VIRTIO_COMMON_FEATURES(_state, _field), \
         DEFINE_PROP_BIT("config-wce", _state, _field, VIRTIO_BLK_F_CONFIG_WCE, true)