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