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