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