Skip to content

Commit d6c58f4

Browse files
yishaihrleon
authored andcommitted
RDMA/mlx5: Implement DMABUF export ops
Enable p2pdma on the mlx5 PCI device to allow DMABUF-based peer-to-peer DMA mappings. Add implementation of the mmap_get_pfns and pgoff_to_mmap_entry device operations required for DMABUF support in the mlx5 RDMA driver. The pgoff_to_mmap_entry operation converts a page offset to the corresponding rdma_user_mmap_entry by extracting the command and index from the offset and looking it up in the ucontext's mmap_xa. The mmap_get_pfns operation retrieves the physical address and length from the mmap entry and obtains the p2pdma provider for the underlying PCI device, which is needed for peer-to-peer DMA operations with DMABUFs. Signed-off-by: Yishai Hadas <yishaih@nvidia.com> Signed-off-by: Edward Srouji <edwards@nvidia.com> Link: https://patch.msgid.link/20260201-dmabuf-export-v3-3-da238b614fe3@nvidia.com Signed-off-by: Leon Romanovsky <leon@kernel.org>
1 parent 0ac6f40 commit d6c58f4

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

  • drivers/infiniband/hw/mlx5

drivers/infiniband/hw/mlx5/main.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,6 +2454,70 @@ static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
24542454
virt_to_page(dev->mdev->clock_info));
24552455
}
24562456

2457+
static int phys_addr_to_bar(struct pci_dev *pdev, phys_addr_t pa)
2458+
{
2459+
resource_size_t start, end;
2460+
int bar;
2461+
2462+
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
2463+
/* Skip BARs not present or not memory-mapped */
2464+
if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
2465+
continue;
2466+
2467+
start = pci_resource_start(pdev, bar);
2468+
end = pci_resource_end(pdev, bar);
2469+
2470+
if (!start || !end)
2471+
continue;
2472+
2473+
if (pa >= start && pa <= end)
2474+
return bar;
2475+
}
2476+
2477+
return -1;
2478+
}
2479+
2480+
static int mlx5_ib_mmap_get_pfns(struct rdma_user_mmap_entry *entry,
2481+
struct phys_vec *phys_vec,
2482+
struct p2pdma_provider **provider)
2483+
{
2484+
struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2485+
struct pci_dev *pdev = to_mdev(entry->ucontext->device)->mdev->pdev;
2486+
int bar;
2487+
2488+
phys_vec->paddr = mentry->address;
2489+
phys_vec->len = entry->npages * PAGE_SIZE;
2490+
2491+
bar = phys_addr_to_bar(pdev, phys_vec->paddr);
2492+
if (bar < 0)
2493+
return -EINVAL;
2494+
2495+
*provider = pcim_p2pdma_provider(pdev, bar);
2496+
/* If the kernel was not compiled with CONFIG_PCI_P2PDMA the
2497+
* functionality is not supported.
2498+
*/
2499+
if (!*provider)
2500+
return -EOPNOTSUPP;
2501+
2502+
return 0;
2503+
}
2504+
2505+
static struct rdma_user_mmap_entry *
2506+
mlx5_ib_pgoff_to_mmap_entry(struct ib_ucontext *ucontext, off_t pg_off)
2507+
{
2508+
unsigned long entry_pgoff;
2509+
unsigned long idx;
2510+
u8 command;
2511+
2512+
pg_off = pg_off >> PAGE_SHIFT;
2513+
command = get_command(pg_off);
2514+
idx = get_extended_index(pg_off);
2515+
2516+
entry_pgoff = command << 16 | idx;
2517+
2518+
return rdma_user_mmap_entry_get_pgoff(ucontext, entry_pgoff);
2519+
}
2520+
24572521
static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
24582522
{
24592523
struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
@@ -4419,7 +4483,13 @@ static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
44194483
if (err)
44204484
goto err_mp;
44214485

4486+
err = pcim_p2pdma_init(mdev->pdev);
4487+
if (err && err != -EOPNOTSUPP)
4488+
goto err_dd;
4489+
44224490
return 0;
4491+
err_dd:
4492+
mlx5_ib_data_direct_cleanup(dev);
44234493
err_mp:
44244494
mlx5_ib_cleanup_multiport_master(dev);
44254495
err:
@@ -4471,11 +4541,13 @@ static const struct ib_device_ops mlx5_ib_dev_ops = {
44714541
.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
44724542
.mmap = mlx5_ib_mmap,
44734543
.mmap_free = mlx5_ib_mmap_free,
4544+
.mmap_get_pfns = mlx5_ib_mmap_get_pfns,
44744545
.modify_cq = mlx5_ib_modify_cq,
44754546
.modify_device = mlx5_ib_modify_device,
44764547
.modify_port = mlx5_ib_modify_port,
44774548
.modify_qp = mlx5_ib_modify_qp,
44784549
.modify_srq = mlx5_ib_modify_srq,
4550+
.pgoff_to_mmap_entry = mlx5_ib_pgoff_to_mmap_entry,
44794551
.pre_destroy_cq = mlx5_ib_pre_destroy_cq,
44804552
.poll_cq = mlx5_ib_poll_cq,
44814553
.post_destroy_cq = mlx5_ib_post_destroy_cq,

0 commit comments

Comments
 (0)