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