[igb] Remove __BIG_ENDIAN conditional
[gpxe.git] / src / drivers / net / phantom / nx_bitops.h
blob40686326a6609e134444d7f154c3b4d47a75e2c2
1 #ifndef _NX_BITOPS_H
2 #define _NX_BITOPS_H
4 /*
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 FILE_LICENCE ( GPL2_OR_LATER );
24 /**
25 * @file
27 * NetXen bit operations
31 /** Datatype used to represent a bit in the pseudo-structures */
32 typedef unsigned char pseudo_bit_t;
34 /**
35 * Wrapper structure for pseudo_bit_t structures
37 * This structure provides a wrapper around pseudo_bit_t structures.
38 * It has the correct size, and also encapsulates type information
39 * about the underlying pseudo_bit_t-based structure, which allows the
40 * NX_FILL etc. macros to work without requiring explicit type
41 * information.
43 #define NX_PSEUDO_BIT_STRUCT( _structure ) \
44 union { \
45 uint8_t bytes[ sizeof ( _structure ) / 8 ]; \
46 uint64_t qwords[ sizeof ( _structure ) / 64 ]; \
47 _structure *dummy[0]; \
48 } u;
50 /** Get pseudo_bit_t structure type from wrapper structure pointer */
51 #define NX_PSEUDO_STRUCT( _ptr ) \
52 typeof ( *((_ptr)->u.dummy[0]) )
54 /** Bit offset of a field within a pseudo_bit_t structure */
55 #define NX_BIT_OFFSET( _ptr, _field ) \
56 offsetof ( NX_PSEUDO_STRUCT ( _ptr ), _field )
58 /** Bit width of a field within a pseudo_bit_t structure */
59 #define NX_BIT_WIDTH( _ptr, _field ) \
60 sizeof ( ( ( NX_PSEUDO_STRUCT ( _ptr ) * ) NULL )->_field )
62 /** Qword offset of a field within a pseudo_bit_t structure */
63 #define NX_QWORD_OFFSET( _ptr, _field ) \
64 ( NX_BIT_OFFSET ( _ptr, _field ) / 64 )
66 /** Qword bit offset of a field within a pseudo_bit_t structure
68 * Yes, using mod-64 would work, but would lose the check for the
69 * error of specifying a mismatched field name and qword index.
71 #define NX_QWORD_BIT_OFFSET( _ptr, _index, _field ) \
72 ( NX_BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) )
74 /** Bit mask for a field within a pseudo_bit_t structure */
75 #define NX_BIT_MASK( _ptr, _field ) \
76 ( ( ~( ( uint64_t ) 0 ) ) >> \
77 ( 64 - NX_BIT_WIDTH ( _ptr, _field ) ) )
80 * Assemble native-endian qword from named fields and values
84 #define NX_ASSEMBLE_1( _ptr, _index, _field, _value ) \
85 ( ( ( uint64_t) (_value) ) << \
86 NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
88 #define NX_ASSEMBLE_2( _ptr, _index, _field, _value, ... ) \
89 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
90 NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
92 #define NX_ASSEMBLE_3( _ptr, _index, _field, _value, ... ) \
93 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
94 NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
96 #define NX_ASSEMBLE_4( _ptr, _index, _field, _value, ... ) \
97 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
98 NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
100 #define NX_ASSEMBLE_5( _ptr, _index, _field, _value, ... ) \
101 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
102 NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
104 #define NX_ASSEMBLE_6( _ptr, _index, _field, _value, ... ) \
105 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
106 NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
108 #define NX_ASSEMBLE_7( _ptr, _index, _field, _value, ... ) \
109 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
110 NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
113 * Build native-endian (positive) qword bitmasks from named fields
117 #define NX_MASK_1( _ptr, _index, _field ) \
118 ( NX_BIT_MASK ( _ptr, _field ) << \
119 NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
121 #define NX_MASK_2( _ptr, _index, _field, ... ) \
122 ( NX_MASK_1 ( _ptr, _index, _field ) | \
123 NX_MASK_1 ( _ptr, _index, __VA_ARGS__ ) )
125 #define NX_MASK_3( _ptr, _index, _field, ... ) \
126 ( NX_MASK_1 ( _ptr, _index, _field ) | \
127 NX_MASK_2 ( _ptr, _index, __VA_ARGS__ ) )
129 #define NX_MASK_4( _ptr, _index, _field, ... ) \
130 ( NX_MASK_1 ( _ptr, _index, _field ) | \
131 NX_MASK_3 ( _ptr, _index, __VA_ARGS__ ) )
133 #define NX_MASK_5( _ptr, _index, _field, ... ) \
134 ( NX_MASK_1 ( _ptr, _index, _field ) | \
135 NX_MASK_4 ( _ptr, _index, __VA_ARGS__ ) )
137 #define NX_MASK_6( _ptr, _index, _field, ... ) \
138 ( NX_MASK_1 ( _ptr, _index, _field ) | \
139 NX_MASK_5 ( _ptr, _index, __VA_ARGS__ ) )
141 #define NX_MASK_7( _ptr, _index, _field, ... ) \
142 ( NX_MASK_1 ( _ptr, _index, _field ) | \
143 NX_MASK_6 ( _ptr, _index, __VA_ARGS__ ) )
146 * Populate big-endian qwords from named fields and values
150 #define NX_FILL( _ptr, _index, _assembled ) \
151 do { \
152 uint64_t *__ptr = &(_ptr)->u.qwords[(_index)]; \
153 uint64_t __assembled = (_assembled); \
154 *__ptr = cpu_to_le64 ( __assembled ); \
155 } while ( 0 )
157 #define NX_FILL_1( _ptr, _index, ... ) \
158 NX_FILL ( _ptr, _index, NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
160 #define NX_FILL_2( _ptr, _index, ... ) \
161 NX_FILL ( _ptr, _index, NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
163 #define NX_FILL_3( _ptr, _index, ... ) \
164 NX_FILL ( _ptr, _index, NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
166 #define NX_FILL_4( _ptr, _index, ... ) \
167 NX_FILL ( _ptr, _index, NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
169 #define NX_FILL_5( _ptr, _index, ... ) \
170 NX_FILL ( _ptr, _index, NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
172 #define NX_FILL_6( _ptr, _index, ... ) \
173 NX_FILL ( _ptr, _index, NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
175 #define NX_FILL_7( _ptr, _index, ... ) \
176 NX_FILL ( _ptr, _index, NX_ASSEMBLE_7 ( _ptr, _index, __VA_ARGS__ ) )
178 /** Extract value of named field */
179 #define NX_GET64( _ptr, _field ) \
180 ( { \
181 unsigned int __index = NX_QWORD_OFFSET ( _ptr, _field ); \
182 uint64_t *__ptr = &(_ptr)->u.qwords[__index]; \
183 uint64_t __value = le64_to_cpu ( *__ptr ); \
184 __value >>= \
185 NX_QWORD_BIT_OFFSET ( _ptr, __index, _field ); \
186 __value &= NX_BIT_MASK ( _ptr, _field ); \
187 __value; \
190 /** Extract value of named field (for fields up to the size of a long) */
191 #define NX_GET( _ptr, _field ) \
192 ( ( unsigned long ) NX_GET64 ( _ptr, _field ) )
194 #endif /* _NX_BITOPS_H */