Patchwork [04/61] virtio-pci : refactor virtio-pci device.

login
register
mail settings
Submitter fred.konrad@greensocs.com
Date Jan. 7, 2013, 6:40 p.m.
Message ID <1357584074-10852-5-git-send-email-fred.konrad@greensocs.com>
Download mbox | patch
Permalink /patch/210033/
State New
Headers show

Comments

fred.konrad@greensocs.com - Jan. 7, 2013, 6:40 p.m.
From: KONRAD Frederic <fred.konrad@greensocs.com>

Create the virtio-pci device. This transport device will create a
virtio-pci-bus, so one VirtIODevice can be connected.

Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
---
 hw/virtio-pci.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio-pci.h |  18 ++++++++
 2 files changed, 145 insertions(+)
Peter Maydell - Jan. 8, 2013, 5:54 p.m.
On 7 January 2013 18:40,  <fred.konrad@greensocs.com> wrote:
> From: KONRAD Frederic <fred.konrad@greensocs.com>
>
> Create the virtio-pci device. This transport device will create a
> virtio-pci-bus, so one VirtIODevice can be connected.
>
> Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
> ---
>  hw/virtio-pci.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  hw/virtio-pci.h |  18 ++++++++
>  2 files changed, 145 insertions(+)
>
> diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c
> index 5ff03e8..1f0ecbe 100644
> --- a/hw/virtio-pci.c
> +++ b/hw/virtio-pci.c
> @@ -1163,6 +1163,130 @@ static TypeInfo virtio_scsi_info = {
>      .class_init    = virtio_scsi_class_init,
>  };
>
> +/*
> + * virtio-pci : This is the PCIDevice which have a virtio-pci-bus.

"has"

> + */
> +
> +/* This is called by virtio-bus just after the device is plugged. */
> +static void virtio_pci_device_plugged(DeviceState *d)
> +{
> +    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
> +    VirtioBusState *bus = proxy->bus;
> +    uint8_t *config;
> +    uint32_t size;
> +
> +    /* Put the PCI IDs */
> +    switch (virtio_device_get_id(proxy->bus)) {
> +
> +
> +    default:
> +        error_report("unknown device id\n");
> +        break;
> +
> +    }

This doesn't have any code in it yet (it gets added in later patches) but
the final result looks very repetitive. I think you'd be better off just having
some arrays:

uint16_t virtio_pci_device_id[] = {
     [VIRTIO_ID_BLOCK] = PCI_DEVICE_ID_VIRTIO_BLOCK,
     [VIRTIO_ID_NET] = PCI_DEVICE_ID_VIRTIO_NET,
(etc)
};

similarly for the class. Then you can just drop the switch statement.

In fact I think you might as well put in the array entries for all
the virtio devices in this patch rather than adding one in each of the
"add virtio-foo device" patches; it will do no harm for them to be
there early and it makes the later patches a little smaller.


> +
> +    proxy->vdev = proxy->bus->vdev;
> +
> +    config = proxy->pci_dev.config;
> +    if (proxy->class_code) {
> +        pci_config_set_class(config, proxy->class_code);
> +    }
> +    pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
> +                 pci_get_word(config + PCI_VENDOR_ID));
> +    pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_device_get_id(proxy->bus));
> +    config[PCI_INTERRUPT_PIN] = 1;

> --- a/hw/virtio-pci.h
> +++ b/hw/virtio-pci.h
> @@ -45,6 +45,22 @@ typedef struct {
>      unsigned int users;
>  } VirtIOIRQFD;
>
> +/*
> + * virtio-pci : This is the PCIDevice which have a virtio-pci-bus.

"has"

> + */

-- PMM
fred.konrad@greensocs.com - Jan. 9, 2013, 8:45 a.m.
On 08/01/2013 18:54, Peter Maydell wrote:
> On 7 January 2013 18:40,  <fred.konrad@greensocs.com> wrote:
>> From: KONRAD Frederic <fred.konrad@greensocs.com>
>>
>> Create the virtio-pci device. This transport device will create a
>> virtio-pci-bus, so one VirtIODevice can be connected.
>>
>> Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
>> ---
>>   hw/virtio-pci.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>   hw/virtio-pci.h |  18 ++++++++
>>   2 files changed, 145 insertions(+)
>>
>> diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c
>> index 5ff03e8..1f0ecbe 100644
>> --- a/hw/virtio-pci.c
>> +++ b/hw/virtio-pci.c
>> @@ -1163,6 +1163,130 @@ static TypeInfo virtio_scsi_info = {
>>       .class_init    = virtio_scsi_class_init,
>>   };
>>
>> +/*
>> + * virtio-pci : This is the PCIDevice which have a virtio-pci-bus.
> "has"
>
>> + */
>> +
>> +/* This is called by virtio-bus just after the device is plugged. */
>> +static void virtio_pci_device_plugged(DeviceState *d)
>> +{
>> +    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
>> +    VirtioBusState *bus = proxy->bus;
>> +    uint8_t *config;
>> +    uint32_t size;
>> +
>> +    /* Put the PCI IDs */
>> +    switch (virtio_device_get_id(proxy->bus)) {
>> +
>> +
>> +    default:
>> +        error_report("unknown device id\n");
>> +        break;
>> +
>> +    }
> This doesn't have any code in it yet (it gets added in later patches) but
> the final result looks very repetitive. I think you'd be better off just having
> some arrays:
>
> uint16_t virtio_pci_device_id[] = {
>       [VIRTIO_ID_BLOCK] = PCI_DEVICE_ID_VIRTIO_BLOCK,
>       [VIRTIO_ID_NET] = PCI_DEVICE_ID_VIRTIO_NET,
> (etc)
> };
>
> similarly for the class. Then you can just drop the switch statement.
>
> In fact I think you might as well put in the array entries for all
> the virtio devices in this patch rather than adding one in each of the
> "add virtio-foo device" patches; it will do no harm for them to be
> there early and it makes the later patches a little smaller.
And what happen when it is a bad ID ?

Fred

>
>> +
>> +    proxy->vdev = proxy->bus->vdev;
>> +
>> +    config = proxy->pci_dev.config;
>> +    if (proxy->class_code) {
>> +        pci_config_set_class(config, proxy->class_code);
>> +    }
>> +    pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
>> +                 pci_get_word(config + PCI_VENDOR_ID));
>> +    pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_device_get_id(proxy->bus));
>> +    config[PCI_INTERRUPT_PIN] = 1;
>> --- a/hw/virtio-pci.h
>> +++ b/hw/virtio-pci.h
>> @@ -45,6 +45,22 @@ typedef struct {
>>       unsigned int users;
>>   } VirtIOIRQFD;
>>
>> +/*
>> + * virtio-pci : This is the PCIDevice which have a virtio-pci-bus.
> "has"
>
>> + */
> -- PMM
>
Peter Maydell - Jan. 9, 2013, 2:33 p.m.
On 9 January 2013 08:45, KONRAD Frédéric <fred.konrad@greensocs.com> wrote:
> On 08/01/2013 18:54, Peter Maydell wrote:
>> This doesn't have any code in it yet (it gets added in later patches) but
>> the final result looks very repetitive. I think you'd be better off just
>> having
>> some arrays:
>>
>> uint16_t virtio_pci_device_id[] = {
>>       [VIRTIO_ID_BLOCK] = PCI_DEVICE_ID_VIRTIO_BLOCK,
>>       [VIRTIO_ID_NET] = PCI_DEVICE_ID_VIRTIO_NET,
>> (etc)
>> };
>>
>> similarly for the class. Then you can just drop the switch statement.
>>
>> In fact I think you might as well put in the array entries for all
>> the virtio devices in this patch rather than adding one in each of the
>> "add virtio-foo device" patches; it will do no harm for them to be
>> there early and it makes the later patches a little smaller.
>
> And what happen when it is a bad ID ?

Error, same as before. Obviously you protect the array lookup
with a sizeof check for out of range, and you know if the array
entry is 0 it's invalid (0 being neither a valid device id or class).

If you think that looks too ugly then you could keep the switch, but
don't put calls to pci_config_set_device_id() and pci_config_set_class()
in each switch, that's just code duplication.

-- PMM

Patch

diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c
index 5ff03e8..1f0ecbe 100644
--- a/hw/virtio-pci.c
+++ b/hw/virtio-pci.c
@@ -1163,6 +1163,130 @@  static TypeInfo virtio_scsi_info = {
     .class_init    = virtio_scsi_class_init,
 };
 
+/*
+ * virtio-pci : This is the PCIDevice which have a virtio-pci-bus.
+ */
+
+/* This is called by virtio-bus just after the device is plugged. */
+static void virtio_pci_device_plugged(DeviceState *d)
+{
+    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
+    VirtioBusState *bus = proxy->bus;
+    uint8_t *config;
+    uint32_t size;
+
+    /* Put the PCI IDs */
+    switch (virtio_device_get_id(proxy->bus)) {
+
+
+    default:
+        error_report("unknown device id\n");
+        break;
+
+    }
+
+    proxy->vdev = proxy->bus->vdev;
+
+    config = proxy->pci_dev.config;
+    if (proxy->class_code) {
+        pci_config_set_class(config, proxy->class_code);
+    }
+    pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
+                 pci_get_word(config + PCI_VENDOR_ID));
+    pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_device_get_id(proxy->bus));
+    config[PCI_INTERRUPT_PIN] = 1;
+
+    if (proxy->nvectors &&
+        msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) {
+        proxy->nvectors = 0;
+    }
+
+    proxy->pci_dev.config_write = virtio_write_config;
+
+    size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
+         + virtio_device_get_config_len(bus);
+    if (size & (size - 1)) {
+        size = 1 << qemu_fls(size);
+    }
+
+    memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
+                          "virtio-pci", size);
+    pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
+                     &proxy->bar);
+
+    if (!kvm_has_many_ioeventfds()) {
+        proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
+    }
+
+    proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
+    proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
+    proxy->host_features = virtio_device_get_features(bus,
+                                                      proxy->host_features);
+}
+
+/* This is called by virtio-bus just before the device is unplugged. */
+static void virtio_pci_device_unplug(DeviceState *d)
+{
+    VirtIOPCIProxy *dev = VIRTIO_PCI(d);
+    virtio_pci_stop_ioeventfd(dev);
+}
+
+static int virtio_pci_init(PCIDevice *pci_dev)
+{
+    VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev);
+    VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
+    dev->bus = virtio_pci_bus_new(dev);
+    if (k->init != NULL) {
+        return k->init(dev);
+    }
+    return 0;
+}
+
+static void virtio_pci_exit(PCIDevice *pci_dev)
+{
+    VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
+    VirtioBusState *bus = VIRTIO_BUS(proxy->bus);
+    BusState *qbus = BUS(proxy->bus);
+    virtio_bus_destroy_device(bus);
+    qbus_free(qbus);
+    virtio_exit_pci(pci_dev);
+}
+
+/*
+ * This will be renamed virtio_pci_reset at the end of the series.
+ * virtio_pci_reset is still in use at this moment.
+ */
+static void virtio_pci_rst(DeviceState *qdev)
+{
+    VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
+    VirtioBusState *bus = VIRTIO_BUS(proxy->bus);
+    virtio_pci_stop_ioeventfd(proxy);
+    virtio_bus_reset(bus);
+    msix_unuse_all_vectors(&proxy->pci_dev);
+    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+}
+
+static void virtio_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
+
+    k->init = virtio_pci_init;
+    k->exit = virtio_pci_exit;
+    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
+    k->revision = VIRTIO_PCI_ABI_VERSION;
+    k->class_id = PCI_CLASS_OTHERS;
+    dc->reset = virtio_pci_rst;
+}
+
+static const TypeInfo virtio_pci_info = {
+    .name          = TYPE_VIRTIO_PCI,
+    .parent        = TYPE_PCI_DEVICE,
+    .instance_size = sizeof(VirtIOPCIProxy),
+    .class_init    = virtio_pci_class_init,
+    .class_size    = sizeof(VirtioPCIClass),
+};
+
 /* virtio-pci-bus */
 
 VirtioBusState *virtio_pci_bus_new(VirtIOPCIProxy *dev)
@@ -1189,6 +1313,8 @@  static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
     k->set_host_notifier = virtio_pci_set_host_notifier;
     k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
     k->vmstate_change = virtio_pci_vmstate_change;
+    k->device_plugged = virtio_pci_device_plugged;
+    k->device_unplug = virtio_pci_device_unplug;
 }
 
 static const TypeInfo virtio_pci_bus_info = {
@@ -1207,6 +1333,7 @@  static void virtio_pci_register_types(void)
     type_register_static(&virtio_scsi_info);
     type_register_static(&virtio_rng_info);
     type_register_static(&virtio_pci_bus_info);
+    type_register_static(&virtio_pci_info);
 }
 
 type_init(virtio_pci_register_types)
diff --git a/hw/virtio-pci.h b/hw/virtio-pci.h
index 2ac18b3..05d52cb 100644
--- a/hw/virtio-pci.h
+++ b/hw/virtio-pci.h
@@ -45,6 +45,22 @@  typedef struct {
     unsigned int users;
 } VirtIOIRQFD;
 
+/*
+ * virtio-pci : This is the PCIDevice which have a virtio-pci-bus.
+ */
+#define TYPE_VIRTIO_PCI "virtio-pci"
+#define VIRTIO_PCI_GET_CLASS(obj) \
+        OBJECT_GET_CLASS(VirtioPCIClass, obj, TYPE_VIRTIO_PCI)
+#define VIRTIO_PCI_CLASS(klass) \
+        OBJECT_CLASS_CHECK(VirtioPCIClass, klass, TYPE_VIRTIO_PCI)
+#define VIRTIO_PCI(obj) \
+        OBJECT_CHECK(VirtIOPCIProxy, (obj), TYPE_VIRTIO_PCI)
+
+typedef struct VirtioPCIClass {
+    PCIDeviceClass parent_class;
+    int (*init)(VirtIOPCIProxy *vpci_dev);
+} VirtioPCIClass;
+
 struct VirtIOPCIProxy {
     PCIDevice pci_dev;
     VirtIODevice *vdev;
@@ -65,6 +81,8 @@  struct VirtIOPCIProxy {
     bool ioeventfd_disabled;
     bool ioeventfd_started;
     VirtIOIRQFD *vector_irqfd;
+    /* That's the virtio-bus on which VirtioDevice will be connected. */
+    VirtioBusState *bus;
 };
 
 void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev);