xref: /dpdk/drivers/net/mlx5/mlx5_utils.h (revision 1ff37bee32579fddd4c8036c2b01d440db3e3d1b)
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>
217b4f1e6bSMatan Azrad 
22771fa900SAdrien Mazarguil #include "mlx5_defs.h"
23771fa900SAdrien Mazarguil 
24e69a5922SXueming Li #define mlx5_hlist_remove(h, e) \
25e69a5922SXueming Li 	mlx5_hlist_unregister(h, e)
26e69a5922SXueming Li 
27e69a5922SXueming Li #define mlx5_hlist_insert(h, e) \
28e69a5922SXueming Li 	mlx5_hlist_register(h, 0, e)
297b4f1e6bSMatan Azrad 
30b113cb5eSEdward Makarov /* Convert a bit number to the corresponding 64-bit mask */
31b113cb5eSEdward Makarov #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
32b113cb5eSEdward Makarov 
33771fa900SAdrien Mazarguil /* Save and restore errno around argument evaluation. */
34771fa900SAdrien Mazarguil #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
35771fa900SAdrien Mazarguil 
36a170a30dSNélio Laranjeiro extern int mlx5_logtype;
37a170a30dSNélio Laranjeiro 
38771fa900SAdrien Mazarguil /* Generic printf()-like logging macro with automatic line feed. */
39a170a30dSNélio Laranjeiro #define DRV_LOG(level, ...) \
407b4f1e6bSMatan Azrad 	PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \
41771fa900SAdrien Mazarguil 		__VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
42771fa900SAdrien Mazarguil 		PMD_DRV_LOG_CPAREN)
43771fa900SAdrien Mazarguil 
442e22920bSAdrien Mazarguil /* Convenience macros for accessing mbuf fields. */
452e22920bSAdrien Mazarguil #define NEXT(m) ((m)->next)
462e22920bSAdrien Mazarguil #define DATA_LEN(m) ((m)->data_len)
472e22920bSAdrien Mazarguil #define PKT_LEN(m) ((m)->pkt_len)
482e22920bSAdrien Mazarguil #define DATA_OFF(m) ((m)->data_off)
492e22920bSAdrien Mazarguil #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
502e22920bSAdrien Mazarguil #define NB_SEGS(m) ((m)->nb_segs)
512e22920bSAdrien Mazarguil #define PORT(m) ((m)->port)
522e22920bSAdrien Mazarguil 
5367fa62bcSAdrien Mazarguil /* Transpose flags. Useful to convert IBV to DPDK flags. */
5467fa62bcSAdrien Mazarguil #define TRANSPOSE(val, from, to) \
5567fa62bcSAdrien Mazarguil 	(((from) >= (to)) ? \
5667fa62bcSAdrien Mazarguil 	 (((val) & (from)) / ((from) / (to))) : \
5767fa62bcSAdrien Mazarguil 	 (((val) & (from)) * ((to) / (from))))
5867fa62bcSAdrien Mazarguil 
59a3cf59f5SSuanming Mou /*
60bd81eaebSSuanming Mou  * For the case which data is linked with sequence increased index, the
61bd81eaebSSuanming Mou  * array table will be more efficiect than hash table once need to serarch
62bd81eaebSSuanming Mou  * one data entry in large numbers of entries. Since the traditional hash
63bd81eaebSSuanming Mou  * tables has fixed table size, when huge numbers of data saved to the hash
64bd81eaebSSuanming Mou  * table, it also comes lots of hash conflict.
65bd81eaebSSuanming Mou  *
66bd81eaebSSuanming Mou  * But simple array table also has fixed size, allocates all the needed
67bd81eaebSSuanming Mou  * memory at once will waste lots of memory. For the case don't know the
68bd81eaebSSuanming Mou  * exactly number of entries will be impossible to allocate the array.
69bd81eaebSSuanming Mou  *
70bd81eaebSSuanming Mou  * Then the multiple level table helps to balance the two disadvantages.
71bd81eaebSSuanming Mou  * Allocate a global high level table with sub table entries at first,
72bd81eaebSSuanming Mou  * the global table contains the sub table entries, and the sub table will
73bd81eaebSSuanming Mou  * be allocated only once the corresponding index entry need to be saved.
74bd81eaebSSuanming Mou  * e.g. for up to 32-bits index, three level table with 10-10-12 splitting,
75bd81eaebSSuanming Mou  * with sequence increased index, the memory grows with every 4K entries.
76bd81eaebSSuanming Mou  *
77bd81eaebSSuanming Mou  * The currently implementation introduces 10-10-12 32-bits splitting
78bd81eaebSSuanming Mou  * Three-Level table to help the cases which have millions of enties to
79bd81eaebSSuanming Mou  * save. The index entries can be addressed directly by the index, no
80bd81eaebSSuanming Mou  * search will be needed.q
81bd81eaebSSuanming Mou  */
82bd81eaebSSuanming Mou 
83bd81eaebSSuanming Mou /* L3 table global table define. */
84bd81eaebSSuanming Mou #define MLX5_L3T_GT_OFFSET 22
85bd81eaebSSuanming Mou #define MLX5_L3T_GT_SIZE (1 << 10)
86bd81eaebSSuanming Mou #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1)
87bd81eaebSSuanming Mou 
88bd81eaebSSuanming Mou /* L3 table middle table define. */
89bd81eaebSSuanming Mou #define MLX5_L3T_MT_OFFSET 12
90bd81eaebSSuanming Mou #define MLX5_L3T_MT_SIZE (1 << 10)
91bd81eaebSSuanming Mou #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1)
92bd81eaebSSuanming Mou 
93bd81eaebSSuanming Mou /* L3 table entry table define. */
94bd81eaebSSuanming Mou #define MLX5_L3T_ET_OFFSET 0
95bd81eaebSSuanming Mou #define MLX5_L3T_ET_SIZE (1 << 12)
96bd81eaebSSuanming Mou #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1)
97bd81eaebSSuanming Mou 
98bd81eaebSSuanming Mou /* L3 table type. */
99bd81eaebSSuanming Mou enum mlx5_l3t_type {
100bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_WORD = 0,
101bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_DWORD,
102bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_QWORD,
103bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_PTR,
104bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_MAX,
105bd81eaebSSuanming Mou };
106bd81eaebSSuanming Mou 
107bd81eaebSSuanming Mou struct mlx5_indexed_pool;
108bd81eaebSSuanming Mou 
109bd81eaebSSuanming Mou /* Generic data struct. */
110bd81eaebSSuanming Mou union mlx5_l3t_data {
111bd81eaebSSuanming Mou 	uint16_t word;
112bd81eaebSSuanming Mou 	uint32_t dword;
113bd81eaebSSuanming Mou 	uint64_t qword;
114bd81eaebSSuanming Mou 	void *ptr;
115bd81eaebSSuanming Mou };
116bd81eaebSSuanming Mou 
117bd81eaebSSuanming Mou /* L3 level table data structure. */
118bd81eaebSSuanming Mou struct mlx5_l3t_level_tbl {
119bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
120bd81eaebSSuanming Mou 	void *tbl[]; /* Table array. */
121bd81eaebSSuanming Mou };
122bd81eaebSSuanming Mou 
123bd81eaebSSuanming Mou /* L3 word entry table data structure. */
124bd81eaebSSuanming Mou struct mlx5_l3t_entry_word {
125bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
126bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1270796c7b1SSuanming Mou 	struct {
1280796c7b1SSuanming Mou 		uint16_t data;
1290796c7b1SSuanming Mou 		uint32_t ref_cnt;
1300796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1310796c7b1SSuanming Mou } __rte_packed;
132bd81eaebSSuanming Mou 
133bd81eaebSSuanming Mou /* L3 double word entry table data structure. */
134bd81eaebSSuanming Mou struct mlx5_l3t_entry_dword {
135bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
136bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1370796c7b1SSuanming Mou 	struct {
1380796c7b1SSuanming Mou 		uint32_t data;
1390796c7b1SSuanming Mou 		int32_t ref_cnt;
1400796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1410796c7b1SSuanming Mou } __rte_packed;
142bd81eaebSSuanming Mou 
143bd81eaebSSuanming Mou /* L3 quad word entry table data structure. */
144bd81eaebSSuanming Mou struct mlx5_l3t_entry_qword {
145bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
146bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1470796c7b1SSuanming Mou 	struct {
1480796c7b1SSuanming Mou 		uint64_t data;
1490796c7b1SSuanming Mou 		uint32_t ref_cnt;
1500796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1510796c7b1SSuanming Mou } __rte_packed;
152bd81eaebSSuanming Mou 
153bd81eaebSSuanming Mou /* L3 pointer entry table data structure. */
154bd81eaebSSuanming Mou struct mlx5_l3t_entry_ptr {
155bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
156bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1570796c7b1SSuanming Mou 	struct {
1580796c7b1SSuanming Mou 		void *data;
1590796c7b1SSuanming Mou 		uint32_t ref_cnt;
1600796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1610796c7b1SSuanming Mou } __rte_packed;
162bd81eaebSSuanming Mou 
163bd81eaebSSuanming Mou /* L3 table data structure. */
164bd81eaebSSuanming Mou struct mlx5_l3t_tbl {
165bd81eaebSSuanming Mou 	enum mlx5_l3t_type type; /* Table type. */
166bd81eaebSSuanming Mou 	struct mlx5_indexed_pool *eip;
167bd81eaebSSuanming Mou 	/* Table index pool handles. */
168bd81eaebSSuanming Mou 	struct mlx5_l3t_level_tbl *tbl; /* Global table index. */
1690796c7b1SSuanming Mou 	rte_spinlock_t sl; /* The table lock. */
170bd81eaebSSuanming Mou };
171bd81eaebSSuanming Mou 
1720796c7b1SSuanming Mou /** Type of function that is used to handle the data before freeing. */
1730796c7b1SSuanming Mou typedef int32_t (*mlx5_l3t_alloc_callback_fn)(void *ctx,
1740796c7b1SSuanming Mou 					   union mlx5_l3t_data *data);
1750796c7b1SSuanming Mou 
176bd81eaebSSuanming Mou /*
177a3cf59f5SSuanming Mou  * The indexed memory entry index is made up of trunk index and offset of
178a3cf59f5SSuanming Mou  * the entry in the trunk. Since the entry index is 32 bits, in case user
179a3cf59f5SSuanming Mou  * prefers to have small trunks, user can change the macro below to a big
180a3cf59f5SSuanming Mou  * number which helps the pool contains more trunks with lots of entries
181a3cf59f5SSuanming Mou  * allocated.
182a3cf59f5SSuanming Mou  */
183a3cf59f5SSuanming Mou #define TRUNK_IDX_BITS 16
184a3cf59f5SSuanming Mou #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
185a3cf59f5SSuanming Mou #define TRUNK_INVALID TRUNK_MAX_IDX
186a3cf59f5SSuanming Mou #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
187a3cf59f5SSuanming Mou #ifdef RTE_LIBRTE_MLX5_DEBUG
188a3cf59f5SSuanming Mou #define POOL_DEBUG 1
189a3cf59f5SSuanming Mou #endif
190a3cf59f5SSuanming Mou 
191a3cf59f5SSuanming Mou struct mlx5_indexed_pool_config {
192a3cf59f5SSuanming Mou 	uint32_t size; /* Pool entry size. */
19362d7d519SSuanming Mou 	uint32_t trunk_size:22;
19462d7d519SSuanming Mou 	/*
19562d7d519SSuanming Mou 	 * Trunk entry number. Must be power of 2. It can be increased
19662d7d519SSuanming Mou 	 * if trunk_grow enable. The trunk entry number increases with
19762d7d519SSuanming Mou 	 * left shift grow_shift. Trunks with index are after grow_trunk
19862d7d519SSuanming Mou 	 * will keep the entry number same with the last grow trunk.
19962d7d519SSuanming Mou 	 */
20062d7d519SSuanming Mou 	uint32_t grow_trunk:4;
20162d7d519SSuanming Mou 	/*
20262d7d519SSuanming Mou 	 * Trunks with entry number increase in the pool. Set it to 0
20362d7d519SSuanming Mou 	 * to make the pool works as trunk entry fixed pool. It works
20462d7d519SSuanming Mou 	 * only if grow_shift is not 0.
20562d7d519SSuanming Mou 	 */
20662d7d519SSuanming Mou 	uint32_t grow_shift:4;
20762d7d519SSuanming Mou 	/*
20862d7d519SSuanming Mou 	 * Trunk entry number increase shift value, stop after grow_trunk.
20962d7d519SSuanming Mou 	 * It works only if grow_trunk is not 0.
21062d7d519SSuanming Mou 	 */
21162d7d519SSuanming Mou 	uint32_t need_lock:1;
212a3cf59f5SSuanming Mou 	/* Lock is needed for multiple thread usage. */
2131fd4bb67SSuanming Mou 	uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
214a3cf59f5SSuanming Mou 	const char *type; /* Memory allocate type name. */
21583c2047cSSuanming Mou 	void *(*malloc)(uint32_t flags, size_t size, unsigned int align,
216a3cf59f5SSuanming Mou 			int socket);
217a3cf59f5SSuanming Mou 	/* User defined memory allocator. */
218a3cf59f5SSuanming Mou 	void (*free)(void *addr); /* User defined memory release. */
219a3cf59f5SSuanming Mou };
220a3cf59f5SSuanming Mou 
221a3cf59f5SSuanming Mou struct mlx5_indexed_trunk {
222a3cf59f5SSuanming Mou 	uint32_t idx; /* Trunk id. */
223a3cf59f5SSuanming Mou 	uint32_t prev; /* Previous free trunk in free list. */
224a3cf59f5SSuanming Mou 	uint32_t next; /* Next free trunk in free list. */
225a3cf59f5SSuanming Mou 	uint32_t free; /* Free entries available */
226a3cf59f5SSuanming Mou 	struct rte_bitmap *bmp;
227691b3d3eSSuanming Mou 	uint8_t data[] __rte_cache_aligned; /* Entry data start. */
228a3cf59f5SSuanming Mou };
229a3cf59f5SSuanming Mou 
230a3cf59f5SSuanming Mou struct mlx5_indexed_pool {
231a3cf59f5SSuanming Mou 	struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
232a3cf59f5SSuanming Mou 	rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
233a3cf59f5SSuanming Mou 	uint32_t n_trunk_valid; /* Trunks allocated. */
234a3cf59f5SSuanming Mou 	uint32_t n_trunk; /* Trunk pointer array size. */
235a3cf59f5SSuanming Mou 	/* Dim of trunk pointer array. */
236a3cf59f5SSuanming Mou 	struct mlx5_indexed_trunk **trunks;
237a3cf59f5SSuanming Mou 	uint32_t free_list; /* Index to first free trunk. */
238a3cf59f5SSuanming Mou #ifdef POOL_DEBUG
239a3cf59f5SSuanming Mou 	uint32_t n_entry;
240a3cf59f5SSuanming Mou 	uint32_t trunk_new;
241a3cf59f5SSuanming Mou 	uint32_t trunk_avail;
242a3cf59f5SSuanming Mou 	uint32_t trunk_empty;
243a3cf59f5SSuanming Mou 	uint32_t trunk_free;
244a3cf59f5SSuanming Mou #endif
24562d7d519SSuanming Mou 	uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
246a3cf59f5SSuanming Mou };
247a3cf59f5SSuanming Mou 
248634efbc2SNelio Laranjeiro /**
24946287eacSBing Zhao  * Return logarithm of the nearest power of two above input value.
250634efbc2SNelio Laranjeiro  *
251634efbc2SNelio Laranjeiro  * @param v
252634efbc2SNelio Laranjeiro  *   Input value.
253634efbc2SNelio Laranjeiro  *
254634efbc2SNelio Laranjeiro  * @return
25546287eacSBing Zhao  *   Logarithm of the nearest power of two above input value.
256634efbc2SNelio Laranjeiro  */
257634efbc2SNelio Laranjeiro static inline unsigned int
258634efbc2SNelio Laranjeiro log2above(unsigned int v)
259634efbc2SNelio Laranjeiro {
260634efbc2SNelio Laranjeiro 	unsigned int l;
261634efbc2SNelio Laranjeiro 	unsigned int r;
262634efbc2SNelio Laranjeiro 
263634efbc2SNelio Laranjeiro 	for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
264634efbc2SNelio Laranjeiro 		r |= (v & 1);
265693f715dSHuawei Xie 	return l + r;
266634efbc2SNelio Laranjeiro }
267634efbc2SNelio Laranjeiro 
268e69a5922SXueming Li #define MLX5_HLIST_DIRECT_KEY 0x0001 /* Use the key directly as hash index. */
269e69a5922SXueming Li #define MLX5_HLIST_WRITE_MOST 0x0002 /* List mostly used for append new. */
270e69a5922SXueming Li 
27146287eacSBing Zhao /** Maximum size of string for naming the hlist table. */
27246287eacSBing Zhao #define MLX5_HLIST_NAMESIZE			32
27346287eacSBing Zhao 
274e69a5922SXueming Li struct mlx5_hlist;
275e69a5922SXueming Li 
27646287eacSBing Zhao /**
27746287eacSBing Zhao  * Structure of the entry in the hash list, user should define its own struct
27846287eacSBing Zhao  * that contains this in order to store the data. The 'key' is 64-bits right
27946287eacSBing Zhao  * now and its user's responsibility to guarantee there is no collision.
28046287eacSBing Zhao  */
28146287eacSBing Zhao struct mlx5_hlist_entry {
28246287eacSBing Zhao 	LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
28346287eacSBing Zhao 	uint64_t key; /* user defined 'key', could be the hash signature. */
284e69a5922SXueming Li 	uint32_t ref_cnt; /* Reference count. */
28546287eacSBing Zhao };
28646287eacSBing Zhao 
28746287eacSBing Zhao /** Structure for hash head. */
28846287eacSBing Zhao LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
28946287eacSBing Zhao 
29046287eacSBing Zhao /** Type of function that is used to handle the data before freeing. */
29146287eacSBing Zhao typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
29246287eacSBing Zhao 
293095c397bSSuanming Mou /**
294095c397bSSuanming Mou  * Type of function for user defined matching.
295095c397bSSuanming Mou  *
296095c397bSSuanming Mou  * @param entry
297095c397bSSuanming Mou  *   The entry in the list.
298095c397bSSuanming Mou  * @param ctx
299095c397bSSuanming Mou  *   The pointer to new entry context.
300095c397bSSuanming Mou  *
301095c397bSSuanming Mou  * @return
302095c397bSSuanming Mou  *   0 if matching, -1 otherwise.
303095c397bSSuanming Mou  */
304095c397bSSuanming Mou typedef int (*mlx5_hlist_match_callback_fn)(struct mlx5_hlist_entry *entry,
305095c397bSSuanming Mou 					     void *ctx);
306095c397bSSuanming Mou 
307e69a5922SXueming Li /**
308e69a5922SXueming Li  * Type of callback function for entry removal.
309e69a5922SXueming Li  *
310e69a5922SXueming Li  * @param list
311e69a5922SXueming Li  *   The hash list.
312e69a5922SXueming Li  * @param entry
313e69a5922SXueming Li  *   The entry in the list.
314e69a5922SXueming Li  */
315e69a5922SXueming Li typedef void (*mlx5_hlist_remove_cb)(struct mlx5_hlist *list,
316e69a5922SXueming Li 				     struct mlx5_hlist_entry *entry);
317e69a5922SXueming Li 
318e69a5922SXueming Li /**
319e69a5922SXueming Li  * Type of function for user defined matching.
320e69a5922SXueming Li  *
321e69a5922SXueming Li  * @param list
322e69a5922SXueming Li  *   The hash list.
323e69a5922SXueming Li  * @param entry
324e69a5922SXueming Li  *   The entry in the list.
325e69a5922SXueming Li  * @param key
326e69a5922SXueming Li  *   The new entry key.
327e69a5922SXueming Li  * @param ctx
328e69a5922SXueming Li  *   The pointer to new entry context.
329e69a5922SXueming Li  *
330e69a5922SXueming Li  * @return
331e69a5922SXueming Li  *   0 if matching, non-zero number otherwise.
332e69a5922SXueming Li  */
333e69a5922SXueming Li typedef int (*mlx5_hlist_match_cb)(struct mlx5_hlist *list,
334e69a5922SXueming Li 				   struct mlx5_hlist_entry *entry,
335e69a5922SXueming Li 				   uint64_t key, void *ctx);
336e69a5922SXueming Li 
337e69a5922SXueming Li /**
338e69a5922SXueming Li  * Type of function for user defined hash list entry creation.
339e69a5922SXueming Li  *
340e69a5922SXueming Li  * @param list
341e69a5922SXueming Li  *   The hash list.
342e69a5922SXueming Li  * @param key
343e69a5922SXueming Li  *   The key of the new entry.
344e69a5922SXueming Li  * @param ctx
345e69a5922SXueming Li  *   The pointer to new entry context.
346e69a5922SXueming Li  *
347e69a5922SXueming Li  * @return
348e69a5922SXueming Li  *   Pointer to allocated entry on success, NULL otherwise.
349e69a5922SXueming Li  */
350e69a5922SXueming Li typedef struct mlx5_hlist_entry *(*mlx5_hlist_create_cb)
351e69a5922SXueming Li 				  (struct mlx5_hlist *list,
352e69a5922SXueming Li 				   uint64_t key, void *ctx);
353e69a5922SXueming Li 
354e69a5922SXueming Li /**
355e69a5922SXueming Li  * Hash list table structure
356e69a5922SXueming Li  *
357e69a5922SXueming Li  * Entry in hash list could be reused if entry already exists, reference
358e69a5922SXueming Li  * count will increase and the existing entry returns.
359e69a5922SXueming Li  *
360e69a5922SXueming Li  * When destroy an entry from list, decrease reference count and only
361e69a5922SXueming Li  * destroy when no further reference.
362e69a5922SXueming Li  */
36346287eacSBing Zhao struct mlx5_hlist {
36446287eacSBing Zhao 	char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
36546287eacSBing Zhao 	/**< number of heads, need to be power of 2. */
36646287eacSBing Zhao 	uint32_t table_sz;
367e69a5922SXueming Li 	uint32_t entry_sz; /**< Size of entry, used to allocate entry. */
36846287eacSBing Zhao 	/**< mask to get the index of the list heads. */
36946287eacSBing Zhao 	uint32_t mask;
370e69a5922SXueming Li 	rte_rwlock_t lock;
371e69a5922SXueming Li 	uint32_t gen_cnt; /* List modification will update generation count. */
372e69a5922SXueming Li 	bool direct_key; /* Use the new entry key directly as hash index. */
373e69a5922SXueming Li 	bool write_most; /* List mostly used for append new or destroy. */
374e69a5922SXueming Li 	void *ctx;
375e69a5922SXueming Li 	mlx5_hlist_create_cb cb_create; /**< entry create callback. */
376e69a5922SXueming Li 	mlx5_hlist_match_cb cb_match; /**< entry match callback. */
377e69a5922SXueming Li 	mlx5_hlist_remove_cb cb_remove; /**< entry remove callback. */
37846287eacSBing Zhao 	struct mlx5_hlist_head heads[];	/**< list head arrays. */
37946287eacSBing Zhao };
38046287eacSBing Zhao 
38146287eacSBing Zhao /**
38246287eacSBing Zhao  * Create a hash list table, the user can specify the list heads array size
38346287eacSBing Zhao  * of the table, now the size should be a power of 2 in order to get better
38446287eacSBing Zhao  * distribution for the entries. Each entry is a part of the whole data element
38546287eacSBing Zhao  * and the caller should be responsible for the data element's allocation and
38646287eacSBing Zhao  * cleanup / free. Key of each entry will be calculated with CRC in order to
38746287eacSBing Zhao  * generate a little fairer distribution.
38846287eacSBing Zhao  *
38946287eacSBing Zhao  * @param name
39046287eacSBing Zhao  *   Name of the hash list(optional).
39146287eacSBing Zhao  * @param size
39246287eacSBing Zhao  *   Heads array size of the hash list.
393e69a5922SXueming Li  * @param entry_size
394e69a5922SXueming Li  *   Entry size to allocate if cb_create not specified.
395e69a5922SXueming Li  * @param flags
396e69a5922SXueming Li  *   The hash list attribute flags.
397e69a5922SXueming Li  * @param cb_create
398e69a5922SXueming Li  *   Callback function for entry create.
399e69a5922SXueming Li  * @param cb_match
400e69a5922SXueming Li  *   Callback function for entry match.
401e69a5922SXueming Li  * @param cb_destroy
402e69a5922SXueming Li  *   Callback function for entry destroy.
40346287eacSBing Zhao  * @return
40446287eacSBing Zhao  *   Pointer of the hash list table created, NULL on failure.
40546287eacSBing Zhao  */
406e69a5922SXueming Li struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size,
407e69a5922SXueming Li 				     uint32_t entry_size, uint32_t flags,
408e69a5922SXueming Li 				     mlx5_hlist_create_cb cb_create,
409e69a5922SXueming Li 				     mlx5_hlist_match_cb cb_match,
410e69a5922SXueming Li 				     mlx5_hlist_remove_cb cb_destroy);
41146287eacSBing Zhao 
41246287eacSBing Zhao /**
41346287eacSBing Zhao  * Search an entry matching the key.
41446287eacSBing Zhao  *
415e69a5922SXueming Li  * Result returned might be destroyed by other thread, must use
416e69a5922SXueming Li  * this function only in main thread.
417e69a5922SXueming Li  *
41846287eacSBing Zhao  * @param h
41946287eacSBing Zhao  *   Pointer to the hast list table.
42046287eacSBing Zhao  * @param key
42146287eacSBing Zhao  *   Key for the searching entry.
422e69a5922SXueming Li  * @param ctx
423e69a5922SXueming Li  *   Common context parameter used by entry callback function.
42446287eacSBing Zhao  *
42546287eacSBing Zhao  * @return
42646287eacSBing Zhao  *   Pointer of the hlist entry if found, NULL otherwise.
42746287eacSBing Zhao  */
428e69a5922SXueming Li struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key,
429e69a5922SXueming Li 					   void *ctx);
43046287eacSBing Zhao 
43146287eacSBing Zhao /**
432095c397bSSuanming Mou  * Extended routine to search an entry matching the context with
433095c397bSSuanming Mou  * user defined match function.
434095c397bSSuanming Mou  *
435095c397bSSuanming Mou  * @param h
436095c397bSSuanming Mou  *   Pointer to the hast list table.
437095c397bSSuanming Mou  * @param key
438095c397bSSuanming Mou  *   Key for the searching entry.
439095c397bSSuanming Mou  * @param cb
440095c397bSSuanming Mou  *   Callback function to match the node with context.
441095c397bSSuanming Mou  * @param ctx
442095c397bSSuanming Mou  *   Common context parameter used by callback function.
443095c397bSSuanming Mou  *
444095c397bSSuanming Mou  * @return
445095c397bSSuanming Mou  *   Pointer of the hlist entry if found, NULL otherwise.
446095c397bSSuanming Mou  */
447095c397bSSuanming Mou struct mlx5_hlist_entry *mlx5_hlist_lookup_ex(struct mlx5_hlist *h,
448095c397bSSuanming Mou 					      uint64_t key,
449095c397bSSuanming Mou 					      mlx5_hlist_match_callback_fn cb,
450095c397bSSuanming Mou 					      void *ctx);
451095c397bSSuanming Mou 
452095c397bSSuanming Mou /**
453095c397bSSuanming Mou  * Extended routine to insert an entry to the list with key collisions.
454095c397bSSuanming Mou  *
455095c397bSSuanming Mou  * For the list have key collision, the extra user defined match function
456095c397bSSuanming Mou  * allows node with same key will be inserted.
457095c397bSSuanming Mou  *
458095c397bSSuanming Mou  * @param h
459095c397bSSuanming Mou  *   Pointer to the hast list table.
460095c397bSSuanming Mou  * @param entry
461095c397bSSuanming Mou  *   Entry to be inserted into the hash list table.
462095c397bSSuanming Mou  * @param cb
463095c397bSSuanming Mou  *   Callback function to match the node with context.
464095c397bSSuanming Mou  * @param ctx
465095c397bSSuanming Mou  *   Common context parameter used by callback function.
466095c397bSSuanming Mou  *
467095c397bSSuanming Mou  * @return
468095c397bSSuanming Mou  *   - zero for success.
469095c397bSSuanming Mou  *   - -EEXIST if the entry is already inserted.
470095c397bSSuanming Mou  */
471095c397bSSuanming Mou int mlx5_hlist_insert_ex(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry,
472095c397bSSuanming Mou 			 mlx5_hlist_match_callback_fn cb, void *ctx);
473095c397bSSuanming Mou 
474095c397bSSuanming Mou /**
475e69a5922SXueming Li  * Insert an entry to the hash list table, the entry is only part of whole data
476e69a5922SXueming Li  * element and a 64B key is used for matching. User should construct the key or
477e69a5922SXueming Li  * give a calculated hash signature and guarantee there is no collision.
478e69a5922SXueming Li  *
479e69a5922SXueming Li  * @param h
480e69a5922SXueming Li  *   Pointer to the hast list table.
481e69a5922SXueming Li  * @param entry
482e69a5922SXueming Li  *   Entry to be inserted into the hash list table.
483e69a5922SXueming Li  * @param ctx
484e69a5922SXueming Li  *   Common context parameter used by callback function.
485e69a5922SXueming Li  *
486e69a5922SXueming Li  * @return
487e69a5922SXueming Li  *   registered entry on success, NULL otherwise
488e69a5922SXueming Li  */
489e69a5922SXueming Li struct mlx5_hlist_entry *mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key,
490e69a5922SXueming Li 					     void *ctx);
491e69a5922SXueming Li 
492e69a5922SXueming Li /**
49346287eacSBing Zhao  * Remove an entry from the hash list table. User should guarantee the validity
49446287eacSBing Zhao  * of the entry.
49546287eacSBing Zhao  *
49646287eacSBing Zhao  * @param h
49746287eacSBing Zhao  *   Pointer to the hast list table. (not used)
49846287eacSBing Zhao  * @param entry
49946287eacSBing Zhao  *   Entry to be removed from the hash list table.
500e69a5922SXueming Li  * @return
501e69a5922SXueming Li  *   0 on entry removed, 1 on entry still referenced.
50246287eacSBing Zhao  */
503e69a5922SXueming Li int mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
50446287eacSBing Zhao 
50546287eacSBing Zhao /**
50646287eacSBing Zhao  * Destroy the hash list table, all the entries already inserted into the lists
50746287eacSBing Zhao  * will be handled by the callback function provided by the user (including
50846287eacSBing Zhao  * free if needed) before the table is freed.
50946287eacSBing Zhao  *
51046287eacSBing Zhao  * @param h
51146287eacSBing Zhao  *   Pointer to the hast list table.
51246287eacSBing Zhao  */
513e69a5922SXueming Li void mlx5_hlist_destroy(struct mlx5_hlist *h);
51446287eacSBing Zhao 
515*1ff37beeSXueming Li /************************ cache list *****************************/
516*1ff37beeSXueming Li 
517*1ff37beeSXueming Li /** Maximum size of string for naming. */
518*1ff37beeSXueming Li #define MLX5_NAME_SIZE			32
519*1ff37beeSXueming Li 
520*1ff37beeSXueming Li struct mlx5_cache_list;
521*1ff37beeSXueming Li 
522*1ff37beeSXueming Li /**
523*1ff37beeSXueming Li  * Structure of the entry in the cache list, user should define its own struct
524*1ff37beeSXueming Li  * that contains this in order to store the data.
525*1ff37beeSXueming Li  */
526*1ff37beeSXueming Li struct mlx5_cache_entry {
527*1ff37beeSXueming Li 	LIST_ENTRY(mlx5_cache_entry) next; /* Entry pointers in the list. */
528*1ff37beeSXueming Li 	uint32_t ref_cnt; /* Reference count. */
529*1ff37beeSXueming Li };
530*1ff37beeSXueming Li 
531*1ff37beeSXueming Li /**
532*1ff37beeSXueming Li  * Type of callback function for entry removal.
533*1ff37beeSXueming Li  *
534*1ff37beeSXueming Li  * @param list
535*1ff37beeSXueming Li  *   The cache list.
536*1ff37beeSXueming Li  * @param entry
537*1ff37beeSXueming Li  *   The entry in the list.
538*1ff37beeSXueming Li  */
539*1ff37beeSXueming Li typedef void (*mlx5_cache_remove_cb)(struct mlx5_cache_list *list,
540*1ff37beeSXueming Li 				     struct mlx5_cache_entry *entry);
541*1ff37beeSXueming Li 
542*1ff37beeSXueming Li /**
543*1ff37beeSXueming Li  * Type of function for user defined matching.
544*1ff37beeSXueming Li  *
545*1ff37beeSXueming Li  * @param list
546*1ff37beeSXueming Li  *   The cache list.
547*1ff37beeSXueming Li  * @param entry
548*1ff37beeSXueming Li  *   The entry in the list.
549*1ff37beeSXueming Li  * @param ctx
550*1ff37beeSXueming Li  *   The pointer to new entry context.
551*1ff37beeSXueming Li  *
552*1ff37beeSXueming Li  * @return
553*1ff37beeSXueming Li  *   0 if matching, non-zero number otherwise.
554*1ff37beeSXueming Li  */
555*1ff37beeSXueming Li typedef int (*mlx5_cache_match_cb)(struct mlx5_cache_list *list,
556*1ff37beeSXueming Li 				   struct mlx5_cache_entry *entry, void *ctx);
557*1ff37beeSXueming Li 
558*1ff37beeSXueming Li /**
559*1ff37beeSXueming Li  * Type of function for user defined cache list entry creation.
560*1ff37beeSXueming Li  *
561*1ff37beeSXueming Li  * @param list
562*1ff37beeSXueming Li  *   The cache list.
563*1ff37beeSXueming Li  * @param entry
564*1ff37beeSXueming Li  *   The new allocated entry, NULL if list entry size unspecified,
565*1ff37beeSXueming Li  *   New entry has to be allocated in callback and return.
566*1ff37beeSXueming Li  * @param ctx
567*1ff37beeSXueming Li  *   The pointer to new entry context.
568*1ff37beeSXueming Li  *
569*1ff37beeSXueming Li  * @return
570*1ff37beeSXueming Li  *   Pointer of entry on success, NULL otherwise.
571*1ff37beeSXueming Li  */
572*1ff37beeSXueming Li typedef struct mlx5_cache_entry *(*mlx5_cache_create_cb)
573*1ff37beeSXueming Li 				 (struct mlx5_cache_list *list,
574*1ff37beeSXueming Li 				  struct mlx5_cache_entry *entry,
575*1ff37beeSXueming Li 				  void *ctx);
576*1ff37beeSXueming Li 
577*1ff37beeSXueming Li /**
578*1ff37beeSXueming Li  * Linked cache list structure.
579*1ff37beeSXueming Li  *
580*1ff37beeSXueming Li  * Entry in cache list could be reused if entry already exists,
581*1ff37beeSXueming Li  * reference count will increase and the existing entry returns.
582*1ff37beeSXueming Li  *
583*1ff37beeSXueming Li  * When destroy an entry from list, decrease reference count and only
584*1ff37beeSXueming Li  * destroy when no further reference.
585*1ff37beeSXueming Li  *
586*1ff37beeSXueming Li  * Linked list cache is designed for limited number of entries cache,
587*1ff37beeSXueming Li  * read mostly, less modification.
588*1ff37beeSXueming Li  *
589*1ff37beeSXueming Li  * For huge amount of entries cache, please consider hash list cache.
590*1ff37beeSXueming Li  *
591*1ff37beeSXueming Li  */
592*1ff37beeSXueming Li struct mlx5_cache_list {
593*1ff37beeSXueming Li 	char name[MLX5_NAME_SIZE]; /**< Name of the cache list. */
594*1ff37beeSXueming Li 	uint32_t entry_sz; /**< Entry size, 0: use create callback. */
595*1ff37beeSXueming Li 	rte_rwlock_t lock; /* read/write lock. */
596*1ff37beeSXueming Li 	uint32_t gen_cnt; /* List modification will update generation count. */
597*1ff37beeSXueming Li 	uint32_t count; /* number of entries in list. */
598*1ff37beeSXueming Li 	void *ctx; /* user objects target to callback. */
599*1ff37beeSXueming Li 	mlx5_cache_create_cb cb_create; /**< entry create callback. */
600*1ff37beeSXueming Li 	mlx5_cache_match_cb cb_match; /**< entry match callback. */
601*1ff37beeSXueming Li 	mlx5_cache_remove_cb cb_remove; /**< entry remove callback. */
602*1ff37beeSXueming Li 	LIST_HEAD(mlx5_cache_head, mlx5_cache_entry) head;
603*1ff37beeSXueming Li };
604*1ff37beeSXueming Li 
605*1ff37beeSXueming Li /**
606*1ff37beeSXueming Li  * Initialize a cache list.
607*1ff37beeSXueming Li  *
608*1ff37beeSXueming Li  * @param list
609*1ff37beeSXueming Li  *   Pointer to the hast list table.
610*1ff37beeSXueming Li  * @param name
611*1ff37beeSXueming Li  *   Name of the cache list.
612*1ff37beeSXueming Li  * @param entry_size
613*1ff37beeSXueming Li  *   Entry size to allocate, 0 to allocate by creation callback.
614*1ff37beeSXueming Li  * @param ctx
615*1ff37beeSXueming Li  *   Pointer to the list context data.
616*1ff37beeSXueming Li  * @param cb_create
617*1ff37beeSXueming Li  *   Callback function for entry create.
618*1ff37beeSXueming Li  * @param cb_match
619*1ff37beeSXueming Li  *   Callback function for entry match.
620*1ff37beeSXueming Li  * @param cb_remove
621*1ff37beeSXueming Li  *   Callback function for entry remove.
622*1ff37beeSXueming Li  * @return
623*1ff37beeSXueming Li  *   0 on success, otherwise failure.
624*1ff37beeSXueming Li  */
625*1ff37beeSXueming Li int mlx5_cache_list_init(struct mlx5_cache_list *list,
626*1ff37beeSXueming Li 			 const char *name, uint32_t entry_size, void *ctx,
627*1ff37beeSXueming Li 			 mlx5_cache_create_cb cb_create,
628*1ff37beeSXueming Li 			 mlx5_cache_match_cb cb_match,
629*1ff37beeSXueming Li 			 mlx5_cache_remove_cb cb_remove);
630*1ff37beeSXueming Li 
631*1ff37beeSXueming Li /**
632*1ff37beeSXueming Li  * Search an entry matching the key.
633*1ff37beeSXueming Li  *
634*1ff37beeSXueming Li  * Result returned might be destroyed by other thread, must use
635*1ff37beeSXueming Li  * this function only in main thread.
636*1ff37beeSXueming Li  *
637*1ff37beeSXueming Li  * @param list
638*1ff37beeSXueming Li  *   Pointer to the cache list.
639*1ff37beeSXueming Li  * @param ctx
640*1ff37beeSXueming Li  *   Common context parameter used by entry callback function.
641*1ff37beeSXueming Li  *
642*1ff37beeSXueming Li  * @return
643*1ff37beeSXueming Li  *   Pointer of the cache entry if found, NULL otherwise.
644*1ff37beeSXueming Li  */
645*1ff37beeSXueming Li struct mlx5_cache_entry *mlx5_cache_lookup(struct mlx5_cache_list *list,
646*1ff37beeSXueming Li 					   void *ctx);
647*1ff37beeSXueming Li 
648*1ff37beeSXueming Li /**
649*1ff37beeSXueming Li  * Reuse or create an entry to the cache list.
650*1ff37beeSXueming Li  *
651*1ff37beeSXueming Li  * @param list
652*1ff37beeSXueming Li  *   Pointer to the hast list table.
653*1ff37beeSXueming Li  * @param ctx
654*1ff37beeSXueming Li  *   Common context parameter used by callback function.
655*1ff37beeSXueming Li  *
656*1ff37beeSXueming Li  * @return
657*1ff37beeSXueming Li  *   registered entry on success, NULL otherwise
658*1ff37beeSXueming Li  */
659*1ff37beeSXueming Li struct mlx5_cache_entry *mlx5_cache_register(struct mlx5_cache_list *list,
660*1ff37beeSXueming Li 					     void *ctx);
661*1ff37beeSXueming Li 
662*1ff37beeSXueming Li /**
663*1ff37beeSXueming Li  * Remove an entry from the cache list.
664*1ff37beeSXueming Li  *
665*1ff37beeSXueming Li  * User should guarantee the validity of the entry.
666*1ff37beeSXueming Li  *
667*1ff37beeSXueming Li  * @param list
668*1ff37beeSXueming Li  *   Pointer to the hast list.
669*1ff37beeSXueming Li  * @param entry
670*1ff37beeSXueming Li  *   Entry to be removed from the cache list table.
671*1ff37beeSXueming Li  * @return
672*1ff37beeSXueming Li  *   0 on entry removed, 1 on entry still referenced.
673*1ff37beeSXueming Li  */
674*1ff37beeSXueming Li int mlx5_cache_unregister(struct mlx5_cache_list *list,
675*1ff37beeSXueming Li 			  struct mlx5_cache_entry *entry);
676*1ff37beeSXueming Li 
677*1ff37beeSXueming Li /**
678*1ff37beeSXueming Li  * Destroy the cache list.
679*1ff37beeSXueming Li  *
680*1ff37beeSXueming Li  * @param list
681*1ff37beeSXueming Li  *   Pointer to the cache list.
682*1ff37beeSXueming Li  */
683*1ff37beeSXueming Li void mlx5_cache_list_destroy(struct mlx5_cache_list *list);
684*1ff37beeSXueming Li 
685*1ff37beeSXueming Li /**
686*1ff37beeSXueming Li  * Get entry number from the cache list.
687*1ff37beeSXueming Li  *
688*1ff37beeSXueming Li  * @param list
689*1ff37beeSXueming Li  *   Pointer to the hast list.
690*1ff37beeSXueming Li  * @return
691*1ff37beeSXueming Li  *   Cache list entry number.
692*1ff37beeSXueming Li  */
693*1ff37beeSXueming Li uint32_t
694*1ff37beeSXueming Li mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list);
695*1ff37beeSXueming Li 
696*1ff37beeSXueming Li /********************************* indexed pool *************************/
697*1ff37beeSXueming Li 
698a3cf59f5SSuanming Mou /**
699a3cf59f5SSuanming Mou  * This function allocates non-initialized memory entry from pool.
700a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
701a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
702a3cf59f5SSuanming Mou  *
703a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
704a3cf59f5SSuanming Mou  *
705a3cf59f5SSuanming Mou  * @param pool
706a3cf59f5SSuanming Mou  *   Pointer to indexed memory entry pool.
707a3cf59f5SSuanming Mou  *   No initialization required.
708a3cf59f5SSuanming Mou  * @param[out] idx
709a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
710a3cf59f5SSuanming Mou  *   Memory index always positive value.
711a3cf59f5SSuanming Mou  * @return
712a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry.
713a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
714a3cf59f5SSuanming Mou  */
715a3cf59f5SSuanming Mou void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
716a3cf59f5SSuanming Mou 
717a3cf59f5SSuanming Mou /**
718a3cf59f5SSuanming Mou  * This function allocates zero initialized memory entry from pool.
719a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
720a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
721a3cf59f5SSuanming Mou  *
722a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
723a3cf59f5SSuanming Mou  *
724a3cf59f5SSuanming Mou  * @param pool
725a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
726a3cf59f5SSuanming Mou  *   No initialization required.
727a3cf59f5SSuanming Mou  * @param[out] idx
728a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
729a3cf59f5SSuanming Mou  *   Memory index always positive value.
730a3cf59f5SSuanming Mou  * @return
731a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry .
732a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
733a3cf59f5SSuanming Mou  */
734a3cf59f5SSuanming Mou void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
735a3cf59f5SSuanming Mou 
736a3cf59f5SSuanming Mou /**
737a3cf59f5SSuanming Mou  * This function frees indexed memory entry to pool.
738a3cf59f5SSuanming Mou  * Caller has to make sure that the index is allocated from same pool.
739a3cf59f5SSuanming Mou  *
740a3cf59f5SSuanming Mou  * @param pool
741a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
742a3cf59f5SSuanming Mou  * @param idx
743a3cf59f5SSuanming Mou  *   Allocated memory entry index.
744a3cf59f5SSuanming Mou  */
745a3cf59f5SSuanming Mou void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
746a3cf59f5SSuanming Mou 
747a3cf59f5SSuanming Mou /**
748a3cf59f5SSuanming Mou  * This function returns pointer of indexed memory entry from index.
749a3cf59f5SSuanming Mou  * Caller has to make sure that the index is valid, and allocated
750a3cf59f5SSuanming Mou  * from same pool.
751a3cf59f5SSuanming Mou  *
752a3cf59f5SSuanming Mou  * @param pool
753a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
754a3cf59f5SSuanming Mou  * @param idx
755a3cf59f5SSuanming Mou  *   Allocated memory index.
756a3cf59f5SSuanming Mou  * @return
757a3cf59f5SSuanming Mou  *   - Pointer to indexed memory entry.
758a3cf59f5SSuanming Mou  */
759a3cf59f5SSuanming Mou void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
760a3cf59f5SSuanming Mou 
761a3cf59f5SSuanming Mou /**
762a3cf59f5SSuanming Mou  * This function creates indexed memory pool.
763a3cf59f5SSuanming Mou  * Caller has to configure the configuration accordingly.
764a3cf59f5SSuanming Mou  *
765a3cf59f5SSuanming Mou  * @param pool
766a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
767a3cf59f5SSuanming Mou  * @param cfg
768a3cf59f5SSuanming Mou  *   Allocated memory index.
769a3cf59f5SSuanming Mou  */
770a3cf59f5SSuanming Mou struct mlx5_indexed_pool *
771a3cf59f5SSuanming Mou mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
772a3cf59f5SSuanming Mou 
773a3cf59f5SSuanming Mou /**
774a3cf59f5SSuanming Mou  * This function releases all resources of pool.
775a3cf59f5SSuanming Mou  * Caller has to make sure that all indexes and memories allocated
776a3cf59f5SSuanming Mou  * from this pool not referenced anymore.
777a3cf59f5SSuanming Mou  *
778a3cf59f5SSuanming Mou  * @param pool
779a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
780a3cf59f5SSuanming Mou  * @return
781a3cf59f5SSuanming Mou  *   - non-zero value on error.
782a3cf59f5SSuanming Mou  *   - 0 on success.
783a3cf59f5SSuanming Mou  */
784a3cf59f5SSuanming Mou int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
785a3cf59f5SSuanming Mou 
786a3cf59f5SSuanming Mou /**
787a3cf59f5SSuanming Mou  * This function dumps debug info of pool.
788a3cf59f5SSuanming Mou  *
789a3cf59f5SSuanming Mou  * @param pool
790a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
791a3cf59f5SSuanming Mou  */
792a3cf59f5SSuanming Mou void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
793a3cf59f5SSuanming Mou 
794bd81eaebSSuanming Mou /**
795bd81eaebSSuanming Mou  * This function allocates new empty Three-level table.
796bd81eaebSSuanming Mou  *
797bd81eaebSSuanming Mou  * @param type
798bd81eaebSSuanming Mou  *   The l3t can set as word, double word, quad word or pointer with index.
799bd81eaebSSuanming Mou  *
800bd81eaebSSuanming Mou  * @return
801bd81eaebSSuanming Mou  *   - Pointer to the allocated l3t.
802bd81eaebSSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
803bd81eaebSSuanming Mou  */
804bd81eaebSSuanming Mou struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
805bd81eaebSSuanming Mou 
806bd81eaebSSuanming Mou /**
807bd81eaebSSuanming Mou  * This function destroys Three-level table.
808bd81eaebSSuanming Mou  *
809bd81eaebSSuanming Mou  * @param tbl
810bd81eaebSSuanming Mou  *   Pointer to the l3t.
811bd81eaebSSuanming Mou  */
812bd81eaebSSuanming Mou void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
813bd81eaebSSuanming Mou 
814bd81eaebSSuanming Mou /**
815bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
816bd81eaebSSuanming Mou  *
817bd81eaebSSuanming Mou  * @param tbl
818bd81eaebSSuanming Mou  *   Pointer to the l3t.
819bd81eaebSSuanming Mou  * @param idx
820bd81eaebSSuanming Mou  *   Index to the entry.
821bd81eaebSSuanming Mou  * @param data
822bd81eaebSSuanming Mou  *   Pointer to the memory which saves the entry data.
823bd81eaebSSuanming Mou  *   When function call returns 0, data contains the entry data get from
824bd81eaebSSuanming Mou  *   l3t.
825bd81eaebSSuanming Mou  *   When function call returns -1, data is not modified.
826bd81eaebSSuanming Mou  *
827bd81eaebSSuanming Mou  * @return
828bd81eaebSSuanming Mou  *   0 if success, -1 on error.
829bd81eaebSSuanming Mou  */
830bd81eaebSSuanming Mou 
8310796c7b1SSuanming Mou int32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
832bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
833bd81eaebSSuanming Mou 
834bd81eaebSSuanming Mou /**
835bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
836bd81eaebSSuanming Mou  *
8370796c7b1SSuanming Mou  * If the index entry is not available, allocate new one by callback
8380796c7b1SSuanming Mou  * function and fill in the entry.
8390796c7b1SSuanming Mou  *
840bd81eaebSSuanming Mou  * @param tbl
841bd81eaebSSuanming Mou  *   Pointer to the l3t.
842bd81eaebSSuanming Mou  * @param idx
843bd81eaebSSuanming Mou  *   Index to the entry.
844bd81eaebSSuanming Mou  * @param data
8450796c7b1SSuanming Mou  *   Pointer to the memory which saves the entry data.
8460796c7b1SSuanming Mou  *   When function call returns 0, data contains the entry data get from
8470796c7b1SSuanming Mou  *   l3t.
8480796c7b1SSuanming Mou  *   When function call returns -1, data is not modified.
8490796c7b1SSuanming Mou  * @param cb
8500796c7b1SSuanming Mou  *   Callback function to allocate new data.
8510796c7b1SSuanming Mou  * @param ctx
8520796c7b1SSuanming Mou  *   Context for callback function.
853bd81eaebSSuanming Mou  *
854bd81eaebSSuanming Mou  * @return
855bd81eaebSSuanming Mou  *   0 if success, -1 on error.
856bd81eaebSSuanming Mou  */
8570796c7b1SSuanming Mou 
8580796c7b1SSuanming Mou int32_t mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
8590796c7b1SSuanming Mou 			       union mlx5_l3t_data *data,
8600796c7b1SSuanming Mou 			       mlx5_l3t_alloc_callback_fn cb, void *ctx);
8610796c7b1SSuanming Mou 
8620796c7b1SSuanming Mou /**
8630796c7b1SSuanming Mou  * This function decreases and clear index entry if reference
8640796c7b1SSuanming Mou  * counter is 0 from Three-level table.
8650796c7b1SSuanming Mou  *
8660796c7b1SSuanming Mou  * @param tbl
8670796c7b1SSuanming Mou  *   Pointer to the l3t.
8680796c7b1SSuanming Mou  * @param idx
8690796c7b1SSuanming Mou  *   Index to the entry.
8700796c7b1SSuanming Mou  *
8710796c7b1SSuanming Mou  * @return
8720796c7b1SSuanming Mou  *   The remaining reference count, 0 means entry be cleared, -1 on error.
8730796c7b1SSuanming Mou  */
8740796c7b1SSuanming Mou int32_t mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
8750796c7b1SSuanming Mou 
8760796c7b1SSuanming Mou /**
8770796c7b1SSuanming Mou  * This function sets the index entry to Three-level table.
8780796c7b1SSuanming Mou  * If the entry is already set, the EEXIST errno will be given, and
8790796c7b1SSuanming Mou  * the set data will be filled to the data.
8800796c7b1SSuanming Mou  *
8810796c7b1SSuanming Mou  * @param tbl[in]
8820796c7b1SSuanming Mou  *   Pointer to the l3t.
8830796c7b1SSuanming Mou  * @param idx[in]
8840796c7b1SSuanming Mou  *   Index to the entry.
8850796c7b1SSuanming Mou  * @param data[in/out]
8860796c7b1SSuanming Mou  *   Pointer to the memory which contains the entry data save to l3t.
8870796c7b1SSuanming Mou  *   If the entry is already set, the set data will be filled.
8880796c7b1SSuanming Mou  *
8890796c7b1SSuanming Mou  * @return
8900796c7b1SSuanming Mou  *   0 if success, -1 on error.
8910796c7b1SSuanming Mou  */
8920796c7b1SSuanming Mou int32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
893bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
894bd81eaebSSuanming Mou 
895a3cf59f5SSuanming Mou /*
896a3cf59f5SSuanming Mou  * Macros for linked list based on indexed memory.
897a3cf59f5SSuanming Mou  * Example data structure:
898a3cf59f5SSuanming Mou  * struct Foo {
899a3cf59f5SSuanming Mou  *	ILIST_ENTRY(uint16_t) next;
900a3cf59f5SSuanming Mou  *	...
901a3cf59f5SSuanming Mou  * }
902a3cf59f5SSuanming Mou  *
903a3cf59f5SSuanming Mou  */
904a3cf59f5SSuanming Mou #define ILIST_ENTRY(type)						\
905a3cf59f5SSuanming Mou struct {								\
906a3cf59f5SSuanming Mou 	type prev; /* Index of previous element. */			\
907a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
908a3cf59f5SSuanming Mou }
909a3cf59f5SSuanming Mou 
910a3cf59f5SSuanming Mou #define ILIST_INSERT(pool, head, idx, elem, field)			\
911a3cf59f5SSuanming Mou 	do {								\
912a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
913a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
914a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
915a3cf59f5SSuanming Mou 		(elem)->field.prev = 0;					\
916a3cf59f5SSuanming Mou 		if (*(head)) {						\
917a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get(pool, *(head));		\
918a3cf59f5SSuanming Mou 			if (peer)					\
919a3cf59f5SSuanming Mou 				(peer)->field.prev = (idx);		\
920a3cf59f5SSuanming Mou 		}							\
921a3cf59f5SSuanming Mou 		*(head) = (idx);					\
922a3cf59f5SSuanming Mou 	} while (0)
923a3cf59f5SSuanming Mou 
924a3cf59f5SSuanming Mou #define ILIST_REMOVE(pool, head, idx, elem, field)			\
925a3cf59f5SSuanming Mou 	do {								\
926a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
927a3cf59f5SSuanming Mou 		MLX5_ASSERT(elem);					\
928a3cf59f5SSuanming Mou 		MLX5_ASSERT(head);					\
929a3cf59f5SSuanming Mou 		if ((elem)->field.prev) {				\
930a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
931a3cf59f5SSuanming Mou 				 (pool, (elem)->field.prev);		\
932a3cf59f5SSuanming Mou 			if (peer)					\
933a3cf59f5SSuanming Mou 				(peer)->field.next = (elem)->field.next;\
934a3cf59f5SSuanming Mou 		}							\
935a3cf59f5SSuanming Mou 		if ((elem)->field.next) {				\
936a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
937a3cf59f5SSuanming Mou 				 (pool, (elem)->field.next);		\
938a3cf59f5SSuanming Mou 			if (peer)					\
939a3cf59f5SSuanming Mou 				(peer)->field.prev = (elem)->field.prev;\
940a3cf59f5SSuanming Mou 		}							\
941a3cf59f5SSuanming Mou 		if (*(head) == (idx))					\
942a3cf59f5SSuanming Mou 			*(head) = (elem)->field.next;			\
943a3cf59f5SSuanming Mou 	} while (0)
944a3cf59f5SSuanming Mou 
945a3cf59f5SSuanming Mou #define ILIST_FOREACH(pool, head, idx, elem, field)			\
946a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
947a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
948a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
949a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
950a3cf59f5SSuanming Mou 
951a3cf59f5SSuanming Mou /* Single index list. */
952a3cf59f5SSuanming Mou #define SILIST_ENTRY(type)						\
953a3cf59f5SSuanming Mou struct {								\
954a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
955a3cf59f5SSuanming Mou }
956a3cf59f5SSuanming Mou 
957a3cf59f5SSuanming Mou #define SILIST_INSERT(head, idx, elem, field)				\
958a3cf59f5SSuanming Mou 	do {								\
959a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
960a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
961a3cf59f5SSuanming Mou 		*(head) = (idx);					\
962a3cf59f5SSuanming Mou 	} while (0)
963a3cf59f5SSuanming Mou 
964a3cf59f5SSuanming Mou #define SILIST_FOREACH(pool, head, idx, elem, field)			\
965a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
966a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
967a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
968a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
969a3cf59f5SSuanming Mou 
970771fa900SAdrien Mazarguil #endif /* RTE_PMD_MLX5_UTILS_H_ */
971