xref: /dpdk/drivers/net/mlx5/mlx5_utils.h (revision 691b3d3ebb6acf7f6a88b64690b1f6b319ab7b6d)
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 /*
58a3cf59f5SSuanming Mou  * The indexed memory entry index is made up of trunk index and offset of
59a3cf59f5SSuanming Mou  * the entry in the trunk. Since the entry index is 32 bits, in case user
60a3cf59f5SSuanming Mou  * prefers to have small trunks, user can change the macro below to a big
61a3cf59f5SSuanming Mou  * number which helps the pool contains more trunks with lots of entries
62a3cf59f5SSuanming Mou  * allocated.
63a3cf59f5SSuanming Mou  */
64a3cf59f5SSuanming Mou #define TRUNK_IDX_BITS 16
65a3cf59f5SSuanming Mou #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
66a3cf59f5SSuanming Mou #define TRUNK_INVALID TRUNK_MAX_IDX
67a3cf59f5SSuanming Mou #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
68a3cf59f5SSuanming Mou #ifdef RTE_LIBRTE_MLX5_DEBUG
69a3cf59f5SSuanming Mou #define POOL_DEBUG 1
70a3cf59f5SSuanming Mou #endif
71a3cf59f5SSuanming Mou 
72a3cf59f5SSuanming Mou struct mlx5_indexed_pool_config {
73a3cf59f5SSuanming Mou 	uint32_t size; /* Pool entry size. */
7462d7d519SSuanming Mou 	uint32_t trunk_size:22;
7562d7d519SSuanming Mou 	/*
7662d7d519SSuanming Mou 	 * Trunk entry number. Must be power of 2. It can be increased
7762d7d519SSuanming Mou 	 * if trunk_grow enable. The trunk entry number increases with
7862d7d519SSuanming Mou 	 * left shift grow_shift. Trunks with index are after grow_trunk
7962d7d519SSuanming Mou 	 * will keep the entry number same with the last grow trunk.
8062d7d519SSuanming Mou 	 */
8162d7d519SSuanming Mou 	uint32_t grow_trunk:4;
8262d7d519SSuanming Mou 	/*
8362d7d519SSuanming Mou 	 * Trunks with entry number increase in the pool. Set it to 0
8462d7d519SSuanming Mou 	 * to make the pool works as trunk entry fixed pool. It works
8562d7d519SSuanming Mou 	 * only if grow_shift is not 0.
8662d7d519SSuanming Mou 	 */
8762d7d519SSuanming Mou 	uint32_t grow_shift:4;
8862d7d519SSuanming Mou 	/*
8962d7d519SSuanming Mou 	 * Trunk entry number increase shift value, stop after grow_trunk.
9062d7d519SSuanming Mou 	 * It works only if grow_trunk is not 0.
9162d7d519SSuanming Mou 	 */
9262d7d519SSuanming Mou 	uint32_t need_lock:1;
93a3cf59f5SSuanming Mou 	/* Lock is needed for multiple thread usage. */
941fd4bb67SSuanming Mou 	uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
95a3cf59f5SSuanming Mou 	const char *type; /* Memory allocate type name. */
96a3cf59f5SSuanming Mou 	void *(*malloc)(const char *type, size_t size, unsigned int align,
97a3cf59f5SSuanming Mou 			int socket);
98a3cf59f5SSuanming Mou 	/* User defined memory allocator. */
99a3cf59f5SSuanming Mou 	void (*free)(void *addr); /* User defined memory release. */
100a3cf59f5SSuanming Mou };
101a3cf59f5SSuanming Mou 
102a3cf59f5SSuanming Mou struct mlx5_indexed_trunk {
103a3cf59f5SSuanming Mou 	uint32_t idx; /* Trunk id. */
104a3cf59f5SSuanming Mou 	uint32_t prev; /* Previous free trunk in free list. */
105a3cf59f5SSuanming Mou 	uint32_t next; /* Next free trunk in free list. */
106a3cf59f5SSuanming Mou 	uint32_t free; /* Free entries available */
107a3cf59f5SSuanming Mou 	struct rte_bitmap *bmp;
108*691b3d3eSSuanming Mou 	uint8_t data[] __rte_cache_aligned; /* Entry data start. */
109a3cf59f5SSuanming Mou };
110a3cf59f5SSuanming Mou 
111a3cf59f5SSuanming Mou struct mlx5_indexed_pool {
112a3cf59f5SSuanming Mou 	struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
113a3cf59f5SSuanming Mou 	rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
114a3cf59f5SSuanming Mou 	uint32_t n_trunk_valid; /* Trunks allocated. */
115a3cf59f5SSuanming Mou 	uint32_t n_trunk; /* Trunk pointer array size. */
116a3cf59f5SSuanming Mou 	/* Dim of trunk pointer array. */
117a3cf59f5SSuanming Mou 	struct mlx5_indexed_trunk **trunks;
118a3cf59f5SSuanming Mou 	uint32_t free_list; /* Index to first free trunk. */
119a3cf59f5SSuanming Mou #ifdef POOL_DEBUG
120a3cf59f5SSuanming Mou 	uint32_t n_entry;
121a3cf59f5SSuanming Mou 	uint32_t trunk_new;
122a3cf59f5SSuanming Mou 	uint32_t trunk_avail;
123a3cf59f5SSuanming Mou 	uint32_t trunk_empty;
124a3cf59f5SSuanming Mou 	uint32_t trunk_free;
125a3cf59f5SSuanming Mou #endif
12662d7d519SSuanming Mou 	uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
127a3cf59f5SSuanming Mou };
128a3cf59f5SSuanming Mou 
129634efbc2SNelio Laranjeiro /**
13046287eacSBing Zhao  * Return logarithm of the nearest power of two above input value.
131634efbc2SNelio Laranjeiro  *
132634efbc2SNelio Laranjeiro  * @param v
133634efbc2SNelio Laranjeiro  *   Input value.
134634efbc2SNelio Laranjeiro  *
135634efbc2SNelio Laranjeiro  * @return
13646287eacSBing Zhao  *   Logarithm of the nearest power of two above input value.
137634efbc2SNelio Laranjeiro  */
138634efbc2SNelio Laranjeiro static inline unsigned int
139634efbc2SNelio Laranjeiro log2above(unsigned int v)
140634efbc2SNelio Laranjeiro {
141634efbc2SNelio Laranjeiro 	unsigned int l;
142634efbc2SNelio Laranjeiro 	unsigned int r;
143634efbc2SNelio Laranjeiro 
144634efbc2SNelio Laranjeiro 	for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
145634efbc2SNelio Laranjeiro 		r |= (v & 1);
146693f715dSHuawei Xie 	return l + r;
147634efbc2SNelio Laranjeiro }
148634efbc2SNelio Laranjeiro 
14946287eacSBing Zhao /** Maximum size of string for naming the hlist table. */
15046287eacSBing Zhao #define MLX5_HLIST_NAMESIZE			32
15146287eacSBing Zhao 
15246287eacSBing Zhao /**
15346287eacSBing Zhao  * Structure of the entry in the hash list, user should define its own struct
15446287eacSBing Zhao  * that contains this in order to store the data. The 'key' is 64-bits right
15546287eacSBing Zhao  * now and its user's responsibility to guarantee there is no collision.
15646287eacSBing Zhao  */
15746287eacSBing Zhao struct mlx5_hlist_entry {
15846287eacSBing Zhao 	LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
15946287eacSBing Zhao 	uint64_t key; /* user defined 'key', could be the hash signature. */
16046287eacSBing Zhao };
16146287eacSBing Zhao 
16246287eacSBing Zhao /** Structure for hash head. */
16346287eacSBing Zhao LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
16446287eacSBing Zhao 
16546287eacSBing Zhao /** Type of function that is used to handle the data before freeing. */
16646287eacSBing Zhao typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
16746287eacSBing Zhao 
16846287eacSBing Zhao /** hash list table structure */
16946287eacSBing Zhao struct mlx5_hlist {
17046287eacSBing Zhao 	char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
17146287eacSBing Zhao 	/**< number of heads, need to be power of 2. */
17246287eacSBing Zhao 	uint32_t table_sz;
17346287eacSBing Zhao 	/**< mask to get the index of the list heads. */
17446287eacSBing Zhao 	uint32_t mask;
17546287eacSBing Zhao 	struct mlx5_hlist_head heads[];	/**< list head arrays. */
17646287eacSBing Zhao };
17746287eacSBing Zhao 
17846287eacSBing Zhao /**
17946287eacSBing Zhao  * Create a hash list table, the user can specify the list heads array size
18046287eacSBing Zhao  * of the table, now the size should be a power of 2 in order to get better
18146287eacSBing Zhao  * distribution for the entries. Each entry is a part of the whole data element
18246287eacSBing Zhao  * and the caller should be responsible for the data element's allocation and
18346287eacSBing Zhao  * cleanup / free. Key of each entry will be calculated with CRC in order to
18446287eacSBing Zhao  * generate a little fairer distribution.
18546287eacSBing Zhao  *
18646287eacSBing Zhao  * @param name
18746287eacSBing Zhao  *   Name of the hash list(optional).
18846287eacSBing Zhao  * @param size
18946287eacSBing Zhao  *   Heads array size of the hash list.
19046287eacSBing Zhao  *
19146287eacSBing Zhao  * @return
19246287eacSBing Zhao  *   Pointer of the hash list table created, NULL on failure.
19346287eacSBing Zhao  */
19446287eacSBing Zhao struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size);
19546287eacSBing Zhao 
19646287eacSBing Zhao /**
19746287eacSBing Zhao  * Search an entry matching the key.
19846287eacSBing Zhao  *
19946287eacSBing Zhao  * @param h
20046287eacSBing Zhao  *   Pointer to the hast list table.
20146287eacSBing Zhao  * @param key
20246287eacSBing Zhao  *   Key for the searching entry.
20346287eacSBing Zhao  *
20446287eacSBing Zhao  * @return
20546287eacSBing Zhao  *   Pointer of the hlist entry if found, NULL otherwise.
20646287eacSBing Zhao  */
20746287eacSBing Zhao struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key);
20846287eacSBing Zhao 
20946287eacSBing Zhao /**
21046287eacSBing Zhao  * Insert an entry to the hash list table, the entry is only part of whole data
21146287eacSBing Zhao  * element and a 64B key is used for matching. User should construct the key or
21246287eacSBing Zhao  * give a calculated hash signature and guarantee there is no collision.
21346287eacSBing Zhao  *
21446287eacSBing Zhao  * @param h
21546287eacSBing Zhao  *   Pointer to the hast list table.
21646287eacSBing Zhao  * @param entry
21746287eacSBing Zhao  *   Entry to be inserted into the hash list table.
21846287eacSBing Zhao  *
21946287eacSBing Zhao  * @return
22046287eacSBing Zhao  *   - zero for success.
22146287eacSBing Zhao  *   - -EEXIST if the entry is already inserted.
22246287eacSBing Zhao  */
22346287eacSBing Zhao int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
22446287eacSBing Zhao 
22546287eacSBing Zhao /**
22646287eacSBing Zhao  * Remove an entry from the hash list table. User should guarantee the validity
22746287eacSBing Zhao  * of the entry.
22846287eacSBing Zhao  *
22946287eacSBing Zhao  * @param h
23046287eacSBing Zhao  *   Pointer to the hast list table. (not used)
23146287eacSBing Zhao  * @param entry
23246287eacSBing Zhao  *   Entry to be removed from the hash list table.
23346287eacSBing Zhao  */
23446287eacSBing Zhao void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused,
23546287eacSBing Zhao 		       struct mlx5_hlist_entry *entry);
23646287eacSBing Zhao 
23746287eacSBing Zhao /**
23846287eacSBing Zhao  * Destroy the hash list table, all the entries already inserted into the lists
23946287eacSBing Zhao  * will be handled by the callback function provided by the user (including
24046287eacSBing Zhao  * free if needed) before the table is freed.
24146287eacSBing Zhao  *
24246287eacSBing Zhao  * @param h
24346287eacSBing Zhao  *   Pointer to the hast list table.
24446287eacSBing Zhao  * @param cb
24546287eacSBing Zhao  *   Callback function for each inserted entry when destroying the hash list.
24646287eacSBing Zhao  * @param ctx
24746287eacSBing Zhao  *   Common context parameter used by callback function for each entry.
24846287eacSBing Zhao  */
24946287eacSBing Zhao void mlx5_hlist_destroy(struct mlx5_hlist *h,
25046287eacSBing Zhao 			mlx5_hlist_destroy_callback_fn cb, void *ctx);
25146287eacSBing Zhao 
252a3cf59f5SSuanming Mou /**
253a3cf59f5SSuanming Mou  * This function allocates non-initialized memory entry from pool.
254a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
255a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
256a3cf59f5SSuanming Mou  *
257a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
258a3cf59f5SSuanming Mou  *
259a3cf59f5SSuanming Mou  * @param pool
260a3cf59f5SSuanming Mou  *   Pointer to indexed memory entry pool.
261a3cf59f5SSuanming Mou  *   No initialization required.
262a3cf59f5SSuanming Mou  * @param[out] idx
263a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
264a3cf59f5SSuanming Mou  *   Memory index always positive value.
265a3cf59f5SSuanming Mou  * @return
266a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry.
267a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
268a3cf59f5SSuanming Mou  */
269a3cf59f5SSuanming Mou void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
270a3cf59f5SSuanming Mou 
271a3cf59f5SSuanming Mou /**
272a3cf59f5SSuanming Mou  * This function allocates zero initialized memory entry from pool.
273a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
274a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
275a3cf59f5SSuanming Mou  *
276a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
277a3cf59f5SSuanming Mou  *
278a3cf59f5SSuanming Mou  * @param pool
279a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
280a3cf59f5SSuanming Mou  *   No initialization required.
281a3cf59f5SSuanming Mou  * @param[out] idx
282a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
283a3cf59f5SSuanming Mou  *   Memory index always positive value.
284a3cf59f5SSuanming Mou  * @return
285a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry .
286a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
287a3cf59f5SSuanming Mou  */
288a3cf59f5SSuanming Mou void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
289a3cf59f5SSuanming Mou 
290a3cf59f5SSuanming Mou /**
291a3cf59f5SSuanming Mou  * This function frees indexed memory entry to pool.
292a3cf59f5SSuanming Mou  * Caller has to make sure that the index is allocated from same pool.
293a3cf59f5SSuanming Mou  *
294a3cf59f5SSuanming Mou  * @param pool
295a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
296a3cf59f5SSuanming Mou  * @param idx
297a3cf59f5SSuanming Mou  *   Allocated memory entry index.
298a3cf59f5SSuanming Mou  */
299a3cf59f5SSuanming Mou void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
300a3cf59f5SSuanming Mou 
301a3cf59f5SSuanming Mou /**
302a3cf59f5SSuanming Mou  * This function returns pointer of indexed memory entry from index.
303a3cf59f5SSuanming Mou  * Caller has to make sure that the index is valid, and allocated
304a3cf59f5SSuanming Mou  * from same pool.
305a3cf59f5SSuanming Mou  *
306a3cf59f5SSuanming Mou  * @param pool
307a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
308a3cf59f5SSuanming Mou  * @param idx
309a3cf59f5SSuanming Mou  *   Allocated memory index.
310a3cf59f5SSuanming Mou  * @return
311a3cf59f5SSuanming Mou  *   - Pointer to indexed memory entry.
312a3cf59f5SSuanming Mou  */
313a3cf59f5SSuanming Mou void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
314a3cf59f5SSuanming Mou 
315a3cf59f5SSuanming Mou /**
316a3cf59f5SSuanming Mou  * This function creates indexed memory pool.
317a3cf59f5SSuanming Mou  * Caller has to configure the configuration accordingly.
318a3cf59f5SSuanming Mou  *
319a3cf59f5SSuanming Mou  * @param pool
320a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
321a3cf59f5SSuanming Mou  * @param cfg
322a3cf59f5SSuanming Mou  *   Allocated memory index.
323a3cf59f5SSuanming Mou  */
324a3cf59f5SSuanming Mou struct mlx5_indexed_pool *
325a3cf59f5SSuanming Mou mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
326a3cf59f5SSuanming Mou 
327a3cf59f5SSuanming Mou /**
328a3cf59f5SSuanming Mou  * This function releases all resources of pool.
329a3cf59f5SSuanming Mou  * Caller has to make sure that all indexes and memories allocated
330a3cf59f5SSuanming Mou  * from this pool not referenced anymore.
331a3cf59f5SSuanming Mou  *
332a3cf59f5SSuanming Mou  * @param pool
333a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
334a3cf59f5SSuanming Mou  * @return
335a3cf59f5SSuanming Mou  *   - non-zero value on error.
336a3cf59f5SSuanming Mou  *   - 0 on success.
337a3cf59f5SSuanming Mou  */
338a3cf59f5SSuanming Mou int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
339a3cf59f5SSuanming Mou 
340a3cf59f5SSuanming Mou /**
341a3cf59f5SSuanming Mou  * This function dumps debug info of pool.
342a3cf59f5SSuanming Mou  *
343a3cf59f5SSuanming Mou  * @param pool
344a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
345a3cf59f5SSuanming Mou  */
346a3cf59f5SSuanming Mou void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
347a3cf59f5SSuanming Mou 
348a3cf59f5SSuanming Mou /*
349a3cf59f5SSuanming Mou  * Macros for linked list based on indexed memory.
350a3cf59f5SSuanming Mou  * Example data structure:
351a3cf59f5SSuanming Mou  * struct Foo {
352a3cf59f5SSuanming Mou  *	ILIST_ENTRY(uint16_t) next;
353a3cf59f5SSuanming Mou  *	...
354a3cf59f5SSuanming Mou  * }
355a3cf59f5SSuanming Mou  *
356a3cf59f5SSuanming Mou  */
357a3cf59f5SSuanming Mou #define ILIST_ENTRY(type)						\
358a3cf59f5SSuanming Mou struct {								\
359a3cf59f5SSuanming Mou 	type prev; /* Index of previous element. */			\
360a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
361a3cf59f5SSuanming Mou }
362a3cf59f5SSuanming Mou 
363a3cf59f5SSuanming Mou #define ILIST_INSERT(pool, head, idx, elem, field)			\
364a3cf59f5SSuanming Mou 	do {								\
365a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
366a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
367a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
368a3cf59f5SSuanming Mou 		(elem)->field.prev = 0;					\
369a3cf59f5SSuanming Mou 		if (*(head)) {						\
370a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get(pool, *(head));		\
371a3cf59f5SSuanming Mou 			if (peer)					\
372a3cf59f5SSuanming Mou 				(peer)->field.prev = (idx);		\
373a3cf59f5SSuanming Mou 		}							\
374a3cf59f5SSuanming Mou 		*(head) = (idx);					\
375a3cf59f5SSuanming Mou 	} while (0)
376a3cf59f5SSuanming Mou 
377a3cf59f5SSuanming Mou #define ILIST_REMOVE(pool, head, idx, elem, field)			\
378a3cf59f5SSuanming Mou 	do {								\
379a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
380a3cf59f5SSuanming Mou 		MLX5_ASSERT(elem);					\
381a3cf59f5SSuanming Mou 		MLX5_ASSERT(head);					\
382a3cf59f5SSuanming Mou 		if ((elem)->field.prev) {				\
383a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
384a3cf59f5SSuanming Mou 				 (pool, (elem)->field.prev);		\
385a3cf59f5SSuanming Mou 			if (peer)					\
386a3cf59f5SSuanming Mou 				(peer)->field.next = (elem)->field.next;\
387a3cf59f5SSuanming Mou 		}							\
388a3cf59f5SSuanming Mou 		if ((elem)->field.next) {				\
389a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
390a3cf59f5SSuanming Mou 				 (pool, (elem)->field.next);		\
391a3cf59f5SSuanming Mou 			if (peer)					\
392a3cf59f5SSuanming Mou 				(peer)->field.prev = (elem)->field.prev;\
393a3cf59f5SSuanming Mou 		}							\
394a3cf59f5SSuanming Mou 		if (*(head) == (idx))					\
395a3cf59f5SSuanming Mou 			*(head) = (elem)->field.next;			\
396a3cf59f5SSuanming Mou 	} while (0)
397a3cf59f5SSuanming Mou 
398a3cf59f5SSuanming Mou #define ILIST_FOREACH(pool, head, idx, elem, field)			\
399a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
400a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
401a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
402a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
403a3cf59f5SSuanming Mou 
404a3cf59f5SSuanming Mou /* Single index list. */
405a3cf59f5SSuanming Mou #define SILIST_ENTRY(type)						\
406a3cf59f5SSuanming Mou struct {								\
407a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
408a3cf59f5SSuanming Mou }
409a3cf59f5SSuanming Mou 
410a3cf59f5SSuanming Mou #define SILIST_INSERT(head, idx, elem, field)				\
411a3cf59f5SSuanming Mou 	do {								\
412a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
413a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
414a3cf59f5SSuanming Mou 		*(head) = (idx);					\
415a3cf59f5SSuanming Mou 	} while (0)
416a3cf59f5SSuanming Mou 
417a3cf59f5SSuanming Mou #define SILIST_FOREACH(pool, head, idx, elem, field)			\
418a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
419a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
420a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
421a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
422a3cf59f5SSuanming Mou 
423771fa900SAdrien Mazarguil #endif /* RTE_PMD_MLX5_UTILS_H_ */
424