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