218ae831fceea52396ab2e10bba5709a6752d690
[dpdk.git] / drivers / net / mlx5 / mlx5_utils.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef RTE_PMD_MLX5_UTILS_H_
35 #define RTE_PMD_MLX5_UTILS_H_
36
37 #include <stddef.h>
38 #include <stdio.h>
39 #include <limits.h>
40 #include <assert.h>
41 #include <errno.h>
42
43 #include "mlx5_defs.h"
44
45 /* Bit-field manipulation. */
46 #define BITFIELD_DECLARE(bf, type, size) \
47         type bf[(((size_t)(size) / (sizeof(type) * CHAR_BIT)) + \
48                  !!((size_t)(size) % (sizeof(type) * CHAR_BIT)))]
49 #define BITFIELD_DEFINE(bf, type, size) \
50         BITFIELD_DECLARE((bf), type, (size)) = { 0 }
51 #define BITFIELD_SET(bf, b) \
52         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
53          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] |= \
54                 ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
55 #define BITFIELD_RESET(bf, b) \
56         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
57          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] &= \
58                 ~((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
59 #define BITFIELD_ISSET(bf, b) \
60         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
61          !!(((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] & \
62              ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT))))))
63
64 /* Save and restore errno around argument evaluation. */
65 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
66
67 /*
68  * Helper macros to work around __VA_ARGS__ limitations in a C99 compliant
69  * manner.
70  */
71 #define PMD_DRV_LOG_STRIP(a, b) a
72 #define PMD_DRV_LOG_OPAREN (
73 #define PMD_DRV_LOG_CPAREN )
74 #define PMD_DRV_LOG_COMMA ,
75
76 /* Return the file name part of a path. */
77 static inline const char *
78 pmd_drv_log_basename(const char *s)
79 {
80         const char *n = s;
81
82         while (*n)
83                 if (*(n++) == '/')
84                         s = n;
85         return s;
86 }
87
88 /*
89  * When debugging is enabled (NDEBUG not defined), file, line and function
90  * information replace the driver name (MLX5_DRIVER_NAME) in log messages.
91  */
92 #ifndef NDEBUG
93
94 #define PMD_DRV_LOG___(level, ...) \
95         ERRNO_SAFE(RTE_LOG(level, PMD, __VA_ARGS__))
96 #define PMD_DRV_LOG__(level, ...) \
97         PMD_DRV_LOG___(level, "%s:%u: %s(): " __VA_ARGS__)
98 #define PMD_DRV_LOG_(level, s, ...) \
99         PMD_DRV_LOG__(level, \
100                 s "\n" PMD_DRV_LOG_COMMA \
101                 pmd_drv_log_basename(__FILE__) PMD_DRV_LOG_COMMA \
102                 __LINE__ PMD_DRV_LOG_COMMA \
103                 __func__, \
104                 __VA_ARGS__)
105
106 #else /* NDEBUG */
107
108 #define PMD_DRV_LOG___(level, ...) \
109         ERRNO_SAFE(RTE_LOG(level, PMD, MLX5_DRIVER_NAME ": " __VA_ARGS__))
110 #define PMD_DRV_LOG__(level, ...) \
111         PMD_DRV_LOG___(level, __VA_ARGS__)
112 #define PMD_DRV_LOG_(level, s, ...) \
113         PMD_DRV_LOG__(level, s "\n", __VA_ARGS__)
114
115 #endif /* NDEBUG */
116
117 /* Generic printf()-like logging macro with automatic line feed. */
118 #define PMD_DRV_LOG(level, ...) \
119         PMD_DRV_LOG_(level, \
120                 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
121                 PMD_DRV_LOG_CPAREN)
122
123 /*
124  * Like assert(), DEBUG() becomes a no-op and claim_zero() does not perform
125  * any check when debugging is disabled.
126  */
127 #ifndef NDEBUG
128
129 #define DEBUG(...) PMD_DRV_LOG(DEBUG, __VA_ARGS__)
130 #define claim_zero(...) assert((__VA_ARGS__) == 0)
131 #define claim_nonzero(...) assert((__VA_ARGS__) != 0)
132
133 #else /* NDEBUG */
134
135 #define DEBUG(...) (void)0
136 #define claim_zero(...) (__VA_ARGS__)
137 #define claim_nonzero(...) (__VA_ARGS__)
138
139 #endif /* NDEBUG */
140
141 #define INFO(...) PMD_DRV_LOG(INFO, __VA_ARGS__)
142 #define WARN(...) PMD_DRV_LOG(WARNING, __VA_ARGS__)
143 #define ERROR(...) PMD_DRV_LOG(ERR, __VA_ARGS__)
144
145 /* Convenience macros for accessing mbuf fields. */
146 #define NEXT(m) ((m)->next)
147 #define DATA_LEN(m) ((m)->data_len)
148 #define PKT_LEN(m) ((m)->pkt_len)
149 #define DATA_OFF(m) ((m)->data_off)
150 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
151 #define NB_SEGS(m) ((m)->nb_segs)
152 #define PORT(m) ((m)->port)
153
154 /* Transpose flags. Useful to convert IBV to DPDK flags. */
155 #define TRANSPOSE(val, from, to) \
156         (((from) >= (to)) ? \
157          (((val) & (from)) / ((from) / (to))) : \
158          (((val) & (from)) * ((to) / (from))))
159
160 /* Allocate a buffer on the stack and fill it with a printf format string. */
161 #define MKSTR(name, ...) \
162         char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
163         \
164         snprintf(name, sizeof(name), __VA_ARGS__)
165
166 /**
167  * Return nearest power of two above input value.
168  *
169  * @param v
170  *   Input value.
171  *
172  * @return
173  *   Nearest power of two above input value.
174  */
175 static inline unsigned int
176 log2above(unsigned int v)
177 {
178         unsigned int l;
179         unsigned int r;
180
181         for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
182                 r |= (v & 1);
183         return l + r;
184 }
185
186 #endif /* RTE_PMD_MLX5_UTILS_H_ */