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