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