xref: /dpdk/drivers/common/mlx5/mlx5_common_mr.c (revision 334ed198ab4d22f249d7b8274568253df33914a6)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2020 Mellanox Technologies, Ltd
4  */
5 #include <stddef.h>
6 
7 #include <rte_eal_memconfig.h>
8 #include <rte_eal_paging.h>
9 #include <rte_errno.h>
10 #include <rte_mempool.h>
11 #include <rte_malloc.h>
12 #include <rte_rwlock.h>
13 
14 #include "mlx5_glue.h"
15 #include "mlx5_common.h"
16 #include "mlx5_common_mp.h"
17 #include "mlx5_common_mr.h"
18 #include "mlx5_common_os.h"
19 #include "mlx5_common_log.h"
20 #include "mlx5_malloc.h"
21 
22 struct mr_find_contig_memsegs_data {
23 	uintptr_t addr;
24 	uintptr_t start;
25 	uintptr_t end;
26 	const struct rte_memseg_list *msl;
27 };
28 
29 /* Virtual memory range. */
30 struct mlx5_range {
31 	uintptr_t start;
32 	uintptr_t end;
33 };
34 
35 /** Memory region for a mempool. */
36 struct mlx5_mempool_mr {
37 	struct mlx5_pmd_mr pmd_mr;
38 	uint32_t refcnt; /**< Number of mempools sharing this MR. */
39 };
40 
41 /* Mempool registration. */
42 struct mlx5_mempool_reg {
43 	LIST_ENTRY(mlx5_mempool_reg) next;
44 	/** Registered mempool, used to designate registrations. */
45 	struct rte_mempool *mp;
46 	/** Memory regions for the address ranges of the mempool. */
47 	struct mlx5_mempool_mr *mrs;
48 	/** Number of memory regions. */
49 	unsigned int mrs_n;
50 };
51 
52 void
53 mlx5_mprq_buf_free_cb(void *addr __rte_unused, void *opaque)
54 {
55 	struct mlx5_mprq_buf *buf = opaque;
56 
57 	if (__atomic_load_n(&buf->refcnt, __ATOMIC_RELAXED) == 1) {
58 		rte_mempool_put(buf->mp, buf);
59 	} else if (unlikely(__atomic_sub_fetch(&buf->refcnt, 1,
60 					       __ATOMIC_RELAXED) == 0)) {
61 		__atomic_store_n(&buf->refcnt, 1, __ATOMIC_RELAXED);
62 		rte_mempool_put(buf->mp, buf);
63 	}
64 }
65 
66 /**
67  * Expand B-tree table to a given size. Can't be called with holding
68  * memory_hotplug_lock or share_cache.rwlock due to rte_realloc().
69  *
70  * @param bt
71  *   Pointer to B-tree structure.
72  * @param n
73  *   Number of entries for expansion.
74  *
75  * @return
76  *   0 on success, -1 on failure.
77  */
78 static int
79 mr_btree_expand(struct mlx5_mr_btree *bt, int n)
80 {
81 	void *mem;
82 	int ret = 0;
83 
84 	if (n <= bt->size)
85 		return ret;
86 	/*
87 	 * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is
88 	 * used inside if there's no room to expand. Because this is a quite
89 	 * rare case and a part of very slow path, it is very acceptable.
90 	 * Initially cache_bh[] will be given practically enough space and once
91 	 * it is expanded, expansion wouldn't be needed again ever.
92 	 */
93 	mem = mlx5_realloc(bt->table, MLX5_MEM_RTE | MLX5_MEM_ZERO,
94 			   n * sizeof(struct mr_cache_entry), 0, SOCKET_ID_ANY);
95 	if (mem == NULL) {
96 		/* Not an error, B-tree search will be skipped. */
97 		DRV_LOG(WARNING, "failed to expand MR B-tree (%p) table",
98 			(void *)bt);
99 		ret = -1;
100 	} else {
101 		DRV_LOG(DEBUG, "expanded MR B-tree table (size=%u)", n);
102 		bt->table = mem;
103 		bt->size = n;
104 	}
105 	return ret;
106 }
107 
108 /**
109  * Look up LKey from given B-tree lookup table, store the last index and return
110  * searched LKey.
111  *
112  * @param bt
113  *   Pointer to B-tree structure.
114  * @param[out] idx
115  *   Pointer to index. Even on search failure, returns index where it stops
116  *   searching so that index can be used when inserting a new entry.
117  * @param addr
118  *   Search key.
119  *
120  * @return
121  *   Searched LKey on success, UINT32_MAX on no match.
122  */
123 static uint32_t
124 mr_btree_lookup(struct mlx5_mr_btree *bt, uint16_t *idx, uintptr_t addr)
125 {
126 	struct mr_cache_entry *lkp_tbl;
127 	uint16_t n;
128 	uint16_t base = 0;
129 
130 	MLX5_ASSERT(bt != NULL);
131 	lkp_tbl = *bt->table;
132 	n = bt->len;
133 	/* First entry must be NULL for comparison. */
134 	MLX5_ASSERT(bt->len > 0 || (lkp_tbl[0].start == 0 &&
135 				    lkp_tbl[0].lkey == UINT32_MAX));
136 	/* Binary search. */
137 	do {
138 		register uint16_t delta = n >> 1;
139 
140 		if (addr < lkp_tbl[base + delta].start) {
141 			n = delta;
142 		} else {
143 			base += delta;
144 			n -= delta;
145 		}
146 	} while (n > 1);
147 	MLX5_ASSERT(addr >= lkp_tbl[base].start);
148 	*idx = base;
149 	if (addr < lkp_tbl[base].end)
150 		return lkp_tbl[base].lkey;
151 	/* Not found. */
152 	return UINT32_MAX;
153 }
154 
155 /**
156  * Insert an entry to B-tree lookup table.
157  *
158  * @param bt
159  *   Pointer to B-tree structure.
160  * @param entry
161  *   Pointer to new entry to insert.
162  *
163  * @return
164  *   0 on success, -1 on failure.
165  */
166 static int
167 mr_btree_insert(struct mlx5_mr_btree *bt, struct mr_cache_entry *entry)
168 {
169 	struct mr_cache_entry *lkp_tbl;
170 	uint16_t idx = 0;
171 	size_t shift;
172 
173 	MLX5_ASSERT(bt != NULL);
174 	MLX5_ASSERT(bt->len <= bt->size);
175 	MLX5_ASSERT(bt->len > 0);
176 	lkp_tbl = *bt->table;
177 	/* Find out the slot for insertion. */
178 	if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) {
179 		DRV_LOG(DEBUG,
180 			"abort insertion to B-tree(%p): already exist at"
181 			" idx=%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
182 			(void *)bt, idx, entry->start, entry->end, entry->lkey);
183 		/* Already exist, return. */
184 		return 0;
185 	}
186 	/* If table is full, return error. */
187 	if (unlikely(bt->len == bt->size)) {
188 		bt->overflow = 1;
189 		return -1;
190 	}
191 	/* Insert entry. */
192 	++idx;
193 	shift = (bt->len - idx) * sizeof(struct mr_cache_entry);
194 	if (shift)
195 		memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift);
196 	lkp_tbl[idx] = *entry;
197 	bt->len++;
198 	DRV_LOG(DEBUG,
199 		"inserted B-tree(%p)[%u],"
200 		" [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
201 		(void *)bt, idx, entry->start, entry->end, entry->lkey);
202 	return 0;
203 }
204 
205 /**
206  * Initialize B-tree and allocate memory for lookup table.
207  *
208  * @param bt
209  *   Pointer to B-tree structure.
210  * @param n
211  *   Number of entries to allocate.
212  * @param socket
213  *   NUMA socket on which memory must be allocated.
214  *
215  * @return
216  *   0 on success, a negative errno value otherwise and rte_errno is set.
217  */
218 static int
219 mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket)
220 {
221 	if (bt == NULL) {
222 		rte_errno = EINVAL;
223 		return -rte_errno;
224 	}
225 	MLX5_ASSERT(!bt->table && !bt->size);
226 	memset(bt, 0, sizeof(*bt));
227 	bt->table = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
228 				sizeof(struct mr_cache_entry) * n,
229 				0, socket);
230 	if (bt->table == NULL) {
231 		rte_errno = ENOMEM;
232 		DRV_LOG(DEBUG,
233 			"failed to allocate memory for btree cache on socket "
234 			"%d", socket);
235 		return -rte_errno;
236 	}
237 	bt->size = n;
238 	/* First entry must be NULL for binary search. */
239 	(*bt->table)[bt->len++] = (struct mr_cache_entry) {
240 		.lkey = UINT32_MAX,
241 	};
242 	DRV_LOG(DEBUG, "initialized B-tree %p with table %p",
243 	      (void *)bt, (void *)bt->table);
244 	return 0;
245 }
246 
247 /**
248  * Free B-tree resources.
249  *
250  * @param bt
251  *   Pointer to B-tree structure.
252  */
253 void
254 mlx5_mr_btree_free(struct mlx5_mr_btree *bt)
255 {
256 	if (bt == NULL)
257 		return;
258 	DRV_LOG(DEBUG, "freeing B-tree %p with table %p",
259 	      (void *)bt, (void *)bt->table);
260 	mlx5_free(bt->table);
261 	memset(bt, 0, sizeof(*bt));
262 }
263 
264 /**
265  * Dump all the entries in a B-tree
266  *
267  * @param bt
268  *   Pointer to B-tree structure.
269  */
270 void
271 mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused)
272 {
273 #ifdef RTE_LIBRTE_MLX5_DEBUG
274 	int idx;
275 	struct mr_cache_entry *lkp_tbl;
276 
277 	if (bt == NULL)
278 		return;
279 	lkp_tbl = *bt->table;
280 	for (idx = 0; idx < bt->len; ++idx) {
281 		struct mr_cache_entry *entry = &lkp_tbl[idx];
282 
283 		DRV_LOG(DEBUG, "B-tree(%p)[%u],"
284 		      " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
285 		      (void *)bt, idx, entry->start, entry->end, entry->lkey);
286 	}
287 #endif
288 }
289 
290 /**
291  * Initialize per-queue MR control descriptor.
292  *
293  * @param mr_ctrl
294  *   Pointer to MR control structure.
295  * @param cdev
296  *   Pointer to the mlx5 device structure.
297  * @param socket
298  *   NUMA socket on which memory must be allocated.
299  *
300  * @return
301  *   0 on success, a negative errno value otherwise and rte_errno is set.
302  */
303 int
304 mlx5_mr_ctrl_init(struct mlx5_mr_ctrl *mr_ctrl, struct mlx5_common_device *cdev,
305 		  int socket)
306 {
307 	if (mr_ctrl == NULL) {
308 		rte_errno = EINVAL;
309 		return -rte_errno;
310 	}
311 	mr_ctrl->cdev = cdev;
312 	/* Save pointer of global generation number to check memory event. */
313 	mr_ctrl->dev_gen_ptr = &cdev->mr_scache.dev_gen;
314 	/* Initialize B-tree and allocate memory for bottom-half cache table. */
315 	return mlx5_mr_btree_init(&mr_ctrl->cache_bh, MLX5_MR_BTREE_CACHE_N,
316 				  socket);
317 }
318 
319 /**
320  * Find virtually contiguous memory chunk in a given MR.
321  *
322  * @param dev
323  *   Pointer to MR structure.
324  * @param[out] entry
325  *   Pointer to returning MR cache entry. If not found, this will not be
326  *   updated.
327  * @param start_idx
328  *   Start index of the memseg bitmap.
329  *
330  * @return
331  *   Next index to go on lookup.
332  */
333 static int
334 mr_find_next_chunk(struct mlx5_mr *mr, struct mr_cache_entry *entry,
335 		   int base_idx)
336 {
337 	uintptr_t start = 0;
338 	uintptr_t end = 0;
339 	uint32_t idx = 0;
340 
341 	/* MR for external memory doesn't have memseg list. */
342 	if (mr->msl == NULL) {
343 		MLX5_ASSERT(mr->ms_bmp_n == 1);
344 		MLX5_ASSERT(mr->ms_n == 1);
345 		MLX5_ASSERT(base_idx == 0);
346 		/*
347 		 * Can't search it from memseg list but get it directly from
348 		 * pmd_mr as there's only one chunk.
349 		 */
350 		entry->start = (uintptr_t)mr->pmd_mr.addr;
351 		entry->end = (uintptr_t)mr->pmd_mr.addr + mr->pmd_mr.len;
352 		entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
353 		/* Returning 1 ends iteration. */
354 		return 1;
355 	}
356 	for (idx = base_idx; idx < mr->ms_bmp_n; ++idx) {
357 		if (rte_bitmap_get(mr->ms_bmp, idx)) {
358 			const struct rte_memseg_list *msl;
359 			const struct rte_memseg *ms;
360 
361 			msl = mr->msl;
362 			ms = rte_fbarray_get(&msl->memseg_arr,
363 					     mr->ms_base_idx + idx);
364 			MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
365 			if (!start)
366 				start = ms->addr_64;
367 			end = ms->addr_64 + ms->hugepage_sz;
368 		} else if (start) {
369 			/* Passed the end of a fragment. */
370 			break;
371 		}
372 	}
373 	if (start) {
374 		/* Found one chunk. */
375 		entry->start = start;
376 		entry->end = end;
377 		entry->lkey = rte_cpu_to_be_32(mr->pmd_mr.lkey);
378 	}
379 	return idx;
380 }
381 
382 /**
383  * Insert a MR to the global B-tree cache. It may fail due to low-on-memory.
384  * Then, this entry will have to be searched by mr_lookup_list() in
385  * mlx5_mr_create() on miss.
386  *
387  * @param share_cache
388  *   Pointer to a global shared MR cache.
389  * @param mr
390  *   Pointer to MR to insert.
391  *
392  * @return
393  *   0 on success, -1 on failure.
394  */
395 int
396 mlx5_mr_insert_cache(struct mlx5_mr_share_cache *share_cache,
397 		     struct mlx5_mr *mr)
398 {
399 	unsigned int n;
400 
401 	DRV_LOG(DEBUG, "Inserting MR(%p) to global cache(%p)",
402 		(void *)mr, (void *)share_cache);
403 	for (n = 0; n < mr->ms_bmp_n; ) {
404 		struct mr_cache_entry entry;
405 
406 		memset(&entry, 0, sizeof(entry));
407 		/* Find a contiguous chunk and advance the index. */
408 		n = mr_find_next_chunk(mr, &entry, n);
409 		if (!entry.end)
410 			break;
411 		if (mr_btree_insert(&share_cache->cache, &entry) < 0) {
412 			/*
413 			 * Overflowed, but the global table cannot be expanded
414 			 * because of deadlock.
415 			 */
416 			return -1;
417 		}
418 	}
419 	return 0;
420 }
421 
422 /**
423  * Look up address in the original global MR list.
424  *
425  * @param share_cache
426  *   Pointer to a global shared MR cache.
427  * @param[out] entry
428  *   Pointer to returning MR cache entry. If no match, this will not be updated.
429  * @param addr
430  *   Search key.
431  *
432  * @return
433  *   Found MR on match, NULL otherwise.
434  */
435 struct mlx5_mr *
436 mlx5_mr_lookup_list(struct mlx5_mr_share_cache *share_cache,
437 		    struct mr_cache_entry *entry, uintptr_t addr)
438 {
439 	struct mlx5_mr *mr;
440 
441 	/* Iterate all the existing MRs. */
442 	LIST_FOREACH(mr, &share_cache->mr_list, mr) {
443 		unsigned int n;
444 
445 		if (mr->ms_n == 0)
446 			continue;
447 		for (n = 0; n < mr->ms_bmp_n; ) {
448 			struct mr_cache_entry ret;
449 
450 			memset(&ret, 0, sizeof(ret));
451 			n = mr_find_next_chunk(mr, &ret, n);
452 			if (addr >= ret.start && addr < ret.end) {
453 				/* Found. */
454 				*entry = ret;
455 				return mr;
456 			}
457 		}
458 	}
459 	return NULL;
460 }
461 
462 /**
463  * Look up address on global MR cache.
464  *
465  * @param share_cache
466  *   Pointer to a global shared MR cache.
467  * @param[out] entry
468  *   Pointer to returning MR cache entry. If no match, this will not be updated.
469  * @param addr
470  *   Search key.
471  *
472  * @return
473  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
474  */
475 static uint32_t
476 mlx5_mr_lookup_cache(struct mlx5_mr_share_cache *share_cache,
477 		     struct mr_cache_entry *entry, uintptr_t addr)
478 {
479 	uint16_t idx;
480 	uint32_t lkey = UINT32_MAX;
481 	struct mlx5_mr *mr;
482 
483 	/*
484 	 * If the global cache has overflowed since it failed to expand the
485 	 * B-tree table, it can't have all the existing MRs. Then, the address
486 	 * has to be searched by traversing the original MR list instead, which
487 	 * is very slow path. Otherwise, the global cache is all inclusive.
488 	 */
489 	if (!unlikely(share_cache->cache.overflow)) {
490 		lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
491 		if (lkey != UINT32_MAX)
492 			*entry = (*share_cache->cache.table)[idx];
493 	} else {
494 		/* Falling back to the slowest path. */
495 		mr = mlx5_mr_lookup_list(share_cache, entry, addr);
496 		if (mr != NULL)
497 			lkey = entry->lkey;
498 	}
499 	MLX5_ASSERT(lkey == UINT32_MAX || (addr >= entry->start &&
500 					   addr < entry->end));
501 	return lkey;
502 }
503 
504 /**
505  * Free MR resources. MR lock must not be held to avoid a deadlock. rte_free()
506  * can raise memory free event and the callback function will spin on the lock.
507  *
508  * @param mr
509  *   Pointer to MR to free.
510  */
511 void
512 mlx5_mr_free(struct mlx5_mr *mr, mlx5_dereg_mr_t dereg_mr_cb)
513 {
514 	if (mr == NULL)
515 		return;
516 	DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr);
517 	dereg_mr_cb(&mr->pmd_mr);
518 	if (mr->ms_bmp != NULL)
519 		rte_bitmap_free(mr->ms_bmp);
520 	mlx5_free(mr);
521 }
522 
523 void
524 mlx5_mr_rebuild_cache(struct mlx5_mr_share_cache *share_cache)
525 {
526 	struct mlx5_mr *mr;
527 
528 	DRV_LOG(DEBUG, "Rebuild dev cache[] %p", (void *)share_cache);
529 	/* Flush cache to rebuild. */
530 	share_cache->cache.len = 1;
531 	share_cache->cache.overflow = 0;
532 	/* Iterate all the existing MRs. */
533 	LIST_FOREACH(mr, &share_cache->mr_list, mr)
534 		if (mlx5_mr_insert_cache(share_cache, mr) < 0)
535 			return;
536 }
537 
538 /**
539  * Release resources of detached MR having no online entry.
540  *
541  * @param share_cache
542  *   Pointer to a global shared MR cache.
543  */
544 static void
545 mlx5_mr_garbage_collect(struct mlx5_mr_share_cache *share_cache)
546 {
547 	struct mlx5_mr *mr_next;
548 	struct mlx5_mr_list free_list = LIST_HEAD_INITIALIZER(free_list);
549 
550 	/* Must be called from the primary process. */
551 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
552 	/*
553 	 * MR can't be freed with holding the lock because rte_free() could call
554 	 * memory free callback function. This will be a deadlock situation.
555 	 */
556 	rte_rwlock_write_lock(&share_cache->rwlock);
557 	/* Detach the whole free list and release it after unlocking. */
558 	free_list = share_cache->mr_free_list;
559 	LIST_INIT(&share_cache->mr_free_list);
560 	rte_rwlock_write_unlock(&share_cache->rwlock);
561 	/* Release resources. */
562 	mr_next = LIST_FIRST(&free_list);
563 	while (mr_next != NULL) {
564 		struct mlx5_mr *mr = mr_next;
565 
566 		mr_next = LIST_NEXT(mr, mr);
567 		mlx5_mr_free(mr, share_cache->dereg_mr_cb);
568 	}
569 }
570 
571 /* Called during rte_memseg_contig_walk() by mlx5_mr_create(). */
572 static int
573 mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl,
574 			  const struct rte_memseg *ms, size_t len, void *arg)
575 {
576 	struct mr_find_contig_memsegs_data *data = arg;
577 
578 	if (data->addr < ms->addr_64 || data->addr >= ms->addr_64 + len)
579 		return 0;
580 	/* Found, save it and stop walking. */
581 	data->start = ms->addr_64;
582 	data->end = ms->addr_64 + len;
583 	data->msl = msl;
584 	return 1;
585 }
586 
587 /**
588  * Create a new global Memory Region (MR) for a missing virtual address.
589  * This API should be called on a secondary process, then a request is sent to
590  * the primary process in order to create a MR for the address. As the global MR
591  * list is on the shared memory, following LKey lookup should succeed unless the
592  * request fails.
593  *
594  * @param pd
595  *   Pointer to pd of a device (net, regex, vdpa,...).
596  * @param mp_id
597  *   Multi-process identifier, may be NULL for the primary process.
598  * @param share_cache
599  *   Pointer to a global shared MR cache.
600  * @param[out] entry
601  *   Pointer to returning MR cache entry, found in the global cache or newly
602  *   created. If failed to create one, this will not be updated.
603  * @param addr
604  *   Target virtual address to register.
605  * @param mr_ext_memseg_en
606  *   Configurable flag about external memory segment enable or not.
607  *
608  * @return
609  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
610  */
611 static uint32_t
612 mlx5_mr_create_secondary(void *pd __rte_unused,
613 			 struct mlx5_mp_id *mp_id,
614 			 struct mlx5_mr_share_cache *share_cache,
615 			 struct mr_cache_entry *entry, uintptr_t addr,
616 			 unsigned int mr_ext_memseg_en __rte_unused)
617 {
618 	int ret;
619 
620 	if (mp_id == NULL) {
621 		rte_errno = EINVAL;
622 		return UINT32_MAX;
623 	}
624 	DRV_LOG(DEBUG, "port %u requesting MR creation for address (%p)",
625 	      mp_id->port_id, (void *)addr);
626 	ret = mlx5_mp_req_mr_create(mp_id, addr);
627 	if (ret) {
628 		DRV_LOG(DEBUG, "Fail to request MR creation for address (%p)",
629 		      (void *)addr);
630 		return UINT32_MAX;
631 	}
632 	rte_rwlock_read_lock(&share_cache->rwlock);
633 	/* Fill in output data. */
634 	mlx5_mr_lookup_cache(share_cache, entry, addr);
635 	/* Lookup can't fail. */
636 	MLX5_ASSERT(entry->lkey != UINT32_MAX);
637 	rte_rwlock_read_unlock(&share_cache->rwlock);
638 	DRV_LOG(DEBUG, "MR CREATED by primary process for %p:\n"
639 	      "  [0x%" PRIxPTR ", 0x%" PRIxPTR "), lkey=0x%x",
640 	      (void *)addr, entry->start, entry->end, entry->lkey);
641 	return entry->lkey;
642 }
643 
644 /**
645  * Create a new global Memory Region (MR) for a missing virtual address.
646  * Register entire virtually contiguous memory chunk around the address.
647  *
648  * @param pd
649  *   Pointer to pd of a device (net, regex, vdpa,...).
650  * @param share_cache
651  *   Pointer to a global shared MR cache.
652  * @param[out] entry
653  *   Pointer to returning MR cache entry, found in the global cache or newly
654  *   created. If failed to create one, this will not be updated.
655  * @param addr
656  *   Target virtual address to register.
657  * @param mr_ext_memseg_en
658  *   Configurable flag about external memory segment enable or not.
659  *
660  * @return
661  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
662  */
663 uint32_t
664 mlx5_mr_create_primary(void *pd,
665 		       struct mlx5_mr_share_cache *share_cache,
666 		       struct mr_cache_entry *entry, uintptr_t addr,
667 		       unsigned int mr_ext_memseg_en)
668 {
669 	struct mr_find_contig_memsegs_data data = {.addr = addr, };
670 	struct mr_find_contig_memsegs_data data_re;
671 	const struct rte_memseg_list *msl;
672 	const struct rte_memseg *ms;
673 	struct mlx5_mr *mr = NULL;
674 	int ms_idx_shift = -1;
675 	uint32_t bmp_size;
676 	void *bmp_mem;
677 	uint32_t ms_n;
678 	uint32_t n;
679 	size_t len;
680 
681 	DRV_LOG(DEBUG, "Creating a MR using address (%p)", (void *)addr);
682 	/*
683 	 * Release detached MRs if any. This can't be called with holding either
684 	 * memory_hotplug_lock or share_cache->rwlock. MRs on the free list have
685 	 * been detached by the memory free event but it couldn't be released
686 	 * inside the callback due to deadlock. As a result, releasing resources
687 	 * is quite opportunistic.
688 	 */
689 	mlx5_mr_garbage_collect(share_cache);
690 	/*
691 	 * If enabled, find out a contiguous virtual address chunk in use, to
692 	 * which the given address belongs, in order to register maximum range.
693 	 * In the best case where mempools are not dynamically recreated and
694 	 * '--socket-mem' is specified as an EAL option, it is very likely to
695 	 * have only one MR(LKey) per a socket and per a hugepage-size even
696 	 * though the system memory is highly fragmented. As the whole memory
697 	 * chunk will be pinned by kernel, it can't be reused unless entire
698 	 * chunk is freed from EAL.
699 	 *
700 	 * If disabled, just register one memseg (page). Then, memory
701 	 * consumption will be minimized but it may drop performance if there
702 	 * are many MRs to lookup on the datapath.
703 	 */
704 	if (!mr_ext_memseg_en) {
705 		data.msl = rte_mem_virt2memseg_list((void *)addr);
706 		data.start = RTE_ALIGN_FLOOR(addr, data.msl->page_sz);
707 		data.end = data.start + data.msl->page_sz;
708 	} else if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data)) {
709 		DRV_LOG(WARNING,
710 			"Unable to find virtually contiguous"
711 			" chunk for address (%p)."
712 			" rte_memseg_contig_walk() failed.", (void *)addr);
713 		rte_errno = ENXIO;
714 		goto err_nolock;
715 	}
716 alloc_resources:
717 	/* Addresses must be page-aligned. */
718 	MLX5_ASSERT(data.msl);
719 	MLX5_ASSERT(rte_is_aligned((void *)data.start, data.msl->page_sz));
720 	MLX5_ASSERT(rte_is_aligned((void *)data.end, data.msl->page_sz));
721 	msl = data.msl;
722 	ms = rte_mem_virt2memseg((void *)data.start, msl);
723 	len = data.end - data.start;
724 	MLX5_ASSERT(ms);
725 	MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
726 	/* Number of memsegs in the range. */
727 	ms_n = len / msl->page_sz;
728 	DRV_LOG(DEBUG, "Extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
729 	      " page_sz=0x%" PRIx64 ", ms_n=%u",
730 	      (void *)addr, data.start, data.end, msl->page_sz, ms_n);
731 	/* Size of memory for bitmap. */
732 	bmp_size = rte_bitmap_get_memory_footprint(ms_n);
733 	mr = mlx5_malloc(MLX5_MEM_RTE |  MLX5_MEM_ZERO,
734 			 RTE_ALIGN_CEIL(sizeof(*mr), RTE_CACHE_LINE_SIZE) +
735 			 bmp_size, RTE_CACHE_LINE_SIZE, msl->socket_id);
736 	if (mr == NULL) {
737 		DRV_LOG(DEBUG, "Unable to allocate memory for a new MR of"
738 		      " address (%p).", (void *)addr);
739 		rte_errno = ENOMEM;
740 		goto err_nolock;
741 	}
742 	mr->msl = msl;
743 	/*
744 	 * Save the index of the first memseg and initialize memseg bitmap. To
745 	 * see if a memseg of ms_idx in the memseg-list is still valid, check:
746 	 *	rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx)
747 	 */
748 	mr->ms_base_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
749 	bmp_mem = RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE);
750 	mr->ms_bmp = rte_bitmap_init(ms_n, bmp_mem, bmp_size);
751 	if (mr->ms_bmp == NULL) {
752 		DRV_LOG(DEBUG, "Unable to initialize bitmap for a new MR of"
753 		      " address (%p).", (void *)addr);
754 		rte_errno = EINVAL;
755 		goto err_nolock;
756 	}
757 	/*
758 	 * Should recheck whether the extended contiguous chunk is still valid.
759 	 * Because memory_hotplug_lock can't be held if there's any memory
760 	 * related calls in a critical path, resource allocation above can't be
761 	 * locked. If the memory has been changed at this point, try again with
762 	 * just single page. If not, go on with the big chunk atomically from
763 	 * here.
764 	 */
765 	rte_mcfg_mem_read_lock();
766 	data_re = data;
767 	if (len > msl->page_sz &&
768 	    !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) {
769 		DRV_LOG(DEBUG,
770 			"Unable to find virtually contiguous chunk for address "
771 			"(%p). rte_memseg_contig_walk() failed.", (void *)addr);
772 		rte_errno = ENXIO;
773 		goto err_memlock;
774 	}
775 	if (data.start != data_re.start || data.end != data_re.end) {
776 		/*
777 		 * The extended contiguous chunk has been changed. Try again
778 		 * with single memseg instead.
779 		 */
780 		data.start = RTE_ALIGN_FLOOR(addr, msl->page_sz);
781 		data.end = data.start + msl->page_sz;
782 		rte_mcfg_mem_read_unlock();
783 		mlx5_mr_free(mr, share_cache->dereg_mr_cb);
784 		goto alloc_resources;
785 	}
786 	MLX5_ASSERT(data.msl == data_re.msl);
787 	rte_rwlock_write_lock(&share_cache->rwlock);
788 	/*
789 	 * Check the address is really missing. If other thread already created
790 	 * one or it is not found due to overflow, abort and return.
791 	 */
792 	if (mlx5_mr_lookup_cache(share_cache, entry, addr) != UINT32_MAX) {
793 		/*
794 		 * Insert to the global cache table. It may fail due to
795 		 * low-on-memory. Then, this entry will have to be searched
796 		 * here again.
797 		 */
798 		mr_btree_insert(&share_cache->cache, entry);
799 		DRV_LOG(DEBUG, "Found MR for %p on final lookup, abort",
800 			(void *)addr);
801 		rte_rwlock_write_unlock(&share_cache->rwlock);
802 		rte_mcfg_mem_read_unlock();
803 		/*
804 		 * Must be unlocked before calling rte_free() because
805 		 * mlx5_mr_mem_event_free_cb() can be called inside.
806 		 */
807 		mlx5_mr_free(mr, share_cache->dereg_mr_cb);
808 		return entry->lkey;
809 	}
810 	/*
811 	 * Trim start and end addresses for verbs MR. Set bits for registering
812 	 * memsegs but exclude already registered ones. Bitmap can be
813 	 * fragmented.
814 	 */
815 	for (n = 0; n < ms_n; ++n) {
816 		uintptr_t start;
817 		struct mr_cache_entry ret;
818 
819 		memset(&ret, 0, sizeof(ret));
820 		start = data_re.start + n * msl->page_sz;
821 		/* Exclude memsegs already registered by other MRs. */
822 		if (mlx5_mr_lookup_cache(share_cache, &ret, start) ==
823 		    UINT32_MAX) {
824 			/*
825 			 * Start from the first unregistered memseg in the
826 			 * extended range.
827 			 */
828 			if (ms_idx_shift == -1) {
829 				mr->ms_base_idx += n;
830 				data.start = start;
831 				ms_idx_shift = n;
832 			}
833 			data.end = start + msl->page_sz;
834 			rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift);
835 			++mr->ms_n;
836 		}
837 	}
838 	len = data.end - data.start;
839 	mr->ms_bmp_n = len / msl->page_sz;
840 	MLX5_ASSERT(ms_idx_shift + mr->ms_bmp_n <= ms_n);
841 	/*
842 	 * Finally create an MR for the memory chunk. Verbs: ibv_reg_mr() can
843 	 * be called with holding the memory lock because it doesn't use
844 	 * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket()
845 	 * through mlx5_alloc_verbs_buf().
846 	 */
847 	share_cache->reg_mr_cb(pd, (void *)data.start, len, &mr->pmd_mr);
848 	if (mr->pmd_mr.obj == NULL) {
849 		DRV_LOG(DEBUG, "Fail to create an MR for address (%p)",
850 		      (void *)addr);
851 		rte_errno = EINVAL;
852 		goto err_mrlock;
853 	}
854 	MLX5_ASSERT((uintptr_t)mr->pmd_mr.addr == data.start);
855 	MLX5_ASSERT(mr->pmd_mr.len);
856 	LIST_INSERT_HEAD(&share_cache->mr_list, mr, mr);
857 	DRV_LOG(DEBUG, "MR CREATED (%p) for %p:\n"
858 	      "  [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
859 	      " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
860 	      (void *)mr, (void *)addr, data.start, data.end,
861 	      rte_cpu_to_be_32(mr->pmd_mr.lkey),
862 	      mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
863 	/* Insert to the global cache table. */
864 	mlx5_mr_insert_cache(share_cache, mr);
865 	/* Fill in output data. */
866 	mlx5_mr_lookup_cache(share_cache, entry, addr);
867 	/* Lookup can't fail. */
868 	MLX5_ASSERT(entry->lkey != UINT32_MAX);
869 	rte_rwlock_write_unlock(&share_cache->rwlock);
870 	rte_mcfg_mem_read_unlock();
871 	return entry->lkey;
872 err_mrlock:
873 	rte_rwlock_write_unlock(&share_cache->rwlock);
874 err_memlock:
875 	rte_mcfg_mem_read_unlock();
876 err_nolock:
877 	/*
878 	 * In case of error, as this can be called in a datapath, a warning
879 	 * message per an error is preferable instead. Must be unlocked before
880 	 * calling rte_free() because mlx5_mr_mem_event_free_cb() can be called
881 	 * inside.
882 	 */
883 	mlx5_mr_free(mr, share_cache->dereg_mr_cb);
884 	return UINT32_MAX;
885 }
886 
887 /**
888  * Create a new global Memory Region (MR) for a missing virtual address.
889  * This can be called from primary and secondary process.
890  *
891  * @param pd
892  *   Pointer to pd handle of a device (net, regex, vdpa,...).
893  * @param mp_id
894  *   Multi-process identifier, may be NULL for the primary process.
895  * @param share_cache
896  *   Pointer to a global shared MR cache.
897  * @param[out] entry
898  *   Pointer to returning MR cache entry, found in the global cache or newly
899  *   created. If failed to create one, this will not be updated.
900  * @param addr
901  *   Target virtual address to register.
902  * @param mr_ext_memseg_en
903  *   Configurable flag about external memory segment enable or not.
904  *
905  * @return
906  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
907  */
908 static uint32_t
909 mlx5_mr_create(void *pd, struct mlx5_mp_id *mp_id,
910 	       struct mlx5_mr_share_cache *share_cache,
911 	       struct mr_cache_entry *entry, uintptr_t addr,
912 	       unsigned int mr_ext_memseg_en)
913 {
914 	uint32_t ret = 0;
915 
916 	switch (rte_eal_process_type()) {
917 	case RTE_PROC_PRIMARY:
918 		ret = mlx5_mr_create_primary(pd, share_cache, entry,
919 					     addr, mr_ext_memseg_en);
920 		break;
921 	case RTE_PROC_SECONDARY:
922 		ret = mlx5_mr_create_secondary(pd, mp_id, share_cache, entry,
923 					       addr, mr_ext_memseg_en);
924 		break;
925 	default:
926 		break;
927 	}
928 	return ret;
929 }
930 
931 /**
932  * Look up address in the global MR cache table. If not found, create a new MR.
933  * Insert the found/created entry to local bottom-half cache table.
934  *
935  * @param pd
936  *   Pointer to pd of a device (net, regex, vdpa,...).
937  * @param mp_id
938  *   Multi-process identifier, may be NULL for the primary process.
939  * @param share_cache
940  *   Pointer to a global shared MR cache.
941  * @param mr_ctrl
942  *   Pointer to per-queue MR control structure.
943  * @param[out] entry
944  *   Pointer to returning MR cache entry, found in the global cache or newly
945  *   created. If failed to create one, this is not written.
946  * @param addr
947  *   Search key.
948  * @param mr_ext_memseg_en
949  *   Configurable flag about external memory segment enable or not.
950  *
951  * @return
952  *   Searched LKey on success, UINT32_MAX on no match.
953  */
954 static uint32_t
955 mr_lookup_caches(void *pd, struct mlx5_mp_id *mp_id,
956 		 struct mlx5_mr_share_cache *share_cache,
957 		 struct mlx5_mr_ctrl *mr_ctrl,
958 		 struct mr_cache_entry *entry, uintptr_t addr,
959 		 unsigned int mr_ext_memseg_en)
960 {
961 	struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh;
962 	uint32_t lkey;
963 	uint16_t idx;
964 
965 	/* If local cache table is full, try to double it. */
966 	if (unlikely(bt->len == bt->size))
967 		mr_btree_expand(bt, bt->size << 1);
968 	/* Look up in the global cache. */
969 	rte_rwlock_read_lock(&share_cache->rwlock);
970 	lkey = mr_btree_lookup(&share_cache->cache, &idx, addr);
971 	if (lkey != UINT32_MAX) {
972 		/* Found. */
973 		*entry = (*share_cache->cache.table)[idx];
974 		rte_rwlock_read_unlock(&share_cache->rwlock);
975 		/*
976 		 * Update local cache. Even if it fails, return the found entry
977 		 * to update top-half cache. Next time, this entry will be found
978 		 * in the global cache.
979 		 */
980 		mr_btree_insert(bt, entry);
981 		return lkey;
982 	}
983 	rte_rwlock_read_unlock(&share_cache->rwlock);
984 	/* First time to see the address? Create a new MR. */
985 	lkey = mlx5_mr_create(pd, mp_id, share_cache, entry, addr,
986 			      mr_ext_memseg_en);
987 	/*
988 	 * Update the local cache if successfully created a new global MR. Even
989 	 * if failed to create one, there's no action to take in this datapath
990 	 * code. As returning LKey is invalid, this will eventually make HW
991 	 * fail.
992 	 */
993 	if (lkey != UINT32_MAX)
994 		mr_btree_insert(bt, entry);
995 	return lkey;
996 }
997 
998 /**
999  * Bottom-half of LKey search on datapath. First search in cache_bh[] and if
1000  * misses, search in the global MR cache table and update the new entry to
1001  * per-queue local caches.
1002  *
1003  * @param pd
1004  *   Pointer to pd of a device (net, regex, vdpa,...).
1005  * @param mp_id
1006  *   Multi-process identifier, may be NULL for the primary process.
1007  * @param share_cache
1008  *   Pointer to a global shared MR cache.
1009  * @param mr_ctrl
1010  *   Pointer to per-queue MR control structure.
1011  * @param addr
1012  *   Search key.
1013  * @param mr_ext_memseg_en
1014  *   Configurable flag about external memory segment enable or not.
1015  *
1016  * @return
1017  *   Searched LKey on success, UINT32_MAX on no match.
1018  */
1019 static uint32_t
1020 mlx5_mr_addr2mr_bh(void *pd, struct mlx5_mp_id *mp_id,
1021 		   struct mlx5_mr_share_cache *share_cache,
1022 		   struct mlx5_mr_ctrl *mr_ctrl, uintptr_t addr,
1023 		   unsigned int mr_ext_memseg_en)
1024 {
1025 	uint32_t lkey;
1026 	uint16_t bh_idx = 0;
1027 	/* Victim in top-half cache to replace with new entry. */
1028 	struct mr_cache_entry *repl = &mr_ctrl->cache[mr_ctrl->head];
1029 
1030 	/* Binary-search MR translation table. */
1031 	lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
1032 	/* Update top-half cache. */
1033 	if (likely(lkey != UINT32_MAX)) {
1034 		*repl = (*mr_ctrl->cache_bh.table)[bh_idx];
1035 	} else {
1036 		/*
1037 		 * If missed in local lookup table, search in the global cache
1038 		 * and local cache_bh[] will be updated inside if possible.
1039 		 * Top-half cache entry will also be updated.
1040 		 */
1041 		lkey = mr_lookup_caches(pd, mp_id, share_cache, mr_ctrl,
1042 					repl, addr, mr_ext_memseg_en);
1043 		if (unlikely(lkey == UINT32_MAX))
1044 			return UINT32_MAX;
1045 	}
1046 	/* Update the most recently used entry. */
1047 	mr_ctrl->mru = mr_ctrl->head;
1048 	/* Point to the next victim, the oldest. */
1049 	mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N;
1050 	return lkey;
1051 }
1052 
1053 /**
1054  * Release all the created MRs and resources on global MR cache of a device
1055  * list.
1056  *
1057  * @param share_cache
1058  *   Pointer to a global shared MR cache.
1059  */
1060 void
1061 mlx5_mr_release_cache(struct mlx5_mr_share_cache *share_cache)
1062 {
1063 	struct mlx5_mr *mr_next;
1064 
1065 	rte_rwlock_write_lock(&share_cache->rwlock);
1066 	/* Detach from MR list and move to free list. */
1067 	mr_next = LIST_FIRST(&share_cache->mr_list);
1068 	while (mr_next != NULL) {
1069 		struct mlx5_mr *mr = mr_next;
1070 
1071 		mr_next = LIST_NEXT(mr, mr);
1072 		LIST_REMOVE(mr, mr);
1073 		LIST_INSERT_HEAD(&share_cache->mr_free_list, mr, mr);
1074 	}
1075 	LIST_INIT(&share_cache->mr_list);
1076 	/* Free global cache. */
1077 	mlx5_mr_btree_free(&share_cache->cache);
1078 	rte_rwlock_write_unlock(&share_cache->rwlock);
1079 	/* Free all remaining MRs. */
1080 	mlx5_mr_garbage_collect(share_cache);
1081 }
1082 
1083 /**
1084  * Initialize global MR cache of a device.
1085  *
1086  * @param share_cache
1087  *   Pointer to a global shared MR cache.
1088  * @param socket
1089  *   NUMA socket on which memory must be allocated.
1090  *
1091  * @return
1092  *   0 on success, a negative errno value otherwise and rte_errno is set.
1093  */
1094 int
1095 mlx5_mr_create_cache(struct mlx5_mr_share_cache *share_cache, int socket)
1096 {
1097 	/* Set the reg_mr and dereg_mr callback functions */
1098 	mlx5_os_set_reg_mr_cb(&share_cache->reg_mr_cb,
1099 			      &share_cache->dereg_mr_cb);
1100 	rte_rwlock_init(&share_cache->rwlock);
1101 	rte_rwlock_init(&share_cache->mprwlock);
1102 	share_cache->mp_cb_registered = 0;
1103 	/* Initialize B-tree and allocate memory for global MR cache table. */
1104 	return mlx5_mr_btree_init(&share_cache->cache,
1105 				  MLX5_MR_BTREE_CACHE_N * 2, socket);
1106 }
1107 
1108 /**
1109  * Flush all of the local cache entries.
1110  *
1111  * @param mr_ctrl
1112  *   Pointer to per-queue MR local cache.
1113  */
1114 void
1115 mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl)
1116 {
1117 	/* Reset the most-recently-used index. */
1118 	mr_ctrl->mru = 0;
1119 	/* Reset the linear search array. */
1120 	mr_ctrl->head = 0;
1121 	memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache));
1122 	/* Reset the B-tree table. */
1123 	mr_ctrl->cache_bh.len = 1;
1124 	mr_ctrl->cache_bh.overflow = 0;
1125 	/* Update the generation number. */
1126 	mr_ctrl->cur_gen = *mr_ctrl->dev_gen_ptr;
1127 	DRV_LOG(DEBUG, "mr_ctrl(%p): flushed, cur_gen=%d",
1128 		(void *)mr_ctrl, mr_ctrl->cur_gen);
1129 }
1130 
1131 /**
1132  * Creates a memory region for external memory, that is memory which is not
1133  * part of the DPDK memory segments.
1134  *
1135  * @param pd
1136  *   Pointer to pd of a device (net, regex, vdpa,...).
1137  * @param addr
1138  *   Starting virtual address of memory.
1139  * @param len
1140  *   Length of memory segment being mapped.
1141  * @param socked_id
1142  *   Socket to allocate heap memory for the control structures.
1143  *
1144  * @return
1145  *   Pointer to MR structure on success, NULL otherwise.
1146  */
1147 struct mlx5_mr *
1148 mlx5_create_mr_ext(void *pd, uintptr_t addr, size_t len, int socket_id,
1149 		   mlx5_reg_mr_t reg_mr_cb)
1150 {
1151 	struct mlx5_mr *mr = NULL;
1152 
1153 	mr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1154 			 RTE_ALIGN_CEIL(sizeof(*mr), RTE_CACHE_LINE_SIZE),
1155 			 RTE_CACHE_LINE_SIZE, socket_id);
1156 	if (mr == NULL)
1157 		return NULL;
1158 	reg_mr_cb(pd, (void *)addr, len, &mr->pmd_mr);
1159 	if (mr->pmd_mr.obj == NULL) {
1160 		DRV_LOG(WARNING,
1161 			"Fail to create MR for address (%p)",
1162 			(void *)addr);
1163 		mlx5_free(mr);
1164 		return NULL;
1165 	}
1166 	mr->msl = NULL; /* Mark it is external memory. */
1167 	mr->ms_bmp = NULL;
1168 	mr->ms_n = 1;
1169 	mr->ms_bmp_n = 1;
1170 	DRV_LOG(DEBUG,
1171 		"MR CREATED (%p) for external memory %p:\n"
1172 		"  [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
1173 		" lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
1174 		(void *)mr, (void *)addr,
1175 		addr, addr + len, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1176 		mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
1177 	return mr;
1178 }
1179 
1180 /**
1181  * Callback for memory free event. Iterate freed memsegs and check whether it
1182  * belongs to an existing MR. If found, clear the bit from bitmap of MR. As a
1183  * result, the MR would be fragmented. If it becomes empty, the MR will be freed
1184  * later by mlx5_mr_garbage_collect(). Even if this callback is called from a
1185  * secondary process, the garbage collector will be called in primary process
1186  * as the secondary process can't call mlx5_mr_create().
1187  *
1188  * The global cache must be rebuilt if there's any change and this event has to
1189  * be propagated to dataplane threads to flush the local caches.
1190  *
1191  * @param share_cache
1192  *   Pointer to a global shared MR cache.
1193  * @param ibdev_name
1194  *   Name of ibv device.
1195  * @param addr
1196  *   Address of freed memory.
1197  * @param len
1198  *   Size of freed memory.
1199  */
1200 void
1201 mlx5_free_mr_by_addr(struct mlx5_mr_share_cache *share_cache,
1202 		     const char *ibdev_name, const void *addr, size_t len)
1203 {
1204 	const struct rte_memseg_list *msl;
1205 	struct mlx5_mr *mr;
1206 	int ms_n;
1207 	int i;
1208 	int rebuild = 0;
1209 
1210 	DRV_LOG(DEBUG, "device %s free callback: addr=%p, len=%zu",
1211 		ibdev_name, addr, len);
1212 	msl = rte_mem_virt2memseg_list(addr);
1213 	/* addr and len must be page-aligned. */
1214 	MLX5_ASSERT((uintptr_t)addr ==
1215 		    RTE_ALIGN((uintptr_t)addr, msl->page_sz));
1216 	MLX5_ASSERT(len == RTE_ALIGN(len, msl->page_sz));
1217 	ms_n = len / msl->page_sz;
1218 	rte_rwlock_write_lock(&share_cache->rwlock);
1219 	/* Clear bits of freed memsegs from MR. */
1220 	for (i = 0; i < ms_n; ++i) {
1221 		const struct rte_memseg *ms;
1222 		struct mr_cache_entry entry;
1223 		uintptr_t start;
1224 		int ms_idx;
1225 		uint32_t pos;
1226 
1227 		/* Find MR having this memseg. */
1228 		start = (uintptr_t)addr + i * msl->page_sz;
1229 		mr = mlx5_mr_lookup_list(share_cache, &entry, start);
1230 		if (mr == NULL)
1231 			continue;
1232 		MLX5_ASSERT(mr->msl); /* Can't be external memory. */
1233 		ms = rte_mem_virt2memseg((void *)start, msl);
1234 		MLX5_ASSERT(ms != NULL);
1235 		MLX5_ASSERT(msl->page_sz == ms->hugepage_sz);
1236 		ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
1237 		pos = ms_idx - mr->ms_base_idx;
1238 		MLX5_ASSERT(rte_bitmap_get(mr->ms_bmp, pos));
1239 		MLX5_ASSERT(pos < mr->ms_bmp_n);
1240 		DRV_LOG(DEBUG, "device %s MR(%p): clear bitmap[%u] for addr %p",
1241 			ibdev_name, (void *)mr, pos, (void *)start);
1242 		rte_bitmap_clear(mr->ms_bmp, pos);
1243 		if (--mr->ms_n == 0) {
1244 			LIST_REMOVE(mr, mr);
1245 			LIST_INSERT_HEAD(&share_cache->mr_free_list, mr, mr);
1246 			DRV_LOG(DEBUG, "device %s remove MR(%p) from list",
1247 				ibdev_name, (void *)mr);
1248 		}
1249 		/*
1250 		 * MR is fragmented or will be freed. the global cache must be
1251 		 * rebuilt.
1252 		 */
1253 		rebuild = 1;
1254 	}
1255 	if (rebuild) {
1256 		mlx5_mr_rebuild_cache(share_cache);
1257 		/*
1258 		 * No explicit wmb is needed after updating dev_gen due to
1259 		 * store-release ordering in unlock that provides the
1260 		 * implicit barrier at the software visible level.
1261 		 */
1262 		++share_cache->dev_gen;
1263 		DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
1264 			share_cache->dev_gen);
1265 	}
1266 	rte_rwlock_write_unlock(&share_cache->rwlock);
1267 }
1268 
1269 /**
1270  * Dump all the created MRs and the global cache entries.
1271  *
1272  * @param share_cache
1273  *   Pointer to a global shared MR cache.
1274  */
1275 void
1276 mlx5_mr_dump_cache(struct mlx5_mr_share_cache *share_cache __rte_unused)
1277 {
1278 #ifdef RTE_LIBRTE_MLX5_DEBUG
1279 	struct mlx5_mr *mr;
1280 	int mr_n = 0;
1281 	int chunk_n = 0;
1282 
1283 	rte_rwlock_read_lock(&share_cache->rwlock);
1284 	/* Iterate all the existing MRs. */
1285 	LIST_FOREACH(mr, &share_cache->mr_list, mr) {
1286 		unsigned int n;
1287 
1288 		DRV_LOG(DEBUG, "MR[%u], LKey = 0x%x, ms_n = %u, ms_bmp_n = %u",
1289 		      mr_n++, rte_cpu_to_be_32(mr->pmd_mr.lkey),
1290 		      mr->ms_n, mr->ms_bmp_n);
1291 		if (mr->ms_n == 0)
1292 			continue;
1293 		for (n = 0; n < mr->ms_bmp_n; ) {
1294 			struct mr_cache_entry ret = { 0, };
1295 
1296 			n = mr_find_next_chunk(mr, &ret, n);
1297 			if (!ret.end)
1298 				break;
1299 			DRV_LOG(DEBUG,
1300 				"  chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR ")",
1301 				chunk_n++, ret.start, ret.end);
1302 		}
1303 	}
1304 	DRV_LOG(DEBUG, "Dumping global cache %p", (void *)share_cache);
1305 	mlx5_mr_btree_dump(&share_cache->cache);
1306 	rte_rwlock_read_unlock(&share_cache->rwlock);
1307 #endif
1308 }
1309 
1310 static int
1311 mlx5_range_compare_start(const void *lhs, const void *rhs)
1312 {
1313 	const struct mlx5_range *r1 = lhs, *r2 = rhs;
1314 
1315 	if (r1->start > r2->start)
1316 		return 1;
1317 	else if (r1->start < r2->start)
1318 		return -1;
1319 	return 0;
1320 }
1321 
1322 static void
1323 mlx5_range_from_mempool_chunk(struct rte_mempool *mp, void *opaque,
1324 			      struct rte_mempool_memhdr *memhdr,
1325 			      unsigned int idx)
1326 {
1327 	struct mlx5_range *ranges = opaque, *range = &ranges[idx];
1328 	uint64_t page_size = rte_mem_page_size();
1329 
1330 	RTE_SET_USED(mp);
1331 	range->start = RTE_ALIGN_FLOOR((uintptr_t)memhdr->addr, page_size);
1332 	range->end = RTE_ALIGN_CEIL(range->start + memhdr->len, page_size);
1333 }
1334 
1335 /**
1336  * Get VA-contiguous ranges of the mempool memory.
1337  * Each range start and end is aligned to the system page size.
1338  *
1339  * @param[in] mp
1340  *   Analyzed mempool.
1341  * @param[out] out
1342  *   Receives the ranges, caller must release it with free().
1343  * @param[out] ount_n
1344  *   Receives the number of @p out elements.
1345  *
1346  * @return
1347  *   0 on success, (-1) on failure.
1348  */
1349 static int
1350 mlx5_get_mempool_ranges(struct rte_mempool *mp, struct mlx5_range **out,
1351 			unsigned int *out_n)
1352 {
1353 	struct mlx5_range *chunks;
1354 	unsigned int chunks_n = mp->nb_mem_chunks, contig_n, i;
1355 
1356 	/* Collect page-aligned memory ranges of the mempool. */
1357 	chunks = calloc(sizeof(chunks[0]), chunks_n);
1358 	if (chunks == NULL)
1359 		return -1;
1360 	rte_mempool_mem_iter(mp, mlx5_range_from_mempool_chunk, chunks);
1361 	/* Merge adjacent chunks and place them at the beginning. */
1362 	qsort(chunks, chunks_n, sizeof(chunks[0]), mlx5_range_compare_start);
1363 	contig_n = 1;
1364 	for (i = 1; i < chunks_n; i++)
1365 		if (chunks[i - 1].end != chunks[i].start) {
1366 			chunks[contig_n - 1].end = chunks[i - 1].end;
1367 			chunks[contig_n] = chunks[i];
1368 			contig_n++;
1369 		}
1370 	/* Extend the last contiguous chunk to the end of the mempool. */
1371 	chunks[contig_n - 1].end = chunks[i - 1].end;
1372 	*out = chunks;
1373 	*out_n = contig_n;
1374 	return 0;
1375 }
1376 
1377 /**
1378  * Analyze mempool memory to select memory ranges to register.
1379  *
1380  * @param[in] mp
1381  *   Mempool to analyze.
1382  * @param[out] out
1383  *   Receives memory ranges to register, aligned to the system page size.
1384  *   The caller must release them with free().
1385  * @param[out] out_n
1386  *   Receives the number of @p out items.
1387  * @param[out] share_hugepage
1388  *   Receives True if the entire pool resides within a single hugepage.
1389  *
1390  * @return
1391  *   0 on success, (-1) on failure.
1392  */
1393 static int
1394 mlx5_mempool_reg_analyze(struct rte_mempool *mp, struct mlx5_range **out,
1395 			 unsigned int *out_n, bool *share_hugepage)
1396 {
1397 	struct mlx5_range *ranges = NULL;
1398 	unsigned int i, ranges_n = 0;
1399 	struct rte_memseg_list *msl;
1400 
1401 	if (mlx5_get_mempool_ranges(mp, &ranges, &ranges_n) < 0) {
1402 		DRV_LOG(ERR, "Cannot get address ranges for mempool %s",
1403 			mp->name);
1404 		return -1;
1405 	}
1406 	/* Check if the hugepage of the pool can be shared. */
1407 	*share_hugepage = false;
1408 	msl = rte_mem_virt2memseg_list((void *)ranges[0].start);
1409 	if (msl != NULL) {
1410 		uint64_t hugepage_sz = 0;
1411 
1412 		/* Check that all ranges are on pages of the same size. */
1413 		for (i = 0; i < ranges_n; i++) {
1414 			if (hugepage_sz != 0 && hugepage_sz != msl->page_sz)
1415 				break;
1416 			hugepage_sz = msl->page_sz;
1417 		}
1418 		if (i == ranges_n) {
1419 			/*
1420 			 * If the entire pool is within one hugepage,
1421 			 * combine all ranges into one of the hugepage size.
1422 			 */
1423 			uintptr_t reg_start = ranges[0].start;
1424 			uintptr_t reg_end = ranges[ranges_n - 1].end;
1425 			uintptr_t hugepage_start =
1426 				RTE_ALIGN_FLOOR(reg_start, hugepage_sz);
1427 			uintptr_t hugepage_end = hugepage_start + hugepage_sz;
1428 			if (reg_end < hugepage_end) {
1429 				ranges[0].start = hugepage_start;
1430 				ranges[0].end = hugepage_end;
1431 				ranges_n = 1;
1432 				*share_hugepage = true;
1433 			}
1434 		}
1435 	}
1436 	*out = ranges;
1437 	*out_n = ranges_n;
1438 	return 0;
1439 }
1440 
1441 /** Create a registration object for the mempool. */
1442 static struct mlx5_mempool_reg *
1443 mlx5_mempool_reg_create(struct rte_mempool *mp, unsigned int mrs_n)
1444 {
1445 	struct mlx5_mempool_reg *mpr = NULL;
1446 
1447 	mpr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1448 			  sizeof(*mpr) + mrs_n * sizeof(mpr->mrs[0]),
1449 			  RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
1450 	if (mpr == NULL) {
1451 		DRV_LOG(ERR, "Cannot allocate mempool %s registration object",
1452 			mp->name);
1453 		return NULL;
1454 	}
1455 	mpr->mp = mp;
1456 	mpr->mrs = (struct mlx5_mempool_mr *)(mpr + 1);
1457 	mpr->mrs_n = mrs_n;
1458 	return mpr;
1459 }
1460 
1461 /**
1462  * Destroy a mempool registration object.
1463  *
1464  * @param standalone
1465  *   Whether @p mpr owns its MRs excludively, i.e. they are not shared.
1466  */
1467 static void
1468 mlx5_mempool_reg_destroy(struct mlx5_mr_share_cache *share_cache,
1469 			 struct mlx5_mempool_reg *mpr, bool standalone)
1470 {
1471 	if (standalone) {
1472 		unsigned int i;
1473 
1474 		for (i = 0; i < mpr->mrs_n; i++)
1475 			share_cache->dereg_mr_cb(&mpr->mrs[i].pmd_mr);
1476 	}
1477 	mlx5_free(mpr);
1478 }
1479 
1480 /** Find registration object of a mempool. */
1481 static struct mlx5_mempool_reg *
1482 mlx5_mempool_reg_lookup(struct mlx5_mr_share_cache *share_cache,
1483 			struct rte_mempool *mp)
1484 {
1485 	struct mlx5_mempool_reg *mpr;
1486 
1487 	LIST_FOREACH(mpr, &share_cache->mempool_reg_list, next)
1488 		if (mpr->mp == mp)
1489 			break;
1490 	return mpr;
1491 }
1492 
1493 /** Increment reference counters of MRs used in the registration. */
1494 static void
1495 mlx5_mempool_reg_attach(struct mlx5_mempool_reg *mpr)
1496 {
1497 	unsigned int i;
1498 
1499 	for (i = 0; i < mpr->mrs_n; i++)
1500 		__atomic_add_fetch(&mpr->mrs[i].refcnt, 1, __ATOMIC_RELAXED);
1501 }
1502 
1503 /**
1504  * Decrement reference counters of MRs used in the registration.
1505  *
1506  * @return True if no more references to @p mpr MRs exist, False otherwise.
1507  */
1508 static bool
1509 mlx5_mempool_reg_detach(struct mlx5_mempool_reg *mpr)
1510 {
1511 	unsigned int i;
1512 	bool ret = false;
1513 
1514 	for (i = 0; i < mpr->mrs_n; i++)
1515 		ret |= __atomic_sub_fetch(&mpr->mrs[i].refcnt, 1,
1516 					  __ATOMIC_RELAXED) == 0;
1517 	return ret;
1518 }
1519 
1520 static int
1521 mlx5_mr_mempool_register_primary(struct mlx5_mr_share_cache *share_cache,
1522 				 void *pd, struct rte_mempool *mp)
1523 {
1524 	struct mlx5_range *ranges = NULL;
1525 	struct mlx5_mempool_reg *mpr, *new_mpr;
1526 	unsigned int i, ranges_n;
1527 	bool share_hugepage;
1528 	int ret = -1;
1529 
1530 	/* Early check to avoid unnecessary creation of MRs. */
1531 	rte_rwlock_read_lock(&share_cache->rwlock);
1532 	mpr = mlx5_mempool_reg_lookup(share_cache, mp);
1533 	rte_rwlock_read_unlock(&share_cache->rwlock);
1534 	if (mpr != NULL) {
1535 		DRV_LOG(DEBUG, "Mempool %s is already registered for PD %p",
1536 			mp->name, pd);
1537 		rte_errno = EEXIST;
1538 		goto exit;
1539 	}
1540 	if (mlx5_mempool_reg_analyze(mp, &ranges, &ranges_n,
1541 				     &share_hugepage) < 0) {
1542 		DRV_LOG(ERR, "Cannot get mempool %s memory ranges", mp->name);
1543 		rte_errno = ENOMEM;
1544 		goto exit;
1545 	}
1546 	new_mpr = mlx5_mempool_reg_create(mp, ranges_n);
1547 	if (new_mpr == NULL) {
1548 		DRV_LOG(ERR,
1549 			"Cannot create a registration object for mempool %s in PD %p",
1550 			mp->name, pd);
1551 		rte_errno = ENOMEM;
1552 		goto exit;
1553 	}
1554 	/*
1555 	 * If the entire mempool fits in a single hugepage, the MR for this
1556 	 * hugepage can be shared across mempools that also fit in it.
1557 	 */
1558 	if (share_hugepage) {
1559 		rte_rwlock_write_lock(&share_cache->rwlock);
1560 		LIST_FOREACH(mpr, &share_cache->mempool_reg_list, next) {
1561 			if (mpr->mrs[0].pmd_mr.addr == (void *)ranges[0].start)
1562 				break;
1563 		}
1564 		if (mpr != NULL) {
1565 			new_mpr->mrs = mpr->mrs;
1566 			mlx5_mempool_reg_attach(new_mpr);
1567 			LIST_INSERT_HEAD(&share_cache->mempool_reg_list,
1568 					 new_mpr, next);
1569 		}
1570 		rte_rwlock_write_unlock(&share_cache->rwlock);
1571 		if (mpr != NULL) {
1572 			DRV_LOG(DEBUG, "Shared MR %#x in PD %p for mempool %s with mempool %s",
1573 				mpr->mrs[0].pmd_mr.lkey, pd, mp->name,
1574 				mpr->mp->name);
1575 			ret = 0;
1576 			goto exit;
1577 		}
1578 	}
1579 	for (i = 0; i < ranges_n; i++) {
1580 		struct mlx5_mempool_mr *mr = &new_mpr->mrs[i];
1581 		const struct mlx5_range *range = &ranges[i];
1582 		size_t len = range->end - range->start;
1583 
1584 		if (share_cache->reg_mr_cb(pd, (void *)range->start, len,
1585 		    &mr->pmd_mr) < 0) {
1586 			DRV_LOG(ERR,
1587 				"Failed to create an MR in PD %p for address range "
1588 				"[0x%" PRIxPTR ", 0x%" PRIxPTR "] (%zu bytes) for mempool %s",
1589 				pd, range->start, range->end, len, mp->name);
1590 			break;
1591 		}
1592 		DRV_LOG(DEBUG,
1593 			"Created a new MR %#x in PD %p for address range "
1594 			"[0x%" PRIxPTR ", 0x%" PRIxPTR "] (%zu bytes) for mempool %s",
1595 			mr->pmd_mr.lkey, pd, range->start, range->end, len,
1596 			mp->name);
1597 	}
1598 	if (i != ranges_n) {
1599 		mlx5_mempool_reg_destroy(share_cache, new_mpr, true);
1600 		rte_errno = EINVAL;
1601 		goto exit;
1602 	}
1603 	/* Concurrent registration is not supposed to happen. */
1604 	rte_rwlock_write_lock(&share_cache->rwlock);
1605 	mpr = mlx5_mempool_reg_lookup(share_cache, mp);
1606 	if (mpr == NULL) {
1607 		mlx5_mempool_reg_attach(new_mpr);
1608 		LIST_INSERT_HEAD(&share_cache->mempool_reg_list, new_mpr, next);
1609 		ret = 0;
1610 	}
1611 	rte_rwlock_write_unlock(&share_cache->rwlock);
1612 	if (mpr != NULL) {
1613 		DRV_LOG(DEBUG, "Mempool %s is already registered for PD %p",
1614 			mp->name, pd);
1615 		mlx5_mempool_reg_destroy(share_cache, new_mpr, true);
1616 		rte_errno = EEXIST;
1617 		goto exit;
1618 	}
1619 exit:
1620 	free(ranges);
1621 	return ret;
1622 }
1623 
1624 static int
1625 mlx5_mr_mempool_register_secondary(struct mlx5_mr_share_cache *share_cache,
1626 				   void *pd, struct rte_mempool *mp,
1627 				   struct mlx5_mp_id *mp_id)
1628 {
1629 	if (mp_id == NULL) {
1630 		rte_errno = EINVAL;
1631 		return -1;
1632 	}
1633 	return mlx5_mp_req_mempool_reg(mp_id, share_cache, pd, mp, true);
1634 }
1635 
1636 /**
1637  * Register the memory of a mempool in the protection domain.
1638  *
1639  * @param share_cache
1640  *   Shared MR cache of the protection domain.
1641  * @param pd
1642  *   Protection domain object.
1643  * @param mp
1644  *   Mempool to register.
1645  * @param mp_id
1646  *   Multi-process identifier, may be NULL for the primary process.
1647  *
1648  * @return
1649  *   0 on success, (-1) on failure and rte_errno is set.
1650  */
1651 int
1652 mlx5_mr_mempool_register(struct mlx5_mr_share_cache *share_cache, void *pd,
1653 			 struct rte_mempool *mp, struct mlx5_mp_id *mp_id)
1654 {
1655 	if (mp->flags & RTE_MEMPOOL_F_NON_IO)
1656 		return 0;
1657 	switch (rte_eal_process_type()) {
1658 	case RTE_PROC_PRIMARY:
1659 		return mlx5_mr_mempool_register_primary(share_cache, pd, mp);
1660 	case RTE_PROC_SECONDARY:
1661 		return mlx5_mr_mempool_register_secondary(share_cache, pd, mp,
1662 							  mp_id);
1663 	default:
1664 		return -1;
1665 	}
1666 }
1667 
1668 static int
1669 mlx5_mr_mempool_unregister_primary(struct mlx5_mr_share_cache *share_cache,
1670 				   struct rte_mempool *mp)
1671 {
1672 	struct mlx5_mempool_reg *mpr;
1673 	bool standalone = false;
1674 
1675 	rte_rwlock_write_lock(&share_cache->rwlock);
1676 	LIST_FOREACH(mpr, &share_cache->mempool_reg_list, next)
1677 		if (mpr->mp == mp) {
1678 			LIST_REMOVE(mpr, next);
1679 			standalone = mlx5_mempool_reg_detach(mpr);
1680 			if (standalone)
1681 				/*
1682 				 * The unlock operation below provides a memory
1683 				 * barrier due to its store-release semantics.
1684 				 */
1685 				++share_cache->dev_gen;
1686 			break;
1687 		}
1688 	rte_rwlock_write_unlock(&share_cache->rwlock);
1689 	if (mpr == NULL) {
1690 		rte_errno = ENOENT;
1691 		return -1;
1692 	}
1693 	mlx5_mempool_reg_destroy(share_cache, mpr, standalone);
1694 	return 0;
1695 }
1696 
1697 static int
1698 mlx5_mr_mempool_unregister_secondary(struct mlx5_mr_share_cache *share_cache,
1699 				     struct rte_mempool *mp,
1700 				     struct mlx5_mp_id *mp_id)
1701 {
1702 	if (mp_id == NULL) {
1703 		rte_errno = EINVAL;
1704 		return -1;
1705 	}
1706 	return mlx5_mp_req_mempool_reg(mp_id, share_cache, NULL, mp, false);
1707 }
1708 
1709 /**
1710  * Unregister the memory of a mempool from the protection domain.
1711  *
1712  * @param share_cache
1713  *   Shared MR cache of the protection domain.
1714  * @param mp
1715  *   Mempool to unregister.
1716  * @param mp_id
1717  *   Multi-process identifier, may be NULL for the primary process.
1718  *
1719  * @return
1720  *   0 on success, (-1) on failure and rte_errno is set.
1721  */
1722 int
1723 mlx5_mr_mempool_unregister(struct mlx5_mr_share_cache *share_cache,
1724 			   struct rte_mempool *mp, struct mlx5_mp_id *mp_id)
1725 {
1726 	if (mp->flags & RTE_MEMPOOL_F_NON_IO)
1727 		return 0;
1728 	switch (rte_eal_process_type()) {
1729 	case RTE_PROC_PRIMARY:
1730 		return mlx5_mr_mempool_unregister_primary(share_cache, mp);
1731 	case RTE_PROC_SECONDARY:
1732 		return mlx5_mr_mempool_unregister_secondary(share_cache, mp,
1733 							    mp_id);
1734 	default:
1735 		return -1;
1736 	}
1737 }
1738 
1739 /**
1740  * Lookup a MR key by and address in a registered mempool.
1741  *
1742  * @param mpr
1743  *   Mempool registration object.
1744  * @param addr
1745  *   Address within the mempool.
1746  * @param entry
1747  *   Bottom-half cache entry to fill.
1748  *
1749  * @return
1750  *   MR key or UINT32_MAX on failure, which can only happen
1751  *   if the address is not from within the mempool.
1752  */
1753 static uint32_t
1754 mlx5_mempool_reg_addr2mr(struct mlx5_mempool_reg *mpr, uintptr_t addr,
1755 			 struct mr_cache_entry *entry)
1756 {
1757 	uint32_t lkey = UINT32_MAX;
1758 	unsigned int i;
1759 
1760 	for (i = 0; i < mpr->mrs_n; i++) {
1761 		const struct mlx5_pmd_mr *mr = &mpr->mrs[i].pmd_mr;
1762 		uintptr_t mr_addr = (uintptr_t)mr->addr;
1763 
1764 		if (mr_addr <= addr) {
1765 			lkey = rte_cpu_to_be_32(mr->lkey);
1766 			entry->start = mr_addr;
1767 			entry->end = mr_addr + mr->len;
1768 			entry->lkey = lkey;
1769 			break;
1770 		}
1771 	}
1772 	return lkey;
1773 }
1774 
1775 /**
1776  * Update bottom-half cache from the list of mempool registrations.
1777  *
1778  * @param share_cache
1779  *   Pointer to a global shared MR cache.
1780  * @param mr_ctrl
1781  *   Per-queue MR control handle.
1782  * @param entry
1783  *   Pointer to an entry in the bottom-half cache to update
1784  *   with the MR lkey looked up.
1785  * @param mp
1786  *   Mempool containing the address.
1787  * @param addr
1788  *   Address to lookup.
1789  * @return
1790  *   MR lkey on success, UINT32_MAX on failure.
1791  */
1792 static uint32_t
1793 mlx5_lookup_mempool_regs(struct mlx5_mr_share_cache *share_cache,
1794 			 struct mlx5_mr_ctrl *mr_ctrl,
1795 			 struct mr_cache_entry *entry,
1796 			 struct rte_mempool *mp, uintptr_t addr)
1797 {
1798 	struct mlx5_mr_btree *bt = &mr_ctrl->cache_bh;
1799 	struct mlx5_mempool_reg *mpr;
1800 	uint32_t lkey = UINT32_MAX;
1801 
1802 	/* If local cache table is full, try to double it. */
1803 	if (unlikely(bt->len == bt->size))
1804 		mr_btree_expand(bt, bt->size << 1);
1805 	/* Look up in mempool registrations. */
1806 	rte_rwlock_read_lock(&share_cache->rwlock);
1807 	mpr = mlx5_mempool_reg_lookup(share_cache, mp);
1808 	if (mpr != NULL)
1809 		lkey = mlx5_mempool_reg_addr2mr(mpr, addr, entry);
1810 	rte_rwlock_read_unlock(&share_cache->rwlock);
1811 	/*
1812 	 * Update local cache. Even if it fails, return the found entry
1813 	 * to update top-half cache. Next time, this entry will be found
1814 	 * in the global cache.
1815 	 */
1816 	if (lkey != UINT32_MAX)
1817 		mr_btree_insert(bt, entry);
1818 	return lkey;
1819 }
1820 
1821 /**
1822  * Bottom-half lookup for the address from the mempool.
1823  *
1824  * @param share_cache
1825  *   Pointer to a global shared MR cache.
1826  * @param mr_ctrl
1827  *   Per-queue MR control handle.
1828  * @param mp
1829  *   Mempool containing the address.
1830  * @param addr
1831  *   Address to lookup.
1832  * @return
1833  *   MR lkey on success, UINT32_MAX on failure.
1834  */
1835 uint32_t
1836 mlx5_mr_mempool2mr_bh(struct mlx5_mr_share_cache *share_cache,
1837 		      struct mlx5_mr_ctrl *mr_ctrl,
1838 		      struct rte_mempool *mp, uintptr_t addr)
1839 {
1840 	struct mr_cache_entry *repl = &mr_ctrl->cache[mr_ctrl->head];
1841 	uint32_t lkey;
1842 	uint16_t bh_idx = 0;
1843 
1844 	/* Binary-search MR translation table. */
1845 	lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
1846 	/* Update top-half cache. */
1847 	if (likely(lkey != UINT32_MAX)) {
1848 		*repl = (*mr_ctrl->cache_bh.table)[bh_idx];
1849 	} else {
1850 		lkey = mlx5_lookup_mempool_regs(share_cache, mr_ctrl, repl,
1851 						mp, addr);
1852 		/* Can only fail if the address is not from the mempool. */
1853 		if (unlikely(lkey == UINT32_MAX))
1854 			return UINT32_MAX;
1855 	}
1856 	/* Update the most recently used entry. */
1857 	mr_ctrl->mru = mr_ctrl->head;
1858 	/* Point to the next victim, the oldest. */
1859 	mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N;
1860 	return lkey;
1861 }
1862 
1863 uint32_t
1864 mlx5_mr_mb2mr_bh(struct mlx5_mr_ctrl *mr_ctrl, struct rte_mbuf *mb,
1865 		 struct mlx5_mp_id *mp_id)
1866 {
1867 	uint32_t lkey;
1868 	uintptr_t addr = (uintptr_t)mb->buf_addr;
1869 	struct mlx5_common_device *cdev = mr_ctrl->cdev;
1870 
1871 	if (cdev->config.mr_mempool_reg_en) {
1872 		struct rte_mempool *mp = NULL;
1873 		struct mlx5_mprq_buf *buf;
1874 
1875 		if (!RTE_MBUF_HAS_EXTBUF(mb)) {
1876 			mp = mlx5_mb2mp(mb);
1877 		} else if (mb->shinfo->free_cb == mlx5_mprq_buf_free_cb) {
1878 			/* Recover MPRQ mempool. */
1879 			buf = mb->shinfo->fcb_opaque;
1880 			mp = buf->mp;
1881 		}
1882 		if (mp != NULL) {
1883 			lkey = mlx5_mr_mempool2mr_bh(&cdev->mr_scache,
1884 						     mr_ctrl, mp, addr);
1885 			/*
1886 			 * Lookup can only fail on invalid input, e.g. "addr"
1887 			 * is not from "mp" or "mp" has MEMPOOL_F_NON_IO set.
1888 			 */
1889 			if (lkey != UINT32_MAX)
1890 				return lkey;
1891 		}
1892 		/* Fallback for generic mechanism in corner cases. */
1893 	}
1894 	return mlx5_mr_addr2mr_bh(cdev->pd, mp_id, &cdev->mr_scache, mr_ctrl,
1895 				  addr, cdev->config.mr_ext_memseg_en);
1896 }
1897