cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-hw-vfio-pci-add-type-name-and-group-fields-in-VFIODe.patch

9ae3a8
From dde4b959d6722bc2ebc5cd247b4e21055d2f0abd Mon Sep 17 00:00:00 2001
9ae3a8
From: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
Date: Fri, 29 Sep 2017 21:44:35 +0200
9ae3a8
Subject: [PATCH 05/27] hw/vfio/pci: add type, name and group fields in
9ae3a8
 VFIODevice
9ae3a8
9ae3a8
RH-Author: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
Message-id: <20170929214435.16765.27045.stgit@gimli.home>
9ae3a8
Patchwork-id: 76763
9ae3a8
O-Subject: [RHEL-7.5 qemu-kvm PATCH 05/16] hw/vfio/pci: add type, name and group fields in VFIODevice
9ae3a8
Bugzilla: 1494181
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
From: Eric Auger <eric.auger@linaro.org>
9ae3a8
9ae3a8
Upstream: 462037c9e85b27149e71d7f5c7f41375ca6e47d5
9ae3a8
9ae3a8
Add 3 new fields in the VFIODevice struct. Type is set to
9ae3a8
VFIO_DEVICE_TYPE_PCI. The type enum value will later be used
9ae3a8
to discriminate between VFIO PCI and platform devices. The name is
9ae3a8
set to domain:bus:slot:function. Currently used to test whether
9ae3a8
the device already is attached to the group. Later on, the name
9ae3a8
will be used to simplify all traces. The group is simply moved
9ae3a8
from VFIOPCIDevice to VFIODevice.
9ae3a8
9ae3a8
Signed-off-by: Eric Auger <eric.auger@linaro.org>
9ae3a8
[Fix g_strdup_printf() usage]
9ae3a8
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 hw/misc/vfio.c | 27 ++++++++++++++++++---------
9ae3a8
 1 file changed, 18 insertions(+), 9 deletions(-)
9ae3a8
9ae3a8
diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c
9ae3a8
index 340d967..cc151e2 100644
9ae3a8
--- a/hw/misc/vfio.c
9ae3a8
+++ b/hw/misc/vfio.c
9ae3a8
@@ -55,6 +55,10 @@
9ae3a8
 #define VFIO_ALLOW_KVM_MSI 1
9ae3a8
 #define VFIO_ALLOW_KVM_MSIX 1
9ae3a8
 
9ae3a8
+enum {
9ae3a8
+    VFIO_DEVICE_TYPE_PCI = 0,
9ae3a8
+};
9ae3a8
+
9ae3a8
 struct VFIOPCIDevice;
9ae3a8
 
9ae3a8
 typedef struct VFIOQuirk {
9ae3a8
@@ -175,7 +179,10 @@ typedef struct VFIOMSIXInfo {
9ae3a8
 } VFIOMSIXInfo;
9ae3a8
 
9ae3a8
 typedef struct VFIODevice {
9ae3a8
+    struct VFIOGroup *group;
9ae3a8
+    char *name;
9ae3a8
     int fd;
9ae3a8
+    int type;
9ae3a8
 } VFIODevice;
9ae3a8
 
9ae3a8
 typedef struct VFIOPCIDevice {
9ae3a8
@@ -197,7 +204,6 @@ typedef struct VFIOPCIDevice {
9ae3a8
     VFIOVGA vga; /* 0xa0000, 0x3b0, 0x3c0 */
9ae3a8
     PCIHostDeviceAddress host;
9ae3a8
     QLIST_ENTRY(VFIOPCIDevice) next;
9ae3a8
-    struct VFIOGroup *group;
9ae3a8
     EventNotifier err_notifier;
9ae3a8
     EventNotifier req_notifier;
9ae3a8
     uint32_t features;
9ae3a8
@@ -3526,7 +3532,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name,
9ae3a8
     }
9ae3a8
 
9ae3a8
     vdev->vbasedev.fd = ret;
9ae3a8
-    vdev->group = group;
9ae3a8
+    vdev->vbasedev.group = group;
9ae3a8
     QLIST_INSERT_HEAD(&group->device_list, vdev, next);
9ae3a8
 
9ae3a8
     /* Sanity check device */
9ae3a8
@@ -3658,7 +3664,7 @@ static int vfio_get_device(VFIOGroup *group, const char *name,
9ae3a8
 error:
9ae3a8
     if (ret) {
9ae3a8
         QLIST_REMOVE(vdev, next);
9ae3a8
-        vdev->group = NULL;
9ae3a8
+        vdev->vbasedev.group = NULL;
9ae3a8
         close(vdev->vbasedev.fd);
9ae3a8
     }
9ae3a8
     return ret;
9ae3a8
@@ -3667,9 +3673,10 @@ error:
9ae3a8
 static void vfio_put_device(VFIOPCIDevice *vdev)
9ae3a8
 {
9ae3a8
     QLIST_REMOVE(vdev, next);
9ae3a8
-    vdev->group = NULL;
9ae3a8
+    vdev->vbasedev.group = NULL;
9ae3a8
     DPRINTF("vfio_put_device: close vdev->vbasedev.fd\n");
9ae3a8
     close(vdev->vbasedev.fd);
9ae3a8
+    g_free(vdev->vbasedev.name);
9ae3a8
     if (vdev->msix) {
9ae3a8
         g_free(vdev->msix);
9ae3a8
         vdev->msix = NULL;
9ae3a8
@@ -3904,6 +3911,11 @@ static int vfio_initfn(PCIDevice *pdev)
9ae3a8
         return -errno;
9ae3a8
     }
9ae3a8
 
9ae3a8
+    vdev->vbasedev.type = VFIO_DEVICE_TYPE_PCI;
9ae3a8
+    vdev->vbasedev.name = g_strdup_printf("%04x:%02x:%02x.%01x",
9ae3a8
+                                          vdev->host.domain, vdev->host.bus,
9ae3a8
+                                          vdev->host.slot, vdev->host.function);
9ae3a8
+
9ae3a8
     strncat(path, "iommu_group", sizeof(path) - strlen(path) - 1);
9ae3a8
 
9ae3a8
     len = readlink(path, iommu_group_path, sizeof(path));
9ae3a8
@@ -3934,10 +3946,7 @@ static int vfio_initfn(PCIDevice *pdev)
9ae3a8
             vdev->host.function);
9ae3a8
 
9ae3a8
     QLIST_FOREACH(pvdev, &group->device_list, next) {
9ae3a8
-        if (pvdev->host.domain == vdev->host.domain &&
9ae3a8
-            pvdev->host.bus == vdev->host.bus &&
9ae3a8
-            pvdev->host.slot == vdev->host.slot &&
9ae3a8
-            pvdev->host.function == vdev->host.function) {
9ae3a8
+        if (strcmp(pvdev->vbasedev.name, vdev->vbasedev.name) == 0) {
9ae3a8
 
9ae3a8
             error_report("vfio: error: device %s is already attached", path);
9ae3a8
             vfio_put_group(group);
9ae3a8
@@ -4042,7 +4051,7 @@ out_put:
9ae3a8
 static void vfio_exitfn(PCIDevice *pdev)
9ae3a8
 {
9ae3a8
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
9ae3a8
-    VFIOGroup *group = vdev->group;
9ae3a8
+    VFIOGroup *group = vdev->vbasedev.group;
9ae3a8
 
9ae3a8
     vfio_unregister_req_notifier(vdev);
9ae3a8
     vfio_unregister_err_notifier(vdev);
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8