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