xref: /linux/drivers/net/ethernet/sfc/falcon/bitfield.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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 EF4_BITFIELD_H
9 #define EF4_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 (ef4_oword_t, ef4_qword_t and
23  * ef4_dword_t) to be little-endian.
24  */
25 
26 /* Lowest bit numbers and widths */
27 #define EF4_DUMMY_FIELD_LBN 0
28 #define EF4_DUMMY_FIELD_WIDTH 0
29 #define EF4_WORD_0_LBN 0
30 #define EF4_WORD_0_WIDTH 16
31 #define EF4_WORD_1_LBN 16
32 #define EF4_WORD_1_WIDTH 16
33 #define EF4_DWORD_0_LBN 0
34 #define EF4_DWORD_0_WIDTH 32
35 #define EF4_DWORD_1_LBN 32
36 #define EF4_DWORD_1_WIDTH 32
37 #define EF4_DWORD_2_LBN 64
38 #define EF4_DWORD_2_WIDTH 32
39 #define EF4_DWORD_3_LBN 96
40 #define EF4_DWORD_3_WIDTH 32
41 #define EF4_QWORD_0_LBN 0
42 #define EF4_QWORD_0_WIDTH 64
43 
44 /* Specified attribute (e.g. LBN) of the specified field */
45 #define EF4_VAL(field, attribute) field ## _ ## attribute
46 /* Low bit number of the specified field */
47 #define EF4_LOW_BIT(field) EF4_VAL(field, LBN)
48 /* Bit width of the specified field */
49 #define EF4_WIDTH(field) EF4_VAL(field, WIDTH)
50 /* High bit number of the specified field */
51 #define EF4_HIGH_BIT(field) (EF4_LOW_BIT(field) + EF4_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 EF4_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  * EF4_MASK64 for higher width fields.
68  */
69 #define EF4_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 ef4_dword {
75 	__le32 u32[1];
76 } ef4_dword_t;
77 
78 /* A quadword (i.e. 8 byte) datatype - little-endian in HW */
79 typedef union ef4_qword {
80 	__le64 u64[1];
81 	__le32 u32[2];
82 	ef4_dword_t dword[2];
83 } ef4_qword_t;
84 
85 /* An octword (eight-word, i.e. 16 byte) datatype - little-endian in HW */
86 typedef union ef4_oword {
87 	__le64 u64[2];
88 	ef4_qword_t qword[2];
89 	__le32 u32[4];
90 	ef4_dword_t dword[4];
91 } ef4_oword_t;
92 
93 /* Format string and value expanders for printk */
94 #define EF4_DWORD_FMT "%08x"
95 #define EF4_QWORD_FMT "%08x:%08x"
96 #define EF4_OWORD_FMT "%08x:%08x:%08x:%08x"
97 #define EF4_DWORD_VAL(dword)				\
98 	((unsigned int) le32_to_cpu((dword).u32[0]))
99 #define EF4_QWORD_VAL(qword)				\
100 	((unsigned int) le32_to_cpu((qword).u32[1])),	\
101 	((unsigned int) le32_to_cpu((qword).u32[0]))
102 #define EF4_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 EF4_EXTRACT ( element, 32, 63, 28, 45 ) would give
117  *
118  *   ( element ) << 4
119  *
120  * The result will contain the relevant bits filled in the range
121  * [0,high-low), with garbage in bits [high-low+1,...).
122  */
123 #define EF4_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 EF4_EXTRACT64(element, min, max, low, high)			\
134 	EF4_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 EF4_EXTRACT32(element, min, max, low, high)			\
141 	EF4_EXTRACT_NATIVE(le32_to_cpu(element), min, max, low, high)
142 
143 #define EF4_EXTRACT_OWORD64(oword, low, high)				\
144 	((EF4_EXTRACT64((oword).u64[0], 0, 63, low, high) |		\
145 	  EF4_EXTRACT64((oword).u64[1], 64, 127, low, high)) &		\
146 	 EF4_MASK64((high) + 1 - (low)))
147 
148 #define EF4_EXTRACT_QWORD64(qword, low, high)				\
149 	(EF4_EXTRACT64((qword).u64[0], 0, 63, low, high) &		\
150 	 EF4_MASK64((high) + 1 - (low)))
151 
152 #define EF4_EXTRACT_OWORD32(oword, low, high)				\
153 	((EF4_EXTRACT32((oword).u32[0], 0, 31, low, high) |		\
154 	  EF4_EXTRACT32((oword).u32[1], 32, 63, low, high) |		\
155 	  EF4_EXTRACT32((oword).u32[2], 64, 95, low, high) |		\
156 	  EF4_EXTRACT32((oword).u32[3], 96, 127, low, high)) &		\
157 	 EF4_MASK32((high) + 1 - (low)))
158 
159 #define EF4_EXTRACT_QWORD32(qword, low, high)				\
160 	((EF4_EXTRACT32((qword).u32[0], 0, 31, low, high) |		\
161 	  EF4_EXTRACT32((qword).u32[1], 32, 63, low, high)) &		\
162 	 EF4_MASK32((high) + 1 - (low)))
163 
164 #define EF4_EXTRACT_DWORD(dword, low, high)			\
165 	(EF4_EXTRACT32((dword).u32[0], 0, 31, low, high) &	\
166 	 EF4_MASK32((high) + 1 - (low)))
167 
168 #define EF4_OWORD_FIELD64(oword, field)				\
169 	EF4_EXTRACT_OWORD64(oword, EF4_LOW_BIT(field),		\
170 			    EF4_HIGH_BIT(field))
171 
172 #define EF4_QWORD_FIELD64(qword, field)				\
173 	EF4_EXTRACT_QWORD64(qword, EF4_LOW_BIT(field),		\
174 			    EF4_HIGH_BIT(field))
175 
176 #define EF4_OWORD_FIELD32(oword, field)				\
177 	EF4_EXTRACT_OWORD32(oword, EF4_LOW_BIT(field),		\
178 			    EF4_HIGH_BIT(field))
179 
180 #define EF4_QWORD_FIELD32(qword, field)				\
181 	EF4_EXTRACT_QWORD32(qword, EF4_LOW_BIT(field),		\
182 			    EF4_HIGH_BIT(field))
183 
184 #define EF4_DWORD_FIELD(dword, field)				\
185 	EF4_EXTRACT_DWORD(dword, EF4_LOW_BIT(field),		\
186 			  EF4_HIGH_BIT(field))
187 
188 #define EF4_OWORD_IS_ZERO64(oword)					\
189 	(((oword).u64[0] | (oword).u64[1]) == (__force __le64) 0)
190 
191 #define EF4_QWORD_IS_ZERO64(qword)					\
192 	(((qword).u64[0]) == (__force __le64) 0)
193 
194 #define EF4_OWORD_IS_ZERO32(oword)					     \
195 	(((oword).u32[0] | (oword).u32[1] | (oword).u32[2] | (oword).u32[3]) \
196 	 == (__force __le32) 0)
197 
198 #define EF4_QWORD_IS_ZERO32(qword)					\
199 	(((qword).u32[0] | (qword).u32[1]) == (__force __le32) 0)
200 
201 #define EF4_DWORD_IS_ZERO(dword)					\
202 	(((dword).u32[0]) == (__force __le32) 0)
203 
204 #define EF4_OWORD_IS_ALL_ONES64(oword)					\
205 	(((oword).u64[0] & (oword).u64[1]) == ~((__force __le64) 0))
206 
207 #define EF4_QWORD_IS_ALL_ONES64(qword)					\
208 	((qword).u64[0] == ~((__force __le64) 0))
209 
210 #define EF4_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 EF4_QWORD_IS_ALL_ONES32(qword)					\
215 	(((qword).u32[0] & (qword).u32[1]) == ~((__force __le32) 0))
216 
217 #define EF4_DWORD_IS_ALL_ONES(dword)					\
218 	((dword).u32[0] == ~((__force __le32) 0))
219 
220 #if BITS_PER_LONG == 64
221 #define EF4_OWORD_FIELD		EF4_OWORD_FIELD64
222 #define EF4_QWORD_FIELD		EF4_QWORD_FIELD64
223 #define EF4_OWORD_IS_ZERO	EF4_OWORD_IS_ZERO64
224 #define EF4_QWORD_IS_ZERO	EF4_QWORD_IS_ZERO64
225 #define EF4_OWORD_IS_ALL_ONES	EF4_OWORD_IS_ALL_ONES64
226 #define EF4_QWORD_IS_ALL_ONES	EF4_QWORD_IS_ALL_ONES64
227 #else
228 #define EF4_OWORD_FIELD		EF4_OWORD_FIELD32
229 #define EF4_QWORD_FIELD		EF4_QWORD_FIELD32
230 #define EF4_OWORD_IS_ZERO	EF4_OWORD_IS_ZERO32
231 #define EF4_QWORD_IS_ZERO	EF4_QWORD_IS_ZERO32
232 #define EF4_OWORD_IS_ALL_ONES	EF4_OWORD_IS_ALL_ONES32
233 #define EF4_QWORD_IS_ALL_ONES	EF4_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 EF4_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 EF4_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 EF4_INSERT_NATIVE(min, max, low, high, value)		\
255 	((((max - min) >= 32) || ((high - low) >= 32)) ?	\
256 	 EF4_INSERT_NATIVE64(min, max, low, high, value) :	\
257 	 EF4_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 EF4_INSERT_FIELD_NATIVE(min, max, field, value)		\
266 	EF4_INSERT_NATIVE(min, max, EF4_LOW_BIT(field),		\
267 			  EF4_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 EF4_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 	(EF4_INSERT_FIELD_NATIVE((min), (max), field1, (value1)) |	\
287 	 EF4_INSERT_FIELD_NATIVE((min), (max), field2, (value2)) |	\
288 	 EF4_INSERT_FIELD_NATIVE((min), (max), field3, (value3)) |	\
289 	 EF4_INSERT_FIELD_NATIVE((min), (max), field4, (value4)) |	\
290 	 EF4_INSERT_FIELD_NATIVE((min), (max), field5, (value5)) |	\
291 	 EF4_INSERT_FIELD_NATIVE((min), (max), field6, (value6)) |	\
292 	 EF4_INSERT_FIELD_NATIVE((min), (max), field7, (value7)) |	\
293 	 EF4_INSERT_FIELD_NATIVE((min), (max), field8, (value8)) |	\
294 	 EF4_INSERT_FIELD_NATIVE((min), (max), field9, (value9)) |	\
295 	 EF4_INSERT_FIELD_NATIVE((min), (max), field10, (value10)))
296 
297 #define EF4_INSERT_FIELDS64(...)				\
298 	cpu_to_le64(EF4_INSERT_FIELDS_NATIVE(__VA_ARGS__))
299 
300 #define EF4_INSERT_FIELDS32(...)				\
301 	cpu_to_le32(EF4_INSERT_FIELDS_NATIVE(__VA_ARGS__))
302 
303 #define EF4_POPULATE_OWORD64(oword, ...) do {				\
304 	(oword).u64[0] = EF4_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
305 	(oword).u64[1] = EF4_INSERT_FIELDS64(64, 127, __VA_ARGS__);	\
306 	} while (0)
307 
308 #define EF4_POPULATE_QWORD64(qword, ...) do {				\
309 	(qword).u64[0] = EF4_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
310 	} while (0)
311 
312 #define EF4_POPULATE_OWORD32(oword, ...) do {				\
313 	(oword).u32[0] = EF4_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
314 	(oword).u32[1] = EF4_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
315 	(oword).u32[2] = EF4_INSERT_FIELDS32(64, 95, __VA_ARGS__);	\
316 	(oword).u32[3] = EF4_INSERT_FIELDS32(96, 127, __VA_ARGS__);	\
317 	} while (0)
318 
319 #define EF4_POPULATE_QWORD32(qword, ...) do {				\
320 	(qword).u32[0] = EF4_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
321 	(qword).u32[1] = EF4_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
322 	} while (0)
323 
324 #define EF4_POPULATE_DWORD(dword, ...) do {				\
325 	(dword).u32[0] = EF4_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
326 	} while (0)
327 
328 #if BITS_PER_LONG == 64
329 #define EF4_POPULATE_OWORD EF4_POPULATE_OWORD64
330 #define EF4_POPULATE_QWORD EF4_POPULATE_QWORD64
331 #else
332 #define EF4_POPULATE_OWORD EF4_POPULATE_OWORD32
333 #define EF4_POPULATE_QWORD EF4_POPULATE_QWORD32
334 #endif
335 
336 /* Populate an octword field with various numbers of arguments */
337 #define EF4_POPULATE_OWORD_10 EF4_POPULATE_OWORD
338 #define EF4_POPULATE_OWORD_9(oword, ...) \
339 	EF4_POPULATE_OWORD_10(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
340 #define EF4_POPULATE_OWORD_8(oword, ...) \
341 	EF4_POPULATE_OWORD_9(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
342 #define EF4_POPULATE_OWORD_7(oword, ...) \
343 	EF4_POPULATE_OWORD_8(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
344 #define EF4_POPULATE_OWORD_6(oword, ...) \
345 	EF4_POPULATE_OWORD_7(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
346 #define EF4_POPULATE_OWORD_5(oword, ...) \
347 	EF4_POPULATE_OWORD_6(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
348 #define EF4_POPULATE_OWORD_4(oword, ...) \
349 	EF4_POPULATE_OWORD_5(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
350 #define EF4_POPULATE_OWORD_3(oword, ...) \
351 	EF4_POPULATE_OWORD_4(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
352 #define EF4_POPULATE_OWORD_2(oword, ...) \
353 	EF4_POPULATE_OWORD_3(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
354 #define EF4_POPULATE_OWORD_1(oword, ...) \
355 	EF4_POPULATE_OWORD_2(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
356 #define EF4_ZERO_OWORD(oword) \
357 	EF4_POPULATE_OWORD_1(oword, EF4_DUMMY_FIELD, 0)
358 #define EF4_SET_OWORD(oword) \
359 	EF4_POPULATE_OWORD_4(oword, \
360 			     EF4_DWORD_0, 0xffffffff, \
361 			     EF4_DWORD_1, 0xffffffff, \
362 			     EF4_DWORD_2, 0xffffffff, \
363 			     EF4_DWORD_3, 0xffffffff)
364 
365 /* Populate a quadword field with various numbers of arguments */
366 #define EF4_POPULATE_QWORD_10 EF4_POPULATE_QWORD
367 #define EF4_POPULATE_QWORD_9(qword, ...) \
368 	EF4_POPULATE_QWORD_10(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
369 #define EF4_POPULATE_QWORD_8(qword, ...) \
370 	EF4_POPULATE_QWORD_9(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
371 #define EF4_POPULATE_QWORD_7(qword, ...) \
372 	EF4_POPULATE_QWORD_8(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
373 #define EF4_POPULATE_QWORD_6(qword, ...) \
374 	EF4_POPULATE_QWORD_7(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
375 #define EF4_POPULATE_QWORD_5(qword, ...) \
376 	EF4_POPULATE_QWORD_6(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
377 #define EF4_POPULATE_QWORD_4(qword, ...) \
378 	EF4_POPULATE_QWORD_5(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
379 #define EF4_POPULATE_QWORD_3(qword, ...) \
380 	EF4_POPULATE_QWORD_4(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
381 #define EF4_POPULATE_QWORD_2(qword, ...) \
382 	EF4_POPULATE_QWORD_3(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
383 #define EF4_POPULATE_QWORD_1(qword, ...) \
384 	EF4_POPULATE_QWORD_2(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
385 #define EF4_ZERO_QWORD(qword) \
386 	EF4_POPULATE_QWORD_1(qword, EF4_DUMMY_FIELD, 0)
387 #define EF4_SET_QWORD(qword) \
388 	EF4_POPULATE_QWORD_2(qword, \
389 			     EF4_DWORD_0, 0xffffffff, \
390 			     EF4_DWORD_1, 0xffffffff)
391 
392 /* Populate a dword field with various numbers of arguments */
393 #define EF4_POPULATE_DWORD_10 EF4_POPULATE_DWORD
394 #define EF4_POPULATE_DWORD_9(dword, ...) \
395 	EF4_POPULATE_DWORD_10(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
396 #define EF4_POPULATE_DWORD_8(dword, ...) \
397 	EF4_POPULATE_DWORD_9(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
398 #define EF4_POPULATE_DWORD_7(dword, ...) \
399 	EF4_POPULATE_DWORD_8(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
400 #define EF4_POPULATE_DWORD_6(dword, ...) \
401 	EF4_POPULATE_DWORD_7(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
402 #define EF4_POPULATE_DWORD_5(dword, ...) \
403 	EF4_POPULATE_DWORD_6(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
404 #define EF4_POPULATE_DWORD_4(dword, ...) \
405 	EF4_POPULATE_DWORD_5(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
406 #define EF4_POPULATE_DWORD_3(dword, ...) \
407 	EF4_POPULATE_DWORD_4(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
408 #define EF4_POPULATE_DWORD_2(dword, ...) \
409 	EF4_POPULATE_DWORD_3(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
410 #define EF4_POPULATE_DWORD_1(dword, ...) \
411 	EF4_POPULATE_DWORD_2(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
412 #define EF4_ZERO_DWORD(dword) \
413 	EF4_POPULATE_DWORD_1(dword, EF4_DUMMY_FIELD, 0)
414 #define EF4_SET_DWORD(dword) \
415 	EF4_POPULATE_DWORD_1(dword, EF4_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 EF4_INVERT_OWORD(oword) do {		\
423 	(oword).u64[0] = ~((oword).u64[0]);	\
424 	(oword).u64[1] = ~((oword).u64[1]);	\
425 	} while (0)
426 
427 #define EF4_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 EF4_OR_OWORD(oword, from, mask)				\
434 	do {							\
435 		(oword).u64[0] = (from).u64[0] | (mask).u64[0];	\
436 		(oword).u64[1] = (from).u64[1] | (mask).u64[1];	\
437 	} while (0)
438 
439 #define EF4_INSERT64(min, max, low, high, value)			\
440 	cpu_to_le64(EF4_INSERT_NATIVE(min, max, low, high, value))
441 
442 #define EF4_INSERT32(min, max, low, high, value)			\
443 	cpu_to_le32(EF4_INSERT_NATIVE(min, max, low, high, value))
444 
445 #define EF4_INPLACE_MASK64(min, max, low, high)				\
446 	EF4_INSERT64(min, max, low, high, EF4_MASK64((high) + 1 - (low)))
447 
448 #define EF4_INPLACE_MASK32(min, max, low, high)				\
449 	EF4_INSERT32(min, max, low, high, EF4_MASK32((high) + 1 - (low)))
450 
451 #define EF4_SET_OWORD64(oword, low, high, value) do {			\
452 	(oword).u64[0] = (((oword).u64[0]				\
453 			   & ~EF4_INPLACE_MASK64(0,  63, low, high))	\
454 			  | EF4_INSERT64(0,  63, low, high, value));	\
455 	(oword).u64[1] = (((oword).u64[1]				\
456 			   & ~EF4_INPLACE_MASK64(64, 127, low, high))	\
457 			  | EF4_INSERT64(64, 127, low, high, value));	\
458 	} while (0)
459 
460 #define EF4_SET_QWORD64(qword, low, high, value) do {			\
461 	(qword).u64[0] = (((qword).u64[0]				\
462 			   & ~EF4_INPLACE_MASK64(0, 63, low, high))	\
463 			  | EF4_INSERT64(0, 63, low, high, value));	\
464 	} while (0)
465 
466 #define EF4_SET_OWORD32(oword, low, high, value) do {			\
467 	(oword).u32[0] = (((oword).u32[0]				\
468 			   & ~EF4_INPLACE_MASK32(0, 31, low, high))	\
469 			  | EF4_INSERT32(0, 31, low, high, value));	\
470 	(oword).u32[1] = (((oword).u32[1]				\
471 			   & ~EF4_INPLACE_MASK32(32, 63, low, high))	\
472 			  | EF4_INSERT32(32, 63, low, high, value));	\
473 	(oword).u32[2] = (((oword).u32[2]				\
474 			   & ~EF4_INPLACE_MASK32(64, 95, low, high))	\
475 			  | EF4_INSERT32(64, 95, low, high, value));	\
476 	(oword).u32[3] = (((oword).u32[3]				\
477 			   & ~EF4_INPLACE_MASK32(96, 127, low, high))	\
478 			  | EF4_INSERT32(96, 127, low, high, value));	\
479 	} while (0)
480 
481 #define EF4_SET_QWORD32(qword, low, high, value) do {			\
482 	(qword).u32[0] = (((qword).u32[0]				\
483 			   & ~EF4_INPLACE_MASK32(0, 31, low, high))	\
484 			  | EF4_INSERT32(0, 31, low, high, value));	\
485 	(qword).u32[1] = (((qword).u32[1]				\
486 			   & ~EF4_INPLACE_MASK32(32, 63, low, high))	\
487 			  | EF4_INSERT32(32, 63, low, high, value));	\
488 	} while (0)
489 
490 #define EF4_SET_DWORD32(dword, low, high, value) do {			\
491 	(dword).u32[0] = (((dword).u32[0]				\
492 			   & ~EF4_INPLACE_MASK32(0, 31, low, high))	\
493 			  | EF4_INSERT32(0, 31, low, high, value));	\
494 	} while (0)
495 
496 #define EF4_SET_OWORD_FIELD64(oword, field, value)			\
497 	EF4_SET_OWORD64(oword, EF4_LOW_BIT(field),			\
498 			 EF4_HIGH_BIT(field), value)
499 
500 #define EF4_SET_QWORD_FIELD64(qword, field, value)			\
501 	EF4_SET_QWORD64(qword, EF4_LOW_BIT(field),			\
502 			 EF4_HIGH_BIT(field), value)
503 
504 #define EF4_SET_OWORD_FIELD32(oword, field, value)			\
505 	EF4_SET_OWORD32(oword, EF4_LOW_BIT(field),			\
506 			 EF4_HIGH_BIT(field), value)
507 
508 #define EF4_SET_QWORD_FIELD32(qword, field, value)			\
509 	EF4_SET_QWORD32(qword, EF4_LOW_BIT(field),			\
510 			 EF4_HIGH_BIT(field), value)
511 
512 #define EF4_SET_DWORD_FIELD(dword, field, value)			\
513 	EF4_SET_DWORD32(dword, EF4_LOW_BIT(field),			\
514 			 EF4_HIGH_BIT(field), value)
515 
516 
517 
518 #if BITS_PER_LONG == 64
519 #define EF4_SET_OWORD_FIELD EF4_SET_OWORD_FIELD64
520 #define EF4_SET_QWORD_FIELD EF4_SET_QWORD_FIELD64
521 #else
522 #define EF4_SET_OWORD_FIELD EF4_SET_OWORD_FIELD32
523 #define EF4_SET_QWORD_FIELD EF4_SET_QWORD_FIELD32
524 #endif
525 
526 /* Used to avoid compiler warnings about shift range exceeding width
527  * of the data types when dma_addr_t is only 32 bits wide.
528  */
529 #define DMA_ADDR_T_WIDTH	(8 * sizeof(dma_addr_t))
530 #define EF4_DMA_TYPE_WIDTH(width) \
531 	(((width) < DMA_ADDR_T_WIDTH) ? (width) : DMA_ADDR_T_WIDTH)
532 
533 
534 /* Static initialiser */
535 #define EF4_OWORD32(a, b, c, d)				\
536 	{ .u32 = { cpu_to_le32(a), cpu_to_le32(b),	\
537 		   cpu_to_le32(c), cpu_to_le32(d) } }
538 
539 #endif /* EF4_BITFIELD_H */
540