diff mbox

[v6,07/12] pci: add a pci_function_is_valid callback to check function if valid

Message ID 1459856523-17085-8-git-send-email-caoj.fnst@cn.fujitsu.com
State New
Headers show

Commit Message

Cao jin April 5, 2016, 11:41 a.m. UTC
From: Chen Fan <chen.fan.fnst@cn.fujitsu.com>

PCI hotplug requires that function 0 is added last to close the
slot.  Since vfio supporting AER, we require that the VM bus
contains the same set of devices as the host bus to support AER,
we can perform an AER validation test whenever a function 0 in
the VM is hot-added.

Signed-off-by: Chen Fan <chen.fan.fnst@cn.fujitsu.com>
---
 hw/pci/pci.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/pci/pci.h |  1 +
 2 files changed, 50 insertions(+)
diff mbox

Patch

diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index e67664d..9dcd7d5 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -1840,6 +1840,40 @@  PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
     return bus->devices[devfn];
 }
 
+static void pci_functions_validate(PCIBus *bus, PCIDevice *d, Error **errp)
+{
+    PCIDevice *br = pci_bridge_get_device(bus);
+    PCIDeviceClass *pc;
+    PCIDevice *dev;
+    int devfn;
+    int limit = d->devfn + PCI_FUNC_MAX;
+    Error *local_err = NULL;
+
+    if (br &&
+        pci_bus_is_express(bus) &&
+        pcie_cap_is_arifwd_enabled(br)) {
+        limit = 255;
+    }
+
+    for (devfn = d->devfn; devfn < limit; devfn++) {
+        dev = pci_find_device(bus, pci_bus_num(bus), devfn);
+        if (!dev) {
+            continue;
+        }
+
+        pc = PCI_DEVICE_GET_CLASS(dev);
+        if (!pc->is_valid_func) {
+            continue;
+        }
+
+        pc->is_valid_func(d, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
+    }
+}
+
 static void pci_qdev_realize(DeviceState *qdev, Error **errp)
 {
     PCIDevice *pci_dev = (PCIDevice *)qdev;
@@ -1882,6 +1916,21 @@  static void pci_qdev_realize(DeviceState *qdev, Error **errp)
         pci_qdev_unrealize(DEVICE(pci_dev), NULL);
         return;
     }
+
+    /*
+     *  If the function number is 0, indicate the closure of the slot.
+     *  then we get the chance to check all functions on same device
+     *  if valid.
+     */
+    if (DEVICE(pci_dev)->hotplugged &&
+        pci_get_function_0(pci_dev) == pci_dev) {
+        pci_functions_validate(bus, pci_dev, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            pci_qdev_unrealize(DEVICE(pci_dev), NULL);
+            return;
+        }
+    }
 }
 
 static void pci_default_realize(PCIDevice *dev, Error **errp)
diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
index 0be07c8..4a2f7d4 100644
--- a/include/hw/pci/pci.h
+++ b/include/hw/pci/pci.h
@@ -190,6 +190,7 @@  typedef struct PCIDeviceClass {
 
     void (*realize)(PCIDevice *dev, Error **errp);
     int (*init)(PCIDevice *dev);/* TODO convert to realize() and remove */
+    void (*is_valid_func)(PCIDevice *dev, Error **errp);
     PCIUnregisterFunc *exit;
     PCIConfigReadFunc *config_read;
     PCIConfigWriteFunc *config_write;