Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018-2022 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _ICE_OSDEP_H_
6 : : #define _ICE_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_io.h>
26 : :
27 : : #include "ice_alloc.h"
28 : :
29 : : #include "ice_logs.h"
30 : :
31 : : #ifndef __INTEL_NET_BASE_OSDEP__
32 : : #define __INTEL_NET_BASE_OSDEP__
33 : :
34 : : #define INLINE inline
35 : : #define STATIC static
36 : :
37 : : typedef uint8_t u8;
38 : : typedef int8_t s8;
39 : : typedef uint16_t u16;
40 : : typedef int16_t s16;
41 : : typedef uint32_t u32;
42 : : typedef int32_t s32;
43 : : typedef uint64_t u64;
44 : : typedef uint64_t s64;
45 : :
46 : : #ifndef __le16
47 : : #define __le16 uint16_t
48 : : #endif
49 : : #ifndef __le32
50 : : #define __le32 uint32_t
51 : : #endif
52 : : #ifndef __le64
53 : : #define __le64 uint64_t
54 : : #endif
55 : : #ifndef __be16
56 : : #define __be16 uint16_t
57 : : #endif
58 : : #ifndef __be32
59 : : #define __be32 uint32_t
60 : : #endif
61 : : #ifndef __be64
62 : : #define __be64 uint64_t
63 : : #endif
64 : :
65 : : /* Avoid macro redefinition warning on Windows */
66 : : #ifdef RTE_EXEC_ENV_WINDOWS
67 : : #ifdef min
68 : : #undef min
69 : : #endif
70 : : #ifdef max
71 : : #undef max
72 : : #endif
73 : : #endif
74 : : #define min(a, b) RTE_MIN(a, b)
75 : : #define max(a, b) RTE_MAX(a, b)
76 : :
77 : : #define FIELD_SIZEOF(t, f) RTE_SIZEOF_FIELD(t, f)
78 : : #define ARRAY_SIZE(arr) RTE_DIM(arr)
79 : :
80 : : #define CPU_TO_LE16(o) rte_cpu_to_le_16(o)
81 : : #define CPU_TO_LE32(s) rte_cpu_to_le_32(s)
82 : : #define CPU_TO_LE64(h) rte_cpu_to_le_64(h)
83 : : #define LE16_TO_CPU(a) rte_le_to_cpu_16(a)
84 : : #define LE32_TO_CPU(c) rte_le_to_cpu_32(c)
85 : : #define LE64_TO_CPU(k) rte_le_to_cpu_64(k)
86 : :
87 : : #define CPU_TO_BE16(o) rte_cpu_to_be_16(o)
88 : : #define CPU_TO_BE32(o) rte_cpu_to_be_32(o)
89 : : #define CPU_TO_BE64(o) rte_cpu_to_be_64(o)
90 : : #define BE16_TO_CPU(o) rte_be_to_cpu_16(o)
91 : :
92 : : #define NTOHS(a) rte_be_to_cpu_16(a)
93 : : #define NTOHL(a) rte_be_to_cpu_32(a)
94 : : #define HTONS(a) rte_cpu_to_be_16(a)
95 : : #define HTONL(a) rte_cpu_to_be_32(a)
96 : :
97 : : static __rte_always_inline uint32_t
98 : : readl(volatile void *addr)
99 : : {
100 : 0 : return rte_le_to_cpu_32(rte_read32(addr));
101 : : }
102 : :
103 : : static __rte_always_inline void
104 : : writel(uint32_t value, volatile void *addr)
105 : : {
106 : : rte_write32(rte_cpu_to_le_32(value), addr);
107 : 0 : }
108 : :
109 : : static __rte_always_inline void
110 : : writel_relaxed(uint32_t value, volatile void *addr)
111 : : {
112 : : rte_write32_relaxed(rte_cpu_to_le_32(value), addr);
113 : : }
114 : :
115 : : static __rte_always_inline uint64_t
116 : : readq(volatile void *addr)
117 : : {
118 : : return rte_le_to_cpu_64(rte_read64(addr));
119 : : }
120 : :
121 : : static __rte_always_inline void
122 : : writeq(uint64_t value, volatile void *addr)
123 : : {
124 : : rte_write64(rte_cpu_to_le_64(value), addr);
125 : : }
126 : :
127 : : #define wr32(a, reg, value) writel((value), (a)->hw_addr + (reg))
128 : : #define rd32(a, reg) readl((a)->hw_addr + (reg))
129 : : #define wr64(a, reg, value) writeq((value), (a)->hw_addr + (reg))
130 : : #define rd64(a, reg) readq((a)->hw_addr + (reg))
131 : :
132 : : #ifdef RTE_TOOLCHAIN_MSVC
133 : : #define __builtin_add_overflow(a, b, res) \
134 : : _Generic((a), \
135 : : uint8_t : _addcarry_u8, \
136 : : uint16_t : _addcarry_u16, \
137 : : uint32_t : _addcarry_u32, \
138 : : uint64_t : _addcarry_u64)(0, a, b, res)
139 : : #endif
140 : :
141 : : #endif /* __INTEL_NET_BASE_OSDEP__ */
142 : :
143 : : #ifndef __always_unused
144 : : #define __always_unused __rte_unused
145 : : #endif
146 : : #ifndef __maybe_unused
147 : : #define __maybe_unused __rte_unused
148 : : #endif
149 : :
150 : : #ifndef BIT_ULL
151 : : #define BIT_ULL(a) (1ULL << (a))
152 : : #endif
153 : :
154 : : #define MAKEMASK(m, s) ((m) << (s))
155 : :
156 : : #define ice_debug(h, m, s, ...) \
157 : : do { \
158 : : if (((m) & (h)->debug_mask)) \
159 : : RTE_LOG(DEBUG, ICE_DRIVER, "ice %02x.%x " s, \
160 : : (h)->bus.device, (h)->bus.func, \
161 : : ##__VA_ARGS__); \
162 : : } while (0)
163 : :
164 : : #define ice_info(hw, fmt, ...) ice_debug(hw, ICE_DBG_ALL, fmt, ##__VA_ARGS__)
165 : : #define ice_warn(hw, fmt, ...) ice_debug(hw, ICE_DBG_ALL, fmt, ##__VA_ARGS__)
166 : : #define ice_debug_array(hw, type, rowsize, groupsize, buf, len) \
167 : : do { \
168 : : struct ice_hw *hw_l = hw; \
169 : : u16 len_l = len; \
170 : : u8 *buf_l = buf; \
171 : : int i; \
172 : : for (i = 0; i < len_l; i += 8) \
173 : : ice_debug(hw_l, type, \
174 : : "0x%04X 0x%016"PRIx64"\n", \
175 : : i, *((u64 *)((buf_l) + i))); \
176 : : } while (0)
177 : : #define ice_snprintf snprintf
178 : : #ifndef SNPRINTF
179 : : #define SNPRINTF ice_snprintf
180 : : #endif
181 : :
182 : : #define ICE_PCI_REG_WRITE(reg, value) writel(value, reg)
183 : : #define ICE_PCI_REG_WC_WRITE(reg, value) rte_write32_wc(value, reg)
184 : :
185 : : #define ICE_READ_REG(hw, reg) rd32(hw, reg)
186 : : #define ICE_WRITE_REG(hw, reg, value) wr32(hw, reg, value)
187 : :
188 : : #define ice_flush(a) ICE_READ_REG((a), GLGEN_STAT)
189 : : #define icevf_flush(a) ICE_READ_REG((a), VFGEN_RSTAT)
190 : :
191 : : #define flush(a) ICE_READ_REG((a), GLGEN_STAT)
192 : : #define div64_long(n, d) ((n) / (d))
193 : :
194 : : #define BITS_PER_BYTE 8
195 : :
196 : : /* memory allocation tracking */
197 : : struct __rte_packed_begin ice_dma_mem {
198 : : void *va;
199 : : u64 pa;
200 : : u32 size;
201 : : const void *zone;
202 : : } __rte_packed_end;
203 : :
204 : : struct __rte_packed_begin ice_virt_mem {
205 : : void *va;
206 : : u32 size;
207 : : } __rte_packed_end;
208 : :
209 : : #define ice_malloc(h, s) ((void)h, rte_zmalloc(NULL, s, 0))
210 : : #define ice_calloc(h, c, s) ((void)h, rte_calloc(NULL, c, s, 0))
211 : : #define ice_free(h, m) ((void)h, rte_free(m))
212 : :
213 : : #define ice_memset(a, b, c, d) memset((a), (b), (c))
214 : : #define ice_memcpy(a, b, c, d) memcpy((a), (b), (c))
215 : :
216 : : /* Memory fence barrier */
217 : : #define ice_memfence_read() rte_io_rmb()
218 : : #define ice_memfence_read_write() rte_io_mb()
219 : : #define ice_memfence_write() rte_io_wmb()
220 : :
221 : :
222 : : /* SW spinlock */
223 : : struct ice_lock {
224 : : rte_spinlock_t spinlock;
225 : : };
226 : :
227 : : #define ice_init_lock(sp) rte_spinlock_init(&(sp)->spinlock)
228 : : #define ice_acquire_lock(sp) rte_spinlock_lock(&(sp)->spinlock)
229 : : #define ice_release_lock(sp) rte_spinlock_unlock(&(sp)->spinlock)
230 : : #define ice_destroy_lock(sp) RTE_SET_USED(sp)
231 : :
232 : : struct ice_hw;
233 : :
234 : : static __rte_always_inline void *
235 : : ice_memdup(__rte_unused struct ice_hw *hw, const void *src, size_t size,
236 : : __rte_unused enum ice_memcpy_type dir)
237 : : {
238 : : void *p;
239 : :
240 : 0 : p = ice_malloc(hw, size);
241 [ # # # # ]: 0 : if (p)
242 : : memcpy(p, src, size);
243 : :
244 : : return p;
245 : : }
246 : :
247 : : static inline void *
248 : 0 : ice_alloc_dma_mem(__rte_unused struct ice_hw *hw,
249 : : struct ice_dma_mem *mem, u64 size)
250 : : {
251 : : static RTE_ATOMIC(uint64_t) ice_dma_memzone_id;
252 : : const struct rte_memzone *mz = NULL;
253 : : char z_name[RTE_MEMZONE_NAMESIZE];
254 : :
255 [ # # ]: 0 : if (!mem)
256 : : return NULL;
257 : :
258 : 0 : snprintf(z_name, sizeof(z_name), "ice_dma_%" PRIu64,
259 : : rte_atomic_fetch_add_explicit(&ice_dma_memzone_id, 1, rte_memory_order_relaxed));
260 : 0 : mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, 0,
261 : : 0, RTE_PGSIZE_2M);
262 [ # # ]: 0 : if (!mz)
263 : : return NULL;
264 : :
265 : 0 : mem->size = size;
266 : 0 : mem->va = mz->addr;
267 : 0 : mem->pa = mz->iova;
268 : 0 : mem->zone = (const void *)mz;
269 : 0 : PMD_DRV_LOG(DEBUG, "memzone %s allocated with physical address: "
270 : : "%"PRIu64, mz->name, mem->pa);
271 : :
272 : 0 : return mem->va;
273 : : }
274 : :
275 : : static inline void
276 : 0 : ice_free_dma_mem(__rte_unused struct ice_hw *hw,
277 : : struct ice_dma_mem *mem)
278 : : {
279 : 0 : PMD_DRV_LOG(DEBUG, "memzone %s to be freed with physical address: "
280 : : "%"PRIu64, ((const struct rte_memzone *)mem->zone)->name,
281 : : mem->pa);
282 : 0 : rte_memzone_free((const struct rte_memzone *)mem->zone);
283 : 0 : mem->zone = NULL;
284 : 0 : mem->va = NULL;
285 : 0 : mem->pa = (u64)0;
286 : 0 : }
287 : :
288 : : static inline u8
289 : : ice_hweight8(u32 num)
290 : : {
291 : : u8 bits = 0;
292 : : u32 i;
293 : :
294 [ # # # # ]: 0 : for (i = 0; i < 8; i++) {
295 : 0 : bits += (u8)(num & 0x1);
296 : 0 : num >>= 1;
297 : : }
298 : :
299 : : return bits;
300 : : }
301 : :
302 : : static inline u8
303 : : ice_hweight32(u32 num)
304 : : {
305 : : u8 bits = 0;
306 : : u32 i;
307 : :
308 [ # # ]: 0 : for (i = 0; i < 32; i++) {
309 : 0 : bits += (u8)(num & 0x1);
310 : 0 : num >>= 1;
311 : : }
312 : :
313 : : return bits;
314 : : }
315 : :
316 : : #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
317 : : #define DELAY(x) rte_delay_us(x)
318 : : #define ice_usec_delay(x, y) rte_delay_us(x)
319 : : #define ice_msec_delay(x, y) rte_delay_us(1000 * (x))
320 : : #define udelay(x) DELAY(x)
321 : : #define msleep(x) DELAY(1000 * (x))
322 : : #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
323 : :
324 : : struct ice_list_entry {
325 : : LIST_ENTRY(ice_list_entry) next;
326 : : };
327 : :
328 : : LIST_HEAD(ice_list_head, ice_list_entry);
329 : :
330 : : #define LIST_ENTRY_TYPE ice_list_entry
331 : : #define LIST_HEAD_TYPE ice_list_head
332 : : #define INIT_LIST_HEAD(list_head) LIST_INIT(list_head)
333 : : #define LIST_DEL(entry) LIST_REMOVE(entry, next)
334 : : /* LIST_EMPTY(list_head)) the same in sys/queue.h */
335 : :
336 : : /*Note parameters are swapped*/
337 : : #define LIST_FIRST_ENTRY(head, type, field) (type *)((head)->lh_first)
338 : : #define LIST_NEXT_ENTRY(entry, type, field) \
339 : : ((type *)(entry)->field.next.le_next)
340 : : #define LIST_ADD(entry, list_head) LIST_INSERT_HEAD(list_head, entry, next)
341 : : #define LIST_ADD_AFTER(entry, list_entry) \
342 : : LIST_INSERT_AFTER(list_entry, entry, next)
343 : :
344 : : static inline void list_add_tail(struct ice_list_entry *entry,
345 : : struct ice_list_head *head)
346 : : {
347 : 0 : struct ice_list_entry *tail = head->lh_first;
348 : :
349 [ # # # # ]: 0 : if (tail == NULL) {
350 : 0 : LIST_INSERT_HEAD(head, entry, next);
351 : 0 : return;
352 : : }
353 [ # # # # ]: 0 : while (tail->next.le_next != NULL)
354 : : tail = tail->next.le_next;
355 : 0 : LIST_INSERT_AFTER(tail, entry, next);
356 : : }
357 : :
358 : : #define LIST_ADD_TAIL(entry, head) list_add_tail(entry, head)
359 : : #define LIST_FOR_EACH_ENTRY(pos, head, type, member) \
360 : : for ((pos) = (head)->lh_first ? \
361 : : container_of((head)->lh_first, struct type, member) : \
362 : : 0; \
363 : : (pos); \
364 : : (pos) = (pos)->member.next.le_next ? \
365 : : container_of((pos)->member.next.le_next, struct type, \
366 : : member) : \
367 : : 0)
368 : :
369 : : #define LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, head, type, member) \
370 : : for ((pos) = (head)->lh_first ? \
371 : : container_of((head)->lh_first, struct type, member) : \
372 : : 0, \
373 : : (tmp) = (pos) == 0 ? 0 : ((pos)->member.next.le_next ? \
374 : : container_of((pos)->member.next.le_next, struct type, \
375 : : member) : \
376 : : 0); \
377 : : (pos); \
378 : : (pos) = (tmp), \
379 : : (tmp) = (pos) == 0 ? 0 : ((tmp)->member.next.le_next ? \
380 : : container_of((pos)->member.next.le_next, struct type, \
381 : : member) : \
382 : : 0))
383 : :
384 : : #define LIST_REPLACE_INIT(list_head, head) do { \
385 : : (head)->lh_first = (list_head)->lh_first; \
386 : : INIT_LIST_HEAD(list_head); \
387 : : } while (0)
388 : :
389 : : #define HLIST_NODE_TYPE LIST_ENTRY_TYPE
390 : : #define HLIST_HEAD_TYPE LIST_HEAD_TYPE
391 : : #define INIT_HLIST_HEAD(list_head) INIT_LIST_HEAD(list_head)
392 : : #define HLIST_ADD_HEAD(entry, list_head) LIST_ADD(entry, list_head)
393 : : #define HLIST_EMPTY(list_head) LIST_EMPTY(list_head)
394 : : #define HLIST_DEL(entry) LIST_DEL(entry)
395 : : #define HLIST_FOR_EACH_ENTRY(pos, head, type, member) \
396 : : LIST_FOR_EACH_ENTRY(pos, head, type, member)
397 : :
398 : : #ifndef ICE_DBG_TRACE
399 : : #define ICE_DBG_TRACE BIT_ULL(0)
400 : : #endif
401 : :
402 : : #ifndef DIVIDE_AND_ROUND_UP
403 : : #define DIVIDE_AND_ROUND_UP(a, b) (((a) + (b) - 1) / (b))
404 : : #endif
405 : :
406 : : #ifndef ICE_INTEL_VENDOR_ID
407 : : #define ICE_INTEL_VENDOR_ID 0x8086
408 : : #endif
409 : :
410 : : #ifndef IS_UNICAST_ETHER_ADDR
411 : : #define IS_UNICAST_ETHER_ADDR(addr) \
412 : : ((bool)((((u8 *)(addr))[0] % ((u8)0x2)) == 0))
413 : : #endif
414 : :
415 : : #ifndef IS_MULTICAST_ETHER_ADDR
416 : : #define IS_MULTICAST_ETHER_ADDR(addr) \
417 : : ((bool)((((u8 *)(addr))[0] % ((u8)0x2)) == 1))
418 : : #endif
419 : :
420 : : #ifndef IS_BROADCAST_ETHER_ADDR
421 : : /* Check whether an address is broadcast. */
422 : : #define IS_BROADCAST_ETHER_ADDR(addr) \
423 : : ((bool)((((u16 *)(addr))[0] == ((u16)0xffff))))
424 : : #endif
425 : :
426 : : #ifndef IS_ZERO_ETHER_ADDR
427 : : #define IS_ZERO_ETHER_ADDR(addr) \
428 : : (((bool)((((u16 *)(addr))[0] == ((u16)0x0)))) && \
429 : : ((bool)((((u16 *)(addr))[1] == ((u16)0x0)))) && \
430 : : ((bool)((((u16 *)(addr))[2] == ((u16)0x0)))))
431 : : #endif
432 : :
433 : : #endif /* _ICE_OSDEP_H_ */
|