net/mlx5: add trunk dynamic grow for indexed pool
[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 /*
25  * Compilation workaround for PPC64 when AltiVec is fully enabled, e.g. std=c11.
26  * Otherwise there would be a type conflict between stdbool and altivec.
27  */
28 #if defined(__PPC64__) && !defined(__APPLE_ALTIVEC__)
29 #undef bool
30 /* redefine as in stdbool.h */
31 #define bool _Bool
32 #endif
33
34 /* Convert a bit number to the corresponding 64-bit mask */
35 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
36
37 /* Save and restore errno around argument evaluation. */
38 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
39
40 extern int mlx5_logtype;
41
42 /* Generic printf()-like logging macro with automatic line feed. */
43 #define DRV_LOG(level, ...) \
44         PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \
45                 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
46                 PMD_DRV_LOG_CPAREN)
47
48 #define INFO(...) DRV_LOG(INFO, __VA_ARGS__)
49 #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__)
50 #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__)
51
52 /* Convenience macros for accessing mbuf fields. */
53 #define NEXT(m) ((m)->next)
54 #define DATA_LEN(m) ((m)->data_len)
55 #define PKT_LEN(m) ((m)->pkt_len)
56 #define DATA_OFF(m) ((m)->data_off)
57 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
58 #define NB_SEGS(m) ((m)->nb_segs)
59 #define PORT(m) ((m)->port)
60
61 /* Transpose flags. Useful to convert IBV to DPDK flags. */
62 #define TRANSPOSE(val, from, to) \
63         (((from) >= (to)) ? \
64          (((val) & (from)) / ((from) / (to))) : \
65          (((val) & (from)) * ((to) / (from))))
66
67 /*
68  * The indexed memory entry index is made up of trunk index and offset of
69  * the entry in the trunk. Since the entry index is 32 bits, in case user
70  * prefers to have small trunks, user can change the macro below to a big
71  * number which helps the pool contains more trunks with lots of entries
72  * allocated.
73  */
74 #define TRUNK_IDX_BITS 16
75 #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
76 #define TRUNK_INVALID TRUNK_MAX_IDX
77 #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
78 #ifdef RTE_LIBRTE_MLX5_DEBUG
79 #define POOL_DEBUG 1
80 #endif
81
82 struct mlx5_indexed_pool_config {
83         uint32_t size; /* Pool entry size. */
84         uint32_t trunk_size:22;
85         /*
86          * Trunk entry number. Must be power of 2. It can be increased
87          * if trunk_grow enable. The trunk entry number increases with
88          * left shift grow_shift. Trunks with index are after grow_trunk
89          * will keep the entry number same with the last grow trunk.
90          */
91         uint32_t grow_trunk:4;
92         /*
93          * Trunks with entry number increase in the pool. Set it to 0
94          * to make the pool works as trunk entry fixed pool. It works
95          * only if grow_shift is not 0.
96          */
97         uint32_t grow_shift:4;
98         /*
99          * Trunk entry number increase shift value, stop after grow_trunk.
100          * It works only if grow_trunk is not 0.
101          */
102         uint32_t need_lock:1;
103         /* Lock is needed for multiple thread usage. */
104         const char *type; /* Memory allocate type name. */
105         void *(*malloc)(const char *type, size_t size, unsigned int align,
106                         int socket);
107         /* User defined memory allocator. */
108         void (*free)(void *addr); /* User defined memory release. */
109 };
110
111 struct mlx5_indexed_trunk {
112         uint32_t idx; /* Trunk id. */
113         uint32_t prev; /* Previous free trunk in free list. */
114         uint32_t next; /* Next free trunk in free list. */
115         uint32_t free; /* Free entries available */
116         struct rte_bitmap *bmp;
117         uint8_t data[] __rte_cache_min_aligned; /* Entry data start. */
118 };
119
120 struct mlx5_indexed_pool {
121         struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
122         rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
123         uint32_t n_trunk_valid; /* Trunks allocated. */
124         uint32_t n_trunk; /* Trunk pointer array size. */
125         /* Dim of trunk pointer array. */
126         struct mlx5_indexed_trunk **trunks;
127         uint32_t free_list; /* Index to first free trunk. */
128 #ifdef POOL_DEBUG
129         uint32_t n_entry;
130         uint32_t trunk_new;
131         uint32_t trunk_avail;
132         uint32_t trunk_empty;
133         uint32_t trunk_free;
134 #endif
135         uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
136 };
137
138 /**
139  * Return logarithm of the nearest power of two above input value.
140  *
141  * @param v
142  *   Input value.
143  *
144  * @return
145  *   Logarithm of the nearest power of two above input value.
146  */
147 static inline unsigned int
148 log2above(unsigned int v)
149 {
150         unsigned int l;
151         unsigned int r;
152
153         for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
154                 r |= (v & 1);
155         return l + r;
156 }
157
158 /** Maximum size of string for naming the hlist table. */
159 #define MLX5_HLIST_NAMESIZE                     32
160
161 /**
162  * Structure of the entry in the hash list, user should define its own struct
163  * that contains this in order to store the data. The 'key' is 64-bits right
164  * now and its user's responsibility to guarantee there is no collision.
165  */
166 struct mlx5_hlist_entry {
167         LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
168         uint64_t key; /* user defined 'key', could be the hash signature. */
169 };
170
171 /** Structure for hash head. */
172 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
173
174 /** Type of function that is used to handle the data before freeing. */
175 typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
176
177 /** hash list table structure */
178 struct mlx5_hlist {
179         char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
180         /**< number of heads, need to be power of 2. */
181         uint32_t table_sz;
182         /**< mask to get the index of the list heads. */
183         uint32_t mask;
184         struct mlx5_hlist_head heads[]; /**< list head arrays. */
185 };
186
187 /**
188  * Create a hash list table, the user can specify the list heads array size
189  * of the table, now the size should be a power of 2 in order to get better
190  * distribution for the entries. Each entry is a part of the whole data element
191  * and the caller should be responsible for the data element's allocation and
192  * cleanup / free. Key of each entry will be calculated with CRC in order to
193  * generate a little fairer distribution.
194  *
195  * @param name
196  *   Name of the hash list(optional).
197  * @param size
198  *   Heads array size of the hash list.
199  *
200  * @return
201  *   Pointer of the hash list table created, NULL on failure.
202  */
203 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size);
204
205 /**
206  * Search an entry matching the key.
207  *
208  * @param h
209  *   Pointer to the hast list table.
210  * @param key
211  *   Key for the searching entry.
212  *
213  * @return
214  *   Pointer of the hlist entry if found, NULL otherwise.
215  */
216 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key);
217
218 /**
219  * Insert an entry to the hash list table, the entry is only part of whole data
220  * element and a 64B key is used for matching. User should construct the key or
221  * give a calculated hash signature and guarantee there is no collision.
222  *
223  * @param h
224  *   Pointer to the hast list table.
225  * @param entry
226  *   Entry to be inserted into the hash list table.
227  *
228  * @return
229  *   - zero for success.
230  *   - -EEXIST if the entry is already inserted.
231  */
232 int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
233
234 /**
235  * Remove an entry from the hash list table. User should guarantee the validity
236  * of the entry.
237  *
238  * @param h
239  *   Pointer to the hast list table. (not used)
240  * @param entry
241  *   Entry to be removed from the hash list table.
242  */
243 void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused,
244                        struct mlx5_hlist_entry *entry);
245
246 /**
247  * Destroy the hash list table, all the entries already inserted into the lists
248  * will be handled by the callback function provided by the user (including
249  * free if needed) before the table is freed.
250  *
251  * @param h
252  *   Pointer to the hast list table.
253  * @param cb
254  *   Callback function for each inserted entry when destroying the hash list.
255  * @param ctx
256  *   Common context parameter used by callback function for each entry.
257  */
258 void mlx5_hlist_destroy(struct mlx5_hlist *h,
259                         mlx5_hlist_destroy_callback_fn cb, void *ctx);
260
261 /**
262  * This function allocates non-initialized memory entry from pool.
263  * In NUMA systems, the memory entry allocated resides on the same
264  * NUMA socket as the core that calls this function.
265  *
266  * Memory entry is allocated from memory trunk, no alignment.
267  *
268  * @param pool
269  *   Pointer to indexed memory entry pool.
270  *   No initialization required.
271  * @param[out] idx
272  *   Pointer to memory to save allocated index.
273  *   Memory index always positive value.
274  * @return
275  *   - Pointer to the allocated memory entry.
276  *   - NULL on error. Not enough memory, or invalid arguments.
277  */
278 void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
279
280 /**
281  * This function allocates zero initialized memory entry from pool.
282  * In NUMA systems, the memory entry allocated resides on the same
283  * NUMA socket as the core that calls this function.
284  *
285  * Memory entry is allocated from memory trunk, no alignment.
286  *
287  * @param pool
288  *   Pointer to indexed memory pool.
289  *   No initialization required.
290  * @param[out] idx
291  *   Pointer to memory to save allocated index.
292  *   Memory index always positive value.
293  * @return
294  *   - Pointer to the allocated memory entry .
295  *   - NULL on error. Not enough memory, or invalid arguments.
296  */
297 void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
298
299 /**
300  * This function frees indexed memory entry to pool.
301  * Caller has to make sure that the index is allocated from same pool.
302  *
303  * @param pool
304  *   Pointer to indexed memory pool.
305  * @param idx
306  *   Allocated memory entry index.
307  */
308 void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
309
310 /**
311  * This function returns pointer of indexed memory entry from index.
312  * Caller has to make sure that the index is valid, and allocated
313  * from same pool.
314  *
315  * @param pool
316  *   Pointer to indexed memory pool.
317  * @param idx
318  *   Allocated memory index.
319  * @return
320  *   - Pointer to indexed memory entry.
321  */
322 void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
323
324 /**
325  * This function creates indexed memory pool.
326  * Caller has to configure the configuration accordingly.
327  *
328  * @param pool
329  *   Pointer to indexed memory pool.
330  * @param cfg
331  *   Allocated memory index.
332  */
333 struct mlx5_indexed_pool *
334 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
335
336 /**
337  * This function releases all resources of pool.
338  * Caller has to make sure that all indexes and memories allocated
339  * from this pool not referenced anymore.
340  *
341  * @param pool
342  *   Pointer to indexed memory pool.
343  * @return
344  *   - non-zero value on error.
345  *   - 0 on success.
346  */
347 int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
348
349 /**
350  * This function dumps debug info of pool.
351  *
352  * @param pool
353  *   Pointer to indexed memory pool.
354  */
355 void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
356
357 /*
358  * Macros for linked list based on indexed memory.
359  * Example data structure:
360  * struct Foo {
361  *      ILIST_ENTRY(uint16_t) next;
362  *      ...
363  * }
364  *
365  */
366 #define ILIST_ENTRY(type)                                               \
367 struct {                                                                \
368         type prev; /* Index of previous element. */                     \
369         type next; /* Index of next element. */                         \
370 }
371
372 #define ILIST_INSERT(pool, head, idx, elem, field)                      \
373         do {                                                            \
374                 typeof(elem) peer;                                      \
375                 MLX5_ASSERT((elem) && (idx));                           \
376                 (elem)->field.next = *(head);                           \
377                 (elem)->field.prev = 0;                                 \
378                 if (*(head)) {                                          \
379                         (peer) = mlx5_ipool_get(pool, *(head));         \
380                         if (peer)                                       \
381                                 (peer)->field.prev = (idx);             \
382                 }                                                       \
383                 *(head) = (idx);                                        \
384         } while (0)
385
386 #define ILIST_REMOVE(pool, head, idx, elem, field)                      \
387         do {                                                            \
388                 typeof(elem) peer;                                      \
389                 MLX5_ASSERT(elem);                                      \
390                 MLX5_ASSERT(head);                                      \
391                 if ((elem)->field.prev) {                               \
392                         (peer) = mlx5_ipool_get                         \
393                                  (pool, (elem)->field.prev);            \
394                         if (peer)                                       \
395                                 (peer)->field.next = (elem)->field.next;\
396                 }                                                       \
397                 if ((elem)->field.next) {                               \
398                         (peer) = mlx5_ipool_get                         \
399                                  (pool, (elem)->field.next);            \
400                         if (peer)                                       \
401                                 (peer)->field.prev = (elem)->field.prev;\
402                 }                                                       \
403                 if (*(head) == (idx))                                   \
404                         *(head) = (elem)->field.next;                   \
405         } while (0)
406
407 #define ILIST_FOREACH(pool, head, idx, elem, field)                     \
408         for ((idx) = (head), (elem) =                                   \
409              (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);        \
410              idx = (elem)->field.next, (elem) =                         \
411              (idx) ? mlx5_ipool_get(pool, idx) : NULL)
412
413 /* Single index list. */
414 #define SILIST_ENTRY(type)                                              \
415 struct {                                                                \
416         type next; /* Index of next element. */                         \
417 }
418
419 #define SILIST_INSERT(head, idx, elem, field)                           \
420         do {                                                            \
421                 MLX5_ASSERT((elem) && (idx));                           \
422                 (elem)->field.next = *(head);                           \
423                 *(head) = (idx);                                        \
424         } while (0)
425
426 #define SILIST_FOREACH(pool, head, idx, elem, field)                    \
427         for ((idx) = (head), (elem) =                                   \
428              (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem);        \
429              idx = (elem)->field.next, (elem) =                         \
430              (idx) ? mlx5_ipool_get(pool, idx) : NULL)
431
432 #endif /* RTE_PMD_MLX5_UTILS_H_ */