2 This software is available to you under a choice of one of two
3 licenses. You may choose to be licensed under the terms of the GNU
4 General Public License (GPL) Version 2, available at
5 <http://www.fsf.org/copyleft/gpl.html>, or the OpenIB.org BSD
6 license, available in the LICENSE.TXT file accompanying this
7 software. These details are also available at
8 <http://openib.org/license.html>.
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved.
25 typedef unsigned long MT_offset_t;
26 typedef unsigned long MT_size_t;
27 typedef unsigned char pseudo_bit_t;
33 #define MT_BIT_OFFSET(object_struct,reg_path) \
34 ((MT_offset_t) &( ((struct object_struct *)(0))-> reg_path ))
36 #define MT_BIT_SIZE(object_struct,reg_path) \
37 ((MT_size_t) sizeof( ((struct object_struct *)(0))-> reg_path ))
39 #define MT_BIT_OFFSET_SIZE(object_struct,reg_path) \
40 MT_BIT_OFFSET(object_struct,reg_path),MT_BIT_SIZE(object_struct,reg_path)
42 #define MT_BYTE_OFFSET(object_struct,reg_path) \
43 ((MT_offset_t) (MT_BIT_OFFSET(object_struct,reg_path)/8))
45 #define MT_BYTE_SIZE(object_struct,reg_path) \
46 ((MT_size_t) MT_BIT_SIZE(object_struct,reg_path)/8)
48 #define MT_BYTE_OFFSET_SIZE(object_struct,reg_path) \
49 MT_BYTE_OFFSET(object_struct,reg_path),MT_BYTE_SIZE(object_struct,reg_path)
51 #define MT_STRUCT_SIZE(object_struct) (sizeof(struct object_struct) >> 3)
53 /*****************************************************************************************
54 * Bit manipulation macros
55 *****************************************************************************************/
57 /* MASK generate a bit mask S bits width */
58 #define MASK32(S) ( ((__u32) ~0L) >> (32-(S)) )
61 * BITS generate a bit mask with bits O+S..O set (assumes 32 bit integer).
62 * numbering bits as following: 31........................76543210
64 #define BITS32(O,S) ( MASK32(S) << (O) )
67 * MT_EXTRACT32 macro extracts S bits from (__u32)W with offset O
68 * and shifts them O places to the right (right justifies the field extracted).
70 #define MT_EXTRACT32(W,O,S) ( ((W)>>(O)) & MASK32(S) )
73 * MT_INSERT32 macro inserts S bits with offset O from field F into word W (__u32)
75 #define MT_INSERT32(W,F,O,S) ((W)= ( ( (W) & (~BITS32(O,S)) ) | (((F) & MASK32(S))<<(O)) ))
78 * MT_EXTRACT_ARRAY32 macro is similar to EXTRACT but works on an array of (__u32),
79 * thus offset may be larger than 32 (but not size).
81 #define MT_EXTRACT_ARRAY32(A,O,S) MT_EXTRACT32(((__u32*)A)[O >> 5],(O & MASK32(5)),S)
84 * MT_EXTRACT_ARRAY32_BE macro is similar to EXTRACT but works on an array of (__u32),
85 * thus offset may be larger than 32 (but not size).
89 #define MT_EXTRACT_ARRAY32_BE(A,O,S) MT_EXTRACT32(be32_to_cpu(((__u32*)A)[O >> 5]),(O & MASK32(5)),S)
92 * MT_INSERT_ARRAY32 macro is similar to INSERT but works on an array of (__u32),
93 * thus offset may be larger than 32 (but not size).
95 #define MT_INSERT_ARRAY32(A,F,O,S) MT_INSERT32(((__u32*)A)[O >> 5],F,(O & MASK32(5)),S)
97 #define INS_FLD(src, a, st, fld) MT_INSERT_ARRAY32(a, src, MT_BIT_OFFSET(st, fld), MT_BIT_SIZE(st, fld))
99 #define EX_FLD(a, st, fld) MT_EXTRACT_ARRAY32(a, MT_BIT_OFFSET(st, fld), MT_BIT_SIZE(st, fld))
101 #define EX_FLD_BE(a, st, fld) MT_EXTRACT_ARRAY32_BE(a, MT_BIT_OFFSET(st, fld), MT_BIT_SIZE(st, fld))
103 /* return the address of the dword holding the field
105 buf = pointer to buffer where to place the value
106 st = struct describing the buffer
107 fld = field in the struct where to insert the value */
109 #define FLD_DW_ADDR(buf, st, fld) ((__u32 *)((__u32 *)(buf)+(((__u32)(&(((struct st *)(0))->fld))) >> 5)))
112 val = value to insert
113 buf = pointer to buffer where to place the value
114 st = struct describing the buffer
115 fld = field in the struct where to insert the value */
117 #define INS_FLD_TO_BE(val, buf, st, fld) \
119 *FLD_DW_ADDR(buf, st, fld) = be32_to_cpu(*FLD_DW_ADDR(buf, st, fld)); \
120 INS_FLD(val, buf, st, fld); \
121 *FLD_DW_ADDR(buf, st, fld) = cpu_to_be32(*FLD_DW_ADDR(buf, st, fld)); \
125 #define EX_FLD_FROM_BE(buf, st, fld, type) \
129 *FLD_DW_ADDR(buf, st, fld) = be32_to_cpu(*FLD_DW_ADDR(buf, st, fld)); \
130 field= EX_FLD(buf, st, fld); \
131 *FLD_DW_ADDR(buf, st, fld) = cpu_to_be32(*FLD_DW_ADDR(buf, st, fld)); \
138 /* Remaining code Copyright Fen Systems Ltd. 2007 */
141 * Wrapper structure for pseudo_bit_t structures
143 * This structure provides a wrapper around the autogenerated
144 * pseudo_bit_t structures. It has the correct size, and also
145 * encapsulates type information about the underlying pseudo_bit_t
146 * structure, which allows the MLX_FILL etc. macros to work without
147 * requiring explicit type information.
149 #define MLX_DECLARE_STRUCT( _structure ) \
152 uint8_t bytes[ sizeof ( struct _structure ## _st ) / 8 ]; \
153 uint32_t dwords[ sizeof ( struct _structure ## _st ) / 32 ]; \
154 struct _structure ## _st *dummy[0]; \
158 /** Get pseudo_bit_t structure type from wrapper structure pointer */
159 #define MLX_PSEUDO_STRUCT( _ptr ) \
160 typeof ( *((_ptr)->u.dummy[0]) )
162 /** Bit offset of a field within a pseudo_bit_t structure */
163 #define MLX_BIT_OFFSET( _structure_st, _field ) \
164 offsetof ( _structure_st, _field )
166 /** Dword offset of a field within a pseudo_bit_t structure */
167 #define MLX_DWORD_OFFSET( _structure_st, _field ) \
168 ( MLX_BIT_OFFSET ( _structure_st, _field ) / 32 )
170 /** Dword bit offset of a field within a pseudo_bit_t structure
172 * Yes, using mod-32 would work, but would lose the check for the
173 * error of specifying a mismatched field name and dword index.
175 #define MLX_DWORD_BIT_OFFSET( _structure_st, _index, _field ) \
176 ( MLX_BIT_OFFSET ( _structure_st, _field ) - ( 32 * (_index) ) )
178 /** Bit width of a field within a pseudo_bit_t structure */
179 #define MLX_BIT_WIDTH( _structure_st, _field ) \
180 sizeof ( ( ( _structure_st * ) NULL )->_field )
182 /** Bit mask for a field within a pseudo_bit_t structure */
183 #define MLX_BIT_MASK( _structure_st, _field ) \
184 ( ( ~( ( uint32_t ) 0 ) ) >> \
185 ( 32 - MLX_BIT_WIDTH ( _structure_st, _field ) ) )
188 * Assemble native-endian dword from named fields and values
192 #define MLX_ASSEMBLE_1( _structure_st, _index, _field, _value ) \
193 ( (_value) << MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
195 #define MLX_ASSEMBLE_2( _structure_st, _index, _field, _value, ... ) \
196 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
197 MLX_ASSEMBLE_1 ( _structure_st, _index, __VA_ARGS__ ) )
199 #define MLX_ASSEMBLE_3( _structure_st, _index, _field, _value, ... ) \
200 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
201 MLX_ASSEMBLE_2 ( _structure_st, _index, __VA_ARGS__ ) )
203 #define MLX_ASSEMBLE_4( _structure_st, _index, _field, _value, ... ) \
204 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
205 MLX_ASSEMBLE_3 ( _structure_st, _index, __VA_ARGS__ ) )
208 * Build native-endian (positive) dword bitmasks from named fields
212 #define MLX_MASK_1( _structure_st, _index, _field ) \
213 ( MLX_BIT_MASK ( _structure_st, _field ) << \
214 MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
216 #define MLX_MASK_2( _structure_st, _index, _field, ... ) \
217 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
218 MLX_MASK_1 ( _structure_st, _index, __VA_ARGS__ ) )
220 #define MLX_MASK_3( _structure_st, _index, _field, ... ) \
221 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
222 MLX_MASK_2 ( _structure_st, _index, __VA_ARGS__ ) )
224 #define MLX_MASK_4( _structure_st, _index, _field, ... ) \
225 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
226 MLX_MASK_3 ( _structure_st, _index, __VA_ARGS__ ) )
229 * Populate big-endian dwords from named fields and values
233 #define MLX_FILL( _ptr, _index, _assembled ) \
235 uint32_t *__ptr = &(_ptr)->u.dwords[(_index)]; \
236 uint32_t __assembled = (_assembled); \
237 *__ptr = cpu_to_be32 ( __assembled ); \
240 #define MLX_FILL_1( _ptr, _index, ... ) \
241 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ),\
242 _index, __VA_ARGS__ ) )
244 #define MLX_FILL_2( _ptr, _index, ... ) \
245 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_2 ( MLX_PSEUDO_STRUCT ( _ptr ),\
246 _index, __VA_ARGS__ ) )
248 #define MLX_FILL_3( _ptr, _index, ... ) \
249 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_3 ( MLX_PSEUDO_STRUCT ( _ptr ),\
250 _index, __VA_ARGS__ ) )
252 #define MLX_FILL_4( _ptr, _index, ... ) \
253 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_4 ( MLX_PSEUDO_STRUCT ( _ptr ),\
254 _index, __VA_ARGS__ ) )
258 * Modify big-endian dword using named field and value
262 #define MLX_SET( _ptr, _field, _value ) \
264 unsigned int __index = \
265 MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
266 uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
267 uint32_t __value = be32_to_cpu ( *__ptr ); \
268 __value &= ~( MLX_MASK_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
269 __index, _field ) ); \
270 __value |= MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
271 __index, _field, _value ); \
272 *__ptr = cpu_to_be32 ( __value ); \
276 * Extract value of named field
280 #define MLX_GET( _ptr, _field ) \
282 unsigned int __index = \
283 MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
284 uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
285 uint32_t __value = be32_to_cpu ( *__ptr ); \
287 MLX_DWORD_BIT_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), \
290 MLX_BIT_MASK ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
294 #endif /* __bit_ops_h__ */