Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / phantom / nx_bitops.h
1 #ifndef _NX_BITOPS_H
2 #define _NX_BITOPS_H
3
4 /*
5  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
6  *
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.
11  *
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.
16  *
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
20  * 02110-1301, USA.
21  */
22
23 FILE_LICENCE ( GPL2_OR_LATER );
24
25 /**
26  * @file
27  *
28  * NetXen bit operations
29  *
30  */
31
32 /** Datatype used to represent a bit in the pseudo-structures */
33 typedef unsigned char pseudo_bit_t;
34
35 /**
36  * Wrapper structure for pseudo_bit_t structures
37  *
38  * This structure provides a wrapper around pseudo_bit_t structures.
39  * It has the correct size, and also encapsulates type information
40  * about the underlying pseudo_bit_t-based structure, which allows the
41  * NX_FILL etc. macros to work without requiring explicit type
42  * information.
43  */
44 #define NX_PSEUDO_BIT_STRUCT( _structure )                                   \
45         union {                                                              \
46                 uint8_t bytes[ sizeof ( _structure ) / 8 ];                  \
47                 uint64_t qwords[ sizeof ( _structure ) / 64 ];               \
48                 _structure *dummy[0];                                        \
49         } u;
50
51 /** Get pseudo_bit_t structure type from wrapper structure pointer */
52 #define NX_PSEUDO_STRUCT( _ptr )                                             \
53         typeof ( *((_ptr)->u.dummy[0]) )
54
55 /** Bit offset of a field within a pseudo_bit_t structure */
56 #define NX_BIT_OFFSET( _ptr, _field )                                        \
57         offsetof ( NX_PSEUDO_STRUCT ( _ptr ), _field )
58
59 /** Bit width of a field within a pseudo_bit_t structure */
60 #define NX_BIT_WIDTH( _ptr, _field )                                         \
61         sizeof ( ( ( NX_PSEUDO_STRUCT ( _ptr ) * ) NULL )->_field )
62
63 /** Qword offset of a field within a pseudo_bit_t structure */
64 #define NX_QWORD_OFFSET( _ptr, _field )                                      \
65         ( NX_BIT_OFFSET ( _ptr, _field ) / 64 )
66
67 /** Qword bit offset of a field within a pseudo_bit_t structure
68  *
69  * Yes, using mod-64 would work, but would lose the check for the
70  * error of specifying a mismatched field name and qword index.
71  */
72 #define NX_QWORD_BIT_OFFSET( _ptr, _index, _field )                          \
73         ( NX_BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) )
74
75 /** Bit mask for a field within a pseudo_bit_t structure */
76 #define NX_BIT_MASK( _ptr, _field )                                          \
77         ( ( ~( ( uint64_t ) 0 ) ) >>                                         \
78           ( 64 - NX_BIT_WIDTH ( _ptr, _field ) ) )
79
80 /*
81  * Assemble native-endian qword from named fields and values
82  *
83  */
84
85 #define NX_ASSEMBLE_1( _ptr, _index, _field, _value )                        \
86         ( ( ( uint64_t) (_value) ) <<                                        \
87           NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
88
89 #define NX_ASSEMBLE_2( _ptr, _index, _field, _value, ... )                   \
90         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
91           NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
92
93 #define NX_ASSEMBLE_3( _ptr, _index, _field, _value, ... )                   \
94         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
95           NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
96
97 #define NX_ASSEMBLE_4( _ptr, _index, _field, _value, ... )                   \
98         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
99           NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
100
101 #define NX_ASSEMBLE_5( _ptr, _index, _field, _value, ... )                   \
102         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
103           NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
104
105 #define NX_ASSEMBLE_6( _ptr, _index, _field, _value, ... )                   \
106         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
107           NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
108
109 #define NX_ASSEMBLE_7( _ptr, _index, _field, _value, ... )                   \
110         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
111           NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
112
113 /*
114  * Build native-endian (positive) qword bitmasks from named fields
115  *
116  */
117
118 #define NX_MASK_1( _ptr, _index, _field )                            \
119         ( NX_BIT_MASK ( _ptr, _field ) <<                            \
120           NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
121
122 #define NX_MASK_2( _ptr, _index, _field, ... )                       \
123         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
124           NX_MASK_1 ( _ptr, _index, __VA_ARGS__ ) )
125
126 #define NX_MASK_3( _ptr, _index, _field, ... )                       \
127         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
128           NX_MASK_2 ( _ptr, _index, __VA_ARGS__ ) )
129
130 #define NX_MASK_4( _ptr, _index, _field, ... )                       \
131         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
132           NX_MASK_3 ( _ptr, _index, __VA_ARGS__ ) )
133
134 #define NX_MASK_5( _ptr, _index, _field, ... )                       \
135         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
136           NX_MASK_4 ( _ptr, _index, __VA_ARGS__ ) )
137
138 #define NX_MASK_6( _ptr, _index, _field, ... )                       \
139         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
140           NX_MASK_5 ( _ptr, _index, __VA_ARGS__ ) )
141
142 #define NX_MASK_7( _ptr, _index, _field, ... )                       \
143         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
144           NX_MASK_6 ( _ptr, _index, __VA_ARGS__ ) )
145
146 /*
147  * Populate big-endian qwords from named fields and values
148  *
149  */
150
151 #define NX_FILL( _ptr, _index, _assembled )                                  \
152         do {                                                                 \
153                 uint64_t *__ptr = &(_ptr)->u.qwords[(_index)];               \
154                 uint64_t __assembled = (_assembled);                         \
155                 *__ptr = cpu_to_le64 ( __assembled );                        \
156         } while ( 0 )
157
158 #define NX_FILL_1( _ptr, _index, ... )                                       \
159         NX_FILL ( _ptr, _index, NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
160
161 #define NX_FILL_2( _ptr, _index, ... )                                       \
162         NX_FILL ( _ptr, _index, NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
163
164 #define NX_FILL_3( _ptr, _index, ... )                                       \
165         NX_FILL ( _ptr, _index, NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
166
167 #define NX_FILL_4( _ptr, _index, ... )                                       \
168         NX_FILL ( _ptr, _index, NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
169
170 #define NX_FILL_5( _ptr, _index, ... )                                       \
171         NX_FILL ( _ptr, _index, NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
172
173 #define NX_FILL_6( _ptr, _index, ... )                                       \
174         NX_FILL ( _ptr, _index, NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
175
176 #define NX_FILL_7( _ptr, _index, ... )                                       \
177         NX_FILL ( _ptr, _index, NX_ASSEMBLE_7 ( _ptr, _index, __VA_ARGS__ ) )
178
179 /** Extract value of named field */
180 #define NX_GET64( _ptr, _field )                                             \
181         ( {                                                                  \
182                 unsigned int __index = NX_QWORD_OFFSET ( _ptr, _field );     \
183                 uint64_t *__ptr = &(_ptr)->u.qwords[__index];                \
184                 uint64_t __value = le64_to_cpu ( *__ptr );                   \
185                 __value >>=                                                  \
186                     NX_QWORD_BIT_OFFSET ( _ptr, __index, _field );           \
187                 __value &= NX_BIT_MASK ( _ptr, _field );                     \
188                 __value;                                                     \
189         } )
190
191 /** Extract value of named field (for fields up to the size of a long) */
192 #define NX_GET( _ptr, _field )                                               \
193         ( ( unsigned long ) NX_GET64 ( _ptr, _field ) )
194
195 #endif /* _NX_BITOPS_H */