1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2006-2013 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 EF4_BITFIELD_H
12 #define EF4_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 (ef4_oword_t, ef4_qword_t and
26  * ef4_dword_t) to be little-endian.
27  */
28 
29 /* Lowest bit numbers and widths */
30 #define EF4_DUMMY_FIELD_LBN 0
31 #define EF4_DUMMY_FIELD_WIDTH 0
32 #define EF4_WORD_0_LBN 0
33 #define EF4_WORD_0_WIDTH 16
34 #define EF4_WORD_1_LBN 16
35 #define EF4_WORD_1_WIDTH 16
36 #define EF4_DWORD_0_LBN 0
37 #define EF4_DWORD_0_WIDTH 32
38 #define EF4_DWORD_1_LBN 32
39 #define EF4_DWORD_1_WIDTH 32
40 #define EF4_DWORD_2_LBN 64
41 #define EF4_DWORD_2_WIDTH 32
42 #define EF4_DWORD_3_LBN 96
43 #define EF4_DWORD_3_WIDTH 32
44 #define EF4_QWORD_0_LBN 0
45 #define EF4_QWORD_0_WIDTH 64
46 
47 /* Specified attribute (e.g. LBN) of the specified field */
48 #define EF4_VAL(field, attribute) field ## _ ## attribute
49 /* Low bit number of the specified field */
50 #define EF4_LOW_BIT(field) EF4_VAL(field, LBN)
51 /* Bit width of the specified field */
52 #define EF4_WIDTH(field) EF4_VAL(field, WIDTH)
53 /* High bit number of the specified field */
54 #define EF4_HIGH_BIT(field) (EF4_LOW_BIT(field) + EF4_WIDTH(field) - 1)
55 /* Mask equal in width to the specified field.
56  *
57  * For example, a field with width 5 would have a mask of 0x1f.
58  *
59  * The maximum width mask that can be generated is 64 bits.
60  */
61 #define EF4_MASK64(width)			\
62 	((width) == 64 ? ~((u64) 0) :		\
63 	 (((((u64) 1) << (width))) - 1))
64 
65 /* Mask equal in width to the specified field.
66  *
67  * For example, a field with width 5 would have a mask of 0x1f.
68  *
69  * The maximum width mask that can be generated is 32 bits.  Use
70  * EF4_MASK64 for higher width fields.
71  */
72 #define EF4_MASK32(width)			\
73 	((width) == 32 ? ~((u32) 0) :		\
74 	 (((((u32) 1) << (width))) - 1))
75 
76 /* A doubleword (i.e. 4 byte) datatype - little-endian in HW */
77 typedef union ef4_dword {
78 	__le32 u32[1];
79 } ef4_dword_t;
80 
81 /* A quadword (i.e. 8 byte) datatype - little-endian in HW */
82 typedef union ef4_qword {
83 	__le64 u64[1];
84 	__le32 u32[2];
85 	ef4_dword_t dword[2];
86 } ef4_qword_t;
87 
88 /* An octword (eight-word, i.e. 16 byte) datatype - little-endian in HW */
89 typedef union ef4_oword {
90 	__le64 u64[2];
91 	ef4_qword_t qword[2];
92 	__le32 u32[4];
93 	ef4_dword_t dword[4];
94 } ef4_oword_t;
95 
96 /* Format string and value expanders for printk */
97 #define EF4_DWORD_FMT "%08x"
98 #define EF4_QWORD_FMT "%08x:%08x"
99 #define EF4_OWORD_FMT "%08x:%08x:%08x:%08x"
100 #define EF4_DWORD_VAL(dword)				\
101 	((unsigned int) le32_to_cpu((dword).u32[0]))
102 #define EF4_QWORD_VAL(qword)				\
103 	((unsigned int) le32_to_cpu((qword).u32[1])),	\
104 	((unsigned int) le32_to_cpu((qword).u32[0]))
105 #define EF4_OWORD_VAL(oword)				\
106 	((unsigned int) le32_to_cpu((oword).u32[3])),	\
107 	((unsigned int) le32_to_cpu((oword).u32[2])),	\
108 	((unsigned int) le32_to_cpu((oword).u32[1])),	\
109 	((unsigned int) le32_to_cpu((oword).u32[0]))
110 
111 /*
112  * Extract bit field portion [low,high) from the native-endian element
113  * which contains bits [min,max).
114  *
115  * For example, suppose "element" represents the high 32 bits of a
116  * 64-bit value, and we wish to extract the bits belonging to the bit
117  * field occupying bits 28-45 of this 64-bit value.
118  *
119  * Then EF4_EXTRACT ( element, 32, 63, 28, 45 ) would give
120  *
121  *   ( element ) << 4
122  *
123  * The result will contain the relevant bits filled in in the range
124  * [0,high-low), with garbage in bits [high-low+1,...).
125  */
126 #define EF4_EXTRACT_NATIVE(native_element, min, max, low, high)		\
127 	((low) > (max) || (high) < (min) ? 0 :				\
128 	 (low) > (min) ?						\
129 	 (native_element) >> ((low) - (min)) :				\
130 	 (native_element) << ((min) - (low)))
131 
132 /*
133  * Extract bit field portion [low,high) from the 64-bit little-endian
134  * element which contains bits [min,max)
135  */
136 #define EF4_EXTRACT64(element, min, max, low, high)			\
137 	EF4_EXTRACT_NATIVE(le64_to_cpu(element), min, max, low, high)
138 
139 /*
140  * Extract bit field portion [low,high) from the 32-bit little-endian
141  * element which contains bits [min,max)
142  */
143 #define EF4_EXTRACT32(element, min, max, low, high)			\
144 	EF4_EXTRACT_NATIVE(le32_to_cpu(element), min, max, low, high)
145 
146 #define EF4_EXTRACT_OWORD64(oword, low, high)				\
147 	((EF4_EXTRACT64((oword).u64[0], 0, 63, low, high) |		\
148 	  EF4_EXTRACT64((oword).u64[1], 64, 127, low, high)) &		\
149 	 EF4_MASK64((high) + 1 - (low)))
150 
151 #define EF4_EXTRACT_QWORD64(qword, low, high)				\
152 	(EF4_EXTRACT64((qword).u64[0], 0, 63, low, high) &		\
153 	 EF4_MASK64((high) + 1 - (low)))
154 
155 #define EF4_EXTRACT_OWORD32(oword, low, high)				\
156 	((EF4_EXTRACT32((oword).u32[0], 0, 31, low, high) |		\
157 	  EF4_EXTRACT32((oword).u32[1], 32, 63, low, high) |		\
158 	  EF4_EXTRACT32((oword).u32[2], 64, 95, low, high) |		\
159 	  EF4_EXTRACT32((oword).u32[3], 96, 127, low, high)) &		\
160 	 EF4_MASK32((high) + 1 - (low)))
161 
162 #define EF4_EXTRACT_QWORD32(qword, low, high)				\
163 	((EF4_EXTRACT32((qword).u32[0], 0, 31, low, high) |		\
164 	  EF4_EXTRACT32((qword).u32[1], 32, 63, low, high)) &		\
165 	 EF4_MASK32((high) + 1 - (low)))
166 
167 #define EF4_EXTRACT_DWORD(dword, low, high)			\
168 	(EF4_EXTRACT32((dword).u32[0], 0, 31, low, high) &	\
169 	 EF4_MASK32((high) + 1 - (low)))
170 
171 #define EF4_OWORD_FIELD64(oword, field)				\
172 	EF4_EXTRACT_OWORD64(oword, EF4_LOW_BIT(field),		\
173 			    EF4_HIGH_BIT(field))
174 
175 #define EF4_QWORD_FIELD64(qword, field)				\
176 	EF4_EXTRACT_QWORD64(qword, EF4_LOW_BIT(field),		\
177 			    EF4_HIGH_BIT(field))
178 
179 #define EF4_OWORD_FIELD32(oword, field)				\
180 	EF4_EXTRACT_OWORD32(oword, EF4_LOW_BIT(field),		\
181 			    EF4_HIGH_BIT(field))
182 
183 #define EF4_QWORD_FIELD32(qword, field)				\
184 	EF4_EXTRACT_QWORD32(qword, EF4_LOW_BIT(field),		\
185 			    EF4_HIGH_BIT(field))
186 
187 #define EF4_DWORD_FIELD(dword, field)				\
188 	EF4_EXTRACT_DWORD(dword, EF4_LOW_BIT(field),		\
189 			  EF4_HIGH_BIT(field))
190 
191 #define EF4_OWORD_IS_ZERO64(oword)					\
192 	(((oword).u64[0] | (oword).u64[1]) == (__force __le64) 0)
193 
194 #define EF4_QWORD_IS_ZERO64(qword)					\
195 	(((qword).u64[0]) == (__force __le64) 0)
196 
197 #define EF4_OWORD_IS_ZERO32(oword)					     \
198 	(((oword).u32[0] | (oword).u32[1] | (oword).u32[2] | (oword).u32[3]) \
199 	 == (__force __le32) 0)
200 
201 #define EF4_QWORD_IS_ZERO32(qword)					\
202 	(((qword).u32[0] | (qword).u32[1]) == (__force __le32) 0)
203 
204 #define EF4_DWORD_IS_ZERO(dword)					\
205 	(((dword).u32[0]) == (__force __le32) 0)
206 
207 #define EF4_OWORD_IS_ALL_ONES64(oword)					\
208 	(((oword).u64[0] & (oword).u64[1]) == ~((__force __le64) 0))
209 
210 #define EF4_QWORD_IS_ALL_ONES64(qword)					\
211 	((qword).u64[0] == ~((__force __le64) 0))
212 
213 #define EF4_OWORD_IS_ALL_ONES32(oword)					\
214 	(((oword).u32[0] & (oword).u32[1] & (oword).u32[2] & (oword).u32[3]) \
215 	 == ~((__force __le32) 0))
216 
217 #define EF4_QWORD_IS_ALL_ONES32(qword)					\
218 	(((qword).u32[0] & (qword).u32[1]) == ~((__force __le32) 0))
219 
220 #define EF4_DWORD_IS_ALL_ONES(dword)					\
221 	((dword).u32[0] == ~((__force __le32) 0))
222 
223 #if BITS_PER_LONG == 64
224 #define EF4_OWORD_FIELD		EF4_OWORD_FIELD64
225 #define EF4_QWORD_FIELD		EF4_QWORD_FIELD64
226 #define EF4_OWORD_IS_ZERO	EF4_OWORD_IS_ZERO64
227 #define EF4_QWORD_IS_ZERO	EF4_QWORD_IS_ZERO64
228 #define EF4_OWORD_IS_ALL_ONES	EF4_OWORD_IS_ALL_ONES64
229 #define EF4_QWORD_IS_ALL_ONES	EF4_QWORD_IS_ALL_ONES64
230 #else
231 #define EF4_OWORD_FIELD		EF4_OWORD_FIELD32
232 #define EF4_QWORD_FIELD		EF4_QWORD_FIELD32
233 #define EF4_OWORD_IS_ZERO	EF4_OWORD_IS_ZERO32
234 #define EF4_QWORD_IS_ZERO	EF4_QWORD_IS_ZERO32
235 #define EF4_OWORD_IS_ALL_ONES	EF4_OWORD_IS_ALL_ONES32
236 #define EF4_QWORD_IS_ALL_ONES	EF4_QWORD_IS_ALL_ONES32
237 #endif
238 
239 /*
240  * Construct bit field portion
241  *
242  * Creates the portion of the bit field [low,high) that lies within
243  * the range [min,max).
244  */
245 #define EF4_INSERT_NATIVE64(min, max, low, high, value)		\
246 	(((low > max) || (high < min)) ? 0 :			\
247 	 ((low > min) ?						\
248 	  (((u64) (value)) << (low - min)) :		\
249 	  (((u64) (value)) >> (min - low))))
250 
251 #define EF4_INSERT_NATIVE32(min, max, low, high, value)		\
252 	(((low > max) || (high < min)) ? 0 :			\
253 	 ((low > min) ?						\
254 	  (((u32) (value)) << (low - min)) :		\
255 	  (((u32) (value)) >> (min - low))))
256 
257 #define EF4_INSERT_NATIVE(min, max, low, high, value)		\
258 	((((max - min) >= 32) || ((high - low) >= 32)) ?	\
259 	 EF4_INSERT_NATIVE64(min, max, low, high, value) :	\
260 	 EF4_INSERT_NATIVE32(min, max, low, high, value))
261 
262 /*
263  * Construct bit field portion
264  *
265  * Creates the portion of the named bit field that lies within the
266  * range [min,max).
267  */
268 #define EF4_INSERT_FIELD_NATIVE(min, max, field, value)		\
269 	EF4_INSERT_NATIVE(min, max, EF4_LOW_BIT(field),		\
270 			  EF4_HIGH_BIT(field), value)
271 
272 /*
273  * Construct bit field
274  *
275  * Creates the portion of the named bit fields that lie within the
276  * range [min,max).
277  */
278 #define EF4_INSERT_FIELDS_NATIVE(min, max,				\
279 				 field1, value1,			\
280 				 field2, value2,			\
281 				 field3, value3,			\
282 				 field4, value4,			\
283 				 field5, value5,			\
284 				 field6, value6,			\
285 				 field7, value7,			\
286 				 field8, value8,			\
287 				 field9, value9,			\
288 				 field10, value10)			\
289 	(EF4_INSERT_FIELD_NATIVE((min), (max), field1, (value1)) |	\
290 	 EF4_INSERT_FIELD_NATIVE((min), (max), field2, (value2)) |	\
291 	 EF4_INSERT_FIELD_NATIVE((min), (max), field3, (value3)) |	\
292 	 EF4_INSERT_FIELD_NATIVE((min), (max), field4, (value4)) |	\
293 	 EF4_INSERT_FIELD_NATIVE((min), (max), field5, (value5)) |	\
294 	 EF4_INSERT_FIELD_NATIVE((min), (max), field6, (value6)) |	\
295 	 EF4_INSERT_FIELD_NATIVE((min), (max), field7, (value7)) |	\
296 	 EF4_INSERT_FIELD_NATIVE((min), (max), field8, (value8)) |	\
297 	 EF4_INSERT_FIELD_NATIVE((min), (max), field9, (value9)) |	\
298 	 EF4_INSERT_FIELD_NATIVE((min), (max), field10, (value10)))
299 
300 #define EF4_INSERT_FIELDS64(...)				\
301 	cpu_to_le64(EF4_INSERT_FIELDS_NATIVE(__VA_ARGS__))
302 
303 #define EF4_INSERT_FIELDS32(...)				\
304 	cpu_to_le32(EF4_INSERT_FIELDS_NATIVE(__VA_ARGS__))
305 
306 #define EF4_POPULATE_OWORD64(oword, ...) do {				\
307 	(oword).u64[0] = EF4_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
308 	(oword).u64[1] = EF4_INSERT_FIELDS64(64, 127, __VA_ARGS__);	\
309 	} while (0)
310 
311 #define EF4_POPULATE_QWORD64(qword, ...) do {				\
312 	(qword).u64[0] = EF4_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
313 	} while (0)
314 
315 #define EF4_POPULATE_OWORD32(oword, ...) do {				\
316 	(oword).u32[0] = EF4_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
317 	(oword).u32[1] = EF4_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
318 	(oword).u32[2] = EF4_INSERT_FIELDS32(64, 95, __VA_ARGS__);	\
319 	(oword).u32[3] = EF4_INSERT_FIELDS32(96, 127, __VA_ARGS__);	\
320 	} while (0)
321 
322 #define EF4_POPULATE_QWORD32(qword, ...) do {				\
323 	(qword).u32[0] = EF4_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
324 	(qword).u32[1] = EF4_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
325 	} while (0)
326 
327 #define EF4_POPULATE_DWORD(dword, ...) do {				\
328 	(dword).u32[0] = EF4_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
329 	} while (0)
330 
331 #if BITS_PER_LONG == 64
332 #define EF4_POPULATE_OWORD EF4_POPULATE_OWORD64
333 #define EF4_POPULATE_QWORD EF4_POPULATE_QWORD64
334 #else
335 #define EF4_POPULATE_OWORD EF4_POPULATE_OWORD32
336 #define EF4_POPULATE_QWORD EF4_POPULATE_QWORD32
337 #endif
338 
339 /* Populate an octword field with various numbers of arguments */
340 #define EF4_POPULATE_OWORD_10 EF4_POPULATE_OWORD
341 #define EF4_POPULATE_OWORD_9(oword, ...) \
342 	EF4_POPULATE_OWORD_10(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
343 #define EF4_POPULATE_OWORD_8(oword, ...) \
344 	EF4_POPULATE_OWORD_9(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
345 #define EF4_POPULATE_OWORD_7(oword, ...) \
346 	EF4_POPULATE_OWORD_8(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
347 #define EF4_POPULATE_OWORD_6(oword, ...) \
348 	EF4_POPULATE_OWORD_7(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
349 #define EF4_POPULATE_OWORD_5(oword, ...) \
350 	EF4_POPULATE_OWORD_6(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
351 #define EF4_POPULATE_OWORD_4(oword, ...) \
352 	EF4_POPULATE_OWORD_5(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
353 #define EF4_POPULATE_OWORD_3(oword, ...) \
354 	EF4_POPULATE_OWORD_4(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
355 #define EF4_POPULATE_OWORD_2(oword, ...) \
356 	EF4_POPULATE_OWORD_3(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
357 #define EF4_POPULATE_OWORD_1(oword, ...) \
358 	EF4_POPULATE_OWORD_2(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
359 #define EF4_ZERO_OWORD(oword) \
360 	EF4_POPULATE_OWORD_1(oword, EF4_DUMMY_FIELD, 0)
361 #define EF4_SET_OWORD(oword) \
362 	EF4_POPULATE_OWORD_4(oword, \
363 			     EF4_DWORD_0, 0xffffffff, \
364 			     EF4_DWORD_1, 0xffffffff, \
365 			     EF4_DWORD_2, 0xffffffff, \
366 			     EF4_DWORD_3, 0xffffffff)
367 
368 /* Populate a quadword field with various numbers of arguments */
369 #define EF4_POPULATE_QWORD_10 EF4_POPULATE_QWORD
370 #define EF4_POPULATE_QWORD_9(qword, ...) \
371 	EF4_POPULATE_QWORD_10(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
372 #define EF4_POPULATE_QWORD_8(qword, ...) \
373 	EF4_POPULATE_QWORD_9(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
374 #define EF4_POPULATE_QWORD_7(qword, ...) \
375 	EF4_POPULATE_QWORD_8(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
376 #define EF4_POPULATE_QWORD_6(qword, ...) \
377 	EF4_POPULATE_QWORD_7(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
378 #define EF4_POPULATE_QWORD_5(qword, ...) \
379 	EF4_POPULATE_QWORD_6(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
380 #define EF4_POPULATE_QWORD_4(qword, ...) \
381 	EF4_POPULATE_QWORD_5(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
382 #define EF4_POPULATE_QWORD_3(qword, ...) \
383 	EF4_POPULATE_QWORD_4(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
384 #define EF4_POPULATE_QWORD_2(qword, ...) \
385 	EF4_POPULATE_QWORD_3(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
386 #define EF4_POPULATE_QWORD_1(qword, ...) \
387 	EF4_POPULATE_QWORD_2(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
388 #define EF4_ZERO_QWORD(qword) \
389 	EF4_POPULATE_QWORD_1(qword, EF4_DUMMY_FIELD, 0)
390 #define EF4_SET_QWORD(qword) \
391 	EF4_POPULATE_QWORD_2(qword, \
392 			     EF4_DWORD_0, 0xffffffff, \
393 			     EF4_DWORD_1, 0xffffffff)
394 
395 /* Populate a dword field with various numbers of arguments */
396 #define EF4_POPULATE_DWORD_10 EF4_POPULATE_DWORD
397 #define EF4_POPULATE_DWORD_9(dword, ...) \
398 	EF4_POPULATE_DWORD_10(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
399 #define EF4_POPULATE_DWORD_8(dword, ...) \
400 	EF4_POPULATE_DWORD_9(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
401 #define EF4_POPULATE_DWORD_7(dword, ...) \
402 	EF4_POPULATE_DWORD_8(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
403 #define EF4_POPULATE_DWORD_6(dword, ...) \
404 	EF4_POPULATE_DWORD_7(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
405 #define EF4_POPULATE_DWORD_5(dword, ...) \
406 	EF4_POPULATE_DWORD_6(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
407 #define EF4_POPULATE_DWORD_4(dword, ...) \
408 	EF4_POPULATE_DWORD_5(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
409 #define EF4_POPULATE_DWORD_3(dword, ...) \
410 	EF4_POPULATE_DWORD_4(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
411 #define EF4_POPULATE_DWORD_2(dword, ...) \
412 	EF4_POPULATE_DWORD_3(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
413 #define EF4_POPULATE_DWORD_1(dword, ...) \
414 	EF4_POPULATE_DWORD_2(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
415 #define EF4_ZERO_DWORD(dword) \
416 	EF4_POPULATE_DWORD_1(dword, EF4_DUMMY_FIELD, 0)
417 #define EF4_SET_DWORD(dword) \
418 	EF4_POPULATE_DWORD_1(dword, EF4_DWORD_0, 0xffffffff)
419 
420 /*
421  * Modify a named field within an already-populated structure.  Used
422  * for read-modify-write operations.
423  *
424  */
425 #define EF4_INVERT_OWORD(oword) do {		\
426 	(oword).u64[0] = ~((oword).u64[0]);	\
427 	(oword).u64[1] = ~((oword).u64[1]);	\
428 	} while (0)
429 
430 #define EF4_AND_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 EF4_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 EF4_INSERT64(min, max, low, high, value)			\
443 	cpu_to_le64(EF4_INSERT_NATIVE(min, max, low, high, value))
444 
445 #define EF4_INSERT32(min, max, low, high, value)			\
446 	cpu_to_le32(EF4_INSERT_NATIVE(min, max, low, high, value))
447 
448 #define EF4_INPLACE_MASK64(min, max, low, high)				\
449 	EF4_INSERT64(min, max, low, high, EF4_MASK64((high) + 1 - (low)))
450 
451 #define EF4_INPLACE_MASK32(min, max, low, high)				\
452 	EF4_INSERT32(min, max, low, high, EF4_MASK32((high) + 1 - (low)))
453 
454 #define EF4_SET_OWORD64(oword, low, high, value) do {			\
455 	(oword).u64[0] = (((oword).u64[0]				\
456 			   & ~EF4_INPLACE_MASK64(0,  63, low, high))	\
457 			  | EF4_INSERT64(0,  63, low, high, value));	\
458 	(oword).u64[1] = (((oword).u64[1]				\
459 			   & ~EF4_INPLACE_MASK64(64, 127, low, high))	\
460 			  | EF4_INSERT64(64, 127, low, high, value));	\
461 	} while (0)
462 
463 #define EF4_SET_QWORD64(qword, low, high, value) do {			\
464 	(qword).u64[0] = (((qword).u64[0]				\
465 			   & ~EF4_INPLACE_MASK64(0, 63, low, high))	\
466 			  | EF4_INSERT64(0, 63, low, high, value));	\
467 	} while (0)
468 
469 #define EF4_SET_OWORD32(oword, low, high, value) do {			\
470 	(oword).u32[0] = (((oword).u32[0]				\
471 			   & ~EF4_INPLACE_MASK32(0, 31, low, high))	\
472 			  | EF4_INSERT32(0, 31, low, high, value));	\
473 	(oword).u32[1] = (((oword).u32[1]				\
474 			   & ~EF4_INPLACE_MASK32(32, 63, low, high))	\
475 			  | EF4_INSERT32(32, 63, low, high, value));	\
476 	(oword).u32[2] = (((oword).u32[2]				\
477 			   & ~EF4_INPLACE_MASK32(64, 95, low, high))	\
478 			  | EF4_INSERT32(64, 95, low, high, value));	\
479 	(oword).u32[3] = (((oword).u32[3]				\
480 			   & ~EF4_INPLACE_MASK32(96, 127, low, high))	\
481 			  | EF4_INSERT32(96, 127, low, high, value));	\
482 	} while (0)
483 
484 #define EF4_SET_QWORD32(qword, low, high, value) do {			\
485 	(qword).u32[0] = (((qword).u32[0]				\
486 			   & ~EF4_INPLACE_MASK32(0, 31, low, high))	\
487 			  | EF4_INSERT32(0, 31, low, high, value));	\
488 	(qword).u32[1] = (((qword).u32[1]				\
489 			   & ~EF4_INPLACE_MASK32(32, 63, low, high))	\
490 			  | EF4_INSERT32(32, 63, low, high, value));	\
491 	} while (0)
492 
493 #define EF4_SET_DWORD32(dword, low, high, value) do {			\
494 	(dword).u32[0] = (((dword).u32[0]				\
495 			   & ~EF4_INPLACE_MASK32(0, 31, low, high))	\
496 			  | EF4_INSERT32(0, 31, low, high, value));	\
497 	} while (0)
498 
499 #define EF4_SET_OWORD_FIELD64(oword, field, value)			\
500 	EF4_SET_OWORD64(oword, EF4_LOW_BIT(field),			\
501 			 EF4_HIGH_BIT(field), value)
502 
503 #define EF4_SET_QWORD_FIELD64(qword, field, value)			\
504 	EF4_SET_QWORD64(qword, EF4_LOW_BIT(field),			\
505 			 EF4_HIGH_BIT(field), value)
506 
507 #define EF4_SET_OWORD_FIELD32(oword, field, value)			\
508 	EF4_SET_OWORD32(oword, EF4_LOW_BIT(field),			\
509 			 EF4_HIGH_BIT(field), value)
510 
511 #define EF4_SET_QWORD_FIELD32(qword, field, value)			\
512 	EF4_SET_QWORD32(qword, EF4_LOW_BIT(field),			\
513 			 EF4_HIGH_BIT(field), value)
514 
515 #define EF4_SET_DWORD_FIELD(dword, field, value)			\
516 	EF4_SET_DWORD32(dword, EF4_LOW_BIT(field),			\
517 			 EF4_HIGH_BIT(field), value)
518 
519 
520 
521 #if BITS_PER_LONG == 64
522 #define EF4_SET_OWORD_FIELD EF4_SET_OWORD_FIELD64
523 #define EF4_SET_QWORD_FIELD EF4_SET_QWORD_FIELD64
524 #else
525 #define EF4_SET_OWORD_FIELD EF4_SET_OWORD_FIELD32
526 #define EF4_SET_QWORD_FIELD EF4_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 EF4_DMA_TYPE_WIDTH(width) \
534 	(((width) < DMA_ADDR_T_WIDTH) ? (width) : DMA_ADDR_T_WIDTH)
535 
536 
537 /* Static initialiser */
538 #define EF4_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 /* EF4_BITFIELD_H */
543