New upstream version 17.11.3
[deb_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 <stdint.h>
39 #include <stdio.h>
40 #include <limits.h>
41 #include <assert.h>
42 #include <errno.h>
43
44 #include "mlx5_defs.h"
45
46 /* Bit-field manipulation. */
47 #define BITFIELD_DECLARE(bf, type, size) \
48         type bf[(((size_t)(size) / (sizeof(type) * CHAR_BIT)) + \
49                  !!((size_t)(size) % (sizeof(type) * CHAR_BIT)))]
50 #define BITFIELD_DEFINE(bf, type, size) \
51         BITFIELD_DECLARE((bf), type, (size)) = { 0 }
52 #define BITFIELD_SET(bf, b) \
53         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
54          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] |= \
55                 ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
56 #define BITFIELD_RESET(bf, b) \
57         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
58          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] &= \
59                 ~((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
60 #define BITFIELD_ISSET(bf, b) \
61         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
62          !!(((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] & \
63              ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT))))))
64
65 /* Convert a bit number to the corresponding 64-bit mask */
66 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
67
68 /* Save and restore errno around argument evaluation. */
69 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
70
71 /*
72  * Helper macros to work around __VA_ARGS__ limitations in a C99 compliant
73  * manner.
74  */
75 #define PMD_DRV_LOG_STRIP(a, b) a
76 #define PMD_DRV_LOG_OPAREN (
77 #define PMD_DRV_LOG_CPAREN )
78 #define PMD_DRV_LOG_COMMA ,
79
80 /* Return the file name part of a path. */
81 static inline const char *
82 pmd_drv_log_basename(const char *s)
83 {
84         const char *n = s;
85
86         while (*n)
87                 if (*(n++) == '/')
88                         s = n;
89         return s;
90 }
91
92 extern int mlx5_logtype;
93
94 #define PMD_DRV_LOG___(level, ...) \
95         rte_log(RTE_LOG_ ## level, \
96                 mlx5_logtype, \
97                 RTE_FMT(MLX5_DRIVER_NAME ": " \
98                         RTE_FMT_HEAD(__VA_ARGS__,), \
99                 RTE_FMT_TAIL(__VA_ARGS__,)))
100
101 /*
102  * When debugging is enabled (NDEBUG not defined), file, line and function
103  * information replace the driver name (MLX5_DRIVER_NAME) in log messages.
104  */
105 #ifndef NDEBUG
106
107 #define PMD_DRV_LOG__(level, ...) \
108         PMD_DRV_LOG___(level, "%s:%u: %s(): " __VA_ARGS__)
109 #define PMD_DRV_LOG_(level, s, ...) \
110         PMD_DRV_LOG__(level, \
111                 s "\n" PMD_DRV_LOG_COMMA \
112                 pmd_drv_log_basename(__FILE__) PMD_DRV_LOG_COMMA \
113                 __LINE__ PMD_DRV_LOG_COMMA \
114                 __func__, \
115                 __VA_ARGS__)
116
117 #else /* NDEBUG */
118 #define PMD_DRV_LOG__(level, ...) \
119         PMD_DRV_LOG___(level, __VA_ARGS__)
120 #define PMD_DRV_LOG_(level, s, ...) \
121         PMD_DRV_LOG__(level, s "\n", __VA_ARGS__)
122
123 #endif /* NDEBUG */
124
125 /* Generic printf()-like logging macro with automatic line feed. */
126 #define DRV_LOG(level, ...) \
127         PMD_DRV_LOG_(level, \
128                 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
129                 PMD_DRV_LOG_CPAREN)
130
131 /* claim_zero() does not perform any check when debugging is disabled. */
132 #ifndef NDEBUG
133
134 #define DEBUG(...) DRV_LOG(DEBUG, __VA_ARGS__)
135 #define claim_zero(...) assert((__VA_ARGS__) == 0)
136 #define claim_nonzero(...) assert((__VA_ARGS__) != 0)
137
138 #else /* NDEBUG */
139
140 #define DEBUG(...) (void)0
141 #define claim_zero(...) (__VA_ARGS__)
142 #define claim_nonzero(...) (__VA_ARGS__)
143
144 #endif /* NDEBUG */
145
146 #define INFO(...) DRV_LOG(INFO, __VA_ARGS__)
147 #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__)
148 #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__)
149
150 /* Convenience macros for accessing mbuf fields. */
151 #define NEXT(m) ((m)->next)
152 #define DATA_LEN(m) ((m)->data_len)
153 #define PKT_LEN(m) ((m)->pkt_len)
154 #define DATA_OFF(m) ((m)->data_off)
155 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
156 #define NB_SEGS(m) ((m)->nb_segs)
157 #define PORT(m) ((m)->port)
158
159 /* Transpose flags. Useful to convert IBV to DPDK flags. */
160 #define TRANSPOSE(val, from, to) \
161         (((from) >= (to)) ? \
162          (((val) & (from)) / ((from) / (to))) : \
163          (((val) & (from)) * ((to) / (from))))
164
165 /* Allocate a buffer on the stack and fill it with a printf format string. */
166 #define MKSTR(name, ...) \
167         char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
168         \
169         snprintf(name, sizeof(name), __VA_ARGS__)
170
171 /**
172  * Return nearest power of two above input value.
173  *
174  * @param v
175  *   Input value.
176  *
177  * @return
178  *   Nearest power of two above input value.
179  */
180 static inline unsigned int
181 log2above(unsigned int v)
182 {
183         unsigned int l;
184         unsigned int r;
185
186         for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
187                 r |= (v & 1);
188         return l + r;
189 }
190
191 #endif /* RTE_PMD_MLX5_UTILS_H_ */