Blob Blame History Raw
From c15fbdd1e0b82b8ce257685896d0b16358ee45f8 Mon Sep 17 00:00:00 2001
Message-Id: <c15fbdd1e0b82b8ce257685896d0b16358ee45f8@dist-git>
From: Laine Stump <laine@laine.org>
Date: Thu, 13 Apr 2017 14:29:26 -0400
Subject: [PATCH] util: new function virPCIDeviceRebind()

This function unbinds a device from its driver, then immediately
rebinds it to its driver again. The code for this new function is just
the 2nd half of virPCIDeviceBindWithDriverOverride(), so that
function's 2nd half is replaced with a call to virPCIDeviceRebind().

Resolves: https://bugzilla.redhat.com/1442040 (RHEL 7.3.z)
Resolves: https://bugzilla.redhat.com/1415609 (RHEL 7.4)

(cherry picked from commit 251d179bf2c10c7d7fb594c68f1aac56a9b0d932)

Conflicts:
 src/util/virpci.c - virPCIDeviceBindWithDriverOverride()
   which calls the new function, doesn't exist yet on this branch.
---
 src/libvirt_private.syms |  1 +
 src/util/virpci.c        | 25 +++++++++++++++++++++++++
 src/util/virpci.h        |  1 +
 3 files changed, 27 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index b9ea1cfe9..b9e85449b 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2113,6 +2113,7 @@ virPCIDeviceListSteal;
 virPCIDeviceListStealIndex;
 virPCIDeviceNew;
 virPCIDeviceReattach;
+virPCIDeviceRebind;
 virPCIDeviceReset;
 virPCIDeviceSetManaged;
 virPCIDeviceSetRemoveSlot;
diff --git a/src/util/virpci.c b/src/util/virpci.c
index 7d9c81167..de90a82c2 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -1089,6 +1089,31 @@ virPCIDeviceUnbind(virPCIDevicePtr dev)
     return ret;
 }
 
+
+/**
+ * virPCIDeviceRebind:
+ *  @dev: virPCIDevice object describing the device to rebind
+ *
+ * unbind a device from its driver, then immediately rebind it.
+ *
+ * Returns 0 on success, -1 on failure
+ */
+int virPCIDeviceRebind(virPCIDevicePtr dev)
+{
+    if (virPCIDeviceUnbind(dev) < 0)
+        return -1;
+
+    if (virFileWriteStr(PCI_SYSFS "drivers_probe", dev->name, 0) < 0) {
+        virReportSystemError(errno,
+                             _("Failed to trigger a probe for PCI device '%s'"),
+                             dev->name);
+        return -1;
+    }
+
+    return 0;
+}
+
+
 static int
 virPCIDeviceUnbindFromStub(virPCIDevicePtr dev)
 {
diff --git a/src/util/virpci.h b/src/util/virpci.h
index c3cb2d679..afd85502c 100644
--- a/src/util/virpci.h
+++ b/src/util/virpci.h
@@ -224,6 +224,7 @@ int virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path,
                                  char **pfname, int *vf_index);
 
 int virPCIDeviceUnbind(virPCIDevicePtr dev);
+int virPCIDeviceRebind(virPCIDevicePtr dev);
 int virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev,
                                      char **path,
                                      char **name);
-- 
2.12.2