xref: /openbmc/linux/drivers/net/ethernet/sfc/falcon/bitfield.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
25a6681e2SEdward Cree /****************************************************************************
35a6681e2SEdward Cree  * Driver for Solarflare network controllers and boards
45a6681e2SEdward Cree  * Copyright 2005-2006 Fen Systems Ltd.
55a6681e2SEdward Cree  * Copyright 2006-2013 Solarflare Communications Inc.
65a6681e2SEdward Cree  */
75a6681e2SEdward Cree 
85a6681e2SEdward Cree #ifndef EF4_BITFIELD_H
95a6681e2SEdward Cree #define EF4_BITFIELD_H
105a6681e2SEdward Cree 
115a6681e2SEdward Cree /*
125a6681e2SEdward Cree  * Efx bitfield access
135a6681e2SEdward Cree  *
145a6681e2SEdward Cree  * Efx NICs make extensive use of bitfields up to 128 bits
155a6681e2SEdward Cree  * wide.  Since there is no native 128-bit datatype on most systems,
165a6681e2SEdward Cree  * and since 64-bit datatypes are inefficient on 32-bit systems and
175a6681e2SEdward Cree  * vice versa, we wrap accesses in a way that uses the most efficient
185a6681e2SEdward Cree  * datatype.
195a6681e2SEdward Cree  *
205a6681e2SEdward Cree  * The NICs are PCI devices and therefore little-endian.  Since most
215a6681e2SEdward Cree  * of the quantities that we deal with are DMAed to/from host memory,
225a6681e2SEdward Cree  * we define our datatypes (ef4_oword_t, ef4_qword_t and
235a6681e2SEdward Cree  * ef4_dword_t) to be little-endian.
245a6681e2SEdward Cree  */
255a6681e2SEdward Cree 
265a6681e2SEdward Cree /* Lowest bit numbers and widths */
275a6681e2SEdward Cree #define EF4_DUMMY_FIELD_LBN 0
285a6681e2SEdward Cree #define EF4_DUMMY_FIELD_WIDTH 0
295a6681e2SEdward Cree #define EF4_WORD_0_LBN 0
305a6681e2SEdward Cree #define EF4_WORD_0_WIDTH 16
315a6681e2SEdward Cree #define EF4_WORD_1_LBN 16
325a6681e2SEdward Cree #define EF4_WORD_1_WIDTH 16
335a6681e2SEdward Cree #define EF4_DWORD_0_LBN 0
345a6681e2SEdward Cree #define EF4_DWORD_0_WIDTH 32
355a6681e2SEdward Cree #define EF4_DWORD_1_LBN 32
365a6681e2SEdward Cree #define EF4_DWORD_1_WIDTH 32
375a6681e2SEdward Cree #define EF4_DWORD_2_LBN 64
385a6681e2SEdward Cree #define EF4_DWORD_2_WIDTH 32
395a6681e2SEdward Cree #define EF4_DWORD_3_LBN 96
405a6681e2SEdward Cree #define EF4_DWORD_3_WIDTH 32
415a6681e2SEdward Cree #define EF4_QWORD_0_LBN 0
425a6681e2SEdward Cree #define EF4_QWORD_0_WIDTH 64
435a6681e2SEdward Cree 
445a6681e2SEdward Cree /* Specified attribute (e.g. LBN) of the specified field */
455a6681e2SEdward Cree #define EF4_VAL(field, attribute) field ## _ ## attribute
465a6681e2SEdward Cree /* Low bit number of the specified field */
475a6681e2SEdward Cree #define EF4_LOW_BIT(field) EF4_VAL(field, LBN)
485a6681e2SEdward Cree /* Bit width of the specified field */
495a6681e2SEdward Cree #define EF4_WIDTH(field) EF4_VAL(field, WIDTH)
505a6681e2SEdward Cree /* High bit number of the specified field */
515a6681e2SEdward Cree #define EF4_HIGH_BIT(field) (EF4_LOW_BIT(field) + EF4_WIDTH(field) - 1)
525a6681e2SEdward Cree /* Mask equal in width to the specified field.
535a6681e2SEdward Cree  *
545a6681e2SEdward Cree  * For example, a field with width 5 would have a mask of 0x1f.
555a6681e2SEdward Cree  *
565a6681e2SEdward Cree  * The maximum width mask that can be generated is 64 bits.
575a6681e2SEdward Cree  */
585a6681e2SEdward Cree #define EF4_MASK64(width)			\
595a6681e2SEdward Cree 	((width) == 64 ? ~((u64) 0) :		\
605a6681e2SEdward Cree 	 (((((u64) 1) << (width))) - 1))
615a6681e2SEdward Cree 
625a6681e2SEdward Cree /* Mask equal in width to the specified field.
635a6681e2SEdward Cree  *
645a6681e2SEdward Cree  * For example, a field with width 5 would have a mask of 0x1f.
655a6681e2SEdward Cree  *
665a6681e2SEdward Cree  * The maximum width mask that can be generated is 32 bits.  Use
675a6681e2SEdward Cree  * EF4_MASK64 for higher width fields.
685a6681e2SEdward Cree  */
695a6681e2SEdward Cree #define EF4_MASK32(width)			\
705a6681e2SEdward Cree 	((width) == 32 ? ~((u32) 0) :		\
715a6681e2SEdward Cree 	 (((((u32) 1) << (width))) - 1))
725a6681e2SEdward Cree 
735a6681e2SEdward Cree /* A doubleword (i.e. 4 byte) datatype - little-endian in HW */
745a6681e2SEdward Cree typedef union ef4_dword {
755a6681e2SEdward Cree 	__le32 u32[1];
765a6681e2SEdward Cree } ef4_dword_t;
775a6681e2SEdward Cree 
785a6681e2SEdward Cree /* A quadword (i.e. 8 byte) datatype - little-endian in HW */
795a6681e2SEdward Cree typedef union ef4_qword {
805a6681e2SEdward Cree 	__le64 u64[1];
815a6681e2SEdward Cree 	__le32 u32[2];
825a6681e2SEdward Cree 	ef4_dword_t dword[2];
835a6681e2SEdward Cree } ef4_qword_t;
845a6681e2SEdward Cree 
855a6681e2SEdward Cree /* An octword (eight-word, i.e. 16 byte) datatype - little-endian in HW */
865a6681e2SEdward Cree typedef union ef4_oword {
875a6681e2SEdward Cree 	__le64 u64[2];
885a6681e2SEdward Cree 	ef4_qword_t qword[2];
895a6681e2SEdward Cree 	__le32 u32[4];
905a6681e2SEdward Cree 	ef4_dword_t dword[4];
915a6681e2SEdward Cree } ef4_oword_t;
925a6681e2SEdward Cree 
935a6681e2SEdward Cree /* Format string and value expanders for printk */
945a6681e2SEdward Cree #define EF4_DWORD_FMT "%08x"
955a6681e2SEdward Cree #define EF4_QWORD_FMT "%08x:%08x"
965a6681e2SEdward Cree #define EF4_OWORD_FMT "%08x:%08x:%08x:%08x"
975a6681e2SEdward Cree #define EF4_DWORD_VAL(dword)				\
985a6681e2SEdward Cree 	((unsigned int) le32_to_cpu((dword).u32[0]))
995a6681e2SEdward Cree #define EF4_QWORD_VAL(qword)				\
1005a6681e2SEdward Cree 	((unsigned int) le32_to_cpu((qword).u32[1])),	\
1015a6681e2SEdward Cree 	((unsigned int) le32_to_cpu((qword).u32[0]))
1025a6681e2SEdward Cree #define EF4_OWORD_VAL(oword)				\
1035a6681e2SEdward Cree 	((unsigned int) le32_to_cpu((oword).u32[3])),	\
1045a6681e2SEdward Cree 	((unsigned int) le32_to_cpu((oword).u32[2])),	\
1055a6681e2SEdward Cree 	((unsigned int) le32_to_cpu((oword).u32[1])),	\
1065a6681e2SEdward Cree 	((unsigned int) le32_to_cpu((oword).u32[0]))
1075a6681e2SEdward Cree 
1085a6681e2SEdward Cree /*
1095a6681e2SEdward Cree  * Extract bit field portion [low,high) from the native-endian element
1105a6681e2SEdward Cree  * which contains bits [min,max).
1115a6681e2SEdward Cree  *
1125a6681e2SEdward Cree  * For example, suppose "element" represents the high 32 bits of a
1135a6681e2SEdward Cree  * 64-bit value, and we wish to extract the bits belonging to the bit
1145a6681e2SEdward Cree  * field occupying bits 28-45 of this 64-bit value.
1155a6681e2SEdward Cree  *
1165a6681e2SEdward Cree  * Then EF4_EXTRACT ( element, 32, 63, 28, 45 ) would give
1175a6681e2SEdward Cree  *
1185a6681e2SEdward Cree  *   ( element ) << 4
1195a6681e2SEdward Cree  *
120*bb4a0c88SJilin Yuan  * The result will contain the relevant bits filled in the range
1215a6681e2SEdward Cree  * [0,high-low), with garbage in bits [high-low+1,...).
1225a6681e2SEdward Cree  */
1235a6681e2SEdward Cree #define EF4_EXTRACT_NATIVE(native_element, min, max, low, high)		\
1245a6681e2SEdward Cree 	((low) > (max) || (high) < (min) ? 0 :				\
1255a6681e2SEdward Cree 	 (low) > (min) ?						\
1265a6681e2SEdward Cree 	 (native_element) >> ((low) - (min)) :				\
1275a6681e2SEdward Cree 	 (native_element) << ((min) - (low)))
1285a6681e2SEdward Cree 
1295a6681e2SEdward Cree /*
1305a6681e2SEdward Cree  * Extract bit field portion [low,high) from the 64-bit little-endian
1315a6681e2SEdward Cree  * element which contains bits [min,max)
1325a6681e2SEdward Cree  */
1335a6681e2SEdward Cree #define EF4_EXTRACT64(element, min, max, low, high)			\
1345a6681e2SEdward Cree 	EF4_EXTRACT_NATIVE(le64_to_cpu(element), min, max, low, high)
1355a6681e2SEdward Cree 
1365a6681e2SEdward Cree /*
1375a6681e2SEdward Cree  * Extract bit field portion [low,high) from the 32-bit little-endian
1385a6681e2SEdward Cree  * element which contains bits [min,max)
1395a6681e2SEdward Cree  */
1405a6681e2SEdward Cree #define EF4_EXTRACT32(element, min, max, low, high)			\
1415a6681e2SEdward Cree 	EF4_EXTRACT_NATIVE(le32_to_cpu(element), min, max, low, high)
1425a6681e2SEdward Cree 
1435a6681e2SEdward Cree #define EF4_EXTRACT_OWORD64(oword, low, high)				\
1445a6681e2SEdward Cree 	((EF4_EXTRACT64((oword).u64[0], 0, 63, low, high) |		\
1455a6681e2SEdward Cree 	  EF4_EXTRACT64((oword).u64[1], 64, 127, low, high)) &		\
1465a6681e2SEdward Cree 	 EF4_MASK64((high) + 1 - (low)))
1475a6681e2SEdward Cree 
1485a6681e2SEdward Cree #define EF4_EXTRACT_QWORD64(qword, low, high)				\
1495a6681e2SEdward Cree 	(EF4_EXTRACT64((qword).u64[0], 0, 63, low, high) &		\
1505a6681e2SEdward Cree 	 EF4_MASK64((high) + 1 - (low)))
1515a6681e2SEdward Cree 
1525a6681e2SEdward Cree #define EF4_EXTRACT_OWORD32(oword, low, high)				\
1535a6681e2SEdward Cree 	((EF4_EXTRACT32((oword).u32[0], 0, 31, low, high) |		\
1545a6681e2SEdward Cree 	  EF4_EXTRACT32((oword).u32[1], 32, 63, low, high) |		\
1555a6681e2SEdward Cree 	  EF4_EXTRACT32((oword).u32[2], 64, 95, low, high) |		\
1565a6681e2SEdward Cree 	  EF4_EXTRACT32((oword).u32[3], 96, 127, low, high)) &		\
1575a6681e2SEdward Cree 	 EF4_MASK32((high) + 1 - (low)))
1585a6681e2SEdward Cree 
1595a6681e2SEdward Cree #define EF4_EXTRACT_QWORD32(qword, low, high)				\
1605a6681e2SEdward Cree 	((EF4_EXTRACT32((qword).u32[0], 0, 31, low, high) |		\
1615a6681e2SEdward Cree 	  EF4_EXTRACT32((qword).u32[1], 32, 63, low, high)) &		\
1625a6681e2SEdward Cree 	 EF4_MASK32((high) + 1 - (low)))
1635a6681e2SEdward Cree 
1645a6681e2SEdward Cree #define EF4_EXTRACT_DWORD(dword, low, high)			\
1655a6681e2SEdward Cree 	(EF4_EXTRACT32((dword).u32[0], 0, 31, low, high) &	\
1665a6681e2SEdward Cree 	 EF4_MASK32((high) + 1 - (low)))
1675a6681e2SEdward Cree 
1685a6681e2SEdward Cree #define EF4_OWORD_FIELD64(oword, field)				\
1695a6681e2SEdward Cree 	EF4_EXTRACT_OWORD64(oword, EF4_LOW_BIT(field),		\
1705a6681e2SEdward Cree 			    EF4_HIGH_BIT(field))
1715a6681e2SEdward Cree 
1725a6681e2SEdward Cree #define EF4_QWORD_FIELD64(qword, field)				\
1735a6681e2SEdward Cree 	EF4_EXTRACT_QWORD64(qword, EF4_LOW_BIT(field),		\
1745a6681e2SEdward Cree 			    EF4_HIGH_BIT(field))
1755a6681e2SEdward Cree 
1765a6681e2SEdward Cree #define EF4_OWORD_FIELD32(oword, field)				\
1775a6681e2SEdward Cree 	EF4_EXTRACT_OWORD32(oword, EF4_LOW_BIT(field),		\
1785a6681e2SEdward Cree 			    EF4_HIGH_BIT(field))
1795a6681e2SEdward Cree 
1805a6681e2SEdward Cree #define EF4_QWORD_FIELD32(qword, field)				\
1815a6681e2SEdward Cree 	EF4_EXTRACT_QWORD32(qword, EF4_LOW_BIT(field),		\
1825a6681e2SEdward Cree 			    EF4_HIGH_BIT(field))
1835a6681e2SEdward Cree 
1845a6681e2SEdward Cree #define EF4_DWORD_FIELD(dword, field)				\
1855a6681e2SEdward Cree 	EF4_EXTRACT_DWORD(dword, EF4_LOW_BIT(field),		\
1865a6681e2SEdward Cree 			  EF4_HIGH_BIT(field))
1875a6681e2SEdward Cree 
1885a6681e2SEdward Cree #define EF4_OWORD_IS_ZERO64(oword)					\
1895a6681e2SEdward Cree 	(((oword).u64[0] | (oword).u64[1]) == (__force __le64) 0)
1905a6681e2SEdward Cree 
1915a6681e2SEdward Cree #define EF4_QWORD_IS_ZERO64(qword)					\
1925a6681e2SEdward Cree 	(((qword).u64[0]) == (__force __le64) 0)
1935a6681e2SEdward Cree 
1945a6681e2SEdward Cree #define EF4_OWORD_IS_ZERO32(oword)					     \
1955a6681e2SEdward Cree 	(((oword).u32[0] | (oword).u32[1] | (oword).u32[2] | (oword).u32[3]) \
1965a6681e2SEdward Cree 	 == (__force __le32) 0)
1975a6681e2SEdward Cree 
1985a6681e2SEdward Cree #define EF4_QWORD_IS_ZERO32(qword)					\
1995a6681e2SEdward Cree 	(((qword).u32[0] | (qword).u32[1]) == (__force __le32) 0)
2005a6681e2SEdward Cree 
2015a6681e2SEdward Cree #define EF4_DWORD_IS_ZERO(dword)					\
2025a6681e2SEdward Cree 	(((dword).u32[0]) == (__force __le32) 0)
2035a6681e2SEdward Cree 
2045a6681e2SEdward Cree #define EF4_OWORD_IS_ALL_ONES64(oword)					\
2055a6681e2SEdward Cree 	(((oword).u64[0] & (oword).u64[1]) == ~((__force __le64) 0))
2065a6681e2SEdward Cree 
2075a6681e2SEdward Cree #define EF4_QWORD_IS_ALL_ONES64(qword)					\
2085a6681e2SEdward Cree 	((qword).u64[0] == ~((__force __le64) 0))
2095a6681e2SEdward Cree 
2105a6681e2SEdward Cree #define EF4_OWORD_IS_ALL_ONES32(oword)					\
2115a6681e2SEdward Cree 	(((oword).u32[0] & (oword).u32[1] & (oword).u32[2] & (oword).u32[3]) \
2125a6681e2SEdward Cree 	 == ~((__force __le32) 0))
2135a6681e2SEdward Cree 
2145a6681e2SEdward Cree #define EF4_QWORD_IS_ALL_ONES32(qword)					\
2155a6681e2SEdward Cree 	(((qword).u32[0] & (qword).u32[1]) == ~((__force __le32) 0))
2165a6681e2SEdward Cree 
2175a6681e2SEdward Cree #define EF4_DWORD_IS_ALL_ONES(dword)					\
2185a6681e2SEdward Cree 	((dword).u32[0] == ~((__force __le32) 0))
2195a6681e2SEdward Cree 
2205a6681e2SEdward Cree #if BITS_PER_LONG == 64
2215a6681e2SEdward Cree #define EF4_OWORD_FIELD		EF4_OWORD_FIELD64
2225a6681e2SEdward Cree #define EF4_QWORD_FIELD		EF4_QWORD_FIELD64
2235a6681e2SEdward Cree #define EF4_OWORD_IS_ZERO	EF4_OWORD_IS_ZERO64
2245a6681e2SEdward Cree #define EF4_QWORD_IS_ZERO	EF4_QWORD_IS_ZERO64
2255a6681e2SEdward Cree #define EF4_OWORD_IS_ALL_ONES	EF4_OWORD_IS_ALL_ONES64
2265a6681e2SEdward Cree #define EF4_QWORD_IS_ALL_ONES	EF4_QWORD_IS_ALL_ONES64
2275a6681e2SEdward Cree #else
2285a6681e2SEdward Cree #define EF4_OWORD_FIELD		EF4_OWORD_FIELD32
2295a6681e2SEdward Cree #define EF4_QWORD_FIELD		EF4_QWORD_FIELD32
2305a6681e2SEdward Cree #define EF4_OWORD_IS_ZERO	EF4_OWORD_IS_ZERO32
2315a6681e2SEdward Cree #define EF4_QWORD_IS_ZERO	EF4_QWORD_IS_ZERO32
2325a6681e2SEdward Cree #define EF4_OWORD_IS_ALL_ONES	EF4_OWORD_IS_ALL_ONES32
2335a6681e2SEdward Cree #define EF4_QWORD_IS_ALL_ONES	EF4_QWORD_IS_ALL_ONES32
2345a6681e2SEdward Cree #endif
2355a6681e2SEdward Cree 
2365a6681e2SEdward Cree /*
2375a6681e2SEdward Cree  * Construct bit field portion
2385a6681e2SEdward Cree  *
2395a6681e2SEdward Cree  * Creates the portion of the bit field [low,high) that lies within
2405a6681e2SEdward Cree  * the range [min,max).
2415a6681e2SEdward Cree  */
2425a6681e2SEdward Cree #define EF4_INSERT_NATIVE64(min, max, low, high, value)		\
2435a6681e2SEdward Cree 	(((low > max) || (high < min)) ? 0 :			\
2445a6681e2SEdward Cree 	 ((low > min) ?						\
2455a6681e2SEdward Cree 	  (((u64) (value)) << (low - min)) :		\
2465a6681e2SEdward Cree 	  (((u64) (value)) >> (min - low))))
2475a6681e2SEdward Cree 
2485a6681e2SEdward Cree #define EF4_INSERT_NATIVE32(min, max, low, high, value)		\
2495a6681e2SEdward Cree 	(((low > max) || (high < min)) ? 0 :			\
2505a6681e2SEdward Cree 	 ((low > min) ?						\
2515a6681e2SEdward Cree 	  (((u32) (value)) << (low - min)) :		\
2525a6681e2SEdward Cree 	  (((u32) (value)) >> (min - low))))
2535a6681e2SEdward Cree 
2545a6681e2SEdward Cree #define EF4_INSERT_NATIVE(min, max, low, high, value)		\
2555a6681e2SEdward Cree 	((((max - min) >= 32) || ((high - low) >= 32)) ?	\
2565a6681e2SEdward Cree 	 EF4_INSERT_NATIVE64(min, max, low, high, value) :	\
2575a6681e2SEdward Cree 	 EF4_INSERT_NATIVE32(min, max, low, high, value))
2585a6681e2SEdward Cree 
2595a6681e2SEdward Cree /*
2605a6681e2SEdward Cree  * Construct bit field portion
2615a6681e2SEdward Cree  *
2625a6681e2SEdward Cree  * Creates the portion of the named bit field that lies within the
2635a6681e2SEdward Cree  * range [min,max).
2645a6681e2SEdward Cree  */
2655a6681e2SEdward Cree #define EF4_INSERT_FIELD_NATIVE(min, max, field, value)		\
2665a6681e2SEdward Cree 	EF4_INSERT_NATIVE(min, max, EF4_LOW_BIT(field),		\
2675a6681e2SEdward Cree 			  EF4_HIGH_BIT(field), value)
2685a6681e2SEdward Cree 
2695a6681e2SEdward Cree /*
2705a6681e2SEdward Cree  * Construct bit field
2715a6681e2SEdward Cree  *
2725a6681e2SEdward Cree  * Creates the portion of the named bit fields that lie within the
2735a6681e2SEdward Cree  * range [min,max).
2745a6681e2SEdward Cree  */
2755a6681e2SEdward Cree #define EF4_INSERT_FIELDS_NATIVE(min, max,				\
2765a6681e2SEdward Cree 				 field1, value1,			\
2775a6681e2SEdward Cree 				 field2, value2,			\
2785a6681e2SEdward Cree 				 field3, value3,			\
2795a6681e2SEdward Cree 				 field4, value4,			\
2805a6681e2SEdward Cree 				 field5, value5,			\
2815a6681e2SEdward Cree 				 field6, value6,			\
2825a6681e2SEdward Cree 				 field7, value7,			\
2835a6681e2SEdward Cree 				 field8, value8,			\
2845a6681e2SEdward Cree 				 field9, value9,			\
2855a6681e2SEdward Cree 				 field10, value10)			\
2865a6681e2SEdward Cree 	(EF4_INSERT_FIELD_NATIVE((min), (max), field1, (value1)) |	\
2875a6681e2SEdward Cree 	 EF4_INSERT_FIELD_NATIVE((min), (max), field2, (value2)) |	\
2885a6681e2SEdward Cree 	 EF4_INSERT_FIELD_NATIVE((min), (max), field3, (value3)) |	\
2895a6681e2SEdward Cree 	 EF4_INSERT_FIELD_NATIVE((min), (max), field4, (value4)) |	\
2905a6681e2SEdward Cree 	 EF4_INSERT_FIELD_NATIVE((min), (max), field5, (value5)) |	\
2915a6681e2SEdward Cree 	 EF4_INSERT_FIELD_NATIVE((min), (max), field6, (value6)) |	\
2925a6681e2SEdward Cree 	 EF4_INSERT_FIELD_NATIVE((min), (max), field7, (value7)) |	\
2935a6681e2SEdward Cree 	 EF4_INSERT_FIELD_NATIVE((min), (max), field8, (value8)) |	\
2945a6681e2SEdward Cree 	 EF4_INSERT_FIELD_NATIVE((min), (max), field9, (value9)) |	\
2955a6681e2SEdward Cree 	 EF4_INSERT_FIELD_NATIVE((min), (max), field10, (value10)))
2965a6681e2SEdward Cree 
2975a6681e2SEdward Cree #define EF4_INSERT_FIELDS64(...)				\
2985a6681e2SEdward Cree 	cpu_to_le64(EF4_INSERT_FIELDS_NATIVE(__VA_ARGS__))
2995a6681e2SEdward Cree 
3005a6681e2SEdward Cree #define EF4_INSERT_FIELDS32(...)				\
3015a6681e2SEdward Cree 	cpu_to_le32(EF4_INSERT_FIELDS_NATIVE(__VA_ARGS__))
3025a6681e2SEdward Cree 
3035a6681e2SEdward Cree #define EF4_POPULATE_OWORD64(oword, ...) do {				\
3045a6681e2SEdward Cree 	(oword).u64[0] = EF4_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
3055a6681e2SEdward Cree 	(oword).u64[1] = EF4_INSERT_FIELDS64(64, 127, __VA_ARGS__);	\
3065a6681e2SEdward Cree 	} while (0)
3075a6681e2SEdward Cree 
3085a6681e2SEdward Cree #define EF4_POPULATE_QWORD64(qword, ...) do {				\
3095a6681e2SEdward Cree 	(qword).u64[0] = EF4_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
3105a6681e2SEdward Cree 	} while (0)
3115a6681e2SEdward Cree 
3125a6681e2SEdward Cree #define EF4_POPULATE_OWORD32(oword, ...) do {				\
3135a6681e2SEdward Cree 	(oword).u32[0] = EF4_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
3145a6681e2SEdward Cree 	(oword).u32[1] = EF4_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
3155a6681e2SEdward Cree 	(oword).u32[2] = EF4_INSERT_FIELDS32(64, 95, __VA_ARGS__);	\
3165a6681e2SEdward Cree 	(oword).u32[3] = EF4_INSERT_FIELDS32(96, 127, __VA_ARGS__);	\
3175a6681e2SEdward Cree 	} while (0)
3185a6681e2SEdward Cree 
3195a6681e2SEdward Cree #define EF4_POPULATE_QWORD32(qword, ...) do {				\
3205a6681e2SEdward Cree 	(qword).u32[0] = EF4_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
3215a6681e2SEdward Cree 	(qword).u32[1] = EF4_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
3225a6681e2SEdward Cree 	} while (0)
3235a6681e2SEdward Cree 
3245a6681e2SEdward Cree #define EF4_POPULATE_DWORD(dword, ...) do {				\
3255a6681e2SEdward Cree 	(dword).u32[0] = EF4_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
3265a6681e2SEdward Cree 	} while (0)
3275a6681e2SEdward Cree 
3285a6681e2SEdward Cree #if BITS_PER_LONG == 64
3295a6681e2SEdward Cree #define EF4_POPULATE_OWORD EF4_POPULATE_OWORD64
3305a6681e2SEdward Cree #define EF4_POPULATE_QWORD EF4_POPULATE_QWORD64
3315a6681e2SEdward Cree #else
3325a6681e2SEdward Cree #define EF4_POPULATE_OWORD EF4_POPULATE_OWORD32
3335a6681e2SEdward Cree #define EF4_POPULATE_QWORD EF4_POPULATE_QWORD32
3345a6681e2SEdward Cree #endif
3355a6681e2SEdward Cree 
3365a6681e2SEdward Cree /* Populate an octword field with various numbers of arguments */
3375a6681e2SEdward Cree #define EF4_POPULATE_OWORD_10 EF4_POPULATE_OWORD
3385a6681e2SEdward Cree #define EF4_POPULATE_OWORD_9(oword, ...) \
3395a6681e2SEdward Cree 	EF4_POPULATE_OWORD_10(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3405a6681e2SEdward Cree #define EF4_POPULATE_OWORD_8(oword, ...) \
3415a6681e2SEdward Cree 	EF4_POPULATE_OWORD_9(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3425a6681e2SEdward Cree #define EF4_POPULATE_OWORD_7(oword, ...) \
3435a6681e2SEdward Cree 	EF4_POPULATE_OWORD_8(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3445a6681e2SEdward Cree #define EF4_POPULATE_OWORD_6(oword, ...) \
3455a6681e2SEdward Cree 	EF4_POPULATE_OWORD_7(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3465a6681e2SEdward Cree #define EF4_POPULATE_OWORD_5(oword, ...) \
3475a6681e2SEdward Cree 	EF4_POPULATE_OWORD_6(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3485a6681e2SEdward Cree #define EF4_POPULATE_OWORD_4(oword, ...) \
3495a6681e2SEdward Cree 	EF4_POPULATE_OWORD_5(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3505a6681e2SEdward Cree #define EF4_POPULATE_OWORD_3(oword, ...) \
3515a6681e2SEdward Cree 	EF4_POPULATE_OWORD_4(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3525a6681e2SEdward Cree #define EF4_POPULATE_OWORD_2(oword, ...) \
3535a6681e2SEdward Cree 	EF4_POPULATE_OWORD_3(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3545a6681e2SEdward Cree #define EF4_POPULATE_OWORD_1(oword, ...) \
3555a6681e2SEdward Cree 	EF4_POPULATE_OWORD_2(oword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3565a6681e2SEdward Cree #define EF4_ZERO_OWORD(oword) \
3575a6681e2SEdward Cree 	EF4_POPULATE_OWORD_1(oword, EF4_DUMMY_FIELD, 0)
3585a6681e2SEdward Cree #define EF4_SET_OWORD(oword) \
3595a6681e2SEdward Cree 	EF4_POPULATE_OWORD_4(oword, \
3605a6681e2SEdward Cree 			     EF4_DWORD_0, 0xffffffff, \
3615a6681e2SEdward Cree 			     EF4_DWORD_1, 0xffffffff, \
3625a6681e2SEdward Cree 			     EF4_DWORD_2, 0xffffffff, \
3635a6681e2SEdward Cree 			     EF4_DWORD_3, 0xffffffff)
3645a6681e2SEdward Cree 
3655a6681e2SEdward Cree /* Populate a quadword field with various numbers of arguments */
3665a6681e2SEdward Cree #define EF4_POPULATE_QWORD_10 EF4_POPULATE_QWORD
3675a6681e2SEdward Cree #define EF4_POPULATE_QWORD_9(qword, ...) \
3685a6681e2SEdward Cree 	EF4_POPULATE_QWORD_10(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3695a6681e2SEdward Cree #define EF4_POPULATE_QWORD_8(qword, ...) \
3705a6681e2SEdward Cree 	EF4_POPULATE_QWORD_9(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3715a6681e2SEdward Cree #define EF4_POPULATE_QWORD_7(qword, ...) \
3725a6681e2SEdward Cree 	EF4_POPULATE_QWORD_8(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3735a6681e2SEdward Cree #define EF4_POPULATE_QWORD_6(qword, ...) \
3745a6681e2SEdward Cree 	EF4_POPULATE_QWORD_7(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3755a6681e2SEdward Cree #define EF4_POPULATE_QWORD_5(qword, ...) \
3765a6681e2SEdward Cree 	EF4_POPULATE_QWORD_6(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3775a6681e2SEdward Cree #define EF4_POPULATE_QWORD_4(qword, ...) \
3785a6681e2SEdward Cree 	EF4_POPULATE_QWORD_5(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3795a6681e2SEdward Cree #define EF4_POPULATE_QWORD_3(qword, ...) \
3805a6681e2SEdward Cree 	EF4_POPULATE_QWORD_4(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3815a6681e2SEdward Cree #define EF4_POPULATE_QWORD_2(qword, ...) \
3825a6681e2SEdward Cree 	EF4_POPULATE_QWORD_3(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3835a6681e2SEdward Cree #define EF4_POPULATE_QWORD_1(qword, ...) \
3845a6681e2SEdward Cree 	EF4_POPULATE_QWORD_2(qword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3855a6681e2SEdward Cree #define EF4_ZERO_QWORD(qword) \
3865a6681e2SEdward Cree 	EF4_POPULATE_QWORD_1(qword, EF4_DUMMY_FIELD, 0)
3875a6681e2SEdward Cree #define EF4_SET_QWORD(qword) \
3885a6681e2SEdward Cree 	EF4_POPULATE_QWORD_2(qword, \
3895a6681e2SEdward Cree 			     EF4_DWORD_0, 0xffffffff, \
3905a6681e2SEdward Cree 			     EF4_DWORD_1, 0xffffffff)
3915a6681e2SEdward Cree 
3925a6681e2SEdward Cree /* Populate a dword field with various numbers of arguments */
3935a6681e2SEdward Cree #define EF4_POPULATE_DWORD_10 EF4_POPULATE_DWORD
3945a6681e2SEdward Cree #define EF4_POPULATE_DWORD_9(dword, ...) \
3955a6681e2SEdward Cree 	EF4_POPULATE_DWORD_10(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3965a6681e2SEdward Cree #define EF4_POPULATE_DWORD_8(dword, ...) \
3975a6681e2SEdward Cree 	EF4_POPULATE_DWORD_9(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
3985a6681e2SEdward Cree #define EF4_POPULATE_DWORD_7(dword, ...) \
3995a6681e2SEdward Cree 	EF4_POPULATE_DWORD_8(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
4005a6681e2SEdward Cree #define EF4_POPULATE_DWORD_6(dword, ...) \
4015a6681e2SEdward Cree 	EF4_POPULATE_DWORD_7(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
4025a6681e2SEdward Cree #define EF4_POPULATE_DWORD_5(dword, ...) \
4035a6681e2SEdward Cree 	EF4_POPULATE_DWORD_6(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
4045a6681e2SEdward Cree #define EF4_POPULATE_DWORD_4(dword, ...) \
4055a6681e2SEdward Cree 	EF4_POPULATE_DWORD_5(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
4065a6681e2SEdward Cree #define EF4_POPULATE_DWORD_3(dword, ...) \
4075a6681e2SEdward Cree 	EF4_POPULATE_DWORD_4(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
4085a6681e2SEdward Cree #define EF4_POPULATE_DWORD_2(dword, ...) \
4095a6681e2SEdward Cree 	EF4_POPULATE_DWORD_3(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
4105a6681e2SEdward Cree #define EF4_POPULATE_DWORD_1(dword, ...) \
4115a6681e2SEdward Cree 	EF4_POPULATE_DWORD_2(dword, EF4_DUMMY_FIELD, 0, __VA_ARGS__)
4125a6681e2SEdward Cree #define EF4_ZERO_DWORD(dword) \
4135a6681e2SEdward Cree 	EF4_POPULATE_DWORD_1(dword, EF4_DUMMY_FIELD, 0)
4145a6681e2SEdward Cree #define EF4_SET_DWORD(dword) \
4155a6681e2SEdward Cree 	EF4_POPULATE_DWORD_1(dword, EF4_DWORD_0, 0xffffffff)
4165a6681e2SEdward Cree 
4175a6681e2SEdward Cree /*
4185a6681e2SEdward Cree  * Modify a named field within an already-populated structure.  Used
4195a6681e2SEdward Cree  * for read-modify-write operations.
4205a6681e2SEdward Cree  *
4215a6681e2SEdward Cree  */
4225a6681e2SEdward Cree #define EF4_INVERT_OWORD(oword) do {		\
4235a6681e2SEdward Cree 	(oword).u64[0] = ~((oword).u64[0]);	\
4245a6681e2SEdward Cree 	(oword).u64[1] = ~((oword).u64[1]);	\
4255a6681e2SEdward Cree 	} while (0)
4265a6681e2SEdward Cree 
4275a6681e2SEdward Cree #define EF4_AND_OWORD(oword, from, mask)			\
4285a6681e2SEdward Cree 	do {							\
4295a6681e2SEdward Cree 		(oword).u64[0] = (from).u64[0] & (mask).u64[0];	\
4305a6681e2SEdward Cree 		(oword).u64[1] = (from).u64[1] & (mask).u64[1];	\
4315a6681e2SEdward Cree 	} while (0)
4325a6681e2SEdward Cree 
4335a6681e2SEdward Cree #define EF4_OR_OWORD(oword, from, mask)				\
4345a6681e2SEdward Cree 	do {							\
4355a6681e2SEdward Cree 		(oword).u64[0] = (from).u64[0] | (mask).u64[0];	\
4365a6681e2SEdward Cree 		(oword).u64[1] = (from).u64[1] | (mask).u64[1];	\
4375a6681e2SEdward Cree 	} while (0)
4385a6681e2SEdward Cree 
4395a6681e2SEdward Cree #define EF4_INSERT64(min, max, low, high, value)			\
4405a6681e2SEdward Cree 	cpu_to_le64(EF4_INSERT_NATIVE(min, max, low, high, value))
4415a6681e2SEdward Cree 
4425a6681e2SEdward Cree #define EF4_INSERT32(min, max, low, high, value)			\
4435a6681e2SEdward Cree 	cpu_to_le32(EF4_INSERT_NATIVE(min, max, low, high, value))
4445a6681e2SEdward Cree 
4455a6681e2SEdward Cree #define EF4_INPLACE_MASK64(min, max, low, high)				\
4465a6681e2SEdward Cree 	EF4_INSERT64(min, max, low, high, EF4_MASK64((high) + 1 - (low)))
4475a6681e2SEdward Cree 
4485a6681e2SEdward Cree #define EF4_INPLACE_MASK32(min, max, low, high)				\
4495a6681e2SEdward Cree 	EF4_INSERT32(min, max, low, high, EF4_MASK32((high) + 1 - (low)))
4505a6681e2SEdward Cree 
4515a6681e2SEdward Cree #define EF4_SET_OWORD64(oword, low, high, value) do {			\
4525a6681e2SEdward Cree 	(oword).u64[0] = (((oword).u64[0]				\
4535a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK64(0,  63, low, high))	\
4545a6681e2SEdward Cree 			  | EF4_INSERT64(0,  63, low, high, value));	\
4555a6681e2SEdward Cree 	(oword).u64[1] = (((oword).u64[1]				\
4565a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK64(64, 127, low, high))	\
4575a6681e2SEdward Cree 			  | EF4_INSERT64(64, 127, low, high, value));	\
4585a6681e2SEdward Cree 	} while (0)
4595a6681e2SEdward Cree 
4605a6681e2SEdward Cree #define EF4_SET_QWORD64(qword, low, high, value) do {			\
4615a6681e2SEdward Cree 	(qword).u64[0] = (((qword).u64[0]				\
4625a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK64(0, 63, low, high))	\
4635a6681e2SEdward Cree 			  | EF4_INSERT64(0, 63, low, high, value));	\
4645a6681e2SEdward Cree 	} while (0)
4655a6681e2SEdward Cree 
4665a6681e2SEdward Cree #define EF4_SET_OWORD32(oword, low, high, value) do {			\
4675a6681e2SEdward Cree 	(oword).u32[0] = (((oword).u32[0]				\
4685a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK32(0, 31, low, high))	\
4695a6681e2SEdward Cree 			  | EF4_INSERT32(0, 31, low, high, value));	\
4705a6681e2SEdward Cree 	(oword).u32[1] = (((oword).u32[1]				\
4715a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK32(32, 63, low, high))	\
4725a6681e2SEdward Cree 			  | EF4_INSERT32(32, 63, low, high, value));	\
4735a6681e2SEdward Cree 	(oword).u32[2] = (((oword).u32[2]				\
4745a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK32(64, 95, low, high))	\
4755a6681e2SEdward Cree 			  | EF4_INSERT32(64, 95, low, high, value));	\
4765a6681e2SEdward Cree 	(oword).u32[3] = (((oword).u32[3]				\
4775a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK32(96, 127, low, high))	\
4785a6681e2SEdward Cree 			  | EF4_INSERT32(96, 127, low, high, value));	\
4795a6681e2SEdward Cree 	} while (0)
4805a6681e2SEdward Cree 
4815a6681e2SEdward Cree #define EF4_SET_QWORD32(qword, low, high, value) do {			\
4825a6681e2SEdward Cree 	(qword).u32[0] = (((qword).u32[0]				\
4835a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK32(0, 31, low, high))	\
4845a6681e2SEdward Cree 			  | EF4_INSERT32(0, 31, low, high, value));	\
4855a6681e2SEdward Cree 	(qword).u32[1] = (((qword).u32[1]				\
4865a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK32(32, 63, low, high))	\
4875a6681e2SEdward Cree 			  | EF4_INSERT32(32, 63, low, high, value));	\
4885a6681e2SEdward Cree 	} while (0)
4895a6681e2SEdward Cree 
4905a6681e2SEdward Cree #define EF4_SET_DWORD32(dword, low, high, value) do {			\
4915a6681e2SEdward Cree 	(dword).u32[0] = (((dword).u32[0]				\
4925a6681e2SEdward Cree 			   & ~EF4_INPLACE_MASK32(0, 31, low, high))	\
4935a6681e2SEdward Cree 			  | EF4_INSERT32(0, 31, low, high, value));	\
4945a6681e2SEdward Cree 	} while (0)
4955a6681e2SEdward Cree 
4965a6681e2SEdward Cree #define EF4_SET_OWORD_FIELD64(oword, field, value)			\
4975a6681e2SEdward Cree 	EF4_SET_OWORD64(oword, EF4_LOW_BIT(field),			\
4985a6681e2SEdward Cree 			 EF4_HIGH_BIT(field), value)
4995a6681e2SEdward Cree 
5005a6681e2SEdward Cree #define EF4_SET_QWORD_FIELD64(qword, field, value)			\
5015a6681e2SEdward Cree 	EF4_SET_QWORD64(qword, EF4_LOW_BIT(field),			\
5025a6681e2SEdward Cree 			 EF4_HIGH_BIT(field), value)
5035a6681e2SEdward Cree 
5045a6681e2SEdward Cree #define EF4_SET_OWORD_FIELD32(oword, field, value)			\
5055a6681e2SEdward Cree 	EF4_SET_OWORD32(oword, EF4_LOW_BIT(field),			\
5065a6681e2SEdward Cree 			 EF4_HIGH_BIT(field), value)
5075a6681e2SEdward Cree 
5085a6681e2SEdward Cree #define EF4_SET_QWORD_FIELD32(qword, field, value)			\
5095a6681e2SEdward Cree 	EF4_SET_QWORD32(qword, EF4_LOW_BIT(field),			\
5105a6681e2SEdward Cree 			 EF4_HIGH_BIT(field), value)
5115a6681e2SEdward Cree 
5125a6681e2SEdward Cree #define EF4_SET_DWORD_FIELD(dword, field, value)			\
5135a6681e2SEdward Cree 	EF4_SET_DWORD32(dword, EF4_LOW_BIT(field),			\
5145a6681e2SEdward Cree 			 EF4_HIGH_BIT(field), value)
5155a6681e2SEdward Cree 
5165a6681e2SEdward Cree 
5175a6681e2SEdward Cree 
5185a6681e2SEdward Cree #if BITS_PER_LONG == 64
5195a6681e2SEdward Cree #define EF4_SET_OWORD_FIELD EF4_SET_OWORD_FIELD64
5205a6681e2SEdward Cree #define EF4_SET_QWORD_FIELD EF4_SET_QWORD_FIELD64
5215a6681e2SEdward Cree #else
5225a6681e2SEdward Cree #define EF4_SET_OWORD_FIELD EF4_SET_OWORD_FIELD32
5235a6681e2SEdward Cree #define EF4_SET_QWORD_FIELD EF4_SET_QWORD_FIELD32
5245a6681e2SEdward Cree #endif
5255a6681e2SEdward Cree 
5265a6681e2SEdward Cree /* Used to avoid compiler warnings about shift range exceeding width
5275a6681e2SEdward Cree  * of the data types when dma_addr_t is only 32 bits wide.
5285a6681e2SEdward Cree  */
5295a6681e2SEdward Cree #define DMA_ADDR_T_WIDTH	(8 * sizeof(dma_addr_t))
5305a6681e2SEdward Cree #define EF4_DMA_TYPE_WIDTH(width) \
5315a6681e2SEdward Cree 	(((width) < DMA_ADDR_T_WIDTH) ? (width) : DMA_ADDR_T_WIDTH)
5325a6681e2SEdward Cree 
5335a6681e2SEdward Cree 
5345a6681e2SEdward Cree /* Static initialiser */
5355a6681e2SEdward Cree #define EF4_OWORD32(a, b, c, d)				\
5365a6681e2SEdward Cree 	{ .u32 = { cpu_to_le32(a), cpu_to_le32(b),	\
5375a6681e2SEdward Cree 		   cpu_to_le32(c), cpu_to_le32(d) } }
5385a6681e2SEdward Cree 
5395a6681e2SEdward Cree #endif /* EF4_BITFIELD_H */
540