net/mlx5: add indexed pool local cache
[dpdk.git] / drivers / net / mlx5 / mlx5_utils.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4
5 #include <rte_malloc.h>
6
7 #include <mlx5_malloc.h>
8
9 #include "mlx5_utils.h"
10
11
12 /********************* Cache list ************************/
13
14 static struct mlx5_cache_entry *
15 mlx5_clist_default_create_cb(struct mlx5_cache_list *list,
16                              struct mlx5_cache_entry *entry __rte_unused,
17                              void *ctx __rte_unused)
18 {
19         return mlx5_malloc(MLX5_MEM_ZERO, list->entry_sz, 0, SOCKET_ID_ANY);
20 }
21
22 static void
23 mlx5_clist_default_remove_cb(struct mlx5_cache_list *list __rte_unused,
24                              struct mlx5_cache_entry *entry)
25 {
26         mlx5_free(entry);
27 }
28
29 int
30 mlx5_cache_list_init(struct mlx5_cache_list *list, const char *name,
31                      uint32_t entry_size, void *ctx,
32                      mlx5_cache_create_cb cb_create,
33                      mlx5_cache_match_cb cb_match,
34                      mlx5_cache_remove_cb cb_remove)
35 {
36         MLX5_ASSERT(list);
37         if (!cb_match || (!cb_create ^ !cb_remove))
38                 return -1;
39         if (name)
40                 snprintf(list->name, sizeof(list->name), "%s", name);
41         list->entry_sz = entry_size;
42         list->ctx = ctx;
43         list->cb_create = cb_create ? cb_create : mlx5_clist_default_create_cb;
44         list->cb_match = cb_match;
45         list->cb_remove = cb_remove ? cb_remove : mlx5_clist_default_remove_cb;
46         rte_rwlock_init(&list->lock);
47         DRV_LOG(DEBUG, "Cache list %s initialized.", list->name);
48         LIST_INIT(&list->head);
49         return 0;
50 }
51
52 static struct mlx5_cache_entry *
53 __cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
54 {
55         struct mlx5_cache_entry *entry;
56
57         LIST_FOREACH(entry, &list->head, next) {
58                 if (list->cb_match(list, entry, ctx))
59                         continue;
60                 if (reuse) {
61                         __atomic_add_fetch(&entry->ref_cnt, 1,
62                                            __ATOMIC_RELAXED);
63                         DRV_LOG(DEBUG, "Cache list %s entry %p ref++: %u.",
64                                 list->name, (void *)entry, entry->ref_cnt);
65                 }
66                 break;
67         }
68         return entry;
69 }
70
71 static struct mlx5_cache_entry *
72 cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
73 {
74         struct mlx5_cache_entry *entry;
75
76         rte_rwlock_read_lock(&list->lock);
77         entry = __cache_lookup(list, ctx, reuse);
78         rte_rwlock_read_unlock(&list->lock);
79         return entry;
80 }
81
82 struct mlx5_cache_entry *
83 mlx5_cache_lookup(struct mlx5_cache_list *list, void *ctx)
84 {
85         return cache_lookup(list, ctx, false);
86 }
87
88 struct mlx5_cache_entry *
89 mlx5_cache_register(struct mlx5_cache_list *list, void *ctx)
90 {
91         struct mlx5_cache_entry *entry;
92         uint32_t prev_gen_cnt = 0;
93
94         MLX5_ASSERT(list);
95         prev_gen_cnt = __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE);
96         /* Lookup with read lock, reuse if found. */
97         entry = cache_lookup(list, ctx, true);
98         if (entry)
99                 return entry;
100         /* Not found, append with write lock - block read from other threads. */
101         rte_rwlock_write_lock(&list->lock);
102         /* If list changed by other threads before lock, search again. */
103         if (prev_gen_cnt != __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE)) {
104                 /* Lookup and reuse w/o read lock. */
105                 entry = __cache_lookup(list, ctx, true);
106                 if (entry)
107                         goto done;
108         }
109         entry = list->cb_create(list, entry, ctx);
110         if (!entry) {
111                 DRV_LOG(ERR, "Failed to init cache list %s entry %p.",
112                         list->name, (void *)entry);
113                 goto done;
114         }
115         entry->ref_cnt = 1;
116         LIST_INSERT_HEAD(&list->head, entry, next);
117         __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_RELEASE);
118         __atomic_add_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
119         DRV_LOG(DEBUG, "Cache list %s entry %p new: %u.",
120                 list->name, (void *)entry, entry->ref_cnt);
121 done:
122         rte_rwlock_write_unlock(&list->lock);
123         return entry;
124 }
125
126 int
127 mlx5_cache_unregister(struct mlx5_cache_list *list,
128                       struct mlx5_cache_entry *entry)
129 {
130         rte_rwlock_write_lock(&list->lock);
131         MLX5_ASSERT(entry && entry->next.le_prev);
132         DRV_LOG(DEBUG, "Cache list %s entry %p ref--: %u.",
133                 list->name, (void *)entry, entry->ref_cnt);
134         if (--entry->ref_cnt) {
135                 rte_rwlock_write_unlock(&list->lock);
136                 return 1;
137         }
138         __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_ACQUIRE);
139         __atomic_sub_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
140         LIST_REMOVE(entry, next);
141         list->cb_remove(list, entry);
142         rte_rwlock_write_unlock(&list->lock);
143         DRV_LOG(DEBUG, "Cache list %s entry %p removed.",
144                 list->name, (void *)entry);
145         return 0;
146 }
147
148 void
149 mlx5_cache_list_destroy(struct mlx5_cache_list *list)
150 {
151         struct mlx5_cache_entry *entry;
152
153         MLX5_ASSERT(list);
154         /* no LIST_FOREACH_SAFE, using while instead */
155         while (!LIST_EMPTY(&list->head)) {
156                 entry = LIST_FIRST(&list->head);
157                 LIST_REMOVE(entry, next);
158                 list->cb_remove(list, entry);
159                 DRV_LOG(DEBUG, "Cache list %s entry %p destroyed.",
160                         list->name, (void *)entry);
161         }
162         memset(list, 0, sizeof(*list));
163 }
164
165 uint32_t
166 mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list)
167 {
168         MLX5_ASSERT(list);
169         return __atomic_load_n(&list->count, __ATOMIC_RELAXED);
170 }
171
172 /********************* Indexed pool **********************/
173
174 static inline void
175 mlx5_ipool_lock(struct mlx5_indexed_pool *pool)
176 {
177         if (pool->cfg.need_lock)
178                 rte_spinlock_lock(&pool->rsz_lock);
179 }
180
181 static inline void
182 mlx5_ipool_unlock(struct mlx5_indexed_pool *pool)
183 {
184         if (pool->cfg.need_lock)
185                 rte_spinlock_unlock(&pool->rsz_lock);
186 }
187
188 static inline uint32_t
189 mlx5_trunk_idx_get(struct mlx5_indexed_pool *pool, uint32_t entry_idx)
190 {
191         struct mlx5_indexed_pool_config *cfg = &pool->cfg;
192         uint32_t trunk_idx = 0;
193         uint32_t i;
194
195         if (!cfg->grow_trunk)
196                 return entry_idx / cfg->trunk_size;
197         if (entry_idx >= pool->grow_tbl[cfg->grow_trunk - 1]) {
198                 trunk_idx = (entry_idx - pool->grow_tbl[cfg->grow_trunk - 1]) /
199                             (cfg->trunk_size << (cfg->grow_shift *
200                             cfg->grow_trunk)) + cfg->grow_trunk;
201         } else {
202                 for (i = 0; i < cfg->grow_trunk; i++) {
203                         if (entry_idx < pool->grow_tbl[i])
204                                 break;
205                 }
206                 trunk_idx = i;
207         }
208         return trunk_idx;
209 }
210
211 static inline uint32_t
212 mlx5_trunk_size_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
213 {
214         struct mlx5_indexed_pool_config *cfg = &pool->cfg;
215
216         return cfg->trunk_size << (cfg->grow_shift *
217                (trunk_idx > cfg->grow_trunk ? cfg->grow_trunk : trunk_idx));
218 }
219
220 static inline uint32_t
221 mlx5_trunk_idx_offset_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
222 {
223         struct mlx5_indexed_pool_config *cfg = &pool->cfg;
224         uint32_t offset = 0;
225
226         if (!trunk_idx)
227                 return 0;
228         if (!cfg->grow_trunk)
229                 return cfg->trunk_size * trunk_idx;
230         if (trunk_idx < cfg->grow_trunk)
231                 offset = pool->grow_tbl[trunk_idx - 1];
232         else
233                 offset = pool->grow_tbl[cfg->grow_trunk - 1] +
234                          (cfg->trunk_size << (cfg->grow_shift *
235                          cfg->grow_trunk)) * (trunk_idx - cfg->grow_trunk);
236         return offset;
237 }
238
239 struct mlx5_indexed_pool *
240 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
241 {
242         struct mlx5_indexed_pool *pool;
243         uint32_t i;
244
245         if (!cfg || (!cfg->malloc ^ !cfg->free) ||
246             (cfg->per_core_cache && cfg->release_mem_en) ||
247             (cfg->trunk_size && ((cfg->trunk_size & (cfg->trunk_size - 1)) ||
248             ((__builtin_ffs(cfg->trunk_size) + TRUNK_IDX_BITS) > 32))))
249                 return NULL;
250         pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool) + cfg->grow_trunk *
251                            sizeof(pool->grow_tbl[0]), RTE_CACHE_LINE_SIZE,
252                            SOCKET_ID_ANY);
253         if (!pool)
254                 return NULL;
255         pool->cfg = *cfg;
256         if (!pool->cfg.trunk_size)
257                 pool->cfg.trunk_size = MLX5_IPOOL_DEFAULT_TRUNK_SIZE;
258         if (!cfg->malloc && !cfg->free) {
259                 pool->cfg.malloc = mlx5_malloc;
260                 pool->cfg.free = mlx5_free;
261         }
262         if (pool->cfg.need_lock)
263                 rte_spinlock_init(&pool->rsz_lock);
264         /*
265          * Initialize the dynamic grow trunk size lookup table to have a quick
266          * lookup for the trunk entry index offset.
267          */
268         for (i = 0; i < cfg->grow_trunk; i++) {
269                 pool->grow_tbl[i] = cfg->trunk_size << (cfg->grow_shift * i);
270                 if (i > 0)
271                         pool->grow_tbl[i] += pool->grow_tbl[i - 1];
272         }
273         if (!pool->cfg.max_idx)
274                 pool->cfg.max_idx =
275                         mlx5_trunk_idx_offset_get(pool, TRUNK_MAX_IDX + 1);
276         if (!cfg->per_core_cache)
277                 pool->free_list = TRUNK_INVALID;
278         return pool;
279 }
280
281 static int
282 mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
283 {
284         struct mlx5_indexed_trunk *trunk;
285         struct mlx5_indexed_trunk **trunk_tmp;
286         struct mlx5_indexed_trunk **p;
287         size_t trunk_size = 0;
288         size_t data_size;
289         size_t bmp_size;
290         uint32_t idx, cur_max_idx, i;
291
292         cur_max_idx = mlx5_trunk_idx_offset_get(pool, pool->n_trunk_valid);
293         if (pool->n_trunk_valid == TRUNK_MAX_IDX ||
294             cur_max_idx >= pool->cfg.max_idx)
295                 return -ENOMEM;
296         if (pool->n_trunk_valid == pool->n_trunk) {
297                 /* No free trunk flags, expand trunk list. */
298                 int n_grow = pool->n_trunk_valid ? pool->n_trunk :
299                              RTE_CACHE_LINE_SIZE / sizeof(void *);
300
301                 p = pool->cfg.malloc(0, (pool->n_trunk_valid + n_grow) *
302                                      sizeof(struct mlx5_indexed_trunk *),
303                                      RTE_CACHE_LINE_SIZE, rte_socket_id());
304                 if (!p)
305                         return -ENOMEM;
306                 if (pool->trunks)
307                         memcpy(p, pool->trunks, pool->n_trunk_valid *
308                                sizeof(struct mlx5_indexed_trunk *));
309                 memset(RTE_PTR_ADD(p, pool->n_trunk_valid * sizeof(void *)), 0,
310                        n_grow * sizeof(void *));
311                 trunk_tmp = pool->trunks;
312                 pool->trunks = p;
313                 if (trunk_tmp)
314                         pool->cfg.free(trunk_tmp);
315                 pool->n_trunk += n_grow;
316         }
317         if (!pool->cfg.release_mem_en) {
318                 idx = pool->n_trunk_valid;
319         } else {
320                 /* Find the first available slot in trunk list */
321                 for (idx = 0; idx < pool->n_trunk; idx++)
322                         if (pool->trunks[idx] == NULL)
323                                 break;
324         }
325         trunk_size += sizeof(*trunk);
326         data_size = mlx5_trunk_size_get(pool, idx);
327         bmp_size = rte_bitmap_get_memory_footprint(data_size);
328         /* rte_bitmap requires memory cacheline aligned. */
329         trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
330         trunk_size += bmp_size;
331         trunk = pool->cfg.malloc(0, trunk_size,
332                                  RTE_CACHE_LINE_SIZE, rte_socket_id());
333         if (!trunk)
334                 return -ENOMEM;
335         pool->trunks[idx] = trunk;
336         trunk->idx = idx;
337         trunk->free = data_size;
338         trunk->prev = TRUNK_INVALID;
339         trunk->next = TRUNK_INVALID;
340         MLX5_ASSERT(pool->free_list == TRUNK_INVALID);
341         pool->free_list = idx;
342         /* Mark all entries as available. */
343         trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
344                      [RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
345                      bmp_size);
346         /* Clear the overhead bits in the trunk if it happens. */
347         if (cur_max_idx + data_size > pool->cfg.max_idx) {
348                 for (i = pool->cfg.max_idx - cur_max_idx; i < data_size; i++)
349                         rte_bitmap_clear(trunk->bmp, i);
350         }
351         MLX5_ASSERT(trunk->bmp);
352         pool->n_trunk_valid++;
353 #ifdef POOL_DEBUG
354         pool->trunk_new++;
355         pool->trunk_avail++;
356 #endif
357         return 0;
358 }
359
360 static inline struct mlx5_indexed_cache *
361 mlx5_ipool_update_global_cache(struct mlx5_indexed_pool *pool, int cidx)
362 {
363         struct mlx5_indexed_cache *gc, *lc, *olc = NULL;
364
365         lc = pool->cache[cidx]->lc;
366         gc = __atomic_load_n(&pool->gc, __ATOMIC_RELAXED);
367         if (gc && lc != gc) {
368                 mlx5_ipool_lock(pool);
369                 if (lc && !(--lc->ref_cnt))
370                         olc = lc;
371                 lc = pool->gc;
372                 lc->ref_cnt++;
373                 pool->cache[cidx]->lc = lc;
374                 mlx5_ipool_unlock(pool);
375                 if (olc)
376                         pool->cfg.free(olc);
377         }
378         return lc;
379 }
380
381 static uint32_t
382 mlx5_ipool_allocate_from_global(struct mlx5_indexed_pool *pool, int cidx)
383 {
384         struct mlx5_indexed_trunk *trunk;
385         struct mlx5_indexed_cache *p, *lc, *olc = NULL;
386         size_t trunk_size = 0;
387         size_t data_size;
388         uint32_t cur_max_idx, trunk_idx, trunk_n;
389         uint32_t fetch_size, ts_idx, i;
390         int n_grow;
391
392 check_again:
393         p = NULL;
394         fetch_size = 0;
395         /*
396          * Fetch new index from global if possible. First round local
397          * cache will be NULL.
398          */
399         lc = pool->cache[cidx]->lc;
400         mlx5_ipool_lock(pool);
401         /* Try to update local cache first. */
402         if (likely(pool->gc)) {
403                 if (lc != pool->gc) {
404                         if (lc && !(--lc->ref_cnt))
405                                 olc = lc;
406                         lc = pool->gc;
407                         lc->ref_cnt++;
408                         pool->cache[cidx]->lc = lc;
409                 }
410                 if (lc->len) {
411                         /* Use the updated local cache to fetch index. */
412                         fetch_size = pool->cfg.per_core_cache >> 2;
413                         if (lc->len < fetch_size)
414                                 fetch_size = lc->len;
415                         lc->len -= fetch_size;
416                         memcpy(pool->cache[cidx]->idx, &lc->idx[lc->len],
417                                sizeof(uint32_t) * fetch_size);
418                 }
419         }
420         mlx5_ipool_unlock(pool);
421         if (unlikely(olc)) {
422                 pool->cfg.free(olc);
423                 olc = NULL;
424         }
425         if (fetch_size) {
426                 pool->cache[cidx]->len = fetch_size - 1;
427                 return pool->cache[cidx]->idx[pool->cache[cidx]->len];
428         }
429         trunk_idx = lc ? __atomic_load_n(&lc->n_trunk_valid,
430                          __ATOMIC_ACQUIRE) : 0;
431         trunk_n = lc ? lc->n_trunk : 0;
432         cur_max_idx = mlx5_trunk_idx_offset_get(pool, trunk_idx);
433         /* Check if index reach maximum. */
434         if (trunk_idx == TRUNK_MAX_IDX ||
435             cur_max_idx >= pool->cfg.max_idx)
436                 return 0;
437         /* No enough space in trunk array, resize the trunks array. */
438         if (trunk_idx == trunk_n) {
439                 n_grow = trunk_idx ? trunk_idx :
440                              RTE_CACHE_LINE_SIZE / sizeof(void *);
441                 cur_max_idx = mlx5_trunk_idx_offset_get(pool, trunk_n + n_grow);
442                 /* Resize the trunk array. */
443                 p = pool->cfg.malloc(0, ((trunk_idx + n_grow) *
444                         sizeof(struct mlx5_indexed_trunk *)) +
445                         (cur_max_idx * sizeof(uint32_t)) + sizeof(*p),
446                         RTE_CACHE_LINE_SIZE, rte_socket_id());
447                 if (!p)
448                         return 0;
449                 p->trunks = (struct mlx5_indexed_trunk **)&p->idx[cur_max_idx];
450                 if (lc)
451                         memcpy(p->trunks, lc->trunks, trunk_idx *
452                        sizeof(struct mlx5_indexed_trunk *));
453 #ifdef RTE_LIBRTE_MLX5_DEBUG
454                 memset(RTE_PTR_ADD(p->trunks, trunk_idx * sizeof(void *)), 0,
455                         n_grow * sizeof(void *));
456 #endif
457                 p->n_trunk_valid = trunk_idx;
458                 p->n_trunk = trunk_n + n_grow;
459                 p->len = 0;
460         }
461         /* Prepare the new trunk. */
462         trunk_size = sizeof(*trunk);
463         data_size = mlx5_trunk_size_get(pool, trunk_idx);
464         trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
465         trunk = pool->cfg.malloc(0, trunk_size,
466                                  RTE_CACHE_LINE_SIZE, rte_socket_id());
467         if (unlikely(!trunk)) {
468                 pool->cfg.free(p);
469                 return 0;
470         }
471         trunk->idx = trunk_idx;
472         trunk->free = data_size;
473         mlx5_ipool_lock(pool);
474         /*
475          * Double check if trunks has been updated or have available index.
476          * During the new trunk allocate, index may still be flushed to the
477          * global cache. So also need to check the pool->gc->len.
478          */
479         if (pool->gc && (lc != pool->gc ||
480             lc->n_trunk_valid != trunk_idx ||
481             pool->gc->len)) {
482                 mlx5_ipool_unlock(pool);
483                 if (p)
484                         pool->cfg.free(p);
485                 pool->cfg.free(trunk);
486                 goto check_again;
487         }
488         /* Resize the trunk array and update local cache first.  */
489         if (p) {
490                 if (lc && !(--lc->ref_cnt))
491                         olc = lc;
492                 lc = p;
493                 lc->ref_cnt = 1;
494                 pool->cache[cidx]->lc = lc;
495                 __atomic_store_n(&pool->gc, p, __ATOMIC_RELAXED);
496         }
497         /* Add trunk to trunks array. */
498         lc->trunks[trunk_idx] = trunk;
499         __atomic_fetch_add(&lc->n_trunk_valid, 1, __ATOMIC_RELAXED);
500         /* Enqueue half of the index to global. */
501         ts_idx = mlx5_trunk_idx_offset_get(pool, trunk_idx) + 1;
502         fetch_size = trunk->free >> 1;
503         for (i = 0; i < fetch_size; i++)
504                 lc->idx[i] = ts_idx + i;
505         lc->len = fetch_size;
506         mlx5_ipool_unlock(pool);
507         /* Copy left half - 1 to local cache index array. */
508         pool->cache[cidx]->len = trunk->free - fetch_size - 1;
509         ts_idx += fetch_size;
510         for (i = 0; i < pool->cache[cidx]->len; i++)
511                 pool->cache[cidx]->idx[i] = ts_idx + i;
512         if (olc)
513                 pool->cfg.free(olc);
514         return ts_idx + i;
515 }
516
517 static void *
518 mlx5_ipool_get_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
519 {
520         struct mlx5_indexed_trunk *trunk;
521         struct mlx5_indexed_cache *lc;
522         uint32_t trunk_idx;
523         uint32_t entry_idx;
524         int cidx;
525
526         MLX5_ASSERT(idx);
527         cidx = rte_lcore_index(rte_lcore_id());
528         if (unlikely(cidx == -1)) {
529                 rte_errno = ENOTSUP;
530                 return NULL;
531         }
532         lc = mlx5_ipool_update_global_cache(pool, cidx);
533         idx -= 1;
534         trunk_idx = mlx5_trunk_idx_get(pool, idx);
535         trunk = lc->trunks[trunk_idx];
536         MLX5_ASSERT(trunk);
537         entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk_idx);
538         return &trunk->data[entry_idx * pool->cfg.size];
539 }
540
541 static void *
542 mlx5_ipool_malloc_cache(struct mlx5_indexed_pool *pool, uint32_t *idx)
543 {
544         int cidx;
545
546         cidx = rte_lcore_index(rte_lcore_id());
547         if (unlikely(cidx == -1)) {
548                 rte_errno = ENOTSUP;
549                 return NULL;
550         }
551         if (unlikely(!pool->cache[cidx])) {
552                 pool->cache[cidx] = pool->cfg.malloc(MLX5_MEM_ZERO,
553                         sizeof(struct mlx5_ipool_per_lcore) +
554                         (pool->cfg.per_core_cache * sizeof(uint32_t)),
555                         RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
556                 if (!pool->cache[cidx]) {
557                         DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
558                         return NULL;
559                 }
560         } else if (pool->cache[cidx]->len) {
561                 pool->cache[cidx]->len--;
562                 *idx = pool->cache[cidx]->idx[pool->cache[cidx]->len];
563                 return mlx5_ipool_get_cache(pool, *idx);
564         }
565         /* Not enough idx in global cache. Keep fetching from global. */
566         *idx = mlx5_ipool_allocate_from_global(pool, cidx);
567         if (unlikely(!(*idx)))
568                 return NULL;
569         return mlx5_ipool_get_cache(pool, *idx);
570 }
571
572 static void
573 mlx5_ipool_free_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
574 {
575         int cidx;
576         struct mlx5_ipool_per_lcore *ilc;
577         struct mlx5_indexed_cache *gc, *olc = NULL;
578         uint32_t reclaim_num = 0;
579
580         MLX5_ASSERT(idx);
581         cidx = rte_lcore_index(rte_lcore_id());
582         if (unlikely(cidx == -1)) {
583                 rte_errno = ENOTSUP;
584                 return;
585         }
586         /*
587          * When index was allocated on core A but freed on core B. In this
588          * case check if local cache on core B was allocated before.
589          */
590         if (unlikely(!pool->cache[cidx])) {
591                 pool->cache[cidx] = pool->cfg.malloc(MLX5_MEM_ZERO,
592                         sizeof(struct mlx5_ipool_per_lcore) +
593                         (pool->cfg.per_core_cache * sizeof(uint32_t)),
594                         RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
595                 if (!pool->cache[cidx]) {
596                         DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
597                         return;
598                 }
599         }
600         /* Try to enqueue to local index cache. */
601         if (pool->cache[cidx]->len < pool->cfg.per_core_cache) {
602                 pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
603                 pool->cache[cidx]->len++;
604                 return;
605         }
606         ilc = pool->cache[cidx];
607         reclaim_num = pool->cfg.per_core_cache >> 2;
608         ilc->len -= reclaim_num;
609         /* Local index cache full, try with global index cache. */
610         mlx5_ipool_lock(pool);
611         gc = pool->gc;
612         if (ilc->lc != gc) {
613                 if (!(--ilc->lc->ref_cnt))
614                         olc = ilc->lc;
615                 gc->ref_cnt++;
616                 ilc->lc = gc;
617         }
618         memcpy(&gc->idx[gc->len], &ilc->idx[ilc->len],
619                reclaim_num * sizeof(uint32_t));
620         gc->len += reclaim_num;
621         mlx5_ipool_unlock(pool);
622         if (olc)
623                 pool->cfg.free(olc);
624         pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
625         pool->cache[cidx]->len++;
626 }
627
628 void *
629 mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
630 {
631         struct mlx5_indexed_trunk *trunk;
632         uint64_t slab = 0;
633         uint32_t iidx = 0;
634         void *p;
635
636         if (pool->cfg.per_core_cache)
637                 return mlx5_ipool_malloc_cache(pool, idx);
638         mlx5_ipool_lock(pool);
639         if (pool->free_list == TRUNK_INVALID) {
640                 /* If no available trunks, grow new. */
641                 if (mlx5_ipool_grow(pool)) {
642                         mlx5_ipool_unlock(pool);
643                         return NULL;
644                 }
645         }
646         MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
647         trunk = pool->trunks[pool->free_list];
648         MLX5_ASSERT(trunk->free);
649         if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
650                 mlx5_ipool_unlock(pool);
651                 return NULL;
652         }
653         MLX5_ASSERT(slab);
654         iidx += __builtin_ctzll(slab);
655         MLX5_ASSERT(iidx != UINT32_MAX);
656         MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
657         rte_bitmap_clear(trunk->bmp, iidx);
658         p = &trunk->data[iidx * pool->cfg.size];
659         /*
660          * The ipool index should grow continually from small to big,
661          * some features as metering only accept limited bits of index.
662          * Random index with MSB set may be rejected.
663          */
664         iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
665         iidx += 1; /* non-zero index. */
666         trunk->free--;
667 #ifdef POOL_DEBUG
668         pool->n_entry++;
669 #endif
670         if (!trunk->free) {
671                 /* Full trunk will be removed from free list in imalloc. */
672                 MLX5_ASSERT(pool->free_list == trunk->idx);
673                 pool->free_list = trunk->next;
674                 if (trunk->next != TRUNK_INVALID)
675                         pool->trunks[trunk->next]->prev = TRUNK_INVALID;
676                 trunk->prev = TRUNK_INVALID;
677                 trunk->next = TRUNK_INVALID;
678 #ifdef POOL_DEBUG
679                 pool->trunk_empty++;
680                 pool->trunk_avail--;
681 #endif
682         }
683         *idx = iidx;
684         mlx5_ipool_unlock(pool);
685         return p;
686 }
687
688 void *
689 mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
690 {
691         void *entry = mlx5_ipool_malloc(pool, idx);
692
693         if (entry && pool->cfg.size)
694                 memset(entry, 0, pool->cfg.size);
695         return entry;
696 }
697
698 void
699 mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
700 {
701         struct mlx5_indexed_trunk *trunk;
702         uint32_t trunk_idx;
703         uint32_t entry_idx;
704
705         if (!idx)
706                 return;
707         if (pool->cfg.per_core_cache) {
708                 mlx5_ipool_free_cache(pool, idx);
709                 return;
710         }
711         idx -= 1;
712         mlx5_ipool_lock(pool);
713         trunk_idx = mlx5_trunk_idx_get(pool, idx);
714         if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
715             (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
716                 goto out;
717         trunk = pool->trunks[trunk_idx];
718         if (!trunk)
719                 goto out;
720         entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
721         if (trunk_idx != trunk->idx ||
722             rte_bitmap_get(trunk->bmp, entry_idx))
723                 goto out;
724         rte_bitmap_set(trunk->bmp, entry_idx);
725         trunk->free++;
726         if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
727            (pool, trunk->idx)) {
728                 if (pool->free_list == trunk->idx)
729                         pool->free_list = trunk->next;
730                 if (trunk->next != TRUNK_INVALID)
731                         pool->trunks[trunk->next]->prev = trunk->prev;
732                 if (trunk->prev != TRUNK_INVALID)
733                         pool->trunks[trunk->prev]->next = trunk->next;
734                 pool->cfg.free(trunk);
735                 pool->trunks[trunk_idx] = NULL;
736                 pool->n_trunk_valid--;
737 #ifdef POOL_DEBUG
738                 pool->trunk_avail--;
739                 pool->trunk_free++;
740 #endif
741                 if (pool->n_trunk_valid == 0) {
742                         pool->cfg.free(pool->trunks);
743                         pool->trunks = NULL;
744                         pool->n_trunk = 0;
745                 }
746         } else if (trunk->free == 1) {
747                 /* Put into free trunk list head. */
748                 MLX5_ASSERT(pool->free_list != trunk->idx);
749                 trunk->next = pool->free_list;
750                 trunk->prev = TRUNK_INVALID;
751                 if (pool->free_list != TRUNK_INVALID)
752                         pool->trunks[pool->free_list]->prev = trunk->idx;
753                 pool->free_list = trunk->idx;
754 #ifdef POOL_DEBUG
755                 pool->trunk_empty--;
756                 pool->trunk_avail++;
757 #endif
758         }
759 #ifdef POOL_DEBUG
760         pool->n_entry--;
761 #endif
762 out:
763         mlx5_ipool_unlock(pool);
764 }
765
766 void *
767 mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
768 {
769         struct mlx5_indexed_trunk *trunk;
770         void *p = NULL;
771         uint32_t trunk_idx;
772         uint32_t entry_idx;
773
774         if (!idx)
775                 return NULL;
776         if (pool->cfg.per_core_cache)
777                 return mlx5_ipool_get_cache(pool, idx);
778         idx -= 1;
779         mlx5_ipool_lock(pool);
780         trunk_idx = mlx5_trunk_idx_get(pool, idx);
781         if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
782             (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
783                 goto out;
784         trunk = pool->trunks[trunk_idx];
785         if (!trunk)
786                 goto out;
787         entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
788         if (trunk_idx != trunk->idx ||
789             rte_bitmap_get(trunk->bmp, entry_idx))
790                 goto out;
791         p = &trunk->data[entry_idx * pool->cfg.size];
792 out:
793         mlx5_ipool_unlock(pool);
794         return p;
795 }
796
797 int
798 mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
799 {
800         struct mlx5_indexed_trunk **trunks = NULL;
801         struct mlx5_indexed_cache *gc = pool->gc;
802         uint32_t i, n_trunk_valid = 0;
803
804         MLX5_ASSERT(pool);
805         mlx5_ipool_lock(pool);
806         if (pool->cfg.per_core_cache) {
807                 for (i = 0; i < RTE_MAX_LCORE; i++) {
808                         /*
809                          * Free only old global cache. Pool gc will be
810                          * freed at last.
811                          */
812                         if (pool->cache[i]) {
813                                 if (pool->cache[i]->lc &&
814                                     pool->cache[i]->lc != pool->gc &&
815                                     (!(--pool->cache[i]->lc->ref_cnt)))
816                                         pool->cfg.free(pool->cache[i]->lc);
817                                 pool->cfg.free(pool->cache[i]);
818                         }
819                 }
820                 if (gc) {
821                         trunks = gc->trunks;
822                         n_trunk_valid = gc->n_trunk_valid;
823                 }
824         } else {
825                 gc = NULL;
826                 trunks = pool->trunks;
827                 n_trunk_valid = pool->n_trunk_valid;
828         }
829         for (i = 0; i < n_trunk_valid; i++) {
830                 if (trunks[i])
831                         pool->cfg.free(trunks[i]);
832         }
833         if (!gc && trunks)
834                 pool->cfg.free(trunks);
835         if (gc)
836                 pool->cfg.free(gc);
837         mlx5_ipool_unlock(pool);
838         mlx5_free(pool);
839         return 0;
840 }
841
842 void
843 mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
844 {
845         printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
846                "total: %d\n",
847                pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
848                pool->cfg.trunk_size, pool->n_trunk_valid);
849 #ifdef POOL_DEBUG
850         printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
851                "available %u free %u\n",
852                pool->cfg.type, pool->n_entry, pool->trunk_new,
853                pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
854 #endif
855 }
856
857 struct mlx5_l3t_tbl *
858 mlx5_l3t_create(enum mlx5_l3t_type type)
859 {
860         struct mlx5_l3t_tbl *tbl;
861         struct mlx5_indexed_pool_config l3t_ip_cfg = {
862                 .trunk_size = 16,
863                 .grow_trunk = 6,
864                 .grow_shift = 1,
865                 .need_lock = 0,
866                 .release_mem_en = 1,
867                 .malloc = mlx5_malloc,
868                 .free = mlx5_free,
869         };
870
871         if (type >= MLX5_L3T_TYPE_MAX) {
872                 rte_errno = EINVAL;
873                 return NULL;
874         }
875         tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
876                           SOCKET_ID_ANY);
877         if (!tbl) {
878                 rte_errno = ENOMEM;
879                 return NULL;
880         }
881         tbl->type = type;
882         switch (type) {
883         case MLX5_L3T_TYPE_WORD:
884                 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
885                 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
886                 break;
887         case MLX5_L3T_TYPE_DWORD:
888                 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
889                 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
890                 break;
891         case MLX5_L3T_TYPE_QWORD:
892                 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
893                 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
894                 break;
895         default:
896                 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
897                 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
898                 break;
899         }
900         rte_spinlock_init(&tbl->sl);
901         tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
902         if (!tbl->eip) {
903                 rte_errno = ENOMEM;
904                 mlx5_free(tbl);
905                 tbl = NULL;
906         }
907         return tbl;
908 }
909
910 void
911 mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
912 {
913         struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
914         uint32_t i, j;
915
916         if (!tbl)
917                 return;
918         g_tbl = tbl->tbl;
919         if (g_tbl) {
920                 for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
921                         m_tbl = g_tbl->tbl[i];
922                         if (!m_tbl)
923                                 continue;
924                         for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
925                                 if (!m_tbl->tbl[j])
926                                         continue;
927                                 MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
928                                             m_tbl->tbl[j])->ref_cnt);
929                                 mlx5_ipool_free(tbl->eip,
930                                                 ((struct mlx5_l3t_entry_word *)
931                                                 m_tbl->tbl[j])->idx);
932                                 m_tbl->tbl[j] = 0;
933                                 if (!(--m_tbl->ref_cnt))
934                                         break;
935                         }
936                         MLX5_ASSERT(!m_tbl->ref_cnt);
937                         mlx5_free(g_tbl->tbl[i]);
938                         g_tbl->tbl[i] = 0;
939                         if (!(--g_tbl->ref_cnt))
940                                 break;
941                 }
942                 MLX5_ASSERT(!g_tbl->ref_cnt);
943                 mlx5_free(tbl->tbl);
944                 tbl->tbl = 0;
945         }
946         mlx5_ipool_destroy(tbl->eip);
947         mlx5_free(tbl);
948 }
949
950 static int32_t
951 __l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
952                 union mlx5_l3t_data *data)
953 {
954         struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
955         struct mlx5_l3t_entry_word *w_e_tbl;
956         struct mlx5_l3t_entry_dword *dw_e_tbl;
957         struct mlx5_l3t_entry_qword *qw_e_tbl;
958         struct mlx5_l3t_entry_ptr *ptr_e_tbl;
959         void *e_tbl;
960         uint32_t entry_idx;
961
962         g_tbl = tbl->tbl;
963         if (!g_tbl)
964                 return -1;
965         m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
966         if (!m_tbl)
967                 return -1;
968         e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
969         if (!e_tbl)
970                 return -1;
971         entry_idx = idx & MLX5_L3T_ET_MASK;
972         switch (tbl->type) {
973         case MLX5_L3T_TYPE_WORD:
974                 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
975                 data->word = w_e_tbl->entry[entry_idx].data;
976                 if (w_e_tbl->entry[entry_idx].data)
977                         w_e_tbl->entry[entry_idx].ref_cnt++;
978                 break;
979         case MLX5_L3T_TYPE_DWORD:
980                 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
981                 data->dword = dw_e_tbl->entry[entry_idx].data;
982                 if (dw_e_tbl->entry[entry_idx].data)
983                         dw_e_tbl->entry[entry_idx].ref_cnt++;
984                 break;
985         case MLX5_L3T_TYPE_QWORD:
986                 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
987                 data->qword = qw_e_tbl->entry[entry_idx].data;
988                 if (qw_e_tbl->entry[entry_idx].data)
989                         qw_e_tbl->entry[entry_idx].ref_cnt++;
990                 break;
991         default:
992                 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
993                 data->ptr = ptr_e_tbl->entry[entry_idx].data;
994                 if (ptr_e_tbl->entry[entry_idx].data)
995                         ptr_e_tbl->entry[entry_idx].ref_cnt++;
996                 break;
997         }
998         return 0;
999 }
1000
1001 int32_t
1002 mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1003                    union mlx5_l3t_data *data)
1004 {
1005         int ret;
1006
1007         rte_spinlock_lock(&tbl->sl);
1008         ret = __l3t_get_entry(tbl, idx, data);
1009         rte_spinlock_unlock(&tbl->sl);
1010         return ret;
1011 }
1012
1013 int32_t
1014 mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
1015 {
1016         struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
1017         struct mlx5_l3t_entry_word *w_e_tbl;
1018         struct mlx5_l3t_entry_dword *dw_e_tbl;
1019         struct mlx5_l3t_entry_qword *qw_e_tbl;
1020         struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1021         void *e_tbl;
1022         uint32_t entry_idx;
1023         uint64_t ref_cnt;
1024         int32_t ret = -1;
1025
1026         rte_spinlock_lock(&tbl->sl);
1027         g_tbl = tbl->tbl;
1028         if (!g_tbl)
1029                 goto out;
1030         m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1031         if (!m_tbl)
1032                 goto out;
1033         e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1034         if (!e_tbl)
1035                 goto out;
1036         entry_idx = idx & MLX5_L3T_ET_MASK;
1037         switch (tbl->type) {
1038         case MLX5_L3T_TYPE_WORD:
1039                 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1040                 MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
1041                 ret = --w_e_tbl->entry[entry_idx].ref_cnt;
1042                 if (ret)
1043                         goto out;
1044                 w_e_tbl->entry[entry_idx].data = 0;
1045                 ref_cnt = --w_e_tbl->ref_cnt;
1046                 break;
1047         case MLX5_L3T_TYPE_DWORD:
1048                 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1049                 MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
1050                 ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
1051                 if (ret)
1052                         goto out;
1053                 dw_e_tbl->entry[entry_idx].data = 0;
1054                 ref_cnt = --dw_e_tbl->ref_cnt;
1055                 break;
1056         case MLX5_L3T_TYPE_QWORD:
1057                 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1058                 MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
1059                 ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
1060                 if (ret)
1061                         goto out;
1062                 qw_e_tbl->entry[entry_idx].data = 0;
1063                 ref_cnt = --qw_e_tbl->ref_cnt;
1064                 break;
1065         default:
1066                 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1067                 MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
1068                 ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
1069                 if (ret)
1070                         goto out;
1071                 ptr_e_tbl->entry[entry_idx].data = NULL;
1072                 ref_cnt = --ptr_e_tbl->ref_cnt;
1073                 break;
1074         }
1075         if (!ref_cnt) {
1076                 mlx5_ipool_free(tbl->eip,
1077                                 ((struct mlx5_l3t_entry_word *)e_tbl)->idx);
1078                 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1079                                                                         NULL;
1080                 if (!(--m_tbl->ref_cnt)) {
1081                         mlx5_free(m_tbl);
1082                         g_tbl->tbl
1083                         [(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
1084                         if (!(--g_tbl->ref_cnt)) {
1085                                 mlx5_free(g_tbl);
1086                                 tbl->tbl = 0;
1087                         }
1088                 }
1089         }
1090 out:
1091         rte_spinlock_unlock(&tbl->sl);
1092         return ret;
1093 }
1094
1095 static int32_t
1096 __l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1097                 union mlx5_l3t_data *data)
1098 {
1099         struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
1100         struct mlx5_l3t_entry_word *w_e_tbl;
1101         struct mlx5_l3t_entry_dword *dw_e_tbl;
1102         struct mlx5_l3t_entry_qword *qw_e_tbl;
1103         struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1104         void *e_tbl;
1105         uint32_t entry_idx, tbl_idx = 0;
1106
1107         /* Check the global table, create it if empty. */
1108         g_tbl = tbl->tbl;
1109         if (!g_tbl) {
1110                 g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1111                                     sizeof(struct mlx5_l3t_level_tbl) +
1112                                     sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
1113                                     SOCKET_ID_ANY);
1114                 if (!g_tbl) {
1115                         rte_errno = ENOMEM;
1116                         return -1;
1117                 }
1118                 tbl->tbl = g_tbl;
1119         }
1120         /*
1121          * Check the middle table, create it if empty. Ref_cnt will be
1122          * increased if new sub table created.
1123          */
1124         m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1125         if (!m_tbl) {
1126                 m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1127                                     sizeof(struct mlx5_l3t_level_tbl) +
1128                                     sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
1129                                     SOCKET_ID_ANY);
1130                 if (!m_tbl) {
1131                         rte_errno = ENOMEM;
1132                         return -1;
1133                 }
1134                 g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
1135                                                                         m_tbl;
1136                 g_tbl->ref_cnt++;
1137         }
1138         /*
1139          * Check the entry table, create it if empty. Ref_cnt will be
1140          * increased if new sub entry table created.
1141          */
1142         e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1143         if (!e_tbl) {
1144                 e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
1145                 if (!e_tbl) {
1146                         rte_errno = ENOMEM;
1147                         return -1;
1148                 }
1149                 ((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
1150                 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1151                                                                         e_tbl;
1152                 m_tbl->ref_cnt++;
1153         }
1154         entry_idx = idx & MLX5_L3T_ET_MASK;
1155         switch (tbl->type) {
1156         case MLX5_L3T_TYPE_WORD:
1157                 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1158                 if (w_e_tbl->entry[entry_idx].data) {
1159                         data->word = w_e_tbl->entry[entry_idx].data;
1160                         w_e_tbl->entry[entry_idx].ref_cnt++;
1161                         rte_errno = EEXIST;
1162                         return -1;
1163                 }
1164                 w_e_tbl->entry[entry_idx].data = data->word;
1165                 w_e_tbl->entry[entry_idx].ref_cnt = 1;
1166                 w_e_tbl->ref_cnt++;
1167                 break;
1168         case MLX5_L3T_TYPE_DWORD:
1169                 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1170                 if (dw_e_tbl->entry[entry_idx].data) {
1171                         data->dword = dw_e_tbl->entry[entry_idx].data;
1172                         dw_e_tbl->entry[entry_idx].ref_cnt++;
1173                         rte_errno = EEXIST;
1174                         return -1;
1175                 }
1176                 dw_e_tbl->entry[entry_idx].data = data->dword;
1177                 dw_e_tbl->entry[entry_idx].ref_cnt = 1;
1178                 dw_e_tbl->ref_cnt++;
1179                 break;
1180         case MLX5_L3T_TYPE_QWORD:
1181                 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1182                 if (qw_e_tbl->entry[entry_idx].data) {
1183                         data->qword = qw_e_tbl->entry[entry_idx].data;
1184                         qw_e_tbl->entry[entry_idx].ref_cnt++;
1185                         rte_errno = EEXIST;
1186                         return -1;
1187                 }
1188                 qw_e_tbl->entry[entry_idx].data = data->qword;
1189                 qw_e_tbl->entry[entry_idx].ref_cnt = 1;
1190                 qw_e_tbl->ref_cnt++;
1191                 break;
1192         default:
1193                 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1194                 if (ptr_e_tbl->entry[entry_idx].data) {
1195                         data->ptr = ptr_e_tbl->entry[entry_idx].data;
1196                         ptr_e_tbl->entry[entry_idx].ref_cnt++;
1197                         rte_errno = EEXIST;
1198                         return -1;
1199                 }
1200                 ptr_e_tbl->entry[entry_idx].data = data->ptr;
1201                 ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
1202                 ptr_e_tbl->ref_cnt++;
1203                 break;
1204         }
1205         return 0;
1206 }
1207
1208 int32_t
1209 mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1210                    union mlx5_l3t_data *data)
1211 {
1212         int ret;
1213
1214         rte_spinlock_lock(&tbl->sl);
1215         ret = __l3t_set_entry(tbl, idx, data);
1216         rte_spinlock_unlock(&tbl->sl);
1217         return ret;
1218 }
1219
1220 int32_t
1221 mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1222                        union mlx5_l3t_data *data,
1223                        mlx5_l3t_alloc_callback_fn cb, void *ctx)
1224 {
1225         int32_t ret;
1226
1227         rte_spinlock_lock(&tbl->sl);
1228         /* Check if entry data is ready. */
1229         ret = __l3t_get_entry(tbl, idx, data);
1230         if (!ret) {
1231                 switch (tbl->type) {
1232                 case MLX5_L3T_TYPE_WORD:
1233                         if (data->word)
1234                                 goto out;
1235                         break;
1236                 case MLX5_L3T_TYPE_DWORD:
1237                         if (data->dword)
1238                                 goto out;
1239                         break;
1240                 case MLX5_L3T_TYPE_QWORD:
1241                         if (data->qword)
1242                                 goto out;
1243                         break;
1244                 default:
1245                         if (data->ptr)
1246                                 goto out;
1247                         break;
1248                 }
1249         }
1250         /* Entry data is not ready, use user callback to create it. */
1251         ret = cb(ctx, data);
1252         if (ret)
1253                 goto out;
1254         /* Save the new allocated data to entry. */
1255         ret = __l3t_set_entry(tbl, idx, data);
1256 out:
1257         rte_spinlock_unlock(&tbl->sl);
1258         return ret;
1259 }