xref: /dpdk/drivers/net/mlx5/mlx5_utils.h (revision 1fd4bb67eb2d0c8a62d642e6ec3b78c0452313e8)
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 
24725f5dd0SThomas Monjalon /*
25725f5dd0SThomas Monjalon  * Compilation workaround for PPC64 when AltiVec is fully enabled, e.g. std=c11.
26725f5dd0SThomas Monjalon  * Otherwise there would be a type conflict between stdbool and altivec.
27725f5dd0SThomas Monjalon  */
28725f5dd0SThomas Monjalon #if defined(__PPC64__) && !defined(__APPLE_ALTIVEC__)
29725f5dd0SThomas Monjalon #undef bool
30725f5dd0SThomas Monjalon /* redefine as in stdbool.h */
31725f5dd0SThomas Monjalon #define bool _Bool
32725f5dd0SThomas Monjalon #endif
33725f5dd0SThomas Monjalon 
34b113cb5eSEdward Makarov /* Convert a bit number to the corresponding 64-bit mask */
35b113cb5eSEdward Makarov #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
36b113cb5eSEdward Makarov 
37771fa900SAdrien Mazarguil /* Save and restore errno around argument evaluation. */
38771fa900SAdrien Mazarguil #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
39771fa900SAdrien Mazarguil 
40a170a30dSNélio Laranjeiro extern int mlx5_logtype;
41a170a30dSNélio Laranjeiro 
42771fa900SAdrien Mazarguil /* Generic printf()-like logging macro with automatic line feed. */
43a170a30dSNélio Laranjeiro #define DRV_LOG(level, ...) \
447b4f1e6bSMatan Azrad 	PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \
45771fa900SAdrien Mazarguil 		__VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
46771fa900SAdrien Mazarguil 		PMD_DRV_LOG_CPAREN)
47771fa900SAdrien Mazarguil 
4880f2d0edSXueming Li #define INFO(...) DRV_LOG(INFO, __VA_ARGS__)
4980f2d0edSXueming Li #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__)
5080f2d0edSXueming Li #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__)
5180f2d0edSXueming Li 
522e22920bSAdrien Mazarguil /* Convenience macros for accessing mbuf fields. */
532e22920bSAdrien Mazarguil #define NEXT(m) ((m)->next)
542e22920bSAdrien Mazarguil #define DATA_LEN(m) ((m)->data_len)
552e22920bSAdrien Mazarguil #define PKT_LEN(m) ((m)->pkt_len)
562e22920bSAdrien Mazarguil #define DATA_OFF(m) ((m)->data_off)
572e22920bSAdrien Mazarguil #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
582e22920bSAdrien Mazarguil #define NB_SEGS(m) ((m)->nb_segs)
592e22920bSAdrien Mazarguil #define PORT(m) ((m)->port)
602e22920bSAdrien Mazarguil 
6167fa62bcSAdrien Mazarguil /* Transpose flags. Useful to convert IBV to DPDK flags. */
6267fa62bcSAdrien Mazarguil #define TRANSPOSE(val, from, to) \
6367fa62bcSAdrien Mazarguil 	(((from) >= (to)) ? \
6467fa62bcSAdrien Mazarguil 	 (((val) & (from)) / ((from) / (to))) : \
6567fa62bcSAdrien Mazarguil 	 (((val) & (from)) * ((to) / (from))))
6667fa62bcSAdrien Mazarguil 
67a3cf59f5SSuanming Mou /*
68a3cf59f5SSuanming Mou  * The indexed memory entry index is made up of trunk index and offset of
69a3cf59f5SSuanming Mou  * the entry in the trunk. Since the entry index is 32 bits, in case user
70a3cf59f5SSuanming Mou  * prefers to have small trunks, user can change the macro below to a big
71a3cf59f5SSuanming Mou  * number which helps the pool contains more trunks with lots of entries
72a3cf59f5SSuanming Mou  * allocated.
73a3cf59f5SSuanming Mou  */
74a3cf59f5SSuanming Mou #define TRUNK_IDX_BITS 16
75a3cf59f5SSuanming Mou #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
76a3cf59f5SSuanming Mou #define TRUNK_INVALID TRUNK_MAX_IDX
77a3cf59f5SSuanming Mou #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
78a3cf59f5SSuanming Mou #ifdef RTE_LIBRTE_MLX5_DEBUG
79a3cf59f5SSuanming Mou #define POOL_DEBUG 1
80a3cf59f5SSuanming Mou #endif
81a3cf59f5SSuanming Mou 
82a3cf59f5SSuanming Mou struct mlx5_indexed_pool_config {
83a3cf59f5SSuanming Mou 	uint32_t size; /* Pool entry size. */
8462d7d519SSuanming Mou 	uint32_t trunk_size:22;
8562d7d519SSuanming Mou 	/*
8662d7d519SSuanming Mou 	 * Trunk entry number. Must be power of 2. It can be increased
8762d7d519SSuanming Mou 	 * if trunk_grow enable. The trunk entry number increases with
8862d7d519SSuanming Mou 	 * left shift grow_shift. Trunks with index are after grow_trunk
8962d7d519SSuanming Mou 	 * will keep the entry number same with the last grow trunk.
9062d7d519SSuanming Mou 	 */
9162d7d519SSuanming Mou 	uint32_t grow_trunk:4;
9262d7d519SSuanming Mou 	/*
9362d7d519SSuanming Mou 	 * Trunks with entry number increase in the pool. Set it to 0
9462d7d519SSuanming Mou 	 * to make the pool works as trunk entry fixed pool. It works
9562d7d519SSuanming Mou 	 * only if grow_shift is not 0.
9662d7d519SSuanming Mou 	 */
9762d7d519SSuanming Mou 	uint32_t grow_shift:4;
9862d7d519SSuanming Mou 	/*
9962d7d519SSuanming Mou 	 * Trunk entry number increase shift value, stop after grow_trunk.
10062d7d519SSuanming Mou 	 * It works only if grow_trunk is not 0.
10162d7d519SSuanming Mou 	 */
10262d7d519SSuanming Mou 	uint32_t need_lock:1;
103a3cf59f5SSuanming Mou 	/* Lock is needed for multiple thread usage. */
104*1fd4bb67SSuanming Mou 	uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
105a3cf59f5SSuanming Mou 	const char *type; /* Memory allocate type name. */
106a3cf59f5SSuanming Mou 	void *(*malloc)(const char *type, size_t size, unsigned int align,
107a3cf59f5SSuanming Mou 			int socket);
108a3cf59f5SSuanming Mou 	/* User defined memory allocator. */
109a3cf59f5SSuanming Mou 	void (*free)(void *addr); /* User defined memory release. */
110a3cf59f5SSuanming Mou };
111a3cf59f5SSuanming Mou 
112a3cf59f5SSuanming Mou struct mlx5_indexed_trunk {
113a3cf59f5SSuanming Mou 	uint32_t idx; /* Trunk id. */
114a3cf59f5SSuanming Mou 	uint32_t prev; /* Previous free trunk in free list. */
115a3cf59f5SSuanming Mou 	uint32_t next; /* Next free trunk in free list. */
116a3cf59f5SSuanming Mou 	uint32_t free; /* Free entries available */
117a3cf59f5SSuanming Mou 	struct rte_bitmap *bmp;
118a3cf59f5SSuanming Mou 	uint8_t data[] __rte_cache_min_aligned; /* Entry data start. */
119a3cf59f5SSuanming Mou };
120a3cf59f5SSuanming Mou 
121a3cf59f5SSuanming Mou struct mlx5_indexed_pool {
122a3cf59f5SSuanming Mou 	struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
123a3cf59f5SSuanming Mou 	rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
124a3cf59f5SSuanming Mou 	uint32_t n_trunk_valid; /* Trunks allocated. */
125a3cf59f5SSuanming Mou 	uint32_t n_trunk; /* Trunk pointer array size. */
126a3cf59f5SSuanming Mou 	/* Dim of trunk pointer array. */
127a3cf59f5SSuanming Mou 	struct mlx5_indexed_trunk **trunks;
128a3cf59f5SSuanming Mou 	uint32_t free_list; /* Index to first free trunk. */
129a3cf59f5SSuanming Mou #ifdef POOL_DEBUG
130a3cf59f5SSuanming Mou 	uint32_t n_entry;
131a3cf59f5SSuanming Mou 	uint32_t trunk_new;
132a3cf59f5SSuanming Mou 	uint32_t trunk_avail;
133a3cf59f5SSuanming Mou 	uint32_t trunk_empty;
134a3cf59f5SSuanming Mou 	uint32_t trunk_free;
135a3cf59f5SSuanming Mou #endif
13662d7d519SSuanming Mou 	uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
137a3cf59f5SSuanming Mou };
138a3cf59f5SSuanming Mou 
139634efbc2SNelio Laranjeiro /**
14046287eacSBing Zhao  * Return logarithm of the nearest power of two above input value.
141634efbc2SNelio Laranjeiro  *
142634efbc2SNelio Laranjeiro  * @param v
143634efbc2SNelio Laranjeiro  *   Input value.
144634efbc2SNelio Laranjeiro  *
145634efbc2SNelio Laranjeiro  * @return
14646287eacSBing Zhao  *   Logarithm of the nearest power of two above input value.
147634efbc2SNelio Laranjeiro  */
148634efbc2SNelio Laranjeiro static inline unsigned int
149634efbc2SNelio Laranjeiro log2above(unsigned int v)
150634efbc2SNelio Laranjeiro {
151634efbc2SNelio Laranjeiro 	unsigned int l;
152634efbc2SNelio Laranjeiro 	unsigned int r;
153634efbc2SNelio Laranjeiro 
154634efbc2SNelio Laranjeiro 	for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
155634efbc2SNelio Laranjeiro 		r |= (v & 1);
156693f715dSHuawei Xie 	return l + r;
157634efbc2SNelio Laranjeiro }
158634efbc2SNelio Laranjeiro 
15946287eacSBing Zhao /** Maximum size of string for naming the hlist table. */
16046287eacSBing Zhao #define MLX5_HLIST_NAMESIZE			32
16146287eacSBing Zhao 
16246287eacSBing Zhao /**
16346287eacSBing Zhao  * Structure of the entry in the hash list, user should define its own struct
16446287eacSBing Zhao  * that contains this in order to store the data. The 'key' is 64-bits right
16546287eacSBing Zhao  * now and its user's responsibility to guarantee there is no collision.
16646287eacSBing Zhao  */
16746287eacSBing Zhao struct mlx5_hlist_entry {
16846287eacSBing Zhao 	LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
16946287eacSBing Zhao 	uint64_t key; /* user defined 'key', could be the hash signature. */
17046287eacSBing Zhao };
17146287eacSBing Zhao 
17246287eacSBing Zhao /** Structure for hash head. */
17346287eacSBing Zhao LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
17446287eacSBing Zhao 
17546287eacSBing Zhao /** Type of function that is used to handle the data before freeing. */
17646287eacSBing Zhao typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
17746287eacSBing Zhao 
17846287eacSBing Zhao /** hash list table structure */
17946287eacSBing Zhao struct mlx5_hlist {
18046287eacSBing Zhao 	char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
18146287eacSBing Zhao 	/**< number of heads, need to be power of 2. */
18246287eacSBing Zhao 	uint32_t table_sz;
18346287eacSBing Zhao 	/**< mask to get the index of the list heads. */
18446287eacSBing Zhao 	uint32_t mask;
18546287eacSBing Zhao 	struct mlx5_hlist_head heads[];	/**< list head arrays. */
18646287eacSBing Zhao };
18746287eacSBing Zhao 
18846287eacSBing Zhao /**
18946287eacSBing Zhao  * Create a hash list table, the user can specify the list heads array size
19046287eacSBing Zhao  * of the table, now the size should be a power of 2 in order to get better
19146287eacSBing Zhao  * distribution for the entries. Each entry is a part of the whole data element
19246287eacSBing Zhao  * and the caller should be responsible for the data element's allocation and
19346287eacSBing Zhao  * cleanup / free. Key of each entry will be calculated with CRC in order to
19446287eacSBing Zhao  * generate a little fairer distribution.
19546287eacSBing Zhao  *
19646287eacSBing Zhao  * @param name
19746287eacSBing Zhao  *   Name of the hash list(optional).
19846287eacSBing Zhao  * @param size
19946287eacSBing Zhao  *   Heads array size of the hash list.
20046287eacSBing Zhao  *
20146287eacSBing Zhao  * @return
20246287eacSBing Zhao  *   Pointer of the hash list table created, NULL on failure.
20346287eacSBing Zhao  */
20446287eacSBing Zhao struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size);
20546287eacSBing Zhao 
20646287eacSBing Zhao /**
20746287eacSBing Zhao  * Search an entry matching the key.
20846287eacSBing Zhao  *
20946287eacSBing Zhao  * @param h
21046287eacSBing Zhao  *   Pointer to the hast list table.
21146287eacSBing Zhao  * @param key
21246287eacSBing Zhao  *   Key for the searching entry.
21346287eacSBing Zhao  *
21446287eacSBing Zhao  * @return
21546287eacSBing Zhao  *   Pointer of the hlist entry if found, NULL otherwise.
21646287eacSBing Zhao  */
21746287eacSBing Zhao struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key);
21846287eacSBing Zhao 
21946287eacSBing Zhao /**
22046287eacSBing Zhao  * Insert an entry to the hash list table, the entry is only part of whole data
22146287eacSBing Zhao  * element and a 64B key is used for matching. User should construct the key or
22246287eacSBing Zhao  * give a calculated hash signature and guarantee there is no collision.
22346287eacSBing Zhao  *
22446287eacSBing Zhao  * @param h
22546287eacSBing Zhao  *   Pointer to the hast list table.
22646287eacSBing Zhao  * @param entry
22746287eacSBing Zhao  *   Entry to be inserted into the hash list table.
22846287eacSBing Zhao  *
22946287eacSBing Zhao  * @return
23046287eacSBing Zhao  *   - zero for success.
23146287eacSBing Zhao  *   - -EEXIST if the entry is already inserted.
23246287eacSBing Zhao  */
23346287eacSBing Zhao int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
23446287eacSBing Zhao 
23546287eacSBing Zhao /**
23646287eacSBing Zhao  * Remove an entry from the hash list table. User should guarantee the validity
23746287eacSBing Zhao  * of the entry.
23846287eacSBing Zhao  *
23946287eacSBing Zhao  * @param h
24046287eacSBing Zhao  *   Pointer to the hast list table. (not used)
24146287eacSBing Zhao  * @param entry
24246287eacSBing Zhao  *   Entry to be removed from the hash list table.
24346287eacSBing Zhao  */
24446287eacSBing Zhao void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused,
24546287eacSBing Zhao 		       struct mlx5_hlist_entry *entry);
24646287eacSBing Zhao 
24746287eacSBing Zhao /**
24846287eacSBing Zhao  * Destroy the hash list table, all the entries already inserted into the lists
24946287eacSBing Zhao  * will be handled by the callback function provided by the user (including
25046287eacSBing Zhao  * free if needed) before the table is freed.
25146287eacSBing Zhao  *
25246287eacSBing Zhao  * @param h
25346287eacSBing Zhao  *   Pointer to the hast list table.
25446287eacSBing Zhao  * @param cb
25546287eacSBing Zhao  *   Callback function for each inserted entry when destroying the hash list.
25646287eacSBing Zhao  * @param ctx
25746287eacSBing Zhao  *   Common context parameter used by callback function for each entry.
25846287eacSBing Zhao  */
25946287eacSBing Zhao void mlx5_hlist_destroy(struct mlx5_hlist *h,
26046287eacSBing Zhao 			mlx5_hlist_destroy_callback_fn cb, void *ctx);
26146287eacSBing Zhao 
262a3cf59f5SSuanming Mou /**
263a3cf59f5SSuanming Mou  * This function allocates non-initialized memory entry from pool.
264a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
265a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
266a3cf59f5SSuanming Mou  *
267a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
268a3cf59f5SSuanming Mou  *
269a3cf59f5SSuanming Mou  * @param pool
270a3cf59f5SSuanming Mou  *   Pointer to indexed memory entry pool.
271a3cf59f5SSuanming Mou  *   No initialization required.
272a3cf59f5SSuanming Mou  * @param[out] idx
273a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
274a3cf59f5SSuanming Mou  *   Memory index always positive value.
275a3cf59f5SSuanming Mou  * @return
276a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry.
277a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
278a3cf59f5SSuanming Mou  */
279a3cf59f5SSuanming Mou void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
280a3cf59f5SSuanming Mou 
281a3cf59f5SSuanming Mou /**
282a3cf59f5SSuanming Mou  * This function allocates zero initialized memory entry from pool.
283a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
284a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
285a3cf59f5SSuanming Mou  *
286a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
287a3cf59f5SSuanming Mou  *
288a3cf59f5SSuanming Mou  * @param pool
289a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
290a3cf59f5SSuanming Mou  *   No initialization required.
291a3cf59f5SSuanming Mou  * @param[out] idx
292a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
293a3cf59f5SSuanming Mou  *   Memory index always positive value.
294a3cf59f5SSuanming Mou  * @return
295a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry .
296a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
297a3cf59f5SSuanming Mou  */
298a3cf59f5SSuanming Mou void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
299a3cf59f5SSuanming Mou 
300a3cf59f5SSuanming Mou /**
301a3cf59f5SSuanming Mou  * This function frees indexed memory entry to pool.
302a3cf59f5SSuanming Mou  * Caller has to make sure that the index is allocated from same pool.
303a3cf59f5SSuanming Mou  *
304a3cf59f5SSuanming Mou  * @param pool
305a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
306a3cf59f5SSuanming Mou  * @param idx
307a3cf59f5SSuanming Mou  *   Allocated memory entry index.
308a3cf59f5SSuanming Mou  */
309a3cf59f5SSuanming Mou void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
310a3cf59f5SSuanming Mou 
311a3cf59f5SSuanming Mou /**
312a3cf59f5SSuanming Mou  * This function returns pointer of indexed memory entry from index.
313a3cf59f5SSuanming Mou  * Caller has to make sure that the index is valid, and allocated
314a3cf59f5SSuanming Mou  * from same pool.
315a3cf59f5SSuanming Mou  *
316a3cf59f5SSuanming Mou  * @param pool
317a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
318a3cf59f5SSuanming Mou  * @param idx
319a3cf59f5SSuanming Mou  *   Allocated memory index.
320a3cf59f5SSuanming Mou  * @return
321a3cf59f5SSuanming Mou  *   - Pointer to indexed memory entry.
322a3cf59f5SSuanming Mou  */
323a3cf59f5SSuanming Mou void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
324a3cf59f5SSuanming Mou 
325a3cf59f5SSuanming Mou /**
326a3cf59f5SSuanming Mou  * This function creates indexed memory pool.
327a3cf59f5SSuanming Mou  * Caller has to configure the configuration accordingly.
328a3cf59f5SSuanming Mou  *
329a3cf59f5SSuanming Mou  * @param pool
330a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
331a3cf59f5SSuanming Mou  * @param cfg
332a3cf59f5SSuanming Mou  *   Allocated memory index.
333a3cf59f5SSuanming Mou  */
334a3cf59f5SSuanming Mou struct mlx5_indexed_pool *
335a3cf59f5SSuanming Mou mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
336a3cf59f5SSuanming Mou 
337a3cf59f5SSuanming Mou /**
338a3cf59f5SSuanming Mou  * This function releases all resources of pool.
339a3cf59f5SSuanming Mou  * Caller has to make sure that all indexes and memories allocated
340a3cf59f5SSuanming Mou  * from this pool not referenced anymore.
341a3cf59f5SSuanming Mou  *
342a3cf59f5SSuanming Mou  * @param pool
343a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
344a3cf59f5SSuanming Mou  * @return
345a3cf59f5SSuanming Mou  *   - non-zero value on error.
346a3cf59f5SSuanming Mou  *   - 0 on success.
347a3cf59f5SSuanming Mou  */
348a3cf59f5SSuanming Mou int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
349a3cf59f5SSuanming Mou 
350a3cf59f5SSuanming Mou /**
351a3cf59f5SSuanming Mou  * This function dumps debug info of pool.
352a3cf59f5SSuanming Mou  *
353a3cf59f5SSuanming Mou  * @param pool
354a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
355a3cf59f5SSuanming Mou  */
356a3cf59f5SSuanming Mou void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
357a3cf59f5SSuanming Mou 
358a3cf59f5SSuanming Mou /*
359a3cf59f5SSuanming Mou  * Macros for linked list based on indexed memory.
360a3cf59f5SSuanming Mou  * Example data structure:
361a3cf59f5SSuanming Mou  * struct Foo {
362a3cf59f5SSuanming Mou  *	ILIST_ENTRY(uint16_t) next;
363a3cf59f5SSuanming Mou  *	...
364a3cf59f5SSuanming Mou  * }
365a3cf59f5SSuanming Mou  *
366a3cf59f5SSuanming Mou  */
367a3cf59f5SSuanming Mou #define ILIST_ENTRY(type)						\
368a3cf59f5SSuanming Mou struct {								\
369a3cf59f5SSuanming Mou 	type prev; /* Index of previous element. */			\
370a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
371a3cf59f5SSuanming Mou }
372a3cf59f5SSuanming Mou 
373a3cf59f5SSuanming Mou #define ILIST_INSERT(pool, head, idx, elem, field)			\
374a3cf59f5SSuanming Mou 	do {								\
375a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
376a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
377a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
378a3cf59f5SSuanming Mou 		(elem)->field.prev = 0;					\
379a3cf59f5SSuanming Mou 		if (*(head)) {						\
380a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get(pool, *(head));		\
381a3cf59f5SSuanming Mou 			if (peer)					\
382a3cf59f5SSuanming Mou 				(peer)->field.prev = (idx);		\
383a3cf59f5SSuanming Mou 		}							\
384a3cf59f5SSuanming Mou 		*(head) = (idx);					\
385a3cf59f5SSuanming Mou 	} while (0)
386a3cf59f5SSuanming Mou 
387a3cf59f5SSuanming Mou #define ILIST_REMOVE(pool, head, idx, elem, field)			\
388a3cf59f5SSuanming Mou 	do {								\
389a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
390a3cf59f5SSuanming Mou 		MLX5_ASSERT(elem);					\
391a3cf59f5SSuanming Mou 		MLX5_ASSERT(head);					\
392a3cf59f5SSuanming Mou 		if ((elem)->field.prev) {				\
393a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
394a3cf59f5SSuanming Mou 				 (pool, (elem)->field.prev);		\
395a3cf59f5SSuanming Mou 			if (peer)					\
396a3cf59f5SSuanming Mou 				(peer)->field.next = (elem)->field.next;\
397a3cf59f5SSuanming Mou 		}							\
398a3cf59f5SSuanming Mou 		if ((elem)->field.next) {				\
399a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
400a3cf59f5SSuanming Mou 				 (pool, (elem)->field.next);		\
401a3cf59f5SSuanming Mou 			if (peer)					\
402a3cf59f5SSuanming Mou 				(peer)->field.prev = (elem)->field.prev;\
403a3cf59f5SSuanming Mou 		}							\
404a3cf59f5SSuanming Mou 		if (*(head) == (idx))					\
405a3cf59f5SSuanming Mou 			*(head) = (elem)->field.next;			\
406a3cf59f5SSuanming Mou 	} while (0)
407a3cf59f5SSuanming Mou 
408a3cf59f5SSuanming Mou #define ILIST_FOREACH(pool, head, idx, elem, field)			\
409a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
410a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
411a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
412a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
413a3cf59f5SSuanming Mou 
414a3cf59f5SSuanming Mou /* Single index list. */
415a3cf59f5SSuanming Mou #define SILIST_ENTRY(type)						\
416a3cf59f5SSuanming Mou struct {								\
417a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
418a3cf59f5SSuanming Mou }
419a3cf59f5SSuanming Mou 
420a3cf59f5SSuanming Mou #define SILIST_INSERT(head, idx, elem, field)				\
421a3cf59f5SSuanming Mou 	do {								\
422a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
423a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
424a3cf59f5SSuanming Mou 		*(head) = (idx);					\
425a3cf59f5SSuanming Mou 	} while (0)
426a3cf59f5SSuanming Mou 
427a3cf59f5SSuanming Mou #define SILIST_FOREACH(pool, head, idx, elem, field)			\
428a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
429a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
430a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
431a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
432a3cf59f5SSuanming Mou 
433771fa900SAdrien Mazarguil #endif /* RTE_PMD_MLX5_UTILS_H_ */
434