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