xref: /linux/drivers/net/ethernet/sfc/bitfield.h (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2874aeea5SJeff Kirsher /****************************************************************************
3f7a6d2c4SBen Hutchings  * Driver for Solarflare network controllers and boards
4874aeea5SJeff Kirsher  * Copyright 2005-2006 Fen Systems Ltd.
5f7a6d2c4SBen Hutchings  * Copyright 2006-2013 Solarflare Communications Inc.
6874aeea5SJeff Kirsher  */
7874aeea5SJeff Kirsher 
8874aeea5SJeff Kirsher #ifndef EFX_BITFIELD_H
9874aeea5SJeff Kirsher #define EFX_BITFIELD_H
10874aeea5SJeff Kirsher 
11874aeea5SJeff Kirsher /*
12874aeea5SJeff Kirsher  * Efx bitfield access
13874aeea5SJeff Kirsher  *
14874aeea5SJeff Kirsher  * Efx NICs make extensive use of bitfields up to 128 bits
15874aeea5SJeff Kirsher  * wide.  Since there is no native 128-bit datatype on most systems,
16874aeea5SJeff Kirsher  * and since 64-bit datatypes are inefficient on 32-bit systems and
17874aeea5SJeff Kirsher  * vice versa, we wrap accesses in a way that uses the most efficient
18874aeea5SJeff Kirsher  * datatype.
19874aeea5SJeff Kirsher  *
20874aeea5SJeff Kirsher  * The NICs are PCI devices and therefore little-endian.  Since most
21874aeea5SJeff Kirsher  * of the quantities that we deal with are DMAed to/from host memory,
22874aeea5SJeff Kirsher  * we define our datatypes (efx_oword_t, efx_qword_t and
23874aeea5SJeff Kirsher  * efx_dword_t) to be little-endian.
24874aeea5SJeff Kirsher  */
25874aeea5SJeff Kirsher 
26874aeea5SJeff Kirsher /* Lowest bit numbers and widths */
27874aeea5SJeff Kirsher #define EFX_DUMMY_FIELD_LBN 0
28874aeea5SJeff Kirsher #define EFX_DUMMY_FIELD_WIDTH 0
298127d661SBen Hutchings #define EFX_WORD_0_LBN 0
308127d661SBen Hutchings #define EFX_WORD_0_WIDTH 16
318127d661SBen Hutchings #define EFX_WORD_1_LBN 16
328127d661SBen Hutchings #define EFX_WORD_1_WIDTH 16
33874aeea5SJeff Kirsher #define EFX_DWORD_0_LBN 0
34874aeea5SJeff Kirsher #define EFX_DWORD_0_WIDTH 32
35874aeea5SJeff Kirsher #define EFX_DWORD_1_LBN 32
36874aeea5SJeff Kirsher #define EFX_DWORD_1_WIDTH 32
37874aeea5SJeff Kirsher #define EFX_DWORD_2_LBN 64
38874aeea5SJeff Kirsher #define EFX_DWORD_2_WIDTH 32
39874aeea5SJeff Kirsher #define EFX_DWORD_3_LBN 96
40874aeea5SJeff Kirsher #define EFX_DWORD_3_WIDTH 32
41874aeea5SJeff Kirsher #define EFX_QWORD_0_LBN 0
42874aeea5SJeff Kirsher #define EFX_QWORD_0_WIDTH 64
43874aeea5SJeff Kirsher 
44874aeea5SJeff Kirsher /* Specified attribute (e.g. LBN) of the specified field */
45874aeea5SJeff Kirsher #define EFX_VAL(field, attribute) field ## _ ## attribute
46874aeea5SJeff Kirsher /* Low bit number of the specified field */
47874aeea5SJeff Kirsher #define EFX_LOW_BIT(field) EFX_VAL(field, LBN)
48874aeea5SJeff Kirsher /* Bit width of the specified field */
49874aeea5SJeff Kirsher #define EFX_WIDTH(field) EFX_VAL(field, WIDTH)
50874aeea5SJeff Kirsher /* High bit number of the specified field */
51874aeea5SJeff Kirsher #define EFX_HIGH_BIT(field) (EFX_LOW_BIT(field) + EFX_WIDTH(field) - 1)
52874aeea5SJeff Kirsher /* Mask equal in width to the specified field.
53874aeea5SJeff Kirsher  *
54874aeea5SJeff Kirsher  * For example, a field with width 5 would have a mask of 0x1f.
55874aeea5SJeff Kirsher  *
56874aeea5SJeff Kirsher  * The maximum width mask that can be generated is 64 bits.
57874aeea5SJeff Kirsher  */
58874aeea5SJeff Kirsher #define EFX_MASK64(width)			\
59874aeea5SJeff Kirsher 	((width) == 64 ? ~((u64) 0) :		\
60874aeea5SJeff Kirsher 	 (((((u64) 1) << (width))) - 1))
61874aeea5SJeff Kirsher 
62874aeea5SJeff Kirsher /* Mask equal in width to the specified field.
63874aeea5SJeff Kirsher  *
64874aeea5SJeff Kirsher  * For example, a field with width 5 would have a mask of 0x1f.
65874aeea5SJeff Kirsher  *
66874aeea5SJeff Kirsher  * The maximum width mask that can be generated is 32 bits.  Use
67874aeea5SJeff Kirsher  * EFX_MASK64 for higher width fields.
68874aeea5SJeff Kirsher  */
69874aeea5SJeff Kirsher #define EFX_MASK32(width)			\
70874aeea5SJeff Kirsher 	((width) == 32 ? ~((u32) 0) :		\
71874aeea5SJeff Kirsher 	 (((((u32) 1) << (width))) - 1))
72874aeea5SJeff Kirsher 
73874aeea5SJeff Kirsher /* A doubleword (i.e. 4 byte) datatype - little-endian in HW */
74874aeea5SJeff Kirsher typedef union efx_dword {
75874aeea5SJeff Kirsher 	__le32 u32[1];
76874aeea5SJeff Kirsher } efx_dword_t;
77874aeea5SJeff Kirsher 
78874aeea5SJeff Kirsher /* A quadword (i.e. 8 byte) datatype - little-endian in HW */
79874aeea5SJeff Kirsher typedef union efx_qword {
80874aeea5SJeff Kirsher 	__le64 u64[1];
81874aeea5SJeff Kirsher 	__le32 u32[2];
82874aeea5SJeff Kirsher 	efx_dword_t dword[2];
83874aeea5SJeff Kirsher } efx_qword_t;
84874aeea5SJeff Kirsher 
85874aeea5SJeff Kirsher /* An octword (eight-word, i.e. 16 byte) datatype - little-endian in HW */
86874aeea5SJeff Kirsher typedef union efx_oword {
87874aeea5SJeff Kirsher 	__le64 u64[2];
88874aeea5SJeff Kirsher 	efx_qword_t qword[2];
89874aeea5SJeff Kirsher 	__le32 u32[4];
90874aeea5SJeff Kirsher 	efx_dword_t dword[4];
91874aeea5SJeff Kirsher } efx_oword_t;
92874aeea5SJeff Kirsher 
93874aeea5SJeff Kirsher /* Format string and value expanders for printk */
94874aeea5SJeff Kirsher #define EFX_DWORD_FMT "%08x"
95874aeea5SJeff Kirsher #define EFX_QWORD_FMT "%08x:%08x"
96874aeea5SJeff Kirsher #define EFX_OWORD_FMT "%08x:%08x:%08x:%08x"
97874aeea5SJeff Kirsher #define EFX_DWORD_VAL(dword)				\
98874aeea5SJeff Kirsher 	((unsigned int) le32_to_cpu((dword).u32[0]))
99874aeea5SJeff Kirsher #define EFX_QWORD_VAL(qword)				\
100874aeea5SJeff Kirsher 	((unsigned int) le32_to_cpu((qword).u32[1])),	\
101874aeea5SJeff Kirsher 	((unsigned int) le32_to_cpu((qword).u32[0]))
102874aeea5SJeff Kirsher #define EFX_OWORD_VAL(oword)				\
103874aeea5SJeff Kirsher 	((unsigned int) le32_to_cpu((oword).u32[3])),	\
104874aeea5SJeff Kirsher 	((unsigned int) le32_to_cpu((oword).u32[2])),	\
105874aeea5SJeff Kirsher 	((unsigned int) le32_to_cpu((oword).u32[1])),	\
106874aeea5SJeff Kirsher 	((unsigned int) le32_to_cpu((oword).u32[0]))
107874aeea5SJeff Kirsher 
108874aeea5SJeff Kirsher /*
109874aeea5SJeff Kirsher  * Extract bit field portion [low,high) from the native-endian element
110874aeea5SJeff Kirsher  * which contains bits [min,max).
111874aeea5SJeff Kirsher  *
112874aeea5SJeff Kirsher  * For example, suppose "element" represents the high 32 bits of a
113874aeea5SJeff Kirsher  * 64-bit value, and we wish to extract the bits belonging to the bit
114874aeea5SJeff Kirsher  * field occupying bits 28-45 of this 64-bit value.
115874aeea5SJeff Kirsher  *
116874aeea5SJeff Kirsher  * Then EFX_EXTRACT ( element, 32, 63, 28, 45 ) would give
117874aeea5SJeff Kirsher  *
118874aeea5SJeff Kirsher  *   ( element ) << 4
119874aeea5SJeff Kirsher  *
120874aeea5SJeff Kirsher  * The result will contain the relevant bits filled in in the range
121874aeea5SJeff Kirsher  * [0,high-low), with garbage in bits [high-low+1,...).
122874aeea5SJeff Kirsher  */
123874aeea5SJeff Kirsher #define EFX_EXTRACT_NATIVE(native_element, min, max, low, high)		\
1249724a850SBen Hutchings 	((low) > (max) || (high) < (min) ? 0 :				\
1259724a850SBen Hutchings 	 (low) > (min) ?						\
1269724a850SBen Hutchings 	 (native_element) >> ((low) - (min)) :				\
1279724a850SBen Hutchings 	 (native_element) << ((min) - (low)))
128874aeea5SJeff Kirsher 
129874aeea5SJeff Kirsher /*
130874aeea5SJeff Kirsher  * Extract bit field portion [low,high) from the 64-bit little-endian
131874aeea5SJeff Kirsher  * element which contains bits [min,max)
132874aeea5SJeff Kirsher  */
133874aeea5SJeff Kirsher #define EFX_EXTRACT64(element, min, max, low, high)			\
134874aeea5SJeff Kirsher 	EFX_EXTRACT_NATIVE(le64_to_cpu(element), min, max, low, high)
135874aeea5SJeff Kirsher 
136874aeea5SJeff Kirsher /*
137874aeea5SJeff Kirsher  * Extract bit field portion [low,high) from the 32-bit little-endian
138874aeea5SJeff Kirsher  * element which contains bits [min,max)
139874aeea5SJeff Kirsher  */
140874aeea5SJeff Kirsher #define EFX_EXTRACT32(element, min, max, low, high)			\
141874aeea5SJeff Kirsher 	EFX_EXTRACT_NATIVE(le32_to_cpu(element), min, max, low, high)
142874aeea5SJeff Kirsher 
143874aeea5SJeff Kirsher #define EFX_EXTRACT_OWORD64(oword, low, high)				\
144874aeea5SJeff Kirsher 	((EFX_EXTRACT64((oword).u64[0], 0, 63, low, high) |		\
145874aeea5SJeff Kirsher 	  EFX_EXTRACT64((oword).u64[1], 64, 127, low, high)) &		\
1469724a850SBen Hutchings 	 EFX_MASK64((high) + 1 - (low)))
147874aeea5SJeff Kirsher 
148874aeea5SJeff Kirsher #define EFX_EXTRACT_QWORD64(qword, low, high)				\
149874aeea5SJeff Kirsher 	(EFX_EXTRACT64((qword).u64[0], 0, 63, low, high) &		\
1509724a850SBen Hutchings 	 EFX_MASK64((high) + 1 - (low)))
151874aeea5SJeff Kirsher 
152874aeea5SJeff Kirsher #define EFX_EXTRACT_OWORD32(oword, low, high)				\
153874aeea5SJeff Kirsher 	((EFX_EXTRACT32((oword).u32[0], 0, 31, low, high) |		\
154874aeea5SJeff Kirsher 	  EFX_EXTRACT32((oword).u32[1], 32, 63, low, high) |		\
155874aeea5SJeff Kirsher 	  EFX_EXTRACT32((oword).u32[2], 64, 95, low, high) |		\
156874aeea5SJeff Kirsher 	  EFX_EXTRACT32((oword).u32[3], 96, 127, low, high)) &		\
1579724a850SBen Hutchings 	 EFX_MASK32((high) + 1 - (low)))
158874aeea5SJeff Kirsher 
159874aeea5SJeff Kirsher #define EFX_EXTRACT_QWORD32(qword, low, high)				\
160874aeea5SJeff Kirsher 	((EFX_EXTRACT32((qword).u32[0], 0, 31, low, high) |		\
161874aeea5SJeff Kirsher 	  EFX_EXTRACT32((qword).u32[1], 32, 63, low, high)) &		\
1629724a850SBen Hutchings 	 EFX_MASK32((high) + 1 - (low)))
163874aeea5SJeff Kirsher 
164874aeea5SJeff Kirsher #define EFX_EXTRACT_DWORD(dword, low, high)			\
165874aeea5SJeff Kirsher 	(EFX_EXTRACT32((dword).u32[0], 0, 31, low, high) &	\
1669724a850SBen Hutchings 	 EFX_MASK32((high) + 1 - (low)))
167874aeea5SJeff Kirsher 
168874aeea5SJeff Kirsher #define EFX_OWORD_FIELD64(oword, field)				\
169874aeea5SJeff Kirsher 	EFX_EXTRACT_OWORD64(oword, EFX_LOW_BIT(field),		\
170874aeea5SJeff Kirsher 			    EFX_HIGH_BIT(field))
171874aeea5SJeff Kirsher 
172874aeea5SJeff Kirsher #define EFX_QWORD_FIELD64(qword, field)				\
173874aeea5SJeff Kirsher 	EFX_EXTRACT_QWORD64(qword, EFX_LOW_BIT(field),		\
174874aeea5SJeff Kirsher 			    EFX_HIGH_BIT(field))
175874aeea5SJeff Kirsher 
176874aeea5SJeff Kirsher #define EFX_OWORD_FIELD32(oword, field)				\
177874aeea5SJeff Kirsher 	EFX_EXTRACT_OWORD32(oword, EFX_LOW_BIT(field),		\
178874aeea5SJeff Kirsher 			    EFX_HIGH_BIT(field))
179874aeea5SJeff Kirsher 
180874aeea5SJeff Kirsher #define EFX_QWORD_FIELD32(qword, field)				\
181874aeea5SJeff Kirsher 	EFX_EXTRACT_QWORD32(qword, EFX_LOW_BIT(field),		\
182874aeea5SJeff Kirsher 			    EFX_HIGH_BIT(field))
183874aeea5SJeff Kirsher 
184874aeea5SJeff Kirsher #define EFX_DWORD_FIELD(dword, field)				\
185874aeea5SJeff Kirsher 	EFX_EXTRACT_DWORD(dword, EFX_LOW_BIT(field),		\
186874aeea5SJeff Kirsher 			  EFX_HIGH_BIT(field))
187874aeea5SJeff Kirsher 
188874aeea5SJeff Kirsher #define EFX_OWORD_IS_ZERO64(oword)					\
189874aeea5SJeff Kirsher 	(((oword).u64[0] | (oword).u64[1]) == (__force __le64) 0)
190874aeea5SJeff Kirsher 
191874aeea5SJeff Kirsher #define EFX_QWORD_IS_ZERO64(qword)					\
192874aeea5SJeff Kirsher 	(((qword).u64[0]) == (__force __le64) 0)
193874aeea5SJeff Kirsher 
194874aeea5SJeff Kirsher #define EFX_OWORD_IS_ZERO32(oword)					     \
195874aeea5SJeff Kirsher 	(((oword).u32[0] | (oword).u32[1] | (oword).u32[2] | (oword).u32[3]) \
196874aeea5SJeff Kirsher 	 == (__force __le32) 0)
197874aeea5SJeff Kirsher 
198874aeea5SJeff Kirsher #define EFX_QWORD_IS_ZERO32(qword)					\
199874aeea5SJeff Kirsher 	(((qword).u32[0] | (qword).u32[1]) == (__force __le32) 0)
200874aeea5SJeff Kirsher 
201874aeea5SJeff Kirsher #define EFX_DWORD_IS_ZERO(dword)					\
202874aeea5SJeff Kirsher 	(((dword).u32[0]) == (__force __le32) 0)
203874aeea5SJeff Kirsher 
204874aeea5SJeff Kirsher #define EFX_OWORD_IS_ALL_ONES64(oword)					\
205874aeea5SJeff Kirsher 	(((oword).u64[0] & (oword).u64[1]) == ~((__force __le64) 0))
206874aeea5SJeff Kirsher 
207874aeea5SJeff Kirsher #define EFX_QWORD_IS_ALL_ONES64(qword)					\
208874aeea5SJeff Kirsher 	((qword).u64[0] == ~((__force __le64) 0))
209874aeea5SJeff Kirsher 
210874aeea5SJeff Kirsher #define EFX_OWORD_IS_ALL_ONES32(oword)					\
211874aeea5SJeff Kirsher 	(((oword).u32[0] & (oword).u32[1] & (oword).u32[2] & (oword).u32[3]) \
212874aeea5SJeff Kirsher 	 == ~((__force __le32) 0))
213874aeea5SJeff Kirsher 
214874aeea5SJeff Kirsher #define EFX_QWORD_IS_ALL_ONES32(qword)					\
215874aeea5SJeff Kirsher 	(((qword).u32[0] & (qword).u32[1]) == ~((__force __le32) 0))
216874aeea5SJeff Kirsher 
217874aeea5SJeff Kirsher #define EFX_DWORD_IS_ALL_ONES(dword)					\
218874aeea5SJeff Kirsher 	((dword).u32[0] == ~((__force __le32) 0))
219874aeea5SJeff Kirsher 
220874aeea5SJeff Kirsher #if BITS_PER_LONG == 64
221874aeea5SJeff Kirsher #define EFX_OWORD_FIELD		EFX_OWORD_FIELD64
222874aeea5SJeff Kirsher #define EFX_QWORD_FIELD		EFX_QWORD_FIELD64
223874aeea5SJeff Kirsher #define EFX_OWORD_IS_ZERO	EFX_OWORD_IS_ZERO64
224874aeea5SJeff Kirsher #define EFX_QWORD_IS_ZERO	EFX_QWORD_IS_ZERO64
225874aeea5SJeff Kirsher #define EFX_OWORD_IS_ALL_ONES	EFX_OWORD_IS_ALL_ONES64
226874aeea5SJeff Kirsher #define EFX_QWORD_IS_ALL_ONES	EFX_QWORD_IS_ALL_ONES64
227874aeea5SJeff Kirsher #else
228874aeea5SJeff Kirsher #define EFX_OWORD_FIELD		EFX_OWORD_FIELD32
229874aeea5SJeff Kirsher #define EFX_QWORD_FIELD		EFX_QWORD_FIELD32
230874aeea5SJeff Kirsher #define EFX_OWORD_IS_ZERO	EFX_OWORD_IS_ZERO32
231874aeea5SJeff Kirsher #define EFX_QWORD_IS_ZERO	EFX_QWORD_IS_ZERO32
232874aeea5SJeff Kirsher #define EFX_OWORD_IS_ALL_ONES	EFX_OWORD_IS_ALL_ONES32
233874aeea5SJeff Kirsher #define EFX_QWORD_IS_ALL_ONES	EFX_QWORD_IS_ALL_ONES32
234874aeea5SJeff Kirsher #endif
235874aeea5SJeff Kirsher 
236874aeea5SJeff Kirsher /*
237874aeea5SJeff Kirsher  * Construct bit field portion
238874aeea5SJeff Kirsher  *
239874aeea5SJeff Kirsher  * Creates the portion of the bit field [low,high) that lies within
240874aeea5SJeff Kirsher  * the range [min,max).
241874aeea5SJeff Kirsher  */
242874aeea5SJeff Kirsher #define EFX_INSERT_NATIVE64(min, max, low, high, value)		\
243874aeea5SJeff Kirsher 	(((low > max) || (high < min)) ? 0 :			\
244874aeea5SJeff Kirsher 	 ((low > min) ?						\
245874aeea5SJeff Kirsher 	  (((u64) (value)) << (low - min)) :		\
246874aeea5SJeff Kirsher 	  (((u64) (value)) >> (min - low))))
247874aeea5SJeff Kirsher 
248874aeea5SJeff Kirsher #define EFX_INSERT_NATIVE32(min, max, low, high, value)		\
249874aeea5SJeff Kirsher 	(((low > max) || (high < min)) ? 0 :			\
250874aeea5SJeff Kirsher 	 ((low > min) ?						\
251874aeea5SJeff Kirsher 	  (((u32) (value)) << (low - min)) :		\
252874aeea5SJeff Kirsher 	  (((u32) (value)) >> (min - low))))
253874aeea5SJeff Kirsher 
254874aeea5SJeff Kirsher #define EFX_INSERT_NATIVE(min, max, low, high, value)		\
255874aeea5SJeff Kirsher 	((((max - min) >= 32) || ((high - low) >= 32)) ?	\
256874aeea5SJeff Kirsher 	 EFX_INSERT_NATIVE64(min, max, low, high, value) :	\
257874aeea5SJeff Kirsher 	 EFX_INSERT_NATIVE32(min, max, low, high, value))
258874aeea5SJeff Kirsher 
259874aeea5SJeff Kirsher /*
260874aeea5SJeff Kirsher  * Construct bit field portion
261874aeea5SJeff Kirsher  *
262874aeea5SJeff Kirsher  * Creates the portion of the named bit field that lies within the
263874aeea5SJeff Kirsher  * range [min,max).
264874aeea5SJeff Kirsher  */
265874aeea5SJeff Kirsher #define EFX_INSERT_FIELD_NATIVE(min, max, field, value)		\
266874aeea5SJeff Kirsher 	EFX_INSERT_NATIVE(min, max, EFX_LOW_BIT(field),		\
267874aeea5SJeff Kirsher 			  EFX_HIGH_BIT(field), value)
268874aeea5SJeff Kirsher 
269874aeea5SJeff Kirsher /*
270874aeea5SJeff Kirsher  * Construct bit field
271874aeea5SJeff Kirsher  *
272874aeea5SJeff Kirsher  * Creates the portion of the named bit fields that lie within the
273874aeea5SJeff Kirsher  * range [min,max).
274874aeea5SJeff Kirsher  */
275874aeea5SJeff Kirsher #define EFX_INSERT_FIELDS_NATIVE(min, max,				\
276874aeea5SJeff Kirsher 				 field1, value1,			\
277874aeea5SJeff Kirsher 				 field2, value2,			\
278874aeea5SJeff Kirsher 				 field3, value3,			\
279874aeea5SJeff Kirsher 				 field4, value4,			\
280874aeea5SJeff Kirsher 				 field5, value5,			\
281874aeea5SJeff Kirsher 				 field6, value6,			\
282874aeea5SJeff Kirsher 				 field7, value7,			\
283874aeea5SJeff Kirsher 				 field8, value8,			\
284874aeea5SJeff Kirsher 				 field9, value9,			\
285874aeea5SJeff Kirsher 				 field10, value10)			\
286874aeea5SJeff Kirsher 	(EFX_INSERT_FIELD_NATIVE((min), (max), field1, (value1)) |	\
287874aeea5SJeff Kirsher 	 EFX_INSERT_FIELD_NATIVE((min), (max), field2, (value2)) |	\
288874aeea5SJeff Kirsher 	 EFX_INSERT_FIELD_NATIVE((min), (max), field3, (value3)) |	\
289874aeea5SJeff Kirsher 	 EFX_INSERT_FIELD_NATIVE((min), (max), field4, (value4)) |	\
290874aeea5SJeff Kirsher 	 EFX_INSERT_FIELD_NATIVE((min), (max), field5, (value5)) |	\
291874aeea5SJeff Kirsher 	 EFX_INSERT_FIELD_NATIVE((min), (max), field6, (value6)) |	\
292874aeea5SJeff Kirsher 	 EFX_INSERT_FIELD_NATIVE((min), (max), field7, (value7)) |	\
293874aeea5SJeff Kirsher 	 EFX_INSERT_FIELD_NATIVE((min), (max), field8, (value8)) |	\
294874aeea5SJeff Kirsher 	 EFX_INSERT_FIELD_NATIVE((min), (max), field9, (value9)) |	\
295874aeea5SJeff Kirsher 	 EFX_INSERT_FIELD_NATIVE((min), (max), field10, (value10)))
296874aeea5SJeff Kirsher 
297874aeea5SJeff Kirsher #define EFX_INSERT_FIELDS64(...)				\
298874aeea5SJeff Kirsher 	cpu_to_le64(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
299874aeea5SJeff Kirsher 
300874aeea5SJeff Kirsher #define EFX_INSERT_FIELDS32(...)				\
301874aeea5SJeff Kirsher 	cpu_to_le32(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
302874aeea5SJeff Kirsher 
303874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD64(oword, ...) do {				\
304874aeea5SJeff Kirsher 	(oword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
305874aeea5SJeff Kirsher 	(oword).u64[1] = EFX_INSERT_FIELDS64(64, 127, __VA_ARGS__);	\
306874aeea5SJeff Kirsher 	} while (0)
307874aeea5SJeff Kirsher 
308874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD64(qword, ...) do {				\
309874aeea5SJeff Kirsher 	(qword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
310874aeea5SJeff Kirsher 	} while (0)
311874aeea5SJeff Kirsher 
312874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD32(oword, ...) do {				\
313874aeea5SJeff Kirsher 	(oword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
314874aeea5SJeff Kirsher 	(oword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
315874aeea5SJeff Kirsher 	(oword).u32[2] = EFX_INSERT_FIELDS32(64, 95, __VA_ARGS__);	\
316874aeea5SJeff Kirsher 	(oword).u32[3] = EFX_INSERT_FIELDS32(96, 127, __VA_ARGS__);	\
317874aeea5SJeff Kirsher 	} while (0)
318874aeea5SJeff Kirsher 
319874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD32(qword, ...) do {				\
320874aeea5SJeff Kirsher 	(qword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
321874aeea5SJeff Kirsher 	(qword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
322874aeea5SJeff Kirsher 	} while (0)
323874aeea5SJeff Kirsher 
324874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD(dword, ...) do {				\
325874aeea5SJeff Kirsher 	(dword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
326874aeea5SJeff Kirsher 	} while (0)
327874aeea5SJeff Kirsher 
328874aeea5SJeff Kirsher #if BITS_PER_LONG == 64
329874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD EFX_POPULATE_OWORD64
330874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD EFX_POPULATE_QWORD64
331874aeea5SJeff Kirsher #else
332874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD EFX_POPULATE_OWORD32
333874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD EFX_POPULATE_QWORD32
334874aeea5SJeff Kirsher #endif
335874aeea5SJeff Kirsher 
336874aeea5SJeff Kirsher /* Populate an octword field with various numbers of arguments */
337874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_10 EFX_POPULATE_OWORD
338874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_9(oword, ...) \
339874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_10(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
340874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_8(oword, ...) \
341874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_9(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
342874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_7(oword, ...) \
343874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_8(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
344874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_6(oword, ...) \
345874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_7(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
346874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_5(oword, ...) \
347874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_6(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
348874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_4(oword, ...) \
349874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_5(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
350874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_3(oword, ...) \
351874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_4(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
352874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_2(oword, ...) \
353874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_3(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
354874aeea5SJeff Kirsher #define EFX_POPULATE_OWORD_1(oword, ...) \
355874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_2(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
356874aeea5SJeff Kirsher #define EFX_ZERO_OWORD(oword) \
357874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_1(oword, EFX_DUMMY_FIELD, 0)
358874aeea5SJeff Kirsher #define EFX_SET_OWORD(oword) \
359874aeea5SJeff Kirsher 	EFX_POPULATE_OWORD_4(oword, \
360874aeea5SJeff Kirsher 			     EFX_DWORD_0, 0xffffffff, \
361874aeea5SJeff Kirsher 			     EFX_DWORD_1, 0xffffffff, \
362874aeea5SJeff Kirsher 			     EFX_DWORD_2, 0xffffffff, \
363874aeea5SJeff Kirsher 			     EFX_DWORD_3, 0xffffffff)
364874aeea5SJeff Kirsher 
365874aeea5SJeff Kirsher /* Populate a quadword field with various numbers of arguments */
366874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_10 EFX_POPULATE_QWORD
367874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_9(qword, ...) \
368874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_10(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
369874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_8(qword, ...) \
370874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_9(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
371874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_7(qword, ...) \
372874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_8(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
373874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_6(qword, ...) \
374874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_7(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
375874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_5(qword, ...) \
376874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_6(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
377874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_4(qword, ...) \
378874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_5(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
379874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_3(qword, ...) \
380874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_4(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
381874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_2(qword, ...) \
382874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_3(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
383874aeea5SJeff Kirsher #define EFX_POPULATE_QWORD_1(qword, ...) \
384874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_2(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
385874aeea5SJeff Kirsher #define EFX_ZERO_QWORD(qword) \
386874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_1(qword, EFX_DUMMY_FIELD, 0)
387874aeea5SJeff Kirsher #define EFX_SET_QWORD(qword) \
388874aeea5SJeff Kirsher 	EFX_POPULATE_QWORD_2(qword, \
389874aeea5SJeff Kirsher 			     EFX_DWORD_0, 0xffffffff, \
390874aeea5SJeff Kirsher 			     EFX_DWORD_1, 0xffffffff)
391874aeea5SJeff Kirsher 
392874aeea5SJeff Kirsher /* Populate a dword field with various numbers of arguments */
393874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_10 EFX_POPULATE_DWORD
394874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_9(dword, ...) \
395874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_10(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
396874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_8(dword, ...) \
397874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_9(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
398874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_7(dword, ...) \
399874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_8(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
400874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_6(dword, ...) \
401874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_7(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
402874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_5(dword, ...) \
403874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_6(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
404874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_4(dword, ...) \
405874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_5(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
406874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_3(dword, ...) \
407874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_4(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
408874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_2(dword, ...) \
409874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_3(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
410874aeea5SJeff Kirsher #define EFX_POPULATE_DWORD_1(dword, ...) \
411874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_2(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
412874aeea5SJeff Kirsher #define EFX_ZERO_DWORD(dword) \
413874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_1(dword, EFX_DUMMY_FIELD, 0)
414874aeea5SJeff Kirsher #define EFX_SET_DWORD(dword) \
415874aeea5SJeff Kirsher 	EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 0xffffffff)
416874aeea5SJeff Kirsher 
417874aeea5SJeff Kirsher /*
418874aeea5SJeff Kirsher  * Modify a named field within an already-populated structure.  Used
419874aeea5SJeff Kirsher  * for read-modify-write operations.
420874aeea5SJeff Kirsher  *
421874aeea5SJeff Kirsher  */
422874aeea5SJeff Kirsher #define EFX_INVERT_OWORD(oword) do {		\
423874aeea5SJeff Kirsher 	(oword).u64[0] = ~((oword).u64[0]);	\
424874aeea5SJeff Kirsher 	(oword).u64[1] = ~((oword).u64[1]);	\
425874aeea5SJeff Kirsher 	} while (0)
426874aeea5SJeff Kirsher 
427874aeea5SJeff Kirsher #define EFX_AND_OWORD(oword, from, mask)			\
428874aeea5SJeff Kirsher 	do {							\
429874aeea5SJeff Kirsher 		(oword).u64[0] = (from).u64[0] & (mask).u64[0];	\
430874aeea5SJeff Kirsher 		(oword).u64[1] = (from).u64[1] & (mask).u64[1];	\
431874aeea5SJeff Kirsher 	} while (0)
432874aeea5SJeff Kirsher 
433a0ee3541SJon Cooper #define EFX_AND_QWORD(qword, from, mask)			\
434a0ee3541SJon Cooper 		(qword).u64[0] = (from).u64[0] & (mask).u64[0]
435a0ee3541SJon Cooper 
436874aeea5SJeff Kirsher #define EFX_OR_OWORD(oword, from, mask)				\
437874aeea5SJeff Kirsher 	do {							\
438874aeea5SJeff Kirsher 		(oword).u64[0] = (from).u64[0] | (mask).u64[0];	\
439874aeea5SJeff Kirsher 		(oword).u64[1] = (from).u64[1] | (mask).u64[1];	\
440874aeea5SJeff Kirsher 	} while (0)
441874aeea5SJeff Kirsher 
442874aeea5SJeff Kirsher #define EFX_INSERT64(min, max, low, high, value)			\
443874aeea5SJeff Kirsher 	cpu_to_le64(EFX_INSERT_NATIVE(min, max, low, high, value))
444874aeea5SJeff Kirsher 
445874aeea5SJeff Kirsher #define EFX_INSERT32(min, max, low, high, value)			\
446874aeea5SJeff Kirsher 	cpu_to_le32(EFX_INSERT_NATIVE(min, max, low, high, value))
447874aeea5SJeff Kirsher 
448874aeea5SJeff Kirsher #define EFX_INPLACE_MASK64(min, max, low, high)				\
4499724a850SBen Hutchings 	EFX_INSERT64(min, max, low, high, EFX_MASK64((high) + 1 - (low)))
450874aeea5SJeff Kirsher 
451874aeea5SJeff Kirsher #define EFX_INPLACE_MASK32(min, max, low, high)				\
4529724a850SBen Hutchings 	EFX_INSERT32(min, max, low, high, EFX_MASK32((high) + 1 - (low)))
453874aeea5SJeff Kirsher 
454874aeea5SJeff Kirsher #define EFX_SET_OWORD64(oword, low, high, value) do {			\
455874aeea5SJeff Kirsher 	(oword).u64[0] = (((oword).u64[0]				\
456874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK64(0,  63, low, high))	\
457874aeea5SJeff Kirsher 			  | EFX_INSERT64(0,  63, low, high, value));	\
458874aeea5SJeff Kirsher 	(oword).u64[1] = (((oword).u64[1]				\
459874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK64(64, 127, low, high))	\
460874aeea5SJeff Kirsher 			  | EFX_INSERT64(64, 127, low, high, value));	\
461874aeea5SJeff Kirsher 	} while (0)
462874aeea5SJeff Kirsher 
463874aeea5SJeff Kirsher #define EFX_SET_QWORD64(qword, low, high, value) do {			\
464874aeea5SJeff Kirsher 	(qword).u64[0] = (((qword).u64[0]				\
465874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK64(0, 63, low, high))	\
466874aeea5SJeff Kirsher 			  | EFX_INSERT64(0, 63, low, high, value));	\
467874aeea5SJeff Kirsher 	} while (0)
468874aeea5SJeff Kirsher 
469874aeea5SJeff Kirsher #define EFX_SET_OWORD32(oword, low, high, value) do {			\
470874aeea5SJeff Kirsher 	(oword).u32[0] = (((oword).u32[0]				\
471874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
472874aeea5SJeff Kirsher 			  | EFX_INSERT32(0, 31, low, high, value));	\
473874aeea5SJeff Kirsher 	(oword).u32[1] = (((oword).u32[1]				\
474874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK32(32, 63, low, high))	\
475874aeea5SJeff Kirsher 			  | EFX_INSERT32(32, 63, low, high, value));	\
476874aeea5SJeff Kirsher 	(oword).u32[2] = (((oword).u32[2]				\
477874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK32(64, 95, low, high))	\
478874aeea5SJeff Kirsher 			  | EFX_INSERT32(64, 95, low, high, value));	\
479874aeea5SJeff Kirsher 	(oword).u32[3] = (((oword).u32[3]				\
480874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK32(96, 127, low, high))	\
481874aeea5SJeff Kirsher 			  | EFX_INSERT32(96, 127, low, high, value));	\
482874aeea5SJeff Kirsher 	} while (0)
483874aeea5SJeff Kirsher 
484874aeea5SJeff Kirsher #define EFX_SET_QWORD32(qword, low, high, value) do {			\
485874aeea5SJeff Kirsher 	(qword).u32[0] = (((qword).u32[0]				\
486874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
487874aeea5SJeff Kirsher 			  | EFX_INSERT32(0, 31, low, high, value));	\
488874aeea5SJeff Kirsher 	(qword).u32[1] = (((qword).u32[1]				\
489874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK32(32, 63, low, high))	\
490874aeea5SJeff Kirsher 			  | EFX_INSERT32(32, 63, low, high, value));	\
491874aeea5SJeff Kirsher 	} while (0)
492874aeea5SJeff Kirsher 
493874aeea5SJeff Kirsher #define EFX_SET_DWORD32(dword, low, high, value) do {			\
494874aeea5SJeff Kirsher 	(dword).u32[0] = (((dword).u32[0]				\
495874aeea5SJeff Kirsher 			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
496874aeea5SJeff Kirsher 			  | EFX_INSERT32(0, 31, low, high, value));	\
497874aeea5SJeff Kirsher 	} while (0)
498874aeea5SJeff Kirsher 
499874aeea5SJeff Kirsher #define EFX_SET_OWORD_FIELD64(oword, field, value)			\
500874aeea5SJeff Kirsher 	EFX_SET_OWORD64(oword, EFX_LOW_BIT(field),			\
501874aeea5SJeff Kirsher 			 EFX_HIGH_BIT(field), value)
502874aeea5SJeff Kirsher 
503874aeea5SJeff Kirsher #define EFX_SET_QWORD_FIELD64(qword, field, value)			\
504874aeea5SJeff Kirsher 	EFX_SET_QWORD64(qword, EFX_LOW_BIT(field),			\
505874aeea5SJeff Kirsher 			 EFX_HIGH_BIT(field), value)
506874aeea5SJeff Kirsher 
507874aeea5SJeff Kirsher #define EFX_SET_OWORD_FIELD32(oword, field, value)			\
508874aeea5SJeff Kirsher 	EFX_SET_OWORD32(oword, EFX_LOW_BIT(field),			\
509874aeea5SJeff Kirsher 			 EFX_HIGH_BIT(field), value)
510874aeea5SJeff Kirsher 
511874aeea5SJeff Kirsher #define EFX_SET_QWORD_FIELD32(qword, field, value)			\
512874aeea5SJeff Kirsher 	EFX_SET_QWORD32(qword, EFX_LOW_BIT(field),			\
513874aeea5SJeff Kirsher 			 EFX_HIGH_BIT(field), value)
514874aeea5SJeff Kirsher 
515874aeea5SJeff Kirsher #define EFX_SET_DWORD_FIELD(dword, field, value)			\
516874aeea5SJeff Kirsher 	EFX_SET_DWORD32(dword, EFX_LOW_BIT(field),			\
517874aeea5SJeff Kirsher 			 EFX_HIGH_BIT(field), value)
518874aeea5SJeff Kirsher 
519874aeea5SJeff Kirsher 
520874aeea5SJeff Kirsher 
521874aeea5SJeff Kirsher #if BITS_PER_LONG == 64
522874aeea5SJeff Kirsher #define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD64
523874aeea5SJeff Kirsher #define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD64
524874aeea5SJeff Kirsher #else
525874aeea5SJeff Kirsher #define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD32
526874aeea5SJeff Kirsher #define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD32
527874aeea5SJeff Kirsher #endif
528874aeea5SJeff Kirsher 
529874aeea5SJeff Kirsher /* Used to avoid compiler warnings about shift range exceeding width
530874aeea5SJeff Kirsher  * of the data types when dma_addr_t is only 32 bits wide.
531874aeea5SJeff Kirsher  */
532874aeea5SJeff Kirsher #define DMA_ADDR_T_WIDTH	(8 * sizeof(dma_addr_t))
533874aeea5SJeff Kirsher #define EFX_DMA_TYPE_WIDTH(width) \
534874aeea5SJeff Kirsher 	(((width) < DMA_ADDR_T_WIDTH) ? (width) : DMA_ADDR_T_WIDTH)
535874aeea5SJeff Kirsher 
536874aeea5SJeff Kirsher 
537874aeea5SJeff Kirsher /* Static initialiser */
538874aeea5SJeff Kirsher #define EFX_OWORD32(a, b, c, d)				\
539874aeea5SJeff Kirsher 	{ .u32 = { cpu_to_le32(a), cpu_to_le32(b),	\
540874aeea5SJeff Kirsher 		   cpu_to_le32(c), cpu_to_le32(d) } }
541874aeea5SJeff Kirsher 
542874aeea5SJeff Kirsher #endif /* EFX_BITFIELD_H */
543