1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2018 Chelsio Communications.
6 #ifndef _CXGBE_COMPAT_H_
7 #define _CXGBE_COMPAT_H_
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>
24 extern int cxgbe_logtype;
25 extern int cxgbe_mbox_logtype;
27 #define dev_printf(level, logtype, fmt, ...) \
28 rte_log(RTE_LOG_ ## level, logtype, \
29 "rte_cxgbe_pmd: " fmt, ##__VA_ARGS__)
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__)
40 #define CXGBE_DEBUG_MBOX(x, fmt, ...) \
41 dev_printf(DEBUG, cxgbe_mbox_logtype, "MBOX:" fmt, ##__VA_ARGS__)
43 #define CXGBE_FUNC_TRACE() \
44 dev_printf(DEBUG, cxgbe_logtype, "CXGBE trace: %s\n", __func__)
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__)
51 #define ASSERT(x) do {\
53 rte_panic("CXGBE: x"); \
55 #define BUG_ON(x) ASSERT(!(x))
58 #define WARN_ON(x) do { \
61 pr_warn("WARN_ON: \"" #x "\" at %s:%d\n", __func__, __LINE__); \
68 #define BIT(n) (1 << (n))
71 #define L1_CACHE_SHIFT 6
72 #define L1_CACHE_BYTES BIT(L1_CACHE_SHIFT)
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)))
79 #define ETHER_ADDR_LEN 6
81 #define rmb() rte_rmb() /* dpdk rte provided rmb */
82 #define wmb() rte_wmb() /* dpdk rte provided wmb */
90 typedef uint64_t dma_addr_t;
93 #define __le16 uint16_t
96 #define __le32 uint32_t
99 #define __le64 uint64_t
102 #define __be16 uint16_t
105 #define __be32 uint32_t
108 #define __be64 uint64_t
114 #define __u16 uint16_t
117 #define __u32 uint32_t
120 #define __u64 uint64_t
127 #define min(a, b) RTE_MIN(a, b)
131 #define max(a, b) RTE_MAX(a, b)
135 * round up val _p to a power of 2 size _s
137 #define cxgbe_roundup(_p, _s) (((unsigned long)(_p) + (_s - 1)) & ~(_s - 1))
140 #define container_of(ptr, type, member) ({ \
141 typeof(((type *)0)->member)(*__mptr) = (ptr); \
142 (type *)((char *)__mptr - offsetof(type, member)); })
145 #define ARRAY_SIZE(arr) RTE_DIM(arr)
147 #define cpu_to_be16(o) rte_cpu_to_be_16(o)
148 #define cpu_to_be32(o) rte_cpu_to_be_32(o)
149 #define cpu_to_be64(o) rte_cpu_to_be_64(o)
150 #define cpu_to_le32(o) rte_cpu_to_le_32(o)
151 #define be16_to_cpu(o) rte_be_to_cpu_16(o)
152 #define be32_to_cpu(o) rte_be_to_cpu_32(o)
153 #define be64_to_cpu(o) rte_be_to_cpu_64(o)
154 #define le32_to_cpu(o) rte_le_to_cpu_32(o)
157 #define ntohs(o) be16_to_cpu(o)
161 #define ntohl(o) be32_to_cpu(o)
165 #define htons(o) cpu_to_be16(o)
169 #define htonl(o) cpu_to_be32(o)
173 typedef char *caddr_t;
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))
182 static inline uint8_t hweight32(uint32_t word32)
184 uint32_t res = word32 - ((word32 >> 1) & 0x55555555);
186 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
187 res = (res + (res >> 4)) & 0x0F0F0F0F;
188 res = res + (res >> 8);
189 return (res + (res >> 16)) & 0x000000FF;
194 * cxgbe_fls - find last (most-significant) bit set
195 * @x: the word to search
197 * This is defined the same way as ffs.
198 * Note cxgbe_fls(0) = 0, cxgbe_fls(1) = 1, cxgbe_fls(0x80000000) = 32.
200 static inline int cxgbe_fls(int x)
202 return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
205 static inline unsigned long ilog2(unsigned long n)
210 if (n & ~((1 << 8) - 1)) {
216 if (n & ~((1 << 4) - 1)) {
232 static inline void writel(unsigned int val, volatile void __iomem *addr)
234 rte_write32(val, addr);
237 static inline void writeq(u64 val, volatile void __iomem *addr)
240 writel(val >> 32, (void *)((uintptr_t)addr + 4));
243 static inline void writel_relaxed(unsigned int val, volatile void __iomem *addr)
245 rte_write32_relaxed(val, addr);
249 * Multiplies an integer by a fraction, while avoiding unnecessary
250 * overflow or loss of precision.
252 static inline unsigned int mult_frac(unsigned int x, unsigned int numer,
255 unsigned int quot = x / denom;
256 unsigned int rem = x % denom;
258 return (quot * numer) + ((rem * numer) / denom);
260 #endif /* _CXGBE_COMPAT_H_ */