pci: use OS generic memory mapping functions
authorTal Shnaiderman <talshn@mellanox.com>
Mon, 29 Jun 2020 12:37:34 +0000 (15:37 +0300)
committerThomas Monjalon <thomas@monjalon.net>
Mon, 29 Jun 2020 22:02:54 +0000 (00:02 +0200)
Changing all of PCIs Unix memory mapping to the
new memory allocation API wrapper.

Change all of PCI mapping function usage in
bus/pci to support the new API.

Signed-off-by: Tal Shnaiderman <talshn@mellanox.com>
drivers/bus/pci/bsd/pci.c
drivers/bus/pci/linux/pci_uio.c
drivers/bus/pci/linux/pci_vfio.c
drivers/bus/pci/pci_common_uio.c
lib/librte_eal/rte_eal_exports.def
lib/librte_pci/rte_pci.c
lib/librte_pci/rte_pci.h

index 6ec27b4..8bc473e 100644 (file)
@@ -192,7 +192,7 @@ pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
        mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
                        (size_t)dev->mem_resource[res_idx].len, 0);
        close(fd);
-       if (mapaddr == MAP_FAILED)
+       if (mapaddr == NULL)
                goto error;
 
        maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
index 097dc19..b622001 100644 (file)
@@ -345,7 +345,7 @@ pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
        mapaddr = pci_map_resource(pci_map_addr, fd, 0,
                        (size_t)dev->mem_resource[res_idx].len, 0);
        close(fd);
-       if (mapaddr == MAP_FAILED)
+       if (mapaddr == NULL)
                goto error;
 
        pci_map_addr = RTE_PTR_ADD(mapaddr,
index ba60e7c..fdeb9a8 100644 (file)
@@ -14,6 +14,7 @@
 #include <rte_log.h>
 #include <rte_pci.h>
 #include <rte_bus_pci.h>
+#include <rte_eal_paging.h>
 #include <rte_malloc.h>
 #include <rte_vfio.h>
 #include <rte_eal.h>
@@ -561,11 +562,11 @@ pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
                        map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
                                                        memreg[0].offset,
                                                        memreg[0].size,
-                                                       MAP_FIXED);
+                                                       RTE_MAP_FORCE_ADDRESS);
                }
 
                /* if there's a second part, try to map it */
-               if (map_addr != MAP_FAILED
+               if (map_addr != NULL
                        && memreg[1].offset && memreg[1].size) {
                        void *second_addr = RTE_PTR_ADD(bar_addr,
                                                (uintptr_t)(memreg[1].offset -
@@ -574,10 +575,10 @@ pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
                                                        vfio_dev_fd,
                                                        memreg[1].offset,
                                                        memreg[1].size,
-                                                       MAP_FIXED);
+                                                       RTE_MAP_FORCE_ADDRESS);
                }
 
-               if (map_addr == MAP_FAILED || !map_addr) {
+               if (map_addr == NULL) {
                        munmap(bar_addr, bar->size);
                        bar_addr = MAP_FAILED;
                        RTE_LOG(ERR, EAL, "Failed to map pci BAR%d\n",
index f4dca9d..793dfd0 100644 (file)
@@ -58,7 +58,7 @@ pci_uio_map_secondary(struct rte_pci_device *dev)
                                        "Cannot mmap device resource file %s to address: %p\n",
                                        uio_res->maps[i].path,
                                        uio_res->maps[i].addr);
-                               if (mapaddr != MAP_FAILED) {
+                               if (mapaddr != NULL) {
                                        /* unmap addrs correctly mapped */
                                        for (j = 0; j < i; j++)
                                                pci_unmap_resource(
index e2eb24f..7f0c8f5 100644 (file)
@@ -46,6 +46,7 @@ EXPORTS
        rte_memzone_reserve_aligned
        rte_memzone_reserve_bounded
        rte_memzone_walk
+       rte_strerror
        rte_vlog
        rte_realloc
        rte_zmalloc
index 9c80c4b..d8272b9 100644 (file)
@@ -9,12 +9,12 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/queue.h>
-#include <sys/mman.h>
 
 #include <rte_errno.h>
 #include <rte_interrupts.h>
 #include <rte_log.h>
 #include <rte_bus.h>
+#include <rte_eal_paging.h>
 #include <rte_per_lcore.h>
 #include <rte_memory.h>
 #include <rte_eal.h>
@@ -154,14 +154,15 @@ pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
        void *mapaddr;
 
        /* Map the PCI memory resource of device */
-       mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
-                       MAP_SHARED | additional_flags, fd, offset);
-       if (mapaddr == MAP_FAILED) {
+       mapaddr = rte_mem_map(requested_addr, size,
+               RTE_PROT_READ | RTE_PROT_WRITE,
+               RTE_MAP_SHARED | additional_flags, fd, offset);
+       if (mapaddr == NULL) {
                RTE_LOG(ERR, EAL,
-                       "%s(): cannot mmap(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
+                       "%s(): cannot map resource(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
                        __func__, fd, requested_addr, size,
                        (unsigned long long)offset,
-                       strerror(errno), mapaddr);
+                       rte_strerror(rte_errno), mapaddr);
        } else
                RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
 
@@ -176,10 +177,10 @@ pci_unmap_resource(void *requested_addr, size_t size)
                return;
 
        /* Unmap the PCI memory resource of device */
-       if (munmap(requested_addr, size)) {
-               RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, %#zx): %s\n",
+       if (rte_mem_unmap(requested_addr, size)) {
+               RTE_LOG(ERR, EAL, "%s(): cannot mem unmap(%p, %#zx): %s\n",
                        __func__, requested_addr, size,
-                       strerror(errno));
+                       rte_strerror(rte_errno));
        } else
                RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
                                requested_addr);
index 4087771..b721bbf 100644 (file)
@@ -159,7 +159,7 @@ int rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr);
  *      The additional flags for the mapping range.
  * @return
  *   - On success, the function returns a pointer to the mapped area.
- *   - On error, the value MAP_FAILED is returned.
+ *   - On error, NULL is returned.
  */
 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
                size_t size, int additional_flags);