1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright (c) 2020, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
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.
35 * @brief OS compatibility layer
37 * Contains various definitions and functions which are part of an OS
38 * compatibility layer for sharing code with other operating systems.
43 #include <sys/endian.h>
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/systm.h>
50 #include <sys/mutex.h>
52 #include <machine/bus.h>
53 #include <sys/bus_dma.h>
54 #include <netinet/in.h>
55 #include <sys/counter.h>
58 #include "ice_alloc.h"
60 #define ICE_INTEL_VENDOR_ID 0x8086
62 #define ICE_STR_BUF_LEN 32
66 device_t ice_hw_to_dev(struct ice_hw *hw);
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);
73 #define ice_info(_hw, _fmt, args...) \
74 device_printf(ice_hw_to_dev(_hw), (_fmt), ##args)
76 #define ice_warn(_hw, _fmt, args...) \
77 device_printf(ice_hw_to_dev(_hw), (_fmt), ##args)
79 #define DIVIDE_AND_ROUND_UP howmany
80 #define ROUND_UP roundup
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);
87 #define ice_flush(_hw) rd32((_hw), GLGEN_STAT)
89 MALLOC_DECLARE(M_ICE_OSDEP);
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
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
102 ice_calloc(struct ice_hw __unused *hw, size_t count, size_t size)
104 return malloc(count * size, M_ICE_OSDEP, M_ZERO | M_NOWAIT);
108 * ice_malloc - Allocate memory of a specified size
109 * @hw: the hardware private structure
110 * @size: the size to allocate
112 * Allocates memory of the specified size. Note that the OS compatibility
113 * layer assumes that all allocations will provide zero'd memory.
116 ice_malloc(struct ice_hw __unused *hw, size_t size)
118 return malloc(size, M_ICE_OSDEP, M_ZERO | M_NOWAIT);
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
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.
133 ice_memdup(struct ice_hw __unused *hw, const void *src, size_t size,
134 enum ice_memcpy_type __unused dir)
136 void *dst = malloc(size, M_ICE_OSDEP, M_NOWAIT);
139 memcpy(dst, src, size);
145 * ice_free - Free previously allocated memory
146 * @hw: the hardware private structure
147 * @mem: pointer to the memory to free
149 * Free memory that was previously allocated by ice_calloc, ice_malloc, or
153 ice_free(struct ice_hw __unused *hw, void *mem)
155 free(mem, M_ICE_OSDEP);
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))
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);
167 #define UNREFERENCED_PARAMETER(_p) _p = _p
168 #define UNREFERENCED_1PARAMETER(_p) do { \
169 UNREFERENCED_PARAMETER(_p); \
171 #define UNREFERENCED_2PARAMETER(_p, _q) do { \
172 UNREFERENCED_PARAMETER(_p); \
173 UNREFERENCED_PARAMETER(_q); \
175 #define UNREFERENCED_3PARAMETER(_p, _q, _r) do { \
176 UNREFERENCED_PARAMETER(_p); \
177 UNREFERENCED_PARAMETER(_q); \
178 UNREFERENCED_PARAMETER(_r); \
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); \
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); \
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))
198 #define LIST_HEAD_TYPE ice_list_head
199 #define LIST_ENTRY_TYPE ice_list_node
202 * @struct ice_list_node
203 * @brief simplified linked list node API
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
209 struct ice_list_node {
210 LIST_ENTRY(ice_list_node) entries;
214 * @struct ice_list_head
215 * @brief simplified linked list head API
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.
222 LIST_HEAD(ice_list_head, ice_list_node);
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); \
241 #define LIST_ENTRY_SAFE(_ptr, _type, _member) \
242 ({ __typeof(_ptr) ____ptr = (_ptr); \
243 ____ptr ? _osdep_LIST_ENTRY(____ptr, _type, _member) : NULL; \
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
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
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.
260 static inline struct ice_list_node *
261 ice_get_list_tail(struct ice_list_head *head)
263 struct ice_list_node *node = LIST_FIRST(head);
267 while (LIST_NEXT(node, entries) != NULL)
268 node = LIST_NEXT(node, entries);
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); \
278 if (node == NULL) { \
279 LIST_ADD(entry, head); \
281 LIST_INSERT_AFTER(node, entry, entries); \
285 #define LIST_LAST_ENTRY(head, type, member) \
286 LIST_ENTRY_SAFE(ice_get_list_tail(head), type, member)
288 #define LIST_FIRST_ENTRY_SAFE(head, type, member) \
289 LIST_ENTRY_SAFE(LIST_FIRST(head), type, member)
291 #define LIST_NEXT_ENTRY_SAFE(ptr, member) \
292 LIST_ENTRY_SAFE(LIST_NEXT(&(ptr->member), entries), __typeof(*ptr), member)
294 #define LIST_FOR_EACH_ENTRY(pos, head, unused, member) \
295 for (pos = LIST_FIRST_ENTRY_SAFE(head, __typeof(*pos), member); \
297 pos = LIST_NEXT_ENTRY_SAFE(pos, member))
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; }); \
304 #define STATIC static
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
319 #define SNPRINTF snprintf
323 * @brief compatibility typedef for uint8_t
329 * @brief compatibility typedef for uint16_t
331 typedef uint16_t u16;
335 * @brief compatibility typedef for uint32_t
337 typedef uint32_t u32;
341 * @brief compatibility typedef for uint64_t
343 typedef uint64_t u64;
347 * @brief compatibility typedef for int8_t
353 * @brief compatibility typedef for int16_t
359 * @brief compatibility typedef for int32_t
365 * @brief compatibility typedef for int64_t
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)
382 * @struct ice_dma_mem
383 * @brief DMA memory allocation
385 * Contains DMA allocation bits, used to simplify DMA allocations.
394 bus_dma_segment_t seg;
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);
403 * @brief simplified lock API
405 * Contains a simple lock implementation used to lock various resources.
409 char name[ICE_STR_BUF_LEN];
412 extern u16 ice_lock_count;
415 * ice_init_lock - Initialize a lock for use
416 * @lock: the lock memory to initialize
418 * OS compatibility layer to provide a simple locking mechanism. We use
419 * a mutex for this purpose.
422 ice_init_lock(struct ice_lock *lock)
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.
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);
435 * ice_acquire_lock - Acquire the lock
436 * @lock: the lock to acquire
438 * Acquires the mutex specified by the lock pointer.
441 ice_acquire_lock(struct ice_lock *lock)
443 mtx_lock(&lock->mutex);
447 * ice_release_lock - Release the lock
448 * @lock: the lock to release
450 * Releases the mutex specified by the lock pointer.
453 ice_release_lock(struct ice_lock *lock)
455 mtx_unlock(&lock->mutex);
459 * ice_destroy_lock - Destroy the lock to de-allocate it
460 * @lock: the lock to destroy
462 * Destroys a previously initialized lock. We only do this if the mutex was
463 * previously initialized.
466 ice_destroy_lock(struct ice_lock *lock)
468 if (mtx_initialized(&lock->mutex))
469 mtx_destroy(&lock->mutex);
470 memset(lock->name, 0, sizeof(lock->name));
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
480 #define __invariant_only __unused
482 #define __invariant_only
485 #define __ALWAYS_UNUSED __unused
488 * ice_ilog2 - Calculate the integer log base 2 of a 64bit value
491 * Calculates the integer log base 2 of a 64bit value, rounded down.
493 * @remark The integer log base 2 of zero is technically undefined, but this
494 * function will return 0 in that case.
505 * ice_is_pow2 - Check if the value is a power of 2
508 * Check if the given value is a power of 2.
510 * @remark FreeBSD's powerof2 function treats zero as a power of 2, while this
513 * @returns true or false
521 #endif /* _ICE_OSDEP_H_ */