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