The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ice/ice_osdep.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* SPDX-License-Identifier: BSD-3-Clause */
    2 /*  Copyright (c) 2021, 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 void ice_info_fwlog(struct ice_hw *hw, uint32_t rowsize, uint32_t groupsize,
   73                     uint8_t *buf, size_t len);
   74 
   75 #define ice_info(_hw, _fmt, args...) \
   76         device_printf(ice_hw_to_dev(_hw), (_fmt), ##args)
   77 
   78 #define ice_warn(_hw, _fmt, args...) \
   79         device_printf(ice_hw_to_dev(_hw), (_fmt), ##args)
   80 
   81 #define DIVIDE_AND_ROUND_UP howmany
   82 #define ROUND_UP roundup
   83 
   84 uint32_t rd32(struct ice_hw *hw, uint32_t reg);
   85 uint64_t rd64(struct ice_hw *hw, uint32_t reg);
   86 void wr32(struct ice_hw *hw, uint32_t reg, uint32_t val);
   87 void wr64(struct ice_hw *hw, uint32_t reg, uint64_t val);
   88 
   89 #define ice_flush(_hw) rd32((_hw), GLGEN_STAT)
   90 
   91 MALLOC_DECLARE(M_ICE_OSDEP);
   92 
   93 /**
   94  * ice_calloc - Allocate an array of elementes
   95  * @hw: the hardware private structure
   96  * @count: number of elements to allocate
   97  * @size: the size of each element
   98  *
   99  * Allocate memory for an array of items equal to size. Note that the OS
  100  * compatibility layer assumes all allocation functions will provide zero'd
  101  * memory.
  102  */
  103 static inline void *
  104 ice_calloc(struct ice_hw __unused *hw, size_t count, size_t size)
  105 {
  106         return malloc(count * size, M_ICE_OSDEP, M_ZERO | M_NOWAIT);
  107 }
  108 
  109 /**
  110  * ice_malloc - Allocate memory of a specified size
  111  * @hw: the hardware private structure
  112  * @size: the size to allocate
  113  *
  114  * Allocates memory of the specified size. Note that the OS compatibility
  115  * layer assumes that all allocations will provide zero'd memory.
  116  */
  117 static inline void *
  118 ice_malloc(struct ice_hw __unused *hw, size_t size)
  119 {
  120         return malloc(size, M_ICE_OSDEP, M_ZERO | M_NOWAIT);
  121 }
  122 
  123 /**
  124  * ice_memdup - Allocate a copy of some other memory
  125  * @hw: private hardware structure
  126  * @src: the source to copy from
  127  * @size: allocation size
  128  * @dir: the direction of copying
  129  *
  130  * Allocate memory of the specified size, and copy bytes from the src to fill
  131  * it. We don't need to zero this memory as we immediately initialize it by
  132  * copying from the src pointer.
  133  */
  134 static inline void *
  135 ice_memdup(struct ice_hw __unused *hw, const void *src, size_t size,
  136            enum ice_memcpy_type __unused dir)
  137 {
  138         void *dst = malloc(size, M_ICE_OSDEP, M_NOWAIT);
  139 
  140         if (dst != NULL)
  141                 memcpy(dst, src, size);
  142 
  143         return dst;
  144 }
  145 
  146 /**
  147  * ice_free - Free previously allocated memory
  148  * @hw: the hardware private structure
  149  * @mem: pointer to the memory to free
  150  *
  151  * Free memory that was previously allocated by ice_calloc, ice_malloc, or
  152  * ice_memdup.
  153  */
  154 static inline void
  155 ice_free(struct ice_hw __unused *hw, void *mem)
  156 {
  157         free(mem, M_ICE_OSDEP);
  158 }
  159 
  160 /* These are macros in order to drop the unused direction enumeration constant */
  161 #define ice_memset(addr, c, len, unused) memset((addr), (c), (len))
  162 #define ice_memcpy(dst, src, len, unused) memcpy((dst), (src), (len))
  163 
  164 void ice_usec_delay(uint32_t time, bool sleep);
  165 void ice_msec_delay(uint32_t time, bool sleep);
  166 void ice_msec_pause(uint32_t time);
  167 void ice_msec_spin(uint32_t time);
  168 
  169 #define UNREFERENCED_PARAMETER(_p) _p = _p
  170 #define UNREFERENCED_1PARAMETER(_p) do {                        \
  171         UNREFERENCED_PARAMETER(_p);                             \
  172 } while (0)
  173 #define UNREFERENCED_2PARAMETER(_p, _q) do {                    \
  174         UNREFERENCED_PARAMETER(_p);                             \
  175         UNREFERENCED_PARAMETER(_q);                             \
  176 } while (0)
  177 #define UNREFERENCED_3PARAMETER(_p, _q, _r) do {                \
  178         UNREFERENCED_PARAMETER(_p);                             \
  179         UNREFERENCED_PARAMETER(_q);                             \
  180         UNREFERENCED_PARAMETER(_r);                             \
  181 } while (0)
  182 #define UNREFERENCED_4PARAMETER(_p, _q, _r, _s) do {            \
  183         UNREFERENCED_PARAMETER(_p);                             \
  184         UNREFERENCED_PARAMETER(_q);                             \
  185         UNREFERENCED_PARAMETER(_r);                             \
  186         UNREFERENCED_PARAMETER(_s);                             \
  187 } while (0)
  188 #define UNREFERENCED_5PARAMETER(_p, _q, _r, _s, _t) do {        \
  189         UNREFERENCED_PARAMETER(_p);                             \
  190         UNREFERENCED_PARAMETER(_q);                             \
  191         UNREFERENCED_PARAMETER(_r);                             \
  192         UNREFERENCED_PARAMETER(_s);                             \
  193         UNREFERENCED_PARAMETER(_t);                             \
  194 } while (0)
  195 
  196 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
  197 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  198 #define MAKEMASK(_m, _s) ((_m) << (_s))
  199 
  200 #define LIST_HEAD_TYPE ice_list_head
  201 #define LIST_ENTRY_TYPE ice_list_node
  202 
  203 /**
  204  * @struct ice_list_node
  205  * @brief simplified linked list node API
  206  *
  207  * Represents a node in a linked list, which can be embedded into a structure
  208  * to allow that structure to be inserted into a linked list. Access to the
  209  * contained structure is done via __containerof
  210  */
  211 struct ice_list_node {
  212         LIST_ENTRY(ice_list_node) entries;
  213 };
  214 
  215 /**
  216  * @struct ice_list_head
  217  * @brief simplified linked list head API
  218  *
  219  * Represents the head of a linked list. The linked list should consist of
  220  * a series of ice_list_node structures embedded into another structure
  221  * accessed using __containerof. This way, the ice_list_head doesn't need to
  222  * know the type of the structure it contains.
  223  */
  224 LIST_HEAD(ice_list_head, ice_list_node);
  225 
  226 #define INIT_LIST_HEAD LIST_INIT
  227 /* LIST_EMPTY doesn't need to be changed */
  228 #define LIST_ADD(entry, head) LIST_INSERT_HEAD(head, entry, entries)
  229 #define LIST_ADD_AFTER(entry, elem) LIST_INSERT_AFTER(elem, entry, entries)
  230 #define LIST_DEL(entry) LIST_REMOVE(entry, entries)
  231 #define _osdep_LIST_ENTRY(ptr, type, member) \
  232         __containerof(ptr, type, member)
  233 #define LIST_FIRST_ENTRY(head, type, member) \
  234         _osdep_LIST_ENTRY(LIST_FIRST(head), type, member)
  235 #define LIST_NEXT_ENTRY(ptr, unused, member) \
  236         _osdep_LIST_ENTRY(LIST_NEXT(&(ptr->member), entries), __typeof(*ptr), member)
  237 #define LIST_REPLACE_INIT(old_head, new_head) do {                      \
  238         __typeof(new_head) _new_head = (new_head);                      \
  239         LIST_INIT(_new_head);                                           \
  240         LIST_SWAP(old_head, _new_head, ice_list_node, entries);         \
  241 } while (0)
  242 
  243 #define LIST_ENTRY_SAFE(_ptr, _type, _member) \
  244 ({ __typeof(_ptr) ____ptr = (_ptr); \
  245    ____ptr ? _osdep_LIST_ENTRY(____ptr, _type, _member) : NULL; \
  246 })
  247 
  248 /**
  249  * ice_get_list_tail - Return the pointer to the last node in the list
  250  * @head: the pointer to the head of the list
  251  *
  252  * A helper function for implementing LIST_ADD_TAIL and LIST_LAST_ENTRY.
  253  * Returns the pointer to the last node in the list, or NULL of the list is
  254  * empty.
  255  *
  256  * Note: due to the list implementation this is O(N), where N is the size of
  257  * the list. An O(1) implementation requires replacing the underlying list
  258  * datastructure with one that has a tail pointer. This is problematic,
  259  * because using a simple TAILQ would require that the addition and deletion
  260  * be given the head of the list.
  261  */
  262 static inline struct ice_list_node *
  263 ice_get_list_tail(struct ice_list_head *head)
  264 {
  265         struct ice_list_node *node = LIST_FIRST(head);
  266 
  267         if (node == NULL)
  268                 return NULL;
  269         while (LIST_NEXT(node, entries) != NULL)
  270                 node = LIST_NEXT(node, entries);
  271 
  272         return node;
  273 }
  274 
  275 /* TODO: This is O(N). An O(1) implementation would require a different
  276  * underlying list structure, such as a circularly linked list. */
  277 #define LIST_ADD_TAIL(entry, head) do {                                 \
  278         struct ice_list_node *node = ice_get_list_tail(head);           \
  279                                                                         \
  280         if (node == NULL) {                                             \
  281                 LIST_ADD(entry, head);                                  \
  282         } else {                                                        \
  283                 LIST_INSERT_AFTER(node, entry, entries);                \
  284         }                                                               \
  285 } while (0)
  286 
  287 #define LIST_LAST_ENTRY(head, type, member) \
  288         LIST_ENTRY_SAFE(ice_get_list_tail(head), type, member)
  289 
  290 #define LIST_FIRST_ENTRY_SAFE(head, type, member) \
  291         LIST_ENTRY_SAFE(LIST_FIRST(head), type, member)
  292 
  293 #define LIST_NEXT_ENTRY_SAFE(ptr, member) \
  294         LIST_ENTRY_SAFE(LIST_NEXT(&(ptr->member), entries), __typeof(*ptr), member)
  295 
  296 #define LIST_FOR_EACH_ENTRY(pos, head, unused, member) \
  297         for (pos = LIST_FIRST_ENTRY_SAFE(head, __typeof(*pos), member);         \
  298             pos;                                                                \
  299             pos = LIST_NEXT_ENTRY_SAFE(pos, member))
  300 
  301 #define LIST_FOR_EACH_ENTRY_SAFE(pos, n, head, unused, member) \
  302         for (pos = LIST_FIRST_ENTRY_SAFE(head, __typeof(*pos), member);         \
  303              pos && ({ n = LIST_NEXT_ENTRY_SAFE(pos, member); 1; });            \
  304              pos = n)
  305 
  306 #define STATIC static
  307 
  308 #define NTOHS ntohs
  309 #define NTOHL ntohl
  310 #define HTONS htons
  311 #define HTONL htonl
  312 #define LE16_TO_CPU le16toh
  313 #define LE32_TO_CPU le32toh
  314 #define LE64_TO_CPU le64toh
  315 #define CPU_TO_LE16 htole16
  316 #define CPU_TO_LE32 htole32
  317 #define CPU_TO_LE64 htole64
  318 #define CPU_TO_BE16 htobe16
  319 #define CPU_TO_BE32 htobe32
  320 
  321 #define SNPRINTF snprintf
  322 
  323 /**
  324  * @typedef u8
  325  * @brief compatibility typedef for uint8_t
  326  */
  327 typedef uint8_t  u8;
  328 
  329 /**
  330  * @typedef u16
  331  * @brief compatibility typedef for uint16_t
  332  */
  333 typedef uint16_t u16;
  334 
  335 /**
  336  * @typedef u32
  337  * @brief compatibility typedef for uint32_t
  338  */
  339 typedef uint32_t u32;
  340 
  341 /**
  342  * @typedef u64
  343  * @brief compatibility typedef for uint64_t
  344  */
  345 typedef uint64_t u64;
  346 
  347 /**
  348  * @typedef s8
  349  * @brief compatibility typedef for int8_t
  350  */
  351 typedef int8_t  s8;
  352 
  353 /**
  354  * @typedef s16
  355  * @brief compatibility typedef for int16_t
  356  */
  357 typedef int16_t s16;
  358 
  359 /**
  360  * @typedef s32
  361  * @brief compatibility typedef for int32_t
  362  */
  363 typedef int32_t s32;
  364 
  365 /**
  366  * @typedef s64
  367  * @brief compatibility typedef for int64_t
  368  */
  369 typedef int64_t s64;
  370 
  371 #define __le16 u16
  372 #define __le32 u32
  373 #define __le64 u64
  374 #define __be16 u16
  375 #define __be32 u32
  376 #define __be64 u64
  377 
  378 #define ice_hweight8(x) bitcount16((u8)x)
  379 #define ice_hweight16(x) bitcount16(x)
  380 #define ice_hweight32(x) bitcount32(x)
  381 #define ice_hweight64(x) bitcount64(x)
  382 
  383 /**
  384  * @struct ice_dma_mem
  385  * @brief DMA memory allocation
  386  *
  387  * Contains DMA allocation bits, used to simplify DMA allocations.
  388  */
  389 struct ice_dma_mem {
  390         void *va;
  391         uint64_t pa;
  392         size_t size;
  393 
  394         bus_dma_tag_t           tag;
  395         bus_dmamap_t            map;
  396         bus_dma_segment_t       seg;
  397 };
  398 
  399 
  400 void * ice_alloc_dma_mem(struct ice_hw *hw, struct ice_dma_mem *mem, u64 size);
  401 void ice_free_dma_mem(struct ice_hw __unused *hw, struct ice_dma_mem *mem);
  402 
  403 /**
  404  * @struct ice_lock
  405  * @brief simplified lock API
  406  *
  407  * Contains a simple lock implementation used to lock various resources.
  408  */
  409 struct ice_lock {
  410         struct mtx mutex;
  411         char name[ICE_STR_BUF_LEN];
  412 };
  413 
  414 extern u16 ice_lock_count;
  415 
  416 /**
  417  * ice_init_lock - Initialize a lock for use
  418  * @lock: the lock memory to initialize
  419  *
  420  * OS compatibility layer to provide a simple locking mechanism. We use
  421  * a mutex for this purpose.
  422  */
  423 static inline void
  424 ice_init_lock(struct ice_lock *lock)
  425 {
  426         /*
  427          * Make each lock unique by incrementing a counter each time this
  428          * function is called. Use of a u16 allows 65535 possible locks before
  429          * we'd hit a duplicate.
  430          */
  431         memset(lock->name, 0, sizeof(lock->name));
  432         snprintf(lock->name, ICE_STR_BUF_LEN, "ice_lock_%u", ice_lock_count++);
  433         mtx_init(&lock->mutex, lock->name, NULL, MTX_DEF);
  434 }
  435 
  436 /**
  437  * ice_acquire_lock - Acquire the lock
  438  * @lock: the lock to acquire
  439  *
  440  * Acquires the mutex specified by the lock pointer.
  441  */
  442 static inline void
  443 ice_acquire_lock(struct ice_lock *lock)
  444 {
  445         mtx_lock(&lock->mutex);
  446 }
  447 
  448 /**
  449  * ice_release_lock - Release the lock
  450  * @lock: the lock to release
  451  *
  452  * Releases the mutex specified by the lock pointer.
  453  */
  454 static inline void
  455 ice_release_lock(struct ice_lock *lock)
  456 {
  457         mtx_unlock(&lock->mutex);
  458 }
  459 
  460 /**
  461  * ice_destroy_lock - Destroy the lock to de-allocate it
  462  * @lock: the lock to destroy
  463  *
  464  * Destroys a previously initialized lock. We only do this if the mutex was
  465  * previously initialized.
  466  */
  467 static inline void
  468 ice_destroy_lock(struct ice_lock *lock)
  469 {
  470         if (mtx_initialized(&lock->mutex))
  471                 mtx_destroy(&lock->mutex);
  472         memset(lock->name, 0, sizeof(lock->name));
  473 }
  474 
  475 /* Some function parameters are unused outside of MPASS/KASSERT macros. Rather
  476  * than marking these as __unused all the time, mark them as __invariant_only,
  477  * and define this to __unused when INVARIANTS is disabled. Otherwise, define
  478  * it empty so that __invariant_only parameters are caught as unused by the
  479  * INVARIANTS build.
  480  */
  481 #ifndef INVARIANTS
  482 #define __invariant_only __unused
  483 #else
  484 #define __invariant_only
  485 #endif
  486 
  487 #define __ALWAYS_UNUSED __unused
  488 
  489 /**
  490  * ice_ilog2 - Calculate the integer log base 2 of a 64bit value
  491  * @n: 64bit number
  492  *
  493  * Calculates the integer log base 2 of a 64bit value, rounded down.
  494  *
  495  * @remark The integer log base 2 of zero is technically undefined, but this
  496  * function will return 0 in that case.
  497  *
  498  */
  499 static inline int
  500 ice_ilog2(u64 n) {
  501         if (n == 0)
  502                 return 0;
  503         return flsll(n) - 1;
  504 }
  505 
  506 /**
  507  * ice_is_pow2 - Check if the value is a power of 2
  508  * @n: 64bit number
  509  *
  510  * Check if the given value is a power of 2.
  511  *
  512  * @remark FreeBSD's powerof2 function treats zero as a power of 2, while this
  513  * function does not.
  514  *
  515  * @returns true or false
  516  */
  517 static inline bool
  518 ice_is_pow2(u64 n) {
  519         if (n == 0)
  520                 return false;
  521         return powerof2(n);
  522 }
  523 #endif /* _ICE_OSDEP_H_ */

Cache object: c2c41ce76e4977e72a4707b903bd08a1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.