xref: /titanic_51/usr/src/uts/common/io/bnxe/577xx/drivers/common/include/utils.h (revision d14abf155341d55053c76eeec58b787a456b753b)
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  * Copyright 2014 QLogic Corporation
22  * The contents of this file are subject to the terms of the
23  * QLogic End User License (the "License").
24  * You may not use this file except in compliance with the License.
25  *
26  * You can obtain a copy of the License at
27  * http://www.qlogic.com/Resources/Documents/DriverDownloadHelp/
28  * QLogic_End_User_Software_License.txt
29  * See the License for the specific language governing permissions
30  * and limitations under the License.
31  *
32  *
33  * Module Description:
34  *  This file should include pure ANSI C defines
35  *
36  * History:
37  *    04/03/07 Alon Elhanani        Inception.
38  ******************************************************************************/
39 
40 #ifndef __utils_h__
41 #define __utils_h__
42 
43 #ifndef BITS_PER_BYTE
44 #define BITS_PER_BYTE                        (8)
45 #endif //BITS_PER_BYTE
46 /*
47 XXX_FLAGS
48 bitwise flags operations, for readability of the code
49 */
50 
51 // get specific flags
52 #define GET_FLAGS(flags,bits)                   ((flags) & (bits))
53 #define GET_FLAGS_WITH_OFFSET(flags,bits,offset)    (((flags) & (bits)) >> (offset))
54 // set specific flags
55 #define SET_FLAGS(flags,bits)                   ((flags) |= (bits))
56 // reset specific flags
57 #define RESET_FLAGS(flags,bits)                 ((flags) &= ~(bits))
58 // clear flags
59 #define CLEAR_FLAGS(flags)                      ((flags)=0)
60 
61 // macros for a single bit
62 #define SET_BIT( _bits, _val )   SET_FLAGS  ( _bits,   (0x1ULL << _val) )
63 #define RESET_BIT( _bits, _val ) RESET_FLAGS( _bits,   (0x1ULL << _val) )
64 #define GET_BIT( _bits, _val )   GET_FLAGS  ( _bits,   (0x1ULL << _val) )
65 
66 /**
67  * \brief offset of byte next to specified struct member
68  *
69  * Find the size of the structure members, up-to and including
70  * the specified meber (_m).
71  *
72  * \param _s            structure type
73  * \param _m            struct member
74  */
75 
76 #define LAST_BYTE_OF(_s,_m)   (OFFSETOF(_s,_m)+sizeof( ((_s *)0)->_m))
77 
78 /*
79 ARRSIZE:
80 used to calcualte item count of an array
81 this macro is used to prevent compile warning for unreferenced parametes
82 */
83 #ifndef ARRSIZE
84 #define ARRSIZE(a)                   (sizeof(a)/sizeof((a)[0]))
85 #endif // ARRSIZE
86 
87 /*
88 UNREFERENCED_PARAMETER
89 this macro is used to prevent compile warning for unreferenced parametes
90 */
91 #ifndef UNREFERENCED_PARAMETER_
92 #define UNREFERENCED_PARAMETER_(P)\
93     /*lint -save -e527 -e530 */  \
94     { \
95         (P) = (P); \
96     }
97 #endif // UNREFERENCED_PARAMETER_
98 
99 
100 /*
101 ASSERT_STATIC
102 this macro is used to raise COMPILE time assertions
103 e.g: ASSERT_STATIC( sizeof(S08) == 1 )
104 relevant errors that compilers gives in such case:
105 build.exe (MS)     - "error  C2196: case value '0' already used"
106 WMAKE.exe (Watcom) - "Error! E1039: Duplicate case value '0' found"
107 */
108 #ifndef ASSERT_STATIC
109 #define ASSERT_STATIC(cond) \
110     {   \
111         const unsigned char dummy_zero = 0 ; \
112         switch(dummy_zero){case 0:case (cond):;} \
113     }
114 #ifdef __SUNPRO_C /* Sun's cc can't deal with this clever hack */
115 #undef ASSERT_STATIC
116 #define ASSERT_STATIC(cond)
117 #endif
118 #endif // ASSERT_STATIC(cond)
119 
120 /*
121 RANGE
122 this macro is used to check that a certain variable is within a given range
123 e.g: RANGE_INCLUDE(a, 10, 100)    - is the following true : 10<=a<=100
124 */
125 #define IN_RANGE(_var, _min, _max)  ( ((_var) >= (_min)) && ((_var) <= (_max)) )
126 
127 /*
128 IS_DIGIT
129 this macro is used to check that a char is a digit
130 example IS_DIGIT('4') - is '4' a digit will return TRUE.
131 */
132 #ifndef IS_DIGIT
133 #define IS_DIGIT(c) ( ((c) >= '0') && ((c) <= '9') )
134 #endif
135 
136 /*
137 Define standard min and max macros
138 */
139 #ifndef min
140 #define min(a,b) (((a) < (b)) ? (a) : (b))
141 #endif
142 
143 #if defined(__LINUX) || defined(USER_LINUX)
144 #undef max
145 #endif // LINUX
146 
147 #ifndef max
148 #define max(a,b) (((a) > (b)) ? (a) : (b))
149 #endif // !max
150 
151 #if defined(__LINUX) || defined(USER_LINUX)
152 #undef DIV_ROUND_UP_BITS
153 #endif // LINUX
154 // Round up a divied operation more optimal way base on bits.
155 // It is the same opration as d == (1<<bits).
156 // DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
157 #ifndef BITS_ROUND_UP
158 #define DIV_ROUND_UP_BITS(n,bits) (((n) + (1 << (bits)) - 1) >> (bits))
159 #endif
160 /*
161 Define for pragma message output with file name and line number
162 usage: #pragma message (MSGSTR("blah blah blah"))
163 */
164 #define _STR(x) #x
165 #define _STR2(x) _STR(x)
166 #define MSGSTR(msg) __FILE__ "(" _STR2(__LINE__)"): message - " ##msg
167 
168 
169 #ifndef POWER_OF_2
170 // known algorithm
171 #define POWER_OF_2(x)       ((0 != x) && (0 == (x &(x-1))))
172 #endif // !POWER_OF_2
173 
174 
175 
176 #ifndef FAST_PATH_MODULO
177 // a = b (mod n)
178 // If a==b the compiler will omit the last line.
179 #define FAST_PATH_MODULO(a,b,n)     \
180     do                              \
181     {                               \
182         while ((b) > ((n) -1))      \
183             (b) = (b) - (n);        \
184         (a)=(b);                    \
185     }                               \
186     while(0)
187 #endif // !FAST_PATH_MODULO
188 
189 #ifndef MINCHAR
190 #define MINCHAR     0x80
191 #endif
192 #ifndef MAXCHAR
193 #define MAXCHAR     0x7f
194 #endif
195 #ifndef MINSHORT
196 #define MINSHORT    0x8000
197 #endif
198 #ifndef MAXSHORT
199 #define MAXSHORT    0x7fff
200 #endif
201 #ifndef MINLONG
202 #define MINLONG     0x80000000
203 #endif
204 #ifndef MAXLONG
205 #define MAXLONG     0x7fffffff
206 #endif
207 #ifndef MAXBYTE
208 #define MAXBYTE     0xff
209 #endif
210 #ifndef MAXWORD
211 #define MAXWORD     0xffff
212 #endif
213 #ifndef MAXDWORD
214 #define MAXDWORD    0xffffffff
215 #endif
216 
217 #define CLEAR_MSB32(_val32) (_val32 & MAXLONG)
218 
219 // Calculate the size of a field in a structure of type type, without
220 // knowing or stating the type of the field.
221 // based on NTDDK RTL_FIELD_SIZE
222 #ifndef FIELD_SIZE
223 #define FIELD_SIZE(type, field) (sizeof(((type *)0)->field))
224 #endif
225 
226 #endif /* __utils_h__ */
227