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