xref: /dpdk/drivers/net/mlx5/mlx5_utils.h (revision 99f9d799ce21ab22e922ffec8aad51d56e24d04d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5 
6 #ifndef RTE_PMD_MLX5_UTILS_H_
7 #define RTE_PMD_MLX5_UTILS_H_
8 
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <limits.h>
13 #include <errno.h>
14 
15 #include <rte_spinlock.h>
16 #include <rte_rwlock.h>
17 #include <rte_memory.h>
18 #include <rte_bitmap.h>
19 
20 #include <mlx5_common.h>
21 #include <mlx5_common_utils.h>
22 
23 #include "mlx5_defs.h"
24 
25 /* Convert a bit number to the corresponding 64-bit mask */
26 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
27 
28 /* Save and restore errno around argument evaluation. */
29 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
30 
31 extern int mlx5_logtype;
32 
33 #define MLX5_NET_LOG_PREFIX "mlx5_net"
34 
35 /* Generic printf()-like logging macro with automatic line feed. */
36 #define DRV_LOG(level, ...) \
37 	PMD_DRV_LOG_(level, mlx5_logtype, MLX5_NET_LOG_PREFIX, \
38 		__VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
39 		PMD_DRV_LOG_CPAREN)
40 
41 /* Convenience macros for accessing mbuf fields. */
42 #define NEXT(m) ((m)->next)
43 #define DATA_LEN(m) ((m)->data_len)
44 #define PKT_LEN(m) ((m)->pkt_len)
45 #define DATA_OFF(m) ((m)->data_off)
46 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
47 #define NB_SEGS(m) ((m)->nb_segs)
48 #define PORT(m) ((m)->port)
49 
50 /* Transpose flags. Useful to convert IBV to DPDK flags. */
51 #define TRANSPOSE(val, from, to) \
52 	(((from) >= (to)) ? \
53 	 (((val) & (from)) / ((from) / (to))) : \
54 	 (((val) & (from)) * ((to) / (from))))
55 
56 /*
57  * For the case which data is linked with sequence increased index, the
58  * array table will be more efficiect than hash table once need to serarch
59  * one data entry in large numbers of entries. Since the traditional hash
60  * tables has fixed table size, when huge numbers of data saved to the hash
61  * table, it also comes lots of hash conflict.
62  *
63  * But simple array table also has fixed size, allocates all the needed
64  * memory at once will waste lots of memory. For the case don't know the
65  * exactly number of entries will be impossible to allocate the array.
66  *
67  * Then the multiple level table helps to balance the two disadvantages.
68  * Allocate a global high level table with sub table entries at first,
69  * the global table contains the sub table entries, and the sub table will
70  * be allocated only once the corresponding index entry need to be saved.
71  * e.g. for up to 32-bits index, three level table with 10-10-12 splitting,
72  * with sequence increased index, the memory grows with every 4K entries.
73  *
74  * The currently implementation introduces 10-10-12 32-bits splitting
75  * Three-Level table to help the cases which have millions of enties to
76  * save. The index entries can be addressed directly by the index, no
77  * search will be needed.q
78  */
79 
80 /* L3 table global table define. */
81 #define MLX5_L3T_GT_OFFSET 22
82 #define MLX5_L3T_GT_SIZE (1 << 10)
83 #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1)
84 
85 /* L3 table middle table define. */
86 #define MLX5_L3T_MT_OFFSET 12
87 #define MLX5_L3T_MT_SIZE (1 << 10)
88 #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1)
89 
90 /* L3 table entry table define. */
91 #define MLX5_L3T_ET_OFFSET 0
92 #define MLX5_L3T_ET_SIZE (1 << 12)
93 #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1)
94 
95 /* L3 table type. */
96 enum mlx5_l3t_type {
97 	MLX5_L3T_TYPE_WORD = 0,
98 	MLX5_L3T_TYPE_DWORD,
99 	MLX5_L3T_TYPE_QWORD,
100 	MLX5_L3T_TYPE_PTR,
101 	MLX5_L3T_TYPE_MAX,
102 };
103 
104 struct mlx5_indexed_pool;
105 
106 /* Generic data struct. */
107 union mlx5_l3t_data {
108 	uint16_t word;
109 	uint32_t dword;
110 	uint64_t qword;
111 	void *ptr;
112 };
113 
114 /* L3 level table data structure. */
115 struct mlx5_l3t_level_tbl {
116 	uint64_t ref_cnt; /* Table ref_cnt. */
117 	void *tbl[]; /* Table array. */
118 };
119 
120 /* L3 word entry table data structure. */
121 struct mlx5_l3t_entry_word {
122 	uint32_t idx; /* Table index. */
123 	uint64_t ref_cnt; /* Table ref_cnt. */
124 	struct {
125 		uint16_t data;
126 		uint32_t ref_cnt;
127 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
128 } __rte_packed;
129 
130 /* L3 double word entry table data structure. */
131 struct mlx5_l3t_entry_dword {
132 	uint32_t idx; /* Table index. */
133 	uint64_t ref_cnt; /* Table ref_cnt. */
134 	struct {
135 		uint32_t data;
136 		int32_t ref_cnt;
137 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
138 } __rte_packed;
139 
140 /* L3 quad word entry table data structure. */
141 struct mlx5_l3t_entry_qword {
142 	uint32_t idx; /* Table index. */
143 	uint64_t ref_cnt; /* Table ref_cnt. */
144 	struct {
145 		uint64_t data;
146 		uint32_t ref_cnt;
147 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
148 } __rte_packed;
149 
150 /* L3 pointer entry table data structure. */
151 struct mlx5_l3t_entry_ptr {
152 	uint32_t idx; /* Table index. */
153 	uint64_t ref_cnt; /* Table ref_cnt. */
154 	struct {
155 		void *data;
156 		uint32_t ref_cnt;
157 	} entry[MLX5_L3T_ET_SIZE]; /* Entry array */
158 } __rte_packed;
159 
160 /* L3 table data structure. */
161 struct mlx5_l3t_tbl {
162 	enum mlx5_l3t_type type; /* Table type. */
163 	struct mlx5_indexed_pool *eip;
164 	/* Table index pool handles. */
165 	struct mlx5_l3t_level_tbl *tbl; /* Global table index. */
166 	rte_spinlock_t sl; /* The table lock. */
167 };
168 
169 /** Type of function that is used to handle the data before freeing. */
170 typedef int32_t (*mlx5_l3t_alloc_callback_fn)(void *ctx,
171 					   union mlx5_l3t_data *data);
172 
173 /*
174  * The indexed memory entry index is made up of trunk index and offset of
175  * the entry in the trunk. Since the entry index is 32 bits, in case user
176  * prefers to have small trunks, user can change the macro below to a big
177  * number which helps the pool contains more trunks with lots of entries
178  * allocated.
179  */
180 #define TRUNK_IDX_BITS 16
181 #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
182 #define TRUNK_INVALID TRUNK_MAX_IDX
183 #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
184 #ifdef RTE_LIBRTE_MLX5_DEBUG
185 #define POOL_DEBUG 1
186 #endif
187 
188 struct mlx5_indexed_pool_config {
189 	uint32_t size; /* Pool entry size. */
190 	uint32_t trunk_size:22;
191 	/*
192 	 * Trunk entry number. Must be power of 2. It can be increased
193 	 * if trunk_grow enable. The trunk entry number increases with
194 	 * left shift grow_shift. Trunks with index are after grow_trunk
195 	 * will keep the entry number same with the last grow trunk.
196 	 */
197 	uint32_t grow_trunk:4;
198 	/*
199 	 * Trunks with entry number increase in the pool. Set it to 0
200 	 * to make the pool works as trunk entry fixed pool. It works
201 	 * only if grow_shift is not 0.
202 	 */
203 	uint32_t grow_shift:4;
204 	/*
205 	 * Trunk entry number increase shift value, stop after grow_trunk.
206 	 * It works only if grow_trunk is not 0.
207 	 */
208 	uint32_t need_lock:1;
209 	/* Lock is needed for multiple thread usage. */
210 	uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
211 	const char *type; /* Memory allocate type name. */
212 	void *(*malloc)(uint32_t flags, size_t size, unsigned int align,
213 			int socket);
214 	/* User defined memory allocator. */
215 	void (*free)(void *addr); /* User defined memory release. */
216 };
217 
218 struct mlx5_indexed_trunk {
219 	uint32_t idx; /* Trunk id. */
220 	uint32_t prev; /* Previous free trunk in free list. */
221 	uint32_t next; /* Next free trunk in free list. */
222 	uint32_t free; /* Free entries available */
223 	struct rte_bitmap *bmp;
224 	uint8_t data[] __rte_cache_aligned; /* Entry data start. */
225 };
226 
227 struct mlx5_indexed_pool {
228 	struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
229 	rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
230 	uint32_t n_trunk_valid; /* Trunks allocated. */
231 	uint32_t n_trunk; /* Trunk pointer array size. */
232 	/* Dim of trunk pointer array. */
233 	struct mlx5_indexed_trunk **trunks;
234 	uint32_t free_list; /* Index to first free trunk. */
235 #ifdef POOL_DEBUG
236 	uint32_t n_entry;
237 	uint32_t trunk_new;
238 	uint32_t trunk_avail;
239 	uint32_t trunk_empty;
240 	uint32_t trunk_free;
241 #endif
242 	uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
243 };
244 
245 /**
246  * Return logarithm of the nearest power of two above input value.
247  *
248  * @param v
249  *   Input value.
250  *
251  * @return
252  *   Logarithm of the nearest power of two above input value.
253  */
254 static inline unsigned int
255 log2above(unsigned int v)
256 {
257 	unsigned int l;
258 	unsigned int r;
259 
260 	for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
261 		r |= (v & 1);
262 	return l + r;
263 }
264 
265 /************************ cache list *****************************/
266 
267 /** Maximum size of string for naming. */
268 #define MLX5_NAME_SIZE			32
269 
270 struct mlx5_cache_list;
271 
272 /**
273  * Structure of the entry in the cache list, user should define its own struct
274  * that contains this in order to store the data.
275  */
276 struct mlx5_cache_entry {
277 	LIST_ENTRY(mlx5_cache_entry) next; /* Entry pointers in the list. */
278 	uint32_t ref_cnt; /* Reference count. */
279 };
280 
281 /**
282  * Type of callback function for entry removal.
283  *
284  * @param list
285  *   The cache list.
286  * @param entry
287  *   The entry in the list.
288  */
289 typedef void (*mlx5_cache_remove_cb)(struct mlx5_cache_list *list,
290 				     struct mlx5_cache_entry *entry);
291 
292 /**
293  * Type of function for user defined matching.
294  *
295  * @param list
296  *   The cache list.
297  * @param entry
298  *   The entry in the list.
299  * @param ctx
300  *   The pointer to new entry context.
301  *
302  * @return
303  *   0 if matching, non-zero number otherwise.
304  */
305 typedef int (*mlx5_cache_match_cb)(struct mlx5_cache_list *list,
306 				   struct mlx5_cache_entry *entry, void *ctx);
307 
308 /**
309  * Type of function for user defined cache list entry creation.
310  *
311  * @param list
312  *   The cache list.
313  * @param entry
314  *   The new allocated entry, NULL if list entry size unspecified,
315  *   New entry has to be allocated in callback and return.
316  * @param ctx
317  *   The pointer to new entry context.
318  *
319  * @return
320  *   Pointer of entry on success, NULL otherwise.
321  */
322 typedef struct mlx5_cache_entry *(*mlx5_cache_create_cb)
323 				 (struct mlx5_cache_list *list,
324 				  struct mlx5_cache_entry *entry,
325 				  void *ctx);
326 
327 /**
328  * Linked cache list structure.
329  *
330  * Entry in cache list could be reused if entry already exists,
331  * reference count will increase and the existing entry returns.
332  *
333  * When destroy an entry from list, decrease reference count and only
334  * destroy when no further reference.
335  *
336  * Linked list cache is designed for limited number of entries cache,
337  * read mostly, less modification.
338  *
339  * For huge amount of entries cache, please consider hash list cache.
340  *
341  */
342 struct mlx5_cache_list {
343 	char name[MLX5_NAME_SIZE]; /**< Name of the cache list. */
344 	uint32_t entry_sz; /**< Entry size, 0: use create callback. */
345 	rte_rwlock_t lock; /* read/write lock. */
346 	uint32_t gen_cnt; /* List modification will update generation count. */
347 	uint32_t count; /* number of entries in list. */
348 	void *ctx; /* user objects target to callback. */
349 	mlx5_cache_create_cb cb_create; /**< entry create callback. */
350 	mlx5_cache_match_cb cb_match; /**< entry match callback. */
351 	mlx5_cache_remove_cb cb_remove; /**< entry remove callback. */
352 	LIST_HEAD(mlx5_cache_head, mlx5_cache_entry) head;
353 };
354 
355 /**
356  * Initialize a cache list.
357  *
358  * @param list
359  *   Pointer to the hast list table.
360  * @param name
361  *   Name of the cache list.
362  * @param entry_size
363  *   Entry size to allocate, 0 to allocate by creation callback.
364  * @param ctx
365  *   Pointer to the list context data.
366  * @param cb_create
367  *   Callback function for entry create.
368  * @param cb_match
369  *   Callback function for entry match.
370  * @param cb_remove
371  *   Callback function for entry remove.
372  * @return
373  *   0 on success, otherwise failure.
374  */
375 int mlx5_cache_list_init(struct mlx5_cache_list *list,
376 			 const char *name, uint32_t entry_size, void *ctx,
377 			 mlx5_cache_create_cb cb_create,
378 			 mlx5_cache_match_cb cb_match,
379 			 mlx5_cache_remove_cb cb_remove);
380 
381 /**
382  * Search an entry matching the key.
383  *
384  * Result returned might be destroyed by other thread, must use
385  * this function only in main thread.
386  *
387  * @param list
388  *   Pointer to the cache list.
389  * @param ctx
390  *   Common context parameter used by entry callback function.
391  *
392  * @return
393  *   Pointer of the cache entry if found, NULL otherwise.
394  */
395 struct mlx5_cache_entry *mlx5_cache_lookup(struct mlx5_cache_list *list,
396 					   void *ctx);
397 
398 /**
399  * Reuse or create an entry to the cache list.
400  *
401  * @param list
402  *   Pointer to the hast list table.
403  * @param ctx
404  *   Common context parameter used by callback function.
405  *
406  * @return
407  *   registered entry on success, NULL otherwise
408  */
409 struct mlx5_cache_entry *mlx5_cache_register(struct mlx5_cache_list *list,
410 					     void *ctx);
411 
412 /**
413  * Remove an entry from the cache list.
414  *
415  * User should guarantee the validity of the entry.
416  *
417  * @param list
418  *   Pointer to the hast list.
419  * @param entry
420  *   Entry to be removed from the cache list table.
421  * @return
422  *   0 on entry removed, 1 on entry still referenced.
423  */
424 int mlx5_cache_unregister(struct mlx5_cache_list *list,
425 			  struct mlx5_cache_entry *entry);
426 
427 /**
428  * Destroy the cache list.
429  *
430  * @param list
431  *   Pointer to the cache list.
432  */
433 void mlx5_cache_list_destroy(struct mlx5_cache_list *list);
434 
435 /**
436  * Get entry number from the cache list.
437  *
438  * @param list
439  *   Pointer to the hast list.
440  * @return
441  *   Cache list entry number.
442  */
443 uint32_t
444 mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list);
445 
446 /********************************* indexed pool *************************/
447 
448 /**
449  * This function allocates non-initialized memory entry from pool.
450  * In NUMA systems, the memory entry allocated resides on the same
451  * NUMA socket as the core that calls this function.
452  *
453  * Memory entry is allocated from memory trunk, no alignment.
454  *
455  * @param pool
456  *   Pointer to indexed memory entry pool.
457  *   No initialization required.
458  * @param[out] idx
459  *   Pointer to memory to save allocated index.
460  *   Memory index always positive value.
461  * @return
462  *   - Pointer to the allocated memory entry.
463  *   - NULL on error. Not enough memory, or invalid arguments.
464  */
465 void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
466 
467 /**
468  * This function allocates zero initialized memory entry from pool.
469  * In NUMA systems, the memory entry allocated resides on the same
470  * NUMA socket as the core that calls this function.
471  *
472  * Memory entry is allocated from memory trunk, no alignment.
473  *
474  * @param pool
475  *   Pointer to indexed memory pool.
476  *   No initialization required.
477  * @param[out] idx
478  *   Pointer to memory to save allocated index.
479  *   Memory index always positive value.
480  * @return
481  *   - Pointer to the allocated memory entry .
482  *   - NULL on error. Not enough memory, or invalid arguments.
483  */
484 void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
485 
486 /**
487  * This function frees indexed memory entry to pool.
488  * Caller has to make sure that the index is allocated from same pool.
489  *
490  * @param pool
491  *   Pointer to indexed memory pool.
492  * @param idx
493  *   Allocated memory entry index.
494  */
495 void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
496 
497 /**
498  * This function returns pointer of indexed memory entry from index.
499  * Caller has to make sure that the index is valid, and allocated
500  * from same pool.
501  *
502  * @param pool
503  *   Pointer to indexed memory pool.
504  * @param idx
505  *   Allocated memory index.
506  * @return
507  *   - Pointer to indexed memory entry.
508  */
509 void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
510 
511 /**
512  * This function creates indexed memory pool.
513  * Caller has to configure the configuration accordingly.
514  *
515  * @param pool
516  *   Pointer to indexed memory pool.
517  * @param cfg
518  *   Allocated memory index.
519  */
520 struct mlx5_indexed_pool *
521 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
522 
523 /**
524  * This function releases all resources of pool.
525  * Caller has to make sure that all indexes and memories allocated
526  * from this pool not referenced anymore.
527  *
528  * @param pool
529  *   Pointer to indexed memory pool.
530  * @return
531  *   - non-zero value on error.
532  *   - 0 on success.
533  */
534 int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
535 
536 /**
537  * This function dumps debug info of pool.
538  *
539  * @param pool
540  *   Pointer to indexed memory pool.
541  */
542 void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
543 
544 /**
545  * This function allocates new empty Three-level table.
546  *
547  * @param type
548  *   The l3t can set as word, double word, quad word or pointer with index.
549  *
550  * @return
551  *   - Pointer to the allocated l3t.
552  *   - NULL on error. Not enough memory, or invalid arguments.
553  */
554 struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
555 
556 /**
557  * This function destroys Three-level table.
558  *
559  * @param tbl
560  *   Pointer to the l3t.
561  */
562 void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
563 
564 /**
565  * This function gets the index entry from Three-level table.
566  *
567  * @param tbl
568  *   Pointer to the l3t.
569  * @param idx
570  *   Index to the entry.
571  * @param data
572  *   Pointer to the memory which saves the entry data.
573  *   When function call returns 0, data contains the entry data get from
574  *   l3t.
575  *   When function call returns -1, data is not modified.
576  *
577  * @return
578  *   0 if success, -1 on error.
579  */
580 
581 int32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
582 			    union mlx5_l3t_data *data);
583 
584 /**
585  * This function gets the index entry from Three-level table.
586  *
587  * If the index entry is not available, allocate new one by callback
588  * function and fill in the entry.
589  *
590  * @param tbl
591  *   Pointer to the l3t.
592  * @param idx
593  *   Index to the entry.
594  * @param data
595  *   Pointer to the memory which saves the entry data.
596  *   When function call returns 0, data contains the entry data get from
597  *   l3t.
598  *   When function call returns -1, data is not modified.
599  * @param cb
600  *   Callback function to allocate new data.
601  * @param ctx
602  *   Context for callback function.
603  *
604  * @return
605  *   0 if success, -1 on error.
606  */
607 
608 int32_t mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
609 			       union mlx5_l3t_data *data,
610 			       mlx5_l3t_alloc_callback_fn cb, void *ctx);
611 
612 /**
613  * This function decreases and clear index entry if reference
614  * counter is 0 from Three-level table.
615  *
616  * @param tbl
617  *   Pointer to the l3t.
618  * @param idx
619  *   Index to the entry.
620  *
621  * @return
622  *   The remaining reference count, 0 means entry be cleared, -1 on error.
623  */
624 int32_t mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
625 
626 /**
627  * This function sets the index entry to Three-level table.
628  * If the entry is already set, the EEXIST errno will be given, and
629  * the set data will be filled to the data.
630  *
631  * @param tbl[in]
632  *   Pointer to the l3t.
633  * @param idx[in]
634  *   Index to the entry.
635  * @param data[in/out]
636  *   Pointer to the memory which contains the entry data save to l3t.
637  *   If the entry is already set, the set data will be filled.
638  *
639  * @return
640  *   0 if success, -1 on error.
641  */
642 int32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
643 			    union mlx5_l3t_data *data);
644 
645 static inline void *
646 mlx5_l3t_get_next(struct mlx5_l3t_tbl *tbl, uint32_t *pos)
647 {
648 	struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
649 	uint32_t i, j, k, g_start, m_start, e_start;
650 	uint32_t idx = *pos;
651 	void *e_tbl;
652 	struct mlx5_l3t_entry_word *w_e_tbl;
653 	struct mlx5_l3t_entry_dword *dw_e_tbl;
654 	struct mlx5_l3t_entry_qword *qw_e_tbl;
655 	struct mlx5_l3t_entry_ptr *ptr_e_tbl;
656 
657 	if (!tbl)
658 		return NULL;
659 	g_tbl = tbl->tbl;
660 	if (!g_tbl)
661 		return NULL;
662 	g_start = (idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK;
663 	m_start = (idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK;
664 	e_start = idx & MLX5_L3T_ET_MASK;
665 	for (i = g_start; i < MLX5_L3T_GT_SIZE; i++) {
666 		m_tbl = g_tbl->tbl[i];
667 		if (!m_tbl) {
668 			/* Jump to new table, reset the sub table start. */
669 			m_start = 0;
670 			e_start = 0;
671 			continue;
672 		}
673 		for (j = m_start; j < MLX5_L3T_MT_SIZE; j++) {
674 			if (!m_tbl->tbl[j]) {
675 				/*
676 				 * Jump to new table, reset the sub table
677 				 * start.
678 				 */
679 				e_start = 0;
680 				continue;
681 			}
682 			e_tbl = m_tbl->tbl[j];
683 			switch (tbl->type) {
684 			case MLX5_L3T_TYPE_WORD:
685 				w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
686 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
687 					if (!w_e_tbl->entry[k].data)
688 						continue;
689 					*pos = (i << MLX5_L3T_GT_OFFSET) |
690 					       (j << MLX5_L3T_MT_OFFSET) | k;
691 					return (void *)&w_e_tbl->entry[k].data;
692 				}
693 				break;
694 			case MLX5_L3T_TYPE_DWORD:
695 				dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
696 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
697 					if (!dw_e_tbl->entry[k].data)
698 						continue;
699 					*pos = (i << MLX5_L3T_GT_OFFSET) |
700 					       (j << MLX5_L3T_MT_OFFSET) | k;
701 					return (void *)&dw_e_tbl->entry[k].data;
702 				}
703 				break;
704 			case MLX5_L3T_TYPE_QWORD:
705 				qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
706 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
707 					if (!qw_e_tbl->entry[k].data)
708 						continue;
709 					*pos = (i << MLX5_L3T_GT_OFFSET) |
710 					       (j << MLX5_L3T_MT_OFFSET) | k;
711 					return (void *)&qw_e_tbl->entry[k].data;
712 				}
713 				break;
714 			default:
715 				ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
716 				for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
717 					if (!ptr_e_tbl->entry[k].data)
718 						continue;
719 					*pos = (i << MLX5_L3T_GT_OFFSET) |
720 					       (j << MLX5_L3T_MT_OFFSET) | k;
721 					return ptr_e_tbl->entry[k].data;
722 				}
723 				break;
724 			}
725 		}
726 	}
727 	return NULL;
728 }
729 
730 /*
731  * Macros for linked list based on indexed memory.
732  * Example data structure:
733  * struct Foo {
734  *	ILIST_ENTRY(uint16_t) next;
735  *	...
736  * }
737  *
738  */
739 #define ILIST_ENTRY(type)						\
740 struct {								\
741 	type prev; /* Index of previous element. */			\
742 	type next; /* Index of next element. */				\
743 }
744 
745 #define ILIST_INSERT(pool, head, idx, elem, field)			\
746 	do {								\
747 		typeof(elem) peer;					\
748 		MLX5_ASSERT((elem) && (idx));				\
749 		(elem)->field.next = *(head);				\
750 		(elem)->field.prev = 0;					\
751 		if (*(head)) {						\
752 			(peer) = mlx5_ipool_get(pool, *(head));		\
753 			if (peer)					\
754 				(peer)->field.prev = (idx);		\
755 		}							\
756 		*(head) = (idx);					\
757 	} while (0)
758 
759 #define ILIST_REMOVE(pool, head, idx, elem, field)			\
760 	do {								\
761 		typeof(elem) peer;					\
762 		MLX5_ASSERT(elem);					\
763 		MLX5_ASSERT(head);					\
764 		if ((elem)->field.prev) {				\
765 			(peer) = mlx5_ipool_get				\
766 				 (pool, (elem)->field.prev);		\
767 			if (peer)					\
768 				(peer)->field.next = (elem)->field.next;\
769 		}							\
770 		if ((elem)->field.next) {				\
771 			(peer) = mlx5_ipool_get				\
772 				 (pool, (elem)->field.next);		\
773 			if (peer)					\
774 				(peer)->field.prev = (elem)->field.prev;\
775 		}							\
776 		if (*(head) == (idx))					\
777 			*(head) = (elem)->field.next;			\
778 	} while (0)
779 
780 #define ILIST_FOREACH(pool, head, idx, elem, field)			\
781 	for ((idx) = (head), (elem) =					\
782 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
783 	     idx = (elem)->field.next, (elem) =				\
784 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
785 
786 /* Single index list. */
787 #define SILIST_ENTRY(type)						\
788 struct {								\
789 	type next; /* Index of next element. */				\
790 }
791 
792 #define SILIST_INSERT(head, idx, elem, field)				\
793 	do {								\
794 		MLX5_ASSERT((elem) && (idx));				\
795 		(elem)->field.next = *(head);				\
796 		*(head) = (idx);					\
797 	} while (0)
798 
799 #define SILIST_FOREACH(pool, head, idx, elem, field)			\
800 	for ((idx) = (head), (elem) =					\
801 	     (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);	\
802 	     idx = (elem)->field.next, (elem) =				\
803 	     (idx) ? mlx5_ipool_get(pool, idx) : NULL)
804 
805 #define MLX5_L3T_FOREACH(tbl, idx, entry)				\
806 	for (idx = 0, (entry) = mlx5_l3t_get_next((tbl), &idx);		\
807 	     (entry);							\
808 	     idx++, (entry) = mlx5_l3t_get_next((tbl), &idx))
809 
810 #endif /* RTE_PMD_MLX5_UTILS_H_ */
811