New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / net / mlx4 / mlx4_mr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 /**
7  * @file
8  * Memory management functions for mlx4 driver.
9  */
10
11 #include <assert.h>
12 #include <errno.h>
13 #include <inttypes.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <string.h>
17
18 /* Verbs headers do not support -pedantic. */
19 #ifdef PEDANTIC
20 #pragma GCC diagnostic ignored "-Wpedantic"
21 #endif
22 #include <infiniband/verbs.h>
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic error "-Wpedantic"
25 #endif
26
27 #include <rte_branch_prediction.h>
28 #include <rte_common.h>
29 #include <rte_errno.h>
30 #include <rte_malloc.h>
31 #include <rte_memory.h>
32 #include <rte_mempool.h>
33 #include <rte_rwlock.h>
34
35 #include "mlx4_glue.h"
36 #include "mlx4_mr.h"
37 #include "mlx4_rxtx.h"
38 #include "mlx4_utils.h"
39
40 struct mr_find_contig_memsegs_data {
41         uintptr_t addr;
42         uintptr_t start;
43         uintptr_t end;
44         const struct rte_memseg_list *msl;
45 };
46
47 struct mr_update_mp_data {
48         struct rte_eth_dev *dev;
49         struct mlx4_mr_ctrl *mr_ctrl;
50         int ret;
51 };
52
53 /**
54  * Expand B-tree table to a given size. Can't be called with holding
55  * memory_hotplug_lock or priv->mr.rwlock due to rte_realloc().
56  *
57  * @param bt
58  *   Pointer to B-tree structure.
59  * @param n
60  *   Number of entries for expansion.
61  *
62  * @return
63  *   0 on success, -1 on failure.
64  */
65 static int
66 mr_btree_expand(struct mlx4_mr_btree *bt, int n)
67 {
68         void *mem;
69         int ret = 0;
70
71         if (n <= bt->size)
72                 return ret;
73         /*
74          * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is
75          * used inside if there's no room to expand. Because this is a quite
76          * rare case and a part of very slow path, it is very acceptable.
77          * Initially cache_bh[] will be given practically enough space and once
78          * it is expanded, expansion wouldn't be needed again ever.
79          */
80         mem = rte_realloc(bt->table, n * sizeof(struct mlx4_mr_cache), 0);
81         if (mem == NULL) {
82                 /* Not an error, B-tree search will be skipped. */
83                 WARN("failed to expand MR B-tree (%p) table", (void *)bt);
84                 ret = -1;
85         } else {
86                 DEBUG("expanded MR B-tree table (size=%u)", n);
87                 bt->table = mem;
88                 bt->size = n;
89         }
90         return ret;
91 }
92
93 /**
94  * Look up LKey from given B-tree lookup table, store the last index and return
95  * searched LKey.
96  *
97  * @param bt
98  *   Pointer to B-tree structure.
99  * @param[out] idx
100  *   Pointer to index. Even on search failure, returns index where it stops
101  *   searching so that index can be used when inserting a new entry.
102  * @param addr
103  *   Search key.
104  *
105  * @return
106  *   Searched LKey on success, UINT32_MAX on no match.
107  */
108 static uint32_t
109 mr_btree_lookup(struct mlx4_mr_btree *bt, uint16_t *idx, uintptr_t addr)
110 {
111         struct mlx4_mr_cache *lkp_tbl;
112         uint16_t n;
113         uint16_t base = 0;
114
115         assert(bt != NULL);
116         lkp_tbl = *bt->table;
117         n = bt->len;
118         /* First entry must be NULL for comparison. */
119         assert(bt->len > 0 || (lkp_tbl[0].start == 0 &&
120                                lkp_tbl[0].lkey == UINT32_MAX));
121         /* Binary search. */
122         do {
123                 register uint16_t delta = n >> 1;
124
125                 if (addr < lkp_tbl[base + delta].start) {
126                         n = delta;
127                 } else {
128                         base += delta;
129                         n -= delta;
130                 }
131         } while (n > 1);
132         assert(addr >= lkp_tbl[base].start);
133         *idx = base;
134         if (addr < lkp_tbl[base].end)
135                 return lkp_tbl[base].lkey;
136         /* Not found. */
137         return UINT32_MAX;
138 }
139
140 /**
141  * Insert an entry to B-tree lookup table.
142  *
143  * @param bt
144  *   Pointer to B-tree structure.
145  * @param entry
146  *   Pointer to new entry to insert.
147  *
148  * @return
149  *   0 on success, -1 on failure.
150  */
151 static int
152 mr_btree_insert(struct mlx4_mr_btree *bt, struct mlx4_mr_cache *entry)
153 {
154         struct mlx4_mr_cache *lkp_tbl;
155         uint16_t idx = 0;
156         size_t shift;
157
158         assert(bt != NULL);
159         assert(bt->len <= bt->size);
160         assert(bt->len > 0);
161         lkp_tbl = *bt->table;
162         /* Find out the slot for insertion. */
163         if (mr_btree_lookup(bt, &idx, entry->start) != UINT32_MAX) {
164                 DEBUG("abort insertion to B-tree(%p): already exist at"
165                       " idx=%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
166                       (void *)bt, idx, entry->start, entry->end, entry->lkey);
167                 /* Already exist, return. */
168                 return 0;
169         }
170         /* If table is full, return error. */
171         if (unlikely(bt->len == bt->size)) {
172                 bt->overflow = 1;
173                 return -1;
174         }
175         /* Insert entry. */
176         ++idx;
177         shift = (bt->len - idx) * sizeof(struct mlx4_mr_cache);
178         if (shift)
179                 memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift);
180         lkp_tbl[idx] = *entry;
181         bt->len++;
182         DEBUG("inserted B-tree(%p)[%u],"
183               " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
184               (void *)bt, idx, entry->start, entry->end, entry->lkey);
185         return 0;
186 }
187
188 /**
189  * Initialize B-tree and allocate memory for lookup table.
190  *
191  * @param bt
192  *   Pointer to B-tree structure.
193  * @param n
194  *   Number of entries to allocate.
195  * @param socket
196  *   NUMA socket on which memory must be allocated.
197  *
198  * @return
199  *   0 on success, a negative errno value otherwise and rte_errno is set.
200  */
201 int
202 mlx4_mr_btree_init(struct mlx4_mr_btree *bt, int n, int socket)
203 {
204         if (bt == NULL) {
205                 rte_errno = EINVAL;
206                 return -rte_errno;
207         }
208         memset(bt, 0, sizeof(*bt));
209         bt->table = rte_calloc_socket("B-tree table",
210                                       n, sizeof(struct mlx4_mr_cache),
211                                       0, socket);
212         if (bt->table == NULL) {
213                 rte_errno = ENOMEM;
214                 ERROR("failed to allocate memory for btree cache on socket %d",
215                       socket);
216                 return -rte_errno;
217         }
218         bt->size = n;
219         /* First entry must be NULL for binary search. */
220         (*bt->table)[bt->len++] = (struct mlx4_mr_cache) {
221                 .lkey = UINT32_MAX,
222         };
223         DEBUG("initialized B-tree %p with table %p",
224               (void *)bt, (void *)bt->table);
225         return 0;
226 }
227
228 /**
229  * Free B-tree resources.
230  *
231  * @param bt
232  *   Pointer to B-tree structure.
233  */
234 void
235 mlx4_mr_btree_free(struct mlx4_mr_btree *bt)
236 {
237         if (bt == NULL)
238                 return;
239         DEBUG("freeing B-tree %p with table %p", (void *)bt, (void *)bt->table);
240         rte_free(bt->table);
241         memset(bt, 0, sizeof(*bt));
242 }
243
244 #ifndef NDEBUG
245 /**
246  * Dump all the entries in a B-tree
247  *
248  * @param bt
249  *   Pointer to B-tree structure.
250  */
251 void
252 mlx4_mr_btree_dump(struct mlx4_mr_btree *bt)
253 {
254         int idx;
255         struct mlx4_mr_cache *lkp_tbl;
256
257         if (bt == NULL)
258                 return;
259         lkp_tbl = *bt->table;
260         for (idx = 0; idx < bt->len; ++idx) {
261                 struct mlx4_mr_cache *entry = &lkp_tbl[idx];
262
263                 DEBUG("B-tree(%p)[%u],"
264                       " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=0x%x",
265                       (void *)bt, idx, entry->start, entry->end, entry->lkey);
266         }
267 }
268 #endif
269
270 /**
271  * Find virtually contiguous memory chunk in a given MR.
272  *
273  * @param dev
274  *   Pointer to MR structure.
275  * @param[out] entry
276  *   Pointer to returning MR cache entry. If not found, this will not be
277  *   updated.
278  * @param start_idx
279  *   Start index of the memseg bitmap.
280  *
281  * @return
282  *   Next index to go on lookup.
283  */
284 static int
285 mr_find_next_chunk(struct mlx4_mr *mr, struct mlx4_mr_cache *entry,
286                    int base_idx)
287 {
288         uintptr_t start = 0;
289         uintptr_t end = 0;
290         uint32_t idx = 0;
291
292         /* MR for external memory doesn't have memseg list. */
293         if (mr->msl == NULL) {
294                 struct ibv_mr *ibv_mr = mr->ibv_mr;
295
296                 assert(mr->ms_bmp_n == 1);
297                 assert(mr->ms_n == 1);
298                 assert(base_idx == 0);
299                 /*
300                  * Can't search it from memseg list but get it directly from
301                  * verbs MR as there's only one chunk.
302                  */
303                 entry->start = (uintptr_t)ibv_mr->addr;
304                 entry->end = (uintptr_t)ibv_mr->addr + mr->ibv_mr->length;
305                 entry->lkey = rte_cpu_to_be_32(mr->ibv_mr->lkey);
306                 /* Returning 1 ends iteration. */
307                 return 1;
308         }
309         for (idx = base_idx; idx < mr->ms_bmp_n; ++idx) {
310                 if (rte_bitmap_get(mr->ms_bmp, idx)) {
311                         const struct rte_memseg_list *msl;
312                         const struct rte_memseg *ms;
313
314                         msl = mr->msl;
315                         ms = rte_fbarray_get(&msl->memseg_arr,
316                                              mr->ms_base_idx + idx);
317                         assert(msl->page_sz == ms->hugepage_sz);
318                         if (!start)
319                                 start = ms->addr_64;
320                         end = ms->addr_64 + ms->hugepage_sz;
321                 } else if (start) {
322                         /* Passed the end of a fragment. */
323                         break;
324                 }
325         }
326         if (start) {
327                 /* Found one chunk. */
328                 entry->start = start;
329                 entry->end = end;
330                 entry->lkey = rte_cpu_to_be_32(mr->ibv_mr->lkey);
331         }
332         return idx;
333 }
334
335 /**
336  * Insert a MR to the global B-tree cache. It may fail due to low-on-memory.
337  * Then, this entry will have to be searched by mr_lookup_dev_list() in
338  * mlx4_mr_create() on miss.
339  *
340  * @param dev
341  *   Pointer to Ethernet device.
342  * @param mr
343  *   Pointer to MR to insert.
344  *
345  * @return
346  *   0 on success, -1 on failure.
347  */
348 static int
349 mr_insert_dev_cache(struct rte_eth_dev *dev, struct mlx4_mr *mr)
350 {
351         struct priv *priv = dev->data->dev_private;
352         unsigned int n;
353
354         DEBUG("port %u inserting MR(%p) to global cache",
355               dev->data->port_id, (void *)mr);
356         for (n = 0; n < mr->ms_bmp_n; ) {
357                 struct mlx4_mr_cache entry = { 0, };
358
359                 /* Find a contiguous chunk and advance the index. */
360                 n = mr_find_next_chunk(mr, &entry, n);
361                 if (!entry.end)
362                         break;
363                 if (mr_btree_insert(&priv->mr.cache, &entry) < 0) {
364                         /*
365                          * Overflowed, but the global table cannot be expanded
366                          * because of deadlock.
367                          */
368                         return -1;
369                 }
370         }
371         return 0;
372 }
373
374 /**
375  * Look up address in the original global MR list.
376  *
377  * @param dev
378  *   Pointer to Ethernet device.
379  * @param[out] entry
380  *   Pointer to returning MR cache entry. If no match, this will not be updated.
381  * @param addr
382  *   Search key.
383  *
384  * @return
385  *   Found MR on match, NULL otherwise.
386  */
387 static struct mlx4_mr *
388 mr_lookup_dev_list(struct rte_eth_dev *dev, struct mlx4_mr_cache *entry,
389                    uintptr_t addr)
390 {
391         struct priv *priv = dev->data->dev_private;
392         struct mlx4_mr *mr;
393
394         /* Iterate all the existing MRs. */
395         LIST_FOREACH(mr, &priv->mr.mr_list, mr) {
396                 unsigned int n;
397
398                 if (mr->ms_n == 0)
399                         continue;
400                 for (n = 0; n < mr->ms_bmp_n; ) {
401                         struct mlx4_mr_cache ret = { 0, };
402
403                         n = mr_find_next_chunk(mr, &ret, n);
404                         if (addr >= ret.start && addr < ret.end) {
405                                 /* Found. */
406                                 *entry = ret;
407                                 return mr;
408                         }
409                 }
410         }
411         return NULL;
412 }
413
414 /**
415  * Look up address on device.
416  *
417  * @param dev
418  *   Pointer to Ethernet device.
419  * @param[out] entry
420  *   Pointer to returning MR cache entry. If no match, this will not be updated.
421  * @param addr
422  *   Search key.
423  *
424  * @return
425  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
426  */
427 static uint32_t
428 mr_lookup_dev(struct rte_eth_dev *dev, struct mlx4_mr_cache *entry,
429               uintptr_t addr)
430 {
431         struct priv *priv = dev->data->dev_private;
432         uint16_t idx;
433         uint32_t lkey = UINT32_MAX;
434         struct mlx4_mr *mr;
435
436         /*
437          * If the global cache has overflowed since it failed to expand the
438          * B-tree table, it can't have all the existing MRs. Then, the address
439          * has to be searched by traversing the original MR list instead, which
440          * is very slow path. Otherwise, the global cache is all inclusive.
441          */
442         if (!unlikely(priv->mr.cache.overflow)) {
443                 lkey = mr_btree_lookup(&priv->mr.cache, &idx, addr);
444                 if (lkey != UINT32_MAX)
445                         *entry = (*priv->mr.cache.table)[idx];
446         } else {
447                 /* Falling back to the slowest path. */
448                 mr = mr_lookup_dev_list(dev, entry, addr);
449                 if (mr != NULL)
450                         lkey = entry->lkey;
451         }
452         assert(lkey == UINT32_MAX || (addr >= entry->start &&
453                                       addr < entry->end));
454         return lkey;
455 }
456
457 /**
458  * Free MR resources. MR lock must not be held to avoid a deadlock. rte_free()
459  * can raise memory free event and the callback function will spin on the lock.
460  *
461  * @param mr
462  *   Pointer to MR to free.
463  */
464 static void
465 mr_free(struct mlx4_mr *mr)
466 {
467         if (mr == NULL)
468                 return;
469         DEBUG("freeing MR(%p):", (void *)mr);
470         if (mr->ibv_mr != NULL)
471                 claim_zero(mlx4_glue->dereg_mr(mr->ibv_mr));
472         if (mr->ms_bmp != NULL)
473                 rte_bitmap_free(mr->ms_bmp);
474         rte_free(mr);
475 }
476
477 /**
478  * Releass resources of detached MR having no online entry.
479  *
480  * @param dev
481  *   Pointer to Ethernet device.
482  */
483 static void
484 mlx4_mr_garbage_collect(struct rte_eth_dev *dev)
485 {
486         struct priv *priv = dev->data->dev_private;
487         struct mlx4_mr *mr_next;
488         struct mlx4_mr_list free_list = LIST_HEAD_INITIALIZER(free_list);
489
490         /*
491          * MR can't be freed with holding the lock because rte_free() could call
492          * memory free callback function. This will be a deadlock situation.
493          */
494         rte_rwlock_write_lock(&priv->mr.rwlock);
495         /* Detach the whole free list and release it after unlocking. */
496         free_list = priv->mr.mr_free_list;
497         LIST_INIT(&priv->mr.mr_free_list);
498         rte_rwlock_write_unlock(&priv->mr.rwlock);
499         /* Release resources. */
500         mr_next = LIST_FIRST(&free_list);
501         while (mr_next != NULL) {
502                 struct mlx4_mr *mr = mr_next;
503
504                 mr_next = LIST_NEXT(mr, mr);
505                 mr_free(mr);
506         }
507 }
508
509 /* Called during rte_memseg_contig_walk() by mlx4_mr_create(). */
510 static int
511 mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl,
512                           const struct rte_memseg *ms, size_t len, void *arg)
513 {
514         struct mr_find_contig_memsegs_data *data = arg;
515
516         if (data->addr < ms->addr_64 || data->addr >= ms->addr_64 + len)
517                 return 0;
518         /* Found, save it and stop walking. */
519         data->start = ms->addr_64;
520         data->end = ms->addr_64 + len;
521         data->msl = msl;
522         return 1;
523 }
524
525 /**
526  * Create a new global Memroy Region (MR) for a missing virtual address.
527  * Register entire virtually contiguous memory chunk around the address.
528  *
529  * @param dev
530  *   Pointer to Ethernet device.
531  * @param[out] entry
532  *   Pointer to returning MR cache entry, found in the global cache or newly
533  *   created. If failed to create one, this will not be updated.
534  * @param addr
535  *   Target virtual address to register.
536  *
537  * @return
538  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
539  */
540 static uint32_t
541 mlx4_mr_create(struct rte_eth_dev *dev, struct mlx4_mr_cache *entry,
542                uintptr_t addr)
543 {
544         struct priv *priv = dev->data->dev_private;
545         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
546         const struct rte_memseg_list *msl;
547         const struct rte_memseg *ms;
548         struct mlx4_mr *mr = NULL;
549         size_t len;
550         uint32_t ms_n;
551         uint32_t bmp_size;
552         void *bmp_mem;
553         int ms_idx_shift = -1;
554         unsigned int n;
555         struct mr_find_contig_memsegs_data data = {
556                 .addr = addr,
557         };
558         struct mr_find_contig_memsegs_data data_re;
559
560         DEBUG("port %u creating a MR using address (%p)",
561               dev->data->port_id, (void *)addr);
562         /*
563          * Release detached MRs if any. This can't be called with holding either
564          * memory_hotplug_lock or priv->mr.rwlock. MRs on the free list have
565          * been detached by the memory free event but it couldn't be released
566          * inside the callback due to deadlock. As a result, releasing resources
567          * is quite opportunistic.
568          */
569         mlx4_mr_garbage_collect(dev);
570         /*
571          * Find out a contiguous virtual address chunk in use, to which the
572          * given address belongs, in order to register maximum range. In the
573          * best case where mempools are not dynamically recreated and
574          * '--socket-mem' is speicified as an EAL option, it is very likely to
575          * have only one MR(LKey) per a socket and per a hugepage-size even
576          * though the system memory is highly fragmented.
577          */
578         if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data)) {
579                 WARN("port %u unable to find virtually contiguous"
580                      " chunk for address (%p)."
581                      " rte_memseg_contig_walk() failed.",
582                      dev->data->port_id, (void *)addr);
583                 rte_errno = ENXIO;
584                 goto err_nolock;
585         }
586 alloc_resources:
587         /* Addresses must be page-aligned. */
588         assert(rte_is_aligned((void *)data.start, data.msl->page_sz));
589         assert(rte_is_aligned((void *)data.end, data.msl->page_sz));
590         msl = data.msl;
591         ms = rte_mem_virt2memseg((void *)data.start, msl);
592         len = data.end - data.start;
593         assert(msl->page_sz == ms->hugepage_sz);
594         /* Number of memsegs in the range. */
595         ms_n = len / msl->page_sz;
596         DEBUG("port %u extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
597               " page_sz=0x%" PRIx64 ", ms_n=%u",
598               dev->data->port_id, (void *)addr,
599               data.start, data.end, msl->page_sz, ms_n);
600         /* Size of memory for bitmap. */
601         bmp_size = rte_bitmap_get_memory_footprint(ms_n);
602         mr = rte_zmalloc_socket(NULL,
603                                 RTE_ALIGN_CEIL(sizeof(*mr),
604                                                RTE_CACHE_LINE_SIZE) +
605                                 bmp_size,
606                                 RTE_CACHE_LINE_SIZE, msl->socket_id);
607         if (mr == NULL) {
608                 WARN("port %u unable to allocate memory for a new MR of"
609                      " address (%p).",
610                      dev->data->port_id, (void *)addr);
611                 rte_errno = ENOMEM;
612                 goto err_nolock;
613         }
614         mr->msl = msl;
615         /*
616          * Save the index of the first memseg and initialize memseg bitmap. To
617          * see if a memseg of ms_idx in the memseg-list is still valid, check:
618          *      rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx)
619          */
620         mr->ms_base_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
621         bmp_mem = RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE);
622         mr->ms_bmp = rte_bitmap_init(ms_n, bmp_mem, bmp_size);
623         if (mr->ms_bmp == NULL) {
624                 WARN("port %u unable to initialize bitamp for a new MR of"
625                      " address (%p).",
626                      dev->data->port_id, (void *)addr);
627                 rte_errno = EINVAL;
628                 goto err_nolock;
629         }
630         /*
631          * Should recheck whether the extended contiguous chunk is still valid.
632          * Because memory_hotplug_lock can't be held if there's any memory
633          * related calls in a critical path, resource allocation above can't be
634          * locked. If the memory has been changed at this point, try again with
635          * just single page. If not, go on with the big chunk atomically from
636          * here.
637          */
638         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
639         data_re = data;
640         if (len > msl->page_sz &&
641             !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) {
642                 WARN("port %u unable to find virtually contiguous"
643                      " chunk for address (%p)."
644                      " rte_memseg_contig_walk() failed.",
645                      dev->data->port_id, (void *)addr);
646                 rte_errno = ENXIO;
647                 goto err_memlock;
648         }
649         if (data.start != data_re.start || data.end != data_re.end) {
650                 /*
651                  * The extended contiguous chunk has been changed. Try again
652                  * with single memseg instead.
653                  */
654                 data.start = RTE_ALIGN_FLOOR(addr, msl->page_sz);
655                 data.end = data.start + msl->page_sz;
656                 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
657                 mr_free(mr);
658                 goto alloc_resources;
659         }
660         assert(data.msl == data_re.msl);
661         rte_rwlock_write_lock(&priv->mr.rwlock);
662         /*
663          * Check the address is really missing. If other thread already created
664          * one or it is not found due to overflow, abort and return.
665          */
666         if (mr_lookup_dev(dev, entry, addr) != UINT32_MAX) {
667                 /*
668                  * Insert to the global cache table. It may fail due to
669                  * low-on-memory. Then, this entry will have to be searched
670                  * here again.
671                  */
672                 mr_btree_insert(&priv->mr.cache, entry);
673                 DEBUG("port %u found MR for %p on final lookup, abort",
674                       dev->data->port_id, (void *)addr);
675                 rte_rwlock_write_unlock(&priv->mr.rwlock);
676                 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
677                 /*
678                  * Must be unlocked before calling rte_free() because
679                  * mlx4_mr_mem_event_free_cb() can be called inside.
680                  */
681                 mr_free(mr);
682                 return entry->lkey;
683         }
684         /*
685          * Trim start and end addresses for verbs MR. Set bits for registering
686          * memsegs but exclude already registered ones. Bitmap can be
687          * fragmented.
688          */
689         for (n = 0; n < ms_n; ++n) {
690                 uintptr_t start;
691                 struct mlx4_mr_cache ret = { 0, };
692
693                 start = data_re.start + n * msl->page_sz;
694                 /* Exclude memsegs already registered by other MRs. */
695                 if (mr_lookup_dev(dev, &ret, start) == UINT32_MAX) {
696                         /*
697                          * Start from the first unregistered memseg in the
698                          * extended range.
699                          */
700                         if (ms_idx_shift == -1) {
701                                 mr->ms_base_idx += n;
702                                 data.start = start;
703                                 ms_idx_shift = n;
704                         }
705                         data.end = start + msl->page_sz;
706                         rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift);
707                         ++mr->ms_n;
708                 }
709         }
710         len = data.end - data.start;
711         mr->ms_bmp_n = len / msl->page_sz;
712         assert(ms_idx_shift + mr->ms_bmp_n <= ms_n);
713         /*
714          * Finally create a verbs MR for the memory chunk. ibv_reg_mr() can be
715          * called with holding the memory lock because it doesn't use
716          * mlx4_alloc_buf_extern() which eventually calls rte_malloc_socket()
717          * through mlx4_alloc_verbs_buf().
718          */
719         mr->ibv_mr = mlx4_glue->reg_mr(priv->pd, (void *)data.start, len,
720                                        IBV_ACCESS_LOCAL_WRITE);
721         if (mr->ibv_mr == NULL) {
722                 WARN("port %u fail to create a verbs MR for address (%p)",
723                      dev->data->port_id, (void *)addr);
724                 rte_errno = EINVAL;
725                 goto err_mrlock;
726         }
727         assert((uintptr_t)mr->ibv_mr->addr == data.start);
728         assert(mr->ibv_mr->length == len);
729         LIST_INSERT_HEAD(&priv->mr.mr_list, mr, mr);
730         DEBUG("port %u MR CREATED (%p) for %p:\n"
731               "  [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
732               " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
733               dev->data->port_id, (void *)mr, (void *)addr,
734               data.start, data.end, rte_cpu_to_be_32(mr->ibv_mr->lkey),
735               mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
736         /* Insert to the global cache table. */
737         mr_insert_dev_cache(dev, mr);
738         /* Fill in output data. */
739         mr_lookup_dev(dev, entry, addr);
740         /* Lookup can't fail. */
741         assert(entry->lkey != UINT32_MAX);
742         rte_rwlock_write_unlock(&priv->mr.rwlock);
743         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
744         return entry->lkey;
745 err_mrlock:
746         rte_rwlock_write_unlock(&priv->mr.rwlock);
747 err_memlock:
748         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
749 err_nolock:
750         /*
751          * In case of error, as this can be called in a datapath, a warning
752          * message per an error is preferable instead. Must be unlocked before
753          * calling rte_free() because mlx4_mr_mem_event_free_cb() can be called
754          * inside.
755          */
756         mr_free(mr);
757         return UINT32_MAX;
758 }
759
760 /**
761  * Rebuild the global B-tree cache of device from the original MR list.
762  *
763  * @param dev
764  *   Pointer to Ethernet device.
765  */
766 static void
767 mr_rebuild_dev_cache(struct rte_eth_dev *dev)
768 {
769         struct priv *priv = dev->data->dev_private;
770         struct mlx4_mr *mr;
771
772         DEBUG("port %u rebuild dev cache[]", dev->data->port_id);
773         /* Flush cache to rebuild. */
774         priv->mr.cache.len = 1;
775         priv->mr.cache.overflow = 0;
776         /* Iterate all the existing MRs. */
777         LIST_FOREACH(mr, &priv->mr.mr_list, mr)
778                 if (mr_insert_dev_cache(dev, mr) < 0)
779                         return;
780 }
781
782 /**
783  * Callback for memory free event. Iterate freed memsegs and check whether it
784  * belongs to an existing MR. If found, clear the bit from bitmap of MR. As a
785  * result, the MR would be fragmented. If it becomes empty, the MR will be freed
786  * later by mlx4_mr_garbage_collect().
787  *
788  * The global cache must be rebuilt if there's any change and this event has to
789  * be propagated to dataplane threads to flush the local caches.
790  *
791  * @param dev
792  *   Pointer to Ethernet device.
793  * @param addr
794  *   Address of freed memory.
795  * @param len
796  *   Size of freed memory.
797  */
798 static void
799 mlx4_mr_mem_event_free_cb(struct rte_eth_dev *dev, const void *addr, size_t len)
800 {
801         struct priv *priv = dev->data->dev_private;
802         const struct rte_memseg_list *msl;
803         struct mlx4_mr *mr;
804         int ms_n;
805         int i;
806         int rebuild = 0;
807
808         DEBUG("port %u free callback: addr=%p, len=%zu",
809               dev->data->port_id, addr, len);
810         msl = rte_mem_virt2memseg_list(addr);
811         /* addr and len must be page-aligned. */
812         assert((uintptr_t)addr == RTE_ALIGN((uintptr_t)addr, msl->page_sz));
813         assert(len == RTE_ALIGN(len, msl->page_sz));
814         ms_n = len / msl->page_sz;
815         rte_rwlock_write_lock(&priv->mr.rwlock);
816         /* Clear bits of freed memsegs from MR. */
817         for (i = 0; i < ms_n; ++i) {
818                 const struct rte_memseg *ms;
819                 struct mlx4_mr_cache entry;
820                 uintptr_t start;
821                 int ms_idx;
822                 uint32_t pos;
823
824                 /* Find MR having this memseg. */
825                 start = (uintptr_t)addr + i * msl->page_sz;
826                 mr = mr_lookup_dev_list(dev, &entry, start);
827                 if (mr == NULL)
828                         continue;
829                 assert(mr->msl); /* Can't be external memory. */
830                 ms = rte_mem_virt2memseg((void *)start, msl);
831                 assert(ms != NULL);
832                 assert(msl->page_sz == ms->hugepage_sz);
833                 ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
834                 pos = ms_idx - mr->ms_base_idx;
835                 assert(rte_bitmap_get(mr->ms_bmp, pos));
836                 assert(pos < mr->ms_bmp_n);
837                 DEBUG("port %u MR(%p): clear bitmap[%u] for addr %p",
838                       dev->data->port_id, (void *)mr, pos, (void *)start);
839                 rte_bitmap_clear(mr->ms_bmp, pos);
840                 if (--mr->ms_n == 0) {
841                         LIST_REMOVE(mr, mr);
842                         LIST_INSERT_HEAD(&priv->mr.mr_free_list, mr, mr);
843                         DEBUG("port %u remove MR(%p) from list",
844                               dev->data->port_id, (void *)mr);
845                 }
846                 /*
847                  * MR is fragmented or will be freed. the global cache must be
848                  * rebuilt.
849                  */
850                 rebuild = 1;
851         }
852         if (rebuild) {
853                 mr_rebuild_dev_cache(dev);
854                 /*
855                  * Flush local caches by propagating invalidation across cores.
856                  * rte_smp_wmb() is enough to synchronize this event. If one of
857                  * freed memsegs is seen by other core, that means the memseg
858                  * has been allocated by allocator, which will come after this
859                  * free call. Therefore, this store instruction (incrementing
860                  * generation below) will be guaranteed to be seen by other core
861                  * before the core sees the newly allocated memory.
862                  */
863                 ++priv->mr.dev_gen;
864                 DEBUG("broadcasting local cache flush, gen=%d",
865                       priv->mr.dev_gen);
866                 rte_smp_wmb();
867         }
868         rte_rwlock_write_unlock(&priv->mr.rwlock);
869 #ifndef NDEBUG
870         if (rebuild)
871                 mlx4_mr_dump_dev(dev);
872 #endif
873 }
874
875 /**
876  * Callback for memory event.
877  *
878  * @param event_type
879  *   Memory event type.
880  * @param addr
881  *   Address of memory.
882  * @param len
883  *   Size of memory.
884  */
885 void
886 mlx4_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
887                      size_t len, void *arg __rte_unused)
888 {
889         struct priv *priv;
890
891         switch (event_type) {
892         case RTE_MEM_EVENT_FREE:
893                 rte_rwlock_read_lock(&mlx4_mem_event_rwlock);
894                 /* Iterate all the existing mlx4 devices. */
895                 LIST_FOREACH(priv, &mlx4_mem_event_cb_list, mem_event_cb)
896                         mlx4_mr_mem_event_free_cb(priv->dev, addr, len);
897                 rte_rwlock_read_unlock(&mlx4_mem_event_rwlock);
898                 break;
899         case RTE_MEM_EVENT_ALLOC:
900         default:
901                 break;
902         }
903 }
904
905 /**
906  * Look up address in the global MR cache table. If not found, create a new MR.
907  * Insert the found/created entry to local bottom-half cache table.
908  *
909  * @param dev
910  *   Pointer to Ethernet device.
911  * @param mr_ctrl
912  *   Pointer to per-queue MR control structure.
913  * @param[out] entry
914  *   Pointer to returning MR cache entry, found in the global cache or newly
915  *   created. If failed to create one, this is not written.
916  * @param addr
917  *   Search key.
918  *
919  * @return
920  *   Searched LKey on success, UINT32_MAX on no match.
921  */
922 static uint32_t
923 mlx4_mr_lookup_dev(struct rte_eth_dev *dev, struct mlx4_mr_ctrl *mr_ctrl,
924                    struct mlx4_mr_cache *entry, uintptr_t addr)
925 {
926         struct priv *priv = dev->data->dev_private;
927         struct mlx4_mr_btree *bt = &mr_ctrl->cache_bh;
928         uint16_t idx;
929         uint32_t lkey;
930
931         /* If local cache table is full, try to double it. */
932         if (unlikely(bt->len == bt->size))
933                 mr_btree_expand(bt, bt->size << 1);
934         /* Look up in the global cache. */
935         rte_rwlock_read_lock(&priv->mr.rwlock);
936         lkey = mr_btree_lookup(&priv->mr.cache, &idx, addr);
937         if (lkey != UINT32_MAX) {
938                 /* Found. */
939                 *entry = (*priv->mr.cache.table)[idx];
940                 rte_rwlock_read_unlock(&priv->mr.rwlock);
941                 /*
942                  * Update local cache. Even if it fails, return the found entry
943                  * to update top-half cache. Next time, this entry will be found
944                  * in the global cache.
945                  */
946                 mr_btree_insert(bt, entry);
947                 return lkey;
948         }
949         rte_rwlock_read_unlock(&priv->mr.rwlock);
950         /* First time to see the address? Create a new MR. */
951         lkey = mlx4_mr_create(dev, entry, addr);
952         /*
953          * Update the local cache if successfully created a new global MR. Even
954          * if failed to create one, there's no action to take in this datapath
955          * code. As returning LKey is invalid, this will eventually make HW
956          * fail.
957          */
958         if (lkey != UINT32_MAX)
959                 mr_btree_insert(bt, entry);
960         return lkey;
961 }
962
963 /**
964  * Bottom-half of LKey search on datapath. Firstly search in cache_bh[] and if
965  * misses, search in the global MR cache table and update the new entry to
966  * per-queue local caches.
967  *
968  * @param dev
969  *   Pointer to Ethernet device.
970  * @param mr_ctrl
971  *   Pointer to per-queue MR control structure.
972  * @param addr
973  *   Search key.
974  *
975  * @return
976  *   Searched LKey on success, UINT32_MAX on no match.
977  */
978 static uint32_t
979 mlx4_mr_addr2mr_bh(struct rte_eth_dev *dev, struct mlx4_mr_ctrl *mr_ctrl,
980                    uintptr_t addr)
981 {
982         uint32_t lkey;
983         uint16_t bh_idx = 0;
984         /* Victim in top-half cache to replace with new entry. */
985         struct mlx4_mr_cache *repl = &mr_ctrl->cache[mr_ctrl->head];
986
987         /* Binary-search MR translation table. */
988         lkey = mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr);
989         /* Update top-half cache. */
990         if (likely(lkey != UINT32_MAX)) {
991                 *repl = (*mr_ctrl->cache_bh.table)[bh_idx];
992         } else {
993                 /*
994                  * If missed in local lookup table, search in the global cache
995                  * and local cache_bh[] will be updated inside if possible.
996                  * Top-half cache entry will also be updated.
997                  */
998                 lkey = mlx4_mr_lookup_dev(dev, mr_ctrl, repl, addr);
999                 if (unlikely(lkey == UINT32_MAX))
1000                         return UINT32_MAX;
1001         }
1002         /* Update the most recently used entry. */
1003         mr_ctrl->mru = mr_ctrl->head;
1004         /* Point to the next victim, the oldest. */
1005         mr_ctrl->head = (mr_ctrl->head + 1) % MLX4_MR_CACHE_N;
1006         return lkey;
1007 }
1008
1009 /**
1010  * Bottom-half of LKey search on Rx.
1011  *
1012  * @param rxq
1013  *   Pointer to Rx queue structure.
1014  * @param addr
1015  *   Search key.
1016  *
1017  * @return
1018  *   Searched LKey on success, UINT32_MAX on no match.
1019  */
1020 uint32_t
1021 mlx4_rx_addr2mr_bh(struct rxq *rxq, uintptr_t addr)
1022 {
1023         struct mlx4_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
1024         struct priv *priv = rxq->priv;
1025
1026         DEBUG("Rx queue %u: miss on top-half, mru=%u, head=%u, addr=%p",
1027               rxq->stats.idx, mr_ctrl->mru, mr_ctrl->head, (void *)addr);
1028         return mlx4_mr_addr2mr_bh(priv->dev, mr_ctrl, addr);
1029 }
1030
1031 /**
1032  * Bottom-half of LKey search on Tx.
1033  *
1034  * @param txq
1035  *   Pointer to Tx queue structure.
1036  * @param addr
1037  *   Search key.
1038  *
1039  * @return
1040  *   Searched LKey on success, UINT32_MAX on no match.
1041  */
1042 uint32_t
1043 mlx4_tx_addr2mr_bh(struct txq *txq, uintptr_t addr)
1044 {
1045         struct mlx4_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
1046         struct priv *priv = txq->priv;
1047
1048         DEBUG("Tx queue %u: miss on top-half, mru=%u, head=%u, addr=%p",
1049               txq->stats.idx, mr_ctrl->mru, mr_ctrl->head, (void *)addr);
1050         return mlx4_mr_addr2mr_bh(priv->dev, mr_ctrl, addr);
1051 }
1052
1053 /**
1054  * Flush all of the local cache entries.
1055  *
1056  * @param mr_ctrl
1057  *   Pointer to per-queue MR control structure.
1058  */
1059 void
1060 mlx4_mr_flush_local_cache(struct mlx4_mr_ctrl *mr_ctrl)
1061 {
1062         /* Reset the most-recently-used index. */
1063         mr_ctrl->mru = 0;
1064         /* Reset the linear search array. */
1065         mr_ctrl->head = 0;
1066         memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache));
1067         /* Reset the B-tree table. */
1068         mr_ctrl->cache_bh.len = 1;
1069         mr_ctrl->cache_bh.overflow = 0;
1070         /* Update the generation number. */
1071         mr_ctrl->cur_gen = *mr_ctrl->dev_gen_ptr;
1072         DEBUG("mr_ctrl(%p): flushed, cur_gen=%d",
1073               (void *)mr_ctrl, mr_ctrl->cur_gen);
1074 }
1075
1076 /**
1077  * Called during rte_mempool_mem_iter() by mlx4_mr_update_ext_mp().
1078  *
1079  * Externally allocated chunk is registered and a MR is created for the chunk.
1080  * The MR object is added to the global list. If memseg list of a MR object
1081  * (mr->msl) is null, the MR object can be regarded as externally allocated
1082  * memory.
1083  *
1084  * Once external memory is registered, it should be static. If the memory is
1085  * freed and the virtual address range has different physical memory mapped
1086  * again, it may cause crash on device due to the wrong translation entry. PMD
1087  * can't track the free event of the external memory for now.
1088  */
1089 static void
1090 mlx4_mr_update_ext_mp_cb(struct rte_mempool *mp, void *opaque,
1091                          struct rte_mempool_memhdr *memhdr,
1092                          unsigned mem_idx __rte_unused)
1093 {
1094         struct mr_update_mp_data *data = opaque;
1095         struct rte_eth_dev *dev = data->dev;
1096         struct priv *priv = dev->data->dev_private;
1097         struct mlx4_mr_ctrl *mr_ctrl = data->mr_ctrl;
1098         struct mlx4_mr *mr = NULL;
1099         uintptr_t addr = (uintptr_t)memhdr->addr;
1100         size_t len = memhdr->len;
1101         struct mlx4_mr_cache entry;
1102         uint32_t lkey;
1103
1104         /* If already registered, it should return. */
1105         rte_rwlock_read_lock(&priv->mr.rwlock);
1106         lkey = mr_lookup_dev(dev, &entry, addr);
1107         rte_rwlock_read_unlock(&priv->mr.rwlock);
1108         if (lkey != UINT32_MAX)
1109                 return;
1110         mr = rte_zmalloc_socket(NULL,
1111                                 RTE_ALIGN_CEIL(sizeof(*mr),
1112                                                RTE_CACHE_LINE_SIZE),
1113                                 RTE_CACHE_LINE_SIZE, mp->socket_id);
1114         if (mr == NULL) {
1115                 WARN("port %u unable to allocate memory for a new MR of"
1116                      " mempool (%s).",
1117                      dev->data->port_id, mp->name);
1118                 data->ret = -1;
1119                 return;
1120         }
1121         DEBUG("port %u register MR for chunk #%d of mempool (%s)",
1122               dev->data->port_id, mem_idx, mp->name);
1123         mr->ibv_mr = mlx4_glue->reg_mr(priv->pd, (void *)addr, len,
1124                                        IBV_ACCESS_LOCAL_WRITE);
1125         if (mr->ibv_mr == NULL) {
1126                 WARN("port %u fail to create a verbs MR for address (%p)",
1127                      dev->data->port_id, (void *)addr);
1128                 rte_free(mr);
1129                 data->ret = -1;
1130                 return;
1131         }
1132         mr->msl = NULL; /* Mark it is external memory. */
1133         mr->ms_bmp = NULL;
1134         mr->ms_n = 1;
1135         mr->ms_bmp_n = 1;
1136         rte_rwlock_write_lock(&priv->mr.rwlock);
1137         LIST_INSERT_HEAD(&priv->mr.mr_list, mr, mr);
1138         DEBUG("port %u MR CREATED (%p) for external memory %p:\n"
1139               "  [0x%" PRIxPTR ", 0x%" PRIxPTR "),"
1140               " lkey=0x%x base_idx=%u ms_n=%u, ms_bmp_n=%u",
1141               dev->data->port_id, (void *)mr, (void *)addr,
1142               addr, addr + len, rte_cpu_to_be_32(mr->ibv_mr->lkey),
1143               mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n);
1144         /* Insert to the global cache table. */
1145         mr_insert_dev_cache(dev, mr);
1146         rte_rwlock_write_unlock(&priv->mr.rwlock);
1147         /* Insert to the local cache table */
1148         mlx4_mr_addr2mr_bh(dev, mr_ctrl, addr);
1149 }
1150
1151 /**
1152  * Register MR for entire memory chunks in a Mempool having externally allocated
1153  * memory and fill in local cache.
1154  *
1155  * @param dev
1156  *   Pointer to Ethernet device.
1157  * @param mr_ctrl
1158  *   Pointer to per-queue MR control structure.
1159  * @param mp
1160  *   Pointer to registering Mempool.
1161  *
1162  * @return
1163  *   0 on success, -1 on failure.
1164  */
1165 static uint32_t
1166 mlx4_mr_update_ext_mp(struct rte_eth_dev *dev, struct mlx4_mr_ctrl *mr_ctrl,
1167                       struct rte_mempool *mp)
1168 {
1169         struct mr_update_mp_data data = {
1170                 .dev = dev,
1171                 .mr_ctrl = mr_ctrl,
1172                 .ret = 0,
1173         };
1174
1175         rte_mempool_mem_iter(mp, mlx4_mr_update_ext_mp_cb, &data);
1176         return data.ret;
1177 }
1178
1179 /**
1180  * Register MR entire memory chunks in a Mempool having externally allocated
1181  * memory and search LKey of the address to return.
1182  *
1183  * @param dev
1184  *   Pointer to Ethernet device.
1185  * @param addr
1186  *   Search key.
1187  * @param mp
1188  *   Pointer to registering Mempool where addr belongs.
1189  *
1190  * @return
1191  *   LKey for address on success, UINT32_MAX on failure.
1192  */
1193 uint32_t
1194 mlx4_tx_update_ext_mp(struct txq *txq, uintptr_t addr, struct rte_mempool *mp)
1195 {
1196         struct mlx4_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
1197         struct priv *priv = txq->priv;
1198
1199         mlx4_mr_update_ext_mp(priv->dev, mr_ctrl, mp);
1200         return mlx4_tx_addr2mr_bh(txq, addr);
1201 }
1202
1203 /* Called during rte_mempool_mem_iter() by mlx4_mr_update_mp(). */
1204 static void
1205 mlx4_mr_update_mp_cb(struct rte_mempool *mp __rte_unused, void *opaque,
1206                      struct rte_mempool_memhdr *memhdr,
1207                      unsigned mem_idx __rte_unused)
1208 {
1209         struct mr_update_mp_data *data = opaque;
1210         uint32_t lkey;
1211
1212         /* Stop iteration if failed in the previous walk. */
1213         if (data->ret < 0)
1214                 return;
1215         /* Register address of the chunk and update local caches. */
1216         lkey = mlx4_mr_addr2mr_bh(data->dev, data->mr_ctrl,
1217                                   (uintptr_t)memhdr->addr);
1218         if (lkey == UINT32_MAX)
1219                 data->ret = -1;
1220 }
1221
1222 /**
1223  * Register entire memory chunks in a Mempool.
1224  *
1225  * @param dev
1226  *   Pointer to Ethernet device.
1227  * @param mr_ctrl
1228  *   Pointer to per-queue MR control structure.
1229  * @param mp
1230  *   Pointer to registering Mempool.
1231  *
1232  * @return
1233  *   0 on success, -1 on failure.
1234  */
1235 int
1236 mlx4_mr_update_mp(struct rte_eth_dev *dev, struct mlx4_mr_ctrl *mr_ctrl,
1237                   struct rte_mempool *mp)
1238 {
1239         struct mr_update_mp_data data = {
1240                 .dev = dev,
1241                 .mr_ctrl = mr_ctrl,
1242                 .ret = 0,
1243         };
1244
1245         rte_mempool_mem_iter(mp, mlx4_mr_update_mp_cb, &data);
1246         if (data.ret < 0 && rte_errno == ENXIO) {
1247                 /* Mempool may have externally allocated memory. */
1248                 return mlx4_mr_update_ext_mp(dev, mr_ctrl, mp);
1249         }
1250         return data.ret;
1251 }
1252
1253 #ifndef NDEBUG
1254 /**
1255  * Dump all the created MRs and the global cache entries.
1256  *
1257  * @param dev
1258  *   Pointer to Ethernet device.
1259  */
1260 void
1261 mlx4_mr_dump_dev(struct rte_eth_dev *dev)
1262 {
1263         struct priv *priv = dev->data->dev_private;
1264         struct mlx4_mr *mr;
1265         int mr_n = 0;
1266         int chunk_n = 0;
1267
1268         rte_rwlock_read_lock(&priv->mr.rwlock);
1269         /* Iterate all the existing MRs. */
1270         LIST_FOREACH(mr, &priv->mr.mr_list, mr) {
1271                 unsigned int n;
1272
1273                 DEBUG("port %u MR[%u], LKey = 0x%x, ms_n = %u, ms_bmp_n = %u",
1274                       dev->data->port_id, mr_n++,
1275                       rte_cpu_to_be_32(mr->ibv_mr->lkey),
1276                       mr->ms_n, mr->ms_bmp_n);
1277                 if (mr->ms_n == 0)
1278                         continue;
1279                 for (n = 0; n < mr->ms_bmp_n; ) {
1280                         struct mlx4_mr_cache ret = { 0, };
1281
1282                         n = mr_find_next_chunk(mr, &ret, n);
1283                         if (!ret.end)
1284                                 break;
1285                         DEBUG("  chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR ")",
1286                               chunk_n++, ret.start, ret.end);
1287                 }
1288         }
1289         DEBUG("port %u dumping global cache", dev->data->port_id);
1290         mlx4_mr_btree_dump(&priv->mr.cache);
1291         rte_rwlock_read_unlock(&priv->mr.rwlock);
1292 }
1293 #endif
1294
1295 /**
1296  * Release all the created MRs and resources. Remove device from memory callback
1297  * list.
1298  *
1299  * @param dev
1300  *   Pointer to Ethernet device.
1301  */
1302 void
1303 mlx4_mr_release(struct rte_eth_dev *dev)
1304 {
1305         struct priv *priv = dev->data->dev_private;
1306         struct mlx4_mr *mr_next = LIST_FIRST(&priv->mr.mr_list);
1307
1308         /* Remove from memory callback device list. */
1309         rte_rwlock_write_lock(&mlx4_mem_event_rwlock);
1310         LIST_REMOVE(priv, mem_event_cb);
1311         rte_rwlock_write_unlock(&mlx4_mem_event_rwlock);
1312 #ifndef NDEBUG
1313         mlx4_mr_dump_dev(dev);
1314 #endif
1315         rte_rwlock_write_lock(&priv->mr.rwlock);
1316         /* Detach from MR list and move to free list. */
1317         while (mr_next != NULL) {
1318                 struct mlx4_mr *mr = mr_next;
1319
1320                 mr_next = LIST_NEXT(mr, mr);
1321                 LIST_REMOVE(mr, mr);
1322                 LIST_INSERT_HEAD(&priv->mr.mr_free_list, mr, mr);
1323         }
1324         LIST_INIT(&priv->mr.mr_list);
1325         /* Free global cache. */
1326         mlx4_mr_btree_free(&priv->mr.cache);
1327         rte_rwlock_write_unlock(&priv->mr.rwlock);
1328         /* Free all remaining MRs. */
1329         mlx4_mr_garbage_collect(dev);
1330 }