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