At the BAR level, multiple MWs may be packed into a single BAR. In
addition, a single MW may itself be subdivided into multiple address
subranges, each of which can be mapped independently by the underlying
NTB hardware.

Introduce an optional ntb_dev_ops callback, .mw_set_trans_ranges(), to
describe and program such layouts explicitly. The helper allows an NTB
driver to provide, for each MW, a list of contiguous subranges that
together cover the MW address space.

Signed-off-by: Koichiro Den <[email protected]>
---
 include/linux/ntb.h | 46 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/include/linux/ntb.h b/include/linux/ntb.h
index 8ff9d663096b..84908753f446 100644
--- a/include/linux/ntb.h
+++ b/include/linux/ntb.h
@@ -206,6 +206,11 @@ static inline int ntb_ctx_ops_is_valid(const struct 
ntb_ctx_ops *ops)
                1;
 }
 
+struct ntb_mw_subrange {
+       dma_addr_t      addr;
+       resource_size_t size;
+};
+
 /**
  * struct ntb_dev_ops - ntb device operations
  * @port_number:       See ntb_port_number().
@@ -218,6 +223,7 @@ static inline int ntb_ctx_ops_is_valid(const struct 
ntb_ctx_ops *ops)
  * @mw_count:          See ntb_mw_count().
  * @mw_get_align:      See ntb_mw_get_align().
  * @mw_set_trans:      See ntb_mw_set_trans().
+ * @mw_set_trans_ranges:See ntb_mw_set_trans_ranges().
  * @mw_clear_trans:    See ntb_mw_clear_trans().
  * @peer_mw_count:     See ntb_peer_mw_count().
  * @peer_mw_get_addr:  See ntb_peer_mw_get_addr().
@@ -276,6 +282,9 @@ struct ntb_dev_ops {
                            resource_size_t *size_max);
        int (*mw_set_trans)(struct ntb_dev *ntb, int pidx, int widx,
                            dma_addr_t addr, resource_size_t size);
+       int (*mw_set_trans_ranges)(struct ntb_dev *ntb, int pidx, int widx,
+                                  unsigned int num_ranges,
+                                  const struct ntb_mw_subrange *ranges);
        int (*mw_clear_trans)(struct ntb_dev *ntb, int pidx, int widx);
        int (*peer_mw_count)(struct ntb_dev *ntb);
        int (*peer_mw_get_addr)(struct ntb_dev *ntb, int widx,
@@ -350,6 +359,7 @@ static inline int ntb_dev_ops_is_valid(const struct 
ntb_dev_ops *ops)
                ops->mw_get_align                       &&
                (ops->mw_set_trans                      ||
                 ops->peer_mw_set_trans)                &&
+               /* ops->mw_set_trans_ranges             && */
                /* ops->mw_clear_trans                  && */
                ops->peer_mw_count                      &&
                ops->peer_mw_get_addr                   &&
@@ -860,6 +870,42 @@ static inline int ntb_mw_set_trans(struct ntb_dev *ntb, 
int pidx, int widx,
        return ntb->ops->mw_set_trans(ntb, pidx, widx, addr, size);
 }
 
+/**
+ * ntb_mw_set_trans_ranges() - set the translations of an inbound memory
+ *                             window, composed of multiple subranges.
+ * @ntb:       NTB device context.
+ * @pidx:      Port index of peer device.
+ * @widx:      Memory window index.
+ * @num_ranges:        The number of ranges described by @ranges array.
+ * @ranges:    Array of subranges. The subranges are interpreted in ascending
+ *             window offset order (i.e. ranges[0] maps the first part of the 
MW,
+ *             ranges[1] the next part, ...).
+ *
+ * Return: Zero on success, otherwise an error number. If the driver does
+ *         not implement the callback, return -EOPNOTSUPP.
+ */
+static inline int ntb_mw_set_trans_ranges(struct ntb_dev *ntb, int pidx, int 
widx,
+                                         unsigned int num_ranges,
+                                         const struct ntb_mw_subrange *ranges)
+{
+       if (!num_ranges || !ranges)
+               return -EINVAL;
+
+       if (ntb->ops->mw_set_trans_ranges)
+               return ntb->ops->mw_set_trans_ranges(ntb, pidx, widx,
+                                                    num_ranges, ranges);
+
+       /*
+        * Fallback for drivers that only support the legacy single-range
+        * translation API.
+        */
+       if (num_ranges == 1)
+               return ntb_mw_set_trans(ntb, pidx, widx,
+                                       ranges[0].addr, ranges[0].size);
+
+       return -EOPNOTSUPP;
+}
+
 /**
  * ntb_mw_clear_trans() - clear the translation address of an inbound memory
  *                        window
-- 
2.51.0


Reply via email to