1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2006-2013 Solarflare Communications Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation, incorporated herein by reference.
9  */
10 
11 #ifndef EFX_BITFIELD_H
12 #define EFX_BITFIELD_H
13 
14 /*
15  * Efx bitfield access
16  *
17  * Efx NICs make extensive use of bitfields up to 128 bits
18  * wide.  Since there is no native 128-bit datatype on most systems,
19  * and since 64-bit datatypes are inefficient on 32-bit systems and
20  * vice versa, we wrap accesses in a way that uses the most efficient
21  * datatype.
22  *
23  * The NICs are PCI devices and therefore little-endian.  Since most
24  * of the quantities that we deal with are DMAed to/from host memory,
25  * we define our datatypes (efx_oword_t, efx_qword_t and
26  * efx_dword_t) to be little-endian.
27  */
28 
29 /* Lowest bit numbers and widths */
30 #define EFX_DUMMY_FIELD_LBN 0
31 #define EFX_DUMMY_FIELD_WIDTH 0
32 #define EFX_WORD_0_LBN 0
33 #define EFX_WORD_0_WIDTH 16
34 #define EFX_WORD_1_LBN 16
35 #define EFX_WORD_1_WIDTH 16
36 #define EFX_DWORD_0_LBN 0
37 #define EFX_DWORD_0_WIDTH 32
38 #define EFX_DWORD_1_LBN 32
39 #define EFX_DWORD_1_WIDTH 32
40 #define EFX_DWORD_2_LBN 64
41 #define EFX_DWORD_2_WIDTH 32
42 #define EFX_DWORD_3_LBN 96
43 #define EFX_DWORD_3_WIDTH 32
44 #define EFX_QWORD_0_LBN 0
45 #define EFX_QWORD_0_WIDTH 64
46 
47 /* Specified attribute (e.g. LBN) of the specified field */
48 #define EFX_VAL(field, attribute) field ## _ ## attribute
49 /* Low bit number of the specified field */
50 #define EFX_LOW_BIT(field) EFX_VAL(field, LBN)
51 /* Bit width of the specified field */
52 #define EFX_WIDTH(field) EFX_VAL(field, WIDTH)
53 /* High bit number of the specified field */
54 #define EFX_HIGH_BIT(field) (EFX_LOW_BIT(field) + EFX_WIDTH(field) - 1)
55 /* Mask equal in width to the specified field.
56  *
57  * For example, a field with width 5 would have a mask of 0x1f.
58  *
59  * The maximum width mask that can be generated is 64 bits.
60  */
61 #define EFX_MASK64(width)			\
62 	((width) == 64 ? ~((u64) 0) :		\
63 	 (((((u64) 1) << (width))) - 1))
64 
65 /* Mask equal in width to the specified field.
66  *
67  * For example, a field with width 5 would have a mask of 0x1f.
68  *
69  * The maximum width mask that can be generated is 32 bits.  Use
70  * EFX_MASK64 for higher width fields.
71  */
72 #define EFX_MASK32(width)			\
73 	((width) == 32 ? ~((u32) 0) :		\
74 	 (((((u32) 1) << (width))) - 1))
75 
76 /* A doubleword (i.e. 4 byte) datatype - little-endian in HW */
77 typedef union efx_dword {
78 	__le32 u32[1];
79 } efx_dword_t;
80 
81 /* A quadword (i.e. 8 byte) datatype - little-endian in HW */
82 typedef union efx_qword {
83 	__le64 u64[1];
84 	__le32 u32[2];
85 	efx_dword_t dword[2];
86 } efx_qword_t;
87 
88 /* An octword (eight-word, i.e. 16 byte) datatype - little-endian in HW */
89 typedef union efx_oword {
90 	__le64 u64[2];
91 	efx_qword_t qword[2];
92 	__le32 u32[4];
93 	efx_dword_t dword[4];
94 } efx_oword_t;
95 
96 /* Format string and value expanders for printk */
97 #define EFX_DWORD_FMT "%08x"
98 #define EFX_QWORD_FMT "%08x:%08x"
99 #define EFX_OWORD_FMT "%08x:%08x:%08x:%08x"
100 #define EFX_DWORD_VAL(dword)				\
101 	((unsigned int) le32_to_cpu((dword).u32[0]))
102 #define EFX_QWORD_VAL(qword)				\
103 	((unsigned int) le32_to_cpu((qword).u32[1])),	\
104 	((unsigned int) le32_to_cpu((qword).u32[0]))
105 #define EFX_OWORD_VAL(oword)				\
106 	((unsigned int) le32_to_cpu((oword).u32[3])),	\
107 	((unsigned int) le32_to_cpu((oword).u32[2])),	\
108 	((unsigned int) le32_to_cpu((oword).u32[1])),	\
109 	((unsigned int) le32_to_cpu((oword).u32[0]))
110 
111 /*
112  * Extract bit field portion [low,high) from the native-endian element
113  * which contains bits [min,max).
114  *
115  * For example, suppose "element" represents the high 32 bits of a
116  * 64-bit value, and we wish to extract the bits belonging to the bit
117  * field occupying bits 28-45 of this 64-bit value.
118  *
119  * Then EFX_EXTRACT ( element, 32, 63, 28, 45 ) would give
120  *
121  *   ( element ) << 4
122  *
123  * The result will contain the relevant bits filled in in the range
124  * [0,high-low), with garbage in bits [high-low+1,...).
125  */
126 #define EFX_EXTRACT_NATIVE(native_element, min, max, low, high)		\
127 	((low) > (max) || (high) < (min) ? 0 :				\
128 	 (low) > (min) ?						\
129 	 (native_element) >> ((low) - (min)) :				\
130 	 (native_element) << ((min) - (low)))
131 
132 /*
133  * Extract bit field portion [low,high) from the 64-bit little-endian
134  * element which contains bits [min,max)
135  */
136 #define EFX_EXTRACT64(element, min, max, low, high)			\
137 	EFX_EXTRACT_NATIVE(le64_to_cpu(element), min, max, low, high)
138 
139 /*
140  * Extract bit field portion [low,high) from the 32-bit little-endian
141  * element which contains bits [min,max)
142  */
143 #define EFX_EXTRACT32(element, min, max, low, high)			\
144 	EFX_EXTRACT_NATIVE(le32_to_cpu(element), min, max, low, high)
145 
146 #define EFX_EXTRACT_OWORD64(oword, low, high)				\
147 	((EFX_EXTRACT64((oword).u64[0], 0, 63, low, high) |		\
148 	  EFX_EXTRACT64((oword).u64[1], 64, 127, low, high)) &		\
149 	 EFX_MASK64((high) + 1 - (low)))
150 
151 #define EFX_EXTRACT_QWORD64(qword, low, high)				\
152 	(EFX_EXTRACT64((qword).u64[0], 0, 63, low, high) &		\
153 	 EFX_MASK64((high) + 1 - (low)))
154 
155 #define EFX_EXTRACT_OWORD32(oword, low, high)				\
156 	((EFX_EXTRACT32((oword).u32[0], 0, 31, low, high) |		\
157 	  EFX_EXTRACT32((oword).u32[1], 32, 63, low, high) |		\
158 	  EFX_EXTRACT32((oword).u32[2], 64, 95, low, high) |		\
159 	  EFX_EXTRACT32((oword).u32[3], 96, 127, low, high)) &		\
160 	 EFX_MASK32((high) + 1 - (low)))
161 
162 #define EFX_EXTRACT_QWORD32(qword, low, high)				\
163 	((EFX_EXTRACT32((qword).u32[0], 0, 31, low, high) |		\
164 	  EFX_EXTRACT32((qword).u32[1], 32, 63, low, high)) &		\
165 	 EFX_MASK32((high) + 1 - (low)))
166 
167 #define EFX_EXTRACT_DWORD(dword, low, high)			\
168 	(EFX_EXTRACT32((dword).u32[0], 0, 31, low, high) &	\
169 	 EFX_MASK32((high) + 1 - (low)))
170 
171 #define EFX_OWORD_FIELD64(oword, field)				\
172 	EFX_EXTRACT_OWORD64(oword, EFX_LOW_BIT(field),		\
173 			    EFX_HIGH_BIT(field))
174 
175 #define EFX_QWORD_FIELD64(qword, field)				\
176 	EFX_EXTRACT_QWORD64(qword, EFX_LOW_BIT(field),		\
177 			    EFX_HIGH_BIT(field))
178 
179 #define EFX_OWORD_FIELD32(oword, field)				\
180 	EFX_EXTRACT_OWORD32(oword, EFX_LOW_BIT(field),		\
181 			    EFX_HIGH_BIT(field))
182 
183 #define EFX_QWORD_FIELD32(qword, field)				\
184 	EFX_EXTRACT_QWORD32(qword, EFX_LOW_BIT(field),		\
185 			    EFX_HIGH_BIT(field))
186 
187 #define EFX_DWORD_FIELD(dword, field)				\
188 	EFX_EXTRACT_DWORD(dword, EFX_LOW_BIT(field),		\
189 			  EFX_HIGH_BIT(field))
190 
191 #define EFX_OWORD_IS_ZERO64(oword)					\
192 	(((oword).u64[0] | (oword).u64[1]) == (__force __le64) 0)
193 
194 #define EFX_QWORD_IS_ZERO64(qword)					\
195 	(((qword).u64[0]) == (__force __le64) 0)
196 
197 #define EFX_OWORD_IS_ZERO32(oword)					     \
198 	(((oword).u32[0] | (oword).u32[1] | (oword).u32[2] | (oword).u32[3]) \
199 	 == (__force __le32) 0)
200 
201 #define EFX_QWORD_IS_ZERO32(qword)					\
202 	(((qword).u32[0] | (qword).u32[1]) == (__force __le32) 0)
203 
204 #define EFX_DWORD_IS_ZERO(dword)					\
205 	(((dword).u32[0]) == (__force __le32) 0)
206 
207 #define EFX_OWORD_IS_ALL_ONES64(oword)					\
208 	(((oword).u64[0] & (oword).u64[1]) == ~((__force __le64) 0))
209 
210 #define EFX_QWORD_IS_ALL_ONES64(qword)					\
211 	((qword).u64[0] == ~((__force __le64) 0))
212 
213 #define EFX_OWORD_IS_ALL_ONES32(oword)					\
214 	(((oword).u32[0] & (oword).u32[1] & (oword).u32[2] & (oword).u32[3]) \
215 	 == ~((__force __le32) 0))
216 
217 #define EFX_QWORD_IS_ALL_ONES32(qword)					\
218 	(((qword).u32[0] & (qword).u32[1]) == ~((__force __le32) 0))
219 
220 #define EFX_DWORD_IS_ALL_ONES(dword)					\
221 	((dword).u32[0] == ~((__force __le32) 0))
222 
223 #if BITS_PER_LONG == 64
224 #define EFX_OWORD_FIELD		EFX_OWORD_FIELD64
225 #define EFX_QWORD_FIELD		EFX_QWORD_FIELD64
226 #define EFX_OWORD_IS_ZERO	EFX_OWORD_IS_ZERO64
227 #define EFX_QWORD_IS_ZERO	EFX_QWORD_IS_ZERO64
228 #define EFX_OWORD_IS_ALL_ONES	EFX_OWORD_IS_ALL_ONES64
229 #define EFX_QWORD_IS_ALL_ONES	EFX_QWORD_IS_ALL_ONES64
230 #else
231 #define EFX_OWORD_FIELD		EFX_OWORD_FIELD32
232 #define EFX_QWORD_FIELD		EFX_QWORD_FIELD32
233 #define EFX_OWORD_IS_ZERO	EFX_OWORD_IS_ZERO32
234 #define EFX_QWORD_IS_ZERO	EFX_QWORD_IS_ZERO32
235 #define EFX_OWORD_IS_ALL_ONES	EFX_OWORD_IS_ALL_ONES32
236 #define EFX_QWORD_IS_ALL_ONES	EFX_QWORD_IS_ALL_ONES32
237 #endif
238 
239 /*
240  * Construct bit field portion
241  *
242  * Creates the portion of the bit field [low,high) that lies within
243  * the range [min,max).
244  */
245 #define EFX_INSERT_NATIVE64(min, max, low, high, value)		\
246 	(((low > max) || (high < min)) ? 0 :			\
247 	 ((low > min) ?						\
248 	  (((u64) (value)) << (low - min)) :		\
249 	  (((u64) (value)) >> (min - low))))
250 
251 #define EFX_INSERT_NATIVE32(min, max, low, high, value)		\
252 	(((low > max) || (high < min)) ? 0 :			\
253 	 ((low > min) ?						\
254 	  (((u32) (value)) << (low - min)) :		\
255 	  (((u32) (value)) >> (min - low))))
256 
257 #define EFX_INSERT_NATIVE(min, max, low, high, value)		\
258 	((((max - min) >= 32) || ((high - low) >= 32)) ?	\
259 	 EFX_INSERT_NATIVE64(min, max, low, high, value) :	\
260 	 EFX_INSERT_NATIVE32(min, max, low, high, value))
261 
262 /*
263  * Construct bit field portion
264  *
265  * Creates the portion of the named bit field that lies within the
266  * range [min,max).
267  */
268 #define EFX_INSERT_FIELD_NATIVE(min, max, field, value)		\
269 	EFX_INSERT_NATIVE(min, max, EFX_LOW_BIT(field),		\
270 			  EFX_HIGH_BIT(field), value)
271 
272 /*
273  * Construct bit field
274  *
275  * Creates the portion of the named bit fields that lie within the
276  * range [min,max).
277  */
278 #define EFX_INSERT_FIELDS_NATIVE(min, max,				\
279 				 field1, value1,			\
280 				 field2, value2,			\
281 				 field3, value3,			\
282 				 field4, value4,			\
283 				 field5, value5,			\
284 				 field6, value6,			\
285 				 field7, value7,			\
286 				 field8, value8,			\
287 				 field9, value9,			\
288 				 field10, value10)			\
289 	(EFX_INSERT_FIELD_NATIVE((min), (max), field1, (value1)) |	\
290 	 EFX_INSERT_FIELD_NATIVE((min), (max), field2, (value2)) |	\
291 	 EFX_INSERT_FIELD_NATIVE((min), (max), field3, (value3)) |	\
292 	 EFX_INSERT_FIELD_NATIVE((min), (max), field4, (value4)) |	\
293 	 EFX_INSERT_FIELD_NATIVE((min), (max), field5, (value5)) |	\
294 	 EFX_INSERT_FIELD_NATIVE((min), (max), field6, (value6)) |	\
295 	 EFX_INSERT_FIELD_NATIVE((min), (max), field7, (value7)) |	\
296 	 EFX_INSERT_FIELD_NATIVE((min), (max), field8, (value8)) |	\
297 	 EFX_INSERT_FIELD_NATIVE((min), (max), field9, (value9)) |	\
298 	 EFX_INSERT_FIELD_NATIVE((min), (max), field10, (value10)))
299 
300 #define EFX_INSERT_FIELDS64(...)				\
301 	cpu_to_le64(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
302 
303 #define EFX_INSERT_FIELDS32(...)				\
304 	cpu_to_le32(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
305 
306 #define EFX_POPULATE_OWORD64(oword, ...) do {				\
307 	(oword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
308 	(oword).u64[1] = EFX_INSERT_FIELDS64(64, 127, __VA_ARGS__);	\
309 	} while (0)
310 
311 #define EFX_POPULATE_QWORD64(qword, ...) do {				\
312 	(qword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
313 	} while (0)
314 
315 #define EFX_POPULATE_OWORD32(oword, ...) do {				\
316 	(oword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
317 	(oword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
318 	(oword).u32[2] = EFX_INSERT_FIELDS32(64, 95, __VA_ARGS__);	\
319 	(oword).u32[3] = EFX_INSERT_FIELDS32(96, 127, __VA_ARGS__);	\
320 	} while (0)
321 
322 #define EFX_POPULATE_QWORD32(qword, ...) do {				\
323 	(qword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
324 	(qword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
325 	} while (0)
326 
327 #define EFX_POPULATE_DWORD(dword, ...) do {				\
328 	(dword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
329 	} while (0)
330 
331 #if BITS_PER_LONG == 64
332 #define EFX_POPULATE_OWORD EFX_POPULATE_OWORD64
333 #define EFX_POPULATE_QWORD EFX_POPULATE_QWORD64
334 #else
335 #define EFX_POPULATE_OWORD EFX_POPULATE_OWORD32
336 #define EFX_POPULATE_QWORD EFX_POPULATE_QWORD32
337 #endif
338 
339 /* Populate an octword field with various numbers of arguments */
340 #define EFX_POPULATE_OWORD_10 EFX_POPULATE_OWORD
341 #define EFX_POPULATE_OWORD_9(oword, ...) \
342 	EFX_POPULATE_OWORD_10(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
343 #define EFX_POPULATE_OWORD_8(oword, ...) \
344 	EFX_POPULATE_OWORD_9(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
345 #define EFX_POPULATE_OWORD_7(oword, ...) \
346 	EFX_POPULATE_OWORD_8(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
347 #define EFX_POPULATE_OWORD_6(oword, ...) \
348 	EFX_POPULATE_OWORD_7(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
349 #define EFX_POPULATE_OWORD_5(oword, ...) \
350 	EFX_POPULATE_OWORD_6(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
351 #define EFX_POPULATE_OWORD_4(oword, ...) \
352 	EFX_POPULATE_OWORD_5(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
353 #define EFX_POPULATE_OWORD_3(oword, ...) \
354 	EFX_POPULATE_OWORD_4(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
355 #define EFX_POPULATE_OWORD_2(oword, ...) \
356 	EFX_POPULATE_OWORD_3(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
357 #define EFX_POPULATE_OWORD_1(oword, ...) \
358 	EFX_POPULATE_OWORD_2(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
359 #define EFX_ZERO_OWORD(oword) \
360 	EFX_POPULATE_OWORD_1(oword, EFX_DUMMY_FIELD, 0)
361 #define EFX_SET_OWORD(oword) \
362 	EFX_POPULATE_OWORD_4(oword, \
363 			     EFX_DWORD_0, 0xffffffff, \
364 			     EFX_DWORD_1, 0xffffffff, \
365 			     EFX_DWORD_2, 0xffffffff, \
366 			     EFX_DWORD_3, 0xffffffff)
367 
368 /* Populate a quadword field with various numbers of arguments */
369 #define EFX_POPULATE_QWORD_10 EFX_POPULATE_QWORD
370 #define EFX_POPULATE_QWORD_9(qword, ...) \
371 	EFX_POPULATE_QWORD_10(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
372 #define EFX_POPULATE_QWORD_8(qword, ...) \
373 	EFX_POPULATE_QWORD_9(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
374 #define EFX_POPULATE_QWORD_7(qword, ...) \
375 	EFX_POPULATE_QWORD_8(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
376 #define EFX_POPULATE_QWORD_6(qword, ...) \
377 	EFX_POPULATE_QWORD_7(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
378 #define EFX_POPULATE_QWORD_5(qword, ...) \
379 	EFX_POPULATE_QWORD_6(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
380 #define EFX_POPULATE_QWORD_4(qword, ...) \
381 	EFX_POPULATE_QWORD_5(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
382 #define EFX_POPULATE_QWORD_3(qword, ...) \
383 	EFX_POPULATE_QWORD_4(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
384 #define EFX_POPULATE_QWORD_2(qword, ...) \
385 	EFX_POPULATE_QWORD_3(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
386 #define EFX_POPULATE_QWORD_1(qword, ...) \
387 	EFX_POPULATE_QWORD_2(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
388 #define EFX_ZERO_QWORD(qword) \
389 	EFX_POPULATE_QWORD_1(qword, EFX_DUMMY_FIELD, 0)
390 #define EFX_SET_QWORD(qword) \
391 	EFX_POPULATE_QWORD_2(qword, \
392 			     EFX_DWORD_0, 0xffffffff, \
393 			     EFX_DWORD_1, 0xffffffff)
394 
395 /* Populate a dword field with various numbers of arguments */
396 #define EFX_POPULATE_DWORD_10 EFX_POPULATE_DWORD
397 #define EFX_POPULATE_DWORD_9(dword, ...) \
398 	EFX_POPULATE_DWORD_10(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
399 #define EFX_POPULATE_DWORD_8(dword, ...) \
400 	EFX_POPULATE_DWORD_9(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
401 #define EFX_POPULATE_DWORD_7(dword, ...) \
402 	EFX_POPULATE_DWORD_8(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
403 #define EFX_POPULATE_DWORD_6(dword, ...) \
404 	EFX_POPULATE_DWORD_7(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
405 #define EFX_POPULATE_DWORD_5(dword, ...) \
406 	EFX_POPULATE_DWORD_6(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
407 #define EFX_POPULATE_DWORD_4(dword, ...) \
408 	EFX_POPULATE_DWORD_5(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
409 #define EFX_POPULATE_DWORD_3(dword, ...) \
410 	EFX_POPULATE_DWORD_4(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
411 #define EFX_POPULATE_DWORD_2(dword, ...) \
412 	EFX_POPULATE_DWORD_3(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
413 #define EFX_POPULATE_DWORD_1(dword, ...) \
414 	EFX_POPULATE_DWORD_2(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
415 #define EFX_ZERO_DWORD(dword) \
416 	EFX_POPULATE_DWORD_1(dword, EFX_DUMMY_FIELD, 0)
417 #define EFX_SET_DWORD(dword) \
418 	EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 0xffffffff)
419 
420 /*
421  * Modify a named field within an already-populated structure.  Used
422  * for read-modify-write operations.
423  *
424  */
425 #define EFX_INVERT_OWORD(oword) do {		\
426 	(oword).u64[0] = ~((oword).u64[0]);	\
427 	(oword).u64[1] = ~((oword).u64[1]);	\
428 	} while (0)
429 
430 #define EFX_AND_OWORD(oword, from, mask)			\
431 	do {							\
432 		(oword).u64[0] = (from).u64[0] & (mask).u64[0];	\
433 		(oword).u64[1] = (from).u64[1] & (mask).u64[1];	\
434 	} while (0)
435 
436 #define EFX_AND_QWORD(qword, from, mask)			\
437 		(qword).u64[0] = (from).u64[0] & (mask).u64[0]
438 
439 #define EFX_OR_OWORD(oword, from, mask)				\
440 	do {							\
441 		(oword).u64[0] = (from).u64[0] | (mask).u64[0];	\
442 		(oword).u64[1] = (from).u64[1] | (mask).u64[1];	\
443 	} while (0)
444 
445 #define EFX_INSERT64(min, max, low, high, value)			\
446 	cpu_to_le64(EFX_INSERT_NATIVE(min, max, low, high, value))
447 
448 #define EFX_INSERT32(min, max, low, high, value)			\
449 	cpu_to_le32(EFX_INSERT_NATIVE(min, max, low, high, value))
450 
451 #define EFX_INPLACE_MASK64(min, max, low, high)				\
452 	EFX_INSERT64(min, max, low, high, EFX_MASK64((high) + 1 - (low)))
453 
454 #define EFX_INPLACE_MASK32(min, max, low, high)				\
455 	EFX_INSERT32(min, max, low, high, EFX_MASK32((high) + 1 - (low)))
456 
457 #define EFX_SET_OWORD64(oword, low, high, value) do {			\
458 	(oword).u64[0] = (((oword).u64[0]				\
459 			   & ~EFX_INPLACE_MASK64(0,  63, low, high))	\
460 			  | EFX_INSERT64(0,  63, low, high, value));	\
461 	(oword).u64[1] = (((oword).u64[1]				\
462 			   & ~EFX_INPLACE_MASK64(64, 127, low, high))	\
463 			  | EFX_INSERT64(64, 127, low, high, value));	\
464 	} while (0)
465 
466 #define EFX_SET_QWORD64(qword, low, high, value) do {			\
467 	(qword).u64[0] = (((qword).u64[0]				\
468 			   & ~EFX_INPLACE_MASK64(0, 63, low, high))	\
469 			  | EFX_INSERT64(0, 63, low, high, value));	\
470 	} while (0)
471 
472 #define EFX_SET_OWORD32(oword, low, high, value) do {			\
473 	(oword).u32[0] = (((oword).u32[0]				\
474 			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
475 			  | EFX_INSERT32(0, 31, low, high, value));	\
476 	(oword).u32[1] = (((oword).u32[1]				\
477 			   & ~EFX_INPLACE_MASK32(32, 63, low, high))	\
478 			  | EFX_INSERT32(32, 63, low, high, value));	\
479 	(oword).u32[2] = (((oword).u32[2]				\
480 			   & ~EFX_INPLACE_MASK32(64, 95, low, high))	\
481 			  | EFX_INSERT32(64, 95, low, high, value));	\
482 	(oword).u32[3] = (((oword).u32[3]				\
483 			   & ~EFX_INPLACE_MASK32(96, 127, low, high))	\
484 			  | EFX_INSERT32(96, 127, low, high, value));	\
485 	} while (0)
486 
487 #define EFX_SET_QWORD32(qword, low, high, value) do {			\
488 	(qword).u32[0] = (((qword).u32[0]				\
489 			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
490 			  | EFX_INSERT32(0, 31, low, high, value));	\
491 	(qword).u32[1] = (((qword).u32[1]				\
492 			   & ~EFX_INPLACE_MASK32(32, 63, low, high))	\
493 			  | EFX_INSERT32(32, 63, low, high, value));	\
494 	} while (0)
495 
496 #define EFX_SET_DWORD32(dword, low, high, value) do {			\
497 	(dword).u32[0] = (((dword).u32[0]				\
498 			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
499 			  | EFX_INSERT32(0, 31, low, high, value));	\
500 	} while (0)
501 
502 #define EFX_SET_OWORD_FIELD64(oword, field, value)			\
503 	EFX_SET_OWORD64(oword, EFX_LOW_BIT(field),			\
504 			 EFX_HIGH_BIT(field), value)
505 
506 #define EFX_SET_QWORD_FIELD64(qword, field, value)			\
507 	EFX_SET_QWORD64(qword, EFX_LOW_BIT(field),			\
508 			 EFX_HIGH_BIT(field), value)
509 
510 #define EFX_SET_OWORD_FIELD32(oword, field, value)			\
511 	EFX_SET_OWORD32(oword, EFX_LOW_BIT(field),			\
512 			 EFX_HIGH_BIT(field), value)
513 
514 #define EFX_SET_QWORD_FIELD32(qword, field, value)			\
515 	EFX_SET_QWORD32(qword, EFX_LOW_BIT(field),			\
516 			 EFX_HIGH_BIT(field), value)
517 
518 #define EFX_SET_DWORD_FIELD(dword, field, value)			\
519 	EFX_SET_DWORD32(dword, EFX_LOW_BIT(field),			\
520 			 EFX_HIGH_BIT(field), value)
521 
522 
523 
524 #if BITS_PER_LONG == 64
525 #define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD64
526 #define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD64
527 #else
528 #define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD32
529 #define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD32
530 #endif
531 
532 /* Used to avoid compiler warnings about shift range exceeding width
533  * of the data types when dma_addr_t is only 32 bits wide.
534  */
535 #define DMA_ADDR_T_WIDTH	(8 * sizeof(dma_addr_t))
536 #define EFX_DMA_TYPE_WIDTH(width) \
537 	(((width) < DMA_ADDR_T_WIDTH) ? (width) : DMA_ADDR_T_WIDTH)
538 
539 
540 /* Static initialiser */
541 #define EFX_OWORD32(a, b, c, d)				\
542 	{ .u32 = { cpu_to_le32(a), cpu_to_le32(b),	\
543 		   cpu_to_le32(c), cpu_to_le32(d) } }
544 
545 #endif /* EFX_BITFIELD_H */
546