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