net/mlx: fix build with clang 9
[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_defs.h"
17
18 /*
19  * Compilation workaround for PPC64 when AltiVec is fully enabled, e.g. std=c11.
20  * Otherwise there would be a type conflict between stdbool and altivec.
21  */
22 #if defined(__PPC64__) && !defined(__APPLE_ALTIVEC__)
23 #undef bool
24 /* redefine as in stdbool.h */
25 #define bool _Bool
26 #endif
27
28 /* Bit-field manipulation. */
29 #define BITFIELD_DECLARE(bf, type, size) \
30         type bf[(((size_t)(size) / (sizeof(type) * CHAR_BIT)) + \
31                  !!((size_t)(size) % (sizeof(type) * CHAR_BIT)))]
32 #define BITFIELD_DEFINE(bf, type, size) \
33         BITFIELD_DECLARE((bf), type, (size)) = { 0 }
34 #define BITFIELD_SET(bf, b) \
35         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
36          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] |= \
37                 ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
38 #define BITFIELD_RESET(bf, b) \
39         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
40          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] &= \
41                 ~((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
42 #define BITFIELD_ISSET(bf, b) \
43         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
44          !!(((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] & \
45              ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT))))))
46
47 /* Convert a bit number to the corresponding 64-bit mask */
48 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
49
50 /* Save and restore errno around argument evaluation. */
51 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
52
53 /*
54  * Helper macros to work around __VA_ARGS__ limitations in a C99 compliant
55  * manner.
56  */
57 #define PMD_DRV_LOG_STRIP(a, b) a
58 #define PMD_DRV_LOG_OPAREN (
59 #define PMD_DRV_LOG_CPAREN )
60 #define PMD_DRV_LOG_COMMA ,
61
62 /* Return the file name part of a path. */
63 static inline const char *
64 pmd_drv_log_basename(const char *s)
65 {
66         const char *n = s;
67
68         while (*n)
69                 if (*(n++) == '/')
70                         s = n;
71         return s;
72 }
73
74 extern int mlx5_logtype;
75
76 #define PMD_DRV_LOG___(level, ...) \
77         rte_log(RTE_LOG_ ## level, \
78                 mlx5_logtype, \
79                 RTE_FMT(MLX5_DRIVER_NAME ": " \
80                         RTE_FMT_HEAD(__VA_ARGS__,), \
81                 RTE_FMT_TAIL(__VA_ARGS__,)))
82
83 /*
84  * When debugging is enabled (NDEBUG not defined), file, line and function
85  * information replace the driver name (MLX5_DRIVER_NAME) in log messages.
86  */
87 #ifndef NDEBUG
88
89 #define PMD_DRV_LOG__(level, ...) \
90         PMD_DRV_LOG___(level, "%s:%u: %s(): " __VA_ARGS__)
91 #define PMD_DRV_LOG_(level, s, ...) \
92         PMD_DRV_LOG__(level, \
93                 s "\n" PMD_DRV_LOG_COMMA \
94                 pmd_drv_log_basename(__FILE__) PMD_DRV_LOG_COMMA \
95                 __LINE__ PMD_DRV_LOG_COMMA \
96                 __func__, \
97                 __VA_ARGS__)
98
99 #else /* NDEBUG */
100 #define PMD_DRV_LOG__(level, ...) \
101         PMD_DRV_LOG___(level, __VA_ARGS__)
102 #define PMD_DRV_LOG_(level, s, ...) \
103         PMD_DRV_LOG__(level, s "\n", __VA_ARGS__)
104
105 #endif /* NDEBUG */
106
107 /* Generic printf()-like logging macro with automatic line feed. */
108 #define DRV_LOG(level, ...) \
109         PMD_DRV_LOG_(level, \
110                 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
111                 PMD_DRV_LOG_CPAREN)
112
113 /* claim_zero() does not perform any check when debugging is disabled. */
114 #ifndef NDEBUG
115
116 #define DEBUG(...) DRV_LOG(DEBUG, __VA_ARGS__)
117 #define claim_zero(...) assert((__VA_ARGS__) == 0)
118 #define claim_nonzero(...) assert((__VA_ARGS__) != 0)
119
120 #else /* NDEBUG */
121
122 #define DEBUG(...) (void)0
123 #define claim_zero(...) (__VA_ARGS__)
124 #define claim_nonzero(...) (__VA_ARGS__)
125
126 #endif /* NDEBUG */
127
128 #define INFO(...) DRV_LOG(INFO, __VA_ARGS__)
129 #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__)
130 #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__)
131
132 /* Convenience macros for accessing mbuf fields. */
133 #define NEXT(m) ((m)->next)
134 #define DATA_LEN(m) ((m)->data_len)
135 #define PKT_LEN(m) ((m)->pkt_len)
136 #define DATA_OFF(m) ((m)->data_off)
137 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
138 #define NB_SEGS(m) ((m)->nb_segs)
139 #define PORT(m) ((m)->port)
140
141 /* Transpose flags. Useful to convert IBV to DPDK flags. */
142 #define TRANSPOSE(val, from, to) \
143         (((from) >= (to)) ? \
144          (((val) & (from)) / ((from) / (to))) : \
145          (((val) & (from)) * ((to) / (from))))
146
147 /* Allocate a buffer on the stack and fill it with a printf format string. */
148 #define MKSTR(name, ...) \
149         int mkstr_size_##name = snprintf(NULL, 0, "" __VA_ARGS__); \
150         char name[mkstr_size_##name + 1]; \
151         \
152         snprintf(name, sizeof(name), "" __VA_ARGS__)
153
154 /**
155  * Return logarithm of the nearest power of two above input value.
156  *
157  * @param v
158  *   Input value.
159  *
160  * @return
161  *   Logarithm of the nearest power of two above input value.
162  */
163 static inline unsigned int
164 log2above(unsigned int v)
165 {
166         unsigned int l;
167         unsigned int r;
168
169         for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
170                 r |= (v & 1);
171         return l + r;
172 }
173
174 /** Maximum size of string for naming the hlist table. */
175 #define MLX5_HLIST_NAMESIZE                     32
176
177 /**
178  * Structure of the entry in the hash list, user should define its own struct
179  * that contains this in order to store the data. The 'key' is 64-bits right
180  * now and its user's responsibility to guarantee there is no collision.
181  */
182 struct mlx5_hlist_entry {
183         LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
184         uint64_t key; /* user defined 'key', could be the hash signature. */
185 };
186
187 /** Structure for hash head. */
188 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
189
190 /** Type of function that is used to handle the data before freeing. */
191 typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
192
193 /** hash list table structure */
194 struct mlx5_hlist {
195         char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
196         /**< number of heads, need to be power of 2. */
197         uint32_t table_sz;
198         /**< mask to get the index of the list heads. */
199         uint32_t mask;
200         struct mlx5_hlist_head heads[]; /**< list head arrays. */
201 };
202
203 /**
204  * Create a hash list table, the user can specify the list heads array size
205  * of the table, now the size should be a power of 2 in order to get better
206  * distribution for the entries. Each entry is a part of the whole data element
207  * and the caller should be responsible for the data element's allocation and
208  * cleanup / free. Key of each entry will be calculated with CRC in order to
209  * generate a little fairer distribution.
210  *
211  * @param name
212  *   Name of the hash list(optional).
213  * @param size
214  *   Heads array size of the hash list.
215  *
216  * @return
217  *   Pointer of the hash list table created, NULL on failure.
218  */
219 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size);
220
221 /**
222  * Search an entry matching the key.
223  *
224  * @param h
225  *   Pointer to the hast list table.
226  * @param key
227  *   Key for the searching entry.
228  *
229  * @return
230  *   Pointer of the hlist entry if found, NULL otherwise.
231  */
232 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key);
233
234 /**
235  * Insert an entry to the hash list table, the entry is only part of whole data
236  * element and a 64B key is used for matching. User should construct the key or
237  * give a calculated hash signature and guarantee there is no collision.
238  *
239  * @param h
240  *   Pointer to the hast list table.
241  * @param entry
242  *   Entry to be inserted into the hash list table.
243  *
244  * @return
245  *   - zero for success.
246  *   - -EEXIST if the entry is already inserted.
247  */
248 int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
249
250 /**
251  * Remove an entry from the hash list table. User should guarantee the validity
252  * of the entry.
253  *
254  * @param h
255  *   Pointer to the hast list table. (not used)
256  * @param entry
257  *   Entry to be removed from the hash list table.
258  */
259 void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused,
260                        struct mlx5_hlist_entry *entry);
261
262 /**
263  * Destroy the hash list table, all the entries already inserted into the lists
264  * will be handled by the callback function provided by the user (including
265  * free if needed) before the table is freed.
266  *
267  * @param h
268  *   Pointer to the hast list table.
269  * @param cb
270  *   Callback function for each inserted entry when destroying the hash list.
271  * @param ctx
272  *   Common context parameter used by callback function for each entry.
273  */
274 void mlx5_hlist_destroy(struct mlx5_hlist *h,
275                         mlx5_hlist_destroy_callback_fn cb, void *ctx);
276
277 #endif /* RTE_PMD_MLX5_UTILS_H_ */