171d10453SEric Joyner /* SPDX-License-Identifier: BSD-3-Clause */ 2*015f8cc5SEric Joyner /* Copyright (c) 2024, Intel Corporation 371d10453SEric Joyner * All rights reserved. 471d10453SEric Joyner * 571d10453SEric Joyner * Redistribution and use in source and binary forms, with or without 671d10453SEric Joyner * modification, are permitted provided that the following conditions are met: 771d10453SEric Joyner * 871d10453SEric Joyner * 1. Redistributions of source code must retain the above copyright notice, 971d10453SEric Joyner * this list of conditions and the following disclaimer. 1071d10453SEric Joyner * 1171d10453SEric Joyner * 2. Redistributions in binary form must reproduce the above copyright 1271d10453SEric Joyner * notice, this list of conditions and the following disclaimer in the 1371d10453SEric Joyner * documentation and/or other materials provided with the distribution. 1471d10453SEric Joyner * 1571d10453SEric Joyner * 3. Neither the name of the Intel Corporation nor the names of its 1671d10453SEric Joyner * contributors may be used to endorse or promote products derived from 1771d10453SEric Joyner * this software without specific prior written permission. 1871d10453SEric Joyner * 1971d10453SEric Joyner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2071d10453SEric Joyner * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2171d10453SEric Joyner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2271d10453SEric Joyner * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 2371d10453SEric Joyner * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2471d10453SEric Joyner * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2571d10453SEric Joyner * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2671d10453SEric Joyner * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2771d10453SEric Joyner * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2871d10453SEric Joyner * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2971d10453SEric Joyner * POSSIBILITY OF SUCH DAMAGE. 3071d10453SEric Joyner */ 3171d10453SEric Joyner 3271d10453SEric Joyner /** 3371d10453SEric Joyner * @file ice_osdep.h 3471d10453SEric Joyner * @brief OS compatibility layer 3571d10453SEric Joyner * 3671d10453SEric Joyner * Contains various definitions and functions which are part of an OS 3771d10453SEric Joyner * compatibility layer for sharing code with other operating systems. 3871d10453SEric Joyner */ 3971d10453SEric Joyner #ifndef _ICE_OSDEP_H_ 4071d10453SEric Joyner #define _ICE_OSDEP_H_ 4171d10453SEric Joyner 4271d10453SEric Joyner #include <sys/endian.h> 4371d10453SEric Joyner #include <sys/param.h> 4471d10453SEric Joyner #include <sys/kernel.h> 4571d10453SEric Joyner #include <sys/malloc.h> 4671d10453SEric Joyner #include <sys/proc.h> 4771d10453SEric Joyner #include <sys/systm.h> 4871d10453SEric Joyner #include <sys/lock.h> 4971d10453SEric Joyner #include <sys/mutex.h> 5071d10453SEric Joyner #include <sys/bus.h> 5171d10453SEric Joyner #include <machine/bus.h> 5271d10453SEric Joyner #include <sys/bus_dma.h> 5371d10453SEric Joyner #include <netinet/in.h> 5471d10453SEric Joyner #include <sys/counter.h> 5571d10453SEric Joyner #include <sys/sbuf.h> 5671d10453SEric Joyner 5771d10453SEric Joyner #include "ice_alloc.h" 5871d10453SEric Joyner 5971d10453SEric Joyner #define ICE_INTEL_VENDOR_ID 0x8086 6071d10453SEric Joyner 6171d10453SEric Joyner #define ICE_STR_BUF_LEN 32 6271d10453SEric Joyner 6371d10453SEric Joyner struct ice_hw; 6471d10453SEric Joyner 6571d10453SEric Joyner device_t ice_hw_to_dev(struct ice_hw *hw); 6671d10453SEric Joyner 6771d10453SEric Joyner /* configure hw->debug_mask to enable debug prints */ 6871d10453SEric Joyner void ice_debug(struct ice_hw *hw, uint64_t mask, char *fmt, ...) __printflike(3, 4); 6971d10453SEric Joyner void ice_debug_array(struct ice_hw *hw, uint64_t mask, uint32_t rowsize, 7071d10453SEric Joyner uint32_t groupsize, uint8_t *buf, size_t len); 7156429daeSEric Joyner void ice_info_fwlog(struct ice_hw *hw, uint32_t rowsize, uint32_t groupsize, 7256429daeSEric Joyner uint8_t *buf, size_t len); 7371d10453SEric Joyner 749dc2f6e2SEric Joyner #define ice_fls(_n) flsl(_n) 759dc2f6e2SEric Joyner 7671d10453SEric Joyner #define ice_info(_hw, _fmt, args...) \ 7771d10453SEric Joyner device_printf(ice_hw_to_dev(_hw), (_fmt), ##args) 7871d10453SEric Joyner 7971d10453SEric Joyner #define ice_warn(_hw, _fmt, args...) \ 8071d10453SEric Joyner device_printf(ice_hw_to_dev(_hw), (_fmt), ##args) 8171d10453SEric Joyner 8271d10453SEric Joyner #define DIVIDE_AND_ROUND_UP howmany 8371d10453SEric Joyner #define ROUND_UP roundup 8471d10453SEric Joyner 8571d10453SEric Joyner uint32_t rd32(struct ice_hw *hw, uint32_t reg); 8671d10453SEric Joyner uint64_t rd64(struct ice_hw *hw, uint32_t reg); 8771d10453SEric Joyner void wr32(struct ice_hw *hw, uint32_t reg, uint32_t val); 8871d10453SEric Joyner void wr64(struct ice_hw *hw, uint32_t reg, uint64_t val); 8971d10453SEric Joyner 9071d10453SEric Joyner #define ice_flush(_hw) rd32((_hw), GLGEN_STAT) 9171d10453SEric Joyner 9271d10453SEric Joyner MALLOC_DECLARE(M_ICE_OSDEP); 9371d10453SEric Joyner 9471d10453SEric Joyner /** 9571d10453SEric Joyner * ice_calloc - Allocate an array of elementes 9671d10453SEric Joyner * @hw: the hardware private structure 9771d10453SEric Joyner * @count: number of elements to allocate 9871d10453SEric Joyner * @size: the size of each element 9971d10453SEric Joyner * 10071d10453SEric Joyner * Allocate memory for an array of items equal to size. Note that the OS 10171d10453SEric Joyner * compatibility layer assumes all allocation functions will provide zero'd 10271d10453SEric Joyner * memory. 10371d10453SEric Joyner */ 10471d10453SEric Joyner static inline void * 10571d10453SEric Joyner ice_calloc(struct ice_hw __unused *hw, size_t count, size_t size) 10671d10453SEric Joyner { 10771d10453SEric Joyner return malloc(count * size, M_ICE_OSDEP, M_ZERO | M_NOWAIT); 10871d10453SEric Joyner } 10971d10453SEric Joyner 11071d10453SEric Joyner /** 11171d10453SEric Joyner * ice_malloc - Allocate memory of a specified size 11271d10453SEric Joyner * @hw: the hardware private structure 11371d10453SEric Joyner * @size: the size to allocate 11471d10453SEric Joyner * 11571d10453SEric Joyner * Allocates memory of the specified size. Note that the OS compatibility 11671d10453SEric Joyner * layer assumes that all allocations will provide zero'd memory. 11771d10453SEric Joyner */ 11871d10453SEric Joyner static inline void * 11971d10453SEric Joyner ice_malloc(struct ice_hw __unused *hw, size_t size) 12071d10453SEric Joyner { 12171d10453SEric Joyner return malloc(size, M_ICE_OSDEP, M_ZERO | M_NOWAIT); 12271d10453SEric Joyner } 12371d10453SEric Joyner 12471d10453SEric Joyner /** 12571d10453SEric Joyner * ice_memdup - Allocate a copy of some other memory 12671d10453SEric Joyner * @hw: private hardware structure 12771d10453SEric Joyner * @src: the source to copy from 12871d10453SEric Joyner * @size: allocation size 12971d10453SEric Joyner * @dir: the direction of copying 13071d10453SEric Joyner * 13171d10453SEric Joyner * Allocate memory of the specified size, and copy bytes from the src to fill 13271d10453SEric Joyner * it. We don't need to zero this memory as we immediately initialize it by 13371d10453SEric Joyner * copying from the src pointer. 13471d10453SEric Joyner */ 13571d10453SEric Joyner static inline void * 13671d10453SEric Joyner ice_memdup(struct ice_hw __unused *hw, const void *src, size_t size, 13771d10453SEric Joyner enum ice_memcpy_type __unused dir) 13871d10453SEric Joyner { 13971d10453SEric Joyner void *dst = malloc(size, M_ICE_OSDEP, M_NOWAIT); 14071d10453SEric Joyner 14171d10453SEric Joyner if (dst != NULL) 14271d10453SEric Joyner memcpy(dst, src, size); 14371d10453SEric Joyner 14471d10453SEric Joyner return dst; 14571d10453SEric Joyner } 14671d10453SEric Joyner 14771d10453SEric Joyner /** 14871d10453SEric Joyner * ice_free - Free previously allocated memory 14971d10453SEric Joyner * @hw: the hardware private structure 15071d10453SEric Joyner * @mem: pointer to the memory to free 15171d10453SEric Joyner * 15271d10453SEric Joyner * Free memory that was previously allocated by ice_calloc, ice_malloc, or 15371d10453SEric Joyner * ice_memdup. 15471d10453SEric Joyner */ 15571d10453SEric Joyner static inline void 15671d10453SEric Joyner ice_free(struct ice_hw __unused *hw, void *mem) 15771d10453SEric Joyner { 15871d10453SEric Joyner free(mem, M_ICE_OSDEP); 15971d10453SEric Joyner } 16071d10453SEric Joyner 16171d10453SEric Joyner /* These are macros in order to drop the unused direction enumeration constant */ 16271d10453SEric Joyner #define ice_memset(addr, c, len, unused) memset((addr), (c), (len)) 16371d10453SEric Joyner #define ice_memcpy(dst, src, len, unused) memcpy((dst), (src), (len)) 16471d10453SEric Joyner 16571d10453SEric Joyner void ice_usec_delay(uint32_t time, bool sleep); 16671d10453SEric Joyner void ice_msec_delay(uint32_t time, bool sleep); 16771d10453SEric Joyner void ice_msec_pause(uint32_t time); 16871d10453SEric Joyner void ice_msec_spin(uint32_t time); 16971d10453SEric Joyner 17071d10453SEric Joyner #define UNREFERENCED_PARAMETER(_p) _p = _p 17171d10453SEric Joyner #define UNREFERENCED_1PARAMETER(_p) do { \ 17271d10453SEric Joyner UNREFERENCED_PARAMETER(_p); \ 17371d10453SEric Joyner } while (0) 17471d10453SEric Joyner #define UNREFERENCED_2PARAMETER(_p, _q) do { \ 17571d10453SEric Joyner UNREFERENCED_PARAMETER(_p); \ 17671d10453SEric Joyner UNREFERENCED_PARAMETER(_q); \ 17771d10453SEric Joyner } while (0) 17871d10453SEric Joyner #define UNREFERENCED_3PARAMETER(_p, _q, _r) do { \ 17971d10453SEric Joyner UNREFERENCED_PARAMETER(_p); \ 18071d10453SEric Joyner UNREFERENCED_PARAMETER(_q); \ 18171d10453SEric Joyner UNREFERENCED_PARAMETER(_r); \ 18271d10453SEric Joyner } while (0) 18371d10453SEric Joyner #define UNREFERENCED_4PARAMETER(_p, _q, _r, _s) do { \ 18471d10453SEric Joyner UNREFERENCED_PARAMETER(_p); \ 18571d10453SEric Joyner UNREFERENCED_PARAMETER(_q); \ 18671d10453SEric Joyner UNREFERENCED_PARAMETER(_r); \ 18771d10453SEric Joyner UNREFERENCED_PARAMETER(_s); \ 18871d10453SEric Joyner } while (0) 18971d10453SEric Joyner #define UNREFERENCED_5PARAMETER(_p, _q, _r, _s, _t) do { \ 19071d10453SEric Joyner UNREFERENCED_PARAMETER(_p); \ 19171d10453SEric Joyner UNREFERENCED_PARAMETER(_q); \ 19271d10453SEric Joyner UNREFERENCED_PARAMETER(_r); \ 19371d10453SEric Joyner UNREFERENCED_PARAMETER(_s); \ 19471d10453SEric Joyner UNREFERENCED_PARAMETER(_t); \ 19571d10453SEric Joyner } while (0) 19671d10453SEric Joyner 19771d10453SEric Joyner #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 19871d10453SEric Joyner #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 19971d10453SEric Joyner #define MAKEMASK(_m, _s) ((_m) << (_s)) 20071d10453SEric Joyner 20171d10453SEric Joyner #define LIST_HEAD_TYPE ice_list_head 20271d10453SEric Joyner #define LIST_ENTRY_TYPE ice_list_node 20371d10453SEric Joyner 20471d10453SEric Joyner /** 20571d10453SEric Joyner * @struct ice_list_node 20671d10453SEric Joyner * @brief simplified linked list node API 20771d10453SEric Joyner * 20871d10453SEric Joyner * Represents a node in a linked list, which can be embedded into a structure 20971d10453SEric Joyner * to allow that structure to be inserted into a linked list. Access to the 21071d10453SEric Joyner * contained structure is done via __containerof 21171d10453SEric Joyner */ 21271d10453SEric Joyner struct ice_list_node { 21371d10453SEric Joyner LIST_ENTRY(ice_list_node) entries; 21471d10453SEric Joyner }; 21571d10453SEric Joyner 21671d10453SEric Joyner /** 21771d10453SEric Joyner * @struct ice_list_head 21871d10453SEric Joyner * @brief simplified linked list head API 21971d10453SEric Joyner * 22071d10453SEric Joyner * Represents the head of a linked list. The linked list should consist of 22171d10453SEric Joyner * a series of ice_list_node structures embedded into another structure 22271d10453SEric Joyner * accessed using __containerof. This way, the ice_list_head doesn't need to 22371d10453SEric Joyner * know the type of the structure it contains. 22471d10453SEric Joyner */ 22571d10453SEric Joyner LIST_HEAD(ice_list_head, ice_list_node); 22671d10453SEric Joyner 22771d10453SEric Joyner #define INIT_LIST_HEAD LIST_INIT 22871d10453SEric Joyner /* LIST_EMPTY doesn't need to be changed */ 22971d10453SEric Joyner #define LIST_ADD(entry, head) LIST_INSERT_HEAD(head, entry, entries) 23071d10453SEric Joyner #define LIST_ADD_AFTER(entry, elem) LIST_INSERT_AFTER(elem, entry, entries) 23171d10453SEric Joyner #define LIST_DEL(entry) LIST_REMOVE(entry, entries) 23271d10453SEric Joyner #define _osdep_LIST_ENTRY(ptr, type, member) \ 23371d10453SEric Joyner __containerof(ptr, type, member) 23471d10453SEric Joyner #define LIST_FIRST_ENTRY(head, type, member) \ 23571d10453SEric Joyner _osdep_LIST_ENTRY(LIST_FIRST(head), type, member) 23671d10453SEric Joyner #define LIST_NEXT_ENTRY(ptr, unused, member) \ 23771d10453SEric Joyner _osdep_LIST_ENTRY(LIST_NEXT(&(ptr->member), entries), __typeof(*ptr), member) 23871d10453SEric Joyner #define LIST_REPLACE_INIT(old_head, new_head) do { \ 23971d10453SEric Joyner __typeof(new_head) _new_head = (new_head); \ 24071d10453SEric Joyner LIST_INIT(_new_head); \ 24171d10453SEric Joyner LIST_SWAP(old_head, _new_head, ice_list_node, entries); \ 24271d10453SEric Joyner } while (0) 24371d10453SEric Joyner 24471d10453SEric Joyner #define LIST_ENTRY_SAFE(_ptr, _type, _member) \ 24571d10453SEric Joyner ({ __typeof(_ptr) ____ptr = (_ptr); \ 24671d10453SEric Joyner ____ptr ? _osdep_LIST_ENTRY(____ptr, _type, _member) : NULL; \ 24771d10453SEric Joyner }) 24871d10453SEric Joyner 24971d10453SEric Joyner /** 25071d10453SEric Joyner * ice_get_list_tail - Return the pointer to the last node in the list 25171d10453SEric Joyner * @head: the pointer to the head of the list 25271d10453SEric Joyner * 25371d10453SEric Joyner * A helper function for implementing LIST_ADD_TAIL and LIST_LAST_ENTRY. 25471d10453SEric Joyner * Returns the pointer to the last node in the list, or NULL of the list is 25571d10453SEric Joyner * empty. 25671d10453SEric Joyner * 25771d10453SEric Joyner * Note: due to the list implementation this is O(N), where N is the size of 25871d10453SEric Joyner * the list. An O(1) implementation requires replacing the underlying list 25971d10453SEric Joyner * datastructure with one that has a tail pointer. This is problematic, 26071d10453SEric Joyner * because using a simple TAILQ would require that the addition and deletion 26171d10453SEric Joyner * be given the head of the list. 26271d10453SEric Joyner */ 26371d10453SEric Joyner static inline struct ice_list_node * 26471d10453SEric Joyner ice_get_list_tail(struct ice_list_head *head) 26571d10453SEric Joyner { 26671d10453SEric Joyner struct ice_list_node *node = LIST_FIRST(head); 26771d10453SEric Joyner 26871d10453SEric Joyner if (node == NULL) 26971d10453SEric Joyner return NULL; 27071d10453SEric Joyner while (LIST_NEXT(node, entries) != NULL) 27171d10453SEric Joyner node = LIST_NEXT(node, entries); 27271d10453SEric Joyner 27371d10453SEric Joyner return node; 27471d10453SEric Joyner } 27571d10453SEric Joyner 27671d10453SEric Joyner /* TODO: This is O(N). An O(1) implementation would require a different 27771d10453SEric Joyner * underlying list structure, such as a circularly linked list. */ 27871d10453SEric Joyner #define LIST_ADD_TAIL(entry, head) do { \ 27971d10453SEric Joyner struct ice_list_node *node = ice_get_list_tail(head); \ 28071d10453SEric Joyner \ 28171d10453SEric Joyner if (node == NULL) { \ 28271d10453SEric Joyner LIST_ADD(entry, head); \ 28371d10453SEric Joyner } else { \ 28471d10453SEric Joyner LIST_INSERT_AFTER(node, entry, entries); \ 28571d10453SEric Joyner } \ 28671d10453SEric Joyner } while (0) 28771d10453SEric Joyner 28871d10453SEric Joyner #define LIST_LAST_ENTRY(head, type, member) \ 28971d10453SEric Joyner LIST_ENTRY_SAFE(ice_get_list_tail(head), type, member) 29071d10453SEric Joyner 29171d10453SEric Joyner #define LIST_FIRST_ENTRY_SAFE(head, type, member) \ 29271d10453SEric Joyner LIST_ENTRY_SAFE(LIST_FIRST(head), type, member) 29371d10453SEric Joyner 29471d10453SEric Joyner #define LIST_NEXT_ENTRY_SAFE(ptr, member) \ 29571d10453SEric Joyner LIST_ENTRY_SAFE(LIST_NEXT(&(ptr->member), entries), __typeof(*ptr), member) 29671d10453SEric Joyner 29771d10453SEric Joyner #define LIST_FOR_EACH_ENTRY(pos, head, unused, member) \ 29871d10453SEric Joyner for (pos = LIST_FIRST_ENTRY_SAFE(head, __typeof(*pos), member); \ 29971d10453SEric Joyner pos; \ 30071d10453SEric Joyner pos = LIST_NEXT_ENTRY_SAFE(pos, member)) 30171d10453SEric Joyner 30271d10453SEric Joyner #define LIST_FOR_EACH_ENTRY_SAFE(pos, n, head, unused, member) \ 30371d10453SEric Joyner for (pos = LIST_FIRST_ENTRY_SAFE(head, __typeof(*pos), member); \ 30471d10453SEric Joyner pos && ({ n = LIST_NEXT_ENTRY_SAFE(pos, member); 1; }); \ 30571d10453SEric Joyner pos = n) 30671d10453SEric Joyner 30771d10453SEric Joyner #define STATIC static 30871d10453SEric Joyner 30971d10453SEric Joyner #define NTOHS ntohs 31071d10453SEric Joyner #define NTOHL ntohl 31171d10453SEric Joyner #define HTONS htons 31271d10453SEric Joyner #define HTONL htonl 31371d10453SEric Joyner #define LE16_TO_CPU le16toh 31471d10453SEric Joyner #define LE32_TO_CPU le32toh 31571d10453SEric Joyner #define LE64_TO_CPU le64toh 31671d10453SEric Joyner #define CPU_TO_LE16 htole16 31771d10453SEric Joyner #define CPU_TO_LE32 htole32 31871d10453SEric Joyner #define CPU_TO_LE64 htole64 31971d10453SEric Joyner #define CPU_TO_BE16 htobe16 32071d10453SEric Joyner #define CPU_TO_BE32 htobe32 32171d10453SEric Joyner 32271d10453SEric Joyner #define SNPRINTF snprintf 32371d10453SEric Joyner 32471d10453SEric Joyner /** 32571d10453SEric Joyner * @typedef u8 32671d10453SEric Joyner * @brief compatibility typedef for uint8_t 32771d10453SEric Joyner */ 32871d10453SEric Joyner typedef uint8_t u8; 32971d10453SEric Joyner 33071d10453SEric Joyner /** 33171d10453SEric Joyner * @typedef u16 33271d10453SEric Joyner * @brief compatibility typedef for uint16_t 33371d10453SEric Joyner */ 33471d10453SEric Joyner typedef uint16_t u16; 33571d10453SEric Joyner 33671d10453SEric Joyner /** 33771d10453SEric Joyner * @typedef u32 33871d10453SEric Joyner * @brief compatibility typedef for uint32_t 33971d10453SEric Joyner */ 34071d10453SEric Joyner typedef uint32_t u32; 34171d10453SEric Joyner 34271d10453SEric Joyner /** 34371d10453SEric Joyner * @typedef u64 34471d10453SEric Joyner * @brief compatibility typedef for uint64_t 34571d10453SEric Joyner */ 34671d10453SEric Joyner typedef uint64_t u64; 34771d10453SEric Joyner 34871d10453SEric Joyner /** 34971d10453SEric Joyner * @typedef s8 35071d10453SEric Joyner * @brief compatibility typedef for int8_t 35171d10453SEric Joyner */ 35271d10453SEric Joyner typedef int8_t s8; 35371d10453SEric Joyner 35471d10453SEric Joyner /** 35571d10453SEric Joyner * @typedef s16 35671d10453SEric Joyner * @brief compatibility typedef for int16_t 35771d10453SEric Joyner */ 35871d10453SEric Joyner typedef int16_t s16; 35971d10453SEric Joyner 36071d10453SEric Joyner /** 36171d10453SEric Joyner * @typedef s32 36271d10453SEric Joyner * @brief compatibility typedef for int32_t 36371d10453SEric Joyner */ 36471d10453SEric Joyner typedef int32_t s32; 36571d10453SEric Joyner 36671d10453SEric Joyner /** 36771d10453SEric Joyner * @typedef s64 36871d10453SEric Joyner * @brief compatibility typedef for int64_t 36971d10453SEric Joyner */ 37071d10453SEric Joyner typedef int64_t s64; 37171d10453SEric Joyner 37271d10453SEric Joyner #define __le16 u16 37371d10453SEric Joyner #define __le32 u32 37471d10453SEric Joyner #define __le64 u64 37571d10453SEric Joyner #define __be16 u16 37671d10453SEric Joyner #define __be32 u32 37771d10453SEric Joyner #define __be64 u64 37871d10453SEric Joyner 37971d10453SEric Joyner #define ice_hweight8(x) bitcount16((u8)x) 38071d10453SEric Joyner #define ice_hweight16(x) bitcount16(x) 38171d10453SEric Joyner #define ice_hweight32(x) bitcount32(x) 38271d10453SEric Joyner #define ice_hweight64(x) bitcount64(x) 38371d10453SEric Joyner 38471d10453SEric Joyner /** 38571d10453SEric Joyner * @struct ice_dma_mem 38671d10453SEric Joyner * @brief DMA memory allocation 38771d10453SEric Joyner * 38871d10453SEric Joyner * Contains DMA allocation bits, used to simplify DMA allocations. 38971d10453SEric Joyner */ 39071d10453SEric Joyner struct ice_dma_mem { 39171d10453SEric Joyner void *va; 39271d10453SEric Joyner uint64_t pa; 39371d10453SEric Joyner size_t size; 39471d10453SEric Joyner 39571d10453SEric Joyner bus_dma_tag_t tag; 39671d10453SEric Joyner bus_dmamap_t map; 39771d10453SEric Joyner bus_dma_segment_t seg; 39871d10453SEric Joyner }; 39971d10453SEric Joyner 40071d10453SEric Joyner 40171d10453SEric Joyner void * ice_alloc_dma_mem(struct ice_hw *hw, struct ice_dma_mem *mem, u64 size); 40271d10453SEric Joyner void ice_free_dma_mem(struct ice_hw __unused *hw, struct ice_dma_mem *mem); 40371d10453SEric Joyner 40471d10453SEric Joyner /** 40571d10453SEric Joyner * @struct ice_lock 40671d10453SEric Joyner * @brief simplified lock API 40771d10453SEric Joyner * 40871d10453SEric Joyner * Contains a simple lock implementation used to lock various resources. 40971d10453SEric Joyner */ 41071d10453SEric Joyner struct ice_lock { 41171d10453SEric Joyner struct mtx mutex; 41271d10453SEric Joyner char name[ICE_STR_BUF_LEN]; 41371d10453SEric Joyner }; 41471d10453SEric Joyner 41571d10453SEric Joyner extern u16 ice_lock_count; 41671d10453SEric Joyner 41771d10453SEric Joyner /** 41871d10453SEric Joyner * ice_init_lock - Initialize a lock for use 41971d10453SEric Joyner * @lock: the lock memory to initialize 42071d10453SEric Joyner * 42171d10453SEric Joyner * OS compatibility layer to provide a simple locking mechanism. We use 42271d10453SEric Joyner * a mutex for this purpose. 42371d10453SEric Joyner */ 42471d10453SEric Joyner static inline void 42571d10453SEric Joyner ice_init_lock(struct ice_lock *lock) 42671d10453SEric Joyner { 42771d10453SEric Joyner /* 42871d10453SEric Joyner * Make each lock unique by incrementing a counter each time this 42971d10453SEric Joyner * function is called. Use of a u16 allows 65535 possible locks before 43071d10453SEric Joyner * we'd hit a duplicate. 43171d10453SEric Joyner */ 43271d10453SEric Joyner memset(lock->name, 0, sizeof(lock->name)); 43371d10453SEric Joyner snprintf(lock->name, ICE_STR_BUF_LEN, "ice_lock_%u", ice_lock_count++); 43471d10453SEric Joyner mtx_init(&lock->mutex, lock->name, NULL, MTX_DEF); 43571d10453SEric Joyner } 43671d10453SEric Joyner 43771d10453SEric Joyner /** 43871d10453SEric Joyner * ice_acquire_lock - Acquire the lock 43971d10453SEric Joyner * @lock: the lock to acquire 44071d10453SEric Joyner * 44171d10453SEric Joyner * Acquires the mutex specified by the lock pointer. 44271d10453SEric Joyner */ 44371d10453SEric Joyner static inline void 44471d10453SEric Joyner ice_acquire_lock(struct ice_lock *lock) 44571d10453SEric Joyner { 44671d10453SEric Joyner mtx_lock(&lock->mutex); 44771d10453SEric Joyner } 44871d10453SEric Joyner 44971d10453SEric Joyner /** 45071d10453SEric Joyner * ice_release_lock - Release the lock 45171d10453SEric Joyner * @lock: the lock to release 45271d10453SEric Joyner * 45371d10453SEric Joyner * Releases the mutex specified by the lock pointer. 45471d10453SEric Joyner */ 45571d10453SEric Joyner static inline void 45671d10453SEric Joyner ice_release_lock(struct ice_lock *lock) 45771d10453SEric Joyner { 45871d10453SEric Joyner mtx_unlock(&lock->mutex); 45971d10453SEric Joyner } 46071d10453SEric Joyner 46171d10453SEric Joyner /** 46271d10453SEric Joyner * ice_destroy_lock - Destroy the lock to de-allocate it 46371d10453SEric Joyner * @lock: the lock to destroy 46471d10453SEric Joyner * 46571d10453SEric Joyner * Destroys a previously initialized lock. We only do this if the mutex was 46671d10453SEric Joyner * previously initialized. 46771d10453SEric Joyner */ 46871d10453SEric Joyner static inline void 46971d10453SEric Joyner ice_destroy_lock(struct ice_lock *lock) 47071d10453SEric Joyner { 47171d10453SEric Joyner if (mtx_initialized(&lock->mutex)) 47271d10453SEric Joyner mtx_destroy(&lock->mutex); 47371d10453SEric Joyner memset(lock->name, 0, sizeof(lock->name)); 47471d10453SEric Joyner } 47571d10453SEric Joyner 47671d10453SEric Joyner /* Some function parameters are unused outside of MPASS/KASSERT macros. Rather 47771d10453SEric Joyner * than marking these as __unused all the time, mark them as __invariant_only, 47871d10453SEric Joyner * and define this to __unused when INVARIANTS is disabled. Otherwise, define 47971d10453SEric Joyner * it empty so that __invariant_only parameters are caught as unused by the 48071d10453SEric Joyner * INVARIANTS build. 48171d10453SEric Joyner */ 48271d10453SEric Joyner #ifndef INVARIANTS 48371d10453SEric Joyner #define __invariant_only __unused 48471d10453SEric Joyner #else 48571d10453SEric Joyner #define __invariant_only 48671d10453SEric Joyner #endif 48771d10453SEric Joyner 48871d10453SEric Joyner #define __ALWAYS_UNUSED __unused 48971d10453SEric Joyner 49071d10453SEric Joyner /** 49171d10453SEric Joyner * ice_ilog2 - Calculate the integer log base 2 of a 64bit value 49271d10453SEric Joyner * @n: 64bit number 49371d10453SEric Joyner * 49471d10453SEric Joyner * Calculates the integer log base 2 of a 64bit value, rounded down. 49571d10453SEric Joyner * 49671d10453SEric Joyner * @remark The integer log base 2 of zero is technically undefined, but this 49771d10453SEric Joyner * function will return 0 in that case. 49871d10453SEric Joyner * 49971d10453SEric Joyner */ 50071d10453SEric Joyner static inline int 50171d10453SEric Joyner ice_ilog2(u64 n) { 50271d10453SEric Joyner if (n == 0) 50371d10453SEric Joyner return 0; 50471d10453SEric Joyner return flsll(n) - 1; 50571d10453SEric Joyner } 50671d10453SEric Joyner 50771d10453SEric Joyner /** 50871d10453SEric Joyner * ice_is_pow2 - Check if the value is a power of 2 50971d10453SEric Joyner * @n: 64bit number 51071d10453SEric Joyner * 51171d10453SEric Joyner * Check if the given value is a power of 2. 51271d10453SEric Joyner * 51371d10453SEric Joyner * @remark FreeBSD's powerof2 function treats zero as a power of 2, while this 51471d10453SEric Joyner * function does not. 51571d10453SEric Joyner * 51671d10453SEric Joyner * @returns true or false 51771d10453SEric Joyner */ 51871d10453SEric Joyner static inline bool 51971d10453SEric Joyner ice_is_pow2(u64 n) { 52071d10453SEric Joyner if (n == 0) 52171d10453SEric Joyner return false; 52271d10453SEric Joyner return powerof2(n); 52371d10453SEric Joyner } 52471d10453SEric Joyner #endif /* _ICE_OSDEP_H_ */ 525