From: Shivaprasad G Bhat <sb...@linux.vnet.ibm.com>

The hostdevices are the only devices which have dependencies
outside of themselves such that, other functions of the PCI
card should also have been detached from host driver before
attempting the hotplug.

This patch moves the detach to the beginning of the hotplug
so that the following patch can detach all functions first before
attempting to hotplug any.

We don't need to move the detach for net devices using SRIOV as
all SRIOV devices are single function devices and can be independently
detached as usual.

Signed-off-by: Shivaprasad G Bhat <sb...@linux.vnet.ibm.com>
Signed-off-by: Daniel Henrique Barboza <danielhb...@gmail.com>
---
 src/qemu/qemu_hotplug.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 172747b2e2..d88a0922d4 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -1222,6 +1222,7 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
     g_autofree char *netdev_name = NULL;
     g_autoptr(virConnect) conn = NULL;
     virErrorPtr save_err = NULL;
+    virDomainHostdevDefPtr hostdev = NULL;
 
     /* preallocate new slot for device */
     if (VIR_REALLOC_N(vm->def->nets, vm->def->nnets + 1) < 0)
@@ -1252,9 +1253,18 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
          * as a hostdev (the hostdev code will reach over into the
          * netdev-specific code as appropriate), then also added to
          * the nets list (see cleanup:) if successful.
+         *
+         * qemuDomainAttachHostDevice uses a connection to resolve
+         * a SCSI hostdev secret, which is not this case, so pass NULL.
          */
-        ret = qemuDomainAttachHostDevice(driver, vm,
-                                         virDomainNetGetActualHostdev(net));
+        hostdev = virDomainNetGetActualHostdev(net);
+        if (qemuDomainAttachPCIHostDevicePrepare(driver, vm->def,
+                                                 hostdev, priv->qemuCaps) < 0)
+            goto cleanup;
+
+        if ((ret = qemuDomainAttachHostDevice(driver, vm, hostdev)) < 0)
+            qemuHostdevReAttachPCIDevices(driver, vm->def->name, &hostdev, 1);
+
         goto cleanup;
     }
 
@@ -1610,10 +1620,6 @@ qemuDomainAttachHostPCIDevice(virQEMUDriverPtr driver,
     if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs + 1) < 0)
         return -1;
 
-    if (qemuDomainAttachPCIHostDevicePrepare(driver, vm->def,
-                                             hostdev, priv->qemuCaps) < 0)
-        return -1;
-
     /* this could have been changed by qemuHostdevPreparePCIDevices */
     backend = hostdev->source.subsys.u.pci.backend;
 
@@ -1689,8 +1695,6 @@ qemuDomainAttachHostPCIDevice(virQEMUDriverPtr driver,
     if (releaseaddr)
         qemuDomainReleaseDeviceAddress(vm, info);
 
-    qemuHostdevReAttachPCIDevices(driver, vm->def->name, &hostdev, 1);
-
     return -1;
 }
 
@@ -2929,6 +2933,8 @@ qemuDomainAttachHostDevice(virQEMUDriverPtr driver,
                            virDomainObjPtr vm,
                            virDomainHostdevDefPtr hostdev)
 {
+    qemuDomainObjPrivatePtr priv = vm->privateData;
+
     if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("hotplug is not supported for hostdev mode '%s'"),
@@ -2938,9 +2944,15 @@ qemuDomainAttachHostDevice(virQEMUDriverPtr driver,
 
     switch (hostdev->source.subsys.type) {
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
-        if (qemuDomainAttachHostPCIDevice(driver, vm,
-                                          hostdev) < 0)
+        if (qemuDomainAttachPCIHostDevicePrepare(driver, vm->def,
+                                                 hostdev, priv->qemuCaps) < 0)
             return -1;
+
+        if (qemuDomainAttachHostPCIDevice(driver, vm, hostdev) < 0) {
+            qemuHostdevReAttachPCIDevices(driver, vm->def->name, &hostdev, 1);
+            return -1;
+        }
+
         break;
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
-- 
2.26.2

Reply via email to