net/ice/base: allow GTP-U filter using only inner protocols
[dpdk.git] / drivers / net / ice / base / ice_osdep.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2021 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_random.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 #ifdef RTE_EXEC_ENV_WINDOWS
78 #define ice_access _access
79 #else
80 #define ice_access access
81 #endif
82
83 #define FIELD_SIZEOF(t, f) RTE_SIZEOF_FIELD(t, f)
84 #define ARRAY_SIZE(arr) RTE_DIM(arr)
85
86 #define CPU_TO_LE16(o) rte_cpu_to_le_16(o)
87 #define CPU_TO_LE32(s) rte_cpu_to_le_32(s)
88 #define CPU_TO_LE64(h) rte_cpu_to_le_64(h)
89 #define LE16_TO_CPU(a) rte_le_to_cpu_16(a)
90 #define LE32_TO_CPU(c) rte_le_to_cpu_32(c)
91 #define LE64_TO_CPU(k) rte_le_to_cpu_64(k)
92
93 #define CPU_TO_BE16(o) rte_cpu_to_be_16(o)
94 #define CPU_TO_BE32(o) rte_cpu_to_be_32(o)
95 #define CPU_TO_BE64(o) rte_cpu_to_be_64(o)
96 #define BE16_TO_CPU(o) rte_be_to_cpu_16(o)
97
98 #define NTOHS(a) rte_be_to_cpu_16(a)
99 #define NTOHL(a) rte_be_to_cpu_32(a)
100 #define HTONS(a) rte_cpu_to_be_16(a)
101 #define HTONL(a) rte_cpu_to_be_32(a)
102
103 static __rte_always_inline uint32_t
104 readl(volatile void *addr)
105 {
106         return rte_le_to_cpu_32(rte_read32(addr));
107 }
108
109 static __rte_always_inline void
110 writel(uint32_t value, volatile void *addr)
111 {
112         rte_write32(rte_cpu_to_le_32(value), addr);
113 }
114
115 static __rte_always_inline void
116 writel_relaxed(uint32_t value, volatile void *addr)
117 {
118         rte_write32_relaxed(rte_cpu_to_le_32(value), addr);
119 }
120
121 static __rte_always_inline uint64_t
122 readq(volatile void *addr)
123 {
124         return rte_le_to_cpu_64(rte_read64(addr));
125 }
126
127 static __rte_always_inline void
128 writeq(uint64_t value, volatile void *addr)
129 {
130         rte_write64(rte_cpu_to_le_64(value), addr);
131 }
132
133 #define wr32(a, reg, value) writel((value), (a)->hw_addr + (reg))
134 #define rd32(a, reg)        readl((a)->hw_addr + (reg))
135 #define wr64(a, reg, value) writeq((value), (a)->hw_addr + (reg))
136 #define rd64(a, reg)        readq((a)->hw_addr + (reg))
137
138 #endif /* __INTEL_NET_BASE_OSDEP__ */
139
140 #ifndef __always_unused
141 #define __always_unused  __rte_unused
142 #endif
143 #ifndef __maybe_unused
144 #define __maybe_unused  __rte_unused
145 #endif
146 #ifndef __packed
147 #define __packed  __rte_packed
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                 PMD_DRV_LOG_RAW(DEBUG, "ice %02x.%x " s,        \
160                         (h)->bus.device, (h)->bus.func,         \
161                                         ##__VA_ARGS__);         \
162 } while (0)
163
164 #define ice_info(hw, fmt, args...) ice_debug(hw, ICE_DBG_ALL, fmt, ##args)
165 #define ice_warn(hw, fmt, args...) ice_debug(hw, ICE_DBG_ALL, fmt, ##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 ice_dma_mem {
198         void *va;
199         u64 pa;
200         u32 size;
201         const void *zone;
202 } __rte_packed;
203
204 struct ice_virt_mem {
205         void *va;
206         u32 size;
207 } __rte_packed;
208
209 #define ice_malloc(h, s)    rte_zmalloc(NULL, s, 0)
210 #define ice_calloc(h, c, s) rte_zmalloc(NULL, (c) * (s), 0)
211 #define ice_free(h, m)         rte_free(m)
212
213 #define ice_memset(a, b, c, d) memset((a), (b), (c))
214 #define ice_memcpy(a, b, c, d) rte_memcpy((a), (b), (c))
215
216 /* SW spinlock */
217 struct ice_lock {
218         rte_spinlock_t spinlock;
219 };
220
221 static inline void
222 ice_init_lock(struct ice_lock *sp)
223 {
224         rte_spinlock_init(&sp->spinlock);
225 }
226
227 static inline void
228 ice_acquire_lock(struct ice_lock *sp)
229 {
230         rte_spinlock_lock(&sp->spinlock);
231 }
232
233 static inline void
234 ice_release_lock(struct ice_lock *sp)
235 {
236         rte_spinlock_unlock(&sp->spinlock);
237 }
238
239 static inline void
240 ice_destroy_lock(__rte_unused struct ice_lock *sp)
241 {
242 }
243
244 struct ice_hw;
245
246 static __rte_always_inline void *
247 ice_memdup(__rte_unused struct ice_hw *hw, const void *src, size_t size,
248            __rte_unused enum ice_memcpy_type dir)
249 {
250         void *p;
251
252         p = ice_malloc(hw, size);
253         if (p)
254                 rte_memcpy(p, src, size);
255
256         return p;
257 }
258
259 static inline void *
260 ice_alloc_dma_mem(__rte_unused struct ice_hw *hw,
261                   struct ice_dma_mem *mem, u64 size)
262 {
263         const struct rte_memzone *mz = NULL;
264         char z_name[RTE_MEMZONE_NAMESIZE];
265
266         if (!mem)
267                 return NULL;
268
269         snprintf(z_name, sizeof(z_name), "ice_dma_%"PRIu64, rte_rand());
270         mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, 0,
271                                          0, RTE_PGSIZE_2M);
272         if (!mz)
273                 return NULL;
274
275         mem->size = size;
276         mem->va = mz->addr;
277         mem->pa = mz->iova;
278         mem->zone = (const void *)mz;
279         PMD_DRV_LOG(DEBUG, "memzone %s allocated with physical address: "
280                     "%"PRIu64, mz->name, mem->pa);
281
282         return mem->va;
283 }
284
285 static inline void
286 ice_free_dma_mem(__rte_unused struct ice_hw *hw,
287                  struct ice_dma_mem *mem)
288 {
289         PMD_DRV_LOG(DEBUG, "memzone %s to be freed with physical address: "
290                     "%"PRIu64, ((const struct rte_memzone *)mem->zone)->name,
291                     mem->pa);
292         rte_memzone_free((const struct rte_memzone *)mem->zone);
293         mem->zone = NULL;
294         mem->va = NULL;
295         mem->pa = (u64)0;
296 }
297
298 static inline u8
299 ice_hweight8(u32 num)
300 {
301         u8 bits = 0;
302         u32 i;
303
304         for (i = 0; i < 8; i++) {
305                 bits += (u8)(num & 0x1);
306                 num >>= 1;
307         }
308
309         return bits;
310 }
311
312 static inline u8
313 ice_hweight32(u32 num)
314 {
315         u8 bits = 0;
316         u32 i;
317
318         for (i = 0; i < 32; i++) {
319                 bits += (u8)(num & 0x1);
320                 num >>= 1;
321         }
322
323         return bits;
324 }
325
326 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
327 #define DELAY(x) rte_delay_us(x)
328 #define ice_usec_delay(x, y) rte_delay_us(x)
329 #define ice_msec_delay(x, y) rte_delay_us(1000 * (x))
330 #define udelay(x) DELAY(x)
331 #define msleep(x) DELAY(1000 * (x))
332 #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
333
334 struct ice_list_entry {
335         LIST_ENTRY(ice_list_entry) next;
336 };
337
338 LIST_HEAD(ice_list_head, ice_list_entry);
339
340 #define LIST_ENTRY_TYPE    ice_list_entry
341 #define LIST_HEAD_TYPE     ice_list_head
342 #define INIT_LIST_HEAD(list_head)  LIST_INIT(list_head)
343 #define LIST_DEL(entry)            LIST_REMOVE(entry, next)
344 /* LIST_EMPTY(list_head)) the same in sys/queue.h */
345
346 /*Note parameters are swapped*/
347 #define LIST_FIRST_ENTRY(head, type, field) (type *)((head)->lh_first)
348 #define LIST_NEXT_ENTRY(entry, type, field) \
349         ((type *)(entry)->field.next.le_next)
350 #define LIST_ADD(entry, list_head)    LIST_INSERT_HEAD(list_head, entry, next)
351 #define LIST_ADD_AFTER(entry, list_entry) \
352         LIST_INSERT_AFTER(list_entry, entry, next)
353
354 static inline void list_add_tail(struct ice_list_entry *entry,
355                                  struct ice_list_head *head)
356 {
357         struct ice_list_entry *tail = head->lh_first;
358
359         if (tail == NULL) {
360                 LIST_INSERT_HEAD(head, entry, next);
361                 return;
362         }
363         while (tail->next.le_next != NULL)
364                 tail = tail->next.le_next;
365         LIST_INSERT_AFTER(tail, entry, next);
366 }
367
368 #define LIST_ADD_TAIL(entry, head) list_add_tail(entry, head)
369 #define LIST_FOR_EACH_ENTRY(pos, head, type, member)                           \
370         for ((pos) = (head)->lh_first ?                                        \
371                      container_of((head)->lh_first, struct type, member) :     \
372                      0;                                                        \
373              (pos);                                                            \
374              (pos) = (pos)->member.next.le_next ?                              \
375                      container_of((pos)->member.next.le_next, struct type,     \
376                                   member) :                                    \
377                      0)
378
379 #define LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, head, type, member)                 \
380         for ((pos) = (head)->lh_first ?                                        \
381                      container_of((head)->lh_first, struct type, member) :     \
382                      0,                                                        \
383                      (tmp) = (pos) == 0 ? 0 : ((pos)->member.next.le_next ?    \
384                      container_of((pos)->member.next.le_next, struct type,     \
385                                   member) :                                    \
386                      0);                                                       \
387              (pos);                                                            \
388              (pos) = (tmp),                                                    \
389              (tmp) = (pos) == 0 ? 0 : ((tmp)->member.next.le_next ?            \
390                      container_of((pos)->member.next.le_next, struct type,     \
391                                   member) :                                    \
392                      0))
393
394 #define LIST_REPLACE_INIT(list_head, head) do {                         \
395         (head)->lh_first = (list_head)->lh_first;                       \
396         INIT_LIST_HEAD(list_head);                                      \
397 } while (0)
398
399 #define HLIST_NODE_TYPE         LIST_ENTRY_TYPE
400 #define HLIST_HEAD_TYPE         LIST_HEAD_TYPE
401 #define INIT_HLIST_HEAD(list_head)             INIT_LIST_HEAD(list_head)
402 #define HLIST_ADD_HEAD(entry, list_head)       LIST_ADD(entry, list_head)
403 #define HLIST_EMPTY(list_head)                 LIST_EMPTY(list_head)
404 #define HLIST_DEL(entry)                       LIST_DEL(entry)
405 #define HLIST_FOR_EACH_ENTRY(pos, head, type, member) \
406         LIST_FOR_EACH_ENTRY(pos, head, type, member)
407
408 #ifndef ICE_DBG_TRACE
409 #define ICE_DBG_TRACE           BIT_ULL(0)
410 #endif
411
412 #ifndef DIVIDE_AND_ROUND_UP
413 #define DIVIDE_AND_ROUND_UP(a, b) (((a) + (b) - 1) / (b))
414 #endif
415
416 #ifndef ICE_INTEL_VENDOR_ID
417 #define ICE_INTEL_VENDOR_ID             0x8086
418 #endif
419
420 #ifndef IS_UNICAST_ETHER_ADDR
421 #define IS_UNICAST_ETHER_ADDR(addr) \
422         ((bool)((((u8 *)(addr))[0] % ((u8)0x2)) == 0))
423 #endif
424
425 #ifndef IS_MULTICAST_ETHER_ADDR
426 #define IS_MULTICAST_ETHER_ADDR(addr) \
427         ((bool)((((u8 *)(addr))[0] % ((u8)0x2)) == 1))
428 #endif
429
430 #ifndef IS_BROADCAST_ETHER_ADDR
431 /* Check whether an address is broadcast. */
432 #define IS_BROADCAST_ETHER_ADDR(addr)   \
433         ((bool)((((u16 *)(addr))[0] == ((u16)0xffff))))
434 #endif
435
436 #ifndef IS_ZERO_ETHER_ADDR
437 #define IS_ZERO_ETHER_ADDR(addr) \
438         (((bool)((((u16 *)(addr))[0] == ((u16)0x0)))) && \
439          ((bool)((((u16 *)(addr))[1] == ((u16)0x0)))) && \
440          ((bool)((((u16 *)(addr))[2] == ((u16)0x0)))))
441 #endif
442
443 #endif /* _ICE_OSDEP_H_ */