1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
5 #include <rte_malloc.h>
7 #include <mlx5_malloc.h>
9 #include "mlx5_utils.h"
12 /********************* Cache list ************************/
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)
19 return mlx5_malloc(MLX5_MEM_ZERO, list->entry_sz, 0, SOCKET_ID_ANY);
23 mlx5_clist_default_remove_cb(struct mlx5_cache_list *list __rte_unused,
24 struct mlx5_cache_entry *entry)
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)
37 if (!cb_match || (!cb_create ^ !cb_remove))
40 snprintf(list->name, sizeof(list->name), "%s", name);
41 list->entry_sz = entry_size;
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);
52 static struct mlx5_cache_entry *
53 __cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
55 struct mlx5_cache_entry *entry;
57 LIST_FOREACH(entry, &list->head, next) {
58 if (list->cb_match(list, entry, ctx))
61 __atomic_add_fetch(&entry->ref_cnt, 1,
63 DRV_LOG(DEBUG, "Cache list %s entry %p ref++: %u.",
64 list->name, (void *)entry, entry->ref_cnt);
71 static struct mlx5_cache_entry *
72 cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
74 struct mlx5_cache_entry *entry;
76 rte_rwlock_read_lock(&list->lock);
77 entry = __cache_lookup(list, ctx, reuse);
78 rte_rwlock_read_unlock(&list->lock);
82 struct mlx5_cache_entry *
83 mlx5_cache_lookup(struct mlx5_cache_list *list, void *ctx)
85 return cache_lookup(list, ctx, false);
88 struct mlx5_cache_entry *
89 mlx5_cache_register(struct mlx5_cache_list *list, void *ctx)
91 struct mlx5_cache_entry *entry;
92 uint32_t prev_gen_cnt = 0;
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);
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);
109 entry = list->cb_create(list, entry, ctx);
111 DRV_LOG(ERR, "Failed to init cache list %s entry %p.",
112 list->name, (void *)entry);
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);
122 rte_rwlock_write_unlock(&list->lock);
127 mlx5_cache_unregister(struct mlx5_cache_list *list,
128 struct mlx5_cache_entry *entry)
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);
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);
149 mlx5_cache_list_destroy(struct mlx5_cache_list *list)
151 struct mlx5_cache_entry *entry;
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);
162 memset(list, 0, sizeof(*list));
166 mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list)
169 return __atomic_load_n(&list->count, __ATOMIC_RELAXED);
172 /********************* Indexed pool **********************/
175 mlx5_ipool_lock(struct mlx5_indexed_pool *pool)
177 if (pool->cfg.need_lock)
178 rte_spinlock_lock(&pool->lock);
182 mlx5_ipool_unlock(struct mlx5_indexed_pool *pool)
184 if (pool->cfg.need_lock)
185 rte_spinlock_unlock(&pool->lock);
188 static inline uint32_t
189 mlx5_trunk_idx_get(struct mlx5_indexed_pool *pool, uint32_t entry_idx)
191 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
192 uint32_t trunk_idx = 0;
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;
202 for (i = 0; i < cfg->grow_trunk; i++) {
203 if (entry_idx < pool->grow_tbl[i])
211 static inline uint32_t
212 mlx5_trunk_size_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
214 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
216 return cfg->trunk_size << (cfg->grow_shift *
217 (trunk_idx > cfg->grow_trunk ? cfg->grow_trunk : trunk_idx));
220 static inline uint32_t
221 mlx5_trunk_idx_offset_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
223 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
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];
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);
239 struct mlx5_indexed_pool *
240 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
242 struct mlx5_indexed_pool *pool;
245 if (!cfg || (!cfg->malloc ^ !cfg->free) ||
246 (cfg->trunk_size && ((cfg->trunk_size & (cfg->trunk_size - 1)) ||
247 ((__builtin_ffs(cfg->trunk_size) + TRUNK_IDX_BITS) > 32))))
249 pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool) + cfg->grow_trunk *
250 sizeof(pool->grow_tbl[0]), RTE_CACHE_LINE_SIZE,
255 if (!pool->cfg.trunk_size)
256 pool->cfg.trunk_size = MLX5_IPOOL_DEFAULT_TRUNK_SIZE;
257 if (!cfg->malloc && !cfg->free) {
258 pool->cfg.malloc = mlx5_malloc;
259 pool->cfg.free = mlx5_free;
261 pool->free_list = TRUNK_INVALID;
262 if (pool->cfg.need_lock)
263 rte_spinlock_init(&pool->lock);
265 * Initialize the dynamic grow trunk size lookup table to have a quick
266 * lookup for the trunk entry index offset.
268 for (i = 0; i < cfg->grow_trunk; i++) {
269 pool->grow_tbl[i] = cfg->trunk_size << (cfg->grow_shift * i);
271 pool->grow_tbl[i] += pool->grow_tbl[i - 1];
277 mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
279 struct mlx5_indexed_trunk *trunk;
280 struct mlx5_indexed_trunk **trunk_tmp;
281 struct mlx5_indexed_trunk **p;
282 size_t trunk_size = 0;
287 if (pool->n_trunk_valid == TRUNK_MAX_IDX)
289 if (pool->n_trunk_valid == pool->n_trunk) {
290 /* No free trunk flags, expand trunk list. */
291 int n_grow = pool->n_trunk_valid ? pool->n_trunk :
292 RTE_CACHE_LINE_SIZE / sizeof(void *);
294 p = pool->cfg.malloc(0, (pool->n_trunk_valid + n_grow) *
295 sizeof(struct mlx5_indexed_trunk *),
296 RTE_CACHE_LINE_SIZE, rte_socket_id());
300 memcpy(p, pool->trunks, pool->n_trunk_valid *
301 sizeof(struct mlx5_indexed_trunk *));
302 memset(RTE_PTR_ADD(p, pool->n_trunk_valid * sizeof(void *)), 0,
303 n_grow * sizeof(void *));
304 trunk_tmp = pool->trunks;
307 pool->cfg.free(trunk_tmp);
308 pool->n_trunk += n_grow;
310 if (!pool->cfg.release_mem_en) {
311 idx = pool->n_trunk_valid;
313 /* Find the first available slot in trunk list */
314 for (idx = 0; idx < pool->n_trunk; idx++)
315 if (pool->trunks[idx] == NULL)
318 trunk_size += sizeof(*trunk);
319 data_size = mlx5_trunk_size_get(pool, idx);
320 bmp_size = rte_bitmap_get_memory_footprint(data_size);
321 /* rte_bitmap requires memory cacheline aligned. */
322 trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
323 trunk_size += bmp_size;
324 trunk = pool->cfg.malloc(0, trunk_size,
325 RTE_CACHE_LINE_SIZE, rte_socket_id());
328 pool->trunks[idx] = trunk;
330 trunk->free = data_size;
331 trunk->prev = TRUNK_INVALID;
332 trunk->next = TRUNK_INVALID;
333 MLX5_ASSERT(pool->free_list == TRUNK_INVALID);
334 pool->free_list = idx;
335 /* Mark all entries as available. */
336 trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
337 [RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
339 MLX5_ASSERT(trunk->bmp);
340 pool->n_trunk_valid++;
349 mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
351 struct mlx5_indexed_trunk *trunk;
356 mlx5_ipool_lock(pool);
357 if (pool->free_list == TRUNK_INVALID) {
358 /* If no available trunks, grow new. */
359 if (mlx5_ipool_grow(pool)) {
360 mlx5_ipool_unlock(pool);
364 MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
365 trunk = pool->trunks[pool->free_list];
366 MLX5_ASSERT(trunk->free);
367 if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
368 mlx5_ipool_unlock(pool);
372 iidx += __builtin_ctzll(slab);
373 MLX5_ASSERT(iidx != UINT32_MAX);
374 MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
375 rte_bitmap_clear(trunk->bmp, iidx);
376 p = &trunk->data[iidx * pool->cfg.size];
378 * The ipool index should grow continually from small to big,
379 * some features as metering only accept limited bits of index.
380 * Random index with MSB set may be rejected.
382 iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
383 iidx += 1; /* non-zero index. */
389 /* Full trunk will be removed from free list in imalloc. */
390 MLX5_ASSERT(pool->free_list == trunk->idx);
391 pool->free_list = trunk->next;
392 if (trunk->next != TRUNK_INVALID)
393 pool->trunks[trunk->next]->prev = TRUNK_INVALID;
394 trunk->prev = TRUNK_INVALID;
395 trunk->next = TRUNK_INVALID;
402 mlx5_ipool_unlock(pool);
407 mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
409 void *entry = mlx5_ipool_malloc(pool, idx);
411 if (entry && pool->cfg.size)
412 memset(entry, 0, pool->cfg.size);
417 mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
419 struct mlx5_indexed_trunk *trunk;
426 mlx5_ipool_lock(pool);
427 trunk_idx = mlx5_trunk_idx_get(pool, idx);
428 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
429 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
431 trunk = pool->trunks[trunk_idx];
434 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
435 if (trunk_idx != trunk->idx ||
436 rte_bitmap_get(trunk->bmp, entry_idx))
438 rte_bitmap_set(trunk->bmp, entry_idx);
440 if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
441 (pool, trunk->idx)) {
442 if (pool->free_list == trunk->idx)
443 pool->free_list = trunk->next;
444 if (trunk->next != TRUNK_INVALID)
445 pool->trunks[trunk->next]->prev = trunk->prev;
446 if (trunk->prev != TRUNK_INVALID)
447 pool->trunks[trunk->prev]->next = trunk->next;
448 pool->cfg.free(trunk);
449 pool->trunks[trunk_idx] = NULL;
450 pool->n_trunk_valid--;
455 if (pool->n_trunk_valid == 0) {
456 pool->cfg.free(pool->trunks);
460 } else if (trunk->free == 1) {
461 /* Put into free trunk list head. */
462 MLX5_ASSERT(pool->free_list != trunk->idx);
463 trunk->next = pool->free_list;
464 trunk->prev = TRUNK_INVALID;
465 if (pool->free_list != TRUNK_INVALID)
466 pool->trunks[pool->free_list]->prev = trunk->idx;
467 pool->free_list = trunk->idx;
477 mlx5_ipool_unlock(pool);
481 mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
483 struct mlx5_indexed_trunk *trunk;
491 mlx5_ipool_lock(pool);
492 trunk_idx = mlx5_trunk_idx_get(pool, idx);
493 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
494 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
496 trunk = pool->trunks[trunk_idx];
499 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
500 if (trunk_idx != trunk->idx ||
501 rte_bitmap_get(trunk->bmp, entry_idx))
503 p = &trunk->data[entry_idx * pool->cfg.size];
505 mlx5_ipool_unlock(pool);
510 mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
512 struct mlx5_indexed_trunk **trunks;
516 mlx5_ipool_lock(pool);
517 trunks = pool->trunks;
518 for (i = 0; i < pool->n_trunk; i++) {
520 pool->cfg.free(trunks[i]);
523 pool->cfg.free(pool->trunks);
524 mlx5_ipool_unlock(pool);
530 mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
532 printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
534 pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
535 pool->cfg.trunk_size, pool->n_trunk_valid);
537 printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
538 "available %u free %u\n",
539 pool->cfg.type, pool->n_entry, pool->trunk_new,
540 pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
544 struct mlx5_l3t_tbl *
545 mlx5_l3t_create(enum mlx5_l3t_type type)
547 struct mlx5_l3t_tbl *tbl;
548 struct mlx5_indexed_pool_config l3t_ip_cfg = {
554 .malloc = mlx5_malloc,
558 if (type >= MLX5_L3T_TYPE_MAX) {
562 tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
570 case MLX5_L3T_TYPE_WORD:
571 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
572 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
574 case MLX5_L3T_TYPE_DWORD:
575 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
576 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
578 case MLX5_L3T_TYPE_QWORD:
579 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
580 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
583 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
584 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
587 rte_spinlock_init(&tbl->sl);
588 tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
598 mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
600 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
607 for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
608 m_tbl = g_tbl->tbl[i];
611 for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
614 MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
615 m_tbl->tbl[j])->ref_cnt);
616 mlx5_ipool_free(tbl->eip,
617 ((struct mlx5_l3t_entry_word *)
618 m_tbl->tbl[j])->idx);
620 if (!(--m_tbl->ref_cnt))
623 MLX5_ASSERT(!m_tbl->ref_cnt);
624 mlx5_free(g_tbl->tbl[i]);
626 if (!(--g_tbl->ref_cnt))
629 MLX5_ASSERT(!g_tbl->ref_cnt);
633 mlx5_ipool_destroy(tbl->eip);
638 __l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
639 union mlx5_l3t_data *data)
641 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
642 struct mlx5_l3t_entry_word *w_e_tbl;
643 struct mlx5_l3t_entry_dword *dw_e_tbl;
644 struct mlx5_l3t_entry_qword *qw_e_tbl;
645 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
652 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
655 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
658 entry_idx = idx & MLX5_L3T_ET_MASK;
660 case MLX5_L3T_TYPE_WORD:
661 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
662 data->word = w_e_tbl->entry[entry_idx].data;
663 if (w_e_tbl->entry[entry_idx].data)
664 w_e_tbl->entry[entry_idx].ref_cnt++;
666 case MLX5_L3T_TYPE_DWORD:
667 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
668 data->dword = dw_e_tbl->entry[entry_idx].data;
669 if (dw_e_tbl->entry[entry_idx].data)
670 dw_e_tbl->entry[entry_idx].ref_cnt++;
672 case MLX5_L3T_TYPE_QWORD:
673 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
674 data->qword = qw_e_tbl->entry[entry_idx].data;
675 if (qw_e_tbl->entry[entry_idx].data)
676 qw_e_tbl->entry[entry_idx].ref_cnt++;
679 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
680 data->ptr = ptr_e_tbl->entry[entry_idx].data;
681 if (ptr_e_tbl->entry[entry_idx].data)
682 ptr_e_tbl->entry[entry_idx].ref_cnt++;
689 mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
690 union mlx5_l3t_data *data)
694 rte_spinlock_lock(&tbl->sl);
695 ret = __l3t_get_entry(tbl, idx, data);
696 rte_spinlock_unlock(&tbl->sl);
701 mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
703 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
704 struct mlx5_l3t_entry_word *w_e_tbl;
705 struct mlx5_l3t_entry_dword *dw_e_tbl;
706 struct mlx5_l3t_entry_qword *qw_e_tbl;
707 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
713 rte_spinlock_lock(&tbl->sl);
717 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
720 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
723 entry_idx = idx & MLX5_L3T_ET_MASK;
725 case MLX5_L3T_TYPE_WORD:
726 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
727 MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
728 ret = --w_e_tbl->entry[entry_idx].ref_cnt;
731 w_e_tbl->entry[entry_idx].data = 0;
732 ref_cnt = --w_e_tbl->ref_cnt;
734 case MLX5_L3T_TYPE_DWORD:
735 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
736 MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
737 ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
740 dw_e_tbl->entry[entry_idx].data = 0;
741 ref_cnt = --dw_e_tbl->ref_cnt;
743 case MLX5_L3T_TYPE_QWORD:
744 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
745 MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
746 ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
749 qw_e_tbl->entry[entry_idx].data = 0;
750 ref_cnt = --qw_e_tbl->ref_cnt;
753 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
754 MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
755 ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
758 ptr_e_tbl->entry[entry_idx].data = NULL;
759 ref_cnt = --ptr_e_tbl->ref_cnt;
763 mlx5_ipool_free(tbl->eip,
764 ((struct mlx5_l3t_entry_word *)e_tbl)->idx);
765 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
767 if (!(--m_tbl->ref_cnt)) {
770 [(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
771 if (!(--g_tbl->ref_cnt)) {
778 rte_spinlock_unlock(&tbl->sl);
783 __l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
784 union mlx5_l3t_data *data)
786 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
787 struct mlx5_l3t_entry_word *w_e_tbl;
788 struct mlx5_l3t_entry_dword *dw_e_tbl;
789 struct mlx5_l3t_entry_qword *qw_e_tbl;
790 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
792 uint32_t entry_idx, tbl_idx = 0;
794 /* Check the global table, create it if empty. */
797 g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
798 sizeof(struct mlx5_l3t_level_tbl) +
799 sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
808 * Check the middle table, create it if empty. Ref_cnt will be
809 * increased if new sub table created.
811 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
813 m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
814 sizeof(struct mlx5_l3t_level_tbl) +
815 sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
821 g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
826 * Check the entry table, create it if empty. Ref_cnt will be
827 * increased if new sub entry table created.
829 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
831 e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
836 ((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
837 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
841 entry_idx = idx & MLX5_L3T_ET_MASK;
843 case MLX5_L3T_TYPE_WORD:
844 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
845 if (w_e_tbl->entry[entry_idx].data) {
846 data->word = w_e_tbl->entry[entry_idx].data;
847 w_e_tbl->entry[entry_idx].ref_cnt++;
851 w_e_tbl->entry[entry_idx].data = data->word;
852 w_e_tbl->entry[entry_idx].ref_cnt = 1;
855 case MLX5_L3T_TYPE_DWORD:
856 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
857 if (dw_e_tbl->entry[entry_idx].data) {
858 data->dword = dw_e_tbl->entry[entry_idx].data;
859 dw_e_tbl->entry[entry_idx].ref_cnt++;
863 dw_e_tbl->entry[entry_idx].data = data->dword;
864 dw_e_tbl->entry[entry_idx].ref_cnt = 1;
867 case MLX5_L3T_TYPE_QWORD:
868 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
869 if (qw_e_tbl->entry[entry_idx].data) {
870 data->qword = qw_e_tbl->entry[entry_idx].data;
871 qw_e_tbl->entry[entry_idx].ref_cnt++;
875 qw_e_tbl->entry[entry_idx].data = data->qword;
876 qw_e_tbl->entry[entry_idx].ref_cnt = 1;
880 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
881 if (ptr_e_tbl->entry[entry_idx].data) {
882 data->ptr = ptr_e_tbl->entry[entry_idx].data;
883 ptr_e_tbl->entry[entry_idx].ref_cnt++;
887 ptr_e_tbl->entry[entry_idx].data = data->ptr;
888 ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
889 ptr_e_tbl->ref_cnt++;
896 mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
897 union mlx5_l3t_data *data)
901 rte_spinlock_lock(&tbl->sl);
902 ret = __l3t_set_entry(tbl, idx, data);
903 rte_spinlock_unlock(&tbl->sl);
908 mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
909 union mlx5_l3t_data *data,
910 mlx5_l3t_alloc_callback_fn cb, void *ctx)
914 rte_spinlock_lock(&tbl->sl);
915 /* Check if entry data is ready. */
916 ret = __l3t_get_entry(tbl, idx, data);
919 case MLX5_L3T_TYPE_WORD:
923 case MLX5_L3T_TYPE_DWORD:
927 case MLX5_L3T_TYPE_QWORD:
937 /* Entry data is not ready, use user callback to create it. */
941 /* Save the new allocated data to entry. */
942 ret = __l3t_set_entry(tbl, idx, data);
944 rte_spinlock_unlock(&tbl->sl);