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,