Remove duplicate code by moving this to helpers in common.

Signed-off-by: David Marchand <david.marchand at 6wind.com>
---
 lib/librte_eal/bsdapp/eal/eal_pci.c     | 34 +++++-----------
 lib/librte_eal/common/eal_common_pci.c  | 70 ++++++++++++++++++++++++---------
 lib/librte_eal/common/include/rte_pci.h | 24 +++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci.c   | 34 +++++-----------
 4 files changed, 94 insertions(+), 68 deletions(-)

diff --git a/lib/librte_eal/bsdapp/eal/eal_pci.c 
b/lib/librte_eal/bsdapp/eal/eal_pci.c
index 4584522..401cda3 100644
--- a/lib/librte_eal/bsdapp/eal/eal_pci.c
+++ b/lib/librte_eal/bsdapp/eal/eal_pci.c
@@ -247,6 +247,7 @@ static int
 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
 {
        struct rte_pci_device *dev;
+       struct rte_pci_device *dev2;
        struct pci_bar_io bar;
        unsigned i, max;

@@ -311,32 +312,15 @@ pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
                dev->mem_resource[i].phys_addr = bar.pbi_base & 
~((uint64_t)0xf);
        }

-       /* device is valid, add in list (sorted) */
-       if (TAILQ_EMPTY(&pci_device_list)) {
-               TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
-       }
+       dev2 = pci_find_device(&dev->addr);
+       if (!dev2)
+               pci_add_device(dev);
        else {
-               struct rte_pci_device *dev2 = NULL;
-               int ret;
-
-               TAILQ_FOREACH(dev2, &pci_device_list, next) {
-                       ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
-                       if (ret > 0)
-                               continue;
-                       else if (ret < 0) {
-                               TAILQ_INSERT_BEFORE(dev2, dev, next);
-                               return 0;
-                       } else { /* already registered */
-                               dev2->kdrv = dev->kdrv;
-                               dev2->max_vfs = dev->max_vfs;
-                               memmove(dev2->mem_resource,
-                                       dev->mem_resource,
-                                       sizeof(dev->mem_resource));
-                               free(dev);
-                               return 0;
-                       }
-               }
-               TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
+               dev2->kdrv = dev->kdrv;
+               dev2->max_vfs = dev->max_vfs;
+               memmove(dev2->mem_resource, dev->mem_resource,
+                       sizeof(dev->mem_resource));
+               free(dev);
        }

        return 0;
diff --git a/lib/librte_eal/common/eal_common_pci.c 
b/lib/librte_eal/common/eal_common_pci.c
index 1e12776..2528775 100644
--- a/lib/librte_eal/common/eal_common_pci.c
+++ b/lib/librte_eal/common/eal_common_pci.c
@@ -139,6 +139,41 @@ pci_unmap_resource(void *requested_addr, size_t size)
                                requested_addr);
 }

+struct rte_pci_device *
+pci_find_device(const struct rte_pci_addr *addr)
+{
+       struct rte_pci_device *dev;
+
+       TAILQ_FOREACH(dev, &pci_device_list, next) {
+               if (!rte_eal_compare_pci_addr(&dev->addr, addr))
+                       break;
+       }
+
+       return dev;
+}
+
+int
+pci_add_device(struct rte_pci_device *dev)
+{
+       struct rte_pci_device *dev2;
+       int ret;
+
+       TAILQ_FOREACH(dev2, &pci_device_list, next) {
+               ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
+               if (ret > 0)
+                       continue;
+
+               if (ret == 0)
+                       return -1;
+
+               TAILQ_INSERT_BEFORE(dev2, dev, next);
+               return 0;
+       }
+
+       TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
+       return 0;
+}
+
 /*
  * If vendor/device ID match, call the devinit() function of the
  * driver.
@@ -332,16 +367,15 @@ rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
        if (addr == NULL)
                return -1;

-       TAILQ_FOREACH(dev, &pci_device_list, next) {
-               if (rte_eal_compare_pci_addr(&dev->addr, addr))
-                       continue;
+       dev = pci_find_device(addr);
+       if (!dev)
+               goto err_return;

-               ret = pci_probe_all_drivers(dev);
-               if (ret < 0)
-                       goto err_return;
-               return 0;
-       }
-       return -1;
+       ret = pci_probe_all_drivers(dev);
+       if (ret < 0)
+               goto err_return;
+
+       return 0;

 err_return:
        RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
@@ -362,18 +396,16 @@ rte_eal_pci_detach(const struct rte_pci_addr *addr)
        if (addr == NULL)
                return -1;

-       TAILQ_FOREACH(dev, &pci_device_list, next) {
-               if (rte_eal_compare_pci_addr(&dev->addr, addr))
-                       continue;
+       dev = pci_find_device(addr);
+       if (!dev)
+               goto err_return;

-               ret = pci_detach_all_drivers(dev);
-               if (ret < 0)
-                       goto err_return;
+       ret = pci_detach_all_drivers(dev);
+       if (ret < 0)
+               goto err_return;

-               TAILQ_REMOVE(&pci_device_list, dev, next);
-               return 0;
-       }
-       return -1;
+       TAILQ_REMOVE(&pci_device_list, dev, next);
+       return 0;

 err_return:
        RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
diff --git a/lib/librte_eal/common/include/rte_pci.h 
b/lib/librte_eal/common/include/rte_pci.h
index 334c12e..4ba7192 100644
--- a/lib/librte_eal/common/include/rte_pci.h
+++ b/lib/librte_eal/common/include/rte_pci.h
@@ -399,6 +399,30 @@ void *pci_map_resource(void *requested_addr, int fd, off_t 
offset,
 void pci_unmap_resource(void *requested_addr, size_t size);

 /**
+ * @internal
+ * Add a pci device to sorted pci device list
+ *
+ * @param dev
+ *     The PCI device object.
+ * @return
+ *   - 0 on success.
+ *   - negative on error.
+ */
+int pci_add_device(struct rte_pci_device *dev);
+
+/**
+ * @internal
+ * Find a pci device object based on a PCI Bus-Device-Function address.
+ *
+ * @param addr
+ *     The PCI Bus-Device-Function address to look for
+ * @return
+ *   - pointer to the object,
+ *   - NULL when not found.
+ */
+struct rte_pci_device *pci_find_device(const struct rte_pci_addr *addr);
+
+/**
  * Probe the single PCI device.
  *
  * Scan the content of the PCI bus, and find the pci device specified by pci
diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c 
b/lib/librte_eal/linuxapp/eal/eal_pci.c
index a354f76..83eec5d 100644
--- a/lib/librte_eal/linuxapp/eal/eal_pci.c
+++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
@@ -259,6 +259,7 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t 
bus,
        char filename[PATH_MAX];
        unsigned long tmp;
        struct rte_pci_device *dev;
+       struct rte_pci_device *dev2;
        char driver[PATH_MAX];
        int ret;

@@ -364,30 +365,15 @@ pci_scan_one(const char *dirname, uint16_t domain, 
uint8_t bus,
        } else
                dev->kdrv = RTE_KDRV_UNKNOWN;

-       /* device is valid, add in list (sorted) */
-       if (TAILQ_EMPTY(&pci_device_list)) {
-               TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
-       } else {
-               struct rte_pci_device *dev2;
-               int ret;
-
-               TAILQ_FOREACH(dev2, &pci_device_list, next) {
-                       ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
-                       if (ret > 0)
-                               continue;
-
-                       if (ret < 0) {
-                               TAILQ_INSERT_BEFORE(dev2, dev, next);
-                       } else { /* already registered */
-                               dev2->kdrv = dev->kdrv;
-                               dev2->max_vfs = dev->max_vfs;
-                               memmove(dev2->mem_resource, dev->mem_resource,
-                                       sizeof(dev->mem_resource));
-                               free(dev);
-                       }
-                       return 0;
-               }
-               TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
+       dev2 = pci_find_device(&dev->addr);
+       if (!dev2)
+               pci_add_device(dev);
+       else {
+               dev2->kdrv = dev->kdrv;
+               dev2->max_vfs = dev->max_vfs;
+               memmove(dev2->mem_resource, dev->mem_resource,
+                       sizeof(dev->mem_resource));
+               free(dev);
        }

        return 0;
-- 
1.9.1

Reply via email to