From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:45068) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TZNy2-0005x9-Ux for qemu-devel@nongnu.org; Fri, 16 Nov 2012 10:36:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TZNxz-0002cX-MI for qemu-devel@nongnu.org; Fri, 16 Nov 2012 10:36:30 -0500 Received: from greensocs.com ([87.106.252.221]:38713 helo=s15328186.onlinehome-server.info) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TZNxz-0002cL-Cj for qemu-devel@nongnu.org; Fri, 16 Nov 2012 10:36:27 -0500 Received: from localhost (unknown [127.0.0.1]) by s15328186.onlinehome-server.info (Postfix) with ESMTP id 6CAF640F4C9 for ; Fri, 16 Nov 2012 15:36:26 +0000 (UTC) From: fred.konrad@greensocs.com Date: Fri, 16 Nov 2012 16:35:59 +0100 Message-Id: <1353080159-10536-4-git-send-email-fred.konrad@greensocs.com> In-Reply-To: <1353080159-10536-1-git-send-email-fred.konrad@greensocs.com> References: <1353080159-10536-1-git-send-email-fred.konrad@greensocs.com> Subject: [Qemu-devel] [RFC PATCH 3/3] virtio-blk : add the virtio-blk device. List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: mark.burton@greensocs.com, KONRAD Frederic From: KONRAD Frederic 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 --- hw/virtio-blk.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-blk.h | 7 +++++ 2 files changed, 91 insertions(+) diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c index e25cc96..0fa2ab9 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 @@ -656,3 +658,85 @@ void virtio_blk_exit(VirtIODevice *vdev) blockdev_mark_auto_del(s->bs); virtio_cleanup(vdev); } + +static int virtio_blk_qdev_init(DeviceState *qdev) +{ + VirtIODevice *vdev; + VirtIOBLKState *s = DO_UPCAST(VirtIOBLKState, qdev, qdev); + VirtioBus *bus = DO_UPCAST(VirtioBus, qbus, s->qdev.parent_bus); + + vdev = virtio_blk_init(qdev, &s->blk); + if (!vdev) { + return -1; + } + + /* Set the PCI IDs : + * What if we add both IDs in VirtIODevice ? + */ + bus->pci_device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; + bus->pci_class = PCI_CLASS_STORAGE_SCSI; + + /* Call the init callback of the transport device eg: virtio-pci */ + if (virtio_bus_init_cb(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..9e4bd27 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,12 @@ struct VirtIOBlkConf uint32_t config_wce; }; +typedef struct { + DeviceState qdev; + uint32_t host_features; + VirtIOBlkConf blk; +} VirtIOBLKState; + #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) -- 1.7.11.7