0a122b
From 9164d579671d6147799c92ee35b9efc1760155cb Mon Sep 17 00:00:00 2001
0a122b
From: Markus Armbruster <armbru@redhat.com>
0a122b
Date: Thu, 23 Jan 2014 14:03:31 +0100
0a122b
Subject: [PATCH 04/14] virtio-bus: remove vdev field
0a122b
0a122b
RH-Author: Markus Armbruster <armbru@redhat.com>
0a122b
Message-id: <1390485820-7585-2-git-send-email-armbru@redhat.com>
0a122b
Patchwork-id: 56927
0a122b
O-Subject: [PATCH 7.0 qemu-kvm 01/10] virtio-bus: remove vdev field
0a122b
Bugzilla: 983344
0a122b
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
0a122b
RH-Acked-by: Marcel Apfelbaum <marcel.a@redhat.com>
0a122b
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
0a122b
0a122b
From: Paolo Bonzini <pbonzini@redhat.com>
0a122b
0a122b
The vdev field is complicated to synchronize.  Just access the
0a122b
BusState's list of children.
0a122b
0a122b
Cc: qemu-stable@nongnu.org
0a122b
Acked-by: Andreas Faerber <afaerber@suse.de>
0a122b
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
0a122b
(cherry picked from commit 06d3dff0723c712a4b109ced4243edf49ef850af)
0a122b
0a122b
Conflicts:
0a122b
	hw/virtio/virtio-mmio.c
0a122b
0a122b
We don't have hw/virtio/virtio-mmio.c.
0a122b
0a122b
Semantic conflict in hw/pci/pci-hotplug, because we got commit 1e3043a
0a122b
via upstream 1.5.3, but upstream master has not.
0a122b
Signed-off-by: Markus Armbruster <armbru@redhat.com>
0a122b
---
0a122b
 hw/pci/pci-hotplug.c           |  2 +-
0a122b
 hw/virtio/virtio-bus.c         | 65 +++++++++++++++++++++++++-----------------
0a122b
 hw/virtio/virtio-pci.c         |  2 +-
0a122b
 include/hw/virtio/virtio-bus.h | 16 ++++++++---
0a122b
 4 files changed, 53 insertions(+), 32 deletions(-)
0a122b
0a122b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
---
0a122b
 hw/pci/pci-hotplug.c           |    2 +-
0a122b
 hw/virtio/virtio-bus.c         |   65 ++++++++++++++++++++++++----------------
0a122b
 hw/virtio/virtio-pci.c         |    2 +-
0a122b
 include/hw/virtio/virtio-bus.h |   16 +++++++--
0a122b
 4 files changed, 53 insertions(+), 32 deletions(-)
0a122b
0a122b
diff --git a/hw/pci/pci-hotplug.c b/hw/pci/pci-hotplug.c
0a122b
index 667e40c..eae28d4 100644
0a122b
--- a/hw/pci/pci-hotplug.c
0a122b
+++ b/hw/pci/pci-hotplug.c
0a122b
@@ -96,7 +96,7 @@ static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
0a122b
             return -1;
0a122b
         }
0a122b
         virtio_proxy = VIRTIO_PCI(adapter);
0a122b
-        adapter = DEVICE(virtio_proxy->bus.vdev);
0a122b
+        adapter = DEVICE(virtio_bus_get_device(&virtio_proxy->bus));
0a122b
         scsibus = (SCSIBus *)
0a122b
                   object_dynamic_cast(OBJECT(QLIST_FIRST(&adapter->child_bus)),
0a122b
                             TYPE_SCSI_BUS);
0a122b
diff --git a/hw/virtio/virtio-bus.c b/hw/virtio/virtio-bus.c
0a122b
index e6b103c..17dd06e 100644
0a122b
--- a/hw/virtio/virtio-bus.c
0a122b
+++ b/hw/virtio/virtio-bus.c
0a122b
@@ -46,8 +46,6 @@ int virtio_bus_plug_device(VirtIODevice *vdev)
0a122b
     VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
0a122b
     DPRINTF("%s: plug device.\n", qbus->name);
0a122b
 
0a122b
-    bus->vdev = vdev;
0a122b
-
0a122b
     if (klass->device_plugged != NULL) {
0a122b
         klass->device_plugged(qbus->parent);
0a122b
     }
0a122b
@@ -58,9 +56,11 @@ int virtio_bus_plug_device(VirtIODevice *vdev)
0a122b
 /* Reset the virtio_bus */
0a122b
 void virtio_bus_reset(VirtioBusState *bus)
0a122b
 {
0a122b
+    VirtIODevice *vdev = virtio_bus_get_device(bus);
0a122b
+
0a122b
     DPRINTF("%s: reset device.\n", qbus->name);
0a122b
-    if (bus->vdev != NULL) {
0a122b
-        virtio_reset(bus->vdev);
0a122b
+    if (vdev != NULL) {
0a122b
+        virtio_reset(vdev);
0a122b
     }
0a122b
 }
0a122b
 
0a122b
@@ -69,62 +69,71 @@ void virtio_bus_destroy_device(VirtioBusState *bus)
0a122b
 {
0a122b
     BusState *qbus = BUS(bus);
0a122b
     VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
0a122b
+    VirtIODevice *vdev = virtio_bus_get_device(bus);
0a122b
+
0a122b
     DPRINTF("%s: remove device.\n", qbus->name);
0a122b
 
0a122b
-    if (bus->vdev != NULL) {
0a122b
+    if (vdev != NULL) {
0a122b
         if (klass->device_unplug != NULL) {
0a122b
             klass->device_unplug(qbus->parent);
0a122b
         }
0a122b
-        object_unparent(OBJECT(bus->vdev));
0a122b
-        bus->vdev = NULL;
0a122b
+        object_unparent(OBJECT(vdev));
0a122b
     }
0a122b
 }
0a122b
 
0a122b
 /* Get the device id of the plugged device. */
0a122b
 uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
0a122b
 {
0a122b
-    assert(bus->vdev != NULL);
0a122b
-    return bus->vdev->device_id;
0a122b
+    VirtIODevice *vdev = virtio_bus_get_device(bus);
0a122b
+    assert(vdev != NULL);
0a122b
+    return vdev->device_id;
0a122b
 }
0a122b
 
0a122b
 /* Get the config_len field of the plugged device. */
0a122b
 size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
0a122b
 {
0a122b
-    assert(bus->vdev != NULL);
0a122b
-    return bus->vdev->config_len;
0a122b
+    VirtIODevice *vdev = virtio_bus_get_device(bus);
0a122b
+    assert(vdev != NULL);
0a122b
+    return vdev->config_len;
0a122b
 }
0a122b
 
0a122b
 /* Get the features of the plugged device. */
0a122b
 uint32_t virtio_bus_get_vdev_features(VirtioBusState *bus,
0a122b
                                     uint32_t requested_features)
0a122b
 {
0a122b
+    VirtIODevice *vdev = virtio_bus_get_device(bus);
0a122b
     VirtioDeviceClass *k;
0a122b
-    assert(bus->vdev != NULL);
0a122b
-    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
0a122b
+
0a122b
+    assert(vdev != NULL);
0a122b
+    k = VIRTIO_DEVICE_GET_CLASS(vdev);
0a122b
     assert(k->get_features != NULL);
0a122b
-    return k->get_features(bus->vdev, requested_features);
0a122b
+    return k->get_features(vdev, requested_features);
0a122b
 }
0a122b
 
0a122b
 /* Set the features of the plugged device. */
0a122b
 void virtio_bus_set_vdev_features(VirtioBusState *bus,
0a122b
                                       uint32_t requested_features)
0a122b
 {
0a122b
+    VirtIODevice *vdev = virtio_bus_get_device(bus);
0a122b
     VirtioDeviceClass *k;
0a122b
-    assert(bus->vdev != NULL);
0a122b
-    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
0a122b
+
0a122b
+    assert(vdev != NULL);
0a122b
+    k = VIRTIO_DEVICE_GET_CLASS(vdev);
0a122b
     if (k->set_features != NULL) {
0a122b
-        k->set_features(bus->vdev, requested_features);
0a122b
+        k->set_features(vdev, requested_features);
0a122b
     }
0a122b
 }
0a122b
 
0a122b
 /* Get bad features of the plugged device. */
0a122b
 uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
0a122b
 {
0a122b
+    VirtIODevice *vdev = virtio_bus_get_device(bus);
0a122b
     VirtioDeviceClass *k;
0a122b
-    assert(bus->vdev != NULL);
0a122b
-    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
0a122b
+
0a122b
+    assert(vdev != NULL);
0a122b
+    k = VIRTIO_DEVICE_GET_CLASS(vdev);
0a122b
     if (k->bad_features != NULL) {
0a122b
-        return k->bad_features(bus->vdev);
0a122b
+        return k->bad_features(vdev);
0a122b
     } else {
0a122b
         return 0;
0a122b
     }
0a122b
@@ -133,22 +142,26 @@ uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
0a122b
 /* Get config of the plugged device. */
0a122b
 void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
0a122b
 {
0a122b
+    VirtIODevice *vdev = virtio_bus_get_device(bus);
0a122b
     VirtioDeviceClass *k;
0a122b
-    assert(bus->vdev != NULL);
0a122b
-    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
0a122b
+
0a122b
+    assert(vdev != NULL);
0a122b
+    k = VIRTIO_DEVICE_GET_CLASS(vdev);
0a122b
     if (k->get_config != NULL) {
0a122b
-        k->get_config(bus->vdev, config);
0a122b
+        k->get_config(vdev, config);
0a122b
     }
0a122b
 }
0a122b
 
0a122b
 /* Set config of the plugged device. */
0a122b
 void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
0a122b
 {
0a122b
+    VirtIODevice *vdev = virtio_bus_get_device(bus);
0a122b
     VirtioDeviceClass *k;
0a122b
-    assert(bus->vdev != NULL);
0a122b
-    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
0a122b
+
0a122b
+    assert(vdev != NULL);
0a122b
+    k = VIRTIO_DEVICE_GET_CLASS(vdev);
0a122b
     if (k->set_config != NULL) {
0a122b
-        k->set_config(bus->vdev, config);
0a122b
+        k->set_config(vdev, config);
0a122b
     }
0a122b
 }
0a122b
 
0a122b
diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
0a122b
index 6f5c434..1287e59 100644
0a122b
--- a/hw/virtio/virtio-pci.c
0a122b
+++ b/hw/virtio/virtio-pci.c
0a122b
@@ -948,7 +948,7 @@ static void virtio_pci_device_plugged(DeviceState *d)
0a122b
     uint8_t *config;
0a122b
     uint32_t size;
0a122b
 
0a122b
-    proxy->vdev = bus->vdev;
0a122b
+    proxy->vdev = virtio_bus_get_device(bus);
0a122b
 
0a122b
     config = proxy->pci_dev.config;
0a122b
     if (proxy->class_code) {
0a122b
diff --git a/include/hw/virtio/virtio-bus.h b/include/hw/virtio/virtio-bus.h
0a122b
index 9ed60f9..105ca6d 100644
0a122b
--- a/include/hw/virtio/virtio-bus.h
0a122b
+++ b/include/hw/virtio/virtio-bus.h
0a122b
@@ -66,10 +66,6 @@ typedef struct VirtioBusClass {
0a122b
 
0a122b
 struct VirtioBusState {
0a122b
     BusState parent_obj;
0a122b
-    /*
0a122b
-     * Only one VirtIODevice can be plugged on the bus.
0a122b
-     */
0a122b
-    VirtIODevice *vdev;
0a122b
 };
0a122b
 
0a122b
 int virtio_bus_plug_device(VirtIODevice *vdev);
0a122b
@@ -92,4 +88,16 @@ void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config);
0a122b
 /* Set config of the plugged device. */
0a122b
 void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config);
0a122b
 
0a122b
+static inline VirtIODevice *virtio_bus_get_device(VirtioBusState *bus)
0a122b
+{
0a122b
+    BusState *qbus = &bus->parent_obj;
0a122b
+    BusChild *kid = QTAILQ_FIRST(&qbus->children);
0a122b
+    DeviceState *qdev = kid ? kid->child : NULL;
0a122b
+
0a122b
+    /* This is used on the data path, the cast is guaranteed
0a122b
+     * to succeed by the qdev machinery.
0a122b
+     */
0a122b
+    return (VirtIODevice *)qdev;
0a122b
+}
0a122b
+
0a122b
 #endif /* VIRTIO_BUS_H */
0a122b
-- 
0a122b
1.7.1
0a122b