5 * Copyright (C) 2008 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 );
35 /* Endianness selection.
37 * This is a property of the NIC, not a property of the host CPU.
39 #ifdef BITOPS_LITTLE_ENDIAN
40 #define cpu_to_BIT64 cpu_to_le64
41 #define cpu_to_BIT32 cpu_to_le32
42 #define BIT64_to_cpu le64_to_cpu
43 #define BIT32_to_cpu le32_to_cpu
45 #ifdef BITOPS_BIG_ENDIAN
46 #define cpu_to_BIT64 cpu_to_be64
47 #define cpu_to_BIT32 cpu_to_be32
48 #define BIT64_to_cpu be64_to_cpu
49 #define BIT32_to_cpu be32_to_cpu
52 /** Datatype used to represent a bit in the pseudo-structures */
53 typedef unsigned char pseudo_bit_t;
56 * Wrapper structure for pseudo_bit_t structures
58 * This structure provides a wrapper around pseudo_bit_t structures.
59 * It has the correct size, and also encapsulates type information
60 * about the underlying pseudo_bit_t-based structure, which allows the
61 * BIT_FILL() etc. macros to work without requiring explicit type
64 #define PSEUDO_BIT_STRUCT( _structure ) \
66 uint8_t bytes[ sizeof ( _structure ) / 8 ]; \
67 uint32_t dwords[ sizeof ( _structure ) / 32 ]; \
68 uint64_t qwords[ sizeof ( _structure ) / 64 ]; \
69 _structure *dummy[0]; \
70 } __attribute__ (( packed )) u
72 /** Get pseudo_bit_t structure type from wrapper structure pointer */
73 #define PSEUDO_BIT_STRUCT_TYPE( _ptr ) \
74 typeof ( *((_ptr)->u.dummy[0]) )
76 /** Bit offset of a field within a pseudo_bit_t structure */
77 #define BIT_OFFSET( _ptr, _field ) \
78 offsetof ( PSEUDO_BIT_STRUCT_TYPE ( _ptr ), _field )
80 /** Bit width of a field within a pseudo_bit_t structure */
81 #define BIT_WIDTH( _ptr, _field ) \
82 sizeof ( ( ( PSEUDO_BIT_STRUCT_TYPE ( _ptr ) * ) NULL )->_field )
84 /** Qword offset of a field within a pseudo_bit_t structure */
85 #define QWORD_OFFSET( _ptr, _field ) \
86 ( BIT_OFFSET ( _ptr, _field ) / 64 )
88 /** Qword bit offset of a field within a pseudo_bit_t structure */
89 #define QWORD_BIT_OFFSET( _ptr, _index, _field ) \
90 ( BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) )
92 /** Bit mask for a field within a pseudo_bit_t structure */
93 #define BIT_MASK( _ptr, _field ) \
94 ( ( ~( ( uint64_t ) 0 ) ) >> \
95 ( 64 - BIT_WIDTH ( _ptr, _field ) ) )
98 * Assemble native-endian qword from named fields and values
102 #define BIT_ASSEMBLE_1( _ptr, _index, _field, _value ) \
103 ( ( ( uint64_t) (_value) ) << \
104 QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
106 #define BIT_ASSEMBLE_2( _ptr, _index, _field, _value, ... ) \
107 ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
108 BIT_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
110 #define BIT_ASSEMBLE_3( _ptr, _index, _field, _value, ... ) \
111 ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
112 BIT_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
114 #define BIT_ASSEMBLE_4( _ptr, _index, _field, _value, ... ) \
115 ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
116 BIT_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
118 #define BIT_ASSEMBLE_5( _ptr, _index, _field, _value, ... ) \
119 ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
120 BIT_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
122 #define BIT_ASSEMBLE_6( _ptr, _index, _field, _value, ... ) \
123 ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
124 BIT_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
126 #define BIT_ASSEMBLE_7( _ptr, _index, _field, _value, ... ) \
127 ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
128 BIT_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
131 * Build native-endian (positive) qword bitmasks from named fields
135 #define BIT_MASK_1( _ptr, _index, _field ) \
136 ( BIT_MASK ( _ptr, _field ) << \
137 QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
139 #define BIT_MASK_2( _ptr, _index, _field, ... ) \
140 ( BIT_MASK_1 ( _ptr, _index, _field ) | \
141 BIT_MASK_1 ( _ptr, _index, __VA_ARGS__ ) )
143 #define BIT_MASK_3( _ptr, _index, _field, ... ) \
144 ( BIT_MASK_1 ( _ptr, _index, _field ) | \
145 BIT_MASK_2 ( _ptr, _index, __VA_ARGS__ ) )
147 #define BIT_MASK_4( _ptr, _index, _field, ... ) \
148 ( BIT_MASK_1 ( _ptr, _index, _field ) | \
149 BIT_MASK_3 ( _ptr, _index, __VA_ARGS__ ) )
151 #define BIT_MASK_5( _ptr, _index, _field, ... ) \
152 ( BIT_MASK_1 ( _ptr, _index, _field ) | \
153 BIT_MASK_4 ( _ptr, _index, __VA_ARGS__ ) )
155 #define BIT_MASK_6( _ptr, _index, _field, ... ) \
156 ( BIT_MASK_1 ( _ptr, _index, _field ) | \
157 BIT_MASK_5 ( _ptr, _index, __VA_ARGS__ ) )
159 #define BIT_MASK_7( _ptr, _index, _field, ... ) \
160 ( BIT_MASK_1 ( _ptr, _index, _field ) | \
161 BIT_MASK_6 ( _ptr, _index, __VA_ARGS__ ) )
164 * Populate little-endian qwords from named fields and values
168 #define BIT_FILL( _ptr, _index, _assembled ) do { \
169 uint64_t *__ptr = &(_ptr)->u.qwords[(_index)]; \
170 uint64_t __assembled = (_assembled); \
171 *__ptr = cpu_to_BIT64 ( __assembled ); \
174 #define BIT_FILL_1( _ptr, _field1, ... ) \
175 BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
176 BIT_ASSEMBLE_1 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
177 _field1, __VA_ARGS__ ) )
179 #define BIT_FILL_2( _ptr, _field1, ... ) \
180 BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
181 BIT_ASSEMBLE_2 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
182 _field1, __VA_ARGS__ ) )
184 #define BIT_FILL_3( _ptr, _field1, ... ) \
185 BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
186 BIT_ASSEMBLE_3 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
187 _field1, __VA_ARGS__ ) )
189 #define BIT_FILL_4( _ptr, _field1, ... ) \
190 BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
191 BIT_ASSEMBLE_4 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
192 _field1, __VA_ARGS__ ) )
194 #define BIT_FILL_5( _ptr, _field1, ... ) \
195 BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
196 BIT_ASSEMBLE_5 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
197 _field1, __VA_ARGS__ ) )
199 #define BIT_FILL_6( _ptr, _field1, ... ) \
200 BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
201 BIT_ASSEMBLE_6 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ), \
202 _field1, __VA_ARGS__ ) )
204 /** Extract value of named field */
205 #define BIT_GET64( _ptr, _field ) \
207 unsigned int __index = QWORD_OFFSET ( _ptr, _field ); \
208 uint64_t *__ptr = &(_ptr)->u.qwords[__index]; \
209 uint64_t __value = BIT64_to_cpu ( *__ptr ); \
211 QWORD_BIT_OFFSET ( _ptr, __index, _field ); \
212 __value &= BIT_MASK ( _ptr, _field ); \
216 /** Extract value of named field (for fields up to the size of a long) */
217 #define BIT_GET( _ptr, _field ) \
218 ( ( unsigned long ) BIT_GET64 ( _ptr, _field ) )
220 #define BIT_SET( _ptr, _field, _value ) do { \
221 unsigned int __index = QWORD_OFFSET ( _ptr, _field ); \
222 uint64_t *__ptr = &(_ptr)->u.qwords[__index]; \
223 unsigned int __shift = \
224 QWORD_BIT_OFFSET ( _ptr, __index, _field ); \
225 uint64_t __value = (_value); \
226 *__ptr &= cpu_to_BIT64 ( ~( BIT_MASK ( _ptr, _field ) << \
228 *__ptr |= cpu_to_BIT64 ( __value << __shift ); \
231 #endif /* _IPXE_BITOPS_H */