bus/pci: support hot-unplug handler
authorJeff Guo <jia.guo@intel.com>
Mon, 15 Oct 2018 11:27:22 +0000 (19:27 +0800)
committerThomas Monjalon <thomas@monjalon.net>
Mon, 15 Oct 2018 20:16:48 +0000 (22:16 +0200)
This patch implements the ops to handle hot-unplug on the PCI bus.
For UIO PCI, it could avoids BARs read/write errors by creating a
new dummy memory to remap the memory where the failure is. For VFIO
or other kernel driver, it could specific implement function to handle
hot-unplug case by case.

Signed-off-by: Jeff Guo <jia.guo@intel.com>
Acked-by: Shaopeng He <shaopeng.he@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
drivers/bus/pci/pci_common.c
drivers/bus/pci/pci_common_uio.c
drivers/bus/pci/private.h

index c7695d1..be7cc1f 100644 (file)
@@ -403,6 +403,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
        return NULL;
 }
 
+static int
+pci_hot_unplug_handler(struct rte_device *dev)
+{
+       struct rte_pci_device *pdev = NULL;
+       int ret = 0;
+
+       pdev = RTE_DEV_TO_PCI(dev);
+       if (!pdev)
+               return -1;
+
+       switch (pdev->kdrv) {
+       case RTE_KDRV_IGB_UIO:
+       case RTE_KDRV_UIO_GENERIC:
+       case RTE_KDRV_NIC_UIO:
+               /* BARs resource is invalid, remap it to be safe. */
+               ret = pci_uio_remap_resource(pdev);
+               break;
+       default:
+               RTE_LOG(DEBUG, EAL,
+                       "Not managed by a supported kernel driver, skipped\n");
+               ret = -1;
+               break;
+       }
+
+       return ret;
+}
+
 static int
 pci_plug(struct rte_device *dev)
 {
@@ -434,6 +461,7 @@ struct rte_pci_bus rte_pci_bus = {
                .parse = pci_parse,
                .get_iommu_class = rte_pci_get_iommu_class,
                .dev_iterate = rte_pci_dev_iterate,
+               .hot_unplug_handler = pci_hot_unplug_handler,
        },
        .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
        .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
index 54bc20b..7ea73db 100644 (file)
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res)
        }
 }
 
+/* remap the PCI resource of a PCI device in anonymous virtual memory */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev)
+{
+       int i;
+       void *map_address;
+
+       if (dev == NULL)
+               return -1;
+
+       /* Remap all BARs */
+       for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+               /* skip empty BAR */
+               if (dev->mem_resource[i].phys_addr == 0)
+                       continue;
+               map_address = mmap(dev->mem_resource[i].addr,
+                               (size_t)dev->mem_resource[i].len,
+                               PROT_READ | PROT_WRITE,
+                               MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+               if (map_address == MAP_FAILED) {
+                       RTE_LOG(ERR, EAL,
+                               "Cannot remap resource for device %s\n",
+                               dev->name);
+                       return -1;
+               }
+               RTE_LOG(INFO, EAL,
+                       "Successful remap resource for device %s\n",
+                       dev->name);
+       }
+
+       return 0;
+}
+
 static struct mapped_pci_resource *
 pci_uio_find_resource(struct rte_pci_device *dev)
 {
index 04bffa6..13c3324 100644 (file)
@@ -126,6 +126,18 @@ int pci_uio_alloc_resource(struct rte_pci_device *dev,
 void pci_uio_free_resource(struct rte_pci_device *dev,
                struct mapped_pci_resource *uio_res);
 
+/**
+ * Remap the PCI resource of a PCI device in anonymous virtual memory.
+ *
+ * @param dev
+ *   Point to the struct rte pci device.
+ * @return
+ *   - On success, zero.
+ *   - On failure, a negative value.
+ */
+int
+pci_uio_remap_resource(struct rte_pci_device *dev);
+
 /**
  * Map device memory to uio resource
  *