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., 51 Franklin Street, Fifth Floor, Boston, MA
23 FILE_LICENCE ( GPL2_OR_LATER );
28 * Mellanox bit operations
32 /* Datatype used to represent a bit in the Mellanox autogenerated headers */
33 typedef unsigned char pseudo_bit_t;
36 * Wrapper structure for pseudo_bit_t structures
38 * This structure provides a wrapper around the autogenerated
39 * pseudo_bit_t structures. It has the correct size, and also
40 * encapsulates type information about the underlying pseudo_bit_t
41 * structure, which allows the MLX_FILL etc. macros to work without
42 * requiring explicit type information.
44 #define MLX_DECLARE_STRUCT( _structure ) \
47 uint8_t bytes[ sizeof ( struct _structure ## _st ) / 8 ]; \
48 uint32_t dwords[ sizeof ( struct _structure ## _st ) / 32 ]; \
49 struct _structure ## _st *dummy[0]; \
50 } __attribute__ (( packed )) u; \
51 } __attribute__ (( packed ))
53 /** Get pseudo_bit_t structure type from wrapper structure pointer */
54 #define MLX_PSEUDO_STRUCT( _ptr ) \
55 typeof ( *((_ptr)->u.dummy[0]) )
57 /** Bit offset of a field within a pseudo_bit_t structure */
58 #define MLX_BIT_OFFSET( _structure_st, _field ) \
59 offsetof ( _structure_st, _field )
61 /** Dword offset of a field within a pseudo_bit_t structure */
62 #define MLX_DWORD_OFFSET( _structure_st, _field ) \
63 ( MLX_BIT_OFFSET ( _structure_st, _field ) / 32 )
65 /** Dword bit offset of a field within a pseudo_bit_t structure
67 * Yes, using mod-32 would work, but would lose the check for the
68 * error of specifying a mismatched field name and dword index.
70 #define MLX_DWORD_BIT_OFFSET( _structure_st, _index, _field ) \
71 ( MLX_BIT_OFFSET ( _structure_st, _field ) - ( 32 * (_index) ) )
73 /** Bit width of a field within a pseudo_bit_t structure */
74 #define MLX_BIT_WIDTH( _structure_st, _field ) \
75 sizeof ( ( ( _structure_st * ) NULL )->_field )
77 /** Bit mask for a field within a pseudo_bit_t structure */
78 #define MLX_BIT_MASK( _structure_st, _field ) \
79 ( ( ~( ( uint32_t ) 0 ) ) >> \
80 ( 32 - MLX_BIT_WIDTH ( _structure_st, _field ) ) )
83 * Assemble native-endian dword from named fields and values
87 #define MLX_ASSEMBLE_1( _structure_st, _index, _field, _value ) \
88 ( (_value) << MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
90 #define MLX_ASSEMBLE_2( _structure_st, _index, _field, _value, ... ) \
91 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
92 MLX_ASSEMBLE_1 ( _structure_st, _index, __VA_ARGS__ ) )
94 #define MLX_ASSEMBLE_3( _structure_st, _index, _field, _value, ... ) \
95 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
96 MLX_ASSEMBLE_2 ( _structure_st, _index, __VA_ARGS__ ) )
98 #define MLX_ASSEMBLE_4( _structure_st, _index, _field, _value, ... ) \
99 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
100 MLX_ASSEMBLE_3 ( _structure_st, _index, __VA_ARGS__ ) )
102 #define MLX_ASSEMBLE_5( _structure_st, _index, _field, _value, ... ) \
103 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
104 MLX_ASSEMBLE_4 ( _structure_st, _index, __VA_ARGS__ ) )
106 #define MLX_ASSEMBLE_6( _structure_st, _index, _field, _value, ... ) \
107 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
108 MLX_ASSEMBLE_5 ( _structure_st, _index, __VA_ARGS__ ) )
110 #define MLX_ASSEMBLE_7( _structure_st, _index, _field, _value, ... ) \
111 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
112 MLX_ASSEMBLE_6 ( _structure_st, _index, __VA_ARGS__ ) )
114 #define MLX_ASSEMBLE_8( _structure_st, _index, _field, _value, ... ) \
115 ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
116 MLX_ASSEMBLE_7 ( _structure_st, _index, __VA_ARGS__ ) )
119 * Build native-endian (positive) dword bitmasks from named fields
123 #define MLX_MASK_1( _structure_st, _index, _field ) \
124 ( MLX_BIT_MASK ( _structure_st, _field ) << \
125 MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
127 #define MLX_MASK_2( _structure_st, _index, _field, ... ) \
128 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
129 MLX_MASK_1 ( _structure_st, _index, __VA_ARGS__ ) )
131 #define MLX_MASK_3( _structure_st, _index, _field, ... ) \
132 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
133 MLX_MASK_2 ( _structure_st, _index, __VA_ARGS__ ) )
135 #define MLX_MASK_4( _structure_st, _index, _field, ... ) \
136 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
137 MLX_MASK_3 ( _structure_st, _index, __VA_ARGS__ ) )
139 #define MLX_MASK_5( _structure_st, _index, _field, ... ) \
140 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
141 MLX_MASK_4 ( _structure_st, _index, __VA_ARGS__ ) )
143 #define MLX_MASK_6( _structure_st, _index, _field, ... ) \
144 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
145 MLX_MASK_5 ( _structure_st, _index, __VA_ARGS__ ) )
147 #define MLX_MASK_7( _structure_st, _index, _field, ... ) \
148 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
149 MLX_MASK_6 ( _structure_st, _index, __VA_ARGS__ ) )
151 #define MLX_MASK_8( _structure_st, _index, _field, ... ) \
152 ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
153 MLX_MASK_7 ( _structure_st, _index, __VA_ARGS__ ) )
156 * Populate big-endian dwords from named fields and values
160 #define MLX_FILL( _ptr, _index, _assembled ) \
162 uint32_t *__ptr = &(_ptr)->u.dwords[(_index)]; \
163 uint32_t __assembled = (_assembled); \
164 *__ptr = cpu_to_be32 ( __assembled ); \
167 #define MLX_FILL_1( _ptr, _index, ... ) \
168 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ),\
169 _index, __VA_ARGS__ ) )
171 #define MLX_FILL_2( _ptr, _index, ... ) \
172 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_2 ( MLX_PSEUDO_STRUCT ( _ptr ),\
173 _index, __VA_ARGS__ ) )
175 #define MLX_FILL_3( _ptr, _index, ... ) \
176 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_3 ( MLX_PSEUDO_STRUCT ( _ptr ),\
177 _index, __VA_ARGS__ ) )
179 #define MLX_FILL_4( _ptr, _index, ... ) \
180 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_4 ( MLX_PSEUDO_STRUCT ( _ptr ),\
181 _index, __VA_ARGS__ ) )
183 #define MLX_FILL_5( _ptr, _index, ... ) \
184 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_5 ( MLX_PSEUDO_STRUCT ( _ptr ),\
185 _index, __VA_ARGS__ ) )
187 #define MLX_FILL_6( _ptr, _index, ... ) \
188 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_6 ( MLX_PSEUDO_STRUCT ( _ptr ),\
189 _index, __VA_ARGS__ ) )
191 #define MLX_FILL_7( _ptr, _index, ... ) \
192 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_7 ( MLX_PSEUDO_STRUCT ( _ptr ),\
193 _index, __VA_ARGS__ ) )
195 #define MLX_FILL_8( _ptr, _index, ... ) \
196 MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_8 ( MLX_PSEUDO_STRUCT ( _ptr ),\
197 _index, __VA_ARGS__ ) )
200 * Modify big-endian dword using named field and value
204 #define MLX_SET( _ptr, _field, _value ) \
206 unsigned int __index = \
207 MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
208 uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
209 uint32_t __value = be32_to_cpu ( *__ptr ); \
210 __value &= ~( MLX_MASK_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
211 __index, _field ) ); \
212 __value |= MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
213 __index, _field, _value ); \
214 *__ptr = cpu_to_be32 ( __value ); \
218 * Extract value of named field
222 #define MLX_GET( _ptr, _field ) \
224 unsigned int __index = \
225 MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
226 uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
227 uint32_t __value = be32_to_cpu ( *__ptr ); \
229 MLX_DWORD_BIT_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), \
232 MLX_BIT_MASK ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
237 * Fill high dword of physical address, if necessary
240 #define MLX_FILL_H( _structure_st, _index, _field, _address ) do { \
241 if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) { \
242 MLX_FILL_1 ( _structure_st, _index, _field, \
243 ( ( ( uint64_t ) (_address) ) >> 32 ) ); \
246 #endif /* _MLX_BITOPS_H */