1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Emulex. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Driver Utility function prototypes 29 */ 30 31 #ifndef _OCE_UTILS_H_ 32 #define _OCE_UTILS_H_ 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 #include <sys/types.h> 39 #include <sys/list.h> 40 41 /* logging macro */ 42 #define MOD_CONFIG 0x0001 43 #define MOD_TX 0x0002 44 #define MOD_RX 0x0004 45 #define MOD_ISR 0x0008 46 47 #define OCE_DEFAULT_LOG_SETTINGS (CE_WARN | \ 48 ((MOD_CONFIG | MOD_TX | MOD_RX) << 16)) 49 50 #define OCE_MAX_LOG_SETTINGS (CE_IGNORE | ((MOD_CONFIG | MOD_TX | \ 51 MOD_RX | MOD_ISR) << 16)) 52 53 #define oce_log(dev_p, level, module, fmt, arg...) { \ 54 if (dev_p) { \ 55 if ((dev_p->mod_mask & module) && \ 56 (dev_p->severity < CE_IGNORE) && \ 57 ((uint32_t)level >= dev_p->severity)) \ 58 cmn_err(level, "%s[%d]: " fmt, OCE_MOD_NAME, \ 59 dev_p->dev_id, ## arg); \ 60 } else { \ 61 cmn_err(level, "%s[%d]: " fmt, OCE_MOD_NAME, \ 62 0, ## arg); \ 63 } \ 64 } 65 66 67 /* Time related */ 68 #define OCE_USDELAY(x) drv_usecwait((x)) 69 #define OCE_MSDELAY(x) OCE_USDELAY((x) * 1000) 70 71 /* Misc Macros */ 72 #define OCE_LOG2(x) (highbit((x)) - 1) 73 #define ADDR_LO(addr) (uint32_t)BMASK_32(addr) /* low 32 bits */ 74 #define ADDR_HI(addr) (uint32_t)BMASK_32((addr >> 32)) /* high 32 bits */ 75 #define ADDR_64(_HI, _LO) ((uint64_t)(((uint64_t)(_HI) << 32)|(_LO))) 76 #define voidptr(x) (void *)((x)) 77 #define u32ptr(x) (uint32_t *)voidptr((x)) 78 #define ptrtou32(x) (uint32_t)((uint32_t *)(void *)(x)) 79 80 #define PAGE_4K (0x1UL << 12) 81 #define OFFSET_IN_4K_PAGE(addr) ((off_t)((uint64_t)addr & (PAGE_4K - 1))) 82 #define OCE_NUM_PAGES(size) howmany(size, PAGE_4K) 83 84 #ifdef OCE_DEBUG 85 #define OCE_DUMP(buf, len) { \ 86 int i = 0; \ 87 uint32_t *p = u32ptr(buf); \ 88 for (i = 0; i < len/4; i++) \ 89 cmn_err(CE_CONT, "[%d] 0x%x", i, p[i]); \ 90 } 91 #endif 92 93 /* Utility Functions */ 94 95 #define OCE_DW_SWAP(datap, length) { \ 96 int len; \ 97 uint32_t *wptr = (uint32_t *)(datap); \ 98 len = (length) + (((length) %4) ? (4 - (4 %(length))) : 0); \ 99 for (len = len/4; len > 0; len--) { \ 100 *wptr = LE_32(*wptr); \ 101 wptr++; \ 102 } \ 103 } 104 105 106 #ifdef _BIG_ENDIAN 107 #define DW_SWAP(_PTR, _LEN) OCE_DW_SWAP(_PTR, _LEN) 108 #else 109 #define DW_SWAP(_PTR, _LEN) 110 #endif 111 112 typedef struct oce_list_entry { 113 struct oce_list_entry *next; 114 struct oce_list_entry *prev; 115 }OCE_LIST_NODE_T; 116 117 typedef struct { 118 kmutex_t list_lock; 119 OCE_LIST_NODE_T head; 120 int32_t nitems; 121 }OCE_LIST_T; 122 123 /* externs for list manipulation functions */ 124 125 126 void oce_list_link_init(OCE_LIST_NODE_T *list_node); 127 void oce_list_create(OCE_LIST_T *list_hdr, void *arg); 128 void oce_list_destroy(OCE_LIST_T *list_hdr); 129 void oce_list_insert_tail(OCE_LIST_T *list_hdr, OCE_LIST_NODE_T *list_node); 130 void *oce_list_remove_head(OCE_LIST_T *list_hdr); 131 void oce_list_remove_node(OCE_LIST_T *list_hdr, OCE_LIST_NODE_T *list_node); 132 boolean_t oce_list_is_empty(OCE_LIST_T *list_hdr); 133 int32_t oce_list_items_avail(OCE_LIST_T *list_hdr); 134 int oce_atomic_reserve(uint32_t *count_p, uint32_t n); 135 136 #define OCE_LIST_CREATE(_LH, _LCK_PRI) oce_list_create((_LH), (_LCK_PRI)) 137 #define OCE_LIST_DESTROY(_LH) oce_list_destroy((_LH)) 138 #define OCE_LIST_INSERT_TAIL(_LH, _N) \ 139 oce_list_insert_tail((_LH), (void *)(_N)) 140 #define OCE_LIST_REM_HEAD(_LH) oce_list_remove_head((_LH)) 141 #define OCE_LIST_EMPTY(_LH) oce_list_is_empty((_LH)) 142 #define OCE_LIST_REMOVE(_LH, _N) \ 143 oce_list_remove_node((_LH), (void *)(_N)) 144 #define OCE_LIST_SIZE(_LH) oce_list_items_avail((_LH)) 145 #define OCE_LIST_LINK_INIT(_N) oce_list_link_init(_N) 146 147 void oce_gen_hkey(char *hkey, int key_size); 148 149 #ifdef __cplusplus 150 } 151 #endif 152 153 #endif /* _OCE_UTILS_H_ */ 154