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_log(dev_p, level, module, fmt, arg...) { \ 51 if (dev_p) { \ 52 if ((dev_p->mod_mask & module) && \ 53 (dev_p->severity < CE_IGNORE) && \ 54 ((uint32_t)level >= dev_p->severity)) \ 55 cmn_err(level, "%s[%d]: %s[%d]: " fmt, OCE_MOD_NAME, \ 56 dev_p->dev_id, __FILE__, __LINE__, ## arg); \ 57 } else { \ 58 cmn_err(level, "%s[%d]: %s[%d]: " fmt, OCE_MOD_NAME, \ 59 0, __FILE__, __LINE__, ## arg); \ 60 } \ 61 } 62 63 64 /* Time related */ 65 #define OCE_USDELAY(x) drv_usecwait((x)) 66 #define OCE_MSDELAY(x) OCE_USDELAY((x) * 1000) 67 68 /* Misc Macros */ 69 #define OCE_LOG2(x) (highbit((x)) - 1) 70 #define ADDR_LO(addr) (uint32_t)BMASK_32(addr) /* low 32 bits */ 71 #define ADDR_HI(addr) (uint32_t)BMASK_32((addr >> 32)) /* high 32 bits */ 72 #define ADDR_64(_HI, _LO) ((uint64_t)(((uint64_t)(_HI) << 32)|(_LO))) 73 #define voidptr(x) (void *)((x)) 74 #define u32ptr(x) (uint32_t *)voidptr((x)) 75 #define ptrtou32(x) (uint32_t)((uint32_t *)(void *)(x)) 76 77 #define PAGE_4K (0x1UL << 12) 78 #define OFFSET_IN_4K_PAGE(addr) ((off_t)((uint64_t)addr & (PAGE_4K - 1))) 79 #define OCE_NUM_PAGES(size) howmany(size, PAGE_4K) 80 81 #ifdef OCE_DEBUG 82 #define OCE_DUMP(buf, len) { \ 83 int i = 0; \ 84 uint32_t *p = u32ptr(buf); \ 85 for (i = 0; i < len/4; i++) \ 86 cmn_err(CE_CONT, "[%d] 0x%x", i, p[i]); \ 87 } 88 #endif 89 90 /* Utility Functions */ 91 92 #define OCE_DW_SWAP(datap, length) { \ 93 int len; \ 94 for (len = ((length)/4 - 1); len >= 0; len--) { \ 95 *((uint32_t *)(datap) + len) = \ 96 LE_32(*((uint32_t *)(datap) + len)); \ 97 } \ 98 } 99 100 101 #ifdef _BIG_ENDIAN 102 #define DW_SWAP(_PTR, _LEN) OCE_DW_SWAP(_PTR, _LEN) 103 #else 104 #define DW_SWAP(_PTR, _LEN) 105 #endif 106 107 typedef struct oce_list_entry { 108 struct oce_list_entry *next; 109 struct oce_list_entry *prev; 110 }OCE_LIST_NODE_T; 111 112 typedef struct { 113 OCE_LIST_NODE_T head; 114 int32_t nitems; 115 kmutex_t list_lock; 116 117 }OCE_LIST_T; 118 119 /* externs for list manipulation functions */ 120 121 void oce_list_create(OCE_LIST_T *list_hdr, void *arg); 122 void oce_list_destroy(OCE_LIST_T *list_hdr); 123 void oce_list_insert_tail(OCE_LIST_T *list_hdr, OCE_LIST_NODE_T *list_node); 124 void *oce_list_remove_head(OCE_LIST_T *list_hdr); 125 void oce_list_remove_node(OCE_LIST_T *list_hdr, OCE_LIST_NODE_T *list_node); 126 boolean_t oce_list_is_empty(OCE_LIST_T *list_hdr); 127 int32_t oce_list_items_avail(OCE_LIST_T *list_hdr); 128 129 #define OCE_LIST_CREATE(_LH, _LCK_PRI) oce_list_create((_LH), (_LCK_PRI)) 130 #define OCE_LIST_DESTROY(_LH) oce_list_destroy((_LH)) 131 #define OCE_LIST_INSERT_TAIL(_LH, _N) \ 132 oce_list_insert_tail((_LH), (void *)(_N)) 133 #define OCE_LIST_REM_HEAD(_LH) oce_list_remove_head((_LH)) 134 #define OCE_LIST_EMPTY(_LH) oce_list_is_empty((_LH)) 135 #define OCE_LIST_REMOVE(_LH, _N) \ 136 oce_list_remove_node((_LH), (void *)(_N)) 137 #define OCE_LIST_SIZE(_LH) oce_list_items_avail((_LH)) 138 139 #ifdef __cplusplus 140 } 141 #endif 142 143 #endif /* _OCE_UTILS_H_ */ 144