net/cxgbe: define symbols only when not available
[dpdk.git] / drivers / net / cxgbe / cxgbe_compat.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Chelsio Communications.
3  * All rights reserved.
4  */
5
6 #ifndef _CXGBE_COMPAT_H_
7 #define _CXGBE_COMPAT_H_
8
9 #include <string.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13
14 #include <rte_common.h>
15 #include <rte_memcpy.h>
16 #include <rte_byteorder.h>
17 #include <rte_cycles.h>
18 #include <rte_spinlock.h>
19 #include <rte_log.h>
20 #include <rte_io.h>
21 #include <rte_net.h>
22
23 #define dev_printf(level, fmt, ...) \
24         RTE_LOG(level, PMD, "rte_cxgbe_pmd: " fmt, ##__VA_ARGS__)
25
26 #define dev_err(x, fmt, ...) dev_printf(ERR, fmt, ##__VA_ARGS__)
27 #define dev_info(x, fmt, ...) dev_printf(INFO, fmt, ##__VA_ARGS__)
28 #define dev_warn(x, fmt, ...) dev_printf(WARNING, fmt, ##__VA_ARGS__)
29
30 #ifdef RTE_LIBRTE_CXGBE_DEBUG
31 #define dev_debug(x, fmt, ...) dev_printf(INFO, fmt, ##__VA_ARGS__)
32 #else
33 #define dev_debug(x, fmt, ...) do { } while (0)
34 #endif
35
36 #ifdef RTE_LIBRTE_CXGBE_DEBUG_REG
37 #define CXGBE_DEBUG_REG(x, fmt, ...) \
38         dev_printf(INFO, "REG:" fmt, ##__VA_ARGS__)
39 #else
40 #define CXGBE_DEBUG_REG(x, fmt, ...) do { } while (0)
41 #endif
42
43 #ifdef RTE_LIBRTE_CXGBE_DEBUG_MBOX
44 #define CXGBE_DEBUG_MBOX(x, fmt, ...) \
45         dev_printf(INFO, "MBOX:" fmt, ##__VA_ARGS__)
46 #else
47 #define CXGBE_DEBUG_MBOX(x, fmt, ...) do { } while (0)
48 #endif
49
50 #ifdef RTE_LIBRTE_CXGBE_DEBUG_TX
51 #define CXGBE_DEBUG_TX(x, fmt, ...) \
52         dev_printf(INFO, "TX:" fmt, ##__VA_ARGS__)
53 #else
54 #define CXGBE_DEBUG_TX(x, fmt, ...) do { } while (0)
55 #endif
56
57 #ifdef RTE_LIBRTE_CXGBE_DEBUG_RX
58 #define CXGBE_DEBUG_RX(x, fmt, ...) \
59         dev_printf(INFO, "RX:" fmt, ##__VA_ARGS__)
60 #else
61 #define CXGBE_DEBUG_RX(x, fmt, ...) do { } while (0)
62 #endif
63
64 #ifdef RTE_LIBRTE_CXGBE_DEBUG
65 #define CXGBE_FUNC_TRACE() \
66         RTE_LOG(DEBUG, PMD, "CXGBE trace: %s\n", __func__)
67 #else
68 #define CXGBE_FUNC_TRACE() do { } while (0)
69 #endif
70
71 #define pr_err(fmt, ...) dev_err(0, fmt, ##__VA_ARGS__)
72 #define pr_warn(fmt, ...) dev_warn(0, fmt, ##__VA_ARGS__)
73 #define pr_info(fmt, ...) dev_info(0, fmt, ##__VA_ARGS__)
74 #define BUG() pr_err("BUG at %s:%d", __func__, __LINE__)
75
76 #define ASSERT(x) do {\
77         if (!(x)) \
78                 rte_panic("CXGBE: x"); \
79 } while (0)
80 #define BUG_ON(x) ASSERT(!(x))
81
82 #ifndef WARN_ON
83 #define WARN_ON(x) do { \
84         int ret = !!(x); \
85         if (unlikely(ret)) \
86                 pr_warn("WARN_ON: \"" #x "\" at %s:%d\n", __func__, __LINE__); \
87 } while (0)
88 #endif
89
90 #define __iomem
91
92 #ifndef BIT
93 #define BIT(n) (1 << (n))
94 #endif
95
96 #define L1_CACHE_SHIFT  6
97 #define L1_CACHE_BYTES  BIT(L1_CACHE_SHIFT)
98
99 #define PAGE_SHIFT  12
100 #define CXGBE_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
101 #define PTR_ALIGN(p, a) ((typeof(p))CXGBE_ALIGN((unsigned long)(p), (a)))
102
103 #define VLAN_HLEN 4
104
105 #define rmb()     rte_rmb() /* dpdk rte provided rmb */
106 #define wmb()     rte_wmb() /* dpdk rte provided wmb */
107
108 typedef uint8_t   u8;
109 typedef int8_t    s8;
110 typedef uint16_t  u16;
111 typedef uint32_t  u32;
112 typedef int32_t   s32;
113 typedef uint64_t  u64;
114 typedef int       bool;
115 typedef uint64_t  dma_addr_t;
116
117 #ifndef __le16
118 #define __le16  uint16_t
119 #endif
120 #ifndef __le32
121 #define __le32  uint32_t
122 #endif
123 #ifndef __le64
124 #define __le64  uint64_t
125 #endif
126 #ifndef __be16
127 #define __be16  uint16_t
128 #endif
129 #ifndef __be32
130 #define __be32  uint32_t
131 #endif
132 #ifndef __be64
133 #define __be64  uint64_t
134 #endif
135 #ifndef __u8
136 #define __u8    uint8_t
137 #endif
138 #ifndef __u16
139 #define __u16   uint16_t
140 #endif
141 #ifndef __u32
142 #define __u32   uint32_t
143 #endif
144 #ifndef __u64
145 #define __u64   uint64_t
146 #endif
147
148 #define FALSE   0
149 #define TRUE    1
150 #define false   0
151 #define true    1
152
153 #ifndef min
154 #define min(a, b) RTE_MIN(a, b)
155 #endif
156
157 #ifndef max
158 #define max(a, b) RTE_MAX(a, b)
159 #endif
160
161 /*
162  * round up val _p to a power of 2 size _s
163  */
164 #define cxgbe_roundup(_p, _s) (((unsigned long)(_p) + (_s - 1)) & ~(_s - 1))
165
166 #ifndef container_of
167 #define container_of(ptr, type, member) ({ \
168                 typeof(((type *)0)->member)(*__mptr) = (ptr); \
169                 (type *)((char *)__mptr - offsetof(type, member)); })
170 #endif
171
172 #define ARRAY_SIZE(arr) RTE_DIM(arr)
173
174 #define cpu_to_be16(o) rte_cpu_to_be_16(o)
175 #define cpu_to_be32(o) rte_cpu_to_be_32(o)
176 #define cpu_to_be64(o) rte_cpu_to_be_64(o)
177 #define cpu_to_le32(o) rte_cpu_to_le_32(o)
178 #define be16_to_cpu(o) rte_be_to_cpu_16(o)
179 #define be32_to_cpu(o) rte_be_to_cpu_32(o)
180 #define be64_to_cpu(o) rte_be_to_cpu_64(o)
181 #define le32_to_cpu(o) rte_le_to_cpu_32(o)
182
183 #ifndef ntohs
184 #define ntohs(o) be16_to_cpu(o)
185 #endif
186
187 #ifndef ntohl
188 #define ntohl(o) be32_to_cpu(o)
189 #endif
190
191 #ifndef htons
192 #define htons(o) cpu_to_be16(o)
193 #endif
194
195 #ifndef htonl
196 #define htonl(o) cpu_to_be32(o)
197 #endif
198
199 #ifndef caddr_t
200 typedef char *caddr_t;
201 #endif
202
203 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
204 #define DELAY(x) rte_delay_us(x)
205 #define udelay(x) DELAY(x)
206 #define msleep(x) DELAY(1000 * (x))
207 #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
208
209 static inline uint8_t hweight32(uint32_t word32)
210 {
211         uint32_t res = word32 - ((word32 >> 1) & 0x55555555);
212
213         res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
214         res = (res + (res >> 4)) & 0x0F0F0F0F;
215         res = res + (res >> 8);
216         return (res + (res >> 16)) & 0x000000FF;
217
218 } /* weight32 */
219
220 /**
221  * cxgbe_fls - find last (most-significant) bit set
222  * @x: the word to search
223  *
224  * This is defined the same way as ffs.
225  * Note cxgbe_fls(0) = 0, cxgbe_fls(1) = 1, cxgbe_fls(0x80000000) = 32.
226  */
227 static inline int cxgbe_fls(int x)
228 {
229         return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
230 }
231
232 static inline unsigned long ilog2(unsigned long n)
233 {
234         unsigned int e = 0;
235
236         while (n) {
237                 if (n & ~((1 << 8) - 1)) {
238                         e += 8;
239                         n >>= 8;
240                         continue;
241                 }
242
243                 if (n & ~((1 << 4) - 1)) {
244                         e += 4;
245                         n >>= 4;
246                 }
247
248                 for (;;) {
249                         n >>= 1;
250                         if (n == 0)
251                                 break;
252                         e++;
253                 }
254         }
255
256         return e;
257 }
258
259 static inline void writel(unsigned int val, volatile void __iomem *addr)
260 {
261         rte_write32(val, addr);
262 }
263
264 static inline void writeq(u64 val, volatile void __iomem *addr)
265 {
266         writel(val, addr);
267         writel(val >> 32, (void *)((uintptr_t)addr + 4));
268 }
269
270 static inline void writel_relaxed(unsigned int val, volatile void __iomem *addr)
271 {
272         rte_write32_relaxed(val, addr);
273 }
274
275 /*
276  * Multiplies an integer by a fraction, while avoiding unnecessary
277  * overflow or loss of precision.
278  */
279 #define mult_frac(x, numer, denom)(                     \
280 {                                                       \
281         typeof(x) quot = (x) / (denom);                 \
282         typeof(x) rem  = (x) % (denom);                 \
283         (quot * (numer)) + ((rem * (numer)) / (denom)); \
284 }                                                       \
285 )
286
287 #endif /* _CXGBE_COMPAT_H_ */