1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 6WIND S.A.
3 * Copyright 2018 Mellanox Technologies, Ltd
6 #ifndef RTE_PMD_MLX5_COMMON_MR_H_
7 #define RTE_PMD_MLX5_COMMON_MR_H_
11 #include <sys/queue.h>
14 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
16 #pragma GCC diagnostic ignored "-Wpedantic"
18 #include <infiniband/verbs.h>
19 #include <infiniband/mlx5dv.h>
21 #pragma GCC diagnostic error "-Wpedantic"
24 #include <rte_rwlock.h>
25 #include <rte_bitmap.h>
26 #include <rte_memory.h>
28 #include "mlx5_common_mp.h"
30 /* Size of per-queue MR cache array for linear search. */
31 #define MLX5_MR_CACHE_N 8
32 #define MLX5_MR_BTREE_CACHE_N 256
34 /* mlx5 PMD MR struct. */
39 void *obj; /* verbs mr object or devx umem object. */
41 /* Memory Region object. */
43 LIST_ENTRY(mlx5_mr) mr; /**< Pointer to the prev/next entry. */
44 struct mlx5_pmd_mr pmd_mr; /* PMD memory region. */
45 const struct rte_memseg_list *msl;
46 int ms_base_idx; /* Start index of msl->memseg_arr[]. */
47 int ms_n; /* Number of memsegs in use. */
48 uint32_t ms_bmp_n; /* Number of bits in memsegs bit-mask. */
49 struct rte_bitmap *ms_bmp; /* Bit-mask of memsegs belonged to MR. */
52 /* Cache entry for Memory Region. */
53 struct mr_cache_entry {
54 uintptr_t start; /* Start address of MR. */
55 uintptr_t end; /* End address of MR. */
56 uint32_t lkey; /* rte_cpu_to_be_32(lkey). */
59 /* MR Cache table for Binary search. */
60 struct mlx5_mr_btree {
61 uint16_t len; /* Number of entries. */
62 uint16_t size; /* Total number of entries. */
63 int overflow; /* Mark failure of table expansion. */
64 struct mr_cache_entry (*table)[];
67 /* Per-queue MR control descriptor. */
69 uint32_t *dev_gen_ptr; /* Generation number of device to poll. */
70 uint32_t cur_gen; /* Generation number saved to flush caches. */
71 uint16_t mru; /* Index of last hit entry in top-half cache. */
72 uint16_t head; /* Index of the oldest entry in top-half cache. */
73 struct mr_cache_entry cache[MLX5_MR_CACHE_N]; /* Cache for top-half. */
74 struct mlx5_mr_btree cache_bh; /* Cache for bottom-half. */
77 LIST_HEAD(mlx5_mr_list, mlx5_mr);
79 /* Global per-device MR cache. */
80 struct mlx5_mr_share_cache {
81 uint32_t dev_gen; /* Generation number to flush local caches. */
82 rte_rwlock_t rwlock; /* MR cache Lock. */
83 struct mlx5_mr_btree cache; /* Global MR cache table. */
84 struct mlx5_mr_list mr_list; /* Registered MR list. */
85 struct mlx5_mr_list mr_free_list; /* Freed MR list. */
89 * Look up LKey from given lookup table by linear search. Firstly look up the
90 * last-hit entry. If miss, the entire array is searched. If found, update the
91 * last-hit index and return LKey.
94 * Pointer to lookup table.
95 * @param[in,out] cached_idx
96 * Pointer to last-hit index.
98 * Size of lookup table.
103 * Searched LKey on success, UINT32_MAX on no match.
105 static __rte_always_inline uint32_t
106 mlx5_mr_lookup_lkey(struct mr_cache_entry *lkp_tbl, uint16_t *cached_idx,
107 uint16_t n, uintptr_t addr)
111 if (likely(addr >= lkp_tbl[*cached_idx].start &&
112 addr < lkp_tbl[*cached_idx].end))
113 return lkp_tbl[*cached_idx].lkey;
114 for (idx = 0; idx < n && lkp_tbl[idx].start != 0; ++idx) {
115 if (addr >= lkp_tbl[idx].start &&
116 addr < lkp_tbl[idx].end) {
119 return lkp_tbl[idx].lkey;
126 int mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket);
128 void mlx5_mr_btree_free(struct mlx5_mr_btree *bt);
130 void mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused);
132 uint32_t mlx5_mr_addr2mr_bh(void *pd, struct mlx5_mp_id *mp_id,
133 struct mlx5_mr_share_cache *share_cache,
134 struct mlx5_mr_ctrl *mr_ctrl,
135 uintptr_t addr, unsigned int mr_ext_memseg_en);
137 void mlx5_mr_release_cache(struct mlx5_mr_share_cache *mr_cache);
139 void mlx5_mr_dump_cache(struct mlx5_mr_share_cache *share_cache __rte_unused);
141 void mlx5_mr_rebuild_cache(struct mlx5_mr_share_cache *share_cache);
143 void mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl);
146 mlx5_mr_insert_cache(struct mlx5_mr_share_cache *share_cache,
150 mlx5_mr_lookup_cache(struct mlx5_mr_share_cache *share_cache,
151 struct mr_cache_entry *entry, uintptr_t addr);
154 mlx5_mr_lookup_list(struct mlx5_mr_share_cache *share_cache,
155 struct mr_cache_entry *entry, uintptr_t addr);
158 mlx5_create_mr_ext(void *pd, uintptr_t addr, size_t len,
162 mlx5_mr_create_primary(void *pd,
163 struct mlx5_mr_share_cache *share_cache,
164 struct mr_cache_entry *entry, uintptr_t addr,
165 unsigned int mr_ext_memseg_en);
168 mlx5_common_verbs_reg_mr(void *pd, void *addr, size_t length,
169 struct mlx5_pmd_mr *pmd_mr);
172 mlx5_common_verbs_dereg_mr(struct mlx5_pmd_mr *pmd_mr);
173 #endif /* RTE_PMD_MLX5_COMMON_MR_H_ */