commit d84b36263c5c54df8ebfaebbe099afec78c14fac
Author: Osier Yang <jyang@redhat.com>
Date:   Thu Oct 20 17:50:10 2011 +0800

    qemu: Restore the original states of PCI device when restarting daemon
    
    To support "managed" mode of host PCI device, we record the original
    states (unbind_from_stub, remove_slot, and reprobe) so that could
    reattach the device to host with original driver. But there is no XML
    for theses attrs, and thus after daemon is restarted, we lose the
    original states. It's easy to reproduce:
    
        1) virsh start domain
        2) virsh attach-device dom hostpci.xml (in 'managed' mode)
        3) service libvirtd restart
        4) virsh destroy domain
    
        You will see the device won't be bound to the original driver
    if there was one.
    
    This patch is to solve the problem by introducing internal XML
    (won't be dumped to user, only dumped to status XML). The XML is:
        <origstates>
          <unbind/>
          <remove_slot/>
          <reprobe/>
        </origstates>
    
    Which will be child node of <hostdev><source>...</souce></hostdev>.
    (only for PCI device).
    
    A new struct "virDomainHostdevOrigStates" is introduced for the XML,
    and the according members are updated when preparing the PCI device.
    And function "qemuUpdateActivePciHostdevs" is modified to honor
    the original states. Use of qemuGetPciHostDeviceList is removed
    in function "qemuUpdateActivePciHostdevs", and the "managed" value of
    the device config is honored by the change. This fixes another problem
    alongside:
    
        qemuGetPciHostDeviceList set the device as "managed" force
        regardless of whether the device is configured as "managed='yes'"
        or not in XML, which is not right.

Index: libvirt-0.9.6/src/conf/domain_conf.c
===================================================================
--- libvirt-0.9.6.orig/src/conf/domain_conf.c
+++ libvirt-0.9.6/src/conf/domain_conf.c
@@ -59,8 +59,12 @@ verify(VIR_DOMAIN_VIRT_LAST <= 32);
 /* Private flags used internally by virDomainSaveStatus and
  * virDomainLoadStatus. */
 typedef enum {
-   VIR_DOMAIN_XML_INTERNAL_STATUS = (1<<16), /* dump internal domain status information */
-   VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET = (1<<17), /* dump/parse <actual> element */
+   /* dump internal domain status information */
+   VIR_DOMAIN_XML_INTERNAL_STATUS = (1<<16),
+   /* dump/parse <actual> element */
+   VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET = (1<<17),
+   /* dump/parse original states of host PCI device */
+   VIR_DOMAIN_XML_INTERNAL_PCI_ORIG_STATES = (1<<18),
 } virDomainXMLInternalFlags;
 
 VIR_ENUM_IMPL(virDomainTaint, VIR_DOMAIN_TAINT_LAST,
@@ -5390,13 +5394,47 @@ out:
     return ret;
 }
 
+/* The internal XML for host PCI device's original states:
+ *
+ * <origstates>
+ *   <unbind/>
+ *   <removeslot/>
+ *   <reprobe/>
+ * </origstates>
+ */
+static int
+virDomainHostdevSubsysPciOrigStatesDefParseXML(const xmlNodePtr node,
+                                               virDomainHostdevOrigStatesPtr def)
+{
+    xmlNodePtr cur;
+    cur = node->children;
+
+    while (cur != NULL) {
+        if (cur->type == XML_ELEMENT_NODE) {
+            if (xmlStrEqual(cur->name, BAD_CAST "unbind")) {
+                def->states.pci.unbind_from_stub = 1;
+            } else if (xmlStrEqual(cur->name, BAD_CAST "removeslot")) {
+                def->states.pci.remove_slot = 1;
+            } else if (xmlStrEqual(cur->name, BAD_CAST "reprobe")) {
+                def->states.pci.reprobe = 1;
+            } else {
+                virDomainReportError(VIR_ERR_INTERNAL_ERROR,
+                                     _("unsupported element '%s' of 'origstates'"),
+                                     cur->name);
+                return -1;
+            }
+        }
+        cur = cur->next;
+    }
+
+    return 0;
+}
 
 static int
 virDomainHostdevSubsysPciDefParseXML(const xmlNodePtr node,
                                      virDomainHostdevDefPtr def,
                                      unsigned int flags)
 {
-
     int ret = -1;
     xmlNodePtr cur;
 
@@ -5423,6 +5461,11 @@ virDomainHostdevSubsysPciDefParseXML(con
                     goto out;
                 }
                 def->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
+            } else if ((flags & VIR_DOMAIN_XML_INTERNAL_PCI_ORIG_STATES) &&
+                       xmlStrEqual(cur->name, BAD_CAST "origstates")) {
+                virDomainHostdevOrigStatesPtr states = &def->origstates;
+                if (virDomainHostdevSubsysPciOrigStatesDefParseXML(cur, states) < 0)
+                    goto out;
             } else {
                 virDomainReportError(VIR_ERR_INTERNAL_ERROR,
                                      _("unknown pci source type '%s'"),
@@ -10378,7 +10421,9 @@ virDomainHostdevDefFormat(virBufferPtr b
     }
 
     type = virDomainHostdevSubsysTypeToString(def->source.subsys.type);
-    if (!type || (def->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB && def->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) ) {
+    if (!type ||
+        (def->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB &&
+         def->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)) {
         virDomainReportError(VIR_ERR_INTERNAL_ERROR,
                              _("unexpected hostdev type %d"),
                              def->source.subsys.type);
@@ -10407,6 +10452,20 @@ virDomainHostdevDefFormat(virBufferPtr b
                           def->source.subsys.u.pci.bus,
                           def->source.subsys.u.pci.slot,
                           def->source.subsys.u.pci.function);
+
+        if ((flags & VIR_DOMAIN_XML_INTERNAL_PCI_ORIG_STATES) &&
+            (def->origstates.states.pci.unbind_from_stub ||
+             def->origstates.states.pci.remove_slot ||
+             def->origstates.states.pci.reprobe)) {
+            virBufferAddLit(buf, "        <origstates>\n");
+            if (def->origstates.states.pci.unbind_from_stub)
+                virBufferAddLit(buf, "          <unbind/>\n");
+            if (def->origstates.states.pci.remove_slot)
+                virBufferAddLit(buf, "          <removeslot/>\n");
+            if (def->origstates.states.pci.reprobe)
+                virBufferAddLit(buf, "          <reprobe/>\n");
+            virBufferAddLit(buf, "        </origstates>\n");
+        }
     }
 
     virBufferAddLit(buf, "      </source>\n");
@@ -10475,7 +10534,8 @@ virDomainHubDefFormat(virBufferPtr buf,
      VIR_DOMAIN_XML_UPDATE_CPU)
 
 verify(((VIR_DOMAIN_XML_INTERNAL_STATUS |
-         VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET)
+         VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET |
+         VIR_DOMAIN_XML_INTERNAL_PCI_ORIG_STATES)
         & DUMPXML_FLAGS) == 0);
 
 /* This internal version can accept VIR_DOMAIN_XML_INTERNAL_*,
@@ -10495,7 +10555,8 @@ virDomainDefFormatInternal(virDomainDefP
 
     virCheckFlags(DUMPXML_FLAGS |
                   VIR_DOMAIN_XML_INTERNAL_STATUS |
-                  VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET,
+                  VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET |
+                  VIR_DOMAIN_XML_INTERNAL_PCI_ORIG_STATES,
                   -1);
 
     if (!(type = virDomainVirtTypeToString(def->virtType))) {
@@ -11071,7 +11132,8 @@ int virDomainSaveStatus(virCapsPtr caps,
 {
     unsigned int flags = (VIR_DOMAIN_XML_SECURE |
                           VIR_DOMAIN_XML_INTERNAL_STATUS |
-                          VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET);
+                          VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET |
+                          VIR_DOMAIN_XML_INTERNAL_PCI_ORIG_STATES);
 
     int ret = -1;
     char *xml;
@@ -11170,7 +11232,8 @@ static virDomainObjPtr virDomainLoadStat
 
     if (!(obj = virDomainObjParseFile(caps, statusFile, expectedVirtTypes,
                                       VIR_DOMAIN_XML_INTERNAL_STATUS |
-                                      VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET)))
+                                      VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET |
+                                      VIR_DOMAIN_XML_INTERNAL_PCI_ORIG_STATES)))
         goto error;
 
     virUUIDFormat(obj->def->uuid, uuidstr);
Index: libvirt-0.9.6/src/conf/domain_conf.h
===================================================================
--- libvirt-0.9.6.orig/src/conf/domain_conf.h
+++ libvirt-0.9.6/src/conf/domain_conf.h
@@ -144,6 +144,31 @@ struct _virDomainDeviceInfo {
     } master;
 };
 
+typedef struct _virDomainHostdevOrigStates virDomainHostdevOrigStates;
+typedef virDomainHostdevOrigStates *virDomainHostdevOrigStatesPtr;
+struct _virDomainHostdevOrigStates {
+    union {
+        struct {
+            /* Does the device need to unbind from stub when
+             * reattaching to host?
+             */
+            unsigned int unbind_from_stub : 1;
+
+            /* Does it need to use remove_slot when reattaching
+             * the device to host?
+             */
+            unsigned int remove_slot : 1;
+
+            /* Does it need to reprobe driver for the device when
+             * reattaching to host?
+             */
+            unsigned int reprobe :1;
+        } pci;
+
+        /* Perhaps 'usb' in future */
+    } states;
+};
+
 typedef struct _virDomainLeaseDef virDomainLeaseDef;
 typedef virDomainLeaseDef *virDomainLeaseDefPtr;
 struct _virDomainLeaseDef {
@@ -966,6 +991,7 @@ struct _virDomainHostdevDef {
     } source;
     int bootIndex;
     virDomainDeviceInfo info; /* Guest address */
+    virDomainHostdevOrigStates origstates;
 };
 
 enum virDomainRedirdevBus {
Index: libvirt-0.9.6/src/libvirt_private.syms
===================================================================
--- libvirt-0.9.6.orig/src/libvirt_private.syms
+++ libvirt-0.9.6/src/libvirt_private.syms
@@ -873,6 +873,9 @@ pciDettachDevice;
 pciDeviceFileIterate;
 pciDeviceGetManaged;
 pciDeviceGetName;
+pciDeviceGetRemoveSlot;
+pciDeviceGetReprobe;
+pciDeviceGetUnbindFromStub;
 pciDeviceGetUsedBy;
 pciDeviceIsAssignable;
 pciDeviceIsVirtualFunction;
@@ -886,6 +889,9 @@ pciDeviceListSteal;
 pciDeviceNetName;
 pciDeviceReAttachInit;
 pciDeviceSetManaged;
+pciDeviceSetRemoveSlot;
+pciDeviceSetReprobe;
+pciDeviceSetUnbindFromStub;
 pciDeviceSetUsedBy;
 pciFreeDevice;
 pciGetDevice;
Index: libvirt-0.9.6/src/qemu/qemu_hostdev.c
===================================================================
--- libvirt-0.9.6.orig/src/qemu/qemu_hostdev.c
+++ libvirt-0.9.6/src/qemu/qemu_hostdev.c
@@ -116,33 +116,46 @@ qemuGetActivePciHostDeviceList(struct qe
 int qemuUpdateActivePciHostdevs(struct qemud_driver *driver,
                                 virDomainDefPtr def)
 {
-    pciDeviceList *pcidevs;
-    int ret = -1;
+    virDomainHostdevDefPtr hostdev = NULL;
+    int i;
 
     if (!def->nhostdevs)
         return 0;
 
-    if (!(pcidevs = qemuGetPciHostDeviceList(def->hostdevs, def->nhostdevs)))
-        return -1;
+    for (i = 0; i < def->nhostdevs; i++) {
+        pciDevice *dev = NULL;
+        hostdev = def->hostdevs[i];
+
+        if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
+            continue;
+        if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
+            continue;
+
+        dev = pciGetDevice(hostdev->source.subsys.u.pci.domain,
+                           hostdev->source.subsys.u.pci.bus,
+                           hostdev->source.subsys.u.pci.slot,
+                           hostdev->source.subsys.u.pci.function);
+
+        if (!dev)
+            return -1;
+
+        pciDeviceSetManaged(dev, hostdev->managed);
+        pciDeviceSetUsedBy(dev, def->name);
+
+        /* Setup the original states for the PCI device */
+        pciDeviceSetUnbindFromStub(dev, hostdev->origstates.states.pci.unbind_from_stub);
+        pciDeviceSetRemoveSlot(dev, hostdev->origstates.states.pci.remove_slot);
+        pciDeviceSetReprobe(dev, hostdev->origstates.states.pci.reprobe);
 
-    while (pciDeviceListCount(pcidevs) > 0) {
-        pciDevice *dev = pciDeviceListGet(pcidevs, 0);
-        pciDeviceListSteal(pcidevs, dev);
         if (pciDeviceListAdd(driver->activePciHostdevs, dev) < 0) {
             pciFreeDevice(dev);
-            goto cleanup;
+            return -1;
         }
     }
 
-    ret = 0;
-
-cleanup:
-    pciDeviceListFree(pcidevs);
-    return ret;
+    return 0;
 }
 
-
-
 int qemuPrepareHostdevPCIDevices(struct qemud_driver *driver,
                                  const char *name,
                                  virDomainHostdevDefPtr *hostdevs,
@@ -155,7 +168,7 @@ int qemuPrepareHostdevPCIDevices(struct
     if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
         return -1;
 
-    /* We have to use 6 loops here. *All* devices must
+    /* We have to use 7 loops here. *All* devices must
      * be detached before we reset any of them, because
      * in some cases you have to reset the whole PCI,
      * which impacts all devices on it. Also, all devices
@@ -232,7 +245,39 @@ int qemuPrepareHostdevPCIDevices(struct
         pciDeviceSetUsedBy(activeDev, name);
     }
 
-    /* Loop 6: Now steal all the devices from pcidevs */
+    /* Loop 6: Now set the original states for hostdev def */
+    for (i = 0; i < nhostdevs; i++) {
+        pciDevice *dev;
+        pciDevice *pcidev;
+        virDomainHostdevDefPtr hostdev = hostdevs[i];
+
+        if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
+            continue;
+        if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
+            continue;
+
+        dev = pciGetDevice(hostdev->source.subsys.u.pci.domain,
+                           hostdev->source.subsys.u.pci.bus,
+                           hostdev->source.subsys.u.pci.slot,
+                           hostdev->source.subsys.u.pci.function);
+
+        /* original states "unbind_from_stub", "remove_slot",
+         * "reprobe" were already set by pciDettachDevice in
+         * loop 2.
+         */
+        if ((pcidev = pciDeviceListFind(pcidevs, dev))) {
+            hostdev->origstates.states.pci.unbind_from_stub =
+                pciDeviceGetUnbindFromStub(pcidev);
+            hostdev->origstates.states.pci.remove_slot =
+                pciDeviceGetRemoveSlot(pcidev);
+            hostdev->origstates.states.pci.reprobe =
+                pciDeviceGetReprobe(pcidev);
+        }
+
+        pciFreeDevice(dev);
+    }
+
+    /* Loop 7: Now steal all the devices from pcidevs */
     while (pciDeviceListCount(pcidevs) > 0) {
         pciDevice *dev = pciDeviceListGet(pcidevs, 0);
         pciDeviceListSteal(pcidevs, dev);
Index: libvirt-0.9.6/src/util/pci.c
===================================================================
--- libvirt-0.9.6.orig/src/util/pci.c
+++ libvirt-0.9.6/src/util/pci.c
@@ -1394,6 +1394,42 @@ unsigned pciDeviceGetManaged(pciDevice *
     return dev->managed;
 }
 
+unsigned
+pciDeviceGetUnbindFromStub(pciDevice *dev)
+{
+    return dev->unbind_from_stub;
+}
+
+void
+pciDeviceSetUnbindFromStub(pciDevice *dev, unsigned unbind)
+{
+    dev->unbind_from_stub = !!unbind;
+}
+
+unsigned
+pciDeviceGetRemoveSlot(pciDevice *dev)
+{
+    return dev->remove_slot;
+}
+
+void
+pciDeviceSetRemoveSlot(pciDevice *dev, unsigned remove_slot)
+{
+    dev->remove_slot = !!remove_slot;
+}
+
+unsigned
+pciDeviceGetReprobe(pciDevice *dev)
+{
+    return dev->reprobe;
+}
+
+void
+pciDeviceSetReprobe(pciDevice *dev, unsigned reprobe)
+{
+    dev->reprobe = !!reprobe;
+}
+
 void
 pciDeviceSetUsedBy(pciDevice *dev, const char *name)
 {
Index: libvirt-0.9.6/src/util/pci.h
===================================================================
--- libvirt-0.9.6.orig/src/util/pci.h
+++ libvirt-0.9.6/src/util/pci.h
@@ -51,6 +51,15 @@ unsigned  pciDeviceGetManaged(pciDevice
 void      pciDeviceSetUsedBy(pciDevice     *dev,
                              const char *used_by);
 const char *pciDeviceGetUsedBy(pciDevice   *dev);
+unsigned  pciDeviceGetUnbindFromStub(pciDevice *dev);
+void      pciDeviceSetUnbindFromStub(pciDevice     *dev,
+                                     unsigned      unbind);
+unsigned  pciDeviceGetRemoveSlot(pciDevice *dev);
+void      pciDeviceSetRemoveSlot(pciDevice     *dev,
+                                 unsigned      remove_slot);
+unsigned  pciDeviceGetReprobe(pciDevice *dev);
+void      pciDeviceSetReprobe(pciDevice     *dev,
+                              unsigned      reprobe);
 void      pciDeviceReAttachInit(pciDevice   *dev);
 
 pciDeviceList *pciDeviceListNew  (void);
