9ae3a8
From 39ea2d15631b21f962924e88b62e3586e531ac66 Mon Sep 17 00:00:00 2001
9ae3a8
From: Markus Armbruster <armbru@redhat.com>
9ae3a8
Date: Thu, 23 Jan 2014 14:03:33 +0100
9ae3a8
Subject: [PATCH 06/14] virtio-bus: cleanup plug/unplug interface
9ae3a8
9ae3a8
RH-Author: Markus Armbruster <armbru@redhat.com>
9ae3a8
Message-id: <1390485820-7585-4-git-send-email-armbru@redhat.com>
9ae3a8
Patchwork-id: 56924
9ae3a8
O-Subject: [PATCH 7.0 qemu-kvm 03/10] virtio-bus: cleanup plug/unplug interface
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
Right now we have these pairs:
9ae3a8
9ae3a8
- virtio_bus_plug_device/virtio_bus_destroy_device.  The first
9ae3a8
  takes a VirtIODevice, the second takes a VirtioBusState
9ae3a8
9ae3a8
- device_plugged/device_unplug callbacks in the VirtioBusClass
9ae3a8
  (here it's just the naming that is inconsistent)
9ae3a8
9ae3a8
- virtio_bus_destroy_device is not called by anyone (and since
9ae3a8
  it calls qdev_free, it would be called by the proxies---but
9ae3a8
  then the callback is useless since the proxies can do whatever
9ae3a8
  they want before calling virtio_bus_destroy_device)
9ae3a8
9ae3a8
And there is a k->init but no k->exit, hence virtio_device_exit is
9ae3a8
overwritten by subclasses (except virtio-9p).  This cleans it up by:
9ae3a8
9ae3a8
- renaming the device_unplug callback to device_unplugged
9ae3a8
9ae3a8
- renaming virtio_bus_plug_device to virtio_bus_device_plugged,
9ae3a8
  matching the callback name
9ae3a8
9ae3a8
- renaming virtio_bus_destroy_device to virtio_bus_device_unplugged,
9ae3a8
  removing the qdev_free, making it take a VirtIODevice and calling it
9ae3a8
  from virtio_device_exit
9ae3a8
9ae3a8
- adding a k->exit callback
9ae3a8
9ae3a8
virtio_device_exit is still overwritten, the next patches will fix that.
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 5e96f5d2f8d2696ef7d2d8d7282c18fa6023470b)
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	include/hw/virtio/virtio-bus.h
9ae3a8
9ae3a8
Trivially conflicts because we lack commit 6ce69d1 "virtio: Support
9ae3a8
transports which can specify the vring alignment".
9ae3a8
Signed-off-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
---
9ae3a8
 hw/virtio/virtio-bus.c         | 19 +++++++++----------
9ae3a8
 hw/virtio/virtio.c             |  7 ++++++-
9ae3a8
 include/hw/virtio/virtio-bus.h |  6 +++---
9ae3a8
 include/hw/virtio/virtio.h     |  1 +
9ae3a8
 4 files changed, 19 insertions(+), 14 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 hw/virtio/virtio-bus.c         |   19 +++++++++----------
9ae3a8
 hw/virtio/virtio.c             |    7 ++++++-
9ae3a8
 include/hw/virtio/virtio-bus.h |    6 +++---
9ae3a8
 include/hw/virtio/virtio.h     |    1 +
9ae3a8
 4 files changed, 19 insertions(+), 14 deletions(-)
9ae3a8
9ae3a8
diff --git a/hw/virtio/virtio-bus.c b/hw/virtio/virtio-bus.c
9ae3a8
index 17dd06e..eb77019 100644
9ae3a8
--- a/hw/virtio/virtio-bus.c
9ae3a8
+++ b/hw/virtio/virtio-bus.c
9ae3a8
@@ -37,8 +37,8 @@ do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
9ae3a8
 #define DPRINTF(fmt, ...) do { } while (0)
9ae3a8
 #endif
9ae3a8
 
9ae3a8
-/* Plug the VirtIODevice */
9ae3a8
-int virtio_bus_plug_device(VirtIODevice *vdev)
9ae3a8
+/* A VirtIODevice is being plugged */
9ae3a8
+int virtio_bus_device_plugged(VirtIODevice *vdev)
9ae3a8
 {
9ae3a8
     DeviceState *qdev = DEVICE(vdev);
9ae3a8
     BusState *qbus = BUS(qdev_get_parent_bus(qdev));
9ae3a8
@@ -64,20 +64,19 @@ void virtio_bus_reset(VirtioBusState *bus)
9ae3a8
     }
9ae3a8
 }
9ae3a8
 
9ae3a8
-/* Destroy the VirtIODevice */
9ae3a8
-void virtio_bus_destroy_device(VirtioBusState *bus)
9ae3a8
+/* A VirtIODevice is being unplugged */
9ae3a8
+void virtio_bus_device_unplugged(VirtIODevice *vdev)
9ae3a8
 {
9ae3a8
-    BusState *qbus = BUS(bus);
9ae3a8
-    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
9ae3a8
-    VirtIODevice *vdev = virtio_bus_get_device(bus);
9ae3a8
+    DeviceState *qdev = DEVICE(vdev);
9ae3a8
+    BusState *qbus = BUS(qdev_get_parent_bus(qdev));
9ae3a8
+    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
9ae3a8
 
9ae3a8
     DPRINTF("%s: remove device.\n", qbus->name);
9ae3a8
 
9ae3a8
     if (vdev != NULL) {
9ae3a8
-        if (klass->device_unplug != NULL) {
9ae3a8
-            klass->device_unplug(qbus->parent);
9ae3a8
+        if (klass->device_unplugged != NULL) {
9ae3a8
+            klass->device_unplugged(qbus->parent);
9ae3a8
         }
9ae3a8
-        object_unparent(OBJECT(vdev));
9ae3a8
     }
9ae3a8
 }
9ae3a8
 
9ae3a8
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
9ae3a8
index a5251cb..b5bb0b6 100644
9ae3a8
--- a/hw/virtio/virtio.c
9ae3a8
+++ b/hw/virtio/virtio.c
9ae3a8
@@ -1118,14 +1118,19 @@ static int virtio_device_init(DeviceState *qdev)
9ae3a8
     if (k->init(vdev) < 0) {
9ae3a8
         return -1;
9ae3a8
     }
9ae3a8
-    virtio_bus_plug_device(vdev);
9ae3a8
+    virtio_bus_device_plugged(vdev);
9ae3a8
     return 0;
9ae3a8
 }
9ae3a8
 
9ae3a8
 static int virtio_device_exit(DeviceState *qdev)
9ae3a8
 {
9ae3a8
     VirtIODevice *vdev = VIRTIO_DEVICE(qdev);
9ae3a8
+    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(qdev);
9ae3a8
 
9ae3a8
+    virtio_bus_device_unplugged(vdev);
9ae3a8
+    if (k->exit) {
9ae3a8
+        k->exit(vdev);
9ae3a8
+    }
9ae3a8
     if (vdev->bus_name) {
9ae3a8
         g_free(vdev->bus_name);
9ae3a8
         vdev->bus_name = NULL;
9ae3a8
diff --git a/include/hw/virtio/virtio-bus.h b/include/hw/virtio/virtio-bus.h
9ae3a8
index 105ca6d..228c9d7 100644
9ae3a8
--- a/include/hw/virtio/virtio-bus.h
9ae3a8
+++ b/include/hw/virtio/virtio-bus.h
9ae3a8
@@ -61,16 +61,16 @@ typedef struct VirtioBusClass {
9ae3a8
      * transport independent exit function.
9ae3a8
      * This is called by virtio-bus just before the device is unplugged.
9ae3a8
      */
9ae3a8
-    void (*device_unplug)(DeviceState *d);
9ae3a8
+    void (*device_unplugged)(DeviceState *d);
9ae3a8
 } VirtioBusClass;
9ae3a8
 
9ae3a8
 struct VirtioBusState {
9ae3a8
     BusState parent_obj;
9ae3a8
 };
9ae3a8
 
9ae3a8
-int virtio_bus_plug_device(VirtIODevice *vdev);
9ae3a8
+int virtio_bus_device_plugged(VirtIODevice *vdev);
9ae3a8
 void virtio_bus_reset(VirtioBusState *bus);
9ae3a8
-void virtio_bus_destroy_device(VirtioBusState *bus);
9ae3a8
+void virtio_bus_device_unplugged(VirtIODevice *bus);
9ae3a8
 /* Get the device id of the plugged device. */
9ae3a8
 uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus);
9ae3a8
 /* Get the config_len field of the plugged device. */
9ae3a8
diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
9ae3a8
index f2aa6a3..cd886c3 100644
9ae3a8
--- a/include/hw/virtio/virtio.h
9ae3a8
+++ b/include/hw/virtio/virtio.h
9ae3a8
@@ -125,6 +125,7 @@ typedef struct VirtioDeviceClass {
9ae3a8
     /* This is what a VirtioDevice must implement */
9ae3a8
     DeviceClass parent;
9ae3a8
     int (*init)(VirtIODevice *vdev);
9ae3a8
+    void (*exit)(VirtIODevice *vdev);
9ae3a8
     uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
9ae3a8
     uint32_t (*bad_features)(VirtIODevice *vdev);
9ae3a8
     void (*set_features)(VirtIODevice *vdev, uint32_t val);
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8