252c8f4e783b2d53edb27b5dd57dcd1193b1da96
[dpdk.git] / drivers / net / ice / base / ice_osdep.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 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_logs.h"
28
29 #define INLINE inline
30 #define STATIC static
31
32 typedef uint8_t         u8;
33 typedef int8_t          s8;
34 typedef uint16_t        u16;
35 typedef int16_t         s16;
36 typedef uint32_t        u32;
37 typedef int32_t         s32;
38 typedef uint64_t        u64;
39 typedef uint64_t        s64;
40
41 #define __iomem
42 #define hw_dbg(hw, S, A...) do {} while (0)
43 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
44 #define lower_32_bits(n) ((u32)(n))
45 #define low_16_bits(x)   ((x) & 0xFFFF)
46 #define high_16_bits(x)  (((x) & 0xFFFF0000) >> 16)
47
48 #ifndef ETH_ADDR_LEN
49 #define ETH_ADDR_LEN                  6
50 #endif
51
52 #ifndef __le16
53 #define __le16          uint16_t
54 #endif
55 #ifndef __le32
56 #define __le32          uint32_t
57 #endif
58 #ifndef __le64
59 #define __le64          uint64_t
60 #endif
61 #ifndef __be16
62 #define __be16          uint16_t
63 #endif
64 #ifndef __be32
65 #define __be32          uint32_t
66 #endif
67 #ifndef __be64
68 #define __be64          uint64_t
69 #endif
70
71 #ifndef __always_unused
72 #define __always_unused  __attribute__((unused))
73 #endif
74 #ifndef __maybe_unused
75 #define __maybe_unused  __attribute__((unused))
76 #endif
77 #ifndef __packed
78 #define __packed  __attribute__((packed))
79 #endif
80
81 #ifndef BIT_ULL
82 #define BIT_ULL(a) (1ULL << (a))
83 #endif
84
85 #define FALSE           0
86 #define TRUE            1
87 #define false           0
88 #define true            1
89
90 #define min(a, b) RTE_MIN(a, b)
91 #define max(a, b) RTE_MAX(a, b)
92
93 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
94 #define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f))
95 #define MAKEMASK(m, s) ((m) << (s))
96
97 #define DEBUGOUT(S, A...) PMD_DRV_LOG_RAW(DEBUG, S, ##A)
98 #define DEBUGFUNC(F) PMD_DRV_LOG_RAW(DEBUG, F)
99
100 #define ice_debug(h, m, s, ...)                                 \
101 do {                                                            \
102         if (((m) & (h)->debug_mask))                            \
103                 PMD_DRV_LOG_RAW(DEBUG, "ice %02x.%x " s,        \
104                         (h)->bus.device, (h)->bus.func,         \
105                                         ##__VA_ARGS__);         \
106 } while (0)
107
108 #define ice_info(hw, fmt, args...) ice_debug(hw, ICE_DBG_ALL, fmt, ##args)
109 #define ice_warn(hw, fmt, args...) ice_debug(hw, ICE_DBG_ALL, fmt, ##args)
110 #define ice_debug_array(hw, type, rowsize, groupsize, buf, len)         \
111 do {                                                                    \
112         struct ice_hw *hw_l = hw;                                       \
113                 u16 len_l = len;                                        \
114                 u8 *buf_l = buf;                                        \
115                 int i;                                                  \
116                 for (i = 0; i < len_l; i += 8)                          \
117                         ice_debug(hw_l, type,                           \
118                                   "0x%04X  0x%016"PRIx64"\n",           \
119                                   i, *((u64 *)((buf_l) + i)));          \
120 } while (0)
121 #define ice_snprintf snprintf
122 #ifndef SNPRINTF
123 #define SNPRINTF ice_snprintf
124 #endif
125
126 #define ICE_PCI_REG(reg)     rte_read32(reg)
127 #define ICE_PCI_REG_ADDR(a, reg) \
128         ((volatile uint32_t *)((char *)(a)->hw_addr + (reg)))
129 static inline uint32_t ice_read_addr(volatile void *addr)
130 {
131         return rte_le_to_cpu_32(ICE_PCI_REG(addr));
132 }
133
134 #define ICE_PCI_REG_WRITE(reg, value) \
135         rte_write32((rte_cpu_to_le_32(value)), reg)
136
137 #define ice_flush(a)   ICE_READ_REG((a), GLGEN_STAT)
138 #define icevf_flush(a) ICE_READ_REG((a), VFGEN_RSTAT)
139 #define ICE_READ_REG(hw, reg) ice_read_addr(ICE_PCI_REG_ADDR((hw), (reg)))
140 #define ICE_WRITE_REG(hw, reg, value) \
141         ICE_PCI_REG_WRITE(ICE_PCI_REG_ADDR((hw), (reg)), (value))
142
143 #define rd32(a, reg) ice_read_addr(ICE_PCI_REG_ADDR((a), (reg)))
144 #define wr32(a, reg, value) \
145         ICE_PCI_REG_WRITE(ICE_PCI_REG_ADDR((a), (reg)), (value))
146 #define flush(a) ice_read_addr(ICE_PCI_REG_ADDR((a), (GLGEN_STAT)))
147 #define div64_long(n, d) ((n) / (d))
148
149 #define BITS_PER_BYTE       8
150 typedef u32 ice_bitmap_t;
151 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
152 #define BITS_TO_CHUNKS(nr)   DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(ice_bitmap_t))
153 #define ice_declare_bitmap(name, bits) \
154         ice_bitmap_t name[BITS_TO_CHUNKS(bits)]
155
156 #define BITS_CHUNK_MASK(nr)     (((ice_bitmap_t)~0) >>                  \
157                 ((BITS_PER_BYTE * sizeof(ice_bitmap_t)) -               \
158                 (((nr) - 1) % (BITS_PER_BYTE * sizeof(ice_bitmap_t))    \
159                  + 1)))
160 #define BITS_PER_CHUNK          (BITS_PER_BYTE * sizeof(ice_bitmap_t))
161 #define BIT_CHUNK(nr)           ((nr) / BITS_PER_CHUNK)
162 #define BIT_IN_CHUNK(nr)        BIT((nr) % BITS_PER_CHUNK)
163
164 static inline bool ice_is_bit_set(const ice_bitmap_t *bitmap, u16 nr)
165 {
166         return !!(bitmap[BIT_CHUNK(nr)] & BIT_IN_CHUNK(nr));
167 }
168
169 #define ice_and_bitmap(d, b1, b2, sz) \
170         ice_intersect_bitmaps((u8 *)d, (u8 *)b1, (const u8 *)b2, (u16)sz)
171 static inline int
172 ice_intersect_bitmaps(u8 *dst, const u8 *bmp1, const u8 *bmp2, u16 sz)
173 {
174         u32 res = 0;
175         int cnt;
176         u16 i;
177
178         /* Utilize 32-bit operations */
179         cnt = (sz % BITS_PER_BYTE) ?
180                 (sz / BITS_PER_BYTE) + 1 : sz / BITS_PER_BYTE;
181         for (i = 0; i < cnt / 4; i++) {
182                 ((u32 *)dst)[i] = ((const u32 *)bmp1)[i] &
183                 ((const u32 *)bmp2)[i];
184                 res |= ((u32 *)dst)[i];
185         }
186
187         for (i *= 4; i < cnt; i++) {
188                 if ((sz % 8 == 0) || (i + 1 < cnt)) {
189                         dst[i] = bmp1[i] & bmp2[i];
190                 } else {
191                         /* Remaining bits that do not occupy the whole byte */
192                         u8 mask = ~0u >> (8 - (sz % 8));
193
194                         dst[i] = bmp1[i] & bmp2[i] & mask;
195                 }
196
197                 res |= dst[i];
198         }
199
200         return res != 0;
201 }
202
203 static inline int ice_find_first_bit(ice_bitmap_t *name, u16 size)
204 {
205         u16 i;
206
207         for (i = 0; i < BITS_PER_BYTE * (size / BITS_PER_BYTE); i++)
208                 if (ice_is_bit_set(name, i))
209                         return i;
210         return size;
211 }
212
213 static inline int ice_find_next_bit(ice_bitmap_t *name, u16 size, u16 bits)
214 {
215         u16 i;
216
217         for (i = bits; i < BITS_PER_BYTE * (size / BITS_PER_BYTE); i++)
218                 if (ice_is_bit_set(name, i))
219                         return i;
220         return bits;
221 }
222
223 #define for_each_set_bit(bit, addr, size)                               \
224         for ((bit) = ice_find_first_bit((addr), (size));                \
225         (bit) < (size);                                                 \
226         (bit) = ice_find_next_bit((addr), (size), (bit) + 1))
227
228 static inline bool ice_is_any_bit_set(ice_bitmap_t *bitmap, u32 bits)
229 {
230         u32 max_index = BITS_TO_CHUNKS(bits);
231         u32 i;
232
233         for (i = 0; i < max_index; i++) {
234                 if (bitmap[i])
235                         return true;
236         }
237         return false;
238 }
239
240 /* memory allocation tracking */
241 struct ice_dma_mem {
242         void *va;
243         u64 pa;
244         u32 size;
245         const void *zone;
246 } __attribute__((packed));
247
248 struct ice_virt_mem {
249         void *va;
250         u32 size;
251 } __attribute__((packed));
252
253 #define ice_malloc(h, s)    rte_zmalloc(NULL, s, 0)
254 #define ice_calloc(h, c, s) rte_zmalloc(NULL, (c) * (s), 0)
255 #define ice_free(h, m)         rte_free(m)
256
257 #define ice_memset(a, b, c, d) memset((a), (b), (c))
258 #define ice_memcpy(a, b, c, d) rte_memcpy((a), (b), (c))
259 #define ice_memdup(a, b, c, d) rte_memcpy(ice_malloc(a, c), b, c)
260
261 #define CPU_TO_BE16(o) rte_cpu_to_be_16(o)
262 #define CPU_TO_BE32(o) rte_cpu_to_be_32(o)
263 #define CPU_TO_BE64(o) rte_cpu_to_be_64(o)
264 #define CPU_TO_LE16(o) rte_cpu_to_le_16(o)
265 #define CPU_TO_LE32(s) rte_cpu_to_le_32(s)
266 #define CPU_TO_LE64(h) rte_cpu_to_le_64(h)
267 #define LE16_TO_CPU(a) rte_le_to_cpu_16(a)
268 #define LE32_TO_CPU(c) rte_le_to_cpu_32(c)
269 #define LE64_TO_CPU(k) rte_le_to_cpu_64(k)
270
271 #define NTOHS(a) rte_be_to_cpu_16(a)
272 #define NTOHL(a) rte_be_to_cpu_32(a)
273 #define HTONS(a) rte_cpu_to_be_16(a)
274 #define HTONL(a) rte_cpu_to_be_32(a)
275
276 static inline void
277 ice_set_bit(unsigned int nr, volatile ice_bitmap_t *addr)
278 {
279         __sync_fetch_and_or(addr, (1UL << nr));
280 }
281
282 static inline void
283 ice_clear_bit(unsigned int nr, volatile ice_bitmap_t *addr)
284 {
285         __sync_fetch_and_and(addr, (0UL << nr));
286 }
287
288 static inline void
289 ice_zero_bitmap(ice_bitmap_t *bmp, u16 size)
290 {
291         unsigned long mask;
292         u16 i;
293
294         for (i = 0; i < BITS_TO_CHUNKS(size) - 1; i++)
295                 bmp[i] = 0;
296         mask = BITS_CHUNK_MASK(size);
297         bmp[i] &= ~mask;
298 }
299
300 static inline void
301 ice_or_bitmap(ice_bitmap_t *dst, const ice_bitmap_t *bmp1,
302               const ice_bitmap_t *bmp2, u16 size)
303 {
304         unsigned long mask;
305         u16 i;
306
307         /* Handle all but last chunk*/
308         for (i = 0; i < BITS_TO_CHUNKS(size) - 1; i++)
309                 dst[i] = bmp1[i] | bmp2[i];
310
311         /* We want to only OR bits within the size. Furthermore, we also do
312          * not want to modify destination bits which are beyond the specified
313          * size. Use a bitmask to ensure that we only modify the bits that are
314          * within the specified size.
315          */
316         mask = BITS_CHUNK_MASK(size);
317         dst[i] &= ~mask;
318         dst[i] |= (bmp1[i] | bmp2[i]) & mask;
319 }
320
321 static inline void ice_cp_bitmap(ice_bitmap_t *dst, ice_bitmap_t *src, u16 size)
322 {
323         ice_bitmap_t mask;
324         u16 i;
325
326         /* Handle all but last chunk*/
327         for (i = 0; i < BITS_TO_CHUNKS(size) - 1; i++)
328                 dst[i] = src[i];
329
330         /* We want to only copy bits within the size.*/
331         mask = BITS_CHUNK_MASK(size);
332         dst[i] &= ~mask;
333         dst[i] |= src[i] & mask;
334 }
335
336 static inline bool
337 ice_cmp_bitmap(ice_bitmap_t *bmp1, ice_bitmap_t *bmp2, u16 size)
338 {
339         ice_bitmap_t mask;
340         u16 i;
341
342         /* Handle all but last chunk*/
343         for (i = 0; i < BITS_TO_CHUNKS(size) - 1; i++)
344                 if (bmp1[i] != bmp2[i])
345                         return false;
346
347         /* We want to only compare bits within the size.*/
348         mask = BITS_CHUNK_MASK(size);
349         if ((bmp1[i] & mask) != (bmp2[i] & mask))
350                 return false;
351
352         return true;
353 }
354
355 /* SW spinlock */
356 struct ice_lock {
357         rte_spinlock_t spinlock;
358 };
359
360 static inline void
361 ice_init_lock(struct ice_lock *sp)
362 {
363         rte_spinlock_init(&sp->spinlock);
364 }
365
366 static inline void
367 ice_acquire_lock(struct ice_lock *sp)
368 {
369         rte_spinlock_lock(&sp->spinlock);
370 }
371
372 static inline void
373 ice_release_lock(struct ice_lock *sp)
374 {
375         rte_spinlock_unlock(&sp->spinlock);
376 }
377
378 static inline void
379 ice_destroy_lock(__attribute__((unused)) struct ice_lock *sp)
380 {
381 }
382
383 struct ice_hw;
384
385 static inline void *
386 ice_alloc_dma_mem(__attribute__((unused)) struct ice_hw *hw,
387                   struct ice_dma_mem *mem, u64 size)
388 {
389         const struct rte_memzone *mz = NULL;
390         char z_name[RTE_MEMZONE_NAMESIZE];
391
392         if (!mem)
393                 return NULL;
394
395         snprintf(z_name, sizeof(z_name), "ice_dma_%"PRIu64, rte_rand());
396         mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, 0,
397                                          0, RTE_PGSIZE_2M);
398         if (!mz)
399                 return NULL;
400
401         mem->size = size;
402         mem->va = mz->addr;
403         mem->pa = mz->phys_addr;
404         mem->zone = (const void *)mz;
405         PMD_DRV_LOG(DEBUG, "memzone %s allocated with physical address: "
406                     "%"PRIu64, mz->name, mem->pa);
407
408         return mem->va;
409 }
410
411 static inline void
412 ice_free_dma_mem(__attribute__((unused)) struct ice_hw *hw,
413                  struct ice_dma_mem *mem)
414 {
415         PMD_DRV_LOG(DEBUG, "memzone %s to be freed with physical address: "
416                     "%"PRIu64, ((const struct rte_memzone *)mem->zone)->name,
417                     mem->pa);
418         rte_memzone_free((const struct rte_memzone *)mem->zone);
419         mem->zone = NULL;
420         mem->va = NULL;
421         mem->pa = (u64)0;
422 }
423
424 static inline u8
425 ice_hweight8(u32 num)
426 {
427         u8 bits = 0;
428         u32 i;
429
430         for (i = 0; i < 8; i++) {
431                 bits += (u8)(num & 0x1);
432                 num >>= 1;
433         }
434
435         return bits;
436 }
437
438 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
439 #define DELAY(x) rte_delay_us(x)
440 #define ice_usec_delay(x) rte_delay_us(x)
441 #define ice_msec_delay(x, y) rte_delay_us(1000 * (x))
442 #define udelay(x) DELAY(x)
443 #define msleep(x) DELAY(1000 * (x))
444 #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
445
446 struct ice_list_entry {
447         LIST_ENTRY(ice_list_entry) next;
448 };
449
450 LIST_HEAD(ice_list_head, ice_list_entry);
451
452 #define LIST_ENTRY_TYPE    ice_list_entry
453 #define LIST_HEAD_TYPE     ice_list_head
454 #define INIT_LIST_HEAD(list_head)  LIST_INIT(list_head)
455 #define LIST_DEL(entry)            LIST_REMOVE(entry, next)
456 /* LIST_EMPTY(list_head)) the same in sys/queue.h */
457
458 /*Note parameters are swapped*/
459 #define LIST_FIRST_ENTRY(head, type, field) (type *)((head)->lh_first)
460 #define LIST_NEXT_ENTRY(entry, type, field) \
461         ((type *)(entry)->field.next.le_next)
462 #define LIST_ADD(entry, list_head)    LIST_INSERT_HEAD(list_head, entry, next)
463 #define LIST_ADD_AFTER(entry, list_entry) \
464         LIST_INSERT_AFTER(list_entry, entry, next)
465 #define LIST_FOR_EACH_ENTRY(pos, head, type, member)                           \
466         for ((pos) = (head)->lh_first ?                                        \
467                      container_of((head)->lh_first, struct type, member) :     \
468                      0;                                                        \
469              (pos);                                                            \
470              (pos) = (pos)->member.next.le_next ?                              \
471                      container_of((pos)->member.next.le_next, struct type,     \
472                                   member) :                                    \
473                      0)
474
475 #define LIST_REPLACE_INIT(list_head, head) do {                         \
476         (head)->lh_first = (list_head)->lh_first;                       \
477         INIT_LIST_HEAD(list_head);                                      \
478 } while (0)
479
480 #define HLIST_NODE_TYPE         LIST_ENTRY_TYPE
481 #define HLIST_HEAD_TYPE         LIST_HEAD_TYPE
482 #define INIT_HLIST_HEAD(list_head)             INIT_LIST_HEAD(list_head)
483 #define HLIST_ADD_HEAD(entry, list_head)       LIST_ADD(entry, list_head)
484 #define HLIST_EMPTY(list_head)                 LIST_EMPTY(list_head)
485 #define HLIST_DEL(entry)                       LIST_DEL(entry)
486 #define HLIST_FOR_EACH_ENTRY(pos, head, type, member) \
487         LIST_FOR_EACH_ENTRY(pos, head, type, member)
488 #define LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, head, type, member) \
489         LIST_FOR_EACH_ENTRY(pos, head, type, member)
490
491 #ifndef ICE_DBG_TRACE
492 #define ICE_DBG_TRACE           BIT_ULL(0)
493 #endif
494
495 #ifndef DIVIDE_AND_ROUND_UP
496 #define DIVIDE_AND_ROUND_UP(a, b) (((a) + (b) - 1) / (b))
497 #endif
498
499 #ifndef ICE_INTEL_VENDOR_ID
500 #define ICE_INTEL_VENDOR_ID             0x8086
501 #endif
502
503 #ifndef IS_UNICAST_ETHER_ADDR
504 #define IS_UNICAST_ETHER_ADDR(addr) \
505         ((bool)((((u8 *)(addr))[0] % ((u8)0x2)) == 0))
506 #endif
507
508 #ifndef IS_MULTICAST_ETHER_ADDR
509 #define IS_MULTICAST_ETHER_ADDR(addr) \
510         ((bool)((((u8 *)(addr))[0] % ((u8)0x2)) == 1))
511 #endif
512
513 #ifndef IS_BROADCAST_ETHER_ADDR
514 /* Check whether an address is broadcast. */
515 #define IS_BROADCAST_ETHER_ADDR(addr)   \
516         ((bool)((((u16 *)(addr))[0] == ((u16)0xffff))))
517 #endif
518
519 #ifndef IS_ZERO_ETHER_ADDR
520 #define IS_ZERO_ETHER_ADDR(addr) \
521         (((bool)((((u16 *)(addr))[0] == ((u16)0x0)))) && \
522          ((bool)((((u16 *)(addr))[1] == ((u16)0x0)))) && \
523          ((bool)((((u16 *)(addr))[2] == ((u16)0x0)))))
524 #endif
525
526 #endif /* _ICE_OSDEP_H_ */