common/mlx5: introduce common library
[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 <assert.h>
14 #include <errno.h>
15
16 #include <mlx5_common.h>
17
18 #include "mlx5_defs.h"
19
20
21 /*
22  * Compilation workaround for PPC64 when AltiVec is fully enabled, e.g. std=c11.
23  * Otherwise there would be a type conflict between stdbool and altivec.
24  */
25 #if defined(__PPC64__) && !defined(__APPLE_ALTIVEC__)
26 #undef bool
27 /* redefine as in stdbool.h */
28 #define bool _Bool
29 #endif
30
31 /* Bit-field manipulation. */
32 #define BITFIELD_DECLARE(bf, type, size) \
33         type bf[(((size_t)(size) / (sizeof(type) * CHAR_BIT)) + \
34                  !!((size_t)(size) % (sizeof(type) * CHAR_BIT)))]
35 #define BITFIELD_DEFINE(bf, type, size) \
36         BITFIELD_DECLARE((bf), type, (size)) = { 0 }
37 #define BITFIELD_SET(bf, b) \
38         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
39          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] |= \
40                 ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
41 #define BITFIELD_RESET(bf, b) \
42         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
43          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] &= \
44                 ~((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
45 #define BITFIELD_ISSET(bf, b) \
46         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
47          !!(((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] & \
48              ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT))))))
49
50 /* Convert a bit number to the corresponding 64-bit mask */
51 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
52
53 /* Save and restore errno around argument evaluation. */
54 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
55
56 extern int mlx5_logtype;
57
58 /* Generic printf()-like logging macro with automatic line feed. */
59 #define DRV_LOG(level, ...) \
60         PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \
61                 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
62                 PMD_DRV_LOG_CPAREN)
63
64 #define INFO(...) DRV_LOG(INFO, __VA_ARGS__)
65 #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__)
66 #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__)
67
68 /* Convenience macros for accessing mbuf fields. */
69 #define NEXT(m) ((m)->next)
70 #define DATA_LEN(m) ((m)->data_len)
71 #define PKT_LEN(m) ((m)->pkt_len)
72 #define DATA_OFF(m) ((m)->data_off)
73 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
74 #define NB_SEGS(m) ((m)->nb_segs)
75 #define PORT(m) ((m)->port)
76
77 /* Transpose flags. Useful to convert IBV to DPDK flags. */
78 #define TRANSPOSE(val, from, to) \
79         (((from) >= (to)) ? \
80          (((val) & (from)) / ((from) / (to))) : \
81          (((val) & (from)) * ((to) / (from))))
82
83 /**
84  * Return logarithm of the nearest power of two above input value.
85  *
86  * @param v
87  *   Input value.
88  *
89  * @return
90  *   Logarithm of the nearest power of two above input value.
91  */
92 static inline unsigned int
93 log2above(unsigned int v)
94 {
95         unsigned int l;
96         unsigned int r;
97
98         for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
99                 r |= (v & 1);
100         return l + r;
101 }
102
103 /** Maximum size of string for naming the hlist table. */
104 #define MLX5_HLIST_NAMESIZE                     32
105
106 /**
107  * Structure of the entry in the hash list, user should define its own struct
108  * that contains this in order to store the data. The 'key' is 64-bits right
109  * now and its user's responsibility to guarantee there is no collision.
110  */
111 struct mlx5_hlist_entry {
112         LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
113         uint64_t key; /* user defined 'key', could be the hash signature. */
114 };
115
116 /** Structure for hash head. */
117 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
118
119 /** Type of function that is used to handle the data before freeing. */
120 typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
121
122 /** hash list table structure */
123 struct mlx5_hlist {
124         char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
125         /**< number of heads, need to be power of 2. */
126         uint32_t table_sz;
127         /**< mask to get the index of the list heads. */
128         uint32_t mask;
129         struct mlx5_hlist_head heads[]; /**< list head arrays. */
130 };
131
132 /**
133  * Create a hash list table, the user can specify the list heads array size
134  * of the table, now the size should be a power of 2 in order to get better
135  * distribution for the entries. Each entry is a part of the whole data element
136  * and the caller should be responsible for the data element's allocation and
137  * cleanup / free. Key of each entry will be calculated with CRC in order to
138  * generate a little fairer distribution.
139  *
140  * @param name
141  *   Name of the hash list(optional).
142  * @param size
143  *   Heads array size of the hash list.
144  *
145  * @return
146  *   Pointer of the hash list table created, NULL on failure.
147  */
148 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size);
149
150 /**
151  * Search an entry matching the key.
152  *
153  * @param h
154  *   Pointer to the hast list table.
155  * @param key
156  *   Key for the searching entry.
157  *
158  * @return
159  *   Pointer of the hlist entry if found, NULL otherwise.
160  */
161 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key);
162
163 /**
164  * Insert an entry to the hash list table, the entry is only part of whole data
165  * element and a 64B key is used for matching. User should construct the key or
166  * give a calculated hash signature and guarantee there is no collision.
167  *
168  * @param h
169  *   Pointer to the hast list table.
170  * @param entry
171  *   Entry to be inserted into the hash list table.
172  *
173  * @return
174  *   - zero for success.
175  *   - -EEXIST if the entry is already inserted.
176  */
177 int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
178
179 /**
180  * Remove an entry from the hash list table. User should guarantee the validity
181  * of the entry.
182  *
183  * @param h
184  *   Pointer to the hast list table. (not used)
185  * @param entry
186  *   Entry to be removed from the hash list table.
187  */
188 void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused,
189                        struct mlx5_hlist_entry *entry);
190
191 /**
192  * Destroy the hash list table, all the entries already inserted into the lists
193  * will be handled by the callback function provided by the user (including
194  * free if needed) before the table is freed.
195  *
196  * @param h
197  *   Pointer to the hast list table.
198  * @param cb
199  *   Callback function for each inserted entry when destroying the hash list.
200  * @param ctx
201  *   Common context parameter used by callback function for each entry.
202  */
203 void mlx5_hlist_destroy(struct mlx5_hlist *h,
204                         mlx5_hlist_destroy_callback_fn cb, void *ctx);
205
206 #endif /* RTE_PMD_MLX5_UTILS_H_ */