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 : : #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 : : #include "../idpf_common_logs.h"
29 : :
30 : : #define INLINE inline
31 : : #define STATIC static
32 : :
33 : : typedef uint8_t u8;
34 : : typedef int8_t s8;
35 : : typedef uint16_t u16;
36 : : typedef int16_t s16;
37 : : typedef uint32_t u32;
38 : : typedef int32_t s32;
39 : : typedef uint64_t u64;
40 : : typedef uint64_t s64;
41 : :
42 : : typedef struct idpf_lock idpf_lock;
43 : :
44 : : #define __iomem
45 : : #define hw_dbg(hw, S, A...) do {} while (0)
46 : : #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
47 : : #define lower_32_bits(n) ((u32)(n))
48 : : #define low_16_bits(x) ((x) & 0xFFFF)
49 : : #define high_16_bits(x) (((x) & 0xFFFF0000) >> 16)
50 : :
51 : : #define IDPF_M(m, s) ((m) << (s))
52 : :
53 : : #define BITS_PER_LONG (8 * sizeof(long))
54 : : #define BITS_PER_LONG_LONG (8 * sizeof(long long))
55 : : #define GENMASK(h, l) \
56 : : (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
57 : : #define GENMASK_ULL(h, l) \
58 : : (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
59 : :
60 : : #ifndef ETH_ADDR_LEN
61 : : #define ETH_ADDR_LEN 6
62 : : #endif
63 : :
64 : : #ifndef __le16
65 : : #define __le16 uint16_t
66 : : #endif
67 : : #ifndef __le32
68 : : #define __le32 uint32_t
69 : : #endif
70 : : #ifndef __le64
71 : : #define __le64 uint64_t
72 : : #endif
73 : : #ifndef __be16
74 : : #define __be16 uint16_t
75 : : #endif
76 : : #ifndef __be32
77 : : #define __be32 uint32_t
78 : : #endif
79 : : #ifndef __be64
80 : : #define __be64 uint64_t
81 : : #endif
82 : :
83 : : #ifndef BIT_ULL
84 : : #define BIT_ULL(a) RTE_BIT64(a)
85 : : #endif
86 : :
87 : : #ifndef BIT
88 : : #define BIT(a) RTE_BIT32(a)
89 : : #endif
90 : :
91 : : #define FALSE 0
92 : : #define TRUE 1
93 : : #define false 0
94 : : #define true 1
95 : :
96 : : /* Avoid macro redefinition warning on Windows */
97 : : #ifdef RTE_EXEC_ENV_WINDOWS
98 : : #ifdef min
99 : : #undef min
100 : : #endif
101 : : #ifdef max
102 : : #undef max
103 : : #endif
104 : : #endif
105 : :
106 : : #define min(a, b) RTE_MIN(a, b)
107 : : #define max(a, b) RTE_MAX(a, b)
108 : :
109 : : #define ARRAY_SIZE(arr) RTE_DIM(arr)
110 : : #define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->(f)))
111 : : #define MAKEMASK(m, s) ((m) << (s))
112 : :
113 : : #define DEBUGOUT(S, ...) RTE_LOG(DEBUG, IDPF_COMMON, S, ## __VA_ARGS__)
114 : : #define DEBUGOUT2(S, ...) DEBUGOUT(S, ## __VA_ARGS__)
115 : : #define DEBUGFUNC(F) DEBUGOUT(F "\n")
116 : :
117 : : #define idpf_debug(h, m, s, ...) \
118 : : do { \
119 : : if (((m) & (h)->debug_mask)) \
120 : : DEBUGOUT("idpf %02x.%x " s "\n", \
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_FOREACH_SAFE
345 : : #define LIST_FOREACH_SAFE(var, head, field, tvar) \
346 : : for ((var) = LIST_FIRST((head)); \
347 : : (var) && ((tvar) = LIST_NEXT((var), field), 1); \
348 : : (var) = (tvar))
349 : : #endif
350 : :
351 : : #ifndef LIST_FOR_EACH_ENTRY_SAFE
352 : : #define LIST_FOR_EACH_ENTRY_SAFE(pos, temp, head, entry_type, list) \
353 : : LIST_FOREACH_SAFE(pos, head, list, temp)
354 : : #endif
355 : :
356 : : #ifndef LIST_FOR_EACH_ENTRY
357 : : #define LIST_FOR_EACH_ENTRY(pos, head, entry_type, list) \
358 : : LIST_FOREACH(pos, head, list)
359 : :
360 : : #endif
361 : :
362 : : enum idpf_mac_type {
363 : : IDPF_MAC_UNKNOWN = 0,
364 : : IDPF_MAC_PF,
365 : : IDPF_MAC_VF,
366 : : IDPF_MAC_GENERIC
367 : : };
368 : :
369 : : #define ETH_ALEN 6
370 : :
371 : : struct idpf_mac_info {
372 : : enum idpf_mac_type type;
373 : : u8 addr[ETH_ALEN];
374 : : u8 perm_addr[ETH_ALEN];
375 : : };
376 : :
377 : : #define IDPF_AQ_LINK_UP 0x1
378 : :
379 : : /* PCI bus types */
380 : : enum idpf_bus_type {
381 : : idpf_bus_type_unknown = 0,
382 : : idpf_bus_type_pci,
383 : : idpf_bus_type_pcix,
384 : : idpf_bus_type_pci_express,
385 : : idpf_bus_type_reserved
386 : : };
387 : :
388 : : /* PCI bus speeds */
389 : : enum idpf_bus_speed {
390 : : idpf_bus_speed_unknown = 0,
391 : : idpf_bus_speed_33 = 33,
392 : : idpf_bus_speed_66 = 66,
393 : : idpf_bus_speed_100 = 100,
394 : : idpf_bus_speed_120 = 120,
395 : : idpf_bus_speed_133 = 133,
396 : : idpf_bus_speed_2500 = 2500,
397 : : idpf_bus_speed_5000 = 5000,
398 : : idpf_bus_speed_8000 = 8000,
399 : : idpf_bus_speed_reserved
400 : : };
401 : :
402 : : /* PCI bus widths */
403 : : enum idpf_bus_width {
404 : : idpf_bus_width_unknown = 0,
405 : : idpf_bus_width_pcie_x1 = 1,
406 : : idpf_bus_width_pcie_x2 = 2,
407 : : idpf_bus_width_pcie_x4 = 4,
408 : : idpf_bus_width_pcie_x8 = 8,
409 : : idpf_bus_width_32 = 32,
410 : : idpf_bus_width_64 = 64,
411 : : idpf_bus_width_reserved
412 : : };
413 : :
414 : : /* Bus parameters */
415 : : struct idpf_bus_info {
416 : : enum idpf_bus_speed speed;
417 : : enum idpf_bus_width width;
418 : : enum idpf_bus_type type;
419 : :
420 : : u16 func;
421 : : u16 device;
422 : : u16 lan_id;
423 : : u16 bus_id;
424 : : };
425 : :
426 : : /* Function specific capabilities */
427 : : struct idpf_hw_func_caps {
428 : : u32 num_alloc_vfs;
429 : : u32 vf_base_id;
430 : : };
431 : :
432 : : #endif /* _IDPF_OSDEP_H_ */
|