c401cc
From 536862d41ed96ec17bbcbaddbaad1bac88ed0d60 Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <536862d41ed96ec17bbcbaddbaad1bac88ed0d60.1390394206.git.jdenemar@redhat.com>
c401cc
From: Jiri Denemark <jdenemar@redhat.com>
c401cc
Date: Mon, 20 Jan 2014 14:41:11 +0100
c401cc
Subject: [PATCH] pci: Make reattach work for unbound devices
c401cc
c401cc
https://bugzilla.redhat.com/show_bug.cgi?id=1046919
c401cc
c401cc
When a PCI device is not bound to any driver, reattach should just
c401cc
trigger driver probe rather than failing with
c401cc
c401cc
    Invalid device 0000:00:19.0 driver file
c401cc
    /sys/bus/pci/devices/0000:00:19.0/driver is not a symlink
c401cc
c401cc
While virPCIDeviceGetDriverPathAndName was documented to return success
c401cc
and NULL driver and path when a device is not attached to any driver but
c401cc
didn't do so. Thus callers could not distinguish unbound devices from
c401cc
failures.
c401cc
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
(cherry picked from commit c982e5e84f47d3e71d794bb034ef2489091f41c6)
c401cc
---
c401cc
 src/util/virpci.c | 15 ++++++++++++---
c401cc
 1 file changed, 12 insertions(+), 3 deletions(-)
c401cc
c401cc
diff --git a/src/util/virpci.c b/src/util/virpci.c
c401cc
index 39bbac8..b48e433 100644
c401cc
--- a/src/util/virpci.c
c401cc
+++ b/src/util/virpci.c
c401cc
@@ -236,6 +236,11 @@ virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, char **path, char **name)
c401cc
     if (virPCIFile(&drvlink, dev->name, "driver") < 0)
c401cc
         goto cleanup;
c401cc
 
c401cc
+    if (!virFileExists(drvlink)) {
c401cc
+        ret = 0;
c401cc
+        goto cleanup;
c401cc
+    }
c401cc
+
c401cc
     if (virFileIsLink(drvlink) != 1) {
c401cc
         virReportError(VIR_ERR_INTERNAL_ERROR,
c401cc
                        _("Invalid device %s driver file %s is not a symlink"),
c401cc
@@ -991,6 +996,11 @@ virPCIDeviceUnbindFromStub(virPCIDevicePtr dev)
c401cc
     if (virPCIDeviceGetDriverPathAndName(dev, &drvdir, &driver) < 0)
c401cc
         goto cleanup;
c401cc
 
c401cc
+    if (!driver) {
c401cc
+        /* The device is not bound to any driver and we are almost done. */
c401cc
+        goto reprobe;
c401cc
+    }
c401cc
+
c401cc
     if (!dev->unbind_from_stub)
c401cc
         goto remove_slot;
c401cc
 
c401cc
@@ -1047,11 +1057,10 @@ reprobe:
c401cc
      * available, then re-probing would just cause the device to be
c401cc
      * re-bound to the stub.
c401cc
      */
c401cc
-    if (virPCIDriverFile(&path, driver, "remove_id") < 0) {
c401cc
+    if (driver && virPCIDriverFile(&path, driver, "remove_id") < 0)
c401cc
         goto cleanup;
c401cc
-    }
c401cc
 
c401cc
-    if (!virFileExists(drvdir) || virFileExists(path)) {
c401cc
+    if (!driver || !virFileExists(drvdir) || virFileExists(path)) {
c401cc
         if (virFileWriteStr(PCI_SYSFS "drivers_probe", dev->name, 0) < 0) {
c401cc
             virReportSystemError(errno,
c401cc
                                  _("Failed to trigger a re-probe for PCI device '%s'"),
c401cc
-- 
c401cc
1.8.5.3
c401cc