]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
NTB: core: Add .get_dma_dev() callback to ntb_dev_ops
authorKoichiro Den <den@valinux.co.jp>
Fri, 6 Mar 2026 03:14:41 +0000 (12:14 +0900)
committerBjorn Helgaas <bhelgaas@google.com>
Fri, 27 Mar 2026 15:56:05 +0000 (10:56 -0500)
Some NTB implementations are backed by a PCI function that is not the right
struct device to use with DMA API helpers (e.g. due to IOMMU topology, or
because the NTB device is virtual).

Add an optional .get_dma_dev() callback to struct ntb_dev_ops and provide a
helper, ntb_get_dma_dev(), so NTB clients can use the appropriate struct
device for DMA allocations and mappings.

If the callback is not implemented, ntb_get_dma_dev() returns the current
default (ntb->dev.parent). Drivers that implement .get_dma_dev() must
return a non-NULL device.

Suggested-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Koichiro Den <den@valinux.co.jp>
Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
[bhelgaas: format doc]
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Link: https://patch.msgid.link/20260306031443.1911860-2-den@valinux.co.jp
include/linux/ntb.h

index 8ff9d663096bd3b467ace242d8b048c0b8613366..879c3e89e026ffe8d1cf65f6892552b4905379b8 100644 (file)
@@ -256,6 +256,7 @@ static inline int ntb_ctx_ops_is_valid(const struct ntb_ctx_ops *ops)
  * @msg_clear_mask:    See ntb_msg_clear_mask().
  * @msg_read:          See ntb_msg_read().
  * @peer_msg_write:    See ntb_peer_msg_write().
+ * @get_dma_dev:       See ntb_get_dma_dev().
  */
 struct ntb_dev_ops {
        int (*port_number)(struct ntb_dev *ntb);
@@ -329,6 +330,7 @@ struct ntb_dev_ops {
        int (*msg_clear_mask)(struct ntb_dev *ntb, u64 mask_bits);
        u32 (*msg_read)(struct ntb_dev *ntb, int *pidx, int midx);
        int (*peer_msg_write)(struct ntb_dev *ntb, int pidx, int midx, u32 msg);
+       struct device *(*get_dma_dev)(struct ntb_dev *ntb);
 };
 
 static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops *ops)
@@ -391,6 +393,8 @@ static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops *ops)
                /* !ops->msg_clear_mask == !ops->msg_count      && */
                !ops->msg_read == !ops->msg_count               &&
                !ops->peer_msg_write == !ops->msg_count         &&
+
+               /* ops->get_dma_dev is optional */
                1;
 }
 
@@ -1563,6 +1567,26 @@ static inline int ntb_peer_msg_write(struct ntb_dev *ntb, int pidx, int midx,
        return ntb->ops->peer_msg_write(ntb, pidx, midx, msg);
 }
 
+/**
+ * ntb_get_dma_dev() - get the device to use for DMA allocations/mappings
+ * @ntb:       NTB device context.
+ *
+ * Return a struct device suitable for DMA API allocations and mappings.
+ * This is typically the parent of the NTB device, but may be overridden by a
+ * driver by implementing .get_dma_dev().
+ *
+ * Drivers that implement .get_dma_dev() must return a non-NULL pointer.
+ *
+ * Return: device pointer to use for DMA operations.
+ */
+static inline struct device *ntb_get_dma_dev(struct ntb_dev *ntb)
+{
+       if (!ntb->ops->get_dma_dev)
+               return ntb->dev.parent;
+
+       return ntb->ops->get_dma_dev(ntb);
+}
+
 /**
  * ntb_peer_resource_idx() - get a resource index for a given peer idx
  * @ntb:       NTB device context.