5d360b
From f81cc30fefd469f19b2f4550d4453a8aaff3239a Mon Sep 17 00:00:00 2001
5d360b
From: Alex Williamson <alex.williamson@redhat.com>
5d360b
Date: Fri, 29 Sep 2017 21:44:49 +0200
5d360b
Subject: [PATCH 06/27] hw/vfio/pci: handle reset at VFIODevice
5d360b
5d360b
RH-Author: Alex Williamson <alex.williamson@redhat.com>
5d360b
Message-id: <20170929214449.16765.43500.stgit@gimli.home>
5d360b
Patchwork-id: 76764
5d360b
O-Subject: [RHEL-7.5 qemu-kvm PATCH 06/16] hw/vfio/pci: handle reset at VFIODevice
5d360b
Bugzilla: 1494181
5d360b
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
5d360b
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
5d360b
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
5d360b
5d360b
From: Eric Auger <eric.auger@linaro.org>
5d360b
5d360b
Upstream: b47d8efa9f430c332bf96ce6eede169eb48422ad
5d360b
5d360b
Since we can potentially have both PCI and platform devices in
5d360b
the same VFIO group, this latter now owns a list of VFIODevices.
5d360b
A unified reset handler, vfio_reset_handler, is registered, looping
5d360b
through this VFIODevice list. 2 specialized operations are introduced
5d360b
(vfio_compute_needs_reset and vfio_hot_reset_multi): they allow to
5d360b
implement type specific behavior. also reset_works and needs_reset
5d360b
VFIOPCIDevice fields are moved into VFIODevice.
5d360b
5d360b
Signed-off-by: Eric Auger <eric.auger@linaro.org>
5d360b
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
5d360b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
5d360b
---
5d360b
 hw/misc/vfio.c | 95 +++++++++++++++++++++++++++++++++++++++-------------------
5d360b
 1 file changed, 64 insertions(+), 31 deletions(-)
5d360b
5d360b
diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c
5d360b
index cc151e2..3e559ed 100644
5d360b
--- a/hw/misc/vfio.c
5d360b
+++ b/hw/misc/vfio.c
5d360b
@@ -178,13 +178,24 @@ typedef struct VFIOMSIXInfo {
5d360b
     void *mmap;
5d360b
 } VFIOMSIXInfo;
5d360b
 
5d360b
+typedef struct VFIODeviceOps VFIODeviceOps;
5d360b
+
5d360b
 typedef struct VFIODevice {
5d360b
+    QLIST_ENTRY(VFIODevice) next;
5d360b
     struct VFIOGroup *group;
5d360b
     char *name;
5d360b
     int fd;
5d360b
     int type;
5d360b
+    bool reset_works;
5d360b
+    bool needs_reset;
5d360b
+    VFIODeviceOps *ops;
5d360b
 } VFIODevice;
5d360b
 
5d360b
+struct VFIODeviceOps {
5d360b
+    void (*vfio_compute_needs_reset)(VFIODevice *vdev);
5d360b
+    int (*vfio_hot_reset_multi)(VFIODevice *vdev);
5d360b
+};
5d360b
+
5d360b
 typedef struct VFIOPCIDevice {
5d360b
     PCIDevice pdev;
5d360b
     VFIODevice vbasedev;
5d360b
@@ -203,7 +214,6 @@ typedef struct VFIOPCIDevice {
5d360b
     VFIOBAR bars[PCI_NUM_REGIONS - 1]; /* No ROM */
5d360b
     VFIOVGA vga; /* 0xa0000, 0x3b0, 0x3c0 */
5d360b
     PCIHostDeviceAddress host;
5d360b
-    QLIST_ENTRY(VFIOPCIDevice) next;
5d360b
     EventNotifier err_notifier;
5d360b
     EventNotifier req_notifier;
5d360b
     uint32_t features;
5d360b
@@ -213,13 +223,11 @@ typedef struct VFIOPCIDevice {
5d360b
 #define VFIO_FEATURE_ENABLE_REQ (1 << VFIO_FEATURE_ENABLE_REQ_BIT)
5d360b
     int32_t bootindex;
5d360b
     uint8_t pm_cap;
5d360b
-    bool reset_works;
5d360b
     bool has_vga;
5d360b
     bool pci_aer;
5d360b
     bool req_enabled;
5d360b
     bool has_flr;
5d360b
     bool has_pm_reset;
5d360b
-    bool needs_reset;
5d360b
     bool rom_read_failed;
5d360b
 } VFIOPCIDevice;
5d360b
 
5d360b
@@ -227,7 +235,7 @@ typedef struct VFIOGroup {
5d360b
     int fd;
5d360b
     int groupid;
5d360b
     VFIOContainer *container;
5d360b
-    QLIST_HEAD(, VFIOPCIDevice) device_list;
5d360b
+    QLIST_HEAD(, VFIODevice) device_list;
5d360b
     QLIST_ENTRY(VFIOGroup) next;
5d360b
     QLIST_ENTRY(VFIOGroup) container_next;
5d360b
 } VFIOGroup;
5d360b
@@ -3064,7 +3072,7 @@ static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single)
5d360b
             single ? "one" : "multi");
5d360b
 
5d360b
     vfio_pci_pre_reset(vdev);
5d360b
-    vdev->needs_reset = false;
5d360b
+    vdev->vbasedev.needs_reset = false;
5d360b
 
5d360b
     info = g_malloc0(sizeof(*info));
5d360b
     info->argsz = sizeof(*info);
5d360b
@@ -3100,6 +3108,7 @@ static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single)
5d360b
     for (i = 0; i < info->count; i++) {
5d360b
         PCIHostDeviceAddress host;
5d360b
         VFIOPCIDevice *tmp;
5d360b
+        VFIODevice *vbasedev_iter;
5d360b
 
5d360b
         host.domain = devices[i].segment;
5d360b
         host.bus = devices[i].bus;
5d360b
@@ -3131,7 +3140,11 @@ static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single)
5d360b
         }
5d360b
 
5d360b
         /* Prep dependent devices for reset and clear our marker. */
5d360b
-        QLIST_FOREACH(tmp, &group->device_list, next) {
5d360b
+        QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
5d360b
+            if (vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
5d360b
+                continue;
5d360b
+            }
5d360b
+            tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
5d360b
             if (vfio_pci_host_match(&host, &tmp->host)) {
5d360b
                 if (single) {
5d360b
                     DPRINTF("vfio: found another in-use device "
5d360b
@@ -3141,7 +3154,7 @@ static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single)
5d360b
                     goto out_single;
5d360b
                 }
5d360b
                 vfio_pci_pre_reset(tmp);
5d360b
-                tmp->needs_reset = false;
5d360b
+                tmp->vbasedev.needs_reset = false;
5d360b
                 multi = true;
5d360b
                 break;
5d360b
             }
5d360b
@@ -3192,6 +3205,7 @@ out:
5d360b
     for (i = 0; i < info->count; i++) {
5d360b
         PCIHostDeviceAddress host;
5d360b
         VFIOPCIDevice *tmp;
5d360b
+        VFIODevice *vbasedev_iter;
5d360b
 
5d360b
         host.domain = devices[i].segment;
5d360b
         host.bus = devices[i].bus;
5d360b
@@ -3212,7 +3226,11 @@ out:
5d360b
             break;
5d360b
         }
5d360b
 
5d360b
-        QLIST_FOREACH(tmp, &group->device_list, next) {
5d360b
+        QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
5d360b
+            if (vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
5d360b
+                continue;
5d360b
+            }
5d360b
+            tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
5d360b
             if (vfio_pci_host_match(&host, &tmp->host)) {
5d360b
                 vfio_pci_post_reset(tmp);
5d360b
                 break;
5d360b
@@ -3246,28 +3264,40 @@ static int vfio_pci_hot_reset_one(VFIOPCIDevice *vdev)
5d360b
     return vfio_pci_hot_reset(vdev, true);
5d360b
 }
5d360b
 
5d360b
-static int vfio_pci_hot_reset_multi(VFIOPCIDevice *vdev)
5d360b
+static int vfio_pci_hot_reset_multi(VFIODevice *vbasedev)
5d360b
 {
5d360b
+    VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
5d360b
     return vfio_pci_hot_reset(vdev, false);
5d360b
 }
5d360b
 
5d360b
-static void vfio_pci_reset_handler(void *opaque)
5d360b
+static void vfio_pci_compute_needs_reset(VFIODevice *vbasedev)
5d360b
+{
5d360b
+    VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
5d360b
+    if (!vbasedev->reset_works || (!vdev->has_flr && vdev->has_pm_reset)) {
5d360b
+        vbasedev->needs_reset = true;
5d360b
+    }
5d360b
+}
5d360b
+
5d360b
+static VFIODeviceOps vfio_pci_ops = {
5d360b
+    .vfio_compute_needs_reset = vfio_pci_compute_needs_reset,
5d360b
+    .vfio_hot_reset_multi = vfio_pci_hot_reset_multi,
5d360b
+};
5d360b
+
5d360b
+static void vfio_reset_handler(void *opaque)
5d360b
 {
5d360b
     VFIOGroup *group;
5d360b
-    VFIOPCIDevice *vdev;
5d360b
+    VFIODevice *vbasedev;
5d360b
 
5d360b
     QLIST_FOREACH(group, &group_list, next) {
5d360b
-        QLIST_FOREACH(vdev, &group->device_list, next) {
5d360b
-            if (!vdev->reset_works || (!vdev->has_flr && vdev->has_pm_reset)) {
5d360b
-                vdev->needs_reset = true;
5d360b
-            }
5d360b
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
5d360b
+            vbasedev->ops->vfio_compute_needs_reset(vbasedev);
5d360b
         }
5d360b
     }
5d360b
 
5d360b
     QLIST_FOREACH(group, &group_list, next) {
5d360b
-        QLIST_FOREACH(vdev, &group->device_list, next) {
5d360b
-            if (vdev->needs_reset) {
5d360b
-                vfio_pci_hot_reset_multi(vdev);
5d360b
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
5d360b
+            if (vbasedev->needs_reset) {
5d360b
+                vbasedev->ops->vfio_hot_reset_multi(vbasedev);
5d360b
             }
5d360b
         }
5d360b
     }
5d360b
@@ -3486,7 +3516,7 @@ static VFIOGroup *vfio_get_group(int groupid)
5d360b
     }
5d360b
 
5d360b
     if (QLIST_EMPTY(&group_list)) {
5d360b
-        qemu_register_reset(vfio_pci_reset_handler, NULL);
5d360b
+        qemu_register_reset(vfio_reset_handler, NULL);
5d360b
     }
5d360b
 
5d360b
     QLIST_INSERT_HEAD(&group_list, group, next);
5d360b
@@ -3510,7 +3540,7 @@ static void vfio_put_group(VFIOGroup *group)
5d360b
     g_free(group);
5d360b
 
5d360b
     if (QLIST_EMPTY(&group_list)) {
5d360b
-        qemu_unregister_reset(vfio_pci_reset_handler, NULL);
5d360b
+        qemu_unregister_reset(vfio_reset_handler, NULL);
5d360b
     }
5d360b
 }
5d360b
 
5d360b
@@ -3533,7 +3563,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name,
5d360b
 
5d360b
     vdev->vbasedev.fd = ret;
5d360b
     vdev->vbasedev.group = group;
5d360b
-    QLIST_INSERT_HEAD(&group->device_list, vdev, next);
5d360b
+    QLIST_INSERT_HEAD(&group->device_list, &vdev->vbasedev, next);
5d360b
 
5d360b
     /* Sanity check device */
5d360b
     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_INFO, &dev_info);
5d360b
@@ -3550,7 +3580,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name,
5d360b
         goto error;
5d360b
     }
5d360b
 
5d360b
-    vdev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
5d360b
+    vdev->vbasedev.reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
5d360b
 
5d360b
     if (dev_info.num_regions < VFIO_PCI_CONFIG_REGION_INDEX + 1) {
5d360b
         error_report("vfio: unexpected number of io regions %u",
5d360b
@@ -3663,7 +3693,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name,
5d360b
 
5d360b
 error:
5d360b
     if (ret) {
5d360b
-        QLIST_REMOVE(vdev, next);
5d360b
+        QLIST_REMOVE(&vdev->vbasedev, next);
5d360b
         vdev->vbasedev.group = NULL;
5d360b
         close(vdev->vbasedev.fd);
5d360b
     }
5d360b
@@ -3672,7 +3702,7 @@ error:
5d360b
 
5d360b
 static void vfio_put_device(VFIOPCIDevice *vdev)
5d360b
 {
5d360b
-    QLIST_REMOVE(vdev, next);
5d360b
+    QLIST_REMOVE(&vdev->vbasedev, next);
5d360b
     vdev->vbasedev.group = NULL;
5d360b
     DPRINTF("vfio_put_device: close vdev->vbasedev.fd\n");
5d360b
     close(vdev->vbasedev.fd);
5d360b
@@ -3881,7 +3911,8 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
5d360b
 
5d360b
 static int vfio_initfn(PCIDevice *pdev)
5d360b
 {
5d360b
-    VFIOPCIDevice *pvdev, *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
5d360b
+    VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
5d360b
+    VFIODevice *vbasedev_iter;
5d360b
     VFIOGroup *group;
5d360b
     char path[PATH_MAX], iommu_group_path[PATH_MAX], *group_name;
5d360b
     ssize_t len;
5d360b
@@ -3890,7 +3921,7 @@ static int vfio_initfn(PCIDevice *pdev)
5d360b
     int ret, i = 0;
5d360b
 
5d360b
     QLIST_FOREACH(group, &group_list, next) {
5d360b
-        QLIST_FOREACH(pvdev, &group->device_list, next) {
5d360b
+        QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
5d360b
             i++;
5d360b
         }
5d360b
     }
5d360b
@@ -3911,6 +3942,8 @@ static int vfio_initfn(PCIDevice *pdev)
5d360b
         return -errno;
5d360b
     }
5d360b
 
5d360b
+    vdev->vbasedev.ops = &vfio_pci_ops;
5d360b
+
5d360b
     vdev->vbasedev.type = VFIO_DEVICE_TYPE_PCI;
5d360b
     vdev->vbasedev.name = g_strdup_printf("%04x:%02x:%02x.%01x",
5d360b
                                           vdev->host.domain, vdev->host.bus,
5d360b
@@ -3945,9 +3978,8 @@ static int vfio_initfn(PCIDevice *pdev)
5d360b
             vdev->host.domain, vdev->host.bus, vdev->host.slot,
5d360b
             vdev->host.function);
5d360b
 
5d360b
-    QLIST_FOREACH(pvdev, &group->device_list, next) {
5d360b
-        if (strcmp(pvdev->vbasedev.name, vdev->vbasedev.name) == 0) {
5d360b
-
5d360b
+    QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
5d360b
+        if (strcmp(vbasedev_iter->name, vdev->vbasedev.name) == 0) {
5d360b
             error_report("vfio: error: device %s is already attached", path);
5d360b
             vfio_put_group(group);
5d360b
             return -EBUSY;
5d360b
@@ -4078,7 +4110,8 @@ static void vfio_pci_reset(DeviceState *dev)
5d360b
 
5d360b
     vfio_pci_pre_reset(vdev);
5d360b
 
5d360b
-    if (vdev->reset_works && (vdev->has_flr || !vdev->has_pm_reset) &&
5d360b
+    if (vdev->vbasedev.reset_works &&
5d360b
+        (vdev->has_flr || !vdev->has_pm_reset) &&
5d360b
         !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) {
5d360b
         DPRINTF("%04x:%02x:%02x.%x FLR/VFIO_DEVICE_RESET\n", vdev->host.domain,
5d360b
             vdev->host.bus, vdev->host.slot, vdev->host.function);
5d360b
@@ -4091,7 +4124,7 @@ static void vfio_pci_reset(DeviceState *dev)
5d360b
     }
5d360b
 
5d360b
     /* If nothing else works and the device supports PM reset, use it */
5d360b
-    if (vdev->reset_works && vdev->has_pm_reset &&
5d360b
+    if (vdev->vbasedev.reset_works && vdev->has_pm_reset &&
5d360b
         !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) {
5d360b
         DPRINTF("%04x:%02x:%02x.%x PCI PM Reset\n", vdev->host.domain,
5d360b
             vdev->host.bus, vdev->host.slot, vdev->host.function);
5d360b
-- 
5d360b
1.8.3.1
5d360b