xref: /dpdk/drivers/net/mlx5/mlx5_utils.h (revision 25245d5dc9ecfa8bc9964c69a756beca6ee1ca72)
18fd92a66SOlivier Matz /* SPDX-License-Identifier: BSD-3-Clause
2771fa900SAdrien Mazarguil  * Copyright 2015 6WIND S.A.
35feecc57SShahaf Shuler  * Copyright 2015 Mellanox Technologies, Ltd
4771fa900SAdrien Mazarguil  */
5771fa900SAdrien Mazarguil 
6771fa900SAdrien Mazarguil #ifndef RTE_PMD_MLX5_UTILS_H_
7771fa900SAdrien Mazarguil #define RTE_PMD_MLX5_UTILS_H_
8771fa900SAdrien Mazarguil 
9771fa900SAdrien Mazarguil #include <stddef.h>
10b113cb5eSEdward Makarov #include <stdint.h>
11771fa900SAdrien Mazarguil #include <stdio.h>
12771fa900SAdrien Mazarguil #include <limits.h>
13771fa900SAdrien Mazarguil #include <errno.h>
14771fa900SAdrien Mazarguil 
15a3cf59f5SSuanming Mou #include <rte_spinlock.h>
16e69a5922SXueming Li #include <rte_rwlock.h>
17a3cf59f5SSuanming Mou #include <rte_memory.h>
18a3cf59f5SSuanming Mou #include <rte_bitmap.h>
19a3cf59f5SSuanming Mou 
207b4f1e6bSMatan Azrad #include <mlx5_common.h>
21*25245d5dSShiri Kuzin #include <mlx5_common_utils.h>
227b4f1e6bSMatan Azrad 
23771fa900SAdrien Mazarguil #include "mlx5_defs.h"
24771fa900SAdrien Mazarguil 
25b113cb5eSEdward Makarov /* Convert a bit number to the corresponding 64-bit mask */
26b113cb5eSEdward Makarov #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
27b113cb5eSEdward Makarov 
28771fa900SAdrien Mazarguil /* Save and restore errno around argument evaluation. */
29771fa900SAdrien Mazarguil #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
30771fa900SAdrien Mazarguil 
31a170a30dSNélio Laranjeiro extern int mlx5_logtype;
32a170a30dSNélio Laranjeiro 
332d2546adSAsaf Penso #define MLX5_NET_LOG_PREFIX "mlx5_net"
342d2546adSAsaf Penso 
35771fa900SAdrien Mazarguil /* Generic printf()-like logging macro with automatic line feed. */
36a170a30dSNélio Laranjeiro #define DRV_LOG(level, ...) \
372d2546adSAsaf Penso 	PMD_DRV_LOG_(level, mlx5_logtype, MLX5_NET_LOG_PREFIX, \
38771fa900SAdrien Mazarguil 		__VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
39771fa900SAdrien Mazarguil 		PMD_DRV_LOG_CPAREN)
40771fa900SAdrien Mazarguil 
412e22920bSAdrien Mazarguil /* Convenience macros for accessing mbuf fields. */
422e22920bSAdrien Mazarguil #define NEXT(m) ((m)->next)
432e22920bSAdrien Mazarguil #define DATA_LEN(m) ((m)->data_len)
442e22920bSAdrien Mazarguil #define PKT_LEN(m) ((m)->pkt_len)
452e22920bSAdrien Mazarguil #define DATA_OFF(m) ((m)->data_off)
462e22920bSAdrien Mazarguil #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
472e22920bSAdrien Mazarguil #define NB_SEGS(m) ((m)->nb_segs)
482e22920bSAdrien Mazarguil #define PORT(m) ((m)->port)
492e22920bSAdrien Mazarguil 
5067fa62bcSAdrien Mazarguil /* Transpose flags. Useful to convert IBV to DPDK flags. */
5167fa62bcSAdrien Mazarguil #define TRANSPOSE(val, from, to) \
5267fa62bcSAdrien Mazarguil 	(((from) >= (to)) ? \
5367fa62bcSAdrien Mazarguil 	 (((val) & (from)) / ((from) / (to))) : \
5467fa62bcSAdrien Mazarguil 	 (((val) & (from)) * ((to) / (from))))
5567fa62bcSAdrien Mazarguil 
56a3cf59f5SSuanming Mou /*
57bd81eaebSSuanming Mou  * For the case which data is linked with sequence increased index, the
58bd81eaebSSuanming Mou  * array table will be more efficiect than hash table once need to serarch
59bd81eaebSSuanming Mou  * one data entry in large numbers of entries. Since the traditional hash
60bd81eaebSSuanming Mou  * tables has fixed table size, when huge numbers of data saved to the hash
61bd81eaebSSuanming Mou  * table, it also comes lots of hash conflict.
62bd81eaebSSuanming Mou  *
63bd81eaebSSuanming Mou  * But simple array table also has fixed size, allocates all the needed
64bd81eaebSSuanming Mou  * memory at once will waste lots of memory. For the case don't know the
65bd81eaebSSuanming Mou  * exactly number of entries will be impossible to allocate the array.
66bd81eaebSSuanming Mou  *
67bd81eaebSSuanming Mou  * Then the multiple level table helps to balance the two disadvantages.
68bd81eaebSSuanming Mou  * Allocate a global high level table with sub table entries at first,
69bd81eaebSSuanming Mou  * the global table contains the sub table entries, and the sub table will
70bd81eaebSSuanming Mou  * be allocated only once the corresponding index entry need to be saved.
71bd81eaebSSuanming Mou  * e.g. for up to 32-bits index, three level table with 10-10-12 splitting,
72bd81eaebSSuanming Mou  * with sequence increased index, the memory grows with every 4K entries.
73bd81eaebSSuanming Mou  *
74bd81eaebSSuanming Mou  * The currently implementation introduces 10-10-12 32-bits splitting
75bd81eaebSSuanming Mou  * Three-Level table to help the cases which have millions of enties to
76bd81eaebSSuanming Mou  * save. The index entries can be addressed directly by the index, no
77bd81eaebSSuanming Mou  * search will be needed.q
78bd81eaebSSuanming Mou  */
79bd81eaebSSuanming Mou 
80bd81eaebSSuanming Mou /* L3 table global table define. */
81bd81eaebSSuanming Mou #define MLX5_L3T_GT_OFFSET 22
82bd81eaebSSuanming Mou #define MLX5_L3T_GT_SIZE (1 << 10)
83bd81eaebSSuanming Mou #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1)
84bd81eaebSSuanming Mou 
85bd81eaebSSuanming Mou /* L3 table middle table define. */
86bd81eaebSSuanming Mou #define MLX5_L3T_MT_OFFSET 12
87bd81eaebSSuanming Mou #define MLX5_L3T_MT_SIZE (1 << 10)
88bd81eaebSSuanming Mou #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1)
89bd81eaebSSuanming Mou 
90bd81eaebSSuanming Mou /* L3 table entry table define. */
91bd81eaebSSuanming Mou #define MLX5_L3T_ET_OFFSET 0
92bd81eaebSSuanming Mou #define MLX5_L3T_ET_SIZE (1 << 12)
93bd81eaebSSuanming Mou #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1)
94bd81eaebSSuanming Mou 
95bd81eaebSSuanming Mou /* L3 table type. */
96bd81eaebSSuanming Mou enum mlx5_l3t_type {
97bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_WORD = 0,
98bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_DWORD,
99bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_QWORD,
100bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_PTR,
101bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_MAX,
102bd81eaebSSuanming Mou };
103bd81eaebSSuanming Mou 
104bd81eaebSSuanming Mou struct mlx5_indexed_pool;
105bd81eaebSSuanming Mou 
106bd81eaebSSuanming Mou /* Generic data struct. */
107bd81eaebSSuanming Mou union mlx5_l3t_data {
108bd81eaebSSuanming Mou 	uint16_t word;
109bd81eaebSSuanming Mou 	uint32_t dword;
110bd81eaebSSuanming Mou 	uint64_t qword;
111bd81eaebSSuanming Mou 	void *ptr;
112bd81eaebSSuanming Mou };
113bd81eaebSSuanming Mou 
114bd81eaebSSuanming Mou /* L3 level table data structure. */
115bd81eaebSSuanming Mou struct mlx5_l3t_level_tbl {
116bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
117bd81eaebSSuanming Mou 	void *tbl[]; /* Table array. */
118bd81eaebSSuanming Mou };
119bd81eaebSSuanming Mou 
120bd81eaebSSuanming Mou /* L3 word entry table data structure. */
121bd81eaebSSuanming Mou struct mlx5_l3t_entry_word {
122bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
123bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1240796c7b1SSuanming Mou 	struct {
1250796c7b1SSuanming Mou 		uint16_t data;
1260796c7b1SSuanming Mou 		uint32_t ref_cnt;
1270796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1280796c7b1SSuanming Mou } __rte_packed;
129bd81eaebSSuanming Mou 
130bd81eaebSSuanming Mou /* L3 double word entry table data structure. */
131bd81eaebSSuanming Mou struct mlx5_l3t_entry_dword {
132bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
133bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1340796c7b1SSuanming Mou 	struct {
1350796c7b1SSuanming Mou 		uint32_t data;
1360796c7b1SSuanming Mou 		int32_t ref_cnt;
1370796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1380796c7b1SSuanming Mou } __rte_packed;
139bd81eaebSSuanming Mou 
140bd81eaebSSuanming Mou /* L3 quad word entry table data structure. */
141bd81eaebSSuanming Mou struct mlx5_l3t_entry_qword {
142bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
143bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1440796c7b1SSuanming Mou 	struct {
1450796c7b1SSuanming Mou 		uint64_t data;
1460796c7b1SSuanming Mou 		uint32_t ref_cnt;
1470796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1480796c7b1SSuanming Mou } __rte_packed;
149bd81eaebSSuanming Mou 
150bd81eaebSSuanming Mou /* L3 pointer entry table data structure. */
151bd81eaebSSuanming Mou struct mlx5_l3t_entry_ptr {
152bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
153bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1540796c7b1SSuanming Mou 	struct {
1550796c7b1SSuanming Mou 		void *data;
1560796c7b1SSuanming Mou 		uint32_t ref_cnt;
1570796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1580796c7b1SSuanming Mou } __rte_packed;
159bd81eaebSSuanming Mou 
160bd81eaebSSuanming Mou /* L3 table data structure. */
161bd81eaebSSuanming Mou struct mlx5_l3t_tbl {
162bd81eaebSSuanming Mou 	enum mlx5_l3t_type type; /* Table type. */
163bd81eaebSSuanming Mou 	struct mlx5_indexed_pool *eip;
164bd81eaebSSuanming Mou 	/* Table index pool handles. */
165bd81eaebSSuanming Mou 	struct mlx5_l3t_level_tbl *tbl; /* Global table index. */
1660796c7b1SSuanming Mou 	rte_spinlock_t sl; /* The table lock. */
167bd81eaebSSuanming Mou };
168bd81eaebSSuanming Mou 
1690796c7b1SSuanming Mou /** Type of function that is used to handle the data before freeing. */
1700796c7b1SSuanming Mou typedef int32_t (*mlx5_l3t_alloc_callback_fn)(void *ctx,
1710796c7b1SSuanming Mou 					   union mlx5_l3t_data *data);
1720796c7b1SSuanming Mou 
173bd81eaebSSuanming Mou /*
174a3cf59f5SSuanming Mou  * The indexed memory entry index is made up of trunk index and offset of
175a3cf59f5SSuanming Mou  * the entry in the trunk. Since the entry index is 32 bits, in case user
176a3cf59f5SSuanming Mou  * prefers to have small trunks, user can change the macro below to a big
177a3cf59f5SSuanming Mou  * number which helps the pool contains more trunks with lots of entries
178a3cf59f5SSuanming Mou  * allocated.
179a3cf59f5SSuanming Mou  */
180a3cf59f5SSuanming Mou #define TRUNK_IDX_BITS 16
181a3cf59f5SSuanming Mou #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
182a3cf59f5SSuanming Mou #define TRUNK_INVALID TRUNK_MAX_IDX
183a3cf59f5SSuanming Mou #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
184a3cf59f5SSuanming Mou #ifdef RTE_LIBRTE_MLX5_DEBUG
185a3cf59f5SSuanming Mou #define POOL_DEBUG 1
186a3cf59f5SSuanming Mou #endif
187a3cf59f5SSuanming Mou 
188a3cf59f5SSuanming Mou struct mlx5_indexed_pool_config {
189a3cf59f5SSuanming Mou 	uint32_t size; /* Pool entry size. */
19062d7d519SSuanming Mou 	uint32_t trunk_size:22;
19162d7d519SSuanming Mou 	/*
19262d7d519SSuanming Mou 	 * Trunk entry number. Must be power of 2. It can be increased
19362d7d519SSuanming Mou 	 * if trunk_grow enable. The trunk entry number increases with
19462d7d519SSuanming Mou 	 * left shift grow_shift. Trunks with index are after grow_trunk
19562d7d519SSuanming Mou 	 * will keep the entry number same with the last grow trunk.
19662d7d519SSuanming Mou 	 */
19762d7d519SSuanming Mou 	uint32_t grow_trunk:4;
19862d7d519SSuanming Mou 	/*
19962d7d519SSuanming Mou 	 * Trunks with entry number increase in the pool. Set it to 0
20062d7d519SSuanming Mou 	 * to make the pool works as trunk entry fixed pool. It works
20162d7d519SSuanming Mou 	 * only if grow_shift is not 0.
20262d7d519SSuanming Mou 	 */
20362d7d519SSuanming Mou 	uint32_t grow_shift:4;
20462d7d519SSuanming Mou 	/*
20562d7d519SSuanming Mou 	 * Trunk entry number increase shift value, stop after grow_trunk.
20662d7d519SSuanming Mou 	 * It works only if grow_trunk is not 0.
20762d7d519SSuanming Mou 	 */
20862d7d519SSuanming Mou 	uint32_t need_lock:1;
209a3cf59f5SSuanming Mou 	/* Lock is needed for multiple thread usage. */
2101fd4bb67SSuanming Mou 	uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
211a3cf59f5SSuanming Mou 	const char *type; /* Memory allocate type name. */
21283c2047cSSuanming Mou 	void *(*malloc)(uint32_t flags, size_t size, unsigned int align,
213a3cf59f5SSuanming Mou 			int socket);
214a3cf59f5SSuanming Mou 	/* User defined memory allocator. */
215a3cf59f5SSuanming Mou 	void (*free)(void *addr); /* User defined memory release. */
216a3cf59f5SSuanming Mou };
217a3cf59f5SSuanming Mou 
218a3cf59f5SSuanming Mou struct mlx5_indexed_trunk {
219a3cf59f5SSuanming Mou 	uint32_t idx; /* Trunk id. */
220a3cf59f5SSuanming Mou 	uint32_t prev; /* Previous free trunk in free list. */
221a3cf59f5SSuanming Mou 	uint32_t next; /* Next free trunk in free list. */
222a3cf59f5SSuanming Mou 	uint32_t free; /* Free entries available */
223a3cf59f5SSuanming Mou 	struct rte_bitmap *bmp;
224691b3d3eSSuanming Mou 	uint8_t data[] __rte_cache_aligned; /* Entry data start. */
225a3cf59f5SSuanming Mou };
226a3cf59f5SSuanming Mou 
227a3cf59f5SSuanming Mou struct mlx5_indexed_pool {
228a3cf59f5SSuanming Mou 	struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
229a3cf59f5SSuanming Mou 	rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
230a3cf59f5SSuanming Mou 	uint32_t n_trunk_valid; /* Trunks allocated. */
231a3cf59f5SSuanming Mou 	uint32_t n_trunk; /* Trunk pointer array size. */
232a3cf59f5SSuanming Mou 	/* Dim of trunk pointer array. */
233a3cf59f5SSuanming Mou 	struct mlx5_indexed_trunk **trunks;
234a3cf59f5SSuanming Mou 	uint32_t free_list; /* Index to first free trunk. */
235a3cf59f5SSuanming Mou #ifdef POOL_DEBUG
236a3cf59f5SSuanming Mou 	uint32_t n_entry;
237a3cf59f5SSuanming Mou 	uint32_t trunk_new;
238a3cf59f5SSuanming Mou 	uint32_t trunk_avail;
239a3cf59f5SSuanming Mou 	uint32_t trunk_empty;
240a3cf59f5SSuanming Mou 	uint32_t trunk_free;
241a3cf59f5SSuanming Mou #endif
24262d7d519SSuanming Mou 	uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
243a3cf59f5SSuanming Mou };
244a3cf59f5SSuanming Mou 
245634efbc2SNelio Laranjeiro /**
24646287eacSBing Zhao  * Return logarithm of the nearest power of two above input value.
247634efbc2SNelio Laranjeiro  *
248634efbc2SNelio Laranjeiro  * @param v
249634efbc2SNelio Laranjeiro  *   Input value.
250634efbc2SNelio Laranjeiro  *
251634efbc2SNelio Laranjeiro  * @return
25246287eacSBing Zhao  *   Logarithm of the nearest power of two above input value.
253634efbc2SNelio Laranjeiro  */
254634efbc2SNelio Laranjeiro static inline unsigned int
255634efbc2SNelio Laranjeiro log2above(unsigned int v)
256634efbc2SNelio Laranjeiro {
257634efbc2SNelio Laranjeiro 	unsigned int l;
258634efbc2SNelio Laranjeiro 	unsigned int r;
259634efbc2SNelio Laranjeiro 
260634efbc2SNelio Laranjeiro 	for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
261634efbc2SNelio Laranjeiro 		r |= (v & 1);
262693f715dSHuawei Xie 	return l + r;
263634efbc2SNelio Laranjeiro }
264634efbc2SNelio Laranjeiro 
2651ff37beeSXueming Li /************************ cache list *****************************/
2661ff37beeSXueming Li 
2671ff37beeSXueming Li /** Maximum size of string for naming. */
2681ff37beeSXueming Li #define MLX5_NAME_SIZE			32
2691ff37beeSXueming Li 
2701ff37beeSXueming Li struct mlx5_cache_list;
2711ff37beeSXueming Li 
2721ff37beeSXueming Li /**
2731ff37beeSXueming Li  * Structure of the entry in the cache list, user should define its own struct
2741ff37beeSXueming Li  * that contains this in order to store the data.
2751ff37beeSXueming Li  */
2761ff37beeSXueming Li struct mlx5_cache_entry {
2771ff37beeSXueming Li 	LIST_ENTRY(mlx5_cache_entry) next; /* Entry pointers in the list. */
2781ff37beeSXueming Li 	uint32_t ref_cnt; /* Reference count. */
2791ff37beeSXueming Li };
2801ff37beeSXueming Li 
2811ff37beeSXueming Li /**
2821ff37beeSXueming Li  * Type of callback function for entry removal.
2831ff37beeSXueming Li  *
2841ff37beeSXueming Li  * @param list
2851ff37beeSXueming Li  *   The cache list.
2861ff37beeSXueming Li  * @param entry
2871ff37beeSXueming Li  *   The entry in the list.
2881ff37beeSXueming Li  */
2891ff37beeSXueming Li typedef void (*mlx5_cache_remove_cb)(struct mlx5_cache_list *list,
2901ff37beeSXueming Li 				     struct mlx5_cache_entry *entry);
2911ff37beeSXueming Li 
2921ff37beeSXueming Li /**
2931ff37beeSXueming Li  * Type of function for user defined matching.
2941ff37beeSXueming Li  *
2951ff37beeSXueming Li  * @param list
2961ff37beeSXueming Li  *   The cache list.
2971ff37beeSXueming Li  * @param entry
2981ff37beeSXueming Li  *   The entry in the list.
2991ff37beeSXueming Li  * @param ctx
3001ff37beeSXueming Li  *   The pointer to new entry context.
3011ff37beeSXueming Li  *
3021ff37beeSXueming Li  * @return
3031ff37beeSXueming Li  *   0 if matching, non-zero number otherwise.
3041ff37beeSXueming Li  */
3051ff37beeSXueming Li typedef int (*mlx5_cache_match_cb)(struct mlx5_cache_list *list,
3061ff37beeSXueming Li 				   struct mlx5_cache_entry *entry, void *ctx);
3071ff37beeSXueming Li 
3081ff37beeSXueming Li /**
3091ff37beeSXueming Li  * Type of function for user defined cache list entry creation.
3101ff37beeSXueming Li  *
3111ff37beeSXueming Li  * @param list
3121ff37beeSXueming Li  *   The cache list.
3131ff37beeSXueming Li  * @param entry
3141ff37beeSXueming Li  *   The new allocated entry, NULL if list entry size unspecified,
3151ff37beeSXueming Li  *   New entry has to be allocated in callback and return.
3161ff37beeSXueming Li  * @param ctx
3171ff37beeSXueming Li  *   The pointer to new entry context.
3181ff37beeSXueming Li  *
3191ff37beeSXueming Li  * @return
3201ff37beeSXueming Li  *   Pointer of entry on success, NULL otherwise.
3211ff37beeSXueming Li  */
3221ff37beeSXueming Li typedef struct mlx5_cache_entry *(*mlx5_cache_create_cb)
3231ff37beeSXueming Li 				 (struct mlx5_cache_list *list,
3241ff37beeSXueming Li 				  struct mlx5_cache_entry *entry,
3251ff37beeSXueming Li 				  void *ctx);
3261ff37beeSXueming Li 
3271ff37beeSXueming Li /**
3281ff37beeSXueming Li  * Linked cache list structure.
3291ff37beeSXueming Li  *
3301ff37beeSXueming Li  * Entry in cache list could be reused if entry already exists,
3311ff37beeSXueming Li  * reference count will increase and the existing entry returns.
3321ff37beeSXueming Li  *
3331ff37beeSXueming Li  * When destroy an entry from list, decrease reference count and only
3341ff37beeSXueming Li  * destroy when no further reference.
3351ff37beeSXueming Li  *
3361ff37beeSXueming Li  * Linked list cache is designed for limited number of entries cache,
3371ff37beeSXueming Li  * read mostly, less modification.
3381ff37beeSXueming Li  *
3391ff37beeSXueming Li  * For huge amount of entries cache, please consider hash list cache.
3401ff37beeSXueming Li  *
3411ff37beeSXueming Li  */
3421ff37beeSXueming Li struct mlx5_cache_list {
3431ff37beeSXueming Li 	char name[MLX5_NAME_SIZE]; /**< Name of the cache list. */
3441ff37beeSXueming Li 	uint32_t entry_sz; /**< Entry size, 0: use create callback. */
3451ff37beeSXueming Li 	rte_rwlock_t lock; /* read/write lock. */
3461ff37beeSXueming Li 	uint32_t gen_cnt; /* List modification will update generation count. */
3471ff37beeSXueming Li 	uint32_t count; /* number of entries in list. */
3481ff37beeSXueming Li 	void *ctx; /* user objects target to callback. */
3491ff37beeSXueming Li 	mlx5_cache_create_cb cb_create; /**< entry create callback. */
3501ff37beeSXueming Li 	mlx5_cache_match_cb cb_match; /**< entry match callback. */
3511ff37beeSXueming Li 	mlx5_cache_remove_cb cb_remove; /**< entry remove callback. */
3521ff37beeSXueming Li 	LIST_HEAD(mlx5_cache_head, mlx5_cache_entry) head;
3531ff37beeSXueming Li };
3541ff37beeSXueming Li 
3551ff37beeSXueming Li /**
3561ff37beeSXueming Li  * Initialize a cache list.
3571ff37beeSXueming Li  *
3581ff37beeSXueming Li  * @param list
3591ff37beeSXueming Li  *   Pointer to the hast list table.
3601ff37beeSXueming Li  * @param name
3611ff37beeSXueming Li  *   Name of the cache list.
3621ff37beeSXueming Li  * @param entry_size
3631ff37beeSXueming Li  *   Entry size to allocate, 0 to allocate by creation callback.
3641ff37beeSXueming Li  * @param ctx
3651ff37beeSXueming Li  *   Pointer to the list context data.
3661ff37beeSXueming Li  * @param cb_create
3671ff37beeSXueming Li  *   Callback function for entry create.
3681ff37beeSXueming Li  * @param cb_match
3691ff37beeSXueming Li  *   Callback function for entry match.
3701ff37beeSXueming Li  * @param cb_remove
3711ff37beeSXueming Li  *   Callback function for entry remove.
3721ff37beeSXueming Li  * @return
3731ff37beeSXueming Li  *   0 on success, otherwise failure.
3741ff37beeSXueming Li  */
3751ff37beeSXueming Li int mlx5_cache_list_init(struct mlx5_cache_list *list,
3761ff37beeSXueming Li 			 const char *name, uint32_t entry_size, void *ctx,
3771ff37beeSXueming Li 			 mlx5_cache_create_cb cb_create,
3781ff37beeSXueming Li 			 mlx5_cache_match_cb cb_match,
3791ff37beeSXueming Li 			 mlx5_cache_remove_cb cb_remove);
3801ff37beeSXueming Li 
3811ff37beeSXueming Li /**
3821ff37beeSXueming Li  * Search an entry matching the key.
3831ff37beeSXueming Li  *
3841ff37beeSXueming Li  * Result returned might be destroyed by other thread, must use
3851ff37beeSXueming Li  * this function only in main thread.
3861ff37beeSXueming Li  *
3871ff37beeSXueming Li  * @param list
3881ff37beeSXueming Li  *   Pointer to the cache list.
3891ff37beeSXueming Li  * @param ctx
3901ff37beeSXueming Li  *   Common context parameter used by entry callback function.
3911ff37beeSXueming Li  *
3921ff37beeSXueming Li  * @return
3931ff37beeSXueming Li  *   Pointer of the cache entry if found, NULL otherwise.
3941ff37beeSXueming Li  */
3951ff37beeSXueming Li struct mlx5_cache_entry *mlx5_cache_lookup(struct mlx5_cache_list *list,
3961ff37beeSXueming Li 					   void *ctx);
3971ff37beeSXueming Li 
3981ff37beeSXueming Li /**
3991ff37beeSXueming Li  * Reuse or create an entry to the cache list.
4001ff37beeSXueming Li  *
4011ff37beeSXueming Li  * @param list
4021ff37beeSXueming Li  *   Pointer to the hast list table.
4031ff37beeSXueming Li  * @param ctx
4041ff37beeSXueming Li  *   Common context parameter used by callback function.
4051ff37beeSXueming Li  *
4061ff37beeSXueming Li  * @return
4071ff37beeSXueming Li  *   registered entry on success, NULL otherwise
4081ff37beeSXueming Li  */
4091ff37beeSXueming Li struct mlx5_cache_entry *mlx5_cache_register(struct mlx5_cache_list *list,
4101ff37beeSXueming Li 					     void *ctx);
4111ff37beeSXueming Li 
4121ff37beeSXueming Li /**
4131ff37beeSXueming Li  * Remove an entry from the cache list.
4141ff37beeSXueming Li  *
4151ff37beeSXueming Li  * User should guarantee the validity of the entry.
4161ff37beeSXueming Li  *
4171ff37beeSXueming Li  * @param list
4181ff37beeSXueming Li  *   Pointer to the hast list.
4191ff37beeSXueming Li  * @param entry
4201ff37beeSXueming Li  *   Entry to be removed from the cache list table.
4211ff37beeSXueming Li  * @return
4221ff37beeSXueming Li  *   0 on entry removed, 1 on entry still referenced.
4231ff37beeSXueming Li  */
4241ff37beeSXueming Li int mlx5_cache_unregister(struct mlx5_cache_list *list,
4251ff37beeSXueming Li 			  struct mlx5_cache_entry *entry);
4261ff37beeSXueming Li 
4271ff37beeSXueming Li /**
4281ff37beeSXueming Li  * Destroy the cache list.
4291ff37beeSXueming Li  *
4301ff37beeSXueming Li  * @param list
4311ff37beeSXueming Li  *   Pointer to the cache list.
4321ff37beeSXueming Li  */
4331ff37beeSXueming Li void mlx5_cache_list_destroy(struct mlx5_cache_list *list);
4341ff37beeSXueming Li 
4351ff37beeSXueming Li /**
4361ff37beeSXueming Li  * Get entry number from the cache list.
4371ff37beeSXueming Li  *
4381ff37beeSXueming Li  * @param list
4391ff37beeSXueming Li  *   Pointer to the hast list.
4401ff37beeSXueming Li  * @return
4411ff37beeSXueming Li  *   Cache list entry number.
4421ff37beeSXueming Li  */
4431ff37beeSXueming Li uint32_t
4441ff37beeSXueming Li mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list);
4451ff37beeSXueming Li 
4461ff37beeSXueming Li /********************************* indexed pool *************************/
4471ff37beeSXueming Li 
448a3cf59f5SSuanming Mou /**
449a3cf59f5SSuanming Mou  * This function allocates non-initialized memory entry from pool.
450a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
451a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
452a3cf59f5SSuanming Mou  *
453a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
454a3cf59f5SSuanming Mou  *
455a3cf59f5SSuanming Mou  * @param pool
456a3cf59f5SSuanming Mou  *   Pointer to indexed memory entry pool.
457a3cf59f5SSuanming Mou  *   No initialization required.
458a3cf59f5SSuanming Mou  * @param[out] idx
459a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
460a3cf59f5SSuanming Mou  *   Memory index always positive value.
461a3cf59f5SSuanming Mou  * @return
462a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry.
463a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
464a3cf59f5SSuanming Mou  */
465a3cf59f5SSuanming Mou void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
466a3cf59f5SSuanming Mou 
467a3cf59f5SSuanming Mou /**
468a3cf59f5SSuanming Mou  * This function allocates zero initialized memory entry from pool.
469a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
470a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
471a3cf59f5SSuanming Mou  *
472a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
473a3cf59f5SSuanming Mou  *
474a3cf59f5SSuanming Mou  * @param pool
475a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
476a3cf59f5SSuanming Mou  *   No initialization required.
477a3cf59f5SSuanming Mou  * @param[out] idx
478a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
479a3cf59f5SSuanming Mou  *   Memory index always positive value.
480a3cf59f5SSuanming Mou  * @return
481a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry .
482a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
483a3cf59f5SSuanming Mou  */
484a3cf59f5SSuanming Mou void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
485a3cf59f5SSuanming Mou 
486a3cf59f5SSuanming Mou /**
487a3cf59f5SSuanming Mou  * This function frees indexed memory entry to pool.
488a3cf59f5SSuanming Mou  * Caller has to make sure that the index is allocated from same pool.
489a3cf59f5SSuanming Mou  *
490a3cf59f5SSuanming Mou  * @param pool
491a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
492a3cf59f5SSuanming Mou  * @param idx
493a3cf59f5SSuanming Mou  *   Allocated memory entry index.
494a3cf59f5SSuanming Mou  */
495a3cf59f5SSuanming Mou void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
496a3cf59f5SSuanming Mou 
497a3cf59f5SSuanming Mou /**
498a3cf59f5SSuanming Mou  * This function returns pointer of indexed memory entry from index.
499a3cf59f5SSuanming Mou  * Caller has to make sure that the index is valid, and allocated
500a3cf59f5SSuanming Mou  * from same pool.
501a3cf59f5SSuanming Mou  *
502a3cf59f5SSuanming Mou  * @param pool
503a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
504a3cf59f5SSuanming Mou  * @param idx
505a3cf59f5SSuanming Mou  *   Allocated memory index.
506a3cf59f5SSuanming Mou  * @return
507a3cf59f5SSuanming Mou  *   - Pointer to indexed memory entry.
508a3cf59f5SSuanming Mou  */
509a3cf59f5SSuanming Mou void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
510a3cf59f5SSuanming Mou 
511a3cf59f5SSuanming Mou /**
512a3cf59f5SSuanming Mou  * This function creates indexed memory pool.
513a3cf59f5SSuanming Mou  * Caller has to configure the configuration accordingly.
514a3cf59f5SSuanming Mou  *
515a3cf59f5SSuanming Mou  * @param pool
516a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
517a3cf59f5SSuanming Mou  * @param cfg
518a3cf59f5SSuanming Mou  *   Allocated memory index.
519a3cf59f5SSuanming Mou  */
520a3cf59f5SSuanming Mou struct mlx5_indexed_pool *
521a3cf59f5SSuanming Mou mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
522a3cf59f5SSuanming Mou 
523a3cf59f5SSuanming Mou /**
524a3cf59f5SSuanming Mou  * This function releases all resources of pool.
525a3cf59f5SSuanming Mou  * Caller has to make sure that all indexes and memories allocated
526a3cf59f5SSuanming Mou  * from this pool not referenced anymore.
527a3cf59f5SSuanming Mou  *
528a3cf59f5SSuanming Mou  * @param pool
529a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
530a3cf59f5SSuanming Mou  * @return
531a3cf59f5SSuanming Mou  *   - non-zero value on error.
532a3cf59f5SSuanming Mou  *   - 0 on success.
533a3cf59f5SSuanming Mou  */
534a3cf59f5SSuanming Mou int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
535a3cf59f5SSuanming Mou 
536a3cf59f5SSuanming Mou /**
537a3cf59f5SSuanming Mou  * This function dumps debug info of pool.
538a3cf59f5SSuanming Mou  *
539a3cf59f5SSuanming Mou  * @param pool
540a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
541a3cf59f5SSuanming Mou  */
542a3cf59f5SSuanming Mou void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
543a3cf59f5SSuanming Mou 
544bd81eaebSSuanming Mou /**
545bd81eaebSSuanming Mou  * This function allocates new empty Three-level table.
546bd81eaebSSuanming Mou  *
547bd81eaebSSuanming Mou  * @param type
548bd81eaebSSuanming Mou  *   The l3t can set as word, double word, quad word or pointer with index.
549bd81eaebSSuanming Mou  *
550bd81eaebSSuanming Mou  * @return
551bd81eaebSSuanming Mou  *   - Pointer to the allocated l3t.
552bd81eaebSSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
553bd81eaebSSuanming Mou  */
554bd81eaebSSuanming Mou struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
555bd81eaebSSuanming Mou 
556bd81eaebSSuanming Mou /**
557bd81eaebSSuanming Mou  * This function destroys Three-level table.
558bd81eaebSSuanming Mou  *
559bd81eaebSSuanming Mou  * @param tbl
560bd81eaebSSuanming Mou  *   Pointer to the l3t.
561bd81eaebSSuanming Mou  */
562bd81eaebSSuanming Mou void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
563bd81eaebSSuanming Mou 
564bd81eaebSSuanming Mou /**
565bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
566bd81eaebSSuanming Mou  *
567bd81eaebSSuanming Mou  * @param tbl
568bd81eaebSSuanming Mou  *   Pointer to the l3t.
569bd81eaebSSuanming Mou  * @param idx
570bd81eaebSSuanming Mou  *   Index to the entry.
571bd81eaebSSuanming Mou  * @param data
572bd81eaebSSuanming Mou  *   Pointer to the memory which saves the entry data.
573bd81eaebSSuanming Mou  *   When function call returns 0, data contains the entry data get from
574bd81eaebSSuanming Mou  *   l3t.
575bd81eaebSSuanming Mou  *   When function call returns -1, data is not modified.
576bd81eaebSSuanming Mou  *
577bd81eaebSSuanming Mou  * @return
578bd81eaebSSuanming Mou  *   0 if success, -1 on error.
579bd81eaebSSuanming Mou  */
580bd81eaebSSuanming Mou 
5810796c7b1SSuanming Mou int32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
582bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
583bd81eaebSSuanming Mou 
584bd81eaebSSuanming Mou /**
585bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
586bd81eaebSSuanming Mou  *
5870796c7b1SSuanming Mou  * If the index entry is not available, allocate new one by callback
5880796c7b1SSuanming Mou  * function and fill in the entry.
5890796c7b1SSuanming Mou  *
590bd81eaebSSuanming Mou  * @param tbl
591bd81eaebSSuanming Mou  *   Pointer to the l3t.
592bd81eaebSSuanming Mou  * @param idx
593bd81eaebSSuanming Mou  *   Index to the entry.
594bd81eaebSSuanming Mou  * @param data
5950796c7b1SSuanming Mou  *   Pointer to the memory which saves the entry data.
5960796c7b1SSuanming Mou  *   When function call returns 0, data contains the entry data get from
5970796c7b1SSuanming Mou  *   l3t.
5980796c7b1SSuanming Mou  *   When function call returns -1, data is not modified.
5990796c7b1SSuanming Mou  * @param cb
6000796c7b1SSuanming Mou  *   Callback function to allocate new data.
6010796c7b1SSuanming Mou  * @param ctx
6020796c7b1SSuanming Mou  *   Context for callback function.
603bd81eaebSSuanming Mou  *
604bd81eaebSSuanming Mou  * @return
605bd81eaebSSuanming Mou  *   0 if success, -1 on error.
606bd81eaebSSuanming Mou  */
6070796c7b1SSuanming Mou 
6080796c7b1SSuanming Mou int32_t mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
6090796c7b1SSuanming Mou 			       union mlx5_l3t_data *data,
6100796c7b1SSuanming Mou 			       mlx5_l3t_alloc_callback_fn cb, void *ctx);
6110796c7b1SSuanming Mou 
6120796c7b1SSuanming Mou /**
6130796c7b1SSuanming Mou  * This function decreases and clear index entry if reference
6140796c7b1SSuanming Mou  * counter is 0 from Three-level table.
6150796c7b1SSuanming Mou  *
6160796c7b1SSuanming Mou  * @param tbl
6170796c7b1SSuanming Mou  *   Pointer to the l3t.
6180796c7b1SSuanming Mou  * @param idx
6190796c7b1SSuanming Mou  *   Index to the entry.
6200796c7b1SSuanming Mou  *
6210796c7b1SSuanming Mou  * @return
6220796c7b1SSuanming Mou  *   The remaining reference count, 0 means entry be cleared, -1 on error.
6230796c7b1SSuanming Mou  */
6240796c7b1SSuanming Mou int32_t mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
6250796c7b1SSuanming Mou 
6260796c7b1SSuanming Mou /**
6270796c7b1SSuanming Mou  * This function sets the index entry to Three-level table.
6280796c7b1SSuanming Mou  * If the entry is already set, the EEXIST errno will be given, and
6290796c7b1SSuanming Mou  * the set data will be filled to the data.
6300796c7b1SSuanming Mou  *
6310796c7b1SSuanming Mou  * @param tbl[in]
6320796c7b1SSuanming Mou  *   Pointer to the l3t.
6330796c7b1SSuanming Mou  * @param idx[in]
6340796c7b1SSuanming Mou  *   Index to the entry.
6350796c7b1SSuanming Mou  * @param data[in/out]
6360796c7b1SSuanming Mou  *   Pointer to the memory which contains the entry data save to l3t.
6370796c7b1SSuanming Mou  *   If the entry is already set, the set data will be filled.
6380796c7b1SSuanming Mou  *
6390796c7b1SSuanming Mou  * @return
6400796c7b1SSuanming Mou  *   0 if success, -1 on error.
6410796c7b1SSuanming Mou  */
6420796c7b1SSuanming Mou int32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
643bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
644bd81eaebSSuanming Mou 
645c123b821SSuanming Mou static inline void *
646c123b821SSuanming Mou mlx5_l3t_get_next(struct mlx5_l3t_tbl *tbl, uint32_t *pos)
647c123b821SSuanming Mou {
648c123b821SSuanming Mou 	struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
649c123b821SSuanming Mou 	uint32_t i, j, k, g_start, m_start, e_start;
650c123b821SSuanming Mou 	uint32_t idx = *pos;
651c123b821SSuanming Mou 	void *e_tbl;
652c123b821SSuanming Mou 	struct mlx5_l3t_entry_word *w_e_tbl;
653c123b821SSuanming Mou 	struct mlx5_l3t_entry_dword *dw_e_tbl;
654c123b821SSuanming Mou 	struct mlx5_l3t_entry_qword *qw_e_tbl;
655c123b821SSuanming Mou 	struct mlx5_l3t_entry_ptr *ptr_e_tbl;
656c123b821SSuanming Mou 
657c123b821SSuanming Mou 	if (!tbl)
658c123b821SSuanming Mou 		return NULL;
659c123b821SSuanming Mou 	g_tbl = tbl->tbl;
660c123b821SSuanming Mou 	if (!g_tbl)
661c123b821SSuanming Mou 		return NULL;
662c123b821SSuanming Mou 	g_start = (idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK;
663c123b821SSuanming Mou 	m_start = (idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK;
664c123b821SSuanming Mou 	e_start = idx & MLX5_L3T_ET_MASK;
665c123b821SSuanming Mou 	for (i = g_start; i < MLX5_L3T_GT_SIZE; i++) {
666c123b821SSuanming Mou 		m_tbl = g_tbl->tbl[i];
667c123b821SSuanming Mou 		if (!m_tbl) {
668c123b821SSuanming Mou 			/* Jump to new table, reset the sub table start. */
669c123b821SSuanming Mou 			m_start = 0;
670c123b821SSuanming Mou 			e_start = 0;
671c123b821SSuanming Mou 			continue;
672c123b821SSuanming Mou 		}
673c123b821SSuanming Mou 		for (j = m_start; j < MLX5_L3T_MT_SIZE; j++) {
674c123b821SSuanming Mou 			if (!m_tbl->tbl[j]) {
675c123b821SSuanming Mou 				/*
676c123b821SSuanming Mou 				 * Jump to new table, reset the sub table
677c123b821SSuanming Mou 				 * start.
678c123b821SSuanming Mou 				 */
679c123b821SSuanming Mou 				e_start = 0;
680c123b821SSuanming Mou 				continue;
681c123b821SSuanming Mou 			}
682c123b821SSuanming Mou 			e_tbl = m_tbl->tbl[j];
683c123b821SSuanming Mou 			switch (tbl->type) {
684c123b821SSuanming Mou 			case MLX5_L3T_TYPE_WORD:
685c123b821SSuanming Mou 				w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
686c123b821SSuanming Mou 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
687c123b821SSuanming Mou 					if (!w_e_tbl->entry[k].data)
688c123b821SSuanming Mou 						continue;
689c123b821SSuanming Mou 					*pos = (i << MLX5_L3T_GT_OFFSET) |
690c123b821SSuanming Mou 					       (j << MLX5_L3T_MT_OFFSET) | k;
691c123b821SSuanming Mou 					return (void *)&w_e_tbl->entry[k].data;
692c123b821SSuanming Mou 				}
693c123b821SSuanming Mou 				break;
694c123b821SSuanming Mou 			case MLX5_L3T_TYPE_DWORD:
695c123b821SSuanming Mou 				dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
696c123b821SSuanming Mou 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
697c123b821SSuanming Mou 					if (!dw_e_tbl->entry[k].data)
698c123b821SSuanming Mou 						continue;
699c123b821SSuanming Mou 					*pos = (i << MLX5_L3T_GT_OFFSET) |
700c123b821SSuanming Mou 					       (j << MLX5_L3T_MT_OFFSET) | k;
701c123b821SSuanming Mou 					return (void *)&dw_e_tbl->entry[k].data;
702c123b821SSuanming Mou 				}
703c123b821SSuanming Mou 				break;
704c123b821SSuanming Mou 			case MLX5_L3T_TYPE_QWORD:
705c123b821SSuanming Mou 				qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
706c123b821SSuanming Mou 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
707c123b821SSuanming Mou 					if (!qw_e_tbl->entry[k].data)
708c123b821SSuanming Mou 						continue;
709c123b821SSuanming Mou 					*pos = (i << MLX5_L3T_GT_OFFSET) |
710c123b821SSuanming Mou 					       (j << MLX5_L3T_MT_OFFSET) | k;
711c123b821SSuanming Mou 					return (void *)&qw_e_tbl->entry[k].data;
712c123b821SSuanming Mou 				}
713c123b821SSuanming Mou 				break;
714c123b821SSuanming Mou 			default:
715c123b821SSuanming Mou 				ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
716c123b821SSuanming Mou 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
717c123b821SSuanming Mou 					if (!ptr_e_tbl->entry[k].data)
718c123b821SSuanming Mou 						continue;
719c123b821SSuanming Mou 					*pos = (i << MLX5_L3T_GT_OFFSET) |
720c123b821SSuanming Mou 					       (j << MLX5_L3T_MT_OFFSET) | k;
721c123b821SSuanming Mou 					return ptr_e_tbl->entry[k].data;
722c123b821SSuanming Mou 				}
723c123b821SSuanming Mou 				break;
724c123b821SSuanming Mou 			}
725c123b821SSuanming Mou 		}
726c123b821SSuanming Mou 	}
727c123b821SSuanming Mou 	return NULL;
728c123b821SSuanming Mou }
729c123b821SSuanming Mou 
730a3cf59f5SSuanming Mou /*
731a3cf59f5SSuanming Mou  * Macros for linked list based on indexed memory.
732a3cf59f5SSuanming Mou  * Example data structure:
733a3cf59f5SSuanming Mou  * struct Foo {
734a3cf59f5SSuanming Mou  *	ILIST_ENTRY(uint16_t) next;
735a3cf59f5SSuanming Mou  *	...
736a3cf59f5SSuanming Mou  * }
737a3cf59f5SSuanming Mou  *
738a3cf59f5SSuanming Mou  */
739a3cf59f5SSuanming Mou #define ILIST_ENTRY(type)						\
740a3cf59f5SSuanming Mou struct {								\
741a3cf59f5SSuanming Mou 	type prev; /* Index of previous element. */			\
742a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
743a3cf59f5SSuanming Mou }
744a3cf59f5SSuanming Mou 
745a3cf59f5SSuanming Mou #define ILIST_INSERT(pool, head, idx, elem, field)			\
746a3cf59f5SSuanming Mou 	do {								\
747a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
748a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
749a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
750a3cf59f5SSuanming Mou 		(elem)->field.prev = 0;					\
751a3cf59f5SSuanming Mou 		if (*(head)) {						\
752a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get(pool, *(head));		\
753a3cf59f5SSuanming Mou 			if (peer)					\
754a3cf59f5SSuanming Mou 				(peer)->field.prev = (idx);		\
755a3cf59f5SSuanming Mou 		}							\
756a3cf59f5SSuanming Mou 		*(head) = (idx);					\
757a3cf59f5SSuanming Mou 	} while (0)
758a3cf59f5SSuanming Mou 
759a3cf59f5SSuanming Mou #define ILIST_REMOVE(pool, head, idx, elem, field)			\
760a3cf59f5SSuanming Mou 	do {								\
761a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
762a3cf59f5SSuanming Mou 		MLX5_ASSERT(elem);					\
763a3cf59f5SSuanming Mou 		MLX5_ASSERT(head);					\
764a3cf59f5SSuanming Mou 		if ((elem)->field.prev) {				\
765a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
766a3cf59f5SSuanming Mou 				 (pool, (elem)->field.prev);		\
767a3cf59f5SSuanming Mou 			if (peer)					\
768a3cf59f5SSuanming Mou 				(peer)->field.next = (elem)->field.next;\
769a3cf59f5SSuanming Mou 		}							\
770a3cf59f5SSuanming Mou 		if ((elem)->field.next) {				\
771a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
772a3cf59f5SSuanming Mou 				 (pool, (elem)->field.next);		\
773a3cf59f5SSuanming Mou 			if (peer)					\
774a3cf59f5SSuanming Mou 				(peer)->field.prev = (elem)->field.prev;\
775a3cf59f5SSuanming Mou 		}							\
776a3cf59f5SSuanming Mou 		if (*(head) == (idx))					\
777a3cf59f5SSuanming Mou 			*(head) = (elem)->field.next;			\
778a3cf59f5SSuanming Mou 	} while (0)
779a3cf59f5SSuanming Mou 
780a3cf59f5SSuanming Mou #define ILIST_FOREACH(pool, head, idx, elem, field)			\
781a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
782a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
783a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
784a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
785a3cf59f5SSuanming Mou 
786a3cf59f5SSuanming Mou /* Single index list. */
787a3cf59f5SSuanming Mou #define SILIST_ENTRY(type)						\
788a3cf59f5SSuanming Mou struct {								\
789a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
790a3cf59f5SSuanming Mou }
791a3cf59f5SSuanming Mou 
792a3cf59f5SSuanming Mou #define SILIST_INSERT(head, idx, elem, field)				\
793a3cf59f5SSuanming Mou 	do {								\
794a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
795a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
796a3cf59f5SSuanming Mou 		*(head) = (idx);					\
797a3cf59f5SSuanming Mou 	} while (0)
798a3cf59f5SSuanming Mou 
799a3cf59f5SSuanming Mou #define SILIST_FOREACH(pool, head, idx, elem, field)			\
800a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
801a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
802a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
803a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
804a3cf59f5SSuanming Mou 
805c123b821SSuanming Mou #define MLX5_L3T_FOREACH(tbl, idx, entry)				\
806c123b821SSuanming Mou 	for (idx = 0, (entry) = mlx5_l3t_get_next((tbl), &idx);		\
807c123b821SSuanming Mou 	     (entry);							\
808c123b821SSuanming Mou 	     idx++, (entry) = mlx5_l3t_get_next((tbl), &idx))
809c123b821SSuanming Mou 
810771fa900SAdrien Mazarguil #endif /* RTE_PMD_MLX5_UTILS_H_ */
811