xref: /dpdk/drivers/net/mlx5/mlx5_utils.h (revision 2d2546ad6b5ffe43fb9efeb92b606029b86af768)
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 
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 
32*2d2546adSAsaf Penso #define MLX5_NET_LOG_PREFIX "mlx5_net"
33*2d2546adSAsaf Penso 
34771fa900SAdrien Mazarguil /* Generic printf()-like logging macro with automatic line feed. */
35a170a30dSNélio Laranjeiro #define DRV_LOG(level, ...) \
36*2d2546adSAsaf Penso 	PMD_DRV_LOG_(level, mlx5_logtype, MLX5_NET_LOG_PREFIX, \
37771fa900SAdrien Mazarguil 		__VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
38771fa900SAdrien Mazarguil 		PMD_DRV_LOG_CPAREN)
39771fa900SAdrien Mazarguil 
402e22920bSAdrien Mazarguil /* Convenience macros for accessing mbuf fields. */
412e22920bSAdrien Mazarguil #define NEXT(m) ((m)->next)
422e22920bSAdrien Mazarguil #define DATA_LEN(m) ((m)->data_len)
432e22920bSAdrien Mazarguil #define PKT_LEN(m) ((m)->pkt_len)
442e22920bSAdrien Mazarguil #define DATA_OFF(m) ((m)->data_off)
452e22920bSAdrien Mazarguil #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
462e22920bSAdrien Mazarguil #define NB_SEGS(m) ((m)->nb_segs)
472e22920bSAdrien Mazarguil #define PORT(m) ((m)->port)
482e22920bSAdrien Mazarguil 
4967fa62bcSAdrien Mazarguil /* Transpose flags. Useful to convert IBV to DPDK flags. */
5067fa62bcSAdrien Mazarguil #define TRANSPOSE(val, from, to) \
5167fa62bcSAdrien Mazarguil 	(((from) >= (to)) ? \
5267fa62bcSAdrien Mazarguil 	 (((val) & (from)) / ((from) / (to))) : \
5367fa62bcSAdrien Mazarguil 	 (((val) & (from)) * ((to) / (from))))
5467fa62bcSAdrien Mazarguil 
55a3cf59f5SSuanming Mou /*
56bd81eaebSSuanming Mou  * For the case which data is linked with sequence increased index, the
57bd81eaebSSuanming Mou  * array table will be more efficiect than hash table once need to serarch
58bd81eaebSSuanming Mou  * one data entry in large numbers of entries. Since the traditional hash
59bd81eaebSSuanming Mou  * tables has fixed table size, when huge numbers of data saved to the hash
60bd81eaebSSuanming Mou  * table, it also comes lots of hash conflict.
61bd81eaebSSuanming Mou  *
62bd81eaebSSuanming Mou  * But simple array table also has fixed size, allocates all the needed
63bd81eaebSSuanming Mou  * memory at once will waste lots of memory. For the case don't know the
64bd81eaebSSuanming Mou  * exactly number of entries will be impossible to allocate the array.
65bd81eaebSSuanming Mou  *
66bd81eaebSSuanming Mou  * Then the multiple level table helps to balance the two disadvantages.
67bd81eaebSSuanming Mou  * Allocate a global high level table with sub table entries at first,
68bd81eaebSSuanming Mou  * the global table contains the sub table entries, and the sub table will
69bd81eaebSSuanming Mou  * be allocated only once the corresponding index entry need to be saved.
70bd81eaebSSuanming Mou  * e.g. for up to 32-bits index, three level table with 10-10-12 splitting,
71bd81eaebSSuanming Mou  * with sequence increased index, the memory grows with every 4K entries.
72bd81eaebSSuanming Mou  *
73bd81eaebSSuanming Mou  * The currently implementation introduces 10-10-12 32-bits splitting
74bd81eaebSSuanming Mou  * Three-Level table to help the cases which have millions of enties to
75bd81eaebSSuanming Mou  * save. The index entries can be addressed directly by the index, no
76bd81eaebSSuanming Mou  * search will be needed.q
77bd81eaebSSuanming Mou  */
78bd81eaebSSuanming Mou 
79bd81eaebSSuanming Mou /* L3 table global table define. */
80bd81eaebSSuanming Mou #define MLX5_L3T_GT_OFFSET 22
81bd81eaebSSuanming Mou #define MLX5_L3T_GT_SIZE (1 << 10)
82bd81eaebSSuanming Mou #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1)
83bd81eaebSSuanming Mou 
84bd81eaebSSuanming Mou /* L3 table middle table define. */
85bd81eaebSSuanming Mou #define MLX5_L3T_MT_OFFSET 12
86bd81eaebSSuanming Mou #define MLX5_L3T_MT_SIZE (1 << 10)
87bd81eaebSSuanming Mou #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1)
88bd81eaebSSuanming Mou 
89bd81eaebSSuanming Mou /* L3 table entry table define. */
90bd81eaebSSuanming Mou #define MLX5_L3T_ET_OFFSET 0
91bd81eaebSSuanming Mou #define MLX5_L3T_ET_SIZE (1 << 12)
92bd81eaebSSuanming Mou #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1)
93bd81eaebSSuanming Mou 
94bd81eaebSSuanming Mou /* L3 table type. */
95bd81eaebSSuanming Mou enum mlx5_l3t_type {
96bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_WORD = 0,
97bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_DWORD,
98bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_QWORD,
99bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_PTR,
100bd81eaebSSuanming Mou 	MLX5_L3T_TYPE_MAX,
101bd81eaebSSuanming Mou };
102bd81eaebSSuanming Mou 
103bd81eaebSSuanming Mou struct mlx5_indexed_pool;
104bd81eaebSSuanming Mou 
105bd81eaebSSuanming Mou /* Generic data struct. */
106bd81eaebSSuanming Mou union mlx5_l3t_data {
107bd81eaebSSuanming Mou 	uint16_t word;
108bd81eaebSSuanming Mou 	uint32_t dword;
109bd81eaebSSuanming Mou 	uint64_t qword;
110bd81eaebSSuanming Mou 	void *ptr;
111bd81eaebSSuanming Mou };
112bd81eaebSSuanming Mou 
113bd81eaebSSuanming Mou /* L3 level table data structure. */
114bd81eaebSSuanming Mou struct mlx5_l3t_level_tbl {
115bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
116bd81eaebSSuanming Mou 	void *tbl[]; /* Table array. */
117bd81eaebSSuanming Mou };
118bd81eaebSSuanming Mou 
119bd81eaebSSuanming Mou /* L3 word entry table data structure. */
120bd81eaebSSuanming Mou struct mlx5_l3t_entry_word {
121bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
122bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1230796c7b1SSuanming Mou 	struct {
1240796c7b1SSuanming Mou 		uint16_t data;
1250796c7b1SSuanming Mou 		uint32_t ref_cnt;
1260796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1270796c7b1SSuanming Mou } __rte_packed;
128bd81eaebSSuanming Mou 
129bd81eaebSSuanming Mou /* L3 double word entry table data structure. */
130bd81eaebSSuanming Mou struct mlx5_l3t_entry_dword {
131bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
132bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1330796c7b1SSuanming Mou 	struct {
1340796c7b1SSuanming Mou 		uint32_t data;
1350796c7b1SSuanming Mou 		int32_t ref_cnt;
1360796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1370796c7b1SSuanming Mou } __rte_packed;
138bd81eaebSSuanming Mou 
139bd81eaebSSuanming Mou /* L3 quad word entry table data structure. */
140bd81eaebSSuanming Mou struct mlx5_l3t_entry_qword {
141bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
142bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1430796c7b1SSuanming Mou 	struct {
1440796c7b1SSuanming Mou 		uint64_t data;
1450796c7b1SSuanming Mou 		uint32_t ref_cnt;
1460796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1470796c7b1SSuanming Mou } __rte_packed;
148bd81eaebSSuanming Mou 
149bd81eaebSSuanming Mou /* L3 pointer entry table data structure. */
150bd81eaebSSuanming Mou struct mlx5_l3t_entry_ptr {
151bd81eaebSSuanming Mou 	uint32_t idx; /* Table index. */
152bd81eaebSSuanming Mou 	uint64_t ref_cnt; /* Table ref_cnt. */
1530796c7b1SSuanming Mou 	struct {
1540796c7b1SSuanming Mou 		void *data;
1550796c7b1SSuanming Mou 		uint32_t ref_cnt;
1560796c7b1SSuanming Mou 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
1570796c7b1SSuanming Mou } __rte_packed;
158bd81eaebSSuanming Mou 
159bd81eaebSSuanming Mou /* L3 table data structure. */
160bd81eaebSSuanming Mou struct mlx5_l3t_tbl {
161bd81eaebSSuanming Mou 	enum mlx5_l3t_type type; /* Table type. */
162bd81eaebSSuanming Mou 	struct mlx5_indexed_pool *eip;
163bd81eaebSSuanming Mou 	/* Table index pool handles. */
164bd81eaebSSuanming Mou 	struct mlx5_l3t_level_tbl *tbl; /* Global table index. */
1650796c7b1SSuanming Mou 	rte_spinlock_t sl; /* The table lock. */
166bd81eaebSSuanming Mou };
167bd81eaebSSuanming Mou 
1680796c7b1SSuanming Mou /** Type of function that is used to handle the data before freeing. */
1690796c7b1SSuanming Mou typedef int32_t (*mlx5_l3t_alloc_callback_fn)(void *ctx,
1700796c7b1SSuanming Mou 					   union mlx5_l3t_data *data);
1710796c7b1SSuanming Mou 
172bd81eaebSSuanming Mou /*
173a3cf59f5SSuanming Mou  * The indexed memory entry index is made up of trunk index and offset of
174a3cf59f5SSuanming Mou  * the entry in the trunk. Since the entry index is 32 bits, in case user
175a3cf59f5SSuanming Mou  * prefers to have small trunks, user can change the macro below to a big
176a3cf59f5SSuanming Mou  * number which helps the pool contains more trunks with lots of entries
177a3cf59f5SSuanming Mou  * allocated.
178a3cf59f5SSuanming Mou  */
179a3cf59f5SSuanming Mou #define TRUNK_IDX_BITS 16
180a3cf59f5SSuanming Mou #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
181a3cf59f5SSuanming Mou #define TRUNK_INVALID TRUNK_MAX_IDX
182a3cf59f5SSuanming Mou #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
183a3cf59f5SSuanming Mou #ifdef RTE_LIBRTE_MLX5_DEBUG
184a3cf59f5SSuanming Mou #define POOL_DEBUG 1
185a3cf59f5SSuanming Mou #endif
186a3cf59f5SSuanming Mou 
187a3cf59f5SSuanming Mou struct mlx5_indexed_pool_config {
188a3cf59f5SSuanming Mou 	uint32_t size; /* Pool entry size. */
18962d7d519SSuanming Mou 	uint32_t trunk_size:22;
19062d7d519SSuanming Mou 	/*
19162d7d519SSuanming Mou 	 * Trunk entry number. Must be power of 2. It can be increased
19262d7d519SSuanming Mou 	 * if trunk_grow enable. The trunk entry number increases with
19362d7d519SSuanming Mou 	 * left shift grow_shift. Trunks with index are after grow_trunk
19462d7d519SSuanming Mou 	 * will keep the entry number same with the last grow trunk.
19562d7d519SSuanming Mou 	 */
19662d7d519SSuanming Mou 	uint32_t grow_trunk:4;
19762d7d519SSuanming Mou 	/*
19862d7d519SSuanming Mou 	 * Trunks with entry number increase in the pool. Set it to 0
19962d7d519SSuanming Mou 	 * to make the pool works as trunk entry fixed pool. It works
20062d7d519SSuanming Mou 	 * only if grow_shift is not 0.
20162d7d519SSuanming Mou 	 */
20262d7d519SSuanming Mou 	uint32_t grow_shift:4;
20362d7d519SSuanming Mou 	/*
20462d7d519SSuanming Mou 	 * Trunk entry number increase shift value, stop after grow_trunk.
20562d7d519SSuanming Mou 	 * It works only if grow_trunk is not 0.
20662d7d519SSuanming Mou 	 */
20762d7d519SSuanming Mou 	uint32_t need_lock:1;
208a3cf59f5SSuanming Mou 	/* Lock is needed for multiple thread usage. */
2091fd4bb67SSuanming Mou 	uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
210a3cf59f5SSuanming Mou 	const char *type; /* Memory allocate type name. */
21183c2047cSSuanming Mou 	void *(*malloc)(uint32_t flags, size_t size, unsigned int align,
212a3cf59f5SSuanming Mou 			int socket);
213a3cf59f5SSuanming Mou 	/* User defined memory allocator. */
214a3cf59f5SSuanming Mou 	void (*free)(void *addr); /* User defined memory release. */
215a3cf59f5SSuanming Mou };
216a3cf59f5SSuanming Mou 
217a3cf59f5SSuanming Mou struct mlx5_indexed_trunk {
218a3cf59f5SSuanming Mou 	uint32_t idx; /* Trunk id. */
219a3cf59f5SSuanming Mou 	uint32_t prev; /* Previous free trunk in free list. */
220a3cf59f5SSuanming Mou 	uint32_t next; /* Next free trunk in free list. */
221a3cf59f5SSuanming Mou 	uint32_t free; /* Free entries available */
222a3cf59f5SSuanming Mou 	struct rte_bitmap *bmp;
223691b3d3eSSuanming Mou 	uint8_t data[] __rte_cache_aligned; /* Entry data start. */
224a3cf59f5SSuanming Mou };
225a3cf59f5SSuanming Mou 
226a3cf59f5SSuanming Mou struct mlx5_indexed_pool {
227a3cf59f5SSuanming Mou 	struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
228a3cf59f5SSuanming Mou 	rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
229a3cf59f5SSuanming Mou 	uint32_t n_trunk_valid; /* Trunks allocated. */
230a3cf59f5SSuanming Mou 	uint32_t n_trunk; /* Trunk pointer array size. */
231a3cf59f5SSuanming Mou 	/* Dim of trunk pointer array. */
232a3cf59f5SSuanming Mou 	struct mlx5_indexed_trunk **trunks;
233a3cf59f5SSuanming Mou 	uint32_t free_list; /* Index to first free trunk. */
234a3cf59f5SSuanming Mou #ifdef POOL_DEBUG
235a3cf59f5SSuanming Mou 	uint32_t n_entry;
236a3cf59f5SSuanming Mou 	uint32_t trunk_new;
237a3cf59f5SSuanming Mou 	uint32_t trunk_avail;
238a3cf59f5SSuanming Mou 	uint32_t trunk_empty;
239a3cf59f5SSuanming Mou 	uint32_t trunk_free;
240a3cf59f5SSuanming Mou #endif
24162d7d519SSuanming Mou 	uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
242a3cf59f5SSuanming Mou };
243a3cf59f5SSuanming Mou 
244634efbc2SNelio Laranjeiro /**
24546287eacSBing Zhao  * Return logarithm of the nearest power of two above input value.
246634efbc2SNelio Laranjeiro  *
247634efbc2SNelio Laranjeiro  * @param v
248634efbc2SNelio Laranjeiro  *   Input value.
249634efbc2SNelio Laranjeiro  *
250634efbc2SNelio Laranjeiro  * @return
25146287eacSBing Zhao  *   Logarithm of the nearest power of two above input value.
252634efbc2SNelio Laranjeiro  */
253634efbc2SNelio Laranjeiro static inline unsigned int
254634efbc2SNelio Laranjeiro log2above(unsigned int v)
255634efbc2SNelio Laranjeiro {
256634efbc2SNelio Laranjeiro 	unsigned int l;
257634efbc2SNelio Laranjeiro 	unsigned int r;
258634efbc2SNelio Laranjeiro 
259634efbc2SNelio Laranjeiro 	for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
260634efbc2SNelio Laranjeiro 		r |= (v & 1);
261693f715dSHuawei Xie 	return l + r;
262634efbc2SNelio Laranjeiro }
263634efbc2SNelio Laranjeiro 
264e69a5922SXueming Li #define MLX5_HLIST_DIRECT_KEY 0x0001 /* Use the key directly as hash index. */
265e69a5922SXueming Li #define MLX5_HLIST_WRITE_MOST 0x0002 /* List mostly used for append new. */
266e69a5922SXueming Li 
26746287eacSBing Zhao /** Maximum size of string for naming the hlist table. */
26846287eacSBing Zhao #define MLX5_HLIST_NAMESIZE			32
26946287eacSBing Zhao 
270e69a5922SXueming Li struct mlx5_hlist;
271e69a5922SXueming Li 
27246287eacSBing Zhao /**
27346287eacSBing Zhao  * Structure of the entry in the hash list, user should define its own struct
27446287eacSBing Zhao  * that contains this in order to store the data. The 'key' is 64-bits right
27546287eacSBing Zhao  * now and its user's responsibility to guarantee there is no collision.
27646287eacSBing Zhao  */
27746287eacSBing Zhao struct mlx5_hlist_entry {
27846287eacSBing Zhao 	LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
279f5b0aed2SSuanming Mou 	uint32_t idx; /* Bucket index the entry belongs to. */
280e69a5922SXueming Li 	uint32_t ref_cnt; /* Reference count. */
28146287eacSBing Zhao };
28246287eacSBing Zhao 
28346287eacSBing Zhao /** Structure for hash head. */
28446287eacSBing Zhao LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
28546287eacSBing Zhao 
286e69a5922SXueming Li /**
287e69a5922SXueming Li  * Type of callback function for entry removal.
288e69a5922SXueming Li  *
289e69a5922SXueming Li  * @param list
290e69a5922SXueming Li  *   The hash list.
291e69a5922SXueming Li  * @param entry
292e69a5922SXueming Li  *   The entry in the list.
293e69a5922SXueming Li  */
294e69a5922SXueming Li typedef void (*mlx5_hlist_remove_cb)(struct mlx5_hlist *list,
295e69a5922SXueming Li 				     struct mlx5_hlist_entry *entry);
296e69a5922SXueming Li 
297e69a5922SXueming Li /**
298e69a5922SXueming Li  * Type of function for user defined matching.
299e69a5922SXueming Li  *
300e69a5922SXueming Li  * @param list
301e69a5922SXueming Li  *   The hash list.
302e69a5922SXueming Li  * @param entry
303e69a5922SXueming Li  *   The entry in the list.
304e69a5922SXueming Li  * @param key
305e69a5922SXueming Li  *   The new entry key.
306e69a5922SXueming Li  * @param ctx
307e69a5922SXueming Li  *   The pointer to new entry context.
308e69a5922SXueming Li  *
309e69a5922SXueming Li  * @return
310e69a5922SXueming Li  *   0 if matching, non-zero number otherwise.
311e69a5922SXueming Li  */
312e69a5922SXueming Li typedef int (*mlx5_hlist_match_cb)(struct mlx5_hlist *list,
313e69a5922SXueming Li 				   struct mlx5_hlist_entry *entry,
314e69a5922SXueming Li 				   uint64_t key, void *ctx);
315e69a5922SXueming Li 
316e69a5922SXueming Li /**
317e69a5922SXueming Li  * Type of function for user defined hash list entry creation.
318e69a5922SXueming Li  *
319e69a5922SXueming Li  * @param list
320e69a5922SXueming Li  *   The hash list.
321e69a5922SXueming Li  * @param key
322e69a5922SXueming Li  *   The key of the new entry.
323e69a5922SXueming Li  * @param ctx
324e69a5922SXueming Li  *   The pointer to new entry context.
325e69a5922SXueming Li  *
326e69a5922SXueming Li  * @return
327e69a5922SXueming Li  *   Pointer to allocated entry on success, NULL otherwise.
328e69a5922SXueming Li  */
329e69a5922SXueming Li typedef struct mlx5_hlist_entry *(*mlx5_hlist_create_cb)
330e69a5922SXueming Li 				  (struct mlx5_hlist *list,
331e69a5922SXueming Li 				   uint64_t key, void *ctx);
332e69a5922SXueming Li 
333d14cbf3dSSuanming Mou /* Hash list bucket head. */
334d14cbf3dSSuanming Mou struct mlx5_hlist_bucket {
335d14cbf3dSSuanming Mou 	struct mlx5_hlist_head head; /* List head. */
336d14cbf3dSSuanming Mou 	rte_rwlock_t lock; /* Bucket lock. */
337d14cbf3dSSuanming Mou 	uint32_t gen_cnt; /* List modification will update generation count. */
338d14cbf3dSSuanming Mou } __rte_cache_aligned;
339d14cbf3dSSuanming Mou 
340e69a5922SXueming Li /**
341e69a5922SXueming Li  * Hash list table structure
342e69a5922SXueming Li  *
343e69a5922SXueming Li  * Entry in hash list could be reused if entry already exists, reference
344e69a5922SXueming Li  * count will increase and the existing entry returns.
345e69a5922SXueming Li  *
346e69a5922SXueming Li  * When destroy an entry from list, decrease reference count and only
347e69a5922SXueming Li  * destroy when no further reference.
348e69a5922SXueming Li  */
34946287eacSBing Zhao struct mlx5_hlist {
35046287eacSBing Zhao 	char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
35146287eacSBing Zhao 	/**< number of heads, need to be power of 2. */
35246287eacSBing Zhao 	uint32_t table_sz;
353e69a5922SXueming Li 	uint32_t entry_sz; /**< Size of entry, used to allocate entry. */
35446287eacSBing Zhao 	/**< mask to get the index of the list heads. */
35546287eacSBing Zhao 	uint32_t mask;
356e69a5922SXueming Li 	bool direct_key; /* Use the new entry key directly as hash index. */
357e69a5922SXueming Li 	bool write_most; /* List mostly used for append new or destroy. */
358e69a5922SXueming Li 	void *ctx;
359e69a5922SXueming Li 	mlx5_hlist_create_cb cb_create; /**< entry create callback. */
360e69a5922SXueming Li 	mlx5_hlist_match_cb cb_match; /**< entry match callback. */
361e69a5922SXueming Li 	mlx5_hlist_remove_cb cb_remove; /**< entry remove callback. */
362d14cbf3dSSuanming Mou 	struct mlx5_hlist_bucket buckets[] __rte_cache_aligned;
363d14cbf3dSSuanming Mou 	/**< list bucket arrays. */
36446287eacSBing Zhao };
36546287eacSBing Zhao 
36646287eacSBing Zhao /**
36746287eacSBing Zhao  * Create a hash list table, the user can specify the list heads array size
36846287eacSBing Zhao  * of the table, now the size should be a power of 2 in order to get better
36946287eacSBing Zhao  * distribution for the entries. Each entry is a part of the whole data element
37046287eacSBing Zhao  * and the caller should be responsible for the data element's allocation and
37146287eacSBing Zhao  * cleanup / free. Key of each entry will be calculated with CRC in order to
37246287eacSBing Zhao  * generate a little fairer distribution.
37346287eacSBing Zhao  *
37446287eacSBing Zhao  * @param name
37546287eacSBing Zhao  *   Name of the hash list(optional).
37646287eacSBing Zhao  * @param size
37746287eacSBing Zhao  *   Heads array size of the hash list.
378e69a5922SXueming Li  * @param entry_size
379e69a5922SXueming Li  *   Entry size to allocate if cb_create not specified.
380e69a5922SXueming Li  * @param flags
381e69a5922SXueming Li  *   The hash list attribute flags.
382e69a5922SXueming Li  * @param cb_create
383e69a5922SXueming Li  *   Callback function for entry create.
384e69a5922SXueming Li  * @param cb_match
385e69a5922SXueming Li  *   Callback function for entry match.
386e69a5922SXueming Li  * @param cb_destroy
387e69a5922SXueming Li  *   Callback function for entry destroy.
38846287eacSBing Zhao  * @return
38946287eacSBing Zhao  *   Pointer of the hash list table created, NULL on failure.
39046287eacSBing Zhao  */
391e69a5922SXueming Li struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size,
392e69a5922SXueming Li 				     uint32_t entry_size, uint32_t flags,
393e69a5922SXueming Li 				     mlx5_hlist_create_cb cb_create,
394e69a5922SXueming Li 				     mlx5_hlist_match_cb cb_match,
395e69a5922SXueming Li 				     mlx5_hlist_remove_cb cb_destroy);
39646287eacSBing Zhao 
39746287eacSBing Zhao /**
39846287eacSBing Zhao  * Search an entry matching the key.
39946287eacSBing Zhao  *
400e69a5922SXueming Li  * Result returned might be destroyed by other thread, must use
401e69a5922SXueming Li  * this function only in main thread.
402e69a5922SXueming Li  *
40346287eacSBing Zhao  * @param h
40446287eacSBing Zhao  *   Pointer to the hast list table.
40546287eacSBing Zhao  * @param key
40646287eacSBing Zhao  *   Key for the searching entry.
407e69a5922SXueming Li  * @param ctx
408e69a5922SXueming Li  *   Common context parameter used by entry callback function.
40946287eacSBing Zhao  *
41046287eacSBing Zhao  * @return
41146287eacSBing Zhao  *   Pointer of the hlist entry if found, NULL otherwise.
41246287eacSBing Zhao  */
413e69a5922SXueming Li struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key,
414e69a5922SXueming Li 					   void *ctx);
41546287eacSBing Zhao 
41646287eacSBing Zhao /**
417e69a5922SXueming Li  * Insert an entry to the hash list table, the entry is only part of whole data
418e69a5922SXueming Li  * element and a 64B key is used for matching. User should construct the key or
419e69a5922SXueming Li  * give a calculated hash signature and guarantee there is no collision.
420e69a5922SXueming Li  *
421e69a5922SXueming Li  * @param h
422e69a5922SXueming Li  *   Pointer to the hast list table.
423e69a5922SXueming Li  * @param entry
424e69a5922SXueming Li  *   Entry to be inserted into the hash list table.
425e69a5922SXueming Li  * @param ctx
426e69a5922SXueming Li  *   Common context parameter used by callback function.
427e69a5922SXueming Li  *
428e69a5922SXueming Li  * @return
429e69a5922SXueming Li  *   registered entry on success, NULL otherwise
430e69a5922SXueming Li  */
431e69a5922SXueming Li struct mlx5_hlist_entry *mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key,
432e69a5922SXueming Li 					     void *ctx);
433e69a5922SXueming Li 
434e69a5922SXueming Li /**
43546287eacSBing Zhao  * Remove an entry from the hash list table. User should guarantee the validity
43646287eacSBing Zhao  * of the entry.
43746287eacSBing Zhao  *
43846287eacSBing Zhao  * @param h
43946287eacSBing Zhao  *   Pointer to the hast list table. (not used)
44046287eacSBing Zhao  * @param entry
44146287eacSBing Zhao  *   Entry to be removed from the hash list table.
442e69a5922SXueming Li  * @return
443e69a5922SXueming Li  *   0 on entry removed, 1 on entry still referenced.
44446287eacSBing Zhao  */
445e69a5922SXueming Li int mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
44646287eacSBing Zhao 
44746287eacSBing Zhao /**
44846287eacSBing Zhao  * Destroy the hash list table, all the entries already inserted into the lists
44946287eacSBing Zhao  * will be handled by the callback function provided by the user (including
45046287eacSBing Zhao  * free if needed) before the table is freed.
45146287eacSBing Zhao  *
45246287eacSBing Zhao  * @param h
45346287eacSBing Zhao  *   Pointer to the hast list table.
45446287eacSBing Zhao  */
455e69a5922SXueming Li void mlx5_hlist_destroy(struct mlx5_hlist *h);
45646287eacSBing Zhao 
4571ff37beeSXueming Li /************************ cache list *****************************/
4581ff37beeSXueming Li 
4591ff37beeSXueming Li /** Maximum size of string for naming. */
4601ff37beeSXueming Li #define MLX5_NAME_SIZE			32
4611ff37beeSXueming Li 
4621ff37beeSXueming Li struct mlx5_cache_list;
4631ff37beeSXueming Li 
4641ff37beeSXueming Li /**
4651ff37beeSXueming Li  * Structure of the entry in the cache list, user should define its own struct
4661ff37beeSXueming Li  * that contains this in order to store the data.
4671ff37beeSXueming Li  */
4681ff37beeSXueming Li struct mlx5_cache_entry {
4691ff37beeSXueming Li 	LIST_ENTRY(mlx5_cache_entry) next; /* Entry pointers in the list. */
4701ff37beeSXueming Li 	uint32_t ref_cnt; /* Reference count. */
4711ff37beeSXueming Li };
4721ff37beeSXueming Li 
4731ff37beeSXueming Li /**
4741ff37beeSXueming Li  * Type of callback function for entry removal.
4751ff37beeSXueming Li  *
4761ff37beeSXueming Li  * @param list
4771ff37beeSXueming Li  *   The cache list.
4781ff37beeSXueming Li  * @param entry
4791ff37beeSXueming Li  *   The entry in the list.
4801ff37beeSXueming Li  */
4811ff37beeSXueming Li typedef void (*mlx5_cache_remove_cb)(struct mlx5_cache_list *list,
4821ff37beeSXueming Li 				     struct mlx5_cache_entry *entry);
4831ff37beeSXueming Li 
4841ff37beeSXueming Li /**
4851ff37beeSXueming Li  * Type of function for user defined matching.
4861ff37beeSXueming Li  *
4871ff37beeSXueming Li  * @param list
4881ff37beeSXueming Li  *   The cache list.
4891ff37beeSXueming Li  * @param entry
4901ff37beeSXueming Li  *   The entry in the list.
4911ff37beeSXueming Li  * @param ctx
4921ff37beeSXueming Li  *   The pointer to new entry context.
4931ff37beeSXueming Li  *
4941ff37beeSXueming Li  * @return
4951ff37beeSXueming Li  *   0 if matching, non-zero number otherwise.
4961ff37beeSXueming Li  */
4971ff37beeSXueming Li typedef int (*mlx5_cache_match_cb)(struct mlx5_cache_list *list,
4981ff37beeSXueming Li 				   struct mlx5_cache_entry *entry, void *ctx);
4991ff37beeSXueming Li 
5001ff37beeSXueming Li /**
5011ff37beeSXueming Li  * Type of function for user defined cache list entry creation.
5021ff37beeSXueming Li  *
5031ff37beeSXueming Li  * @param list
5041ff37beeSXueming Li  *   The cache list.
5051ff37beeSXueming Li  * @param entry
5061ff37beeSXueming Li  *   The new allocated entry, NULL if list entry size unspecified,
5071ff37beeSXueming Li  *   New entry has to be allocated in callback and return.
5081ff37beeSXueming Li  * @param ctx
5091ff37beeSXueming Li  *   The pointer to new entry context.
5101ff37beeSXueming Li  *
5111ff37beeSXueming Li  * @return
5121ff37beeSXueming Li  *   Pointer of entry on success, NULL otherwise.
5131ff37beeSXueming Li  */
5141ff37beeSXueming Li typedef struct mlx5_cache_entry *(*mlx5_cache_create_cb)
5151ff37beeSXueming Li 				 (struct mlx5_cache_list *list,
5161ff37beeSXueming Li 				  struct mlx5_cache_entry *entry,
5171ff37beeSXueming Li 				  void *ctx);
5181ff37beeSXueming Li 
5191ff37beeSXueming Li /**
5201ff37beeSXueming Li  * Linked cache list structure.
5211ff37beeSXueming Li  *
5221ff37beeSXueming Li  * Entry in cache list could be reused if entry already exists,
5231ff37beeSXueming Li  * reference count will increase and the existing entry returns.
5241ff37beeSXueming Li  *
5251ff37beeSXueming Li  * When destroy an entry from list, decrease reference count and only
5261ff37beeSXueming Li  * destroy when no further reference.
5271ff37beeSXueming Li  *
5281ff37beeSXueming Li  * Linked list cache is designed for limited number of entries cache,
5291ff37beeSXueming Li  * read mostly, less modification.
5301ff37beeSXueming Li  *
5311ff37beeSXueming Li  * For huge amount of entries cache, please consider hash list cache.
5321ff37beeSXueming Li  *
5331ff37beeSXueming Li  */
5341ff37beeSXueming Li struct mlx5_cache_list {
5351ff37beeSXueming Li 	char name[MLX5_NAME_SIZE]; /**< Name of the cache list. */
5361ff37beeSXueming Li 	uint32_t entry_sz; /**< Entry size, 0: use create callback. */
5371ff37beeSXueming Li 	rte_rwlock_t lock; /* read/write lock. */
5381ff37beeSXueming Li 	uint32_t gen_cnt; /* List modification will update generation count. */
5391ff37beeSXueming Li 	uint32_t count; /* number of entries in list. */
5401ff37beeSXueming Li 	void *ctx; /* user objects target to callback. */
5411ff37beeSXueming Li 	mlx5_cache_create_cb cb_create; /**< entry create callback. */
5421ff37beeSXueming Li 	mlx5_cache_match_cb cb_match; /**< entry match callback. */
5431ff37beeSXueming Li 	mlx5_cache_remove_cb cb_remove; /**< entry remove callback. */
5441ff37beeSXueming Li 	LIST_HEAD(mlx5_cache_head, mlx5_cache_entry) head;
5451ff37beeSXueming Li };
5461ff37beeSXueming Li 
5471ff37beeSXueming Li /**
5481ff37beeSXueming Li  * Initialize a cache list.
5491ff37beeSXueming Li  *
5501ff37beeSXueming Li  * @param list
5511ff37beeSXueming Li  *   Pointer to the hast list table.
5521ff37beeSXueming Li  * @param name
5531ff37beeSXueming Li  *   Name of the cache list.
5541ff37beeSXueming Li  * @param entry_size
5551ff37beeSXueming Li  *   Entry size to allocate, 0 to allocate by creation callback.
5561ff37beeSXueming Li  * @param ctx
5571ff37beeSXueming Li  *   Pointer to the list context data.
5581ff37beeSXueming Li  * @param cb_create
5591ff37beeSXueming Li  *   Callback function for entry create.
5601ff37beeSXueming Li  * @param cb_match
5611ff37beeSXueming Li  *   Callback function for entry match.
5621ff37beeSXueming Li  * @param cb_remove
5631ff37beeSXueming Li  *   Callback function for entry remove.
5641ff37beeSXueming Li  * @return
5651ff37beeSXueming Li  *   0 on success, otherwise failure.
5661ff37beeSXueming Li  */
5671ff37beeSXueming Li int mlx5_cache_list_init(struct mlx5_cache_list *list,
5681ff37beeSXueming Li 			 const char *name, uint32_t entry_size, void *ctx,
5691ff37beeSXueming Li 			 mlx5_cache_create_cb cb_create,
5701ff37beeSXueming Li 			 mlx5_cache_match_cb cb_match,
5711ff37beeSXueming Li 			 mlx5_cache_remove_cb cb_remove);
5721ff37beeSXueming Li 
5731ff37beeSXueming Li /**
5741ff37beeSXueming Li  * Search an entry matching the key.
5751ff37beeSXueming Li  *
5761ff37beeSXueming Li  * Result returned might be destroyed by other thread, must use
5771ff37beeSXueming Li  * this function only in main thread.
5781ff37beeSXueming Li  *
5791ff37beeSXueming Li  * @param list
5801ff37beeSXueming Li  *   Pointer to the cache list.
5811ff37beeSXueming Li  * @param ctx
5821ff37beeSXueming Li  *   Common context parameter used by entry callback function.
5831ff37beeSXueming Li  *
5841ff37beeSXueming Li  * @return
5851ff37beeSXueming Li  *   Pointer of the cache entry if found, NULL otherwise.
5861ff37beeSXueming Li  */
5871ff37beeSXueming Li struct mlx5_cache_entry *mlx5_cache_lookup(struct mlx5_cache_list *list,
5881ff37beeSXueming Li 					   void *ctx);
5891ff37beeSXueming Li 
5901ff37beeSXueming Li /**
5911ff37beeSXueming Li  * Reuse or create an entry to the cache list.
5921ff37beeSXueming Li  *
5931ff37beeSXueming Li  * @param list
5941ff37beeSXueming Li  *   Pointer to the hast list table.
5951ff37beeSXueming Li  * @param ctx
5961ff37beeSXueming Li  *   Common context parameter used by callback function.
5971ff37beeSXueming Li  *
5981ff37beeSXueming Li  * @return
5991ff37beeSXueming Li  *   registered entry on success, NULL otherwise
6001ff37beeSXueming Li  */
6011ff37beeSXueming Li struct mlx5_cache_entry *mlx5_cache_register(struct mlx5_cache_list *list,
6021ff37beeSXueming Li 					     void *ctx);
6031ff37beeSXueming Li 
6041ff37beeSXueming Li /**
6051ff37beeSXueming Li  * Remove an entry from the cache list.
6061ff37beeSXueming Li  *
6071ff37beeSXueming Li  * User should guarantee the validity of the entry.
6081ff37beeSXueming Li  *
6091ff37beeSXueming Li  * @param list
6101ff37beeSXueming Li  *   Pointer to the hast list.
6111ff37beeSXueming Li  * @param entry
6121ff37beeSXueming Li  *   Entry to be removed from the cache list table.
6131ff37beeSXueming Li  * @return
6141ff37beeSXueming Li  *   0 on entry removed, 1 on entry still referenced.
6151ff37beeSXueming Li  */
6161ff37beeSXueming Li int mlx5_cache_unregister(struct mlx5_cache_list *list,
6171ff37beeSXueming Li 			  struct mlx5_cache_entry *entry);
6181ff37beeSXueming Li 
6191ff37beeSXueming Li /**
6201ff37beeSXueming Li  * Destroy the cache list.
6211ff37beeSXueming Li  *
6221ff37beeSXueming Li  * @param list
6231ff37beeSXueming Li  *   Pointer to the cache list.
6241ff37beeSXueming Li  */
6251ff37beeSXueming Li void mlx5_cache_list_destroy(struct mlx5_cache_list *list);
6261ff37beeSXueming Li 
6271ff37beeSXueming Li /**
6281ff37beeSXueming Li  * Get entry number from the cache list.
6291ff37beeSXueming Li  *
6301ff37beeSXueming Li  * @param list
6311ff37beeSXueming Li  *   Pointer to the hast list.
6321ff37beeSXueming Li  * @return
6331ff37beeSXueming Li  *   Cache list entry number.
6341ff37beeSXueming Li  */
6351ff37beeSXueming Li uint32_t
6361ff37beeSXueming Li mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list);
6371ff37beeSXueming Li 
6381ff37beeSXueming Li /********************************* indexed pool *************************/
6391ff37beeSXueming Li 
640a3cf59f5SSuanming Mou /**
641a3cf59f5SSuanming Mou  * This function allocates non-initialized memory entry from pool.
642a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
643a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
644a3cf59f5SSuanming Mou  *
645a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
646a3cf59f5SSuanming Mou  *
647a3cf59f5SSuanming Mou  * @param pool
648a3cf59f5SSuanming Mou  *   Pointer to indexed memory entry pool.
649a3cf59f5SSuanming Mou  *   No initialization required.
650a3cf59f5SSuanming Mou  * @param[out] idx
651a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
652a3cf59f5SSuanming Mou  *   Memory index always positive value.
653a3cf59f5SSuanming Mou  * @return
654a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry.
655a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
656a3cf59f5SSuanming Mou  */
657a3cf59f5SSuanming Mou void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
658a3cf59f5SSuanming Mou 
659a3cf59f5SSuanming Mou /**
660a3cf59f5SSuanming Mou  * This function allocates zero initialized memory entry from pool.
661a3cf59f5SSuanming Mou  * In NUMA systems, the memory entry allocated resides on the same
662a3cf59f5SSuanming Mou  * NUMA socket as the core that calls this function.
663a3cf59f5SSuanming Mou  *
664a3cf59f5SSuanming Mou  * Memory entry is allocated from memory trunk, no alignment.
665a3cf59f5SSuanming Mou  *
666a3cf59f5SSuanming Mou  * @param pool
667a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
668a3cf59f5SSuanming Mou  *   No initialization required.
669a3cf59f5SSuanming Mou  * @param[out] idx
670a3cf59f5SSuanming Mou  *   Pointer to memory to save allocated index.
671a3cf59f5SSuanming Mou  *   Memory index always positive value.
672a3cf59f5SSuanming Mou  * @return
673a3cf59f5SSuanming Mou  *   - Pointer to the allocated memory entry .
674a3cf59f5SSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
675a3cf59f5SSuanming Mou  */
676a3cf59f5SSuanming Mou void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
677a3cf59f5SSuanming Mou 
678a3cf59f5SSuanming Mou /**
679a3cf59f5SSuanming Mou  * This function frees indexed memory entry to pool.
680a3cf59f5SSuanming Mou  * Caller has to make sure that the index is allocated from same pool.
681a3cf59f5SSuanming Mou  *
682a3cf59f5SSuanming Mou  * @param pool
683a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
684a3cf59f5SSuanming Mou  * @param idx
685a3cf59f5SSuanming Mou  *   Allocated memory entry index.
686a3cf59f5SSuanming Mou  */
687a3cf59f5SSuanming Mou void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
688a3cf59f5SSuanming Mou 
689a3cf59f5SSuanming Mou /**
690a3cf59f5SSuanming Mou  * This function returns pointer of indexed memory entry from index.
691a3cf59f5SSuanming Mou  * Caller has to make sure that the index is valid, and allocated
692a3cf59f5SSuanming Mou  * from same pool.
693a3cf59f5SSuanming Mou  *
694a3cf59f5SSuanming Mou  * @param pool
695a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
696a3cf59f5SSuanming Mou  * @param idx
697a3cf59f5SSuanming Mou  *   Allocated memory index.
698a3cf59f5SSuanming Mou  * @return
699a3cf59f5SSuanming Mou  *   - Pointer to indexed memory entry.
700a3cf59f5SSuanming Mou  */
701a3cf59f5SSuanming Mou void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
702a3cf59f5SSuanming Mou 
703a3cf59f5SSuanming Mou /**
704a3cf59f5SSuanming Mou  * This function creates indexed memory pool.
705a3cf59f5SSuanming Mou  * Caller has to configure the configuration accordingly.
706a3cf59f5SSuanming Mou  *
707a3cf59f5SSuanming Mou  * @param pool
708a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
709a3cf59f5SSuanming Mou  * @param cfg
710a3cf59f5SSuanming Mou  *   Allocated memory index.
711a3cf59f5SSuanming Mou  */
712a3cf59f5SSuanming Mou struct mlx5_indexed_pool *
713a3cf59f5SSuanming Mou mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
714a3cf59f5SSuanming Mou 
715a3cf59f5SSuanming Mou /**
716a3cf59f5SSuanming Mou  * This function releases all resources of pool.
717a3cf59f5SSuanming Mou  * Caller has to make sure that all indexes and memories allocated
718a3cf59f5SSuanming Mou  * from this pool not referenced anymore.
719a3cf59f5SSuanming Mou  *
720a3cf59f5SSuanming Mou  * @param pool
721a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
722a3cf59f5SSuanming Mou  * @return
723a3cf59f5SSuanming Mou  *   - non-zero value on error.
724a3cf59f5SSuanming Mou  *   - 0 on success.
725a3cf59f5SSuanming Mou  */
726a3cf59f5SSuanming Mou int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
727a3cf59f5SSuanming Mou 
728a3cf59f5SSuanming Mou /**
729a3cf59f5SSuanming Mou  * This function dumps debug info of pool.
730a3cf59f5SSuanming Mou  *
731a3cf59f5SSuanming Mou  * @param pool
732a3cf59f5SSuanming Mou  *   Pointer to indexed memory pool.
733a3cf59f5SSuanming Mou  */
734a3cf59f5SSuanming Mou void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
735a3cf59f5SSuanming Mou 
736bd81eaebSSuanming Mou /**
737bd81eaebSSuanming Mou  * This function allocates new empty Three-level table.
738bd81eaebSSuanming Mou  *
739bd81eaebSSuanming Mou  * @param type
740bd81eaebSSuanming Mou  *   The l3t can set as word, double word, quad word or pointer with index.
741bd81eaebSSuanming Mou  *
742bd81eaebSSuanming Mou  * @return
743bd81eaebSSuanming Mou  *   - Pointer to the allocated l3t.
744bd81eaebSSuanming Mou  *   - NULL on error. Not enough memory, or invalid arguments.
745bd81eaebSSuanming Mou  */
746bd81eaebSSuanming Mou struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
747bd81eaebSSuanming Mou 
748bd81eaebSSuanming Mou /**
749bd81eaebSSuanming Mou  * This function destroys Three-level table.
750bd81eaebSSuanming Mou  *
751bd81eaebSSuanming Mou  * @param tbl
752bd81eaebSSuanming Mou  *   Pointer to the l3t.
753bd81eaebSSuanming Mou  */
754bd81eaebSSuanming Mou void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
755bd81eaebSSuanming Mou 
756bd81eaebSSuanming Mou /**
757bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
758bd81eaebSSuanming Mou  *
759bd81eaebSSuanming Mou  * @param tbl
760bd81eaebSSuanming Mou  *   Pointer to the l3t.
761bd81eaebSSuanming Mou  * @param idx
762bd81eaebSSuanming Mou  *   Index to the entry.
763bd81eaebSSuanming Mou  * @param data
764bd81eaebSSuanming Mou  *   Pointer to the memory which saves the entry data.
765bd81eaebSSuanming Mou  *   When function call returns 0, data contains the entry data get from
766bd81eaebSSuanming Mou  *   l3t.
767bd81eaebSSuanming Mou  *   When function call returns -1, data is not modified.
768bd81eaebSSuanming Mou  *
769bd81eaebSSuanming Mou  * @return
770bd81eaebSSuanming Mou  *   0 if success, -1 on error.
771bd81eaebSSuanming Mou  */
772bd81eaebSSuanming Mou 
7730796c7b1SSuanming Mou int32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
774bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
775bd81eaebSSuanming Mou 
776bd81eaebSSuanming Mou /**
777bd81eaebSSuanming Mou  * This function gets the index entry from Three-level table.
778bd81eaebSSuanming Mou  *
7790796c7b1SSuanming Mou  * If the index entry is not available, allocate new one by callback
7800796c7b1SSuanming Mou  * function and fill in the entry.
7810796c7b1SSuanming Mou  *
782bd81eaebSSuanming Mou  * @param tbl
783bd81eaebSSuanming Mou  *   Pointer to the l3t.
784bd81eaebSSuanming Mou  * @param idx
785bd81eaebSSuanming Mou  *   Index to the entry.
786bd81eaebSSuanming Mou  * @param data
7870796c7b1SSuanming Mou  *   Pointer to the memory which saves the entry data.
7880796c7b1SSuanming Mou  *   When function call returns 0, data contains the entry data get from
7890796c7b1SSuanming Mou  *   l3t.
7900796c7b1SSuanming Mou  *   When function call returns -1, data is not modified.
7910796c7b1SSuanming Mou  * @param cb
7920796c7b1SSuanming Mou  *   Callback function to allocate new data.
7930796c7b1SSuanming Mou  * @param ctx
7940796c7b1SSuanming Mou  *   Context for callback function.
795bd81eaebSSuanming Mou  *
796bd81eaebSSuanming Mou  * @return
797bd81eaebSSuanming Mou  *   0 if success, -1 on error.
798bd81eaebSSuanming Mou  */
7990796c7b1SSuanming Mou 
8000796c7b1SSuanming Mou int32_t mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
8010796c7b1SSuanming Mou 			       union mlx5_l3t_data *data,
8020796c7b1SSuanming Mou 			       mlx5_l3t_alloc_callback_fn cb, void *ctx);
8030796c7b1SSuanming Mou 
8040796c7b1SSuanming Mou /**
8050796c7b1SSuanming Mou  * This function decreases and clear index entry if reference
8060796c7b1SSuanming Mou  * counter is 0 from Three-level table.
8070796c7b1SSuanming Mou  *
8080796c7b1SSuanming Mou  * @param tbl
8090796c7b1SSuanming Mou  *   Pointer to the l3t.
8100796c7b1SSuanming Mou  * @param idx
8110796c7b1SSuanming Mou  *   Index to the entry.
8120796c7b1SSuanming Mou  *
8130796c7b1SSuanming Mou  * @return
8140796c7b1SSuanming Mou  *   The remaining reference count, 0 means entry be cleared, -1 on error.
8150796c7b1SSuanming Mou  */
8160796c7b1SSuanming Mou int32_t mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
8170796c7b1SSuanming Mou 
8180796c7b1SSuanming Mou /**
8190796c7b1SSuanming Mou  * This function sets the index entry to Three-level table.
8200796c7b1SSuanming Mou  * If the entry is already set, the EEXIST errno will be given, and
8210796c7b1SSuanming Mou  * the set data will be filled to the data.
8220796c7b1SSuanming Mou  *
8230796c7b1SSuanming Mou  * @param tbl[in]
8240796c7b1SSuanming Mou  *   Pointer to the l3t.
8250796c7b1SSuanming Mou  * @param idx[in]
8260796c7b1SSuanming Mou  *   Index to the entry.
8270796c7b1SSuanming Mou  * @param data[in/out]
8280796c7b1SSuanming Mou  *   Pointer to the memory which contains the entry data save to l3t.
8290796c7b1SSuanming Mou  *   If the entry is already set, the set data will be filled.
8300796c7b1SSuanming Mou  *
8310796c7b1SSuanming Mou  * @return
8320796c7b1SSuanming Mou  *   0 if success, -1 on error.
8330796c7b1SSuanming Mou  */
8340796c7b1SSuanming Mou int32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
835bd81eaebSSuanming Mou 			    union mlx5_l3t_data *data);
836bd81eaebSSuanming Mou 
837a3cf59f5SSuanming Mou /*
838a3cf59f5SSuanming Mou  * Macros for linked list based on indexed memory.
839a3cf59f5SSuanming Mou  * Example data structure:
840a3cf59f5SSuanming Mou  * struct Foo {
841a3cf59f5SSuanming Mou  *	ILIST_ENTRY(uint16_t) next;
842a3cf59f5SSuanming Mou  *	...
843a3cf59f5SSuanming Mou  * }
844a3cf59f5SSuanming Mou  *
845a3cf59f5SSuanming Mou  */
846a3cf59f5SSuanming Mou #define ILIST_ENTRY(type)						\
847a3cf59f5SSuanming Mou struct {								\
848a3cf59f5SSuanming Mou 	type prev; /* Index of previous element. */			\
849a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
850a3cf59f5SSuanming Mou }
851a3cf59f5SSuanming Mou 
852a3cf59f5SSuanming Mou #define ILIST_INSERT(pool, head, idx, elem, field)			\
853a3cf59f5SSuanming Mou 	do {								\
854a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
855a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
856a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
857a3cf59f5SSuanming Mou 		(elem)->field.prev = 0;					\
858a3cf59f5SSuanming Mou 		if (*(head)) {						\
859a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get(pool, *(head));		\
860a3cf59f5SSuanming Mou 			if (peer)					\
861a3cf59f5SSuanming Mou 				(peer)->field.prev = (idx);		\
862a3cf59f5SSuanming Mou 		}							\
863a3cf59f5SSuanming Mou 		*(head) = (idx);					\
864a3cf59f5SSuanming Mou 	} while (0)
865a3cf59f5SSuanming Mou 
866a3cf59f5SSuanming Mou #define ILIST_REMOVE(pool, head, idx, elem, field)			\
867a3cf59f5SSuanming Mou 	do {								\
868a3cf59f5SSuanming Mou 		typeof(elem) peer;					\
869a3cf59f5SSuanming Mou 		MLX5_ASSERT(elem);					\
870a3cf59f5SSuanming Mou 		MLX5_ASSERT(head);					\
871a3cf59f5SSuanming Mou 		if ((elem)->field.prev) {				\
872a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
873a3cf59f5SSuanming Mou 				 (pool, (elem)->field.prev);		\
874a3cf59f5SSuanming Mou 			if (peer)					\
875a3cf59f5SSuanming Mou 				(peer)->field.next = (elem)->field.next;\
876a3cf59f5SSuanming Mou 		}							\
877a3cf59f5SSuanming Mou 		if ((elem)->field.next) {				\
878a3cf59f5SSuanming Mou 			(peer) = mlx5_ipool_get				\
879a3cf59f5SSuanming Mou 				 (pool, (elem)->field.next);		\
880a3cf59f5SSuanming Mou 			if (peer)					\
881a3cf59f5SSuanming Mou 				(peer)->field.prev = (elem)->field.prev;\
882a3cf59f5SSuanming Mou 		}							\
883a3cf59f5SSuanming Mou 		if (*(head) == (idx))					\
884a3cf59f5SSuanming Mou 			*(head) = (elem)->field.next;			\
885a3cf59f5SSuanming Mou 	} while (0)
886a3cf59f5SSuanming Mou 
887a3cf59f5SSuanming Mou #define ILIST_FOREACH(pool, head, idx, elem, field)			\
888a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
889a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
890a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
891a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
892a3cf59f5SSuanming Mou 
893a3cf59f5SSuanming Mou /* Single index list. */
894a3cf59f5SSuanming Mou #define SILIST_ENTRY(type)						\
895a3cf59f5SSuanming Mou struct {								\
896a3cf59f5SSuanming Mou 	type next; /* Index of next element. */				\
897a3cf59f5SSuanming Mou }
898a3cf59f5SSuanming Mou 
899a3cf59f5SSuanming Mou #define SILIST_INSERT(head, idx, elem, field)				\
900a3cf59f5SSuanming Mou 	do {								\
901a3cf59f5SSuanming Mou 		MLX5_ASSERT((elem) && (idx));				\
902a3cf59f5SSuanming Mou 		(elem)->field.next = *(head);				\
903a3cf59f5SSuanming Mou 		*(head) = (idx);					\
904a3cf59f5SSuanming Mou 	} while (0)
905a3cf59f5SSuanming Mou 
906a3cf59f5SSuanming Mou #define SILIST_FOREACH(pool, head, idx, elem, field)			\
907a3cf59f5SSuanming Mou 	for ((idx) = (head), (elem) =					\
908a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
909a3cf59f5SSuanming Mou 	     idx = (elem)->field.next, (elem) =				\
910a3cf59f5SSuanming Mou 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
911a3cf59f5SSuanming Mou 
912771fa900SAdrien Mazarguil #endif /* RTE_PMD_MLX5_UTILS_H_ */
913