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