xref: /dpdk/drivers/net/cxgbe/cxgbe_compat.h (revision 7174d9969e15f899e293bcce46a9b612da4fd25b)
12aa5c722SRahul Lakkireddy /* SPDX-License-Identifier: BSD-3-Clause
22aa5c722SRahul Lakkireddy  * Copyright(c) 2014-2018 Chelsio Communications.
383189849SRahul Lakkireddy  * All rights reserved.
483189849SRahul Lakkireddy  */
583189849SRahul Lakkireddy 
683189849SRahul Lakkireddy #ifndef _CXGBE_COMPAT_H_
783189849SRahul Lakkireddy #define _CXGBE_COMPAT_H_
883189849SRahul Lakkireddy 
983189849SRahul Lakkireddy #include <string.h>
1083189849SRahul Lakkireddy #include <stdint.h>
1183189849SRahul Lakkireddy #include <stdio.h>
1283189849SRahul Lakkireddy #include <stdarg.h>
1300a6e154SDharmik Thakkar #include <stdbool.h>
1483189849SRahul Lakkireddy 
1583189849SRahul Lakkireddy #include <rte_common.h>
1683189849SRahul Lakkireddy #include <rte_memcpy.h>
1783189849SRahul Lakkireddy #include <rte_byteorder.h>
1883189849SRahul Lakkireddy #include <rte_cycles.h>
1983189849SRahul Lakkireddy #include <rte_spinlock.h>
2083189849SRahul Lakkireddy #include <rte_log.h>
21c167acb6SSantosh Shukla #include <rte_io.h>
229986d380SRahul Lakkireddy #include <rte_net.h>
2383189849SRahul Lakkireddy 
2418fab3a9SStephen Hemminger extern int cxgbe_logtype;
2570977725SRahul Lakkireddy extern int cxgbe_mbox_logtype;
2618fab3a9SStephen Hemminger 
2770977725SRahul Lakkireddy #define dev_printf(level, logtype, fmt, ...) \
2870977725SRahul Lakkireddy 	rte_log(RTE_LOG_ ## level, logtype, \
2918fab3a9SStephen Hemminger 		"rte_cxgbe_pmd: " fmt, ##__VA_ARGS__)
3083189849SRahul Lakkireddy 
3170977725SRahul Lakkireddy #define dev_err(x, fmt, ...) \
3270977725SRahul Lakkireddy 	dev_printf(ERR, cxgbe_logtype, fmt, ##__VA_ARGS__)
3370977725SRahul Lakkireddy #define dev_info(x, fmt, ...) \
3470977725SRahul Lakkireddy 	dev_printf(INFO, cxgbe_logtype, fmt, ##__VA_ARGS__)
3570977725SRahul Lakkireddy #define dev_warn(x, fmt, ...) \
3670977725SRahul Lakkireddy 	dev_printf(WARNING, cxgbe_logtype, fmt, ##__VA_ARGS__)
3770977725SRahul Lakkireddy #define dev_debug(x, fmt, ...) \
3870977725SRahul Lakkireddy 	dev_printf(DEBUG, cxgbe_logtype, fmt, ##__VA_ARGS__)
3983189849SRahul Lakkireddy 
40b5c3a5fdSRahul Lakkireddy #define CXGBE_DEBUG_MBOX(x, fmt, ...) \
4170977725SRahul Lakkireddy 	dev_printf(DEBUG, cxgbe_mbox_logtype, "MBOX:" fmt, ##__VA_ARGS__)
4283189849SRahul Lakkireddy 
4383189849SRahul Lakkireddy #define CXGBE_FUNC_TRACE() \
4470977725SRahul Lakkireddy 	dev_printf(DEBUG, cxgbe_logtype, "CXGBE trace: %s\n", __func__)
4583189849SRahul Lakkireddy 
46b5c3a5fdSRahul Lakkireddy #define pr_err(fmt, ...) dev_err(0, fmt, ##__VA_ARGS__)
47b5c3a5fdSRahul Lakkireddy #define pr_warn(fmt, ...) dev_warn(0, fmt, ##__VA_ARGS__)
48b5c3a5fdSRahul Lakkireddy #define pr_info(fmt, ...) dev_info(0, fmt, ##__VA_ARGS__)
4983189849SRahul Lakkireddy #define BUG() pr_err("BUG at %s:%d", __func__, __LINE__)
5083189849SRahul Lakkireddy 
5183189849SRahul Lakkireddy #define ASSERT(x) do {\
5283189849SRahul Lakkireddy 	if (!(x)) \
5383189849SRahul Lakkireddy 		rte_panic("CXGBE: x"); \
5483189849SRahul Lakkireddy } while (0)
5583189849SRahul Lakkireddy #define BUG_ON(x) ASSERT(!(x))
5683189849SRahul Lakkireddy 
5783189849SRahul Lakkireddy #ifndef WARN_ON
5883189849SRahul Lakkireddy #define WARN_ON(x) do { \
5983189849SRahul Lakkireddy 	int ret = !!(x); \
6083189849SRahul Lakkireddy 	if (unlikely(ret)) \
6183189849SRahul Lakkireddy 		pr_warn("WARN_ON: \"" #x "\" at %s:%d\n", __func__, __LINE__); \
6283189849SRahul Lakkireddy } while (0)
6383189849SRahul Lakkireddy #endif
6483189849SRahul Lakkireddy 
6583189849SRahul Lakkireddy #define __iomem
6683189849SRahul Lakkireddy 
6783189849SRahul Lakkireddy #ifndef BIT
6883189849SRahul Lakkireddy #define BIT(n) (1 << (n))
6983189849SRahul Lakkireddy #endif
7083189849SRahul Lakkireddy 
7183189849SRahul Lakkireddy #define L1_CACHE_SHIFT  6
7283189849SRahul Lakkireddy #define L1_CACHE_BYTES  BIT(L1_CACHE_SHIFT)
7383189849SRahul Lakkireddy 
7483189849SRahul Lakkireddy #define PAGE_SHIFT  12
751f8613f1SRahul Lakkireddy #define CXGBE_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
761f8613f1SRahul Lakkireddy #define PTR_ALIGN(p, a) ((typeof(p))CXGBE_ALIGN((unsigned long)(p), (a)))
7783189849SRahul Lakkireddy 
7835b2d13fSOlivier Matz #define ETHER_ADDR_LEN 6
7983189849SRahul Lakkireddy 
8083189849SRahul Lakkireddy #define rmb()     rte_rmb() /* dpdk rte provided rmb */
8183189849SRahul Lakkireddy #define wmb()     rte_wmb() /* dpdk rte provided wmb */
8283189849SRahul Lakkireddy 
8383189849SRahul Lakkireddy typedef uint8_t   u8;
8483189849SRahul Lakkireddy typedef int8_t    s8;
8583189849SRahul Lakkireddy typedef uint16_t  u16;
8683189849SRahul Lakkireddy typedef uint32_t  u32;
8783189849SRahul Lakkireddy typedef int32_t   s32;
8883189849SRahul Lakkireddy typedef uint64_t  u64;
8983189849SRahul Lakkireddy typedef uint64_t  dma_addr_t;
9083189849SRahul Lakkireddy 
9183189849SRahul Lakkireddy #ifndef __le16
9283189849SRahul Lakkireddy #define __le16	uint16_t
9383189849SRahul Lakkireddy #endif
9483189849SRahul Lakkireddy #ifndef __le32
9583189849SRahul Lakkireddy #define __le32	uint32_t
9683189849SRahul Lakkireddy #endif
9783189849SRahul Lakkireddy #ifndef __le64
9883189849SRahul Lakkireddy #define __le64	uint64_t
9983189849SRahul Lakkireddy #endif
10083189849SRahul Lakkireddy #ifndef __be16
10183189849SRahul Lakkireddy #define __be16	uint16_t
10283189849SRahul Lakkireddy #endif
10383189849SRahul Lakkireddy #ifndef __be32
10483189849SRahul Lakkireddy #define __be32	uint32_t
10583189849SRahul Lakkireddy #endif
10683189849SRahul Lakkireddy #ifndef __be64
10783189849SRahul Lakkireddy #define __be64	uint64_t
10883189849SRahul Lakkireddy #endif
10983189849SRahul Lakkireddy #ifndef __u8
11083189849SRahul Lakkireddy #define __u8	uint8_t
11183189849SRahul Lakkireddy #endif
11283189849SRahul Lakkireddy #ifndef __u16
11383189849SRahul Lakkireddy #define __u16	uint16_t
11483189849SRahul Lakkireddy #endif
11583189849SRahul Lakkireddy #ifndef __u32
11683189849SRahul Lakkireddy #define __u32	uint32_t
11783189849SRahul Lakkireddy #endif
11883189849SRahul Lakkireddy #ifndef __u64
11983189849SRahul Lakkireddy #define __u64	uint64_t
12083189849SRahul Lakkireddy #endif
12183189849SRahul Lakkireddy 
12283189849SRahul Lakkireddy #define FALSE	0
12383189849SRahul Lakkireddy #define TRUE	1
12483189849SRahul Lakkireddy 
1259986d380SRahul Lakkireddy #ifndef min
12683189849SRahul Lakkireddy #define min(a, b) RTE_MIN(a, b)
1279986d380SRahul Lakkireddy #endif
1289986d380SRahul Lakkireddy 
1299986d380SRahul Lakkireddy #ifndef max
13083189849SRahul Lakkireddy #define max(a, b) RTE_MAX(a, b)
1319986d380SRahul Lakkireddy #endif
13283189849SRahul Lakkireddy 
13383189849SRahul Lakkireddy /*
13483189849SRahul Lakkireddy  * round up val _p to a power of 2 size _s
13583189849SRahul Lakkireddy  */
1361f8613f1SRahul Lakkireddy #define cxgbe_roundup(_p, _s) (((unsigned long)(_p) + (_s - 1)) & ~(_s - 1))
13783189849SRahul Lakkireddy 
13883189849SRahul Lakkireddy #define ARRAY_SIZE(arr) RTE_DIM(arr)
13983189849SRahul Lakkireddy 
14083189849SRahul Lakkireddy #define cpu_to_be16(o) rte_cpu_to_be_16(o)
14183189849SRahul Lakkireddy #define cpu_to_be32(o) rte_cpu_to_be_32(o)
14283189849SRahul Lakkireddy #define cpu_to_be64(o) rte_cpu_to_be_64(o)
14383189849SRahul Lakkireddy #define cpu_to_le32(o) rte_cpu_to_le_32(o)
14483189849SRahul Lakkireddy #define be16_to_cpu(o) rte_be_to_cpu_16(o)
14583189849SRahul Lakkireddy #define be32_to_cpu(o) rte_be_to_cpu_32(o)
14683189849SRahul Lakkireddy #define be64_to_cpu(o) rte_be_to_cpu_64(o)
14783189849SRahul Lakkireddy #define le32_to_cpu(o) rte_le_to_cpu_32(o)
14883189849SRahul Lakkireddy 
1499986d380SRahul Lakkireddy #ifndef ntohs
1509986d380SRahul Lakkireddy #define ntohs(o) be16_to_cpu(o)
1519986d380SRahul Lakkireddy #endif
1529986d380SRahul Lakkireddy 
1539986d380SRahul Lakkireddy #ifndef ntohl
1549986d380SRahul Lakkireddy #define ntohl(o) be32_to_cpu(o)
1559986d380SRahul Lakkireddy #endif
1569986d380SRahul Lakkireddy 
1579986d380SRahul Lakkireddy #ifndef htons
1589986d380SRahul Lakkireddy #define htons(o) cpu_to_be16(o)
1599986d380SRahul Lakkireddy #endif
1609986d380SRahul Lakkireddy 
1619986d380SRahul Lakkireddy #ifndef htonl
1629986d380SRahul Lakkireddy #define htonl(o) cpu_to_be32(o)
1639986d380SRahul Lakkireddy #endif
1649986d380SRahul Lakkireddy 
1659986d380SRahul Lakkireddy #ifndef caddr_t
1669986d380SRahul Lakkireddy typedef char *caddr_t;
1679986d380SRahul Lakkireddy #endif
1689986d380SRahul Lakkireddy 
16983189849SRahul Lakkireddy #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
17083189849SRahul Lakkireddy #define DELAY(x) rte_delay_us(x)
17183189849SRahul Lakkireddy #define udelay(x) DELAY(x)
17283189849SRahul Lakkireddy #define msleep(x) DELAY(1000 * (x))
17383189849SRahul Lakkireddy #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
17483189849SRahul Lakkireddy 
hweight32(uint32_t word32)17583189849SRahul Lakkireddy static inline uint8_t hweight32(uint32_t word32)
17683189849SRahul Lakkireddy {
17783189849SRahul Lakkireddy 	uint32_t res = word32 - ((word32 >> 1) & 0x55555555);
17883189849SRahul Lakkireddy 
17983189849SRahul Lakkireddy 	res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
18083189849SRahul Lakkireddy 	res = (res + (res >> 4)) & 0x0F0F0F0F;
18183189849SRahul Lakkireddy 	res = res + (res >> 8);
18283189849SRahul Lakkireddy 	return (res + (res >> 16)) & 0x000000FF;
18383189849SRahul Lakkireddy 
18483189849SRahul Lakkireddy } /* weight32 */
18583189849SRahul Lakkireddy 
18683189849SRahul Lakkireddy /**
1871f8613f1SRahul Lakkireddy  * cxgbe_fls - find last (most-significant) bit set
18883189849SRahul Lakkireddy  * @x: the word to search
18983189849SRahul Lakkireddy  *
19083189849SRahul Lakkireddy  * This is defined the same way as ffs.
1911f8613f1SRahul Lakkireddy  * Note cxgbe_fls(0) = 0, cxgbe_fls(1) = 1, cxgbe_fls(0x80000000) = 32.
19283189849SRahul Lakkireddy  */
cxgbe_fls(int x)1931f8613f1SRahul Lakkireddy static inline int cxgbe_fls(int x)
19483189849SRahul Lakkireddy {
195*3d4e27fdSDavid Marchand 	return x ? sizeof(x) * 8 - rte_clz32(x) : 0;
19683189849SRahul Lakkireddy }
19783189849SRahul Lakkireddy 
ilog2(unsigned long n)19883189849SRahul Lakkireddy static inline unsigned long ilog2(unsigned long n)
19983189849SRahul Lakkireddy {
20083189849SRahul Lakkireddy 	unsigned int e = 0;
20183189849SRahul Lakkireddy 
20283189849SRahul Lakkireddy 	while (n) {
20383189849SRahul Lakkireddy 		if (n & ~((1 << 8) - 1)) {
20483189849SRahul Lakkireddy 			e += 8;
20583189849SRahul Lakkireddy 			n >>= 8;
20683189849SRahul Lakkireddy 			continue;
20783189849SRahul Lakkireddy 		}
20883189849SRahul Lakkireddy 
20983189849SRahul Lakkireddy 		if (n & ~((1 << 4) - 1)) {
21083189849SRahul Lakkireddy 			e += 4;
21183189849SRahul Lakkireddy 			n >>= 4;
21283189849SRahul Lakkireddy 		}
21383189849SRahul Lakkireddy 
21483189849SRahul Lakkireddy 		for (;;) {
21583189849SRahul Lakkireddy 			n >>= 1;
21683189849SRahul Lakkireddy 			if (n == 0)
21783189849SRahul Lakkireddy 				break;
21883189849SRahul Lakkireddy 			e++;
21983189849SRahul Lakkireddy 		}
22083189849SRahul Lakkireddy 	}
22183189849SRahul Lakkireddy 
22283189849SRahul Lakkireddy 	return e;
22383189849SRahul Lakkireddy }
22483189849SRahul Lakkireddy 
writel(unsigned int val,volatile void __iomem * addr)22583189849SRahul Lakkireddy static inline void writel(unsigned int val, volatile void __iomem *addr)
22683189849SRahul Lakkireddy {
227c167acb6SSantosh Shukla 	rte_write32(val, addr);
22883189849SRahul Lakkireddy }
22983189849SRahul Lakkireddy 
writeq(u64 val,volatile void __iomem * addr)23083189849SRahul Lakkireddy static inline void writeq(u64 val, volatile void __iomem *addr)
23183189849SRahul Lakkireddy {
23283189849SRahul Lakkireddy 	writel(val, addr);
23383189849SRahul Lakkireddy 	writel(val >> 32, (void *)((uintptr_t)addr + 4));
23483189849SRahul Lakkireddy }
23583189849SRahul Lakkireddy 
writel_relaxed(unsigned int val,volatile void __iomem * addr)236c167acb6SSantosh Shukla static inline void writel_relaxed(unsigned int val, volatile void __iomem *addr)
237c167acb6SSantosh Shukla {
238c167acb6SSantosh Shukla 	rte_write32_relaxed(val, addr);
239c167acb6SSantosh Shukla }
240c167acb6SSantosh Shukla 
2413a381a41SShagun Agrawal /*
2423a381a41SShagun Agrawal  * Multiplies an integer by a fraction, while avoiding unnecessary
2433a381a41SShagun Agrawal  * overflow or loss of precision.
2443a381a41SShagun Agrawal  */
mult_frac(unsigned int x,unsigned int numer,unsigned int denom)245fdd55210SRahul Lakkireddy static inline unsigned int mult_frac(unsigned int x, unsigned int numer,
246fdd55210SRahul Lakkireddy 				     unsigned int denom)
247fdd55210SRahul Lakkireddy {
248fdd55210SRahul Lakkireddy 	unsigned int quot = x / denom;
249fdd55210SRahul Lakkireddy 	unsigned int rem = x % denom;
2503a381a41SShagun Agrawal 
251fdd55210SRahul Lakkireddy 	return (quot * numer) + ((rem * numer) / denom);
252fdd55210SRahul Lakkireddy }
25383189849SRahul Lakkireddy #endif /* _CXGBE_COMPAT_H_ */
254