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