xref: /dpdk/drivers/net/mlx5/mlx5_utils.h (revision d15c0946beeafb62c7620214667dace2c19f528c)
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>
2125245d5dSShiri 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. */
21158ecd3adSSuanming Mou 	uint32_t max_idx; /* The maximum index can be allocated. */
212*d15c0946SSuanming Mou 	uint32_t per_core_cache;
213*d15c0946SSuanming Mou 	/*
214*d15c0946SSuanming Mou 	 * Cache entry number per core for performance. Should not be
215*d15c0946SSuanming Mou 	 * set with release_mem_en.
216*d15c0946SSuanming Mou 	 */
217a3cf59f5SSuanming Mou 	const char *type; /* Memory allocate type name. */
21883c2047cSSuanming Mou 	void *(*malloc)(uint32_t flags, size_t size, unsigned int align,
219a3cf59f5SSuanming Mou 			int socket);
220a3cf59f5SSuanming Mou 	/* User defined memory allocator. */
221a3cf59f5SSuanming Mou 	void (*free)(void *addr); /* User defined memory release. */
222a3cf59f5SSuanming Mou };
223a3cf59f5SSuanming Mou 
224a3cf59f5SSuanming Mou struct mlx5_indexed_trunk {
225a3cf59f5SSuanming Mou 	uint32_t idx; /* Trunk id. */
226a3cf59f5SSuanming Mou 	uint32_t prev; /* Previous free trunk in free list. */
227a3cf59f5SSuanming Mou 	uint32_t next; /* Next free trunk in free list. */
228a3cf59f5SSuanming Mou 	uint32_t free; /* Free entries available */
229a3cf59f5SSuanming Mou 	struct rte_bitmap *bmp;
230691b3d3eSSuanming Mou 	uint8_t data[] __rte_cache_aligned; /* Entry data start. */
231a3cf59f5SSuanming Mou };
232a3cf59f5SSuanming Mou 
233*d15c0946SSuanming Mou struct mlx5_indexed_cache {
234*d15c0946SSuanming Mou 	struct mlx5_indexed_trunk **trunks;
235*d15c0946SSuanming Mou 	volatile uint32_t n_trunk_valid; /* Trunks allocated. */
236*d15c0946SSuanming Mou 	uint32_t n_trunk; /* Trunk pointer array size. */
237*d15c0946SSuanming Mou 	uint32_t ref_cnt;
238*d15c0946SSuanming Mou 	uint32_t len;
239*d15c0946SSuanming Mou 	uint32_t idx[];
240*d15c0946SSuanming Mou };
241*d15c0946SSuanming Mou 
242*d15c0946SSuanming Mou struct mlx5_ipool_per_lcore {
243*d15c0946SSuanming Mou 	struct mlx5_indexed_cache *lc;
244*d15c0946SSuanming Mou 	uint32_t len; /**< Current cache count. */
245*d15c0946SSuanming Mou 	uint32_t idx[]; /**< Cache objects. */
246*d15c0946SSuanming Mou };
247*d15c0946SSuanming Mou 
248a3cf59f5SSuanming Mou struct mlx5_indexed_pool {
249a3cf59f5SSuanming Mou 	struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
250*d15c0946SSuanming Mou 	rte_spinlock_t rsz_lock; /* Pool lock for multiple thread usage. */
251*d15c0946SSuanming Mou 	/* Dim of trunk pointer array. */
252*d15c0946SSuanming Mou 	union {
253*d15c0946SSuanming Mou 		struct {
254a3cf59f5SSuanming Mou 			uint32_t n_trunk_valid; /* Trunks allocated. */
255a3cf59f5SSuanming Mou 			uint32_t n_trunk; /* Trunk pointer array size. */
256a3cf59f5SSuanming Mou 			struct mlx5_indexed_trunk **trunks;
257a3cf59f5SSuanming Mou 			uint32_t free_list; /* Index to first free trunk. */
258*d15c0946SSuanming Mou 		};
259*d15c0946SSuanming Mou 		struct {
260*d15c0946SSuanming Mou 			struct mlx5_indexed_cache *gc;
261*d15c0946SSuanming Mou 			/* Global cache. */
262*d15c0946SSuanming Mou 			struct mlx5_ipool_per_lcore *cache[RTE_MAX_LCORE];
263*d15c0946SSuanming Mou 			/* Local cache. */
264*d15c0946SSuanming Mou 		};
265*d15c0946SSuanming Mou 	};
266a3cf59f5SSuanming Mou #ifdef POOL_DEBUG
267a3cf59f5SSuanming Mou 	uint32_t n_entry;
268a3cf59f5SSuanming Mou 	uint32_t trunk_new;
269a3cf59f5SSuanming Mou 	uint32_t trunk_avail;
270a3cf59f5SSuanming Mou 	uint32_t trunk_empty;
271a3cf59f5SSuanming Mou 	uint32_t trunk_free;
272a3cf59f5SSuanming Mou #endif
27362d7d519SSuanming Mou 	uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
274a3cf59f5SSuanming Mou };
275a3cf59f5SSuanming Mou 
276634efbc2SNelio Laranjeiro /**
27746287eacSBing Zhao  * Return logarithm of the nearest power of two above input value.
278634efbc2SNelio Laranjeiro  *
279634efbc2SNelio Laranjeiro  * @param v
280634efbc2SNelio Laranjeiro  *   Input value.
281634efbc2SNelio Laranjeiro  *
282634efbc2SNelio Laranjeiro  * @return
28346287eacSBing Zhao  *   Logarithm of the nearest power of two above input value.
284634efbc2SNelio Laranjeiro  */
285634efbc2SNelio Laranjeiro static inline unsigned int
286634efbc2SNelio Laranjeiro log2above(unsigned int v)
287634efbc2SNelio Laranjeiro {
288634efbc2SNelio Laranjeiro 	unsigned int l;
289634efbc2SNelio Laranjeiro 	unsigned int r;
290634efbc2SNelio Laranjeiro 
291634efbc2SNelio Laranjeiro 	for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
292634efbc2SNelio Laranjeiro 		r |= (v & 1);
293693f715dSHuawei Xie 	return l + r;
294634efbc2SNelio Laranjeiro }
295634efbc2SNelio Laranjeiro 
2961ff37beeSXueming Li /************************ cache list *****************************/
2971ff37beeSXueming Li 
2981ff37beeSXueming Li /** Maximum size of string for naming. */
2991ff37beeSXueming Li #define MLX5_NAME_SIZE			32
3001ff37beeSXueming Li 
3011ff37beeSXueming Li struct mlx5_cache_list;
3021ff37beeSXueming Li 
3031ff37beeSXueming Li /**
3041ff37beeSXueming Li  * Structure of the entry in the cache list, user should define its own struct
3051ff37beeSXueming Li  * that contains this in order to store the data.
3061ff37beeSXueming Li  */
3071ff37beeSXueming Li struct mlx5_cache_entry {
3081ff37beeSXueming Li 	LIST_ENTRY(mlx5_cache_entry) next; /* Entry pointers in the list. */
3091ff37beeSXueming Li 	uint32_t ref_cnt; /* Reference count. */
3101ff37beeSXueming Li };
3111ff37beeSXueming Li 
3121ff37beeSXueming Li /**
3131ff37beeSXueming Li  * Type of callback function for entry removal.
3141ff37beeSXueming Li  *
3151ff37beeSXueming Li  * @param list
3161ff37beeSXueming Li  *   The cache list.
3171ff37beeSXueming Li  * @param entry
3181ff37beeSXueming Li  *   The entry in the list.
3191ff37beeSXueming Li  */
3201ff37beeSXueming Li typedef void (*mlx5_cache_remove_cb)(struct mlx5_cache_list *list,
3211ff37beeSXueming Li 				     struct mlx5_cache_entry *entry);
3221ff37beeSXueming Li 
3231ff37beeSXueming Li /**
3241ff37beeSXueming Li  * Type of function for user defined matching.
3251ff37beeSXueming Li  *
3261ff37beeSXueming Li  * @param list
3271ff37beeSXueming Li  *   The cache list.
3281ff37beeSXueming Li  * @param entry
3291ff37beeSXueming Li  *   The entry in the list.
3301ff37beeSXueming Li  * @param ctx
3311ff37beeSXueming Li  *   The pointer to new entry context.
3321ff37beeSXueming Li  *
3331ff37beeSXueming Li  * @return
3341ff37beeSXueming Li  *   0 if matching, non-zero number otherwise.
3351ff37beeSXueming Li  */
3361ff37beeSXueming Li typedef int (*mlx5_cache_match_cb)(struct mlx5_cache_list *list,
3371ff37beeSXueming Li 				   struct mlx5_cache_entry *entry, void *ctx);
3381ff37beeSXueming Li 
3391ff37beeSXueming Li /**
3401ff37beeSXueming Li  * Type of function for user defined cache list entry creation.
3411ff37beeSXueming Li  *
3421ff37beeSXueming Li  * @param list
3431ff37beeSXueming Li  *   The cache list.
3441ff37beeSXueming Li  * @param entry
3451ff37beeSXueming Li  *   The new allocated entry, NULL if list entry size unspecified,
3461ff37beeSXueming Li  *   New entry has to be allocated in callback and return.
3471ff37beeSXueming Li  * @param ctx
3481ff37beeSXueming Li  *   The pointer to new entry context.
3491ff37beeSXueming Li  *
3501ff37beeSXueming Li  * @return
3511ff37beeSXueming Li  *   Pointer of entry on success, NULL otherwise.
3521ff37beeSXueming Li  */
3531ff37beeSXueming Li typedef struct mlx5_cache_entry *(*mlx5_cache_create_cb)
3541ff37beeSXueming Li 				 (struct mlx5_cache_list *list,
3551ff37beeSXueming Li 				  struct mlx5_cache_entry *entry,
3561ff37beeSXueming Li 				  void *ctx);
3571ff37beeSXueming Li 
3581ff37beeSXueming Li /**
3591ff37beeSXueming Li  * Linked cache list structure.
3601ff37beeSXueming Li  *
3611ff37beeSXueming Li  * Entry in cache list could be reused if entry already exists,
3621ff37beeSXueming Li  * reference count will increase and the existing entry returns.
3631ff37beeSXueming Li  *
3641ff37beeSXueming Li  * When destroy an entry from list, decrease reference count and only
3651ff37beeSXueming Li  * destroy when no further reference.
3661ff37beeSXueming Li  *
3671ff37beeSXueming Li  * Linked list cache is designed for limited number of entries cache,
3681ff37beeSXueming Li  * read mostly, less modification.
3691ff37beeSXueming Li  *
3701ff37beeSXueming Li  * For huge amount of entries cache, please consider hash list cache.
3711ff37beeSXueming Li  *
3721ff37beeSXueming Li  */
3731ff37beeSXueming Li struct mlx5_cache_list {
3741ff37beeSXueming Li 	char name[MLX5_NAME_SIZE]; /**< Name of the cache list. */
3751ff37beeSXueming Li 	uint32_t entry_sz; /**< Entry size, 0: use create callback. */
3761ff37beeSXueming Li 	rte_rwlock_t lock; /* read/write lock. */
3771ff37beeSXueming Li 	uint32_t gen_cnt; /* List modification will update generation count. */
3781ff37beeSXueming Li 	uint32_t count; /* number of entries in list. */
3791ff37beeSXueming Li 	void *ctx; /* user objects target to callback. */
3801ff37beeSXueming Li 	mlx5_cache_create_cb cb_create; /**< entry create callback. */
3811ff37beeSXueming Li 	mlx5_cache_match_cb cb_match; /**< entry match callback. */
3821ff37beeSXueming Li 	mlx5_cache_remove_cb cb_remove; /**< entry remove callback. */
3831ff37beeSXueming Li 	LIST_HEAD(mlx5_cache_head, mlx5_cache_entry) head;
3841ff37beeSXueming Li };
3851ff37beeSXueming Li 
3861ff37beeSXueming Li /**
3871ff37beeSXueming Li  * Initialize a cache list.
3881ff37beeSXueming Li  *
3891ff37beeSXueming Li  * @param list
3901ff37beeSXueming Li  *   Pointer to the hast list table.
3911ff37beeSXueming Li  * @param name
3921ff37beeSXueming Li  *   Name of the cache list.
3931ff37beeSXueming Li  * @param entry_size
3941ff37beeSXueming Li  *   Entry size to allocate, 0 to allocate by creation callback.
3951ff37beeSXueming Li  * @param ctx
3961ff37beeSXueming Li  *   Pointer to the list context data.
3971ff37beeSXueming Li  * @param cb_create
3981ff37beeSXueming Li  *   Callback function for entry create.
3991ff37beeSXueming Li  * @param cb_match
4001ff37beeSXueming Li  *   Callback function for entry match.
4011ff37beeSXueming Li  * @param cb_remove
4021ff37beeSXueming Li  *   Callback function for entry remove.
4031ff37beeSXueming Li  * @return
4041ff37beeSXueming Li  *   0 on success, otherwise failure.
4051ff37beeSXueming Li  */
4061ff37beeSXueming Li int mlx5_cache_list_init(struct mlx5_cache_list *list,
4071ff37beeSXueming Li 			 const char *name, uint32_t entry_size, void *ctx,
4081ff37beeSXueming Li 			 mlx5_cache_create_cb cb_create,
4091ff37beeSXueming Li 			 mlx5_cache_match_cb cb_match,
4101ff37beeSXueming Li 			 mlx5_cache_remove_cb cb_remove);
4111ff37beeSXueming Li 
4121ff37beeSXueming Li /**
4131ff37beeSXueming Li  * Search an entry matching the key.
4141ff37beeSXueming Li  *
4151ff37beeSXueming Li  * Result returned might be destroyed by other thread, must use
4161ff37beeSXueming Li  * this function only in main thread.
4171ff37beeSXueming Li  *
4181ff37beeSXueming Li  * @param list
4191ff37beeSXueming Li  *   Pointer to the cache list.
4201ff37beeSXueming Li  * @param ctx
4211ff37beeSXueming Li  *   Common context parameter used by entry callback function.
4221ff37beeSXueming Li  *
4231ff37beeSXueming Li  * @return
4241ff37beeSXueming Li  *   Pointer of the cache entry if found, NULL otherwise.
4251ff37beeSXueming Li  */
4261ff37beeSXueming Li struct mlx5_cache_entry *mlx5_cache_lookup(struct mlx5_cache_list *list,
4271ff37beeSXueming Li 					   void *ctx);
4281ff37beeSXueming Li 
4291ff37beeSXueming Li /**
4301ff37beeSXueming Li  * Reuse or create an entry to the cache list.
4311ff37beeSXueming Li  *
4321ff37beeSXueming Li  * @param list
4331ff37beeSXueming Li  *   Pointer to the hast list table.
4341ff37beeSXueming Li  * @param ctx
4351ff37beeSXueming Li  *   Common context parameter used by callback function.
4361ff37beeSXueming Li  *
4371ff37beeSXueming Li  * @return
4381ff37beeSXueming Li  *   registered entry on success, NULL otherwise
4391ff37beeSXueming Li  */
4401ff37beeSXueming Li struct mlx5_cache_entry *mlx5_cache_register(struct mlx5_cache_list *list,
4411ff37beeSXueming Li 					     void *ctx);
4421ff37beeSXueming Li 
4431ff37beeSXueming Li /**
4441ff37beeSXueming Li  * Remove an entry from the cache list.
4451ff37beeSXueming Li  *
4461ff37beeSXueming Li  * User should guarantee the validity of the entry.
4471ff37beeSXueming Li  *
4481ff37beeSXueming Li  * @param list
4491ff37beeSXueming Li  *   Pointer to the hast list.
4501ff37beeSXueming Li  * @param entry
4511ff37beeSXueming Li  *   Entry to be removed from the cache list table.
4521ff37beeSXueming Li  * @return
4531ff37beeSXueming Li  *   0 on entry removed, 1 on entry still referenced.
4541ff37beeSXueming Li  */
4551ff37beeSXueming Li int mlx5_cache_unregister(struct mlx5_cache_list *list,
4561ff37beeSXueming Li 			  struct mlx5_cache_entry *entry);
4571ff37beeSXueming Li 
4581ff37beeSXueming Li /**
4591ff37beeSXueming Li  * Destroy the cache list.
4601ff37beeSXueming Li  *
4611ff37beeSXueming Li  * @param list
4621ff37beeSXueming Li  *   Pointer to the cache list.
4631ff37beeSXueming Li  */
4641ff37beeSXueming Li void mlx5_cache_list_destroy(struct mlx5_cache_list *list);
4651ff37beeSXueming Li 
4661ff37beeSXueming Li /**
4671ff37beeSXueming Li  * Get entry number from the cache list.
4681ff37beeSXueming Li  *
4691ff37beeSXueming Li  * @param list
4701ff37beeSXueming Li  *   Pointer to the hast list.
4711ff37beeSXueming Li  * @return
4721ff37beeSXueming Li  *   Cache list entry number.
4731ff37beeSXueming Li  */
4741ff37beeSXueming Li uint32_t
4751ff37beeSXueming Li mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list);
4761ff37beeSXueming Li 
4771ff37beeSXueming Li /********************************* indexed pool *************************/
4781ff37beeSXueming Li 
479a3cf59f5SSuanming Mou /**
480a3cf59f5SSuanming Mou  * This function allocates non-initialized memory entry from pool.
481a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
482a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
483a3cf59f5SSuanming Mou  *
484a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
485a3cf59f5SSuanming Mou  *
486a3cf59f5SSuanming Mou  * @param pool
487a3cf59f5SSuanming Mou  *   Pointer to indexed memory entry pool.
488a3cf59f5SSuanming Mou  *   No initialization required.
489a3cf59f5SSuanming Mou  * @param[out] idx
490a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
491a3cf59f5SSuanming Mou  *   Memory index always positive value.
492a3cf59f5SSuanming Mou  * @return
493a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry.
494a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
495a3cf59f5SSuanming Mou  */
496a3cf59f5SSuanming Mou void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
497a3cf59f5SSuanming Mou 
498a3cf59f5SSuanming Mou /**
499a3cf59f5SSuanming Mou  * This function allocates zero initialized memory entry from pool.
500a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
501a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
502a3cf59f5SSuanming Mou  *
503a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
504a3cf59f5SSuanming Mou  *
505a3cf59f5SSuanming Mou  * @param pool
506a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
507a3cf59f5SSuanming Mou  *   No initialization required.
508a3cf59f5SSuanming Mou  * @param[out] idx
509a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
510a3cf59f5SSuanming Mou  *   Memory index always positive value.
511a3cf59f5SSuanming Mou  * @return
512a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry .
513a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
514a3cf59f5SSuanming Mou  */
515a3cf59f5SSuanming Mou void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
516a3cf59f5SSuanming Mou 
517a3cf59f5SSuanming Mou /**
518a3cf59f5SSuanming Mou  * This function frees indexed memory entry to pool.
519a3cf59f5SSuanming Mou  * Caller has to make sure that the index is allocated from same pool.
520a3cf59f5SSuanming Mou  *
521a3cf59f5SSuanming Mou  * @param pool
522a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
523a3cf59f5SSuanming Mou  * @param idx
524a3cf59f5SSuanming Mou  *   Allocated memory entry index.
525a3cf59f5SSuanming Mou  */
526a3cf59f5SSuanming Mou void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
527a3cf59f5SSuanming Mou 
528a3cf59f5SSuanming Mou /**
529a3cf59f5SSuanming Mou  * This function returns pointer of indexed memory entry from index.
530a3cf59f5SSuanming Mou  * Caller has to make sure that the index is valid, and allocated
531a3cf59f5SSuanming Mou  * from same pool.
532a3cf59f5SSuanming Mou  *
533a3cf59f5SSuanming Mou  * @param pool
534a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
535a3cf59f5SSuanming Mou  * @param idx
536a3cf59f5SSuanming Mou  *   Allocated memory index.
537a3cf59f5SSuanming Mou  * @return
538a3cf59f5SSuanming Mou  *   - Pointer to indexed memory entry.
539a3cf59f5SSuanming Mou  */
540a3cf59f5SSuanming Mou void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
541a3cf59f5SSuanming Mou 
542a3cf59f5SSuanming Mou /**
543a3cf59f5SSuanming Mou  * This function creates indexed memory pool.
544a3cf59f5SSuanming Mou  * Caller has to configure the configuration accordingly.
545a3cf59f5SSuanming Mou  *
546a3cf59f5SSuanming Mou  * @param pool
547a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
548a3cf59f5SSuanming Mou  * @param cfg
549a3cf59f5SSuanming Mou  *   Allocated memory index.
550a3cf59f5SSuanming Mou  */
551a3cf59f5SSuanming Mou struct mlx5_indexed_pool *
552a3cf59f5SSuanming Mou mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
553a3cf59f5SSuanming Mou 
554a3cf59f5SSuanming Mou /**
555a3cf59f5SSuanming Mou  * This function releases all resources of pool.
556a3cf59f5SSuanming Mou  * Caller has to make sure that all indexes and memories allocated
557a3cf59f5SSuanming Mou  * from this pool not referenced anymore.
558a3cf59f5SSuanming Mou  *
559a3cf59f5SSuanming Mou  * @param pool
560a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
561a3cf59f5SSuanming Mou  * @return
562a3cf59f5SSuanming Mou  *   - non-zero value on error.
563a3cf59f5SSuanming Mou  *   - 0 on success.
564a3cf59f5SSuanming Mou  */
565a3cf59f5SSuanming Mou int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
566a3cf59f5SSuanming Mou 
567a3cf59f5SSuanming Mou /**
568a3cf59f5SSuanming Mou  * This function dumps debug info of pool.
569a3cf59f5SSuanming Mou  *
570a3cf59f5SSuanming Mou  * @param pool
571a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
572a3cf59f5SSuanming Mou  */
573a3cf59f5SSuanming Mou void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
574a3cf59f5SSuanming Mou 
575bd81eaebSSuanming Mou /**
576*d15c0946SSuanming Mou  * This function flushes all the cache index back to pool trunk.
577*d15c0946SSuanming Mou  *
578*d15c0946SSuanming Mou  * @param pool
579*d15c0946SSuanming Mou  *   Pointer to the index memory pool handler.
580*d15c0946SSuanming Mou  *
581*d15c0946SSuanming Mou  */
582*d15c0946SSuanming Mou 
583*d15c0946SSuanming Mou void mlx5_ipool_flush_cache(struct mlx5_indexed_pool *pool);
584*d15c0946SSuanming Mou 
585*d15c0946SSuanming Mou /**
586*d15c0946SSuanming Mou  * This function gets the available entry from pos.
587*d15c0946SSuanming Mou  *
588*d15c0946SSuanming Mou  * @param pool
589*d15c0946SSuanming Mou  *   Pointer to the index memory pool handler.
590*d15c0946SSuanming Mou  * @param pos
591*d15c0946SSuanming Mou  *   Pointer to the index position start from.
592*d15c0946SSuanming Mou  *
593*d15c0946SSuanming Mou  * @return
594*d15c0946SSuanming Mou  *  - Pointer to the next available entry.
595*d15c0946SSuanming Mou  *
596*d15c0946SSuanming Mou  */
597*d15c0946SSuanming Mou void *mlx5_ipool_get_next(struct mlx5_indexed_pool *pool, uint32_t *pos);
598*d15c0946SSuanming Mou 
599*d15c0946SSuanming Mou /**
600bd81eaebSSuanming Mou  * This function allocates new empty Three-level table.
601bd81eaebSSuanming Mou  *
602bd81eaebSSuanming Mou  * @param type
603bd81eaebSSuanming Mou  *   The l3t can set as word, double word, quad word or pointer with index.
604bd81eaebSSuanming Mou  *
605bd81eaebSSuanming Mou  * @return
606bd81eaebSSuanming Mou  *   - Pointer to the allocated l3t.
607bd81eaebSSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
608bd81eaebSSuanming Mou  */
609bd81eaebSSuanming Mou struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
610bd81eaebSSuanming Mou 
611bd81eaebSSuanming Mou /**
612bd81eaebSSuanming Mou  * This function destroys Three-level table.
613bd81eaebSSuanming Mou  *
614bd81eaebSSuanming Mou  * @param tbl
615bd81eaebSSuanming Mou  *   Pointer to the l3t.
616bd81eaebSSuanming Mou  */
617bd81eaebSSuanming Mou void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
618bd81eaebSSuanming Mou 
619bd81eaebSSuanming Mou /**
620bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
621bd81eaebSSuanming Mou  *
622bd81eaebSSuanming Mou  * @param tbl
623bd81eaebSSuanming Mou  *   Pointer to the l3t.
624bd81eaebSSuanming Mou  * @param idx
625bd81eaebSSuanming Mou  *   Index to the entry.
626bd81eaebSSuanming Mou  * @param data
627bd81eaebSSuanming Mou  *   Pointer to the memory which saves the entry data.
628bd81eaebSSuanming Mou  *   When function call returns 0, data contains the entry data get from
629bd81eaebSSuanming Mou  *   l3t.
630bd81eaebSSuanming Mou  *   When function call returns -1, data is not modified.
631bd81eaebSSuanming Mou  *
632bd81eaebSSuanming Mou  * @return
633bd81eaebSSuanming Mou  *   0 if success, -1 on error.
634bd81eaebSSuanming Mou  */
635bd81eaebSSuanming Mou 
6360796c7b1SSuanming Mou int32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
637bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
638bd81eaebSSuanming Mou 
639bd81eaebSSuanming Mou /**
640bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
641bd81eaebSSuanming Mou  *
6420796c7b1SSuanming Mou  * If the index entry is not available, allocate new one by callback
6430796c7b1SSuanming Mou  * function and fill in the entry.
6440796c7b1SSuanming Mou  *
645bd81eaebSSuanming Mou  * @param tbl
646bd81eaebSSuanming Mou  *   Pointer to the l3t.
647bd81eaebSSuanming Mou  * @param idx
648bd81eaebSSuanming Mou  *   Index to the entry.
649bd81eaebSSuanming Mou  * @param data
6500796c7b1SSuanming Mou  *   Pointer to the memory which saves the entry data.
6510796c7b1SSuanming Mou  *   When function call returns 0, data contains the entry data get from
6520796c7b1SSuanming Mou  *   l3t.
6530796c7b1SSuanming Mou  *   When function call returns -1, data is not modified.
6540796c7b1SSuanming Mou  * @param cb
6550796c7b1SSuanming Mou  *   Callback function to allocate new data.
6560796c7b1SSuanming Mou  * @param ctx
6570796c7b1SSuanming Mou  *   Context for callback function.
658bd81eaebSSuanming Mou  *
659bd81eaebSSuanming Mou  * @return
660bd81eaebSSuanming Mou  *   0 if success, -1 on error.
661bd81eaebSSuanming Mou  */
6620796c7b1SSuanming Mou 
6630796c7b1SSuanming Mou int32_t mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
6640796c7b1SSuanming Mou 			       union mlx5_l3t_data *data,
6650796c7b1SSuanming Mou 			       mlx5_l3t_alloc_callback_fn cb, void *ctx);
6660796c7b1SSuanming Mou 
6670796c7b1SSuanming Mou /**
6680796c7b1SSuanming Mou  * This function decreases and clear index entry if reference
6690796c7b1SSuanming Mou  * counter is 0 from Three-level table.
6700796c7b1SSuanming Mou  *
6710796c7b1SSuanming Mou  * @param tbl
6720796c7b1SSuanming Mou  *   Pointer to the l3t.
6730796c7b1SSuanming Mou  * @param idx
6740796c7b1SSuanming Mou  *   Index to the entry.
6750796c7b1SSuanming Mou  *
6760796c7b1SSuanming Mou  * @return
6770796c7b1SSuanming Mou  *   The remaining reference count, 0 means entry be cleared, -1 on error.
6780796c7b1SSuanming Mou  */
6790796c7b1SSuanming Mou int32_t mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
6800796c7b1SSuanming Mou 
6810796c7b1SSuanming Mou /**
6820796c7b1SSuanming Mou  * This function sets the index entry to Three-level table.
6830796c7b1SSuanming Mou  * If the entry is already set, the EEXIST errno will be given, and
6840796c7b1SSuanming Mou  * the set data will be filled to the data.
6850796c7b1SSuanming Mou  *
6860796c7b1SSuanming Mou  * @param tbl[in]
6870796c7b1SSuanming Mou  *   Pointer to the l3t.
6880796c7b1SSuanming Mou  * @param idx[in]
6890796c7b1SSuanming Mou  *   Index to the entry.
6900796c7b1SSuanming Mou  * @param data[in/out]
6910796c7b1SSuanming Mou  *   Pointer to the memory which contains the entry data save to l3t.
6920796c7b1SSuanming Mou  *   If the entry is already set, the set data will be filled.
6930796c7b1SSuanming Mou  *
6940796c7b1SSuanming Mou  * @return
6950796c7b1SSuanming Mou  *   0 if success, -1 on error.
6960796c7b1SSuanming Mou  */
6970796c7b1SSuanming Mou int32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
698bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
699bd81eaebSSuanming Mou 
700c123b821SSuanming Mou static inline void *
701c123b821SSuanming Mou mlx5_l3t_get_next(struct mlx5_l3t_tbl *tbl, uint32_t *pos)
702c123b821SSuanming Mou {
703c123b821SSuanming Mou 	struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
704c123b821SSuanming Mou 	uint32_t i, j, k, g_start, m_start, e_start;
705c123b821SSuanming Mou 	uint32_t idx = *pos;
706c123b821SSuanming Mou 	void *e_tbl;
707c123b821SSuanming Mou 	struct mlx5_l3t_entry_word *w_e_tbl;
708c123b821SSuanming Mou 	struct mlx5_l3t_entry_dword *dw_e_tbl;
709c123b821SSuanming Mou 	struct mlx5_l3t_entry_qword *qw_e_tbl;
710c123b821SSuanming Mou 	struct mlx5_l3t_entry_ptr *ptr_e_tbl;
711c123b821SSuanming Mou 
712c123b821SSuanming Mou 	if (!tbl)
713c123b821SSuanming Mou 		return NULL;
714c123b821SSuanming Mou 	g_tbl = tbl->tbl;
715c123b821SSuanming Mou 	if (!g_tbl)
716c123b821SSuanming Mou 		return NULL;
717c123b821SSuanming Mou 	g_start = (idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK;
718c123b821SSuanming Mou 	m_start = (idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK;
719c123b821SSuanming Mou 	e_start = idx & MLX5_L3T_ET_MASK;
720c123b821SSuanming Mou 	for (i = g_start; i < MLX5_L3T_GT_SIZE; i++) {
721c123b821SSuanming Mou 		m_tbl = g_tbl->tbl[i];
722c123b821SSuanming Mou 		if (!m_tbl) {
723c123b821SSuanming Mou 			/* Jump to new table, reset the sub table start. */
724c123b821SSuanming Mou 			m_start = 0;
725c123b821SSuanming Mou 			e_start = 0;
726c123b821SSuanming Mou 			continue;
727c123b821SSuanming Mou 		}
728c123b821SSuanming Mou 		for (j = m_start; j < MLX5_L3T_MT_SIZE; j++) {
729c123b821SSuanming Mou 			if (!m_tbl->tbl[j]) {
730c123b821SSuanming Mou 				/*
731c123b821SSuanming Mou 				 * Jump to new table, reset the sub table
732c123b821SSuanming Mou 				 * start.
733c123b821SSuanming Mou 				 */
734c123b821SSuanming Mou 				e_start = 0;
735c123b821SSuanming Mou 				continue;
736c123b821SSuanming Mou 			}
737c123b821SSuanming Mou 			e_tbl = m_tbl->tbl[j];
738c123b821SSuanming Mou 			switch (tbl->type) {
739c123b821SSuanming Mou 			case MLX5_L3T_TYPE_WORD:
740c123b821SSuanming Mou 				w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
741c123b821SSuanming Mou 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
742c123b821SSuanming Mou 					if (!w_e_tbl->entry[k].data)
743c123b821SSuanming Mou 						continue;
744c123b821SSuanming Mou 					*pos = (i << MLX5_L3T_GT_OFFSET) |
745c123b821SSuanming Mou 					       (j << MLX5_L3T_MT_OFFSET) | k;
746c123b821SSuanming Mou 					return (void *)&w_e_tbl->entry[k].data;
747c123b821SSuanming Mou 				}
748c123b821SSuanming Mou 				break;
749c123b821SSuanming Mou 			case MLX5_L3T_TYPE_DWORD:
750c123b821SSuanming Mou 				dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
751c123b821SSuanming Mou 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
752c123b821SSuanming Mou 					if (!dw_e_tbl->entry[k].data)
753c123b821SSuanming Mou 						continue;
754c123b821SSuanming Mou 					*pos = (i << MLX5_L3T_GT_OFFSET) |
755c123b821SSuanming Mou 					       (j << MLX5_L3T_MT_OFFSET) | k;
756c123b821SSuanming Mou 					return (void *)&dw_e_tbl->entry[k].data;
757c123b821SSuanming Mou 				}
758c123b821SSuanming Mou 				break;
759c123b821SSuanming Mou 			case MLX5_L3T_TYPE_QWORD:
760c123b821SSuanming Mou 				qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
761c123b821SSuanming Mou 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
762c123b821SSuanming Mou 					if (!qw_e_tbl->entry[k].data)
763c123b821SSuanming Mou 						continue;
764c123b821SSuanming Mou 					*pos = (i << MLX5_L3T_GT_OFFSET) |
765c123b821SSuanming Mou 					       (j << MLX5_L3T_MT_OFFSET) | k;
766c123b821SSuanming Mou 					return (void *)&qw_e_tbl->entry[k].data;
767c123b821SSuanming Mou 				}
768c123b821SSuanming Mou 				break;
769c123b821SSuanming Mou 			default:
770c123b821SSuanming Mou 				ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
771c123b821SSuanming Mou 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
772c123b821SSuanming Mou 					if (!ptr_e_tbl->entry[k].data)
773c123b821SSuanming Mou 						continue;
774c123b821SSuanming Mou 					*pos = (i << MLX5_L3T_GT_OFFSET) |
775c123b821SSuanming Mou 					       (j << MLX5_L3T_MT_OFFSET) | k;
776c123b821SSuanming Mou 					return ptr_e_tbl->entry[k].data;
777c123b821SSuanming Mou 				}
778c123b821SSuanming Mou 				break;
779c123b821SSuanming Mou 			}
780c123b821SSuanming Mou 		}
781c123b821SSuanming Mou 	}
782c123b821SSuanming Mou 	return NULL;
783c123b821SSuanming Mou }
784c123b821SSuanming Mou 
785a3cf59f5SSuanming Mou /*
786a3cf59f5SSuanming Mou  * Macros for linked list based on indexed memory.
787a3cf59f5SSuanming Mou  * Example data structure:
788a3cf59f5SSuanming Mou  * struct Foo {
789a3cf59f5SSuanming Mou  *	ILIST_ENTRY(uint16_t) next;
790a3cf59f5SSuanming Mou  *	...
791a3cf59f5SSuanming Mou  * }
792a3cf59f5SSuanming Mou  *
793a3cf59f5SSuanming Mou  */
794a3cf59f5SSuanming Mou #define ILIST_ENTRY(type)						\
795a3cf59f5SSuanming Mou struct {								\
796a3cf59f5SSuanming Mou 	type prev; /* Index of previous element. */			\
797a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
798a3cf59f5SSuanming Mou }
799a3cf59f5SSuanming Mou 
800a3cf59f5SSuanming Mou #define ILIST_INSERT(pool, head, idx, elem, field)			\
801a3cf59f5SSuanming Mou 	do {								\
802a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
803a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
804a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
805a3cf59f5SSuanming Mou 		(elem)->field.prev = 0;					\
806a3cf59f5SSuanming Mou 		if (*(head)) {						\
807a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get(pool, *(head));		\
808a3cf59f5SSuanming Mou 			if (peer)					\
809a3cf59f5SSuanming Mou 				(peer)->field.prev = (idx);		\
810a3cf59f5SSuanming Mou 		}							\
811a3cf59f5SSuanming Mou 		*(head) = (idx);					\
812a3cf59f5SSuanming Mou 	} while (0)
813a3cf59f5SSuanming Mou 
814a3cf59f5SSuanming Mou #define ILIST_REMOVE(pool, head, idx, elem, field)			\
815a3cf59f5SSuanming Mou 	do {								\
816a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
817a3cf59f5SSuanming Mou 		MLX5_ASSERT(elem);					\
818a3cf59f5SSuanming Mou 		MLX5_ASSERT(head);					\
819a3cf59f5SSuanming Mou 		if ((elem)->field.prev) {				\
820a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
821a3cf59f5SSuanming Mou 				 (pool, (elem)->field.prev);		\
822a3cf59f5SSuanming Mou 			if (peer)					\
823a3cf59f5SSuanming Mou 				(peer)->field.next = (elem)->field.next;\
824a3cf59f5SSuanming Mou 		}							\
825a3cf59f5SSuanming Mou 		if ((elem)->field.next) {				\
826a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
827a3cf59f5SSuanming Mou 				 (pool, (elem)->field.next);		\
828a3cf59f5SSuanming Mou 			if (peer)					\
829a3cf59f5SSuanming Mou 				(peer)->field.prev = (elem)->field.prev;\
830a3cf59f5SSuanming Mou 		}							\
831a3cf59f5SSuanming Mou 		if (*(head) == (idx))					\
832a3cf59f5SSuanming Mou 			*(head) = (elem)->field.next;			\
833a3cf59f5SSuanming Mou 	} while (0)
834a3cf59f5SSuanming Mou 
835a3cf59f5SSuanming Mou #define ILIST_FOREACH(pool, head, idx, elem, field)			\
836a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
837a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
838a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
839a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
840a3cf59f5SSuanming Mou 
841a3cf59f5SSuanming Mou /* Single index list. */
842a3cf59f5SSuanming Mou #define SILIST_ENTRY(type)						\
843a3cf59f5SSuanming Mou struct {								\
844a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
845a3cf59f5SSuanming Mou }
846a3cf59f5SSuanming Mou 
847a3cf59f5SSuanming Mou #define SILIST_INSERT(head, idx, elem, field)				\
848a3cf59f5SSuanming Mou 	do {								\
849a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
850a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
851a3cf59f5SSuanming Mou 		*(head) = (idx);					\
852a3cf59f5SSuanming Mou 	} while (0)
853a3cf59f5SSuanming Mou 
854a3cf59f5SSuanming Mou #define SILIST_FOREACH(pool, head, idx, elem, field)			\
855a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
856a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
857a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
858a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
859a3cf59f5SSuanming Mou 
860c123b821SSuanming Mou #define MLX5_L3T_FOREACH(tbl, idx, entry)				\
861c123b821SSuanming Mou 	for (idx = 0, (entry) = mlx5_l3t_get_next((tbl), &idx);		\
862c123b821SSuanming Mou 	     (entry);							\
863c123b821SSuanming Mou 	     idx++, (entry) = mlx5_l3t_get_next((tbl), &idx))
864c123b821SSuanming Mou 
865771fa900SAdrien Mazarguil #endif /* RTE_PMD_MLX5_UTILS_H_ */
866