4 * Copyright(c) 2014-2015 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>
49 #define dev_printf(level, fmt, args...) \
50 RTE_LOG(level, PMD, "rte_cxgbe_pmd: " fmt, ## args)
52 #define dev_err(x, args...) dev_printf(ERR, args)
53 #define dev_info(x, args...) dev_printf(INFO, args)
54 #define dev_warn(x, args...) dev_printf(WARNING, args)
56 #ifdef RTE_LIBRTE_CXGBE_DEBUG
57 #define dev_debug(x, args...) dev_printf(DEBUG, args)
59 #define dev_debug(x, args...) do { } while (0)
62 #ifdef RTE_LIBRTE_CXGBE_DEBUG_REG
63 #define CXGBE_DEBUG_REG(x, args...) dev_printf(DEBUG, "REG:" args)
65 #define CXGBE_DEBUG_REG(x, args...) do { } while (0)
68 #ifdef RTE_LIBRTE_CXGBE_DEBUG_MBOX
69 #define CXGBE_DEBUG_MBOX(x, args...) dev_printf(DEBUG, "MBOX:" args)
71 #define CXGBE_DEBUG_MBOX(x, args...) do { } while (0)
74 #ifdef RTE_LIBRTE_CXGBE_DEBUG_TX
75 #define CXGBE_DEBUG_TX(x, args...) dev_printf(DEBUG, "TX:" args)
77 #define CXGBE_DEBUG_TX(x, args...) do { } while (0)
80 #ifdef RTE_LIBRTE_CXGBE_DEBUG_RX
81 #define CXGBE_DEBUG_RX(x, args...) dev_printf(DEBUG, "RX:" args)
83 #define CXGBE_DEBUG_RX(x, args...) do { } while (0)
86 #ifdef RTE_LIBRTE_CXGBE_DEBUG
87 #define CXGBE_FUNC_TRACE() \
88 RTE_LOG(DEBUG, PMD, "CXGBE trace: %s\n", __func__)
90 #define CXGBE_FUNC_TRACE() do { } while (0)
93 #define pr_err(y, args...) dev_err(0, y, ##args)
94 #define pr_warn(y, args...) dev_warn(0, y, ##args)
95 #define pr_info(y, args...) dev_info(0, y, ##args)
96 #define BUG() pr_err("BUG at %s:%d", __func__, __LINE__)
98 #define ASSERT(x) do {\
100 rte_panic("CXGBE: x"); \
102 #define BUG_ON(x) ASSERT(!(x))
105 #define WARN_ON(x) do { \
108 pr_warn("WARN_ON: \"" #x "\" at %s:%d\n", __func__, __LINE__); \
115 #define BIT(n) (1 << (n))
118 #define L1_CACHE_SHIFT 6
119 #define L1_CACHE_BYTES BIT(L1_CACHE_SHIFT)
121 #define PAGE_SHIFT 12
122 #define CXGBE_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
123 #define PTR_ALIGN(p, a) ((typeof(p))CXGBE_ALIGN((unsigned long)(p), (a)))
127 #define rmb() rte_rmb() /* dpdk rte provided rmb */
128 #define wmb() rte_wmb() /* dpdk rte provided wmb */
132 typedef uint16_t u16;
133 typedef uint32_t u32;
135 typedef uint64_t u64;
137 typedef uint64_t dma_addr_t;
140 #define __le16 uint16_t
143 #define __le32 uint32_t
146 #define __le64 uint64_t
149 #define __be16 uint16_t
152 #define __be32 uint32_t
155 #define __be64 uint64_t
161 #define __u16 uint16_t
164 #define __u32 uint32_t
167 #define __u64 uint64_t
175 #define min(a, b) RTE_MIN(a, b)
176 #define max(a, b) RTE_MAX(a, b)
179 * round up val _p to a power of 2 size _s
181 #define cxgbe_roundup(_p, _s) (((unsigned long)(_p) + (_s - 1)) & ~(_s - 1))
184 #define container_of(ptr, type, member) ({ \
185 typeof(((type *)0)->member)(*__mptr) = (ptr); \
186 (type *)((char *)__mptr - offsetof(type, member)); })
188 #define ARRAY_SIZE(arr) RTE_DIM(arr)
190 #define cpu_to_be16(o) rte_cpu_to_be_16(o)
191 #define cpu_to_be32(o) rte_cpu_to_be_32(o)
192 #define cpu_to_be64(o) rte_cpu_to_be_64(o)
193 #define cpu_to_le32(o) rte_cpu_to_le_32(o)
194 #define be16_to_cpu(o) rte_be_to_cpu_16(o)
195 #define be32_to_cpu(o) rte_be_to_cpu_32(o)
196 #define be64_to_cpu(o) rte_be_to_cpu_64(o)
197 #define le32_to_cpu(o) rte_le_to_cpu_32(o)
199 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
200 #define DELAY(x) rte_delay_us(x)
201 #define udelay(x) DELAY(x)
202 #define msleep(x) DELAY(1000 * (x))
203 #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
205 static inline uint8_t hweight32(uint32_t word32)
207 uint32_t res = word32 - ((word32 >> 1) & 0x55555555);
209 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
210 res = (res + (res >> 4)) & 0x0F0F0F0F;
211 res = res + (res >> 8);
212 return (res + (res >> 16)) & 0x000000FF;
217 * cxgbe_fls - find last (most-significant) bit set
218 * @x: the word to search
220 * This is defined the same way as ffs.
221 * Note cxgbe_fls(0) = 0, cxgbe_fls(1) = 1, cxgbe_fls(0x80000000) = 32.
223 static inline int cxgbe_fls(int x)
225 return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
228 static inline unsigned long ilog2(unsigned long n)
233 if (n & ~((1 << 8) - 1)) {
239 if (n & ~((1 << 4) - 1)) {
255 static inline void writel(unsigned int val, volatile void __iomem *addr)
257 *(volatile unsigned int *)addr = val;
260 static inline void writeq(u64 val, volatile void __iomem *addr)
263 writel(val >> 32, (void *)((uintptr_t)addr + 4));
266 #endif /* _CXGBE_COMPAT_H_ */