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