Drivers that only need a subset of PCI BARs still pay the cost of
mapping every valid BAR. There is no bus-level way for a driver to
select which BARs are mapped.

Add the internal rte_pci_map_device_bars(). A driver calls it from
probe() and does not set RTE_PCI_DRV_NEED_MAPPING. Bit N selects
BAR N. A zero mask maps every valid BAR, which is what
rte_pci_map_device() does. A mask outside BAR0-BAR5 is rejected.
UIO and VFIO map slots stay indexed by BAR number so primary and
secondary processes stay consistent when some BARs are skipped.

Signed-off-by: Cheryl Bansal <[email protected]>
---
v2:
* Drop the PCI bus devarg. A driver calls rte_pci_map_device_bars()
  from probe() and does not set RTE_PCI_DRV_NEED_MAPPING.
  A failed map is the driver's probe error. Linux, FreeBSD and
  Windows all use the same mask.
* Rebased on v26.11. For 27.03.

 drivers/bus/pci/bsd/pci.c        | 25 +++++++++++++---
 drivers/bus/pci/linux/pci.c      | 25 +++++++++++++---
 drivers/bus/pci/linux/pci_vfio.c |  4 +++
 drivers/bus/pci/pci_common.c     |  2 ++
 drivers/bus/pci/pci_common_uio.c | 49 ++++++++++++++++----------------
 drivers/bus/pci/private.h        | 14 +++++++++
 drivers/bus/pci/rte_bus_pci.h    | 17 +++++++++++
 drivers/bus/pci/windows/pci.c    | 12 ++++++++
 8 files changed, 116 insertions(+), 32 deletions(-)

diff --git a/drivers/bus/pci/bsd/pci.c b/drivers/bus/pci/bsd/pci.c
index 2d29f8d2e2..5c10f2e823 100644
--- a/drivers/bus/pci/bsd/pci.c
+++ b/drivers/bus/pci/bsd/pci.c
@@ -48,10 +48,8 @@
  * PCI probing under BSD.
  */
 
-/* Map pci device */
-RTE_EXPORT_SYMBOL(rte_pci_map_device)
-int
-rte_pci_map_device(struct rte_pci_device *dev)
+static int
+pci_map_device(struct rte_pci_device *dev)
 {
        int ret = -1;
 
@@ -70,6 +68,25 @@ rte_pci_map_device(struct rte_pci_device *dev)
        return ret;
 }
 
+/* Map pci device */
+RTE_EXPORT_SYMBOL(rte_pci_map_device)
+int
+rte_pci_map_device(struct rte_pci_device *dev)
+{
+       return rte_pci_map_device_bars(dev, 0);
+}
+
+RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_map_device_bars)
+int
+rte_pci_map_device_bars(struct rte_pci_device *dev, uint8_t bar_mask)
+{
+       if (bar_mask > PCI_BAR_MASK_ALL)
+               return -EINVAL;
+
+       RTE_PCI_DEVICE_INTERNAL(dev)->bar_mask = bar_mask;
+       return pci_map_device(dev);
+}
+
 /* Unmap pci device */
 RTE_EXPORT_SYMBOL(rte_pci_unmap_device)
 void
diff --git a/drivers/bus/pci/linux/pci.c b/drivers/bus/pci/linux/pci.c
index 28949e3720..74ff60dcf0 100644
--- a/drivers/bus/pci/linux/pci.c
+++ b/drivers/bus/pci/linux/pci.c
@@ -55,10 +55,8 @@ pci_get_kernel_driver_by_path(const char *filename, char 
*dri_name,
        return -1;
 }
 
-/* Map pci device */
-RTE_EXPORT_SYMBOL(rte_pci_map_device)
-int
-rte_pci_map_device(struct rte_pci_device *dev)
+static int
+pci_map_device(struct rte_pci_device *dev)
 {
        int ret = -1;
 
@@ -84,6 +82,25 @@ rte_pci_map_device(struct rte_pci_device *dev)
        return ret;
 }
 
+/* Map pci device */
+RTE_EXPORT_SYMBOL(rte_pci_map_device)
+int
+rte_pci_map_device(struct rte_pci_device *dev)
+{
+       return rte_pci_map_device_bars(dev, 0);
+}
+
+RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_map_device_bars)
+int
+rte_pci_map_device_bars(struct rte_pci_device *dev, uint8_t bar_mask)
+{
+       if (bar_mask > PCI_BAR_MASK_ALL)
+               return -EINVAL;
+
+       RTE_PCI_DEVICE_INTERNAL(dev)->bar_mask = bar_mask;
+       return pci_map_device(dev);
+}
+
 /* Unmap pci device */
 RTE_EXPORT_SYMBOL(rte_pci_unmap_device)
 void
diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c
index 4363d7ff2c..43ef810cc6 100644
--- a/drivers/bus/pci/linux/pci_vfio.c
+++ b/drivers/bus/pci/linux/pci_vfio.c
@@ -738,6 +738,7 @@ pci_vfio_map_resource_primary(struct rte_pci_device *dev)
        struct mapped_pci_resource *vfio_res = NULL;
        struct mapped_pci_res_list *vfio_res_list =
                RTE_TAILQ_CAST(dev_vfio_tailq.head, mapped_pci_res_list);
+       uint8_t bar_mask = pci_get_bar_mask(dev);
 
        struct pci_map *maps;
 
@@ -814,6 +815,9 @@ pci_vfio_map_resource_primary(struct rte_pci_device *dev)
                struct vfio_info_cap_header *hdr;
                struct vfio_region_info_cap_sparse_mmap *sparse;
 
+               if ((bar_mask & (1 << i)) == 0)
+                       continue;
+
                ret = pci_vfio_get_region_info(vfio_dev_fd, &reg, i);
                if (ret < 0) {
                        PCI_LOG(ERR, "%s cannot get device region info error %i 
(%s)",
diff --git a/drivers/bus/pci/pci_common.c b/drivers/bus/pci/pci_common.c
index 3770b4ad75..21d8708b84 100644
--- a/drivers/bus/pci/pci_common.c
+++ b/drivers/bus/pci/pci_common.c
@@ -3,6 +3,7 @@
  * Copyright 2013-2014 6WIND S.A.
  */
 
+#include <errno.h>
 #include <string.h>
 #include <inttypes.h>
 #include <stdint.h>
@@ -25,6 +26,7 @@
 #include <rte_string_fns.h>
 #include <rte_common.h>
 #include <rte_devargs.h>
+#include <rte_kvargs.h>
 #include <rte_tailq.h>
 
 #include "private.h"
diff --git a/drivers/bus/pci/pci_common_uio.c b/drivers/bus/pci/pci_common_uio.c
index 52a10f806a..4635216eff 100644
--- a/drivers/bus/pci/pci_common_uio.c
+++ b/drivers/bus/pci/pci_common_uio.c
@@ -27,7 +27,7 @@ EAL_REGISTER_TAILQ(rte_uio_tailq)
 static int
 pci_uio_map_secondary(struct rte_pci_device *dev)
 {
-       int fd, i = 0, j, res_idx;
+       int fd, i, j;
        struct mapped_pci_resource *uio_res;
        struct mapped_pci_res_list *uio_res_list =
                        RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
@@ -38,15 +38,11 @@ pci_uio_map_secondary(struct rte_pci_device *dev)
                if (rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
                        continue;
 
-               /* Map all BARs */
-               for (res_idx = 0; res_idx != PCI_MAX_RESOURCE; res_idx++) {
-                       /* skip empty BAR */
-                       if (dev->mem_resource[res_idx].phys_addr == 0)
+               /* Replay primary mappings (indexed by BAR) */
+               for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+                       if (uio_res->maps[i].size == 0)
                                continue;
 
-                       if (i >= uio_res->nb_maps)
-                               return -1;
-
                        /*
                         * open devname, to mmap it
                         */
@@ -80,9 +76,7 @@ pci_uio_map_secondary(struct rte_pci_device *dev)
                                }
                                return -1;
                        }
-                       dev->mem_resource[res_idx].addr = mapaddr;
-
-                       i++;
+                       dev->mem_resource[i].addr = mapaddr;
                }
                return 0;
        }
@@ -95,11 +89,12 @@ pci_uio_map_secondary(struct rte_pci_device *dev)
 int
 pci_uio_map_resource(struct rte_pci_device *dev)
 {
-       int i, map_idx = 0, ret;
+       int i, ret;
        uint64_t phaddr;
        struct mapped_pci_resource *uio_res = NULL;
        struct mapped_pci_res_list *uio_res_list =
                RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
+       uint8_t bar_mask = pci_get_bar_mask(dev);
 
        /* allocate uio resource */
        ret = pci_uio_alloc_resource(dev, &uio_res);
@@ -110,31 +105,34 @@ pci_uio_map_resource(struct rte_pci_device *dev)
        if (rte_eal_process_type() != RTE_PROC_PRIMARY)
                return pci_uio_map_secondary(dev);
 
-       /* Map all BARs */
+       /* Index maps[] by BAR number */
        for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+               if ((bar_mask & (1 << i)) == 0)
+                       continue;
+
                /* skip empty BAR */
                phaddr = dev->mem_resource[i].phys_addr;
                if (phaddr == 0)
                        continue;
 
                ret = pci_uio_map_resource_by_index(dev, i,
-                               uio_res, map_idx);
+                               uio_res, i);
                if (ret)
                        goto error;
-
-               map_idx++;
        }
 
-       uio_res->nb_maps = map_idx;
+       uio_res->nb_maps = PCI_MAX_RESOURCE;
 
        TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
 
        return 0;
 error:
-       for (i = 0; i < map_idx; i++) {
-               pci_unmap_resource(uio_res->maps[i].addr,
-                               (size_t)uio_res->maps[i].size);
-               rte_free(uio_res->maps[i].path);
+       for (i = 0; i < PCI_MAX_RESOURCE; i++) {
+               if (uio_res->maps[i].addr != NULL) {
+                       pci_unmap_resource(uio_res->maps[i].addr,
+                                       (size_t)uio_res->maps[i].size);
+                       rte_free(uio_res->maps[i].path);
+               }
        }
        pci_uio_free_resource(dev, uio_res);
        return -1;
@@ -149,6 +147,8 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res)
                return;
 
        for (i = 0; i != uio_res->nb_maps; i++) {
+               if (uio_res->maps[i].addr == NULL)
+                       continue;
                pci_unmap_resource(uio_res->maps[i].addr,
                                (size_t)uio_res->maps[i].size);
                if (rte_eal_process_type() == RTE_PROC_PRIMARY)
@@ -166,10 +166,11 @@ pci_uio_remap_resource(struct rte_pci_device *dev)
        if (dev == NULL)
                return -1;
 
-       /* Remap all BARs */
+       /* Remap mapped BARs only */
        for (i = 0; i != PCI_MAX_RESOURCE; i++) {
-               /* skip empty BAR */
-               if (dev->mem_resource[i].phys_addr == 0)
+               /* skip empty or unmapped BAR */
+               if (dev->mem_resource[i].phys_addr == 0 ||
+                               dev->mem_resource[i].addr == NULL)
                        continue;
                map_address = mmap(dev->mem_resource[i].addr,
                                (size_t)dev->mem_resource[i].len,
diff --git a/drivers/bus/pci/private.h b/drivers/bus/pci/private.h
index f266a1b04d..09c035112c 100644
--- a/drivers/bus/pci/private.h
+++ b/drivers/bus/pci/private.h
@@ -43,8 +43,22 @@ struct rte_pci_device_internal {
        struct rte_pci_device device;
        /* PCI regions provided by e.g. VFIO. */
        struct rte_pci_region region[RTE_MAX_PCI_REGIONS];
+       uint8_t bar_mask;       /* 0 = map all BARs. Set by 
rte_pci_map_device_bars(). */
 };
 
+#define PCI_BAR_MASK_ALL ((1U << PCI_MAX_RESOURCE) - 1)
+
+static inline uint8_t
+pci_get_bar_mask(struct rte_pci_device *dev)
+{
+       struct rte_pci_device_internal *pdev = RTE_PCI_DEVICE_INTERNAL(dev);
+       uint8_t mask = pdev->bar_mask;
+
+       if (mask == 0)
+               mask = PCI_BAR_MASK_ALL;
+       return mask;
+}
+
 /**
  * Scan the content of the PCI bus, and the devices in the devices
  * list
diff --git a/drivers/bus/pci/rte_bus_pci.h b/drivers/bus/pci/rte_bus_pci.h
index 22a7beb1fc..72e3ce1395 100644
--- a/drivers/bus/pci/rte_bus_pci.h
+++ b/drivers/bus/pci/rte_bus_pci.h
@@ -51,6 +51,23 @@ struct rte_devargs;
  */
 int rte_pci_map_device(struct rte_pci_device *dev);
 
+/**
+ * Map a selected set of PCI BARs.
+ *
+ * Bit N of bar_mask selects BAR N (0-5). A zero mask maps every valid BAR,
+ * which is what rte_pci_map_device() does. The driver calls this from probe()
+ * and must not set RTE_PCI_DRV_NEED_MAPPING, or the bus maps every BAR first.
+ *
+ * @param dev
+ *   PCI device to map.
+ * @param bar_mask
+ *   Bitmask of BARs to map.
+ * @return
+ *   0 on success, negative on error and positive if no driver is found.
+ */
+__rte_internal
+int rte_pci_map_device_bars(struct rte_pci_device *dev, uint8_t bar_mask);
+
 /**
  * Unmap this device
  *
diff --git a/drivers/bus/pci/windows/pci.c b/drivers/bus/pci/windows/pci.c
index 383c6bdc2f..9706033938 100644
--- a/drivers/bus/pci/windows/pci.c
+++ b/drivers/bus/pci/windows/pci.c
@@ -42,6 +42,18 @@ RTE_EXPORT_SYMBOL(rte_pci_map_device)
 int
 rte_pci_map_device(struct rte_pci_device *dev)
 {
+       return rte_pci_map_device_bars(dev, 0);
+}
+
+RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_map_device_bars)
+int
+rte_pci_map_device_bars(struct rte_pci_device *dev, uint8_t bar_mask)
+{
+       if (bar_mask > PCI_BAR_MASK_ALL)
+               return -EINVAL;
+
+       RTE_PCI_DEVICE_INTERNAL(dev)->bar_mask = bar_mask;
+
        /* Only return success for devices bound to netuio.
         * Devices that are bound to netuio are mapped at
         * the bus probing stage.
-- 
2.43.0

Reply via email to