xref: /dpdk/drivers/net/mlx5/mlx5_utils.h (revision bd81eaebd97ce33df3e23290f190e8dba1814f28)
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>
16a3cf59f5SSuanming Mou #include <rte_memory.h>
17a3cf59f5SSuanming Mou #include <rte_bitmap.h>
18a3cf59f5SSuanming Mou 
197b4f1e6bSMatan Azrad #include <mlx5_common.h>
207b4f1e6bSMatan Azrad 
21771fa900SAdrien Mazarguil #include "mlx5_defs.h"
22771fa900SAdrien Mazarguil 
237b4f1e6bSMatan Azrad 
24b113cb5eSEdward Makarov /* Convert a bit number to the corresponding 64-bit mask */
25b113cb5eSEdward Makarov #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
26b113cb5eSEdward Makarov 
27771fa900SAdrien Mazarguil /* Save and restore errno around argument evaluation. */
28771fa900SAdrien Mazarguil #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
29771fa900SAdrien Mazarguil 
30a170a30dSNélio Laranjeiro extern int mlx5_logtype;
31a170a30dSNélio Laranjeiro 
32771fa900SAdrien Mazarguil /* Generic printf()-like logging macro with automatic line feed. */
33a170a30dSNélio Laranjeiro #define DRV_LOG(level, ...) \
347b4f1e6bSMatan Azrad 	PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \
35771fa900SAdrien Mazarguil 		__VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
36771fa900SAdrien Mazarguil 		PMD_DRV_LOG_CPAREN)
37771fa900SAdrien Mazarguil 
3880f2d0edSXueming Li #define INFO(...) DRV_LOG(INFO, __VA_ARGS__)
3980f2d0edSXueming Li #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__)
4080f2d0edSXueming Li #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__)
4180f2d0edSXueming Li 
422e22920bSAdrien Mazarguil /* Convenience macros for accessing mbuf fields. */
432e22920bSAdrien Mazarguil #define NEXT(m) ((m)->next)
442e22920bSAdrien Mazarguil #define DATA_LEN(m) ((m)->data_len)
452e22920bSAdrien Mazarguil #define PKT_LEN(m) ((m)->pkt_len)
462e22920bSAdrien Mazarguil #define DATA_OFF(m) ((m)->data_off)
472e22920bSAdrien Mazarguil #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
482e22920bSAdrien Mazarguil #define NB_SEGS(m) ((m)->nb_segs)
492e22920bSAdrien Mazarguil #define PORT(m) ((m)->port)
502e22920bSAdrien Mazarguil 
5167fa62bcSAdrien Mazarguil /* Transpose flags. Useful to convert IBV to DPDK flags. */
5267fa62bcSAdrien Mazarguil #define TRANSPOSE(val, from, to) \
5367fa62bcSAdrien Mazarguil 	(((from) >= (to)) ? \
5467fa62bcSAdrien Mazarguil 	 (((val) & (from)) / ((from) / (to))) : \
5567fa62bcSAdrien Mazarguil 	 (((val) & (from)) * ((to) / (from))))
5667fa62bcSAdrien Mazarguil 
57a3cf59f5SSuanming Mou /*
58*bd81eaebSSuanming Mou  * For the case which data is linked with sequence increased index, the
59*bd81eaebSSuanming Mou  * array table will be more efficiect than hash table once need to serarch
60*bd81eaebSSuanming Mou  * one data entry in large numbers of entries. Since the traditional hash
61*bd81eaebSSuanming Mou  * tables has fixed table size, when huge numbers of data saved to the hash
62*bd81eaebSSuanming Mou  * table, it also comes lots of hash conflict.
63*bd81eaebSSuanming Mou  *
64*bd81eaebSSuanming Mou  * But simple array table also has fixed size, allocates all the needed
65*bd81eaebSSuanming Mou  * memory at once will waste lots of memory. For the case don't know the
66*bd81eaebSSuanming Mou  * exactly number of entries will be impossible to allocate the array.
67*bd81eaebSSuanming Mou  *
68*bd81eaebSSuanming Mou  * Then the multiple level table helps to balance the two disadvantages.
69*bd81eaebSSuanming Mou  * Allocate a global high level table with sub table entries at first,
70*bd81eaebSSuanming Mou  * the global table contains the sub table entries, and the sub table will
71*bd81eaebSSuanming Mou  * be allocated only once the corresponding index entry need to be saved.
72*bd81eaebSSuanming Mou  * e.g. for up to 32-bits index, three level table with 10-10-12 splitting,
73*bd81eaebSSuanming Mou  * with sequence increased index, the memory grows with every 4K entries.
74*bd81eaebSSuanming Mou  *
75*bd81eaebSSuanming Mou  * The currently implementation introduces 10-10-12 32-bits splitting
76*bd81eaebSSuanming Mou  * Three-Level table to help the cases which have millions of enties to
77*bd81eaebSSuanming Mou  * save. The index entries can be addressed directly by the index, no
78*bd81eaebSSuanming Mou  * search will be needed.q
79*bd81eaebSSuanming Mou  */
80*bd81eaebSSuanming Mou 
81*bd81eaebSSuanming Mou /* L3 table global table define. */
82*bd81eaebSSuanming Mou #define MLX5_L3T_GT_OFFSET 22
83*bd81eaebSSuanming Mou #define MLX5_L3T_GT_SIZE (1 << 10)
84*bd81eaebSSuanming Mou #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1)
85*bd81eaebSSuanming Mou 
86*bd81eaebSSuanming Mou /* L3 table middle table define. */
87*bd81eaebSSuanming Mou #define MLX5_L3T_MT_OFFSET 12
88*bd81eaebSSuanming Mou #define MLX5_L3T_MT_SIZE (1 << 10)
89*bd81eaebSSuanming Mou #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1)
90*bd81eaebSSuanming Mou 
91*bd81eaebSSuanming Mou /* L3 table entry table define. */
92*bd81eaebSSuanming Mou #define MLX5_L3T_ET_OFFSET 0
93*bd81eaebSSuanming Mou #define MLX5_L3T_ET_SIZE (1 << 12)
94*bd81eaebSSuanming Mou #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1)
95*bd81eaebSSuanming Mou 
96*bd81eaebSSuanming Mou /* L3 table type. */
97*bd81eaebSSuanming Mou enum mlx5_l3t_type {
98*bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_WORD = 0,
99*bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_DWORD,
100*bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_QWORD,
101*bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_PTR,
102*bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_MAX,
103*bd81eaebSSuanming Mou };
104*bd81eaebSSuanming Mou 
105*bd81eaebSSuanming Mou struct mlx5_indexed_pool;
106*bd81eaebSSuanming Mou 
107*bd81eaebSSuanming Mou /* Generic data struct. */
108*bd81eaebSSuanming Mou union mlx5_l3t_data {
109*bd81eaebSSuanming Mou 	uint16_t word;
110*bd81eaebSSuanming Mou 	uint32_t dword;
111*bd81eaebSSuanming Mou 	uint64_t qword;
112*bd81eaebSSuanming Mou 	void *ptr;
113*bd81eaebSSuanming Mou };
114*bd81eaebSSuanming Mou 
115*bd81eaebSSuanming Mou /* L3 level table data structure. */
116*bd81eaebSSuanming Mou struct mlx5_l3t_level_tbl {
117*bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
118*bd81eaebSSuanming Mou 	void *tbl[]; /* Table array. */
119*bd81eaebSSuanming Mou };
120*bd81eaebSSuanming Mou 
121*bd81eaebSSuanming Mou /* L3 word entry table data structure. */
122*bd81eaebSSuanming Mou struct mlx5_l3t_entry_word {
123*bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
124*bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
125*bd81eaebSSuanming Mou 	uint16_t entry[]; /* Entry array. */
126*bd81eaebSSuanming Mou };
127*bd81eaebSSuanming Mou 
128*bd81eaebSSuanming Mou /* L3 double word entry table data structure. */
129*bd81eaebSSuanming Mou struct mlx5_l3t_entry_dword {
130*bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
131*bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
132*bd81eaebSSuanming Mou 	uint32_t entry[]; /* Entry array. */
133*bd81eaebSSuanming Mou };
134*bd81eaebSSuanming Mou 
135*bd81eaebSSuanming Mou /* L3 quad word entry table data structure. */
136*bd81eaebSSuanming Mou struct mlx5_l3t_entry_qword {
137*bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
138*bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
139*bd81eaebSSuanming Mou 	uint64_t entry[]; /* Entry array. */
140*bd81eaebSSuanming Mou };
141*bd81eaebSSuanming Mou 
142*bd81eaebSSuanming Mou /* L3 pointer entry table data structure. */
143*bd81eaebSSuanming Mou struct mlx5_l3t_entry_ptr {
144*bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
145*bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
146*bd81eaebSSuanming Mou 	void *entry[]; /* Entry array. */
147*bd81eaebSSuanming Mou };
148*bd81eaebSSuanming Mou 
149*bd81eaebSSuanming Mou /* L3 table data structure. */
150*bd81eaebSSuanming Mou struct mlx5_l3t_tbl {
151*bd81eaebSSuanming Mou 	enum mlx5_l3t_type type; /* Table type. */
152*bd81eaebSSuanming Mou 	struct mlx5_indexed_pool *eip;
153*bd81eaebSSuanming Mou 	/* Table index pool handles. */
154*bd81eaebSSuanming Mou 	struct mlx5_l3t_level_tbl *tbl; /* Global table index. */
155*bd81eaebSSuanming Mou };
156*bd81eaebSSuanming Mou 
157*bd81eaebSSuanming Mou /*
158a3cf59f5SSuanming Mou  * The indexed memory entry index is made up of trunk index and offset of
159a3cf59f5SSuanming Mou  * the entry in the trunk. Since the entry index is 32 bits, in case user
160a3cf59f5SSuanming Mou  * prefers to have small trunks, user can change the macro below to a big
161a3cf59f5SSuanming Mou  * number which helps the pool contains more trunks with lots of entries
162a3cf59f5SSuanming Mou  * allocated.
163a3cf59f5SSuanming Mou  */
164a3cf59f5SSuanming Mou #define TRUNK_IDX_BITS 16
165a3cf59f5SSuanming Mou #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
166a3cf59f5SSuanming Mou #define TRUNK_INVALID TRUNK_MAX_IDX
167a3cf59f5SSuanming Mou #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
168a3cf59f5SSuanming Mou #ifdef RTE_LIBRTE_MLX5_DEBUG
169a3cf59f5SSuanming Mou #define POOL_DEBUG 1
170a3cf59f5SSuanming Mou #endif
171a3cf59f5SSuanming Mou 
172a3cf59f5SSuanming Mou struct mlx5_indexed_pool_config {
173a3cf59f5SSuanming Mou 	uint32_t size; /* Pool entry size. */
17462d7d519SSuanming Mou 	uint32_t trunk_size:22;
17562d7d519SSuanming Mou 	/*
17662d7d519SSuanming Mou 	 * Trunk entry number. Must be power of 2. It can be increased
17762d7d519SSuanming Mou 	 * if trunk_grow enable. The trunk entry number increases with
17862d7d519SSuanming Mou 	 * left shift grow_shift. Trunks with index are after grow_trunk
17962d7d519SSuanming Mou 	 * will keep the entry number same with the last grow trunk.
18062d7d519SSuanming Mou 	 */
18162d7d519SSuanming Mou 	uint32_t grow_trunk:4;
18262d7d519SSuanming Mou 	/*
18362d7d519SSuanming Mou 	 * Trunks with entry number increase in the pool. Set it to 0
18462d7d519SSuanming Mou 	 * to make the pool works as trunk entry fixed pool. It works
18562d7d519SSuanming Mou 	 * only if grow_shift is not 0.
18662d7d519SSuanming Mou 	 */
18762d7d519SSuanming Mou 	uint32_t grow_shift:4;
18862d7d519SSuanming Mou 	/*
18962d7d519SSuanming Mou 	 * Trunk entry number increase shift value, stop after grow_trunk.
19062d7d519SSuanming Mou 	 * It works only if grow_trunk is not 0.
19162d7d519SSuanming Mou 	 */
19262d7d519SSuanming Mou 	uint32_t need_lock:1;
193a3cf59f5SSuanming Mou 	/* Lock is needed for multiple thread usage. */
1941fd4bb67SSuanming Mou 	uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
195a3cf59f5SSuanming Mou 	const char *type; /* Memory allocate type name. */
196a3cf59f5SSuanming Mou 	void *(*malloc)(const char *type, size_t size, unsigned int align,
197a3cf59f5SSuanming Mou 			int socket);
198a3cf59f5SSuanming Mou 	/* User defined memory allocator. */
199a3cf59f5SSuanming Mou 	void (*free)(void *addr); /* User defined memory release. */
200a3cf59f5SSuanming Mou };
201a3cf59f5SSuanming Mou 
202a3cf59f5SSuanming Mou struct mlx5_indexed_trunk {
203a3cf59f5SSuanming Mou 	uint32_t idx; /* Trunk id. */
204a3cf59f5SSuanming Mou 	uint32_t prev; /* Previous free trunk in free list. */
205a3cf59f5SSuanming Mou 	uint32_t next; /* Next free trunk in free list. */
206a3cf59f5SSuanming Mou 	uint32_t free; /* Free entries available */
207a3cf59f5SSuanming Mou 	struct rte_bitmap *bmp;
208691b3d3eSSuanming Mou 	uint8_t data[] __rte_cache_aligned; /* Entry data start. */
209a3cf59f5SSuanming Mou };
210a3cf59f5SSuanming Mou 
211a3cf59f5SSuanming Mou struct mlx5_indexed_pool {
212a3cf59f5SSuanming Mou 	struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
213a3cf59f5SSuanming Mou 	rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
214a3cf59f5SSuanming Mou 	uint32_t n_trunk_valid; /* Trunks allocated. */
215a3cf59f5SSuanming Mou 	uint32_t n_trunk; /* Trunk pointer array size. */
216a3cf59f5SSuanming Mou 	/* Dim of trunk pointer array. */
217a3cf59f5SSuanming Mou 	struct mlx5_indexed_trunk **trunks;
218a3cf59f5SSuanming Mou 	uint32_t free_list; /* Index to first free trunk. */
219a3cf59f5SSuanming Mou #ifdef POOL_DEBUG
220a3cf59f5SSuanming Mou 	uint32_t n_entry;
221a3cf59f5SSuanming Mou 	uint32_t trunk_new;
222a3cf59f5SSuanming Mou 	uint32_t trunk_avail;
223a3cf59f5SSuanming Mou 	uint32_t trunk_empty;
224a3cf59f5SSuanming Mou 	uint32_t trunk_free;
225a3cf59f5SSuanming Mou #endif
22662d7d519SSuanming Mou 	uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
227a3cf59f5SSuanming Mou };
228a3cf59f5SSuanming Mou 
229634efbc2SNelio Laranjeiro /**
23046287eacSBing Zhao  * Return logarithm of the nearest power of two above input value.
231634efbc2SNelio Laranjeiro  *
232634efbc2SNelio Laranjeiro  * @param v
233634efbc2SNelio Laranjeiro  *   Input value.
234634efbc2SNelio Laranjeiro  *
235634efbc2SNelio Laranjeiro  * @return
23646287eacSBing Zhao  *   Logarithm of the nearest power of two above input value.
237634efbc2SNelio Laranjeiro  */
238634efbc2SNelio Laranjeiro static inline unsigned int
239634efbc2SNelio Laranjeiro log2above(unsigned int v)
240634efbc2SNelio Laranjeiro {
241634efbc2SNelio Laranjeiro 	unsigned int l;
242634efbc2SNelio Laranjeiro 	unsigned int r;
243634efbc2SNelio Laranjeiro 
244634efbc2SNelio Laranjeiro 	for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
245634efbc2SNelio Laranjeiro 		r |= (v & 1);
246693f715dSHuawei Xie 	return l + r;
247634efbc2SNelio Laranjeiro }
248634efbc2SNelio Laranjeiro 
24946287eacSBing Zhao /** Maximum size of string for naming the hlist table. */
25046287eacSBing Zhao #define MLX5_HLIST_NAMESIZE			32
25146287eacSBing Zhao 
25246287eacSBing Zhao /**
25346287eacSBing Zhao  * Structure of the entry in the hash list, user should define its own struct
25446287eacSBing Zhao  * that contains this in order to store the data. The 'key' is 64-bits right
25546287eacSBing Zhao  * now and its user's responsibility to guarantee there is no collision.
25646287eacSBing Zhao  */
25746287eacSBing Zhao struct mlx5_hlist_entry {
25846287eacSBing Zhao 	LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
25946287eacSBing Zhao 	uint64_t key; /* user defined 'key', could be the hash signature. */
26046287eacSBing Zhao };
26146287eacSBing Zhao 
26246287eacSBing Zhao /** Structure for hash head. */
26346287eacSBing Zhao LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
26446287eacSBing Zhao 
26546287eacSBing Zhao /** Type of function that is used to handle the data before freeing. */
26646287eacSBing Zhao typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
26746287eacSBing Zhao 
26846287eacSBing Zhao /** hash list table structure */
26946287eacSBing Zhao struct mlx5_hlist {
27046287eacSBing Zhao 	char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
27146287eacSBing Zhao 	/**< number of heads, need to be power of 2. */
27246287eacSBing Zhao 	uint32_t table_sz;
27346287eacSBing Zhao 	/**< mask to get the index of the list heads. */
27446287eacSBing Zhao 	uint32_t mask;
27546287eacSBing Zhao 	struct mlx5_hlist_head heads[];	/**< list head arrays. */
27646287eacSBing Zhao };
27746287eacSBing Zhao 
27846287eacSBing Zhao /**
27946287eacSBing Zhao  * Create a hash list table, the user can specify the list heads array size
28046287eacSBing Zhao  * of the table, now the size should be a power of 2 in order to get better
28146287eacSBing Zhao  * distribution for the entries. Each entry is a part of the whole data element
28246287eacSBing Zhao  * and the caller should be responsible for the data element's allocation and
28346287eacSBing Zhao  * cleanup / free. Key of each entry will be calculated with CRC in order to
28446287eacSBing Zhao  * generate a little fairer distribution.
28546287eacSBing Zhao  *
28646287eacSBing Zhao  * @param name
28746287eacSBing Zhao  *   Name of the hash list(optional).
28846287eacSBing Zhao  * @param size
28946287eacSBing Zhao  *   Heads array size of the hash list.
29046287eacSBing Zhao  *
29146287eacSBing Zhao  * @return
29246287eacSBing Zhao  *   Pointer of the hash list table created, NULL on failure.
29346287eacSBing Zhao  */
29446287eacSBing Zhao struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size);
29546287eacSBing Zhao 
29646287eacSBing Zhao /**
29746287eacSBing Zhao  * Search an entry matching the key.
29846287eacSBing Zhao  *
29946287eacSBing Zhao  * @param h
30046287eacSBing Zhao  *   Pointer to the hast list table.
30146287eacSBing Zhao  * @param key
30246287eacSBing Zhao  *   Key for the searching entry.
30346287eacSBing Zhao  *
30446287eacSBing Zhao  * @return
30546287eacSBing Zhao  *   Pointer of the hlist entry if found, NULL otherwise.
30646287eacSBing Zhao  */
30746287eacSBing Zhao struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key);
30846287eacSBing Zhao 
30946287eacSBing Zhao /**
31046287eacSBing Zhao  * Insert an entry to the hash list table, the entry is only part of whole data
31146287eacSBing Zhao  * element and a 64B key is used for matching. User should construct the key or
31246287eacSBing Zhao  * give a calculated hash signature and guarantee there is no collision.
31346287eacSBing Zhao  *
31446287eacSBing Zhao  * @param h
31546287eacSBing Zhao  *   Pointer to the hast list table.
31646287eacSBing Zhao  * @param entry
31746287eacSBing Zhao  *   Entry to be inserted into the hash list table.
31846287eacSBing Zhao  *
31946287eacSBing Zhao  * @return
32046287eacSBing Zhao  *   - zero for success.
32146287eacSBing Zhao  *   - -EEXIST if the entry is already inserted.
32246287eacSBing Zhao  */
32346287eacSBing Zhao int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
32446287eacSBing Zhao 
32546287eacSBing Zhao /**
32646287eacSBing Zhao  * Remove an entry from the hash list table. User should guarantee the validity
32746287eacSBing Zhao  * of the entry.
32846287eacSBing Zhao  *
32946287eacSBing Zhao  * @param h
33046287eacSBing Zhao  *   Pointer to the hast list table. (not used)
33146287eacSBing Zhao  * @param entry
33246287eacSBing Zhao  *   Entry to be removed from the hash list table.
33346287eacSBing Zhao  */
33446287eacSBing Zhao void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused,
33546287eacSBing Zhao 		       struct mlx5_hlist_entry *entry);
33646287eacSBing Zhao 
33746287eacSBing Zhao /**
33846287eacSBing Zhao  * Destroy the hash list table, all the entries already inserted into the lists
33946287eacSBing Zhao  * will be handled by the callback function provided by the user (including
34046287eacSBing Zhao  * free if needed) before the table is freed.
34146287eacSBing Zhao  *
34246287eacSBing Zhao  * @param h
34346287eacSBing Zhao  *   Pointer to the hast list table.
34446287eacSBing Zhao  * @param cb
34546287eacSBing Zhao  *   Callback function for each inserted entry when destroying the hash list.
34646287eacSBing Zhao  * @param ctx
34746287eacSBing Zhao  *   Common context parameter used by callback function for each entry.
34846287eacSBing Zhao  */
34946287eacSBing Zhao void mlx5_hlist_destroy(struct mlx5_hlist *h,
35046287eacSBing Zhao 			mlx5_hlist_destroy_callback_fn cb, void *ctx);
35146287eacSBing Zhao 
352a3cf59f5SSuanming Mou /**
353a3cf59f5SSuanming Mou  * This function allocates non-initialized memory entry from pool.
354a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
355a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
356a3cf59f5SSuanming Mou  *
357a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
358a3cf59f5SSuanming Mou  *
359a3cf59f5SSuanming Mou  * @param pool
360a3cf59f5SSuanming Mou  *   Pointer to indexed memory entry pool.
361a3cf59f5SSuanming Mou  *   No initialization required.
362a3cf59f5SSuanming Mou  * @param[out] idx
363a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
364a3cf59f5SSuanming Mou  *   Memory index always positive value.
365a3cf59f5SSuanming Mou  * @return
366a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry.
367a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
368a3cf59f5SSuanming Mou  */
369a3cf59f5SSuanming Mou void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
370a3cf59f5SSuanming Mou 
371a3cf59f5SSuanming Mou /**
372a3cf59f5SSuanming Mou  * This function allocates zero initialized memory entry from pool.
373a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
374a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
375a3cf59f5SSuanming Mou  *
376a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
377a3cf59f5SSuanming Mou  *
378a3cf59f5SSuanming Mou  * @param pool
379a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
380a3cf59f5SSuanming Mou  *   No initialization required.
381a3cf59f5SSuanming Mou  * @param[out] idx
382a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
383a3cf59f5SSuanming Mou  *   Memory index always positive value.
384a3cf59f5SSuanming Mou  * @return
385a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry .
386a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
387a3cf59f5SSuanming Mou  */
388a3cf59f5SSuanming Mou void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
389a3cf59f5SSuanming Mou 
390a3cf59f5SSuanming Mou /**
391a3cf59f5SSuanming Mou  * This function frees indexed memory entry to pool.
392a3cf59f5SSuanming Mou  * Caller has to make sure that the index is allocated from same pool.
393a3cf59f5SSuanming Mou  *
394a3cf59f5SSuanming Mou  * @param pool
395a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
396a3cf59f5SSuanming Mou  * @param idx
397a3cf59f5SSuanming Mou  *   Allocated memory entry index.
398a3cf59f5SSuanming Mou  */
399a3cf59f5SSuanming Mou void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
400a3cf59f5SSuanming Mou 
401a3cf59f5SSuanming Mou /**
402a3cf59f5SSuanming Mou  * This function returns pointer of indexed memory entry from index.
403a3cf59f5SSuanming Mou  * Caller has to make sure that the index is valid, and allocated
404a3cf59f5SSuanming Mou  * from same pool.
405a3cf59f5SSuanming Mou  *
406a3cf59f5SSuanming Mou  * @param pool
407a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
408a3cf59f5SSuanming Mou  * @param idx
409a3cf59f5SSuanming Mou  *   Allocated memory index.
410a3cf59f5SSuanming Mou  * @return
411a3cf59f5SSuanming Mou  *   - Pointer to indexed memory entry.
412a3cf59f5SSuanming Mou  */
413a3cf59f5SSuanming Mou void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
414a3cf59f5SSuanming Mou 
415a3cf59f5SSuanming Mou /**
416a3cf59f5SSuanming Mou  * This function creates indexed memory pool.
417a3cf59f5SSuanming Mou  * Caller has to configure the configuration accordingly.
418a3cf59f5SSuanming Mou  *
419a3cf59f5SSuanming Mou  * @param pool
420a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
421a3cf59f5SSuanming Mou  * @param cfg
422a3cf59f5SSuanming Mou  *   Allocated memory index.
423a3cf59f5SSuanming Mou  */
424a3cf59f5SSuanming Mou struct mlx5_indexed_pool *
425a3cf59f5SSuanming Mou mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
426a3cf59f5SSuanming Mou 
427a3cf59f5SSuanming Mou /**
428a3cf59f5SSuanming Mou  * This function releases all resources of pool.
429a3cf59f5SSuanming Mou  * Caller has to make sure that all indexes and memories allocated
430a3cf59f5SSuanming Mou  * from this pool not referenced anymore.
431a3cf59f5SSuanming Mou  *
432a3cf59f5SSuanming Mou  * @param pool
433a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
434a3cf59f5SSuanming Mou  * @return
435a3cf59f5SSuanming Mou  *   - non-zero value on error.
436a3cf59f5SSuanming Mou  *   - 0 on success.
437a3cf59f5SSuanming Mou  */
438a3cf59f5SSuanming Mou int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
439a3cf59f5SSuanming Mou 
440a3cf59f5SSuanming Mou /**
441a3cf59f5SSuanming Mou  * This function dumps debug info of pool.
442a3cf59f5SSuanming Mou  *
443a3cf59f5SSuanming Mou  * @param pool
444a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
445a3cf59f5SSuanming Mou  */
446a3cf59f5SSuanming Mou void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
447a3cf59f5SSuanming Mou 
448*bd81eaebSSuanming Mou /**
449*bd81eaebSSuanming Mou  * This function allocates new empty Three-level table.
450*bd81eaebSSuanming Mou  *
451*bd81eaebSSuanming Mou  * @param type
452*bd81eaebSSuanming Mou  *   The l3t can set as word, double word, quad word or pointer with index.
453*bd81eaebSSuanming Mou  *
454*bd81eaebSSuanming Mou  * @return
455*bd81eaebSSuanming Mou  *   - Pointer to the allocated l3t.
456*bd81eaebSSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
457*bd81eaebSSuanming Mou  */
458*bd81eaebSSuanming Mou struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
459*bd81eaebSSuanming Mou 
460*bd81eaebSSuanming Mou /**
461*bd81eaebSSuanming Mou  * This function destroys Three-level table.
462*bd81eaebSSuanming Mou  *
463*bd81eaebSSuanming Mou  * @param tbl
464*bd81eaebSSuanming Mou  *   Pointer to the l3t.
465*bd81eaebSSuanming Mou  */
466*bd81eaebSSuanming Mou void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
467*bd81eaebSSuanming Mou 
468*bd81eaebSSuanming Mou /**
469*bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
470*bd81eaebSSuanming Mou  *
471*bd81eaebSSuanming Mou  * @param tbl
472*bd81eaebSSuanming Mou  *   Pointer to the l3t.
473*bd81eaebSSuanming Mou  * @param idx
474*bd81eaebSSuanming Mou  *   Index to the entry.
475*bd81eaebSSuanming Mou  * @param data
476*bd81eaebSSuanming Mou  *   Pointer to the memory which saves the entry data.
477*bd81eaebSSuanming Mou  *   When function call returns 0, data contains the entry data get from
478*bd81eaebSSuanming Mou  *   l3t.
479*bd81eaebSSuanming Mou  *   When function call returns -1, data is not modified.
480*bd81eaebSSuanming Mou  *
481*bd81eaebSSuanming Mou  * @return
482*bd81eaebSSuanming Mou  *   0 if success, -1 on error.
483*bd81eaebSSuanming Mou  */
484*bd81eaebSSuanming Mou 
485*bd81eaebSSuanming Mou uint32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
486*bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
487*bd81eaebSSuanming Mou /**
488*bd81eaebSSuanming Mou  * This function clears the index entry from Three-level table.
489*bd81eaebSSuanming Mou  *
490*bd81eaebSSuanming Mou  * @param tbl
491*bd81eaebSSuanming Mou  *   Pointer to the l3t.
492*bd81eaebSSuanming Mou  * @param idx
493*bd81eaebSSuanming Mou  *   Index to the entry.
494*bd81eaebSSuanming Mou  */
495*bd81eaebSSuanming Mou void mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
496*bd81eaebSSuanming Mou 
497*bd81eaebSSuanming Mou /**
498*bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
499*bd81eaebSSuanming Mou  *
500*bd81eaebSSuanming Mou  * @param tbl
501*bd81eaebSSuanming Mou  *   Pointer to the l3t.
502*bd81eaebSSuanming Mou  * @param idx
503*bd81eaebSSuanming Mou  *   Index to the entry.
504*bd81eaebSSuanming Mou  * @param data
505*bd81eaebSSuanming Mou  *   Pointer to the memory which contains the entry data save to l3t.
506*bd81eaebSSuanming Mou  *
507*bd81eaebSSuanming Mou  * @return
508*bd81eaebSSuanming Mou  *   0 if success, -1 on error.
509*bd81eaebSSuanming Mou  */
510*bd81eaebSSuanming Mou uint32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
511*bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
512*bd81eaebSSuanming Mou 
513a3cf59f5SSuanming Mou /*
514a3cf59f5SSuanming Mou  * Macros for linked list based on indexed memory.
515a3cf59f5SSuanming Mou  * Example data structure:
516a3cf59f5SSuanming Mou  * struct Foo {
517a3cf59f5SSuanming Mou  *	ILIST_ENTRY(uint16_t) next;
518a3cf59f5SSuanming Mou  *	...
519a3cf59f5SSuanming Mou  * }
520a3cf59f5SSuanming Mou  *
521a3cf59f5SSuanming Mou  */
522a3cf59f5SSuanming Mou #define ILIST_ENTRY(type)						\
523a3cf59f5SSuanming Mou struct {								\
524a3cf59f5SSuanming Mou 	type prev; /* Index of previous element. */			\
525a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
526a3cf59f5SSuanming Mou }
527a3cf59f5SSuanming Mou 
528a3cf59f5SSuanming Mou #define ILIST_INSERT(pool, head, idx, elem, field)			\
529a3cf59f5SSuanming Mou 	do {								\
530a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
531a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
532a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
533a3cf59f5SSuanming Mou 		(elem)->field.prev = 0;					\
534a3cf59f5SSuanming Mou 		if (*(head)) {						\
535a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get(pool, *(head));		\
536a3cf59f5SSuanming Mou 			if (peer)					\
537a3cf59f5SSuanming Mou 				(peer)->field.prev = (idx);		\
538a3cf59f5SSuanming Mou 		}							\
539a3cf59f5SSuanming Mou 		*(head) = (idx);					\
540a3cf59f5SSuanming Mou 	} while (0)
541a3cf59f5SSuanming Mou 
542a3cf59f5SSuanming Mou #define ILIST_REMOVE(pool, head, idx, elem, field)			\
543a3cf59f5SSuanming Mou 	do {								\
544a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
545a3cf59f5SSuanming Mou 		MLX5_ASSERT(elem);					\
546a3cf59f5SSuanming Mou 		MLX5_ASSERT(head);					\
547a3cf59f5SSuanming Mou 		if ((elem)->field.prev) {				\
548a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
549a3cf59f5SSuanming Mou 				 (pool, (elem)->field.prev);		\
550a3cf59f5SSuanming Mou 			if (peer)					\
551a3cf59f5SSuanming Mou 				(peer)->field.next = (elem)->field.next;\
552a3cf59f5SSuanming Mou 		}							\
553a3cf59f5SSuanming Mou 		if ((elem)->field.next) {				\
554a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
555a3cf59f5SSuanming Mou 				 (pool, (elem)->field.next);		\
556a3cf59f5SSuanming Mou 			if (peer)					\
557a3cf59f5SSuanming Mou 				(peer)->field.prev = (elem)->field.prev;\
558a3cf59f5SSuanming Mou 		}							\
559a3cf59f5SSuanming Mou 		if (*(head) == (idx))					\
560a3cf59f5SSuanming Mou 			*(head) = (elem)->field.next;			\
561a3cf59f5SSuanming Mou 	} while (0)
562a3cf59f5SSuanming Mou 
563a3cf59f5SSuanming Mou #define ILIST_FOREACH(pool, head, idx, elem, field)			\
564a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
565a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
566a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
567a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
568a3cf59f5SSuanming Mou 
569a3cf59f5SSuanming Mou /* Single index list. */
570a3cf59f5SSuanming Mou #define SILIST_ENTRY(type)						\
571a3cf59f5SSuanming Mou struct {								\
572a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
573a3cf59f5SSuanming Mou }
574a3cf59f5SSuanming Mou 
575a3cf59f5SSuanming Mou #define SILIST_INSERT(head, idx, elem, field)				\
576a3cf59f5SSuanming Mou 	do {								\
577a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
578a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
579a3cf59f5SSuanming Mou 		*(head) = (idx);					\
580a3cf59f5SSuanming Mou 	} while (0)
581a3cf59f5SSuanming Mou 
582a3cf59f5SSuanming Mou #define SILIST_FOREACH(pool, head, idx, elem, field)			\
583a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
584a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
585a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
586a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
587a3cf59f5SSuanming Mou 
588771fa900SAdrien Mazarguil #endif /* RTE_PMD_MLX5_UTILS_H_ */
589