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 /* Copyright © 2003-2011 Emulex. All rights reserved.  */
23 
24 /*
25  * Driver Utility  function prototypes
26  */
27 
28 #ifndef _OCE_UTILS_H_
29 #define	_OCE_UTILS_H_
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #include <sys/types.h>
36 #include <sys/list.h>
37 
38 /* logging macro */
39 #define	MOD_CONFIG	0x0001
40 #define	MOD_TX		0x0002
41 #define	MOD_RX		0x0004
42 #define	MOD_ISR		0x0008
43 
44 #define	OCE_DEFAULT_LOG_SETTINGS	(CE_WARN	|	\
45 					((MOD_CONFIG | MOD_TX | MOD_RX) << 16))
46 
47 #define	OCE_MAX_LOG_SETTINGS		(CE_IGNORE | ((MOD_CONFIG | MOD_TX | \
48 					MOD_RX | MOD_ISR) << 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]: " fmt, OCE_MOD_NAME,	\
56 			    dev_p->dev_id, ## arg);			\
57 	} else {							\
58 		cmn_err(level, "%s[%d]: " fmt, OCE_MOD_NAME,		\
59 		    0, ## 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 	uint32_t *wptr = (uint32_t *)(datap);                 \
95 	len = (length) + (((length)  %4) ? (4  - (4 %(length))) : 0); \
96 	for (len = len/4; len > 0; len--) {		\
97 		*wptr = LE_32(*wptr);			\
98 		wptr++;	                    \
99 	}							        \
100 }
101 
102 
103 #ifdef _BIG_ENDIAN
104 #define	DW_SWAP(_PTR, _LEN) OCE_DW_SWAP(_PTR, _LEN)
105 #else
106 #define	DW_SWAP(_PTR, _LEN)
107 #endif
108 
109 typedef struct oce_list_entry {
110 	struct oce_list_entry *next;
111 	struct oce_list_entry *prev;
112 }OCE_LIST_NODE_T;
113 
114 typedef struct {
115 	kmutex_t list_lock;
116 	OCE_LIST_NODE_T head;
117 	int32_t nitems;
118 }OCE_LIST_T;
119 
120 /* externs for  list manipulation functions */
121 
122 
123 void oce_list_link_init(OCE_LIST_NODE_T  *list_node);
124 void oce_list_create(OCE_LIST_T  *list_hdr, void *arg);
125 void oce_list_destroy(OCE_LIST_T *list_hdr);
126 void oce_list_insert_tail(OCE_LIST_T *list_hdr, OCE_LIST_NODE_T *list_node);
127 void *oce_list_remove_head(OCE_LIST_T  *list_hdr);
128 void oce_list_remove_node(OCE_LIST_T  *list_hdr, OCE_LIST_NODE_T *list_node);
129 boolean_t oce_list_is_empty(OCE_LIST_T *list_hdr);
130 int32_t oce_list_items_avail(OCE_LIST_T *list_hdr);
131 int oce_atomic_reserve(uint32_t *count_p, uint32_t n);
132 
133 #define	OCE_LIST_CREATE(_LH, _LCK_PRI)	oce_list_create((_LH), (_LCK_PRI))
134 #define	OCE_LIST_DESTROY(_LH)		oce_list_destroy((_LH))
135 #define	OCE_LIST_INSERT_TAIL(_LH, _N)				\
136 			oce_list_insert_tail((_LH), (void *)(_N))
137 #define	OCE_LIST_REM_HEAD(_LH)		oce_list_remove_head((_LH))
138 #define	OCE_LIST_EMPTY(_LH)		oce_list_is_empty((_LH))
139 #define	OCE_LIST_REMOVE(_LH, _N)				\
140 			oce_list_remove_node((_LH), (void *)(_N))
141 #define	OCE_LIST_SIZE(_LH)		oce_list_items_avail((_LH))
142 #define	OCE_LIST_LINK_INIT(_N)		oce_list_link_init(_N)
143 
144 void oce_gen_hkey(char *hkey, int key_size);
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif /* _OCE_UTILS_H_ */
151