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