xref: /linux/drivers/net/ethernet/sfc/bitfield.h (revision e74e1d55728509b352e4eec4283dd5b2781b2070)
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 	(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 	 EFX_INSERT_FIELD_NATIVE((min), (max), field11, (value11)) |	\
300 	 EFX_INSERT_FIELD_NATIVE((min), (max), field12, (value12)) |	\
301 	 EFX_INSERT_FIELD_NATIVE((min), (max), field13, (value13)))
302 
303 #define EFX_INSERT_FIELDS64(...)				\
304 	cpu_to_le64(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
305 
306 #define EFX_INSERT_FIELDS32(...)				\
307 	cpu_to_le32(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
308 
309 #define EFX_POPULATE_OWORD64(oword, ...) do {				\
310 	(oword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
311 	(oword).u64[1] = EFX_INSERT_FIELDS64(64, 127, __VA_ARGS__);	\
312 	} while (0)
313 
314 #define EFX_POPULATE_QWORD64(qword, ...) do {				\
315 	(qword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
316 	} while (0)
317 
318 #define EFX_POPULATE_OWORD32(oword, ...) do {				\
319 	(oword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
320 	(oword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
321 	(oword).u32[2] = EFX_INSERT_FIELDS32(64, 95, __VA_ARGS__);	\
322 	(oword).u32[3] = EFX_INSERT_FIELDS32(96, 127, __VA_ARGS__);	\
323 	} while (0)
324 
325 #define EFX_POPULATE_QWORD32(qword, ...) do {				\
326 	(qword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
327 	(qword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
328 	} while (0)
329 
330 #define EFX_POPULATE_DWORD(dword, ...) do {				\
331 	(dword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
332 	} while (0)
333 
334 #if BITS_PER_LONG == 64
335 #define EFX_POPULATE_OWORD EFX_POPULATE_OWORD64
336 #define EFX_POPULATE_QWORD EFX_POPULATE_QWORD64
337 #else
338 #define EFX_POPULATE_OWORD EFX_POPULATE_OWORD32
339 #define EFX_POPULATE_QWORD EFX_POPULATE_QWORD32
340 #endif
341 
342 /* Populate an octword field with various numbers of arguments */
343 #define EFX_POPULATE_OWORD_13 EFX_POPULATE_OWORD
344 #define EFX_POPULATE_OWORD_12(oword, ...) \
345 	EFX_POPULATE_OWORD_13(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
346 #define EFX_POPULATE_OWORD_11(oword, ...) \
347 	EFX_POPULATE_OWORD_12(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
348 #define EFX_POPULATE_OWORD_10(oword, ...) \
349 	EFX_POPULATE_OWORD_11(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
350 #define EFX_POPULATE_OWORD_9(oword, ...) \
351 	EFX_POPULATE_OWORD_10(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
352 #define EFX_POPULATE_OWORD_8(oword, ...) \
353 	EFX_POPULATE_OWORD_9(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
354 #define EFX_POPULATE_OWORD_7(oword, ...) \
355 	EFX_POPULATE_OWORD_8(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
356 #define EFX_POPULATE_OWORD_6(oword, ...) \
357 	EFX_POPULATE_OWORD_7(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
358 #define EFX_POPULATE_OWORD_5(oword, ...) \
359 	EFX_POPULATE_OWORD_6(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
360 #define EFX_POPULATE_OWORD_4(oword, ...) \
361 	EFX_POPULATE_OWORD_5(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
362 #define EFX_POPULATE_OWORD_3(oword, ...) \
363 	EFX_POPULATE_OWORD_4(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
364 #define EFX_POPULATE_OWORD_2(oword, ...) \
365 	EFX_POPULATE_OWORD_3(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
366 #define EFX_POPULATE_OWORD_1(oword, ...) \
367 	EFX_POPULATE_OWORD_2(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
368 #define EFX_ZERO_OWORD(oword) \
369 	EFX_POPULATE_OWORD_1(oword, EFX_DUMMY_FIELD, 0)
370 #define EFX_SET_OWORD(oword) \
371 	EFX_POPULATE_OWORD_4(oword, \
372 			     EFX_DWORD_0, 0xffffffff, \
373 			     EFX_DWORD_1, 0xffffffff, \
374 			     EFX_DWORD_2, 0xffffffff, \
375 			     EFX_DWORD_3, 0xffffffff)
376 
377 /* Populate a quadword field with various numbers of arguments */
378 #define EFX_POPULATE_QWORD_13 EFX_POPULATE_QWORD
379 #define EFX_POPULATE_QWORD_12(qword, ...) \
380 	EFX_POPULATE_QWORD_13(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
381 #define EFX_POPULATE_QWORD_11(qword, ...) \
382 	EFX_POPULATE_QWORD_12(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
383 #define EFX_POPULATE_QWORD_10(qword, ...) \
384 	EFX_POPULATE_QWORD_11(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
385 #define EFX_POPULATE_QWORD_9(qword, ...) \
386 	EFX_POPULATE_QWORD_10(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
387 #define EFX_POPULATE_QWORD_8(qword, ...) \
388 	EFX_POPULATE_QWORD_9(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
389 #define EFX_POPULATE_QWORD_7(qword, ...) \
390 	EFX_POPULATE_QWORD_8(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
391 #define EFX_POPULATE_QWORD_6(qword, ...) \
392 	EFX_POPULATE_QWORD_7(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
393 #define EFX_POPULATE_QWORD_5(qword, ...) \
394 	EFX_POPULATE_QWORD_6(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
395 #define EFX_POPULATE_QWORD_4(qword, ...) \
396 	EFX_POPULATE_QWORD_5(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
397 #define EFX_POPULATE_QWORD_3(qword, ...) \
398 	EFX_POPULATE_QWORD_4(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
399 #define EFX_POPULATE_QWORD_2(qword, ...) \
400 	EFX_POPULATE_QWORD_3(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
401 #define EFX_POPULATE_QWORD_1(qword, ...) \
402 	EFX_POPULATE_QWORD_2(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
403 #define EFX_ZERO_QWORD(qword) \
404 	EFX_POPULATE_QWORD_1(qword, EFX_DUMMY_FIELD, 0)
405 #define EFX_SET_QWORD(qword) \
406 	EFX_POPULATE_QWORD_2(qword, \
407 			     EFX_DWORD_0, 0xffffffff, \
408 			     EFX_DWORD_1, 0xffffffff)
409 
410 /* Populate a dword field with various numbers of arguments */
411 #define EFX_POPULATE_DWORD_13 EFX_POPULATE_DWORD
412 #define EFX_POPULATE_DWORD_12(dword, ...) \
413 	EFX_POPULATE_DWORD_13(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
414 #define EFX_POPULATE_DWORD_11(dword, ...) \
415 	EFX_POPULATE_DWORD_12(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
416 #define EFX_POPULATE_DWORD_10(dword, ...) \
417 	EFX_POPULATE_DWORD_11(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
418 #define EFX_POPULATE_DWORD_9(dword, ...) \
419 	EFX_POPULATE_DWORD_10(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
420 #define EFX_POPULATE_DWORD_8(dword, ...) \
421 	EFX_POPULATE_DWORD_9(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
422 #define EFX_POPULATE_DWORD_7(dword, ...) \
423 	EFX_POPULATE_DWORD_8(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
424 #define EFX_POPULATE_DWORD_6(dword, ...) \
425 	EFX_POPULATE_DWORD_7(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
426 #define EFX_POPULATE_DWORD_5(dword, ...) \
427 	EFX_POPULATE_DWORD_6(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
428 #define EFX_POPULATE_DWORD_4(dword, ...) \
429 	EFX_POPULATE_DWORD_5(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
430 #define EFX_POPULATE_DWORD_3(dword, ...) \
431 	EFX_POPULATE_DWORD_4(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
432 #define EFX_POPULATE_DWORD_2(dword, ...) \
433 	EFX_POPULATE_DWORD_3(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
434 #define EFX_POPULATE_DWORD_1(dword, ...) \
435 	EFX_POPULATE_DWORD_2(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
436 #define EFX_ZERO_DWORD(dword) \
437 	EFX_POPULATE_DWORD_1(dword, EFX_DUMMY_FIELD, 0)
438 #define EFX_SET_DWORD(dword) \
439 	EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 0xffffffff)
440 
441 /*
442  * Modify a named field within an already-populated structure.  Used
443  * for read-modify-write operations.
444  *
445  */
446 #define EFX_INVERT_OWORD(oword) do {		\
447 	(oword).u64[0] = ~((oword).u64[0]);	\
448 	(oword).u64[1] = ~((oword).u64[1]);	\
449 	} while (0)
450 
451 #define EFX_AND_OWORD(oword, from, mask)			\
452 	do {							\
453 		(oword).u64[0] = (from).u64[0] & (mask).u64[0];	\
454 		(oword).u64[1] = (from).u64[1] & (mask).u64[1];	\
455 	} while (0)
456 
457 #define EFX_AND_QWORD(qword, from, mask)			\
458 		(qword).u64[0] = (from).u64[0] & (mask).u64[0]
459 
460 #define EFX_OR_OWORD(oword, from, mask)				\
461 	do {							\
462 		(oword).u64[0] = (from).u64[0] | (mask).u64[0];	\
463 		(oword).u64[1] = (from).u64[1] | (mask).u64[1];	\
464 	} while (0)
465 
466 #define EFX_INSERT64(min, max, low, high, value)			\
467 	cpu_to_le64(EFX_INSERT_NATIVE(min, max, low, high, value))
468 
469 #define EFX_INSERT32(min, max, low, high, value)			\
470 	cpu_to_le32(EFX_INSERT_NATIVE(min, max, low, high, value))
471 
472 #define EFX_INPLACE_MASK64(min, max, low, high)				\
473 	EFX_INSERT64(min, max, low, high, EFX_MASK64((high) + 1 - (low)))
474 
475 #define EFX_INPLACE_MASK32(min, max, low, high)				\
476 	EFX_INSERT32(min, max, low, high, EFX_MASK32((high) + 1 - (low)))
477 
478 #define EFX_SET_OWORD64(oword, low, high, value) do {			\
479 	(oword).u64[0] = (((oword).u64[0]				\
480 			   & ~EFX_INPLACE_MASK64(0,  63, low, high))	\
481 			  | EFX_INSERT64(0,  63, low, high, value));	\
482 	(oword).u64[1] = (((oword).u64[1]				\
483 			   & ~EFX_INPLACE_MASK64(64, 127, low, high))	\
484 			  | EFX_INSERT64(64, 127, low, high, value));	\
485 	} while (0)
486 
487 #define EFX_SET_QWORD64(qword, low, high, value) do {			\
488 	(qword).u64[0] = (((qword).u64[0]				\
489 			   & ~EFX_INPLACE_MASK64(0, 63, low, high))	\
490 			  | EFX_INSERT64(0, 63, low, high, value));	\
491 	} while (0)
492 
493 #define EFX_SET_OWORD32(oword, low, high, value) do {			\
494 	(oword).u32[0] = (((oword).u32[0]				\
495 			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
496 			  | EFX_INSERT32(0, 31, low, high, value));	\
497 	(oword).u32[1] = (((oword).u32[1]				\
498 			   & ~EFX_INPLACE_MASK32(32, 63, low, high))	\
499 			  | EFX_INSERT32(32, 63, low, high, value));	\
500 	(oword).u32[2] = (((oword).u32[2]				\
501 			   & ~EFX_INPLACE_MASK32(64, 95, low, high))	\
502 			  | EFX_INSERT32(64, 95, low, high, value));	\
503 	(oword).u32[3] = (((oword).u32[3]				\
504 			   & ~EFX_INPLACE_MASK32(96, 127, low, high))	\
505 			  | EFX_INSERT32(96, 127, low, high, value));	\
506 	} while (0)
507 
508 #define EFX_SET_QWORD32(qword, low, high, value) do {			\
509 	(qword).u32[0] = (((qword).u32[0]				\
510 			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
511 			  | EFX_INSERT32(0, 31, low, high, value));	\
512 	(qword).u32[1] = (((qword).u32[1]				\
513 			   & ~EFX_INPLACE_MASK32(32, 63, low, high))	\
514 			  | EFX_INSERT32(32, 63, low, high, value));	\
515 	} while (0)
516 
517 #define EFX_SET_DWORD32(dword, low, high, value) do {			\
518 	(dword).u32[0] = (((dword).u32[0]				\
519 			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
520 			  | EFX_INSERT32(0, 31, low, high, value));	\
521 	} while (0)
522 
523 #define EFX_SET_OWORD_FIELD64(oword, field, value)			\
524 	EFX_SET_OWORD64(oword, EFX_LOW_BIT(field),			\
525 			 EFX_HIGH_BIT(field), value)
526 
527 #define EFX_SET_QWORD_FIELD64(qword, field, value)			\
528 	EFX_SET_QWORD64(qword, EFX_LOW_BIT(field),			\
529 			 EFX_HIGH_BIT(field), value)
530 
531 #define EFX_SET_OWORD_FIELD32(oword, field, value)			\
532 	EFX_SET_OWORD32(oword, EFX_LOW_BIT(field),			\
533 			 EFX_HIGH_BIT(field), value)
534 
535 #define EFX_SET_QWORD_FIELD32(qword, field, value)			\
536 	EFX_SET_QWORD32(qword, EFX_LOW_BIT(field),			\
537 			 EFX_HIGH_BIT(field), value)
538 
539 #define EFX_SET_DWORD_FIELD(dword, field, value)			\
540 	EFX_SET_DWORD32(dword, EFX_LOW_BIT(field),			\
541 			 EFX_HIGH_BIT(field), value)
542 
543 
544 
545 #if BITS_PER_LONG == 64
546 #define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD64
547 #define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD64
548 #else
549 #define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD32
550 #define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD32
551 #endif
552 
553 /* Used to avoid compiler warnings about shift range exceeding width
554  * of the data types when dma_addr_t is only 32 bits wide.
555  */
556 #define DMA_ADDR_T_WIDTH	(8 * sizeof(dma_addr_t))
557 #define EFX_DMA_TYPE_WIDTH(width) \
558 	(((width) < DMA_ADDR_T_WIDTH) ? (width) : DMA_ADDR_T_WIDTH)
559 
560 
561 /* Static initialiser */
562 #define EFX_OWORD32(a, b, c, d)				\
563 	{ .u32 = { cpu_to_le32(a), cpu_to_le32(b),	\
564 		   cpu_to_le32(c), cpu_to_le32(d) } }
565 
566 #endif /* EFX_BITFIELD_H */
567