5 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 FILE_LICENCE ( GPL2_OR_LATER
);
27 * Mellanox bit operations
31 /* Datatype used to represent a bit in the Mellanox autogenerated headers */
32 typedef unsigned char pseudo_bit_t
;
35 * Wrapper structure for pseudo_bit_t structures
37 * This structure provides a wrapper around the autogenerated
38 * pseudo_bit_t structures. It has the correct size, and also
39 * encapsulates type information about the underlying pseudo_bit_t
40 * structure, which allows the MLX_FILL etc. macros to work without
41 * requiring explicit type information.
43 #define MLX_DECLARE_STRUCT( _structure ) \
46 uint8_t bytes[ sizeof ( struct _structure ## _st ) / 8 ]; \
47 uint32_t dwords[ sizeof ( struct _structure ## _st ) / 32 ]; \
48 struct _structure ## _st *dummy[0]; \
52 /** Get pseudo_bit_t structure type from wrapper structure pointer */
53 #define MLX_PSEUDO_STRUCT( _ptr ) \
54 typeof ( *((_ptr)->u.dummy[0]) )
56 /** Bit offset of a field within a pseudo_bit_t structure */
57 #define MLX_BIT_OFFSET( _structure_st, _field ) \
58 offsetof ( _structure_st, _field )
60 /** Dword offset of a field within a pseudo_bit_t structure */
61 #define MLX_DWORD_OFFSET( _structure_st, _field ) \
62 ( MLX_BIT_OFFSET ( _structure_st, _field ) / 32 )
64 /** Dword bit offset of a field within a pseudo_bit_t structure
66 * Yes, using mod-32 would work, but would lose the check for the
67 * error of specifying a mismatched field name and dword index.
69 #define MLX_DWORD_BIT_OFFSET( _structure_st, _index, _field ) \
70 ( MLX_BIT_OFFSET ( _structure_st, _field ) - ( 32 * (_index) ) )
72 /** Bit width of a field within a pseudo_bit_t structure */
73 #define MLX_BIT_WIDTH( _structure_st, _field ) \
74 sizeof ( ( ( _structure_st * ) NULL )->_field )
76 /** Bit mask for a field within a pseudo_bit_t structure */
77 #define MLX_BIT_MASK( _structure_st, _field ) \
78 ( ( ~( ( uint32_t ) 0 ) ) >> \
79 ( 32 - MLX_BIT_WIDTH ( _structure_st, _field ) ) )
82 * Assemble native-endian dword from named fields and values
86 #define MLX_ASSEMBLE_1( _structure_st, _index, _field, _value ) \
87 ( (_value) << MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
89 #define MLX_ASSEMBLE_2( _structure_st, _index, _field, _value, ... ) \
90 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
91 MLX_ASSEMBLE_1 ( _structure_st, _index, __VA_ARGS__ ) )
93 #define MLX_ASSEMBLE_3( _structure_st, _index, _field, _value, ... ) \
94 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
95 MLX_ASSEMBLE_2 ( _structure_st, _index, __VA_ARGS__ ) )
97 #define MLX_ASSEMBLE_4( _structure_st, _index, _field, _value, ... ) \
98 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
99 MLX_ASSEMBLE_3 ( _structure_st, _index, __VA_ARGS__ ) )
101 #define MLX_ASSEMBLE_5( _structure_st, _index, _field, _value, ... ) \
102 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
103 MLX_ASSEMBLE_4 ( _structure_st, _index, __VA_ARGS__ ) )
105 #define MLX_ASSEMBLE_6( _structure_st, _index, _field, _value, ... ) \
106 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
107 MLX_ASSEMBLE_5 ( _structure_st, _index, __VA_ARGS__ ) )
109 #define MLX_ASSEMBLE_7( _structure_st, _index, _field, _value, ... ) \
110 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
111 MLX_ASSEMBLE_6 ( _structure_st, _index, __VA_ARGS__ ) )
114 * Build native-endian (positive) dword bitmasks from named fields
118 #define MLX_MASK_1( _structure_st, _index, _field ) \
119 ( MLX_BIT_MASK ( _structure_st, _field ) << \
120 MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
122 #define MLX_MASK_2( _structure_st, _index, _field, ... ) \
123 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
124 MLX_MASK_1 ( _structure_st, _index, __VA_ARGS__ ) )
126 #define MLX_MASK_3( _structure_st, _index, _field, ... ) \
127 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
128 MLX_MASK_2 ( _structure_st, _index, __VA_ARGS__ ) )
130 #define MLX_MASK_4( _structure_st, _index, _field, ... ) \
131 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
132 MLX_MASK_3 ( _structure_st, _index, __VA_ARGS__ ) )
134 #define MLX_MASK_5( _structure_st, _index, _field, ... ) \
135 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
136 MLX_MASK_4 ( _structure_st, _index, __VA_ARGS__ ) )
138 #define MLX_MASK_6( _structure_st, _index, _field, ... ) \
139 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
140 MLX_MASK_5 ( _structure_st, _index, __VA_ARGS__ ) )
142 #define MLX_MASK_7( _structure_st, _index, _field, ... ) \
143 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
144 MLX_MASK_6 ( _structure_st, _index, __VA_ARGS__ ) )
147 * Populate big-endian dwords from named fields and values
151 #define MLX_FILL( _ptr, _index, _assembled ) \
153 uint32_t *__ptr = &(_ptr)->u.dwords[(_index)]; \
154 uint32_t __assembled = (_assembled); \
155 *__ptr = cpu_to_be32 ( __assembled ); \
158 #define MLX_FILL_1( _ptr, _index, ... ) \
159 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ),\
160 _index, __VA_ARGS__ ) )
162 #define MLX_FILL_2( _ptr, _index, ... ) \
163 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_2 ( MLX_PSEUDO_STRUCT ( _ptr ),\
164 _index, __VA_ARGS__ ) )
166 #define MLX_FILL_3( _ptr, _index, ... ) \
167 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_3 ( MLX_PSEUDO_STRUCT ( _ptr ),\
168 _index, __VA_ARGS__ ) )
170 #define MLX_FILL_4( _ptr, _index, ... ) \
171 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_4 ( MLX_PSEUDO_STRUCT ( _ptr ),\
172 _index, __VA_ARGS__ ) )
174 #define MLX_FILL_5( _ptr, _index, ... ) \
175 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_5 ( MLX_PSEUDO_STRUCT ( _ptr ),\
176 _index, __VA_ARGS__ ) )
178 #define MLX_FILL_6( _ptr, _index, ... ) \
179 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_6 ( MLX_PSEUDO_STRUCT ( _ptr ),\
180 _index, __VA_ARGS__ ) )
182 #define MLX_FILL_7( _ptr, _index, ... ) \
183 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_7 ( MLX_PSEUDO_STRUCT ( _ptr ),\
184 _index, __VA_ARGS__ ) )
187 * Modify big-endian dword using named field and value
191 #define MLX_SET( _ptr, _field, _value ) \
193 unsigned int __index = \
194 MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
195 uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
196 uint32_t __value = be32_to_cpu ( *__ptr ); \
197 __value &= ~( MLX_MASK_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
198 __index, _field ) ); \
199 __value |= MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
200 __index, _field, _value ); \
201 *__ptr = cpu_to_be32 ( __value ); \
205 * Extract value of named field
209 #define MLX_GET( _ptr, _field ) \
211 unsigned int __index = \
212 MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
213 uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
214 uint32_t __value = be32_to_cpu ( *__ptr ); \
216 MLX_DWORD_BIT_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), \
219 MLX_BIT_MASK ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
223 #endif /* _MLX_BITOPS_H */