Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2001-2023 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _IDPF_OSDEP_H_
6 : : #define _IDPF_OSDEP_H_
7 : :
8 : : #include <string.h>
9 : : #include <stdint.h>
10 : : #include <stdio.h>
11 : : #include <stdarg.h>
12 : : #include <inttypes.h>
13 : : #include <sys/queue.h>
14 : : #include <stdbool.h>
15 : :
16 : : #include <rte_common.h>
17 : : #include <rte_memcpy.h>
18 : : #include <rte_malloc.h>
19 : : #include <rte_memzone.h>
20 : : #include <rte_byteorder.h>
21 : : #include <rte_cycles.h>
22 : : #include <rte_spinlock.h>
23 : : #include <rte_log.h>
24 : : #include <rte_random.h>
25 : : #include <rte_io.h>
26 : : #include <rte_compat.h>
27 : :
28 : : #define INLINE inline
29 : : #define STATIC static
30 : :
31 : : typedef uint8_t u8;
32 : : typedef int8_t s8;
33 : : typedef uint16_t u16;
34 : : typedef int16_t s16;
35 : : typedef uint32_t u32;
36 : : typedef int32_t s32;
37 : : typedef uint64_t u64;
38 : : typedef uint64_t s64;
39 : :
40 : : typedef struct idpf_lock idpf_lock;
41 : :
42 : : #define __iomem
43 : : #define hw_dbg(hw, S, A...) do {} while (0)
44 : : #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
45 : : #define lower_32_bits(n) ((u32)(n))
46 : : #define low_16_bits(x) ((x) & 0xFFFF)
47 : : #define high_16_bits(x) (((x) & 0xFFFF0000) >> 16)
48 : :
49 : : #define IDPF_M(m, s) ((m) << (s))
50 : :
51 : : #define BITS_PER_LONG (8 * sizeof(long))
52 : : #define BITS_PER_LONG_LONG (8 * sizeof(long long))
53 : : #define GENMASK(h, l) \
54 : : (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
55 : : #define GENMASK_ULL(h, l) \
56 : : (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
57 : :
58 : : #ifndef ETH_ADDR_LEN
59 : : #define ETH_ADDR_LEN 6
60 : : #endif
61 : :
62 : : #ifndef __le16
63 : : #define __le16 uint16_t
64 : : #endif
65 : : #ifndef __le32
66 : : #define __le32 uint32_t
67 : : #endif
68 : : #ifndef __le64
69 : : #define __le64 uint64_t
70 : : #endif
71 : : #ifndef __be16
72 : : #define __be16 uint16_t
73 : : #endif
74 : : #ifndef __be32
75 : : #define __be32 uint32_t
76 : : #endif
77 : : #ifndef __be64
78 : : #define __be64 uint64_t
79 : : #endif
80 : :
81 : : #ifndef BIT_ULL
82 : : #define BIT_ULL(a) RTE_BIT64(a)
83 : : #endif
84 : :
85 : : #ifndef BIT
86 : : #define BIT(a) RTE_BIT32(a)
87 : : #endif
88 : :
89 : : #define FALSE 0
90 : : #define TRUE 1
91 : : #define false 0
92 : : #define true 1
93 : :
94 : : /* Avoid macro redefinition warning on Windows */
95 : : #ifdef RTE_EXEC_ENV_WINDOWS
96 : : #ifdef min
97 : : #undef min
98 : : #endif
99 : : #ifdef max
100 : : #undef max
101 : : #endif
102 : : #endif
103 : :
104 : : #define min(a, b) RTE_MIN(a, b)
105 : : #define max(a, b) RTE_MAX(a, b)
106 : :
107 : : #define ARRAY_SIZE(arr) RTE_DIM(arr)
108 : : #define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->(f)))
109 : : #define MAKEMASK(m, s) ((m) << (s))
110 : :
111 : : extern int idpf_common_logger;
112 : :
113 : : #define DEBUGOUT(S) rte_log(RTE_LOG_DEBUG, idpf_common_logger, S)
114 : : #define DEBUGOUT2(S, A...) rte_log(RTE_LOG_DEBUG, idpf_common_logger, S, ##A)
115 : : #define DEBUGFUNC(F) DEBUGOUT(F "\n")
116 : :
117 : : #define idpf_debug(h, m, s, ...) \
118 : : do { \
119 : : if (((m) & (h)->debug_mask)) \
120 : : PMD_DRV_LOG_RAW(DEBUG, "idpf %02x.%x " s, \
121 : : (h)->bus.device, (h)->bus.func, \
122 : : ##__VA_ARGS__); \
123 : : } while (0)
124 : :
125 : : #define idpf_info(hw, fmt, args...) idpf_debug(hw, IDPF_DBG_ALL, fmt, ##args)
126 : : #define idpf_warn(hw, fmt, args...) idpf_debug(hw, IDPF_DBG_ALL, fmt, ##args)
127 : : #define idpf_debug_array(hw, type, rowsize, groupsize, buf, len) \
128 : : do { \
129 : : struct idpf_hw *hw_l = hw; \
130 : : u16 len_l = len; \
131 : : u8 *buf_l = buf; \
132 : : int i; \
133 : : for (i = 0; i < len_l; i += 8) \
134 : : idpf_debug(hw_l, type, \
135 : : "0x%04X 0x%016"PRIx64"\n", \
136 : : i, *((u64 *)((buf_l) + i))); \
137 : : } while (0)
138 : : #define idpf_snprintf snprintf
139 : : #ifndef SNPRINTF
140 : : #define SNPRINTF idpf_snprintf
141 : : #endif
142 : :
143 : : #define IDPF_PCI_REG(reg) rte_read32(reg)
144 : : #define IDPF_PCI_REG_ADDR(a, reg) \
145 : : ((volatile uint32_t *)((char *)(a)->hw_addr + (reg)))
146 : : #define IDPF_PCI_REG64(reg) rte_read64(reg)
147 : : #define IDPF_PCI_REG_ADDR64(a, reg) \
148 : : ((volatile uint64_t *)((char *)(a)->hw_addr + (reg)))
149 : :
150 : : #define idpf_wmb() rte_io_wmb()
151 : : #define idpf_rmb() rte_io_rmb()
152 : : #define idpf_mb() rte_io_mb()
153 : :
154 : : static inline uint32_t idpf_read_addr(volatile void *addr)
155 : : {
156 : : return rte_le_to_cpu_32(IDPF_PCI_REG(addr));
157 : : }
158 : :
159 : : static inline uint64_t idpf_read_addr64(volatile void *addr)
160 : : {
161 : : return rte_le_to_cpu_64(IDPF_PCI_REG64(addr));
162 : : }
163 : :
164 : : #define IDPF_PCI_REG_WRITE(reg, value) \
165 : : rte_write32((rte_cpu_to_le_32(value)), reg)
166 : :
167 : : #define IDPF_PCI_REG_WRITE64(reg, value) \
168 : : rte_write64((rte_cpu_to_le_64(value)), reg)
169 : :
170 : : #define IDPF_READ_REG(hw, reg) idpf_read_addr(IDPF_PCI_REG_ADDR((hw), (reg)))
171 : : #define IDPF_WRITE_REG(hw, reg, value) \
172 : : IDPF_PCI_REG_WRITE(IDPF_PCI_REG_ADDR((hw), (reg)), (value))
173 : :
174 : : #define rd32(a, reg) idpf_read_addr(IDPF_PCI_REG_ADDR((a), (reg)))
175 : : #define wr32(a, reg, value) \
176 : : IDPF_PCI_REG_WRITE(IDPF_PCI_REG_ADDR((a), (reg)), (value))
177 : : #define div64_long(n, d) ((n) / (d))
178 : : #define rd64(a, reg) idpf_read_addr64(IDPF_PCI_REG_ADDR64((a), (reg)))
179 : :
180 : : #define BITS_PER_BYTE 8
181 : :
182 : : /* memory allocation tracking */
183 : : struct idpf_dma_mem {
184 : : void *va;
185 : : u64 pa;
186 : : u32 size;
187 : : const void *zone;
188 : : } __rte_packed;
189 : :
190 : : struct idpf_virt_mem {
191 : : void *va;
192 : : u32 size;
193 : : } __rte_packed;
194 : :
195 : : #define idpf_malloc(h, s) rte_zmalloc(NULL, s, 0)
196 : : #define idpf_calloc(h, c, s) rte_zmalloc(NULL, (c) * (s), 0)
197 : : #define idpf_free(h, m) rte_free(m)
198 : :
199 : : #define idpf_memset(a, b, c, d) memset((a), (b), (c))
200 : : #define idpf_memcpy(a, b, c, d) rte_memcpy((a), (b), (c))
201 : : #define idpf_memdup(a, b, c, d) rte_memcpy(idpf_malloc(a, c), b, c)
202 : :
203 : : #define CPU_TO_BE16(o) rte_cpu_to_be_16(o)
204 : : #define CPU_TO_BE32(o) rte_cpu_to_be_32(o)
205 : : #define CPU_TO_BE64(o) rte_cpu_to_be_64(o)
206 : : #define CPU_TO_LE16(o) rte_cpu_to_le_16(o)
207 : : #define CPU_TO_LE32(s) rte_cpu_to_le_32(s)
208 : : #define CPU_TO_LE64(h) rte_cpu_to_le_64(h)
209 : : #define LE16_TO_CPU(a) rte_le_to_cpu_16(a)
210 : : #define LE32_TO_CPU(c) rte_le_to_cpu_32(c)
211 : : #define LE64_TO_CPU(k) rte_le_to_cpu_64(k)
212 : :
213 : : #define NTOHS(a) rte_be_to_cpu_16(a)
214 : : #define NTOHL(a) rte_be_to_cpu_32(a)
215 : : #define HTONS(a) rte_cpu_to_be_16(a)
216 : : #define HTONL(a) rte_cpu_to_be_32(a)
217 : :
218 : : /* SW spinlock */
219 : : struct idpf_lock {
220 : : rte_spinlock_t spinlock;
221 : : };
222 : :
223 : : #define idpf_init_lock(sp) rte_spinlock_init(&(sp)->spinlock)
224 : : #define idpf_acquire_lock(sp) rte_spinlock_lock(&(sp)->spinlock)
225 : : #define idpf_release_lock(sp) rte_spinlock_unlock(&(sp)->spinlock)
226 : : #define idpf_destroy_lock(sp) RTE_SET_USED(sp)
227 : :
228 : : struct idpf_hw;
229 : :
230 : : static inline void *
231 : 0 : idpf_alloc_dma_mem(__rte_unused struct idpf_hw *hw,
232 : : struct idpf_dma_mem *mem, u64 size)
233 : : {
234 : : const struct rte_memzone *mz = NULL;
235 : : char z_name[RTE_MEMZONE_NAMESIZE];
236 : :
237 [ # # ]: 0 : if (!mem)
238 : : return NULL;
239 : :
240 : 0 : snprintf(z_name, sizeof(z_name), "idpf_dma_%"PRIu64, rte_rand());
241 : 0 : mz = rte_memzone_reserve_aligned(z_name, size, SOCKET_ID_ANY,
242 : : RTE_MEMZONE_IOVA_CONTIG, RTE_PGSIZE_4K);
243 [ # # ]: 0 : if (!mz)
244 : : return NULL;
245 : :
246 : 0 : mem->size = size;
247 : 0 : mem->va = mz->addr;
248 : 0 : mem->pa = mz->iova;
249 : 0 : mem->zone = (const void *)mz;
250 : : memset(mem->va, 0, size);
251 : :
252 : 0 : return mem->va;
253 : : }
254 : :
255 : : static inline void
256 : : idpf_free_dma_mem(__rte_unused struct idpf_hw *hw,
257 : : struct idpf_dma_mem *mem)
258 : : {
259 : 0 : rte_memzone_free((const struct rte_memzone *)mem->zone);
260 : 0 : mem->size = 0;
261 : 0 : mem->va = NULL;
262 : 0 : mem->pa = 0;
263 : 0 : }
264 : :
265 : : static inline u8
266 : : idpf_hweight8(u32 num)
267 : : {
268 : : u8 bits = 0;
269 : : u32 i;
270 : :
271 : : for (i = 0; i < 8; i++) {
272 : : bits += (u8)(num & 0x1);
273 : : num >>= 1;
274 : : }
275 : :
276 : : return bits;
277 : : }
278 : :
279 : : static inline u8
280 : : idpf_hweight32(u32 num)
281 : : {
282 : : u8 bits = 0;
283 : : u32 i;
284 : :
285 : : for (i = 0; i < 32; i++) {
286 : : bits += (u8)(num & 0x1);
287 : : num >>= 1;
288 : : }
289 : :
290 : : return bits;
291 : : }
292 : :
293 : : #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
294 : : #define DELAY(x) rte_delay_us(x)
295 : : #define idpf_usec_delay(x) rte_delay_us(x)
296 : : #define idpf_msec_delay(x, y) rte_delay_us(1000 * (x))
297 : : #define udelay(x) DELAY(x)
298 : : #define msleep(x) DELAY(1000 * (x))
299 : : #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
300 : :
301 : : #ifndef IDPF_DBG_TRACE
302 : : #define IDPF_DBG_TRACE BIT_ULL(0)
303 : : #endif
304 : :
305 : : #ifndef DIVIDE_AND_ROUND_UP
306 : : #define DIVIDE_AND_ROUND_UP(a, b) (((a) + (b) - 1) / (b))
307 : : #endif
308 : :
309 : : #ifndef IDPF_INTEL_VENDOR_ID
310 : : #define IDPF_INTEL_VENDOR_ID 0x8086
311 : : #endif
312 : :
313 : : #ifndef IS_UNICAST_ETHER_ADDR
314 : : #define IS_UNICAST_ETHER_ADDR(addr) \
315 : : ((bool)((((u8 *)(addr))[0] % ((u8)0x2)) == 0))
316 : : #endif
317 : :
318 : : #ifndef IS_MULTICAST_ETHER_ADDR
319 : : #define IS_MULTICAST_ETHER_ADDR(addr) \
320 : : ((bool)((((u8 *)(addr))[0] % ((u8)0x2)) == 1))
321 : : #endif
322 : :
323 : : #ifndef IS_BROADCAST_ETHER_ADDR
324 : : /* Check whether an address is broadcast. */
325 : : #define IS_BROADCAST_ETHER_ADDR(addr) \
326 : : ((bool)((((u16 *)(addr))[0] == ((u16)0xffff))))
327 : : #endif
328 : :
329 : : #ifndef IS_ZERO_ETHER_ADDR
330 : : #define IS_ZERO_ETHER_ADDR(addr) \
331 : : (((bool)((((u16 *)(addr))[0] == ((u16)0x0)))) && \
332 : : ((bool)((((u16 *)(addr))[1] == ((u16)0x0)))) && \
333 : : ((bool)((((u16 *)(addr))[2] == ((u16)0x0)))))
334 : : #endif
335 : :
336 : : #ifndef LIST_HEAD_TYPE
337 : : #define LIST_HEAD_TYPE(list_name, type) LIST_HEAD(list_name, type)
338 : : #endif
339 : :
340 : : #ifndef LIST_ENTRY_TYPE
341 : : #define LIST_ENTRY_TYPE(type) LIST_ENTRY(type)
342 : : #endif
343 : :
344 : : #ifndef LIST_FOR_EACH_ENTRY_SAFE
345 : : #define LIST_FOR_EACH_ENTRY_SAFE(pos, temp, head, entry_type, list) \
346 : : LIST_FOREACH(pos, head, list)
347 : :
348 : : #endif
349 : :
350 : : #ifndef LIST_FOR_EACH_ENTRY
351 : : #define LIST_FOR_EACH_ENTRY(pos, head, entry_type, list) \
352 : : LIST_FOREACH(pos, head, list)
353 : :
354 : : #endif
355 : :
356 : : #endif /* _IDPF_OSDEP_H_ */
|