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., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
20 | */ |
---|
21 | |
---|
22 | FILE_LICENCE ( GPL2_OR_LATER ); |
---|
23 | |
---|
24 | /** |
---|
25 | * @file |
---|
26 | * |
---|
27 | * NetXen bit operations |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | /** Datatype used to represent a bit in the pseudo-structures */ |
---|
32 | typedef unsigned char pseudo_bit_t; |
---|
33 | |
---|
34 | /** |
---|
35 | * Wrapper structure for pseudo_bit_t structures |
---|
36 | * |
---|
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. |
---|
42 | */ |
---|
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; |
---|
49 | |
---|
50 | /** Get pseudo_bit_t structure type from wrapper structure pointer */ |
---|
51 | #define NX_PSEUDO_STRUCT( _ptr ) \ |
---|
52 | typeof ( *((_ptr)->u.dummy[0]) ) |
---|
53 | |
---|
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 ) |
---|
57 | |
---|
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 ) |
---|
61 | |
---|
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 ) |
---|
65 | |
---|
66 | /** Qword bit offset of a field within a pseudo_bit_t structure |
---|
67 | * |
---|
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. |
---|
70 | */ |
---|
71 | #define NX_QWORD_BIT_OFFSET( _ptr, _index, _field ) \ |
---|
72 | ( NX_BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) ) |
---|
73 | |
---|
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 ) ) ) |
---|
78 | |
---|
79 | /* |
---|
80 | * Assemble native-endian qword from named fields and values |
---|
81 | * |
---|
82 | */ |
---|
83 | |
---|
84 | #define NX_ASSEMBLE_1( _ptr, _index, _field, _value ) \ |
---|
85 | ( ( ( uint64_t) (_value) ) << \ |
---|
86 | NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) ) |
---|
87 | |
---|
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__ ) ) |
---|
91 | |
---|
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__ ) ) |
---|
95 | |
---|
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__ ) ) |
---|
99 | |
---|
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__ ) ) |
---|
103 | |
---|
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__ ) ) |
---|
107 | |
---|
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__ ) ) |
---|
111 | |
---|
112 | /* |
---|
113 | * Build native-endian (positive) qword bitmasks from named fields |
---|
114 | * |
---|
115 | */ |
---|
116 | |
---|
117 | #define NX_MASK_1( _ptr, _index, _field ) \ |
---|
118 | ( NX_BIT_MASK ( _ptr, _field ) << \ |
---|
119 | NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) ) |
---|
120 | |
---|
121 | #define NX_MASK_2( _ptr, _index, _field, ... ) \ |
---|
122 | ( NX_MASK_1 ( _ptr, _index, _field ) | \ |
---|
123 | NX_MASK_1 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
124 | |
---|
125 | #define NX_MASK_3( _ptr, _index, _field, ... ) \ |
---|
126 | ( NX_MASK_1 ( _ptr, _index, _field ) | \ |
---|
127 | NX_MASK_2 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
128 | |
---|
129 | #define NX_MASK_4( _ptr, _index, _field, ... ) \ |
---|
130 | ( NX_MASK_1 ( _ptr, _index, _field ) | \ |
---|
131 | NX_MASK_3 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
132 | |
---|
133 | #define NX_MASK_5( _ptr, _index, _field, ... ) \ |
---|
134 | ( NX_MASK_1 ( _ptr, _index, _field ) | \ |
---|
135 | NX_MASK_4 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
136 | |
---|
137 | #define NX_MASK_6( _ptr, _index, _field, ... ) \ |
---|
138 | ( NX_MASK_1 ( _ptr, _index, _field ) | \ |
---|
139 | NX_MASK_5 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
140 | |
---|
141 | #define NX_MASK_7( _ptr, _index, _field, ... ) \ |
---|
142 | ( NX_MASK_1 ( _ptr, _index, _field ) | \ |
---|
143 | NX_MASK_6 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
144 | |
---|
145 | /* |
---|
146 | * Populate big-endian qwords from named fields and values |
---|
147 | * |
---|
148 | */ |
---|
149 | |
---|
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 ) |
---|
156 | |
---|
157 | #define NX_FILL_1( _ptr, _index, ... ) \ |
---|
158 | NX_FILL ( _ptr, _index, NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
159 | |
---|
160 | #define NX_FILL_2( _ptr, _index, ... ) \ |
---|
161 | NX_FILL ( _ptr, _index, NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
162 | |
---|
163 | #define NX_FILL_3( _ptr, _index, ... ) \ |
---|
164 | NX_FILL ( _ptr, _index, NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
165 | |
---|
166 | #define NX_FILL_4( _ptr, _index, ... ) \ |
---|
167 | NX_FILL ( _ptr, _index, NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
168 | |
---|
169 | #define NX_FILL_5( _ptr, _index, ... ) \ |
---|
170 | NX_FILL ( _ptr, _index, NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
171 | |
---|
172 | #define NX_FILL_6( _ptr, _index, ... ) \ |
---|
173 | NX_FILL ( _ptr, _index, NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
174 | |
---|
175 | #define NX_FILL_7( _ptr, _index, ... ) \ |
---|
176 | NX_FILL ( _ptr, _index, NX_ASSEMBLE_7 ( _ptr, _index, __VA_ARGS__ ) ) |
---|
177 | |
---|
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; \ |
---|
188 | } ) |
---|
189 | |
---|
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 ) ) |
---|
193 | |
---|
194 | #endif /* _NX_BITOPS_H */ |
---|