From patchwork Wed Feb 13 09:10:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shahaf Shuler X-Patchwork-Id: 50293 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id A7F6E1B1F0; Wed, 13 Feb 2019 10:11:27 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 2B07E7CCA for ; Wed, 13 Feb 2019 10:11:20 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from shahafs@mellanox.com) with ESMTPS (AES256-SHA encrypted); 13 Feb 2019 11:11:18 +0200 Received: from unicorn01.mtl.labs.mlnx. (unicorn01.mtl.labs.mlnx [10.7.12.62]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id x1D9BIOk031440; Wed, 13 Feb 2019 11:11:18 +0200 From: Shahaf Shuler To: anatoly.burakov@intel.com, yskoh@mellanox.com, thomas@monjalon.net, ferruh.yigit@intel.com, nhorman@tuxdriver.com, gaetan.rivet@6wind.com Cc: dev@dpdk.org Date: Wed, 13 Feb 2019 11:10:21 +0200 Message-Id: <0f98e0a06c535d58ac8d2c39645b07eb966ec5ec.1550048187.git.shahafs@mellanox.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH 1/6] vfio: allow DMA map of memory for the default vfio fd X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Enable users the option to call rte_vfio_dma_map with request to map to the default vfio fd. Signed-off-by: Shahaf Shuler --- lib/librte_eal/common/include/rte_vfio.h | 6 ++++-- lib/librte_eal/linuxapp/eal/eal_vfio.c | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/librte_eal/common/include/rte_vfio.h b/lib/librte_eal/common/include/rte_vfio.h index cae96fab90..2a6827012f 100644 --- a/lib/librte_eal/common/include/rte_vfio.h +++ b/lib/librte_eal/common/include/rte_vfio.h @@ -347,7 +347,8 @@ rte_vfio_container_group_unbind(int container_fd, int iommu_group_num); * Perform DMA mapping for devices in a container. * * @param container_fd - * the specified container fd + * the specified container fd. In case of -1 the default container + * fd will be used. * * @param vaddr * Starting virtual address of memory to be mapped. @@ -370,7 +371,8 @@ rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, * Perform DMA unmapping for devices in a container. * * @param container_fd - * the specified container fd + * the specified container fd. In case of -1 the default container + * fd will be used. * * @param vaddr * Starting virtual address of memory to be unmapped. diff --git a/lib/librte_eal/linuxapp/eal/eal_vfio.c b/lib/librte_eal/linuxapp/eal/eal_vfio.c index c821e83826..48ca9465d4 100644 --- a/lib/librte_eal/linuxapp/eal/eal_vfio.c +++ b/lib/librte_eal/linuxapp/eal/eal_vfio.c @@ -1897,7 +1897,12 @@ rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova, return -1; } - vfio_cfg = get_vfio_cfg_by_container_fd(container_fd); + if (container_fd > 0) { + vfio_cfg = get_vfio_cfg_by_container_fd(container_fd); + } else { + /* when no fd provided use the default. */ + vfio_cfg = &vfio_cfgs[0]; + } if (vfio_cfg == NULL) { RTE_LOG(ERR, EAL, "Invalid container fd\n"); return -1; @@ -1917,7 +1922,12 @@ rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova, return -1; } - vfio_cfg = get_vfio_cfg_by_container_fd(container_fd); + if (container_fd > 0) { + vfio_cfg = get_vfio_cfg_by_container_fd(container_fd); + } else { + /* when no fd provided use the default. */ + vfio_cfg = &vfio_cfgs[0]; + } if (vfio_cfg == NULL) { RTE_LOG(ERR, EAL, "Invalid container fd\n"); return -1; From patchwork Wed Feb 13 09:10:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shahaf Shuler X-Patchwork-Id: 50295 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 0AF791B201; Wed, 13 Feb 2019 10:11:30 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 1A4FB1B185 for ; Wed, 13 Feb 2019 10:11:24 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from shahafs@mellanox.com) with ESMTPS (AES256-SHA encrypted); 13 Feb 2019 11:11:19 +0200 Received: from unicorn01.mtl.labs.mlnx. (unicorn01.mtl.labs.mlnx [10.7.12.62]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id x1D9BIOl031440; Wed, 13 Feb 2019 11:11:18 +0200 From: Shahaf Shuler To: anatoly.burakov@intel.com, yskoh@mellanox.com, thomas@monjalon.net, ferruh.yigit@intel.com, nhorman@tuxdriver.com, gaetan.rivet@6wind.com Cc: dev@dpdk.org Date: Wed, 13 Feb 2019 11:10:22 +0200 Message-Id: X-Mailer: git-send-email 2.12.0 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH 2/6] vfio: don't fail to DMA map if memory is already mapped X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Currently vfio DMA map function will fail in case the same memory segment is mapped twice. This is too strict, as this is not an error to map the same memory twice. Instead, use the kernel return value to detect such state and have the DMA function to return as successful. For type1 mapping the kernel driver first implementation returns EBUSY and since kernel 3.11 returns EEXISTS. For spapr mapping EBUSY is returned since kernel 4.10. Signed-off-by: Shahaf Shuler --- lib/librte_eal/linuxapp/eal/eal_vfio.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/librte_eal/linuxapp/eal/eal_vfio.c b/lib/librte_eal/linuxapp/eal/eal_vfio.c index 48ca9465d4..2a2d655b37 100644 --- a/lib/librte_eal/linuxapp/eal/eal_vfio.c +++ b/lib/librte_eal/linuxapp/eal/eal_vfio.c @@ -1263,7 +1263,11 @@ vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova, VFIO_DMA_MAP_FLAG_WRITE; ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map); - if (ret) { + /** + * In case the mapping was already done EEXIST will be + * returned from kernel. + */ + if ((ret != -EEXIST) && (ret != -EBUSY)) { RTE_LOG(ERR, EAL, " cannot set up DMA remapping, error %i (%s)\n", errno, strerror(errno)); return -1; @@ -1324,7 +1328,11 @@ vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova, VFIO_DMA_MAP_FLAG_WRITE; ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map); - if (ret) { + /** + * In case the mapping was already done EBUSY will be + * returned from kernel. + */ + if (ret != -EBUSY) { RTE_LOG(ERR, EAL, " cannot set up DMA remapping, error %i (%s)\n", errno, strerror(errno)); return -1; From patchwork Wed Feb 13 09:10:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shahaf Shuler X-Patchwork-Id: 50290 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 1644F1B147; Wed, 13 Feb 2019 10:11:23 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 316951B0F7 for ; Wed, 13 Feb 2019 10:11:20 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from shahafs@mellanox.com) with ESMTPS (AES256-SHA encrypted); 13 Feb 2019 11:11:18 +0200 Received: from unicorn01.mtl.labs.mlnx. (unicorn01.mtl.labs.mlnx [10.7.12.62]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id x1D9BIOm031440; Wed, 13 Feb 2019 11:11:18 +0200 From: Shahaf Shuler To: anatoly.burakov@intel.com, yskoh@mellanox.com, thomas@monjalon.net, ferruh.yigit@intel.com, nhorman@tuxdriver.com, gaetan.rivet@6wind.com Cc: dev@dpdk.org Date: Wed, 13 Feb 2019 11:10:23 +0200 Message-Id: <323319abdbdc238c3586dafe9ad49dab554d6e64.1550048188.git.shahafs@mellanox.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH 3/6] bus: introduce DMA memory mapping for external memory X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The DPDK APIs expose 3 different modes to work with memory used for DMA: 1. Use the DPDK owned memory (backed by the DPDK provided hugepages). This memory is allocated by the DPDK libraries, included in the DPDK memory system (memseg lists) and automatically DMA mapped by the DPDK layers. 2. Use memory allocated by the user and register to the DPDK memory systems. This is also referred as external memory. Upon registration of the external memory, the DPDK layers will DMA map it to all needed devices. 3. Use memory allocated by the user and not registered to the DPDK memory system. This is for users who wants to have tight control on this memory. The user will need to explicitly call DMA map function in order to register such memory to the different devices. The scope of the patch focus on #3 above. Currently the only way to map external memory is through VFIO (rte_vfio_dma_map). While VFIO is common, there are other vendors which use different ways to map memory (e.g. Mellanox and NXP). The work in this patch moves the DMA mapping to vendor agnostic APIs. A new map and unmap ops were added to rte_bus structure. Implementation of those was done currently only on the PCI bus. The implementation takes the driver map and umap implementation as bypass to the VFIO mapping. That is, in case of no specific map/unmap from the PCI driver, VFIO mapping, if possible, will be used. Application use with those APIs is quite simple: * allocate memory * take a device, and query its rte_device. * call the bus map function for this device. Future work will deprecate the rte_vfio_dma_map and rte_vfio_dma_unmap APIs, leaving the PCI device APIs as the preferred option for the user. Signed-off-by: Shahaf Shuler --- drivers/bus/pci/pci_common.c | 78 ++++++++++++++++++++++++++++ drivers/bus/pci/rte_bus_pci.h | 14 +++++ lib/librte_eal/common/eal_common_bus.c | 22 ++++++++ lib/librte_eal/common/include/rte_bus.h | 57 ++++++++++++++++++++ lib/librte_eal/rte_eal_version.map | 2 + 5 files changed, 173 insertions(+) diff --git a/drivers/bus/pci/pci_common.c b/drivers/bus/pci/pci_common.c index 6276e5d695..018080c48b 100644 --- a/drivers/bus/pci/pci_common.c +++ b/drivers/bus/pci/pci_common.c @@ -528,6 +528,82 @@ pci_unplug(struct rte_device *dev) return ret; } +/** + * DMA Map memory segment to device. After a successful call the device + * will be able to read/write from/to this segment. + * + * @param dev + * Pointer to the PCI device. + * @param addr + * Starting virtual address of memory to be mapped. + * @param iova + * Starting IOVA address of memory to be mapped. + * @param len + * Length of memory segment being mapped. + * @return + * - 0 On success. + * - Negative value and rte_errno is set otherwise. + */ +static int __rte_experimental +pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len) +{ + struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev); + + if (!pdev || !pdev->driver) { + rte_errno = EINVAL; + return -rte_errno; + } + if (pdev->driver->map) + return pdev->driver->map(pdev, addr, iova, len); + /** + * In case driver don't provides any specific mapping + * try fallback to VFIO. + */ + if (pdev->kdrv == RTE_KDRV_VFIO) + return rte_vfio_container_dma_map(-1, (uintptr_t)addr, iova, + len); + rte_errno = ENOTSUP; + return -rte_errno; +} + +/** + * Un-map memory segment to device. After a successful call the device + * will not be able to read/write from/to this segment. + * + * @param dev + * Pointer to the PCI device. + * @param addr + * Starting virtual address of memory to be unmapped. + * @param iova + * Starting IOVA address of memory to be unmapped. + * @param len + * Length of memory segment being unmapped. + * @return + * - 0 On success. + * - Negative value and rte_errno is set otherwise. + */ +static int __rte_experimental +pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len) +{ + struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev); + + if (!pdev || !pdev->driver) { + rte_errno = EINVAL; + return -rte_errno; + } + if (pdev->driver->unmap) + return pdev->driver->unmap(pdev, addr, iova, len); + /** + * In case driver don't provides any specific mapping + * try fallback to VFIO. + */ + if (pdev->kdrv == RTE_KDRV_VFIO) + return rte_vfio_container_dma_unmap(-1, (uintptr_t)addr, iova, + len); + rte_errno = ENOTSUP; + return -rte_errno; +} + struct rte_pci_bus rte_pci_bus = { .bus = { .scan = rte_pci_scan, @@ -536,6 +612,8 @@ struct rte_pci_bus rte_pci_bus = { .plug = pci_plug, .unplug = pci_unplug, .parse = pci_parse, + .map = pci_dma_map, + .unmap = pci_dma_unmap, .get_iommu_class = rte_pci_get_iommu_class, .dev_iterate = rte_pci_dev_iterate, .hot_unplug_handler = pci_hot_unplug_handler, diff --git a/drivers/bus/pci/rte_bus_pci.h b/drivers/bus/pci/rte_bus_pci.h index f0d6d81c00..00b2d412c7 100644 --- a/drivers/bus/pci/rte_bus_pci.h +++ b/drivers/bus/pci/rte_bus_pci.h @@ -114,6 +114,18 @@ typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *); typedef int (pci_remove_t)(struct rte_pci_device *); /** + * Driver-specific DMA mapping. + */ +typedef int (pci_dma_map_t)(struct rte_pci_device *dev, void *addr, + uint64_t iova, size_t len); + +/** + * Driver-specific DMA unmapping. + */ +typedef int (pci_dma_unmap_t)(struct rte_pci_device *dev, void *addr, + uint64_t iova, size_t len); + +/** * A structure describing a PCI driver. */ struct rte_pci_driver { @@ -122,6 +134,8 @@ struct rte_pci_driver { struct rte_pci_bus *bus; /**< PCI bus reference. */ pci_probe_t *probe; /**< Device Probe function. */ pci_remove_t *remove; /**< Device Remove function. */ + pci_dma_map_t *map; /**< device dma map function. */ + pci_dma_unmap_t *unmap; /**< device dma unmap function. */ const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */ uint32_t drv_flags; /**< Flags RTE_PCI_DRV_*. */ }; diff --git a/lib/librte_eal/common/eal_common_bus.c b/lib/librte_eal/common/eal_common_bus.c index c8f1901f0b..b7911d5ddd 100644 --- a/lib/librte_eal/common/eal_common_bus.c +++ b/lib/librte_eal/common/eal_common_bus.c @@ -285,3 +285,25 @@ rte_bus_sigbus_handler(const void *failure_addr) return ret; } + +int __rte_experimental +rte_bus_dma_map(struct rte_device *dev, void *addr, uint64_t iova, + size_t len) +{ + if (dev->bus->map == NULL || len == 0) { + rte_errno = EINVAL; + return -rte_errno; + } + return dev->bus->map(dev, addr, iova, len); +} + +int __rte_experimental +rte_bus_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, + size_t len) +{ + if (dev->bus->unmap == NULL || len == 0) { + rte_errno = EINVAL; + return -rte_errno; + } + return dev->bus->unmap(dev, addr, iova, len); +} diff --git a/lib/librte_eal/common/include/rte_bus.h b/lib/librte_eal/common/include/rte_bus.h index 6be4b5cabe..90e4bf51b2 100644 --- a/lib/librte_eal/common/include/rte_bus.h +++ b/lib/librte_eal/common/include/rte_bus.h @@ -168,6 +168,48 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev); typedef int (*rte_bus_parse_t)(const char *name, void *addr); /** + * Bus specific DMA map function. + * After a successful call, the memory segment will be mapped to the + * given device. + * + * @param dev + * Device pointer. + * @param addr + * Virtual address to map. + * @param iova + * IOVA address to map. + * @param len + * Length of the memory segment being mapped. + * + * @return + * 0 if mapping was successful. + * Negative value and rte_errno is set otherwise. + */ +typedef int (*rte_bus_map_t)(struct rte_device *dev, void *addr, + uint64_t iova, size_t len); + +/** + * Bus specific DMA unmap function. + * After a successful call, the memory segment will no longer be + * accessible by the given device. + * + * @param dev + * Device pointer. + * @param addr + * Virtual address to unmap. + * @param iova + * IOVA address to unmap. + * @param len + * Length of the memory segment being mapped. + * + * @return + * 0 if un-mapping was successful. + * Negative value and rte_errno is set otherwise. + */ +typedef int (*rte_bus_unmap_t)(struct rte_device *dev, void *addr, + uint64_t iova, size_t len); + +/** * Implement a specific hot-unplug handler, which is responsible for * handle the failure when device be hot-unplugged. When the event of * hot-unplug be detected, it could call this function to handle @@ -238,6 +280,8 @@ struct rte_bus { rte_bus_plug_t plug; /**< Probe single device for drivers */ rte_bus_unplug_t unplug; /**< Remove single device from driver */ rte_bus_parse_t parse; /**< Parse a device name */ + rte_bus_map_t map; /**< DMA map for device in the bus */ + rte_bus_unmap_t unmap; /**< DMA unmap for device in the bus */ struct rte_bus_conf conf; /**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu class */ rte_dev_iterate_t dev_iterate; /**< Device iterator. */ @@ -356,6 +400,19 @@ struct rte_bus *rte_bus_find_by_name(const char *busname); enum rte_iova_mode rte_bus_get_iommu_class(void); /** + * Wrapper to call the bus specific DMA map function. + */ +int __rte_experimental +rte_bus_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len); + +/** + * Wrapper to call the bus specific DMA unmap function. + */ +int __rte_experimental +rte_bus_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, + size_t len); + +/** * Helper for Bus registration. * The constructor has higher priority than PMD constructors. */ diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map index eb5f7b9cbd..23f3adb73a 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -364,4 +364,6 @@ EXPERIMENTAL { rte_service_may_be_active; rte_socket_count; rte_socket_id_by_idx; + rte_bus_dma_map; + rte_bus_dma_unmap; }; From patchwork Wed Feb 13 09:10:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shahaf Shuler X-Patchwork-Id: 50292 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 99AA11B1EB; Wed, 13 Feb 2019 10:11:26 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 276056CD8 for ; Wed, 13 Feb 2019 10:11:20 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from shahafs@mellanox.com) with ESMTPS (AES256-SHA encrypted); 13 Feb 2019 11:11:18 +0200 Received: from unicorn01.mtl.labs.mlnx. (unicorn01.mtl.labs.mlnx [10.7.12.62]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id x1D9BIOn031440; Wed, 13 Feb 2019 11:11:18 +0200 From: Shahaf Shuler To: anatoly.burakov@intel.com, yskoh@mellanox.com, thomas@monjalon.net, ferruh.yigit@intel.com, nhorman@tuxdriver.com, gaetan.rivet@6wind.com Cc: dev@dpdk.org Date: Wed, 13 Feb 2019 11:10:24 +0200 Message-Id: <7114601286e69b4a2c14036843d77f2e65d6f530.1550048188.git.shahafs@mellanox.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH 4/6] net/mlx5: refactor external memory registration X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Move the memory region creation to a separate function to prepare the ground for the reuse of it on the PCI driver map and unmap functions. Signed-off-by: Shahaf Shuler --- drivers/net/mlx5/mlx5_mr.c | 86 +++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/drivers/net/mlx5/mlx5_mr.c b/drivers/net/mlx5/mlx5_mr.c index 442b2d2321..32be6a5445 100644 --- a/drivers/net/mlx5/mlx5_mr.c +++ b/drivers/net/mlx5/mlx5_mr.c @@ -1109,6 +1109,58 @@ mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl) } /** + * Creates a memory region for external memory, that is memory which is not + * part of the DPDK memory segments. + * + * @param dev + * Pointer to the ethernet device. + * @param addr + * Starting virtual address of memory. + * @param len + * Length of memory segment being mapped. + * @param socked_id + * Socket to allocate heap memory for the control structures. + * + * @return + * Pointer to MR structure on success, NULL otherwise. + */ +static struct mlx5_mr * +mlx5_create_mr_ext(struct rte_eth_dev *dev, uintptr_t addr, size_t len, + int socket_id) +{ + struct priv *priv = dev->data->dev_private; + struct mlx5_mr *mr = NULL; + + mr = rte_zmalloc_socket(NULL, + RTE_ALIGN_CEIL(sizeof(*mr), + RTE_CACHE_LINE_SIZE), + RTE_CACHE_LINE_SIZE, socket_id); + if (mr == NULL) + return NULL; + mr->ibv_mr = mlx5_glue->reg_mr(priv->pd, (void *)addr, len, + IBV_ACCESS_LOCAL_WRITE); + if (mr->ibv_mr == NULL) { + DRV_LOG(WARNING, + "port %u fail to create a verbs MR for address (%p)", + dev->data->port_id, (void *)addr); + rte_free(mr); + return NULL; + } + mr->msl = NULL; /* Mark it is external memory. */ + mr->ms_bmp = NULL; + mr->ms_n = 1; + mr->ms_bmp_n = 1; + DRV_LOG(DEBUG, + "port %u MR CREATED (%p) for external memory %p:\n" + " [0x%" PRIxPTR ", 0x%" PRIxPTR ")," + " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u", + dev->data->port_id, (void *)mr, (void *)addr, + addr, addr + len, rte_cpu_to_be_32(mr->ibv_mr->lkey), + mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n); + return mr; +} + +/** * Called during rte_mempool_mem_iter() by mlx5_mr_update_ext_mp(). * * Externally allocated chunk is registered and a MR is created for the chunk. @@ -1142,43 +1194,19 @@ mlx5_mr_update_ext_mp_cb(struct rte_mempool *mp, void *opaque, rte_rwlock_read_unlock(&priv->mr.rwlock); if (lkey != UINT32_MAX) return; - mr = rte_zmalloc_socket(NULL, - RTE_ALIGN_CEIL(sizeof(*mr), - RTE_CACHE_LINE_SIZE), - RTE_CACHE_LINE_SIZE, mp->socket_id); - if (mr == NULL) { - DRV_LOG(WARNING, - "port %u unable to allocate memory for a new MR of" - " mempool (%s).", - dev->data->port_id, mp->name); - data->ret = -1; - return; - } DRV_LOG(DEBUG, "port %u register MR for chunk #%d of mempool (%s)", dev->data->port_id, mem_idx, mp->name); - mr->ibv_mr = mlx5_glue->reg_mr(priv->pd, (void *)addr, len, - IBV_ACCESS_LOCAL_WRITE); - if (mr->ibv_mr == NULL) { + mr = mlx5_create_mr_ext(dev, addr, len, mp->socket_id); + if (!mr) { DRV_LOG(WARNING, - "port %u fail to create a verbs MR for address (%p)", - dev->data->port_id, (void *)addr); - rte_free(mr); + "port %u unable to allocate a new MR of" + " mempool (%s).", + dev->data->port_id, mp->name); data->ret = -1; return; } - mr->msl = NULL; /* Mark it is external memory. */ - mr->ms_bmp = NULL; - mr->ms_n = 1; - mr->ms_bmp_n = 1; rte_rwlock_write_lock(&priv->mr.rwlock); LIST_INSERT_HEAD(&priv->mr.mr_list, mr, mr); - DRV_LOG(DEBUG, - "port %u MR CREATED (%p) for external memory %p:\n" - " [0x%" PRIxPTR ", 0x%" PRIxPTR ")," - " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u", - dev->data->port_id, (void *)mr, (void *)addr, - addr, addr + len, rte_cpu_to_be_32(mr->ibv_mr->lkey), - mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n); /* Insert to the global cache table. */ mr_insert_dev_cache(dev, mr); rte_rwlock_write_unlock(&priv->mr.rwlock); From patchwork Wed Feb 13 09:10:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shahaf Shuler X-Patchwork-Id: 50289 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id EAAEE7CCA; Wed, 13 Feb 2019 10:11:21 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 2E6107CDA for ; Wed, 13 Feb 2019 10:11:20 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from shahafs@mellanox.com) with ESMTPS (AES256-SHA encrypted); 13 Feb 2019 11:11:18 +0200 Received: from unicorn01.mtl.labs.mlnx. (unicorn01.mtl.labs.mlnx [10.7.12.62]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id x1D9BIOo031440; Wed, 13 Feb 2019 11:11:18 +0200 From: Shahaf Shuler To: anatoly.burakov@intel.com, yskoh@mellanox.com, thomas@monjalon.net, ferruh.yigit@intel.com, nhorman@tuxdriver.com, gaetan.rivet@6wind.com Cc: dev@dpdk.org Date: Wed, 13 Feb 2019 11:10:25 +0200 Message-Id: X-Mailer: git-send-email 2.12.0 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH 5/6] net/mlx5: support PCI device DMA map and unmap X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The implementation reuses the external memory registration work done by commit[1]. Note about representors: The current representor design will not work with those map and unmap functions. The reason is that for representors we have multiple IB devices share the same PCI function, so mapping will happen only on one of the representors and not all of them. While it is possible to implement such support, the IB representor design is going to be changed during DPDK19.05. The new design will have a single IB device for all representors, hence sharing of a single memory region between all representors will be possible. [1] commit 7e43a32ee060 ("net/mlx5: support externally allocated static memory") Signed-off-by: Shahaf Shuler --- drivers/net/mlx5/mlx5.c | 2 + drivers/net/mlx5/mlx5_mr.c | 146 ++++++++++++++++++++++++++++++++++++++ drivers/net/mlx5/mlx5_rxtx.h | 5 ++ 3 files changed, 153 insertions(+) diff --git a/drivers/net/mlx5/mlx5.c b/drivers/net/mlx5/mlx5.c index a913a5955f..7c91701713 100644 --- a/drivers/net/mlx5/mlx5.c +++ b/drivers/net/mlx5/mlx5.c @@ -1626,6 +1626,8 @@ static struct rte_pci_driver mlx5_driver = { .id_table = mlx5_pci_id_map, .probe = mlx5_pci_probe, .remove = mlx5_pci_remove, + .map = mlx5_dma_map, + .unmap = mlx5_dma_unmap, .drv_flags = (RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV | RTE_PCI_DRV_PROBE_AGAIN), }; diff --git a/drivers/net/mlx5/mlx5_mr.c b/drivers/net/mlx5/mlx5_mr.c index 32be6a5445..7059181959 100644 --- a/drivers/net/mlx5/mlx5_mr.c +++ b/drivers/net/mlx5/mlx5_mr.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "mlx5.h" #include "mlx5_mr.h" @@ -1215,6 +1216,151 @@ mlx5_mr_update_ext_mp_cb(struct rte_mempool *mp, void *opaque, } /** + * Finds the first ethdev that match the pci device. + * The existence of multiple ethdev per pci device is only with representors. + * On such case, it is enough to get only one of the ports as they all share + * the same ibv context. + * + * @param pdev + * Pointer to the PCI device. + * + * @return + * Pointer to the ethdev if found, NULL otherwise. + */ +static struct rte_eth_dev * +pci_dev_to_eth_dev(struct rte_pci_device *pdev) +{ + struct rte_eth_dev *dev; + const char *drv_name; + uint16_t port_id = 0; + + /** + * We really need to iterate all eth devices regardless of + * their owner. + */ + while (port_id < RTE_MAX_ETHPORTS) { + port_id = rte_eth_find_next(port_id); + if (port_id >= RTE_MAX_ETHPORTS) + break; + dev = &rte_eth_devices[port_id]; + drv_name = dev->device->driver->name; + if (!strncmp(drv_name, MLX5_DRIVER_NAME, + sizeof(MLX5_DRIVER_NAME) + 1) && + pdev == RTE_DEV_TO_PCI(dev->device)) { + /* found the PCI device. */ + return dev; + } + } + return NULL; +} + +/** + * DPDK callback to DMA map external memory to a PCI device. + * + * @param pdev + * Pointer to the PCI device. + * @param addr + * Starting virtual address of memory to be mapped. + * @param iova + * Starting IOVA address of memory to be mapped. + * @param len + * Length of memory segment being mapped. + * + * @return + * 0 on success, negative value on error. + */ +int +mlx5_dma_map(struct rte_pci_device *pdev, void *addr, + uint64_t iova __rte_unused, size_t len) +{ + struct rte_eth_dev *dev; + struct mlx5_mr *mr; + struct priv *priv; + + dev = pci_dev_to_eth_dev(pdev); + if (!dev) { + DRV_LOG(WARNING, "unable to find matching ethdev " + "to PCI device %p", (void *)pdev); + return -1; + } + priv = dev->data->dev_private; + mr = mlx5_create_mr_ext(dev, (uintptr_t)addr, len, SOCKET_ID_ANY); + if (!mr) { + DRV_LOG(WARNING, + "port %u unable to dma map", dev->data->port_id); + return -1; + } + rte_rwlock_write_lock(&priv->mr.rwlock); + LIST_INSERT_HEAD(&priv->mr.mr_list, mr, mr); + /* Insert to the global cache table. */ + mr_insert_dev_cache(dev, mr); + rte_rwlock_write_unlock(&priv->mr.rwlock); + return 0; +} + +/** + * DPDK callback to DMA unmap external memory to a PCI device. + * + * @param pdev + * Pointer to the PCI device. + * @param addr + * Starting virtual address of memory to be unmapped. + * @param iova + * Starting IOVA address of memory to be unmapped. + * @param len + * Length of memory segment being unmapped. + * + * @return + * 0 on success, negative value on error. + */ +int +mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr, + uint64_t iova __rte_unused, size_t len __rte_unused) +{ + struct rte_eth_dev *dev; + struct priv *priv; + struct mlx5_mr *mr; + struct mlx5_mr_cache entry; + + dev = pci_dev_to_eth_dev(pdev); + if (!dev) { + DRV_LOG(WARNING, "unable to find matching ethdev " + "to PCI device %p", (void *)pdev); + return -1; + } + priv = dev->data->dev_private; + rte_rwlock_read_lock(&priv->mr.rwlock); + mr = mr_lookup_dev_list(dev, &entry, (uintptr_t)addr); + if (!mr) { + DRV_LOG(WARNING, "address 0x%" PRIxPTR " wasn't registered " + "to PCI device %p", (uintptr_t)addr, + (void *)pdev); + rte_rwlock_read_unlock(&priv->mr.rwlock); + return -1; + } + LIST_REMOVE(mr, mr); + LIST_INSERT_HEAD(&priv->mr.mr_free_list, mr, mr); + DEBUG("port %u remove MR(%p) from list", dev->data->port_id, + (void *)mr); + mr_rebuild_dev_cache(dev); + /* + * Flush local caches by propagating invalidation across cores. + * rte_smp_wmb() is enough to synchronize this event. If one of + * freed memsegs is seen by other core, that means the memseg + * has been allocated by allocator, which will come after this + * free call. Therefore, this store instruction (incrementing + * generation below) will be guaranteed to be seen by other core + * before the core sees the newly allocated memory. + */ + ++priv->mr.dev_gen; + DEBUG("broadcasting local cache flush, gen=%d", + priv->mr.dev_gen); + rte_smp_wmb(); + rte_rwlock_read_unlock(&priv->mr.rwlock); + return 0; +} + +/** * Register MR for entire memory chunks in a Mempool having externally allocated * memory and fill in local cache. * diff --git a/drivers/net/mlx5/mlx5_rxtx.h b/drivers/net/mlx5/mlx5_rxtx.h index c2529f96bc..f3f84dbac3 100644 --- a/drivers/net/mlx5/mlx5_rxtx.h +++ b/drivers/net/mlx5/mlx5_rxtx.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "mlx5_utils.h" #include "mlx5.h" @@ -367,6 +368,10 @@ uint32_t mlx5_rx_addr2mr_bh(struct mlx5_rxq_data *rxq, uintptr_t addr); uint32_t mlx5_tx_mb2mr_bh(struct mlx5_txq_data *txq, struct rte_mbuf *mb); uint32_t mlx5_tx_update_ext_mp(struct mlx5_txq_data *txq, uintptr_t addr, struct rte_mempool *mp); +int mlx5_dma_map(struct rte_pci_device *pdev, void *addr, uint64_t iova, + size_t len); +int mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr, uint64_t iova, + size_t len); /** * Provide safe 64bit store operation to mlx5 UAR region for both 32bit and From patchwork Wed Feb 13 09:10:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shahaf Shuler X-Patchwork-Id: 50294 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 974071B1F7; Wed, 13 Feb 2019 10:11:28 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 3620B1B138 for ; Wed, 13 Feb 2019 10:11:20 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from shahafs@mellanox.com) with ESMTPS (AES256-SHA encrypted); 13 Feb 2019 11:11:19 +0200 Received: from unicorn01.mtl.labs.mlnx. (unicorn01.mtl.labs.mlnx [10.7.12.62]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id x1D9BIOp031440; Wed, 13 Feb 2019 11:11:18 +0200 From: Shahaf Shuler To: anatoly.burakov@intel.com, yskoh@mellanox.com, thomas@monjalon.net, ferruh.yigit@intel.com, nhorman@tuxdriver.com, gaetan.rivet@6wind.com Cc: dev@dpdk.org Date: Wed, 13 Feb 2019 11:10:26 +0200 Message-Id: <385f57ec46cf2c0e0ce6a23cca9d14d415a84f70.1550048188.git.shahafs@mellanox.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH 6/6] doc: deprecate VFIO DMA map APIs X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" As those have been replaced by rte_bus_dma_map and rte_pci_dma_unmap APIs. Signed-off-by: Shahaf Shuler --- doc/guides/prog_guide/env_abstraction_layer.rst | 2 +- doc/guides/rel_notes/deprecation.rst | 4 ++++ lib/librte_eal/common/include/rte_vfio.h | 6 ++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/doc/guides/prog_guide/env_abstraction_layer.rst b/doc/guides/prog_guide/env_abstraction_layer.rst index 929d76dba7..6a1ddf8b4a 100644 --- a/doc/guides/prog_guide/env_abstraction_layer.rst +++ b/doc/guides/prog_guide/env_abstraction_layer.rst @@ -282,7 +282,7 @@ The expected workflow is as follows: - If IOVA table is not specified, IOVA addresses will be assumed to be unavailable - Other processes must attach to the memory area before they can use it -* Perform DMA mapping with ``rte_vfio_dma_map`` if needed +* Perform DMA mapping with ``rte_bus_dma_map`` if needed * Use the memory area in your application * If memory area is no longer needed, it can be unregistered - If the area was mapped for DMA, unmapping must be performed before diff --git a/doc/guides/rel_notes/deprecation.rst b/doc/guides/rel_notes/deprecation.rst index 1b4fcb7e64..f7ae0d56fb 100644 --- a/doc/guides/rel_notes/deprecation.rst +++ b/doc/guides/rel_notes/deprecation.rst @@ -35,6 +35,10 @@ Deprecation Notices + ``rte_eal_devargs_type_count`` +* vfio: removal of ``rte_vfio_dma_map`` and ``rte_vfio_dma_unmap`` APIs which + have been replaced with ``rte_bus_dma_map`` and ``rte_bus_dma_unmap`` + functions. The due date for the removal targets DPDK 19.08. + * pci: Several exposed functions are misnamed. The following functions are deprecated starting from v17.11 and are replaced: diff --git a/lib/librte_eal/common/include/rte_vfio.h b/lib/librte_eal/common/include/rte_vfio.h index 2a6827012f..8dd2c5316d 100644 --- a/lib/librte_eal/common/include/rte_vfio.h +++ b/lib/librte_eal/common/include/rte_vfio.h @@ -188,6 +188,7 @@ int rte_vfio_clear_group(int vfio_group_fd); /** + * @deprecated * Map memory region for use with VFIO. * * @note Require at least one device to be attached at the time of @@ -208,11 +209,12 @@ rte_vfio_clear_group(int vfio_group_fd); * 0 if success. * -1 on error. */ -int +int __rte_deprecated rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len); /** + * @deprecated * Unmap memory region from VFIO. * * @param vaddr @@ -229,7 +231,7 @@ rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len); * -1 on error. */ -int +int __rte_deprecated rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len); /** * Parse IOMMU group number for a device