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