From 2bb7185f1b5e534ee672e9b18aa68982ec014fc3 Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Thu, 15 Sep 2022 13:30:38 +0000 Subject: [PATCH] env_dpdk: add dpdk_pci_device_vtophys() This moves the only references to the rte_pci_device data structure from memory.c to pci.c. This helps prepare SPDK for possible changes to DPDK around visibility of these DPDK data structures, making it easier for SPDK to manage if only one file is affected. Signed-off-by: Jim Harris Change-Id: I26b1907fabd7a6c23701523811abd1ce12606683 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14530 Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Changpeng Liu Reviewed-by: Aleksey Marchuk Reviewed-by: Ben Walker --- lib/env_dpdk/env_internal.h | 2 ++ lib/env_dpdk/memory.c | 17 ++++------------- lib/env_dpdk/pci.c | 19 +++++++++++++++++++ test/env/memory/memory_ut.c | 1 + 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/env_dpdk/env_internal.h b/lib/env_dpdk/env_internal.h index d41782aa6..a9e653136 100644 --- a/lib/env_dpdk/env_internal.h +++ b/lib/env_dpdk/env_internal.h @@ -52,6 +52,8 @@ void pci_env_fini(void); int mem_map_init(bool legacy_mem); int vtophys_init(void); +uint64_t dpdk_pci_device_vtophys(struct rte_pci_device *dev, uint64_t vaddr); + /** * Report a DMA-capable PCI device to the vtophys translation code. * Increases the refcount of active DMA-capable devices managed by SPDK. diff --git a/lib/env_dpdk/memory.c b/lib/env_dpdk/memory.c index 607ffa96f..3f6885f0c 100644 --- a/lib/env_dpdk/memory.c +++ b/lib/env_dpdk/memory.c @@ -930,23 +930,14 @@ vtophys_get_paddr_pci(uint64_t vaddr) struct spdk_vtophys_pci_device *vtophys_dev; uintptr_t paddr; struct rte_pci_device *dev; - struct rte_mem_resource *res; - unsigned r; pthread_mutex_lock(&g_vtophys_pci_devices_mutex); TAILQ_FOREACH(vtophys_dev, &g_vtophys_pci_devices, tailq) { dev = vtophys_dev->pci_device; - - for (r = 0; r < PCI_MAX_RESOURCE; r++) { - res = &dev->mem_resource[r]; - if (res->phys_addr && vaddr >= (uint64_t)res->addr && - vaddr < (uint64_t)res->addr + res->len) { - paddr = res->phys_addr + (vaddr - (uint64_t)res->addr); - DEBUG_PRINT("%s: %p -> %p\n", __func__, (void *)vaddr, - (void *)paddr); - pthread_mutex_unlock(&g_vtophys_pci_devices_mutex); - return paddr; - } + paddr = dpdk_pci_device_vtophys(dev, vaddr); + if (paddr != SPDK_VTOPHYS_ERROR) { + pthread_mutex_unlock(&g_vtophys_pci_devices_mutex); + return paddr; } } pthread_mutex_unlock(&g_vtophys_pci_devices_mutex); diff --git a/lib/env_dpdk/pci.c b/lib/env_dpdk/pci.c index 1e91af40a..3c571df2a 100644 --- a/lib/env_dpdk/pci.c +++ b/lib/env_dpdk/pci.c @@ -1232,3 +1232,22 @@ spdk_pci_device_allow(struct spdk_pci_addr *pci_addr) return 0; } + +uint64_t +dpdk_pci_device_vtophys(struct rte_pci_device *dev, uint64_t vaddr) +{ + struct rte_mem_resource *res; + uint64_t paddr; + unsigned r; + + for (r = 0; r < PCI_MAX_RESOURCE; r++) { + res = &dev->mem_resource[r]; + if (res->phys_addr && vaddr >= (uint64_t)res->addr && + vaddr < (uint64_t)res->addr + res->len) { + paddr = res->phys_addr + (vaddr - (uint64_t)res->addr); + return paddr; + } + } + + return SPDK_VTOPHYS_ERROR; +} diff --git a/test/env/memory/memory_ut.c b/test/env/memory/memory_ut.c index 5bc6bf76c..dd0b07367 100644 --- a/test/env/memory/memory_ut.c +++ b/test/env/memory/memory_ut.c @@ -29,6 +29,7 @@ DEFINE_STUB(rte_vfio_noiommu_is_enabled, int, (void), 0); DEFINE_STUB(rte_memseg_get_fd_thread_unsafe, int, (const struct rte_memseg *ms), 0); DEFINE_STUB(rte_memseg_get_fd_offset_thread_unsafe, int, (const struct rte_memseg *ms, size_t *offset), 0); +DEFINE_STUB(dpdk_pci_device_vtophys, uint64_t, (struct rte_pci_device *dev, uint64_t vaddr), 0); static int test_mem_map_notify(void *cb_ctx, struct spdk_mem_map *map,