1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
6 #ifndef RTE_PMD_MLX5_UTILS_H_
7 #define RTE_PMD_MLX5_UTILS_H_
16 #include "mlx5_defs.h"
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.
22 #if defined(__PPC64__) && !defined(__APPLE_ALTIVEC__)
24 /* redefine as in stdbool.h */
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))))))
47 /* Convert a bit number to the corresponding 64-bit mask */
48 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
50 /* Save and restore errno around argument evaluation. */
51 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
54 * Helper macros to work around __VA_ARGS__ limitations in a C99 compliant
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 ,
62 /* Return the file name part of a path. */
63 static inline const char *
64 pmd_drv_log_basename(const char *s)
74 extern int mlx5_logtype;
76 #define PMD_DRV_LOG___(level, ...) \
77 rte_log(RTE_LOG_ ## level, \
79 RTE_FMT(MLX5_DRIVER_NAME ": " \
80 RTE_FMT_HEAD(__VA_ARGS__,), \
81 RTE_FMT_TAIL(__VA_ARGS__,)))
84 * When debugging is enabled (NDEBUG not defined), file, line and function
85 * information replace the driver name (MLX5_DRIVER_NAME) in log messages.
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 \
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__)
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, \
113 /* claim_zero() does not perform any check when debugging is disabled. */
116 #define DEBUG(...) DRV_LOG(DEBUG, __VA_ARGS__)
117 #define claim_zero(...) assert((__VA_ARGS__) == 0)
118 #define claim_nonzero(...) assert((__VA_ARGS__) != 0)
122 #define DEBUG(...) (void)0
123 #define claim_zero(...) (__VA_ARGS__)
124 #define claim_nonzero(...) (__VA_ARGS__)
128 #define INFO(...) DRV_LOG(INFO, __VA_ARGS__)
129 #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__)
130 #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__)
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)
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))))
147 /* Allocate a buffer on the stack and fill it with a printf format string. */
148 #define MKSTR(name, ...) \
149 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
151 snprintf(name, sizeof(name), __VA_ARGS__)
154 * Return nearest power of two above input value.
160 * Nearest power of two above input value.
162 static inline unsigned int
163 log2above(unsigned int v)
168 for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
173 #endif /* RTE_PMD_MLX5_UTILS_H_ */