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