net/mlx5: allow limiting the indexed pool maximum index
[dpdk.git] / drivers / net / mlx5 / mlx5_utils.h
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         uint32_t max_idx; /* The maximum index can be allocated. */
212         const char *type; /* Memory allocate type name. */
213         void *(*malloc)(uint32_t flags, size_t size, unsigned int align,
214                         int socket);
215         /* User defined memory allocator. */
216         void (*free)(void *addr); /* User defined memory release. */
217 };
218
219 struct mlx5_indexed_trunk {
220         uint32_t idx; /* Trunk id. */
221         uint32_t prev; /* Previous free trunk in free list. */
222         uint32_t next; /* Next free trunk in free list. */
223         uint32_t free; /* Free entries available */
224         struct rte_bitmap *bmp;
225         uint8_t data[] __rte_cache_aligned; /* Entry data start. */
226 };
227
228 struct mlx5_indexed_pool {
229         struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
230         rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
231         uint32_t n_trunk_valid; /* Trunks allocated. */
232         uint32_t n_trunk; /* Trunk pointer array size. */
233         /* Dim of trunk pointer array. */
234         struct mlx5_indexed_trunk **trunks;
235         uint32_t free_list; /* Index to first free trunk. */
236 #ifdef POOL_DEBUG
237         uint32_t n_entry;
238         uint32_t trunk_new;
239         uint32_t trunk_avail;
240         uint32_t trunk_empty;
241         uint32_t trunk_free;
242 #endif
243         uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
244 };
245
246 /**
247  * Return logarithm of the nearest power of two above input value.
248  *
249  * @param v
250  *   Input value.
251  *
252  * @return
253  *   Logarithm of the nearest power of two above input value.
254  */
255 static inline unsigned int
256 log2above(unsigned int v)
257 {
258         unsigned int l;
259         unsigned int r;
260
261         for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
262                 r |= (v & 1);
263         return l + r;
264 }
265
266 /************************ cache list *****************************/
267
268 /** Maximum size of string for naming. */
269 #define MLX5_NAME_SIZE                  32
270
271 struct mlx5_cache_list;
272
273 /**
274  * Structure of the entry in the cache list, user should define its own struct
275  * that contains this in order to store the data.
276  */
277 struct mlx5_cache_entry {
278         LIST_ENTRY(mlx5_cache_entry) next; /* Entry pointers in the list. */
279         uint32_t ref_cnt; /* Reference count. */
280 };
281
282 /**
283  * Type of callback function for entry removal.
284  *
285  * @param list
286  *   The cache list.
287  * @param entry
288  *   The entry in the list.
289  */
290 typedef void (*mlx5_cache_remove_cb)(struct mlx5_cache_list *list,
291                                      struct mlx5_cache_entry *entry);
292
293 /**
294  * Type of function for user defined matching.
295  *
296  * @param list
297  *   The cache list.
298  * @param entry
299  *   The entry in the list.
300  * @param ctx
301  *   The pointer to new entry context.
302  *
303  * @return
304  *   0 if matching, non-zero number otherwise.
305  */
306 typedef int (*mlx5_cache_match_cb)(struct mlx5_cache_list *list,
307                                    struct mlx5_cache_entry *entry, void *ctx);
308
309 /**
310  * Type of function for user defined cache list entry creation.
311  *
312  * @param list
313  *   The cache list.
314  * @param entry
315  *   The new allocated entry, NULL if list entry size unspecified,
316  *   New entry has to be allocated in callback and return.
317  * @param ctx
318  *   The pointer to new entry context.
319  *
320  * @return
321  *   Pointer of entry on success, NULL otherwise.
322  */
323 typedef struct mlx5_cache_entry *(*mlx5_cache_create_cb)
324                                  (struct mlx5_cache_list *list,
325                                   struct mlx5_cache_entry *entry,
326                                   void *ctx);
327
328 /**
329  * Linked cache list structure.
330  *
331  * Entry in cache list could be reused if entry already exists,
332  * reference count will increase and the existing entry returns.
333  *
334  * When destroy an entry from list, decrease reference count and only
335  * destroy when no further reference.
336  *
337  * Linked list cache is designed for limited number of entries cache,
338  * read mostly, less modification.
339  *
340  * For huge amount of entries cache, please consider hash list cache.
341  *
342  */
343 struct mlx5_cache_list {
344         char name[MLX5_NAME_SIZE]; /**< Name of the cache list. */
345         uint32_t entry_sz; /**< Entry size, 0: use create callback. */
346         rte_rwlock_t lock; /* read/write lock. */
347         uint32_t gen_cnt; /* List modification will update generation count. */
348         uint32_t count; /* number of entries in list. */
349         void *ctx; /* user objects target to callback. */
350         mlx5_cache_create_cb cb_create; /**< entry create callback. */
351         mlx5_cache_match_cb cb_match; /**< entry match callback. */
352         mlx5_cache_remove_cb cb_remove; /**< entry remove callback. */
353         LIST_HEAD(mlx5_cache_head, mlx5_cache_entry) head;
354 };
355
356 /**
357  * Initialize a cache list.
358  *
359  * @param list
360  *   Pointer to the hast list table.
361  * @param name
362  *   Name of the cache list.
363  * @param entry_size
364  *   Entry size to allocate, 0 to allocate by creation callback.
365  * @param ctx
366  *   Pointer to the list context data.
367  * @param cb_create
368  *   Callback function for entry create.
369  * @param cb_match
370  *   Callback function for entry match.
371  * @param cb_remove
372  *   Callback function for entry remove.
373  * @return
374  *   0 on success, otherwise failure.
375  */
376 int mlx5_cache_list_init(struct mlx5_cache_list *list,
377                          const char *name, uint32_t entry_size, void *ctx,
378                          mlx5_cache_create_cb cb_create,
379                          mlx5_cache_match_cb cb_match,
380                          mlx5_cache_remove_cb cb_remove);
381
382 /**
383  * Search an entry matching the key.
384  *
385  * Result returned might be destroyed by other thread, must use
386  * this function only in main thread.
387  *
388  * @param list
389  *   Pointer to the cache list.
390  * @param ctx
391  *   Common context parameter used by entry callback function.
392  *
393  * @return
394  *   Pointer of the cache entry if found, NULL otherwise.
395  */
396 struct mlx5_cache_entry *mlx5_cache_lookup(struct mlx5_cache_list *list,
397                                            void *ctx);
398
399 /**
400  * Reuse or create an entry to the cache list.
401  *
402  * @param list
403  *   Pointer to the hast list table.
404  * @param ctx
405  *   Common context parameter used by callback function.
406  *
407  * @return
408  *   registered entry on success, NULL otherwise
409  */
410 struct mlx5_cache_entry *mlx5_cache_register(struct mlx5_cache_list *list,
411                                              void *ctx);
412
413 /**
414  * Remove an entry from the cache list.
415  *
416  * User should guarantee the validity of the entry.
417  *
418  * @param list
419  *   Pointer to the hast list.
420  * @param entry
421  *   Entry to be removed from the cache list table.
422  * @return
423  *   0 on entry removed, 1 on entry still referenced.
424  */
425 int mlx5_cache_unregister(struct mlx5_cache_list *list,
426                           struct mlx5_cache_entry *entry);
427
428 /**
429  * Destroy the cache list.
430  *
431  * @param list
432  *   Pointer to the cache list.
433  */
434 void mlx5_cache_list_destroy(struct mlx5_cache_list *list);
435
436 /**
437  * Get entry number from the cache list.
438  *
439  * @param list
440  *   Pointer to the hast list.
441  * @return
442  *   Cache list entry number.
443  */
444 uint32_t
445 mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list);
446
447 /********************************* indexed pool *************************/
448
449 /**
450  * This function allocates non-initialized memory entry from pool.
451  * In NUMA systems, the memory entry allocated resides on the same
452  * NUMA socket as the core that calls this function.
453  *
454  * Memory entry is allocated from memory trunk, no alignment.
455  *
456  * @param pool
457  *   Pointer to indexed memory entry pool.
458  *   No initialization required.
459  * @param[out] idx
460  *   Pointer to memory to save allocated index.
461  *   Memory index always positive value.
462  * @return
463  *   - Pointer to the allocated memory entry.
464  *   - NULL on error. Not enough memory, or invalid arguments.
465  */
466 void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
467
468 /**
469  * This function allocates zero initialized memory entry from pool.
470  * In NUMA systems, the memory entry allocated resides on the same
471  * NUMA socket as the core that calls this function.
472  *
473  * Memory entry is allocated from memory trunk, no alignment.
474  *
475  * @param pool
476  *   Pointer to indexed memory pool.
477  *   No initialization required.
478  * @param[out] idx
479  *   Pointer to memory to save allocated index.
480  *   Memory index always positive value.
481  * @return
482  *   - Pointer to the allocated memory entry .
483  *   - NULL on error. Not enough memory, or invalid arguments.
484  */
485 void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
486
487 /**
488  * This function frees indexed memory entry to pool.
489  * Caller has to make sure that the index is allocated from same pool.
490  *
491  * @param pool
492  *   Pointer to indexed memory pool.
493  * @param idx
494  *   Allocated memory entry index.
495  */
496 void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
497
498 /**
499  * This function returns pointer of indexed memory entry from index.
500  * Caller has to make sure that the index is valid, and allocated
501  * from same pool.
502  *
503  * @param pool
504  *   Pointer to indexed memory pool.
505  * @param idx
506  *   Allocated memory index.
507  * @return
508  *   - Pointer to indexed memory entry.
509  */
510 void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
511
512 /**
513  * This function creates indexed memory pool.
514  * Caller has to configure the configuration accordingly.
515  *
516  * @param pool
517  *   Pointer to indexed memory pool.
518  * @param cfg
519  *   Allocated memory index.
520  */
521 struct mlx5_indexed_pool *
522 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
523
524 /**
525  * This function releases all resources of pool.
526  * Caller has to make sure that all indexes and memories allocated
527  * from this pool not referenced anymore.
528  *
529  * @param pool
530  *   Pointer to indexed memory pool.
531  * @return
532  *   - non-zero value on error.
533  *   - 0 on success.
534  */
535 int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
536
537 /**
538  * This function dumps debug info of pool.
539  *
540  * @param pool
541  *   Pointer to indexed memory pool.
542  */
543 void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
544
545 /**
546  * This function allocates new empty Three-level table.
547  *
548  * @param type
549  *   The l3t can set as word, double word, quad word or pointer with index.
550  *
551  * @return
552  *   - Pointer to the allocated l3t.
553  *   - NULL on error. Not enough memory, or invalid arguments.
554  */
555 struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
556
557 /**
558  * This function destroys Three-level table.
559  *
560  * @param tbl
561  *   Pointer to the l3t.
562  */
563 void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
564
565 /**
566  * This function gets the index entry from Three-level table.
567  *
568  * @param tbl
569  *   Pointer to the l3t.
570  * @param idx
571  *   Index to the entry.
572  * @param data
573  *   Pointer to the memory which saves the entry data.
574  *   When function call returns 0, data contains the entry data get from
575  *   l3t.
576  *   When function call returns -1, data is not modified.
577  *
578  * @return
579  *   0 if success, -1 on error.
580  */
581
582 int32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
583                             union mlx5_l3t_data *data);
584
585 /**
586  * This function gets the index entry from Three-level table.
587  *
588  * If the index entry is not available, allocate new one by callback
589  * function and fill in the entry.
590  *
591  * @param tbl
592  *   Pointer to the l3t.
593  * @param idx
594  *   Index to the entry.
595  * @param data
596  *   Pointer to the memory which saves the entry data.
597  *   When function call returns 0, data contains the entry data get from
598  *   l3t.
599  *   When function call returns -1, data is not modified.
600  * @param cb
601  *   Callback function to allocate new data.
602  * @param ctx
603  *   Context for callback function.
604  *
605  * @return
606  *   0 if success, -1 on error.
607  */
608
609 int32_t mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
610                                union mlx5_l3t_data *data,
611                                mlx5_l3t_alloc_callback_fn cb, void *ctx);
612
613 /**
614  * This function decreases and clear index entry if reference
615  * counter is 0 from Three-level table.
616  *
617  * @param tbl
618  *   Pointer to the l3t.
619  * @param idx
620  *   Index to the entry.
621  *
622  * @return
623  *   The remaining reference count, 0 means entry be cleared, -1 on error.
624  */
625 int32_t mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
626
627 /**
628  * This function sets the index entry to Three-level table.
629  * If the entry is already set, the EEXIST errno will be given, and
630  * the set data will be filled to the data.
631  *
632  * @param tbl[in]
633  *   Pointer to the l3t.
634  * @param idx[in]
635  *   Index to the entry.
636  * @param data[in/out]
637  *   Pointer to the memory which contains the entry data save to l3t.
638  *   If the entry is already set, the set data will be filled.
639  *
640  * @return
641  *   0 if success, -1 on error.
642  */
643 int32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
644                             union mlx5_l3t_data *data);
645
646 static inline void *
647 mlx5_l3t_get_next(struct mlx5_l3t_tbl *tbl, uint32_t *pos)
648 {
649         struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
650         uint32_t i, j, k, g_start, m_start, e_start;
651         uint32_t idx = *pos;
652         void *e_tbl;
653         struct mlx5_l3t_entry_word *w_e_tbl;
654         struct mlx5_l3t_entry_dword *dw_e_tbl;
655         struct mlx5_l3t_entry_qword *qw_e_tbl;
656         struct mlx5_l3t_entry_ptr *ptr_e_tbl;
657
658         if (!tbl)
659                 return NULL;
660         g_tbl = tbl->tbl;
661         if (!g_tbl)
662                 return NULL;
663         g_start = (idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK;
664         m_start = (idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK;
665         e_start = idx & MLX5_L3T_ET_MASK;
666         for (i = g_start; i < MLX5_L3T_GT_SIZE; i++) {
667                 m_tbl = g_tbl->tbl[i];
668                 if (!m_tbl) {
669                         /* Jump to new table, reset the sub table start. */
670                         m_start = 0;
671                         e_start = 0;
672                         continue;
673                 }
674                 for (j = m_start; j < MLX5_L3T_MT_SIZE; j++) {
675                         if (!m_tbl->tbl[j]) {
676                                 /*
677                                  * Jump to new table, reset the sub table
678                                  * start.
679                                  */
680                                 e_start = 0;
681                                 continue;
682                         }
683                         e_tbl = m_tbl->tbl[j];
684                         switch (tbl->type) {
685                         case MLX5_L3T_TYPE_WORD:
686                                 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
687                                 for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
688                                         if (!w_e_tbl->entry[k].data)
689                                                 continue;
690                                         *pos = (i << MLX5_L3T_GT_OFFSET) |
691                                                (j << MLX5_L3T_MT_OFFSET) | k;
692                                         return (void *)&w_e_tbl->entry[k].data;
693                                 }
694                                 break;
695                         case MLX5_L3T_TYPE_DWORD:
696                                 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
697                                 for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
698                                         if (!dw_e_tbl->entry[k].data)
699                                                 continue;
700                                         *pos = (i << MLX5_L3T_GT_OFFSET) |
701                                                (j << MLX5_L3T_MT_OFFSET) | k;
702                                         return (void *)&dw_e_tbl->entry[k].data;
703                                 }
704                                 break;
705                         case MLX5_L3T_TYPE_QWORD:
706                                 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
707                                 for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
708                                         if (!qw_e_tbl->entry[k].data)
709                                                 continue;
710                                         *pos = (i << MLX5_L3T_GT_OFFSET) |
711                                                (j << MLX5_L3T_MT_OFFSET) | k;
712                                         return (void *)&qw_e_tbl->entry[k].data;
713                                 }
714                                 break;
715                         default:
716                                 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
717                                 for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) {
718                                         if (!ptr_e_tbl->entry[k].data)
719                                                 continue;
720                                         *pos = (i << MLX5_L3T_GT_OFFSET) |
721                                                (j << MLX5_L3T_MT_OFFSET) | k;
722                                         return ptr_e_tbl->entry[k].data;
723                                 }
724                                 break;
725                         }
726                 }
727         }
728         return NULL;
729 }
730
731 /*
732  * Macros for linked list based on indexed memory.
733  * Example data structure:
734  * struct Foo {
735  *      ILIST_ENTRY(uint16_t) next;
736  *      ...
737  * }
738  *
739  */
740 #define ILIST_ENTRY(type)                                               \
741 struct {                                                                \
742         type prev; /* Index of previous element. */                     \
743         type next; /* Index of next element. */                         \
744 }
745
746 #define ILIST_INSERT(pool, head, idx, elem, field)                      \
747         do {                                                            \
748                 typeof(elem) peer;                                      \
749                 MLX5_ASSERT((elem) && (idx));                           \
750                 (elem)->field.next = *(head);                           \
751                 (elem)->field.prev = 0;                                 \
752                 if (*(head)) {                                          \
753                         (peer) = mlx5_ipool_get(pool, *(head));         \
754                         if (peer)                                       \
755                                 (peer)->field.prev = (idx);             \
756                 }                                                       \
757                 *(head) = (idx);                                        \
758         } while (0)
759
760 #define ILIST_REMOVE(pool, head, idx, elem, field)                      \
761         do {                                                            \
762                 typeof(elem) peer;                                      \
763                 MLX5_ASSERT(elem);                                      \
764                 MLX5_ASSERT(head);                                      \
765                 if ((elem)->field.prev) {                               \
766                         (peer) = mlx5_ipool_get                         \
767                                  (pool, (elem)->field.prev);            \
768                         if (peer)                                       \
769                                 (peer)->field.next = (elem)->field.next;\
770                 }                                                       \
771                 if ((elem)->field.next) {                               \
772                         (peer) = mlx5_ipool_get                         \
773                                  (pool, (elem)->field.next);            \
774                         if (peer)                                       \
775                                 (peer)->field.prev = (elem)->field.prev;\
776                 }                                                       \
777                 if (*(head) == (idx))                                   \
778                         *(head) = (elem)->field.next;                   \
779         } while (0)
780
781 #define ILIST_FOREACH(pool, head, idx, elem, field)                     \
782         for ((idx) = (head), (elem) =                                   \
783              (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);        \
784              idx = (elem)->field.next, (elem) =                         \
785              (idx) ? mlx5_ipool_get(pool, idx) : NULL)
786
787 /* Single index list. */
788 #define SILIST_ENTRY(type)                                              \
789 struct {                                                                \
790         type next; /* Index of next element. */                         \
791 }
792
793 #define SILIST_INSERT(head, idx, elem, field)                           \
794         do {                                                            \
795                 MLX5_ASSERT((elem) && (idx));                           \
796                 (elem)->field.next = *(head);                           \
797                 *(head) = (idx);                                        \
798         } while (0)
799
800 #define SILIST_FOREACH(pool, head, idx, elem, field)                    \
801         for ((idx) = (head), (elem) =                                   \
802              (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);        \
803              idx = (elem)->field.next, (elem) =                         \
804              (idx) ? mlx5_ipool_get(pool, idx) : NULL)
805
806 #define MLX5_L3T_FOREACH(tbl, idx, entry)                               \
807         for (idx = 0, (entry) = mlx5_l3t_get_next((tbl), &idx);         \
808              (entry);                                                   \
809              idx++, (entry) = mlx5_l3t_get_next((tbl), &idx))
810
811 #endif /* RTE_PMD_MLX5_UTILS_H_ */