Branch data Line data Source code
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_bitops.h>
14 : : #include <rte_byteorder.h>
15 : : #include <rte_memzone.h>
16 : : #include <rte_memcpy.h>
17 : : #include <rte_malloc.h>
18 : : #include <rte_atomic.h>
19 : : #include <rte_spinlock.h>
20 : : #include <rte_cycles.h>
21 : : #include <rte_log.h>
22 : : #include <rte_thread.h>
23 : :
24 : : typedef uint8_t u8;
25 : : typedef int8_t s8;
26 : : typedef uint16_t u16;
27 : : typedef uint32_t u32;
28 : : typedef int32_t s32;
29 : : typedef uint64_t u64;
30 : :
31 : : #ifndef dma_addr_t
32 : : typedef uint64_t dma_addr_t;
33 : : #endif
34 : :
35 : : #ifndef gfp_t
36 : : #define gfp_t unsigned
37 : : #endif
38 : :
39 : : #ifndef bool
40 : : #define bool int
41 : : #endif
42 : :
43 : : #ifndef FALSE
44 : : #define FALSE (0)
45 : : #endif
46 : :
47 : : #ifndef TRUE
48 : : #define TRUE (1)
49 : : #endif
50 : :
51 : : #ifndef false
52 : : #define false (0)
53 : : #endif
54 : :
55 : : #ifndef true
56 : : #define true (1)
57 : : #endif
58 : :
59 : : #ifndef NULL
60 : : #define NULL ((void *)0)
61 : : #endif
62 : :
63 : : #define HINIC_ERROR (-1)
64 : : #define HINIC_OK (0)
65 : :
66 : : #ifndef BIT
67 : : #define BIT(n) (1 << (n))
68 : : #endif
69 : :
70 : : #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
71 : : #define lower_32_bits(n) ((u32)(n))
72 : :
73 : : /* Returns X / Y, rounding up. X must be nonnegative to round correctly. */
74 : : #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
75 : :
76 : : /* Returns X rounded up to the nearest multiple of Y. */
77 : : #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y))
78 : :
79 : : #undef ALIGN
80 : : #define ALIGN(x, a) RTE_ALIGN(x, a)
81 : :
82 : : #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
83 : :
84 : : extern int hinic_logtype;
85 : : #define RTE_LOGTYPE_NET_HINIC hinic_logtype
86 : :
87 : : #define PMD_DRV_LOG(level, ...) \
88 : : RTE_LOG_LINE(level, NET_HINIC, __VA_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_ALIGN_MIN 8
115 : :
116 : : #define HINIC_PAGE_SIZE_DPDK 6
117 : :
118 : : void *dma_zalloc_coherent(void *dev, size_t size, dma_addr_t *dma_handle,
119 : : unsigned int socket_id);
120 : :
121 : : void *dma_zalloc_coherent_aligned(void *hwdev, size_t size,
122 : : dma_addr_t *dma_handle, unsigned int socket_id);
123 : :
124 : : void *dma_zalloc_coherent_aligned256k(void *hwdev, size_t size,
125 : : dma_addr_t *dma_handle, unsigned int socket_id);
126 : :
127 : : void dma_free_coherent(void *dev, size_t size, void *virt, dma_addr_t phys);
128 : :
129 : : /* dma pool alloc and free */
130 : : #define pci_pool dma_pool
131 : : #define pci_pool_alloc(pool, handle) dma_pool_alloc(pool, handle)
132 : : #define pci_pool_free(pool, vaddr, addr) dma_pool_free(pool, vaddr, addr)
133 : :
134 : : struct dma_pool *dma_pool_create(const char *name, void *dev, size_t size,
135 : : size_t align, size_t boundary);
136 : : void dma_pool_destroy(struct dma_pool *pool);
137 : : void *dma_pool_alloc(struct pci_pool *pool, dma_addr_t *dma_addr);
138 : : void dma_pool_free(struct pci_pool *pool, void *vaddr, dma_addr_t dma);
139 : :
140 : : #define kzalloc(size, flag) rte_zmalloc(NULL, size, HINIC_MEM_ALLOC_ALIGN_MIN)
141 : : #define kzalloc_aligned(size, flag) rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE)
142 : : #define kfree(ptr) rte_free(ptr)
143 : :
144 : : /* mmio interface */
145 : : static inline void writel(u32 value, volatile void *addr)
146 : : {
147 [ # # # # ]: 0 : *(volatile u32 *)addr = value;
148 : 0 : }
149 : :
150 : : static inline u32 readl(const volatile void *addr)
151 : : {
152 [ # # ]: 0 : return *(const volatile u32 *)addr;
153 : : }
154 : :
155 : : #define __raw_writel(value, reg) writel((value), (reg))
156 : : #define __raw_readl(reg) readl((reg))
157 : :
158 : : /* Spinlock related interface */
159 : : #define hinic_spinlock_t rte_spinlock_t
160 : :
161 : : #define spinlock_t rte_spinlock_t
162 : : #define spin_lock_init(spinlock_prt) rte_spinlock_init(spinlock_prt)
163 : : #define spin_lock_deinit(lock)
164 : : #define spin_lock(spinlock_prt) rte_spinlock_lock(spinlock_prt)
165 : : #define spin_unlock(spinlock_prt) rte_spinlock_unlock(spinlock_prt)
166 : :
167 : : #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
168 : : #define CLOCK_TYPE CLOCK_MONOTONIC_RAW
169 : : #else
170 : : #define CLOCK_TYPE CLOCK_MONOTONIC
171 : : #endif
172 : : #define HINIC_MUTEX_TIMEOUT 10
173 : :
174 : : static inline unsigned long clock_gettime_ms(void)
175 : : {
176 : : struct timespec tv;
177 : :
178 : 0 : (void)clock_gettime(CLOCK_TYPE, &tv);
179 : :
180 : 0 : return (unsigned long)tv.tv_sec * 1000 +
181 [ # # # # : 0 : (unsigned long)tv.tv_nsec / 1000000;
# # ]
182 : : }
183 : :
184 : : #define jiffies clock_gettime_ms()
185 : : #define msecs_to_jiffies(ms) (ms)
186 : : #define time_before(now, end) ((now) < (end))
187 : :
188 : : /* misc kernel utils */
189 : : static inline u16 ilog2(u32 n)
190 : : {
191 : : u16 res = 0;
192 : :
193 [ # # # # ]: 0 : while (n > 1) {
194 : 0 : n >>= 1;
195 : 0 : res++;
196 : : }
197 : :
198 : : return res;
199 : : }
200 : :
201 : : static inline int hinic_mutex_init(pthread_mutex_t *pthreadmutex,
202 : : __rte_unused const pthread_mutexattr_t *mattr)
203 : : {
204 : 0 : rte_thread_mutex_init_shared(pthreadmutex);
205 : : return 0;
206 : : }
207 : :
208 : 0 : static inline int hinic_mutex_destroy(pthread_mutex_t *pthreadmutex)
209 : : {
210 : : int err;
211 : :
212 : 0 : err = pthread_mutex_destroy(pthreadmutex);
213 [ # # ]: 0 : if (unlikely(err))
214 : 0 : PMD_DRV_LOG(ERR, "Fail to destroy mutex, error: %d", err);
215 : :
216 : 0 : return err;
217 : : }
218 : :
219 : 0 : static inline int hinic_mutex_lock(pthread_mutex_t *pthreadmutex)
220 : : {
221 : : int err;
222 : : struct timespec tout;
223 : :
224 : 0 : (void)clock_gettime(CLOCK_TYPE, &tout);
225 : :
226 : 0 : tout.tv_sec += HINIC_MUTEX_TIMEOUT;
227 : 0 : err = pthread_mutex_timedlock(pthreadmutex, &tout);
228 [ # # ]: 0 : if (err)
229 : 0 : PMD_DRV_LOG(ERR, "Mutex lock failed. (ErrorNo=%d)", err);
230 : :
231 : 0 : return err;
232 : : }
233 : :
234 : : static inline int hinic_mutex_unlock(pthread_mutex_t *pthreadmutex)
235 : : {
236 : 0 : return pthread_mutex_unlock(pthreadmutex);
237 : : }
238 : :
239 : : #endif /* _HINIC_COMPAT_H_ */
|