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