net/hinic/base: add various headers
[dpdk.git] / drivers / net / hinic / base / hinic_compat.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Huawei Technologies Co., Ltd
3  */
4
5 #ifndef _HINIC_COMPAT_H_
6 #define _HINIC_COMPAT_H_
7
8 #include <stdint.h>
9 #include <sys/time.h>
10 #include <rte_common.h>
11 #include <rte_byteorder.h>
12 #include <rte_memzone.h>
13 #include <rte_memcpy.h>
14 #include <rte_malloc.h>
15 #include <rte_atomic.h>
16 #include <rte_spinlock.h>
17 #include <rte_cycles.h>
18 #include <rte_log.h>
19 #include <rte_config.h>
20
21 typedef uint8_t   u8;
22 typedef int8_t    s8;
23 typedef uint16_t  u16;
24 typedef uint32_t  u32;
25 typedef int32_t   s32;
26 typedef uint64_t  u64;
27
28 #ifndef dma_addr_t
29 typedef uint64_t  dma_addr_t;
30 #endif
31
32 #ifndef gfp_t
33 #define gfp_t unsigned
34 #endif
35
36 #ifndef bool
37 #define bool int
38 #endif
39
40 #ifndef FALSE
41 #define FALSE   (0)
42 #endif
43
44 #ifndef TRUE
45 #define TRUE    (1)
46 #endif
47
48 #ifndef false
49 #define false   (0)
50 #endif
51
52 #ifndef true
53 #define true    (1)
54 #endif
55
56 #ifndef NULL
57 #define NULL ((void *)0)
58 #endif
59
60 #define HINIC_ERROR     (-1)
61 #define HINIC_OK        (0)
62
63 #ifndef BIT
64 #define BIT(n) (1 << (n))
65 #endif
66
67 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
68 #define lower_32_bits(n) ((u32)(n))
69
70 /* Returns X / Y, rounding up.  X must be nonnegative to round correctly. */
71 #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
72
73 /* Returns X rounded up to the nearest multiple of Y. */
74 #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y))
75
76 #undef  ALIGN
77 #define ALIGN(x, a)  RTE_ALIGN(x, a)
78
79 #define PTR_ALIGN(p, a)         ((typeof(p))ALIGN((unsigned long)(p), (a)))
80
81 /* Reported driver name. */
82 #define HINIC_DRIVER_NAME "net_hinic"
83
84 extern int hinic_logtype;
85
86 #define PMD_DRV_LOG(level, fmt, args...) \
87         rte_log(RTE_LOG_ ## level, hinic_logtype, \
88                 HINIC_DRIVER_NAME": " fmt "\n", ##args)
89
90 /* common definition */
91 #ifndef ETH_ALEN
92 #define ETH_ALEN                6
93 #endif
94 #define ETH_HLEN                14
95 #define ETH_CRC_LEN             4
96 #define VLAN_PRIO_SHIFT         13
97 #define VLAN_N_VID              4096
98
99 /* bit order interface */
100 #define cpu_to_be16(o) rte_cpu_to_be_16(o)
101 #define cpu_to_be32(o) rte_cpu_to_be_32(o)
102 #define cpu_to_be64(o) rte_cpu_to_be_64(o)
103 #define cpu_to_le32(o) rte_cpu_to_le_32(o)
104 #define be16_to_cpu(o) rte_be_to_cpu_16(o)
105 #define be32_to_cpu(o) rte_be_to_cpu_32(o)
106 #define be64_to_cpu(o) rte_be_to_cpu_64(o)
107 #define le32_to_cpu(o) rte_le_to_cpu_32(o)
108
109 /* virt memory and dma phy memory */
110 #define __iomem
111 #define GFP_KERNEL              RTE_MEMZONE_IOVA_CONTIG
112 #define HINIC_PAGE_SHIFT        12
113 #define HINIC_PAGE_SIZE         RTE_PGSIZE_4K
114 #define HINIC_MEM_ALLOC_ALIGNE_MIN      8
115
116 #define HINIC_PAGE_SIZE_DPDK    6
117
118 static inline int hinic_test_bit(int nr, volatile unsigned long *addr)
119 {
120         int res;
121
122         rte_mb();
123         res = ((*addr) & (1UL << nr)) != 0;
124         rte_mb();
125         return res;
126 }
127
128 static inline void hinic_set_bit(unsigned int nr, volatile unsigned long *addr)
129 {
130         __sync_fetch_and_or(addr, (1UL << nr));
131 }
132
133 static inline void hinic_clear_bit(int nr, volatile unsigned long *addr)
134 {
135         __sync_fetch_and_and(addr, ~(1UL << nr));
136 }
137
138 static inline int hinic_test_and_clear_bit(int nr, volatile unsigned long *addr)
139 {
140         unsigned long mask = (1UL << nr);
141
142         return __sync_fetch_and_and(addr, ~mask) & mask;
143 }
144
145 static inline int hinic_test_and_set_bit(int nr, volatile unsigned long *addr)
146 {
147         unsigned long mask = (1UL << nr);
148
149         return __sync_fetch_and_or(addr, mask) & mask;
150 }
151
152 void *dma_zalloc_coherent(void *dev, size_t size, dma_addr_t *dma_handle,
153                           gfp_t flag);
154 void *dma_zalloc_coherent_aligned(void *dev, size_t size,
155                                 dma_addr_t *dma_handle, gfp_t flag);
156 void *dma_zalloc_coherent_aligned256k(void *dev, size_t size,
157                                 dma_addr_t *dma_handle, gfp_t flag);
158 void dma_free_coherent(void *dev, size_t size, void *virt, dma_addr_t phys);
159
160 /* dma pool alloc and free */
161 #define pci_pool dma_pool
162 #define pci_pool_alloc(pool, flags, handle) dma_pool_alloc(pool, flags, handle)
163 #define pci_pool_free(pool, vaddr, addr) dma_pool_free(pool, vaddr, addr)
164
165 struct dma_pool *dma_pool_create(const char *name, void *dev, size_t size,
166                                 size_t align, size_t boundary);
167 void dma_pool_destroy(struct dma_pool *pool);
168 void *dma_pool_alloc(struct pci_pool *pool, int flags, dma_addr_t *dma_addr);
169 void dma_pool_free(struct pci_pool *pool, void *vaddr, dma_addr_t dma);
170
171 #define kzalloc(size, flag) rte_zmalloc(NULL, size, HINIC_MEM_ALLOC_ALIGNE_MIN)
172 #define kzalloc_aligned(size, flag) rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE)
173 #define kfree(ptr)            rte_free(ptr)
174
175 /* mmio interface */
176 static inline void writel(u32 value, volatile void  *addr)
177 {
178         *(volatile u32 *)addr = value;
179 }
180
181 static inline u32 readl(const volatile void *addr)
182 {
183         return *(const volatile u32 *)addr;
184 }
185
186 #define __raw_writel(value, reg) writel((value), (reg))
187 #define __raw_readl(reg) readl((reg))
188
189 /* Spinlock related interface */
190 #define hinic_spinlock_t rte_spinlock_t
191
192 #define spinlock_t rte_spinlock_t
193 #define spin_lock_init(spinlock_prt)    rte_spinlock_init(spinlock_prt)
194 #define spin_lock_deinit(lock)
195 #define spin_lock(spinlock_prt)         rte_spinlock_lock(spinlock_prt)
196 #define spin_unlock(spinlock_prt)       rte_spinlock_unlock(spinlock_prt)
197
198 static inline unsigned long get_timeofday_ms(void)
199 {
200         struct timeval tv;
201
202         (void)gettimeofday(&tv, NULL);
203
204         return (unsigned long)tv.tv_sec * 1000 + tv.tv_usec / 1000;
205 }
206
207 #define jiffies get_timeofday_ms()
208 #define msecs_to_jiffies(ms)    (ms)
209 #define time_before(now, end)   ((now) < (end))
210
211 /* misc kernel utils */
212 static inline u16 ilog2(u32 n)
213 {
214         u16 res = 0;
215
216         while (n > 1) {
217                 n >>= 1;
218                 res++;
219         }
220
221         return res;
222 }
223
224 /**
225  * hinic_cpu_to_be32 - convert data to big endian 32 bit format
226  * @data: the data to convert
227  * @len: length of data to convert, must be Multiple of 4B
228  **/
229 static inline void hinic_cpu_to_be32(void *data, u32 len)
230 {
231         u32 i;
232         u32 *mem = (u32 *)data;
233
234         for (i = 0; i < (len >> 2); i++) {
235                 *mem = cpu_to_be32(*mem);
236                 mem++;
237         }
238 }
239
240 /**
241  * hinic_be32_to_cpu - convert data from big endian 32 bit format
242  * @data: the data to convert
243  * @len: length of data to convert, must be Multiple of 4B
244  **/
245 static inline void hinic_be32_to_cpu(void *data, u32 len)
246 {
247         u32 i;
248         u32 *mem = (u32 *)data;
249
250         for (i = 0; i < (len >> 2); i++) {
251                 *mem = be32_to_cpu(*mem);
252                 mem++;
253         }
254 }
255
256 #endif /* _HINIC_COMPAT_H_ */