]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ice/ice_osdep.h
ice(4): Add Intel 100GbE Ethernet Driver to kernel
[FreeBSD/FreeBSD.git] / sys / dev / ice / ice_osdep.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2020, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*$FreeBSD$*/
32
33 /**
34  * @file ice_osdep.h
35  * @brief OS compatibility layer
36  *
37  * Contains various definitions and functions which are part of an OS
38  * compatibility layer for sharing code with other operating systems.
39  */
40 #ifndef _ICE_OSDEP_H_
41 #define _ICE_OSDEP_H_
42
43 #include <sys/endian.h>
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/proc.h>
48 #include <sys/systm.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/bus.h>
52 #include <machine/bus.h>
53 #include <sys/bus_dma.h>
54 #include <netinet/in.h>
55 #include <sys/counter.h>
56 #include <sys/sbuf.h>
57
58 #include "ice_alloc.h"
59
60 #define ICE_INTEL_VENDOR_ID 0x8086
61
62 #define ICE_STR_BUF_LEN 32
63
64 struct ice_hw;
65
66 device_t ice_hw_to_dev(struct ice_hw *hw);
67
68 /* configure hw->debug_mask to enable debug prints */
69 void ice_debug(struct ice_hw *hw, uint64_t mask, char *fmt, ...) __printflike(3, 4);
70 void ice_debug_array(struct ice_hw *hw, uint64_t mask, uint32_t rowsize,
71                      uint32_t groupsize, uint8_t *buf, size_t len);
72
73 #define ice_info(_hw, _fmt, args...) \
74         device_printf(ice_hw_to_dev(_hw), (_fmt), ##args)
75
76 #define ice_warn(_hw, _fmt, args...) \
77         device_printf(ice_hw_to_dev(_hw), (_fmt), ##args)
78
79 #define DIVIDE_AND_ROUND_UP howmany
80 #define ROUND_UP roundup
81
82 uint32_t rd32(struct ice_hw *hw, uint32_t reg);
83 uint64_t rd64(struct ice_hw *hw, uint32_t reg);
84 void wr32(struct ice_hw *hw, uint32_t reg, uint32_t val);
85 void wr64(struct ice_hw *hw, uint32_t reg, uint64_t val);
86
87 #define ice_flush(_hw) rd32((_hw), GLGEN_STAT)
88
89 MALLOC_DECLARE(M_ICE_OSDEP);
90
91 /**
92  * ice_calloc - Allocate an array of elementes
93  * @hw: the hardware private structure
94  * @count: number of elements to allocate
95  * @size: the size of each element
96  *
97  * Allocate memory for an array of items equal to size. Note that the OS
98  * compatibility layer assumes all allocation functions will provide zero'd
99  * memory.
100  */
101 static inline void *
102 ice_calloc(struct ice_hw __unused *hw, size_t count, size_t size)
103 {
104         return malloc(count * size, M_ICE_OSDEP, M_ZERO | M_NOWAIT);
105 }
106
107 /**
108  * ice_malloc - Allocate memory of a specified size
109  * @hw: the hardware private structure
110  * @size: the size to allocate
111  *
112  * Allocates memory of the specified size. Note that the OS compatibility
113  * layer assumes that all allocations will provide zero'd memory.
114  */
115 static inline void *
116 ice_malloc(struct ice_hw __unused *hw, size_t size)
117 {
118         return malloc(size, M_ICE_OSDEP, M_ZERO | M_NOWAIT);
119 }
120
121 /**
122  * ice_memdup - Allocate a copy of some other memory
123  * @hw: private hardware structure
124  * @src: the source to copy from
125  * @size: allocation size
126  * @dir: the direction of copying
127  *
128  * Allocate memory of the specified size, and copy bytes from the src to fill
129  * it. We don't need to zero this memory as we immediately initialize it by
130  * copying from the src pointer.
131  */
132 static inline void *
133 ice_memdup(struct ice_hw __unused *hw, const void *src, size_t size,
134            enum ice_memcpy_type __unused dir)
135 {
136         void *dst = malloc(size, M_ICE_OSDEP, M_NOWAIT);
137
138         if (dst != NULL)
139                 memcpy(dst, src, size);
140
141         return dst;
142 }
143
144 /**
145  * ice_free - Free previously allocated memory
146  * @hw: the hardware private structure
147  * @mem: pointer to the memory to free
148  *
149  * Free memory that was previously allocated by ice_calloc, ice_malloc, or
150  * ice_memdup.
151  */
152 static inline void
153 ice_free(struct ice_hw __unused *hw, void *mem)
154 {
155         free(mem, M_ICE_OSDEP);
156 }
157
158 /* These are macros in order to drop the unused direction enumeration constant */
159 #define ice_memset(addr, c, len, unused) memset((addr), (c), (len))
160 #define ice_memcpy(dst, src, len, unused) memcpy((dst), (src), (len))
161
162 void ice_usec_delay(uint32_t time, bool sleep);
163 void ice_msec_delay(uint32_t time, bool sleep);
164 void ice_msec_pause(uint32_t time);
165 void ice_msec_spin(uint32_t time);
166
167 #define UNREFERENCED_PARAMETER(_p) _p = _p
168 #define UNREFERENCED_1PARAMETER(_p) do {                        \
169         UNREFERENCED_PARAMETER(_p);                             \
170 } while (0)
171 #define UNREFERENCED_2PARAMETER(_p, _q) do {                    \
172         UNREFERENCED_PARAMETER(_p);                             \
173         UNREFERENCED_PARAMETER(_q);                             \
174 } while (0)
175 #define UNREFERENCED_3PARAMETER(_p, _q, _r) do {                \
176         UNREFERENCED_PARAMETER(_p);                             \
177         UNREFERENCED_PARAMETER(_q);                             \
178         UNREFERENCED_PARAMETER(_r);                             \
179 } while (0)
180 #define UNREFERENCED_4PARAMETER(_p, _q, _r, _s) do {            \
181         UNREFERENCED_PARAMETER(_p);                             \
182         UNREFERENCED_PARAMETER(_q);                             \
183         UNREFERENCED_PARAMETER(_r);                             \
184         UNREFERENCED_PARAMETER(_s);                             \
185 } while (0)
186 #define UNREFERENCED_5PARAMETER(_p, _q, _r, _s, _t) do {        \
187         UNREFERENCED_PARAMETER(_p);                             \
188         UNREFERENCED_PARAMETER(_q);                             \
189         UNREFERENCED_PARAMETER(_r);                             \
190         UNREFERENCED_PARAMETER(_s);                             \
191         UNREFERENCED_PARAMETER(_t);                             \
192 } while (0)
193
194 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
195 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
196 #define MAKEMASK(_m, _s) ((_m) << (_s))
197
198 #define LIST_HEAD_TYPE ice_list_head
199 #define LIST_ENTRY_TYPE ice_list_node
200
201 /**
202  * @struct ice_list_node
203  * @brief simplified linked list node API
204  *
205  * Represents a node in a linked list, which can be embedded into a structure
206  * to allow that structure to be inserted into a linked list. Access to the
207  * contained structure is done via __containerof
208  */
209 struct ice_list_node {
210         LIST_ENTRY(ice_list_node) entries;
211 };
212
213 /**
214  * @struct ice_list_head
215  * @brief simplified linked list head API
216  *
217  * Represents the head of a linked list. The linked list should consist of
218  * a series of ice_list_node structures embedded into another structure
219  * accessed using __containerof. This way, the ice_list_head doesn't need to
220  * know the type of the structure it contains.
221  */
222 LIST_HEAD(ice_list_head, ice_list_node);
223
224 #define INIT_LIST_HEAD LIST_INIT
225 /* LIST_EMPTY doesn't need to be changed */
226 #define LIST_ADD(entry, head) LIST_INSERT_HEAD(head, entry, entries)
227 #define LIST_ADD_AFTER(entry, elem) LIST_INSERT_AFTER(elem, entry, entries)
228 #define LIST_DEL(entry) LIST_REMOVE(entry, entries)
229 #define _osdep_LIST_ENTRY(ptr, type, member) \
230         __containerof(ptr, type, member)
231 #define LIST_FIRST_ENTRY(head, type, member) \
232         _osdep_LIST_ENTRY(LIST_FIRST(head), type, member)
233 #define LIST_NEXT_ENTRY(ptr, unused, member) \
234         _osdep_LIST_ENTRY(LIST_NEXT(&(ptr->member), entries), __typeof(*ptr), member)
235 #define LIST_REPLACE_INIT(old_head, new_head) do {                      \
236         __typeof(new_head) _new_head = (new_head);                      \
237         LIST_INIT(_new_head);                                           \
238         LIST_SWAP(old_head, _new_head, ice_list_node, entries);         \
239 } while (0)
240
241 #define LIST_ENTRY_SAFE(_ptr, _type, _member) \
242 ({ __typeof(_ptr) ____ptr = (_ptr); \
243    ____ptr ? _osdep_LIST_ENTRY(____ptr, _type, _member) : NULL; \
244 })
245
246 /**
247  * ice_get_list_tail - Return the pointer to the last node in the list
248  * @head: the pointer to the head of the list
249  *
250  * A helper function for implementing LIST_ADD_TAIL and LIST_LAST_ENTRY.
251  * Returns the pointer to the last node in the list, or NULL of the list is
252  * empty.
253  *
254  * Note: due to the list implementation this is O(N), where N is the size of
255  * the list. An O(1) implementation requires replacing the underlying list
256  * datastructure with one that has a tail pointer. This is problematic,
257  * because using a simple TAILQ would require that the addition and deletion
258  * be given the head of the list.
259  */
260 static inline struct ice_list_node *
261 ice_get_list_tail(struct ice_list_head *head)
262 {
263         struct ice_list_node *node = LIST_FIRST(head);
264
265         if (node == NULL)
266                 return NULL;
267         while (LIST_NEXT(node, entries) != NULL)
268                 node = LIST_NEXT(node, entries);
269
270         return node;
271 }
272
273 /* TODO: This is O(N). An O(1) implementation would require a different
274  * underlying list structure, such as a circularly linked list. */
275 #define LIST_ADD_TAIL(entry, head) do {                                 \
276         struct ice_list_node *node = ice_get_list_tail(head);           \
277                                                                         \
278         if (node == NULL) {                                             \
279                 LIST_ADD(entry, head);                                  \
280         } else {                                                        \
281                 LIST_INSERT_AFTER(node, entry, entries);                \
282         }                                                               \
283 } while (0)
284
285 #define LIST_LAST_ENTRY(head, type, member) \
286         LIST_ENTRY_SAFE(ice_get_list_tail(head), type, member)
287
288 #define LIST_FIRST_ENTRY_SAFE(head, type, member) \
289         LIST_ENTRY_SAFE(LIST_FIRST(head), type, member)
290
291 #define LIST_NEXT_ENTRY_SAFE(ptr, member) \
292         LIST_ENTRY_SAFE(LIST_NEXT(&(ptr->member), entries), __typeof(*ptr), member)
293
294 #define LIST_FOR_EACH_ENTRY(pos, head, unused, member) \
295         for (pos = LIST_FIRST_ENTRY_SAFE(head, __typeof(*pos), member);         \
296             pos;                                                                \
297             pos = LIST_NEXT_ENTRY_SAFE(pos, member))
298
299 #define LIST_FOR_EACH_ENTRY_SAFE(pos, n, head, unused, member) \
300         for (pos = LIST_FIRST_ENTRY_SAFE(head, __typeof(*pos), member);         \
301              pos && ({ n = LIST_NEXT_ENTRY_SAFE(pos, member); 1; });            \
302              pos = n)
303
304 #define STATIC static
305
306 #define NTOHS ntohs
307 #define NTOHL ntohl
308 #define HTONS htons
309 #define HTONL htonl
310 #define LE16_TO_CPU le16toh
311 #define LE32_TO_CPU le32toh
312 #define LE64_TO_CPU le64toh
313 #define CPU_TO_LE16 htole16
314 #define CPU_TO_LE32 htole32
315 #define CPU_TO_LE64 htole64
316 #define CPU_TO_BE16 htobe16
317 #define CPU_TO_BE32 htobe32
318
319 #define SNPRINTF snprintf
320
321 /**
322  * @typedef u8
323  * @brief compatibility typedef for uint8_t
324  */
325 typedef uint8_t  u8;
326
327 /**
328  * @typedef u16
329  * @brief compatibility typedef for uint16_t
330  */
331 typedef uint16_t u16;
332
333 /**
334  * @typedef u32
335  * @brief compatibility typedef for uint32_t
336  */
337 typedef uint32_t u32;
338
339 /**
340  * @typedef u64
341  * @brief compatibility typedef for uint64_t
342  */
343 typedef uint64_t u64;
344
345 /**
346  * @typedef s8
347  * @brief compatibility typedef for int8_t
348  */
349 typedef int8_t  s8;
350
351 /**
352  * @typedef s16
353  * @brief compatibility typedef for int16_t
354  */
355 typedef int16_t s16;
356
357 /**
358  * @typedef s32
359  * @brief compatibility typedef for int32_t
360  */
361 typedef int32_t s32;
362
363 /**
364  * @typedef s64
365  * @brief compatibility typedef for int64_t
366  */
367 typedef int64_t s64;
368
369 #define __le16 u16
370 #define __le32 u32
371 #define __le64 u64
372 #define __be16 u16
373 #define __be32 u32
374 #define __be64 u64
375
376 #define ice_hweight8(x) bitcount16((u8)x)
377 #define ice_hweight16(x) bitcount16(x)
378 #define ice_hweight32(x) bitcount32(x)
379 #define ice_hweight64(x) bitcount64(x)
380
381 /**
382  * @struct ice_dma_mem
383  * @brief DMA memory allocation
384  *
385  * Contains DMA allocation bits, used to simplify DMA allocations.
386  */
387 struct ice_dma_mem {
388         void *va;
389         uint64_t pa;
390         size_t size;
391
392         bus_dma_tag_t           tag;
393         bus_dmamap_t            map;
394         bus_dma_segment_t       seg;
395 };
396
397
398 void * ice_alloc_dma_mem(struct ice_hw *hw, struct ice_dma_mem *mem, u64 size);
399 void ice_free_dma_mem(struct ice_hw __unused *hw, struct ice_dma_mem *mem);
400
401 /**
402  * @struct ice_lock
403  * @brief simplified lock API
404  *
405  * Contains a simple lock implementation used to lock various resources.
406  */
407 struct ice_lock {
408         struct mtx mutex;
409         char name[ICE_STR_BUF_LEN];
410 };
411
412 extern u16 ice_lock_count;
413
414 /**
415  * ice_init_lock - Initialize a lock for use
416  * @lock: the lock memory to initialize
417  *
418  * OS compatibility layer to provide a simple locking mechanism. We use
419  * a mutex for this purpose.
420  */
421 static inline void
422 ice_init_lock(struct ice_lock *lock)
423 {
424         /*
425          * Make each lock unique by incrementing a counter each time this
426          * function is called. Use of a u16 allows 65535 possible locks before
427          * we'd hit a duplicate.
428          */
429         memset(lock->name, 0, sizeof(lock->name));
430         snprintf(lock->name, ICE_STR_BUF_LEN, "ice_lock_%u", ice_lock_count++);
431         mtx_init(&lock->mutex, lock->name, NULL, MTX_DEF);
432 }
433
434 /**
435  * ice_acquire_lock - Acquire the lock
436  * @lock: the lock to acquire
437  *
438  * Acquires the mutex specified by the lock pointer.
439  */
440 static inline void
441 ice_acquire_lock(struct ice_lock *lock)
442 {
443         mtx_lock(&lock->mutex);
444 }
445
446 /**
447  * ice_release_lock - Release the lock
448  * @lock: the lock to release
449  *
450  * Releases the mutex specified by the lock pointer.
451  */
452 static inline void
453 ice_release_lock(struct ice_lock *lock)
454 {
455         mtx_unlock(&lock->mutex);
456 }
457
458 /**
459  * ice_destroy_lock - Destroy the lock to de-allocate it
460  * @lock: the lock to destroy
461  *
462  * Destroys a previously initialized lock. We only do this if the mutex was
463  * previously initialized.
464  */
465 static inline void
466 ice_destroy_lock(struct ice_lock *lock)
467 {
468         if (mtx_initialized(&lock->mutex))
469                 mtx_destroy(&lock->mutex);
470         memset(lock->name, 0, sizeof(lock->name));
471 }
472
473 /* Some function parameters are unused outside of MPASS/KASSERT macros. Rather
474  * than marking these as __unused all the time, mark them as __invariant_only,
475  * and define this to __unused when INVARIANTS is disabled. Otherwise, define
476  * it empty so that __invariant_only parameters are caught as unused by the
477  * INVARIANTS build.
478  */
479 #ifndef INVARIANTS
480 #define __invariant_only __unused
481 #else
482 #define __invariant_only
483 #endif
484
485 #define __ALWAYS_UNUSED __unused
486
487 /**
488  * ice_ilog2 - Calculate the integer log base 2 of a 64bit value
489  * @n: 64bit number
490  *
491  * Calculates the integer log base 2 of a 64bit value, rounded down.
492  *
493  * @remark The integer log base 2 of zero is technically undefined, but this
494  * function will return 0 in that case.
495  *
496  */
497 static inline int
498 ice_ilog2(u64 n) {
499         if (n == 0)
500                 return 0;
501         return flsll(n) - 1;
502 }
503
504 /**
505  * ice_is_pow2 - Check if the value is a power of 2
506  * @n: 64bit number
507  *
508  * Check if the given value is a power of 2.
509  *
510  * @remark FreeBSD's powerof2 function treats zero as a power of 2, while this
511  * function does not.
512  *
513  * @returns true or false
514  */
515 static inline bool
516 ice_is_pow2(u64 n) {
517         if (n == 0)
518                 return false;
519         return powerof2(n);
520 }
521 #endif /* _ICE_OSDEP_H_ */