4 * Copyright(c) 2014-2017 Chelsio Communications.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Chelsio Communications nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _CXGBE_COMPAT_H_
35 #define _CXGBE_COMPAT_H_
42 #include <rte_common.h>
43 #include <rte_memcpy.h>
44 #include <rte_byteorder.h>
45 #include <rte_cycles.h>
46 #include <rte_spinlock.h>
50 #define dev_printf(level, fmt, args...) \
51 RTE_LOG(level, PMD, "rte_cxgbe_pmd: " fmt, ## args)
53 #define dev_err(x, args...) dev_printf(ERR, args)
54 #define dev_info(x, args...) dev_printf(INFO, args)
55 #define dev_warn(x, args...) dev_printf(WARNING, args)
57 #ifdef RTE_LIBRTE_CXGBE_DEBUG
58 #define dev_debug(x, args...) dev_printf(DEBUG, args)
60 #define dev_debug(x, args...) do { } while (0)
63 #ifdef RTE_LIBRTE_CXGBE_DEBUG_REG
64 #define CXGBE_DEBUG_REG(x, args...) dev_printf(DEBUG, "REG:" args)
66 #define CXGBE_DEBUG_REG(x, args...) do { } while (0)
69 #ifdef RTE_LIBRTE_CXGBE_DEBUG_MBOX
70 #define CXGBE_DEBUG_MBOX(x, args...) dev_printf(DEBUG, "MBOX:" args)
72 #define CXGBE_DEBUG_MBOX(x, args...) do { } while (0)
75 #ifdef RTE_LIBRTE_CXGBE_DEBUG_TX
76 #define CXGBE_DEBUG_TX(x, args...) dev_printf(DEBUG, "TX:" args)
78 #define CXGBE_DEBUG_TX(x, args...) do { } while (0)
81 #ifdef RTE_LIBRTE_CXGBE_DEBUG_RX
82 #define CXGBE_DEBUG_RX(x, args...) dev_printf(DEBUG, "RX:" args)
84 #define CXGBE_DEBUG_RX(x, args...) do { } while (0)
87 #ifdef RTE_LIBRTE_CXGBE_DEBUG
88 #define CXGBE_FUNC_TRACE() \
89 RTE_LOG(DEBUG, PMD, "CXGBE trace: %s\n", __func__)
91 #define CXGBE_FUNC_TRACE() do { } while (0)
94 #define pr_err(y, args...) dev_err(0, y, ##args)
95 #define pr_warn(y, args...) dev_warn(0, y, ##args)
96 #define pr_info(y, args...) dev_info(0, y, ##args)
97 #define BUG() pr_err("BUG at %s:%d", __func__, __LINE__)
99 #define ASSERT(x) do {\
101 rte_panic("CXGBE: x"); \
103 #define BUG_ON(x) ASSERT(!(x))
106 #define WARN_ON(x) do { \
109 pr_warn("WARN_ON: \"" #x "\" at %s:%d\n", __func__, __LINE__); \
116 #define BIT(n) (1 << (n))
119 #define L1_CACHE_SHIFT 6
120 #define L1_CACHE_BYTES BIT(L1_CACHE_SHIFT)
122 #define PAGE_SHIFT 12
123 #define CXGBE_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
124 #define PTR_ALIGN(p, a) ((typeof(p))CXGBE_ALIGN((unsigned long)(p), (a)))
128 #define rmb() rte_rmb() /* dpdk rte provided rmb */
129 #define wmb() rte_wmb() /* dpdk rte provided wmb */
133 typedef uint16_t u16;
134 typedef uint32_t u32;
136 typedef uint64_t u64;
138 typedef uint64_t dma_addr_t;
141 #define __le16 uint16_t
144 #define __le32 uint32_t
147 #define __le64 uint64_t
150 #define __be16 uint16_t
153 #define __be32 uint32_t
156 #define __be64 uint64_t
162 #define __u16 uint16_t
165 #define __u32 uint32_t
168 #define __u64 uint64_t
176 #define min(a, b) RTE_MIN(a, b)
177 #define max(a, b) RTE_MAX(a, b)
180 * round up val _p to a power of 2 size _s
182 #define cxgbe_roundup(_p, _s) (((unsigned long)(_p) + (_s - 1)) & ~(_s - 1))
185 #define container_of(ptr, type, member) ({ \
186 typeof(((type *)0)->member)(*__mptr) = (ptr); \
187 (type *)((char *)__mptr - offsetof(type, member)); })
189 #define ARRAY_SIZE(arr) RTE_DIM(arr)
191 #define cpu_to_be16(o) rte_cpu_to_be_16(o)
192 #define cpu_to_be32(o) rte_cpu_to_be_32(o)
193 #define cpu_to_be64(o) rte_cpu_to_be_64(o)
194 #define cpu_to_le32(o) rte_cpu_to_le_32(o)
195 #define be16_to_cpu(o) rte_be_to_cpu_16(o)
196 #define be32_to_cpu(o) rte_be_to_cpu_32(o)
197 #define be64_to_cpu(o) rte_be_to_cpu_64(o)
198 #define le32_to_cpu(o) rte_le_to_cpu_32(o)
200 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
201 #define DELAY(x) rte_delay_us(x)
202 #define udelay(x) DELAY(x)
203 #define msleep(x) DELAY(1000 * (x))
204 #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
206 static inline uint8_t hweight32(uint32_t word32)
208 uint32_t res = word32 - ((word32 >> 1) & 0x55555555);
210 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
211 res = (res + (res >> 4)) & 0x0F0F0F0F;
212 res = res + (res >> 8);
213 return (res + (res >> 16)) & 0x000000FF;
218 * cxgbe_fls - find last (most-significant) bit set
219 * @x: the word to search
221 * This is defined the same way as ffs.
222 * Note cxgbe_fls(0) = 0, cxgbe_fls(1) = 1, cxgbe_fls(0x80000000) = 32.
224 static inline int cxgbe_fls(int x)
226 return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
230 * cxgbe_ffs - find first bit set
231 * @x: the word to search
233 static inline int cxgbe_ffs(int x)
235 return x ? __builtin_ffs(x) : 0;
238 static inline unsigned long ilog2(unsigned long n)
243 if (n & ~((1 << 8) - 1)) {
249 if (n & ~((1 << 4) - 1)) {
265 static inline void writel(unsigned int val, volatile void __iomem *addr)
267 rte_write32(val, addr);
270 static inline void writeq(u64 val, volatile void __iomem *addr)
273 writel(val >> 32, (void *)((uintptr_t)addr + 4));
276 static inline void writel_relaxed(unsigned int val, volatile void __iomem *addr)
278 rte_write32_relaxed(val, addr);
281 #endif /* _CXGBE_COMPAT_H_ */