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