f078bdc65ae84c373676400621415a0f3ede72f0
[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_memory.h>
17 #include <rte_bitmap.h>
18
19 #include <mlx5_common.h>
20
21 #include "mlx5_defs.h"
22
23
24 /* Convert a bit number to the corresponding 64-bit mask */
25 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
26
27 /* Save and restore errno around argument evaluation. */
28 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
29
30 extern int mlx5_logtype;
31
32 /* Generic printf()-like logging macro with automatic line feed. */
33 #define DRV_LOG(level, ...) \
34         PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \
35                 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
36                 PMD_DRV_LOG_CPAREN)
37
38 /* Convenience macros for accessing mbuf fields. */
39 #define NEXT(m) ((m)->next)
40 #define DATA_LEN(m) ((m)->data_len)
41 #define PKT_LEN(m) ((m)->pkt_len)
42 #define DATA_OFF(m) ((m)->data_off)
43 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
44 #define NB_SEGS(m) ((m)->nb_segs)
45 #define PORT(m) ((m)->port)
46
47 /* Transpose flags. Useful to convert IBV to DPDK flags. */
48 #define TRANSPOSE(val, from, to) \
49         (((from) >= (to)) ? \
50          (((val) & (from)) / ((from) / (to))) : \
51          (((val) & (from)) * ((to) / (from))))
52
53 /*
54  * For the case which data is linked with sequence increased index, the
55  * array table will be more efficiect than hash table once need to serarch
56  * one data entry in large numbers of entries. Since the traditional hash
57  * tables has fixed table size, when huge numbers of data saved to the hash
58  * table, it also comes lots of hash conflict.
59  *
60  * But simple array table also has fixed size, allocates all the needed
61  * memory at once will waste lots of memory. For the case don't know the
62  * exactly number of entries will be impossible to allocate the array.
63  *
64  * Then the multiple level table helps to balance the two disadvantages.
65  * Allocate a global high level table with sub table entries at first,
66  * the global table contains the sub table entries, and the sub table will
67  * be allocated only once the corresponding index entry need to be saved.
68  * e.g. for up to 32-bits index, three level table with 10-10-12 splitting,
69  * with sequence increased index, the memory grows with every 4K entries.
70  *
71  * The currently implementation introduces 10-10-12 32-bits splitting
72  * Three-Level table to help the cases which have millions of enties to
73  * save. The index entries can be addressed directly by the index, no
74  * search will be needed.q
75  */
76
77 /* L3 table global table define. */
78 #define MLX5_L3T_GT_OFFSET 22
79 #define MLX5_L3T_GT_SIZE (1 << 10)
80 #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1)
81
82 /* L3 table middle table define. */
83 #define MLX5_L3T_MT_OFFSET 12
84 #define MLX5_L3T_MT_SIZE (1 << 10)
85 #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1)
86
87 /* L3 table entry table define. */
88 #define MLX5_L3T_ET_OFFSET 0
89 #define MLX5_L3T_ET_SIZE (1 << 12)
90 #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1)
91
92 /* L3 table type. */
93 enum mlx5_l3t_type {
94         MLX5_L3T_TYPE_WORD = 0,
95         MLX5_L3T_TYPE_DWORD,
96         MLX5_L3T_TYPE_QWORD,
97         MLX5_L3T_TYPE_PTR,
98         MLX5_L3T_TYPE_MAX,
99 };
100
101 struct mlx5_indexed_pool;
102
103 /* Generic data struct. */
104 union mlx5_l3t_data {
105         uint16_t word;
106         uint32_t dword;
107         uint64_t qword;
108         void *ptr;
109 };
110
111 /* L3 level table data structure. */
112 struct mlx5_l3t_level_tbl {
113         uint64_t ref_cnt; /* Table ref_cnt. */
114         void *tbl[]; /* Table array. */
115 };
116
117 /* L3 word entry table data structure. */
118 struct mlx5_l3t_entry_word {
119         uint32_t idx; /* Table index. */
120         uint64_t ref_cnt; /* Table ref_cnt. */
121         uint16_t entry[]; /* Entry array. */
122 };
123
124 /* L3 double word entry table data structure. */
125 struct mlx5_l3t_entry_dword {
126         uint32_t idx; /* Table index. */
127         uint64_t ref_cnt; /* Table ref_cnt. */
128         uint32_t entry[]; /* Entry array. */
129 };
130
131 /* L3 quad word entry table data structure. */
132 struct mlx5_l3t_entry_qword {
133         uint32_t idx; /* Table index. */
134         uint64_t ref_cnt; /* Table ref_cnt. */
135         uint64_t entry[]; /* Entry array. */
136 };
137
138 /* L3 pointer entry table data structure. */
139 struct mlx5_l3t_entry_ptr {
140         uint32_t idx; /* Table index. */
141         uint64_t ref_cnt; /* Table ref_cnt. */
142         void *entry[]; /* Entry array. */
143 };
144
145 /* L3 table data structure. */
146 struct mlx5_l3t_tbl {
147         enum mlx5_l3t_type type; /* Table type. */
148         struct mlx5_indexed_pool *eip;
149         /* Table index pool handles. */
150         struct mlx5_l3t_level_tbl *tbl; /* Global table index. */
151 };
152
153 /*
154  * The indexed memory entry index is made up of trunk index and offset of
155  * the entry in the trunk. Since the entry index is 32 bits, in case user
156  * prefers to have small trunks, user can change the macro below to a big
157  * number which helps the pool contains more trunks with lots of entries
158  * allocated.
159  */
160 #define TRUNK_IDX_BITS 16
161 #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
162 #define TRUNK_INVALID TRUNK_MAX_IDX
163 #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
164 #ifdef RTE_LIBRTE_MLX5_DEBUG
165 #define POOL_DEBUG 1
166 #endif
167
168 struct mlx5_indexed_pool_config {
169         uint32_t size; /* Pool entry size. */
170         uint32_t trunk_size:22;
171         /*
172          * Trunk entry number. Must be power of 2. It can be increased
173          * if trunk_grow enable. The trunk entry number increases with
174          * left shift grow_shift. Trunks with index are after grow_trunk
175          * will keep the entry number same with the last grow trunk.
176          */
177         uint32_t grow_trunk:4;
178         /*
179          * Trunks with entry number increase in the pool. Set it to 0
180          * to make the pool works as trunk entry fixed pool. It works
181          * only if grow_shift is not 0.
182          */
183         uint32_t grow_shift:4;
184         /*
185          * Trunk entry number increase shift value, stop after grow_trunk.
186          * It works only if grow_trunk is not 0.
187          */
188         uint32_t need_lock:1;
189         /* Lock is needed for multiple thread usage. */
190         uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
191         const char *type; /* Memory allocate type name. */
192         void *(*malloc)(uint32_t flags, size_t size, unsigned int align,
193                         int socket);
194         /* User defined memory allocator. */
195         void (*free)(void *addr); /* User defined memory release. */
196 };
197
198 struct mlx5_indexed_trunk {
199         uint32_t idx; /* Trunk id. */
200         uint32_t prev; /* Previous free trunk in free list. */
201         uint32_t next; /* Next free trunk in free list. */
202         uint32_t free; /* Free entries available */
203         struct rte_bitmap *bmp;
204         uint8_t data[] __rte_cache_aligned; /* Entry data start. */
205 };
206
207 struct mlx5_indexed_pool {
208         struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
209         rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
210         uint32_t n_trunk_valid; /* Trunks allocated. */
211         uint32_t n_trunk; /* Trunk pointer array size. */
212         /* Dim of trunk pointer array. */
213         struct mlx5_indexed_trunk **trunks;
214         uint32_t free_list; /* Index to first free trunk. */
215 #ifdef POOL_DEBUG
216         uint32_t n_entry;
217         uint32_t trunk_new;
218         uint32_t trunk_avail;
219         uint32_t trunk_empty;
220         uint32_t trunk_free;
221 #endif
222         uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
223 };
224
225 /**
226  * Return logarithm of the nearest power of two above input value.
227  *
228  * @param v
229  *   Input value.
230  *
231  * @return
232  *   Logarithm of the nearest power of two above input value.
233  */
234 static inline unsigned int
235 log2above(unsigned int v)
236 {
237         unsigned int l;
238         unsigned int r;
239
240         for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
241                 r |= (v & 1);
242         return l + r;
243 }
244
245 /** Maximum size of string for naming the hlist table. */
246 #define MLX5_HLIST_NAMESIZE                     32
247
248 /**
249  * Structure of the entry in the hash list, user should define its own struct
250  * that contains this in order to store the data. The 'key' is 64-bits right
251  * now and its user's responsibility to guarantee there is no collision.
252  */
253 struct mlx5_hlist_entry {
254         LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
255         uint64_t key; /* user defined 'key', could be the hash signature. */
256 };
257
258 /** Structure for hash head. */
259 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
260
261 /** Type of function that is used to handle the data before freeing. */
262 typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
263
264 /**
265  * Type of function for user defined matching.
266  *
267  * @param entry
268  *   The entry in the list.
269  * @param ctx
270  *   The pointer to new entry context.
271  *
272  * @return
273  *   0 if matching, -1 otherwise.
274  */
275 typedef int (*mlx5_hlist_match_callback_fn)(struct mlx5_hlist_entry *entry,
276                                              void *ctx);
277
278 /** hash list table structure */
279 struct mlx5_hlist {
280         char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
281         /**< number of heads, need to be power of 2. */
282         uint32_t table_sz;
283         /**< mask to get the index of the list heads. */
284         uint32_t mask;
285         struct mlx5_hlist_head heads[]; /**< list head arrays. */
286 };
287
288 /**
289  * Create a hash list table, the user can specify the list heads array size
290  * of the table, now the size should be a power of 2 in order to get better
291  * distribution for the entries. Each entry is a part of the whole data element
292  * and the caller should be responsible for the data element's allocation and
293  * cleanup / free. Key of each entry will be calculated with CRC in order to
294  * generate a little fairer distribution.
295  *
296  * @param name
297  *   Name of the hash list(optional).
298  * @param size
299  *   Heads array size of the hash list.
300  *
301  * @return
302  *   Pointer of the hash list table created, NULL on failure.
303  */
304 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size);
305
306 /**
307  * Search an entry matching the key.
308  *
309  * @param h
310  *   Pointer to the hast list table.
311  * @param key
312  *   Key for the searching entry.
313  *
314  * @return
315  *   Pointer of the hlist entry if found, NULL otherwise.
316  */
317 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key);
318
319 /**
320  * Insert an entry to the hash list table, the entry is only part of whole data
321  * element and a 64B key is used for matching. User should construct the key or
322  * give a calculated hash signature and guarantee there is no collision.
323  *
324  * @param h
325  *   Pointer to the hast list table.
326  * @param entry
327  *   Entry to be inserted into the hash list table.
328  *
329  * @return
330  *   - zero for success.
331  *   - -EEXIST if the entry is already inserted.
332  */
333 int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
334
335 /**
336  * Extended routine to search an entry matching the context with
337  * user defined match function.
338  *
339  * @param h
340  *   Pointer to the hast list table.
341  * @param key
342  *   Key for the searching entry.
343  * @param cb
344  *   Callback function to match the node with context.
345  * @param ctx
346  *   Common context parameter used by callback function.
347  *
348  * @return
349  *   Pointer of the hlist entry if found, NULL otherwise.
350  */
351 struct mlx5_hlist_entry *mlx5_hlist_lookup_ex(struct mlx5_hlist *h,
352                                               uint64_t key,
353                                               mlx5_hlist_match_callback_fn cb,
354                                               void *ctx);
355
356 /**
357  * Extended routine to insert an entry to the list with key collisions.
358  *
359  * For the list have key collision, the extra user defined match function
360  * allows node with same key will be inserted.
361  *
362  * @param h
363  *   Pointer to the hast list table.
364  * @param entry
365  *   Entry to be inserted into the hash list table.
366  * @param cb
367  *   Callback function to match the node with context.
368  * @param ctx
369  *   Common context parameter used by callback function.
370  *
371  * @return
372  *   - zero for success.
373  *   - -EEXIST if the entry is already inserted.
374  */
375 int mlx5_hlist_insert_ex(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry,
376                          mlx5_hlist_match_callback_fn cb, void *ctx);
377
378 /**
379  * Remove an entry from the hash list table. User should guarantee the validity
380  * of the entry.
381  *
382  * @param h
383  *   Pointer to the hast list table. (not used)
384  * @param entry
385  *   Entry to be removed from the hash list table.
386  */
387 void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused,
388                        struct mlx5_hlist_entry *entry);
389
390 /**
391  * Destroy the hash list table, all the entries already inserted into the lists
392  * will be handled by the callback function provided by the user (including
393  * free if needed) before the table is freed.
394  *
395  * @param h
396  *   Pointer to the hast list table.
397  * @param cb
398  *   Callback function for each inserted entry when destroying the hash list.
399  * @param ctx
400  *   Common context parameter used by callback function for each entry.
401  */
402 void mlx5_hlist_destroy(struct mlx5_hlist *h,
403                         mlx5_hlist_destroy_callback_fn cb, void *ctx);
404
405 /**
406  * This function allocates non-initialized memory entry from pool.
407  * In NUMA systems, the memory entry allocated resides on the same
408  * NUMA socket as the core that calls this function.
409  *
410  * Memory entry is allocated from memory trunk, no alignment.
411  *
412  * @param pool
413  *   Pointer to indexed memory entry pool.
414  *   No initialization required.
415  * @param[out] idx
416  *   Pointer to memory to save allocated index.
417  *   Memory index always positive value.
418  * @return
419  *   - Pointer to the allocated memory entry.
420  *   - NULL on error. Not enough memory, or invalid arguments.
421  */
422 void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
423
424 /**
425  * This function allocates zero initialized memory entry from pool.
426  * In NUMA systems, the memory entry allocated resides on the same
427  * NUMA socket as the core that calls this function.
428  *
429  * Memory entry is allocated from memory trunk, no alignment.
430  *
431  * @param pool
432  *   Pointer to indexed memory pool.
433  *   No initialization required.
434  * @param[out] idx
435  *   Pointer to memory to save allocated index.
436  *   Memory index always positive value.
437  * @return
438  *   - Pointer to the allocated memory entry .
439  *   - NULL on error. Not enough memory, or invalid arguments.
440  */
441 void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
442
443 /**
444  * This function frees indexed memory entry to pool.
445  * Caller has to make sure that the index is allocated from same pool.
446  *
447  * @param pool
448  *   Pointer to indexed memory pool.
449  * @param idx
450  *   Allocated memory entry index.
451  */
452 void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
453
454 /**
455  * This function returns pointer of indexed memory entry from index.
456  * Caller has to make sure that the index is valid, and allocated
457  * from same pool.
458  *
459  * @param pool
460  *   Pointer to indexed memory pool.
461  * @param idx
462  *   Allocated memory index.
463  * @return
464  *   - Pointer to indexed memory entry.
465  */
466 void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
467
468 /**
469  * This function creates indexed memory pool.
470  * Caller has to configure the configuration accordingly.
471  *
472  * @param pool
473  *   Pointer to indexed memory pool.
474  * @param cfg
475  *   Allocated memory index.
476  */
477 struct mlx5_indexed_pool *
478 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
479
480 /**
481  * This function releases all resources of pool.
482  * Caller has to make sure that all indexes and memories allocated
483  * from this pool not referenced anymore.
484  *
485  * @param pool
486  *   Pointer to indexed memory pool.
487  * @return
488  *   - non-zero value on error.
489  *   - 0 on success.
490  */
491 int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
492
493 /**
494  * This function dumps debug info of pool.
495  *
496  * @param pool
497  *   Pointer to indexed memory pool.
498  */
499 void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
500
501 /**
502  * This function allocates new empty Three-level table.
503  *
504  * @param type
505  *   The l3t can set as word, double word, quad word or pointer with index.
506  *
507  * @return
508  *   - Pointer to the allocated l3t.
509  *   - NULL on error. Not enough memory, or invalid arguments.
510  */
511 struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
512
513 /**
514  * This function destroys Three-level table.
515  *
516  * @param tbl
517  *   Pointer to the l3t.
518  */
519 void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
520
521 /**
522  * This function gets the index entry from Three-level table.
523  *
524  * @param tbl
525  *   Pointer to the l3t.
526  * @param idx
527  *   Index to the entry.
528  * @param data
529  *   Pointer to the memory which saves the entry data.
530  *   When function call returns 0, data contains the entry data get from
531  *   l3t.
532  *   When function call returns -1, data is not modified.
533  *
534  * @return
535  *   0 if success, -1 on error.
536  */
537
538 uint32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
539                             union mlx5_l3t_data *data);
540 /**
541  * This function clears the index entry from Three-level table.
542  *
543  * @param tbl
544  *   Pointer to the l3t.
545  * @param idx
546  *   Index to the entry.
547  */
548 void mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
549
550 /**
551  * This function gets the index entry from Three-level table.
552  *
553  * @param tbl
554  *   Pointer to the l3t.
555  * @param idx
556  *   Index to the entry.
557  * @param data
558  *   Pointer to the memory which contains the entry data save to l3t.
559  *
560  * @return
561  *   0 if success, -1 on error.
562  */
563 uint32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
564                             union mlx5_l3t_data *data);
565
566 /*
567  * Macros for linked list based on indexed memory.
568  * Example data structure:
569  * struct Foo {
570  *      ILIST_ENTRY(uint16_t) next;
571  *      ...
572  * }
573  *
574  */
575 #define ILIST_ENTRY(type)                                               \
576 struct {                                                                \
577         type prev; /* Index of previous element. */                     \
578         type next; /* Index of next element. */                         \
579 }
580
581 #define ILIST_INSERT(pool, head, idx, elem, field)                      \
582         do {                                                            \
583                 typeof(elem) peer;                                      \
584                 MLX5_ASSERT((elem) && (idx));                           \
585                 (elem)->field.next = *(head);                           \
586                 (elem)->field.prev = 0;                                 \
587                 if (*(head)) {                                          \
588                         (peer) = mlx5_ipool_get(pool, *(head));         \
589                         if (peer)                                       \
590                                 (peer)->field.prev = (idx);             \
591                 }                                                       \
592                 *(head) = (idx);                                        \
593         } while (0)
594
595 #define ILIST_REMOVE(pool, head, idx, elem, field)                      \
596         do {                                                            \
597                 typeof(elem) peer;                                      \
598                 MLX5_ASSERT(elem);                                      \
599                 MLX5_ASSERT(head);                                      \
600                 if ((elem)->field.prev) {                               \
601                         (peer) = mlx5_ipool_get                         \
602                                  (pool, (elem)->field.prev);            \
603                         if (peer)                                       \
604                                 (peer)->field.next = (elem)->field.next;\
605                 }                                                       \
606                 if ((elem)->field.next) {                               \
607                         (peer) = mlx5_ipool_get                         \
608                                  (pool, (elem)->field.next);            \
609                         if (peer)                                       \
610                                 (peer)->field.prev = (elem)->field.prev;\
611                 }                                                       \
612                 if (*(head) == (idx))                                   \
613                         *(head) = (elem)->field.next;                   \
614         } while (0)
615
616 #define ILIST_FOREACH(pool, head, idx, elem, field)                     \
617         for ((idx) = (head), (elem) =                                   \
618              (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);        \
619              idx = (elem)->field.next, (elem) =                         \
620              (idx) ? mlx5_ipool_get(pool, idx) : NULL)
621
622 /* Single index list. */
623 #define SILIST_ENTRY(type)                                              \
624 struct {                                                                \
625         type next; /* Index of next element. */                         \
626 }
627
628 #define SILIST_INSERT(head, idx, elem, field)                           \
629         do {                                                            \
630                 MLX5_ASSERT((elem) && (idx));                           \
631                 (elem)->field.next = *(head);                           \
632                 *(head) = (idx);                                        \
633         } while (0)
634
635 #define SILIST_FOREACH(pool, head, idx, elem, field)                    \
636         for ((idx) = (head), (elem) =                                   \
637              (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);        \
638              idx = (elem)->field.next, (elem) =                         \
639              (idx) ? mlx5_ipool_get(pool, idx) : NULL)
640
641 #endif /* RTE_PMD_MLX5_UTILS_H_ */