1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2018 Chelsio Communications.
6 #ifndef _CXGBE_COMPAT_H_
7 #define _CXGBE_COMPAT_H_
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>
22 #define dev_printf(level, fmt, args...) \
23 RTE_LOG(level, PMD, "rte_cxgbe_pmd: " fmt, ## args)
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)
29 #ifdef RTE_LIBRTE_CXGBE_DEBUG
30 #define dev_debug(x, args...) dev_printf(DEBUG, args)
32 #define dev_debug(x, args...) do { } while (0)
35 #ifdef RTE_LIBRTE_CXGBE_DEBUG_REG
36 #define CXGBE_DEBUG_REG(x, args...) dev_printf(DEBUG, "REG:" args)
38 #define CXGBE_DEBUG_REG(x, args...) do { } while (0)
41 #ifdef RTE_LIBRTE_CXGBE_DEBUG_MBOX
42 #define CXGBE_DEBUG_MBOX(x, args...) dev_printf(DEBUG, "MBOX:" args)
44 #define CXGBE_DEBUG_MBOX(x, args...) do { } while (0)
47 #ifdef RTE_LIBRTE_CXGBE_DEBUG_TX
48 #define CXGBE_DEBUG_TX(x, args...) dev_printf(DEBUG, "TX:" args)
50 #define CXGBE_DEBUG_TX(x, args...) do { } while (0)
53 #ifdef RTE_LIBRTE_CXGBE_DEBUG_RX
54 #define CXGBE_DEBUG_RX(x, args...) dev_printf(DEBUG, "RX:" args)
56 #define CXGBE_DEBUG_RX(x, args...) do { } while (0)
59 #ifdef RTE_LIBRTE_CXGBE_DEBUG
60 #define CXGBE_FUNC_TRACE() \
61 RTE_LOG(DEBUG, PMD, "CXGBE trace: %s\n", __func__)
63 #define CXGBE_FUNC_TRACE() do { } while (0)
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__)
71 #define ASSERT(x) do {\
73 rte_panic("CXGBE: x"); \
75 #define BUG_ON(x) ASSERT(!(x))
78 #define WARN_ON(x) do { \
81 pr_warn("WARN_ON: \"" #x "\" at %s:%d\n", __func__, __LINE__); \
88 #define BIT(n) (1 << (n))
91 #define L1_CACHE_SHIFT 6
92 #define L1_CACHE_BYTES BIT(L1_CACHE_SHIFT)
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)))
100 #define rmb() rte_rmb() /* dpdk rte provided rmb */
101 #define wmb() rte_wmb() /* dpdk rte provided wmb */
105 typedef uint16_t u16;
106 typedef uint32_t u32;
108 typedef uint64_t u64;
110 typedef uint64_t dma_addr_t;
113 #define __le16 uint16_t
116 #define __le32 uint32_t
119 #define __le64 uint64_t
122 #define __be16 uint16_t
125 #define __be32 uint32_t
128 #define __be64 uint64_t
134 #define __u16 uint16_t
137 #define __u32 uint32_t
140 #define __u64 uint64_t
148 #define min(a, b) RTE_MIN(a, b)
149 #define max(a, b) RTE_MAX(a, b)
152 * round up val _p to a power of 2 size _s
154 #define cxgbe_roundup(_p, _s) (((unsigned long)(_p) + (_s - 1)) & ~(_s - 1))
157 #define container_of(ptr, type, member) ({ \
158 typeof(((type *)0)->member)(*__mptr) = (ptr); \
159 (type *)((char *)__mptr - offsetof(type, member)); })
161 #define ARRAY_SIZE(arr) RTE_DIM(arr)
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)
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))
178 static inline uint8_t hweight32(uint32_t word32)
180 uint32_t res = word32 - ((word32 >> 1) & 0x55555555);
182 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
183 res = (res + (res >> 4)) & 0x0F0F0F0F;
184 res = res + (res >> 8);
185 return (res + (res >> 16)) & 0x000000FF;
190 * cxgbe_fls - find last (most-significant) bit set
191 * @x: the word to search
193 * This is defined the same way as ffs.
194 * Note cxgbe_fls(0) = 0, cxgbe_fls(1) = 1, cxgbe_fls(0x80000000) = 32.
196 static inline int cxgbe_fls(int x)
198 return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
201 static inline unsigned long ilog2(unsigned long n)
206 if (n & ~((1 << 8) - 1)) {
212 if (n & ~((1 << 4) - 1)) {
228 static inline void writel(unsigned int val, volatile void __iomem *addr)
230 rte_write32(val, addr);
233 static inline void writeq(u64 val, volatile void __iomem *addr)
236 writel(val >> 32, (void *)((uintptr_t)addr + 4));
239 static inline void writel_relaxed(unsigned int val, volatile void __iomem *addr)
241 rte_write32_relaxed(val, addr);
245 * Multiplies an integer by a fraction, while avoiding unnecessary
246 * overflow or loss of precision.
248 #define mult_frac(x, numer, denom)( \
250 typeof(x) quot = (x) / (denom); \
251 typeof(x) rem = (x) % (denom); \
252 (quot * (numer)) + ((rem * (numer)) / (denom)); \
256 #endif /* _CXGBE_COMPAT_H_ */