bitfield.h 20.2 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0-only */
2
/****************************************************************************
B
Ben Hutchings 已提交
3
 * Driver for Solarflare network controllers and boards
4
 * Copyright 2005-2006 Fen Systems Ltd.
B
Ben Hutchings 已提交
5
 * Copyright 2006-2013 Solarflare Communications Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 */

#ifndef EFX_BITFIELD_H
#define EFX_BITFIELD_H

/*
 * Efx bitfield access
 *
 * Efx NICs make extensive use of bitfields up to 128 bits
 * wide.  Since there is no native 128-bit datatype on most systems,
 * and since 64-bit datatypes are inefficient on 32-bit systems and
 * vice versa, we wrap accesses in a way that uses the most efficient
 * datatype.
 *
 * The NICs are PCI devices and therefore little-endian.  Since most
 * of the quantities that we deal with are DMAed to/from host memory,
 * we define our datatypes (efx_oword_t, efx_qword_t and
 * efx_dword_t) to be little-endian.
 */

/* Lowest bit numbers and widths */
#define EFX_DUMMY_FIELD_LBN 0
#define EFX_DUMMY_FIELD_WIDTH 0
29 30 31 32
#define EFX_WORD_0_LBN 0
#define EFX_WORD_0_WIDTH 16
#define EFX_WORD_1_LBN 16
#define EFX_WORD_1_WIDTH 16
33 34 35 36 37 38 39 40
#define EFX_DWORD_0_LBN 0
#define EFX_DWORD_0_WIDTH 32
#define EFX_DWORD_1_LBN 32
#define EFX_DWORD_1_WIDTH 32
#define EFX_DWORD_2_LBN 64
#define EFX_DWORD_2_WIDTH 32
#define EFX_DWORD_3_LBN 96
#define EFX_DWORD_3_WIDTH 32
41 42
#define EFX_QWORD_0_LBN 0
#define EFX_QWORD_0_WIDTH 64
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

/* Specified attribute (e.g. LBN) of the specified field */
#define EFX_VAL(field, attribute) field ## _ ## attribute
/* Low bit number of the specified field */
#define EFX_LOW_BIT(field) EFX_VAL(field, LBN)
/* Bit width of the specified field */
#define EFX_WIDTH(field) EFX_VAL(field, WIDTH)
/* High bit number of the specified field */
#define EFX_HIGH_BIT(field) (EFX_LOW_BIT(field) + EFX_WIDTH(field) - 1)
/* Mask equal in width to the specified field.
 *
 * For example, a field with width 5 would have a mask of 0x1f.
 *
 * The maximum width mask that can be generated is 64 bits.
 */
58 59 60
#define EFX_MASK64(width)			\
	((width) == 64 ? ~((u64) 0) :		\
	 (((((u64) 1) << (width))) - 1))
61 62 63 64 65 66 67 68

/* Mask equal in width to the specified field.
 *
 * For example, a field with width 5 would have a mask of 0x1f.
 *
 * The maximum width mask that can be generated is 32 bits.  Use
 * EFX_MASK64 for higher width fields.
 */
69 70 71
#define EFX_MASK32(width)			\
	((width) == 32 ? ~((u32) 0) :		\
	 (((((u32) 1) << (width))) - 1))
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

/* A doubleword (i.e. 4 byte) datatype - little-endian in HW */
typedef union efx_dword {
	__le32 u32[1];
} efx_dword_t;

/* A quadword (i.e. 8 byte) datatype - little-endian in HW */
typedef union efx_qword {
	__le64 u64[1];
	__le32 u32[2];
	efx_dword_t dword[2];
} efx_qword_t;

/* An octword (eight-word, i.e. 16 byte) datatype - little-endian in HW */
typedef union efx_oword {
	__le64 u64[2];
	efx_qword_t qword[2];
	__le32 u32[4];
	efx_dword_t dword[4];
} efx_oword_t;

/* Format string and value expanders for printk */
#define EFX_DWORD_FMT "%08x"
#define EFX_QWORD_FMT "%08x:%08x"
#define EFX_OWORD_FMT "%08x:%08x:%08x:%08x"
#define EFX_DWORD_VAL(dword)				\
	((unsigned int) le32_to_cpu((dword).u32[0]))
#define EFX_QWORD_VAL(qword)				\
	((unsigned int) le32_to_cpu((qword).u32[1])),	\
	((unsigned int) le32_to_cpu((qword).u32[0]))
#define EFX_OWORD_VAL(oword)				\
	((unsigned int) le32_to_cpu((oword).u32[3])),	\
	((unsigned int) le32_to_cpu((oword).u32[2])),	\
	((unsigned int) le32_to_cpu((oword).u32[1])),	\
	((unsigned int) le32_to_cpu((oword).u32[0]))

/*
 * Extract bit field portion [low,high) from the native-endian element
 * which contains bits [min,max).
 *
 * For example, suppose "element" represents the high 32 bits of a
 * 64-bit value, and we wish to extract the bits belonging to the bit
 * field occupying bits 28-45 of this 64-bit value.
 *
 * Then EFX_EXTRACT ( element, 32, 63, 28, 45 ) would give
 *
 *   ( element ) << 4
 *
 * The result will contain the relevant bits filled in in the range
 * [0,high-low), with garbage in bits [high-low+1,...).
 */
#define EFX_EXTRACT_NATIVE(native_element, min, max, low, high)		\
124 125 126 127
	((low) > (max) || (high) < (min) ? 0 :				\
	 (low) > (min) ?						\
	 (native_element) >> ((low) - (min)) :				\
	 (native_element) << ((min) - (low)))
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

/*
 * Extract bit field portion [low,high) from the 64-bit little-endian
 * element which contains bits [min,max)
 */
#define EFX_EXTRACT64(element, min, max, low, high)			\
	EFX_EXTRACT_NATIVE(le64_to_cpu(element), min, max, low, high)

/*
 * Extract bit field portion [low,high) from the 32-bit little-endian
 * element which contains bits [min,max)
 */
#define EFX_EXTRACT32(element, min, max, low, high)			\
	EFX_EXTRACT_NATIVE(le32_to_cpu(element), min, max, low, high)

#define EFX_EXTRACT_OWORD64(oword, low, high)				\
144 145
	((EFX_EXTRACT64((oword).u64[0], 0, 63, low, high) |		\
	  EFX_EXTRACT64((oword).u64[1], 64, 127, low, high)) &		\
146
	 EFX_MASK64((high) + 1 - (low)))
147 148

#define EFX_EXTRACT_QWORD64(qword, low, high)				\
149
	(EFX_EXTRACT64((qword).u64[0], 0, 63, low, high) &		\
150
	 EFX_MASK64((high) + 1 - (low)))
151 152

#define EFX_EXTRACT_OWORD32(oword, low, high)				\
153 154 155 156
	((EFX_EXTRACT32((oword).u32[0], 0, 31, low, high) |		\
	  EFX_EXTRACT32((oword).u32[1], 32, 63, low, high) |		\
	  EFX_EXTRACT32((oword).u32[2], 64, 95, low, high) |		\
	  EFX_EXTRACT32((oword).u32[3], 96, 127, low, high)) &		\
157
	 EFX_MASK32((high) + 1 - (low)))
158 159

#define EFX_EXTRACT_QWORD32(qword, low, high)				\
160 161
	((EFX_EXTRACT32((qword).u32[0], 0, 31, low, high) |		\
	  EFX_EXTRACT32((qword).u32[1], 32, 63, low, high)) &		\
162
	 EFX_MASK32((high) + 1 - (low)))
163

164 165
#define EFX_EXTRACT_DWORD(dword, low, high)			\
	(EFX_EXTRACT32((dword).u32[0], 0, 31, low, high) &	\
166
	 EFX_MASK32((high) + 1 - (low)))
167

168 169 170
#define EFX_OWORD_FIELD64(oword, field)				\
	EFX_EXTRACT_OWORD64(oword, EFX_LOW_BIT(field),		\
			    EFX_HIGH_BIT(field))
171

172 173 174
#define EFX_QWORD_FIELD64(qword, field)				\
	EFX_EXTRACT_QWORD64(qword, EFX_LOW_BIT(field),		\
			    EFX_HIGH_BIT(field))
175

176 177 178
#define EFX_OWORD_FIELD32(oword, field)				\
	EFX_EXTRACT_OWORD32(oword, EFX_LOW_BIT(field),		\
			    EFX_HIGH_BIT(field))
179

180 181 182
#define EFX_QWORD_FIELD32(qword, field)				\
	EFX_EXTRACT_QWORD32(qword, EFX_LOW_BIT(field),		\
			    EFX_HIGH_BIT(field))
183

184 185 186
#define EFX_DWORD_FIELD(dword, field)				\
	EFX_EXTRACT_DWORD(dword, EFX_LOW_BIT(field),		\
			  EFX_HIGH_BIT(field))
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

#define EFX_OWORD_IS_ZERO64(oword)					\
	(((oword).u64[0] | (oword).u64[1]) == (__force __le64) 0)

#define EFX_QWORD_IS_ZERO64(qword)					\
	(((qword).u64[0]) == (__force __le64) 0)

#define EFX_OWORD_IS_ZERO32(oword)					     \
	(((oword).u32[0] | (oword).u32[1] | (oword).u32[2] | (oword).u32[3]) \
	 == (__force __le32) 0)

#define EFX_QWORD_IS_ZERO32(qword)					\
	(((qword).u32[0] | (qword).u32[1]) == (__force __le32) 0)

#define EFX_DWORD_IS_ZERO(dword)					\
	(((dword).u32[0]) == (__force __le32) 0)

#define EFX_OWORD_IS_ALL_ONES64(oword)					\
	(((oword).u64[0] & (oword).u64[1]) == ~((__force __le64) 0))

#define EFX_QWORD_IS_ALL_ONES64(qword)					\
	((qword).u64[0] == ~((__force __le64) 0))

#define EFX_OWORD_IS_ALL_ONES32(oword)					\
	(((oword).u32[0] & (oword).u32[1] & (oword).u32[2] & (oword).u32[3]) \
	 == ~((__force __le32) 0))

#define EFX_QWORD_IS_ALL_ONES32(qword)					\
	(((qword).u32[0] & (qword).u32[1]) == ~((__force __le32) 0))

#define EFX_DWORD_IS_ALL_ONES(dword)					\
	((dword).u32[0] == ~((__force __le32) 0))

#if BITS_PER_LONG == 64
#define EFX_OWORD_FIELD		EFX_OWORD_FIELD64
#define EFX_QWORD_FIELD		EFX_QWORD_FIELD64
#define EFX_OWORD_IS_ZERO	EFX_OWORD_IS_ZERO64
#define EFX_QWORD_IS_ZERO	EFX_QWORD_IS_ZERO64
#define EFX_OWORD_IS_ALL_ONES	EFX_OWORD_IS_ALL_ONES64
#define EFX_QWORD_IS_ALL_ONES	EFX_QWORD_IS_ALL_ONES64
#else
#define EFX_OWORD_FIELD		EFX_OWORD_FIELD32
#define EFX_QWORD_FIELD		EFX_QWORD_FIELD32
#define EFX_OWORD_IS_ZERO	EFX_OWORD_IS_ZERO32
#define EFX_QWORD_IS_ZERO	EFX_QWORD_IS_ZERO32
#define EFX_OWORD_IS_ALL_ONES	EFX_OWORD_IS_ALL_ONES32
#define EFX_QWORD_IS_ALL_ONES	EFX_QWORD_IS_ALL_ONES32
#endif

/*
 * Construct bit field portion
 *
 * Creates the portion of the bit field [low,high) that lies within
 * the range [min,max).
 */
#define EFX_INSERT_NATIVE64(min, max, low, high, value)		\
	(((low > max) || (high < min)) ? 0 :			\
	 ((low > min) ?						\
	  (((u64) (value)) << (low - min)) :		\
	  (((u64) (value)) >> (min - low))))

#define EFX_INSERT_NATIVE32(min, max, low, high, value)		\
	(((low > max) || (high < min)) ? 0 :			\
	 ((low > min) ?						\
	  (((u32) (value)) << (low - min)) :		\
	  (((u32) (value)) >> (min - low))))

#define EFX_INSERT_NATIVE(min, max, low, high, value)		\
	((((max - min) >= 32) || ((high - low) >= 32)) ?	\
	 EFX_INSERT_NATIVE64(min, max, low, high, value) :	\
	 EFX_INSERT_NATIVE32(min, max, low, high, value))

/*
 * Construct bit field portion
 *
 * Creates the portion of the named bit field that lies within the
 * range [min,max).
 */
#define EFX_INSERT_FIELD_NATIVE(min, max, field, value)		\
	EFX_INSERT_NATIVE(min, max, EFX_LOW_BIT(field),		\
			  EFX_HIGH_BIT(field), value)

/*
 * Construct bit field
 *
 * Creates the portion of the named bit fields that lie within the
 * range [min,max).
 */
#define EFX_INSERT_FIELDS_NATIVE(min, max,				\
				 field1, value1,			\
				 field2, value2,			\
				 field3, value3,			\
				 field4, value4,			\
				 field5, value5,			\
				 field6, value6,			\
				 field7, value7,			\
				 field8, value8,			\
				 field9, value9,			\
285 286 287 288
				 field10, value10,			\
				 field11, value11,			\
				 field12, value12,			\
				 field13, value13)			\
289 290 291 292 293 294 295 296 297
	(EFX_INSERT_FIELD_NATIVE((min), (max), field1, (value1)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field2, (value2)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field3, (value3)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field4, (value4)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field5, (value5)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field6, (value6)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field7, (value7)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field8, (value8)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field9, (value9)) |	\
298 299 300 301
	 EFX_INSERT_FIELD_NATIVE((min), (max), field10, (value10)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field11, (value11)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field12, (value12)) |	\
	 EFX_INSERT_FIELD_NATIVE((min), (max), field13, (value13)))
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

#define EFX_INSERT_FIELDS64(...)				\
	cpu_to_le64(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))

#define EFX_INSERT_FIELDS32(...)				\
	cpu_to_le32(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))

#define EFX_POPULATE_OWORD64(oword, ...) do {				\
	(oword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
	(oword).u64[1] = EFX_INSERT_FIELDS64(64, 127, __VA_ARGS__);	\
	} while (0)

#define EFX_POPULATE_QWORD64(qword, ...) do {				\
	(qword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__);	\
	} while (0)

#define EFX_POPULATE_OWORD32(oword, ...) do {				\
	(oword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
	(oword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
	(oword).u32[2] = EFX_INSERT_FIELDS32(64, 95, __VA_ARGS__);	\
	(oword).u32[3] = EFX_INSERT_FIELDS32(96, 127, __VA_ARGS__);	\
	} while (0)

#define EFX_POPULATE_QWORD32(qword, ...) do {				\
	(qword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
	(qword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__);	\
	} while (0)

#define EFX_POPULATE_DWORD(dword, ...) do {				\
	(dword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__);	\
	} while (0)

#if BITS_PER_LONG == 64
#define EFX_POPULATE_OWORD EFX_POPULATE_OWORD64
#define EFX_POPULATE_QWORD EFX_POPULATE_QWORD64
#else
#define EFX_POPULATE_OWORD EFX_POPULATE_OWORD32
#define EFX_POPULATE_QWORD EFX_POPULATE_QWORD32
#endif

/* Populate an octword field with various numbers of arguments */
343 344 345 346 347 348 349
#define EFX_POPULATE_OWORD_13 EFX_POPULATE_OWORD
#define EFX_POPULATE_OWORD_12(oword, ...) \
	EFX_POPULATE_OWORD_13(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_11(oword, ...) \
	EFX_POPULATE_OWORD_12(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_10(oword, ...) \
	EFX_POPULATE_OWORD_11(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
#define EFX_POPULATE_OWORD_9(oword, ...) \
	EFX_POPULATE_OWORD_10(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_8(oword, ...) \
	EFX_POPULATE_OWORD_9(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_7(oword, ...) \
	EFX_POPULATE_OWORD_8(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_6(oword, ...) \
	EFX_POPULATE_OWORD_7(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_5(oword, ...) \
	EFX_POPULATE_OWORD_6(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_4(oword, ...) \
	EFX_POPULATE_OWORD_5(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_3(oword, ...) \
	EFX_POPULATE_OWORD_4(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_2(oword, ...) \
	EFX_POPULATE_OWORD_3(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_OWORD_1(oword, ...) \
	EFX_POPULATE_OWORD_2(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_ZERO_OWORD(oword) \
	EFX_POPULATE_OWORD_1(oword, EFX_DUMMY_FIELD, 0)
#define EFX_SET_OWORD(oword) \
	EFX_POPULATE_OWORD_4(oword, \
			     EFX_DWORD_0, 0xffffffff, \
			     EFX_DWORD_1, 0xffffffff, \
			     EFX_DWORD_2, 0xffffffff, \
			     EFX_DWORD_3, 0xffffffff)

/* Populate a quadword field with various numbers of arguments */
378 379 380 381 382 383 384
#define EFX_POPULATE_QWORD_13 EFX_POPULATE_QWORD
#define EFX_POPULATE_QWORD_12(qword, ...) \
	EFX_POPULATE_QWORD_13(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_11(qword, ...) \
	EFX_POPULATE_QWORD_12(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_10(qword, ...) \
	EFX_POPULATE_QWORD_11(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
#define EFX_POPULATE_QWORD_9(qword, ...) \
	EFX_POPULATE_QWORD_10(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_8(qword, ...) \
	EFX_POPULATE_QWORD_9(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_7(qword, ...) \
	EFX_POPULATE_QWORD_8(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_6(qword, ...) \
	EFX_POPULATE_QWORD_7(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_5(qword, ...) \
	EFX_POPULATE_QWORD_6(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_4(qword, ...) \
	EFX_POPULATE_QWORD_5(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_3(qword, ...) \
	EFX_POPULATE_QWORD_4(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_2(qword, ...) \
	EFX_POPULATE_QWORD_3(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_QWORD_1(qword, ...) \
	EFX_POPULATE_QWORD_2(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_ZERO_QWORD(qword) \
	EFX_POPULATE_QWORD_1(qword, EFX_DUMMY_FIELD, 0)
#define EFX_SET_QWORD(qword) \
	EFX_POPULATE_QWORD_2(qword, \
			     EFX_DWORD_0, 0xffffffff, \
			     EFX_DWORD_1, 0xffffffff)

/* Populate a dword field with various numbers of arguments */
411 412 413 414 415 416 417
#define EFX_POPULATE_DWORD_13 EFX_POPULATE_DWORD
#define EFX_POPULATE_DWORD_12(dword, ...) \
	EFX_POPULATE_DWORD_13(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_11(dword, ...) \
	EFX_POPULATE_DWORD_12(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_10(dword, ...) \
	EFX_POPULATE_DWORD_11(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
#define EFX_POPULATE_DWORD_9(dword, ...) \
	EFX_POPULATE_DWORD_10(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_8(dword, ...) \
	EFX_POPULATE_DWORD_9(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_7(dword, ...) \
	EFX_POPULATE_DWORD_8(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_6(dword, ...) \
	EFX_POPULATE_DWORD_7(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_5(dword, ...) \
	EFX_POPULATE_DWORD_6(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_4(dword, ...) \
	EFX_POPULATE_DWORD_5(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_3(dword, ...) \
	EFX_POPULATE_DWORD_4(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_2(dword, ...) \
	EFX_POPULATE_DWORD_3(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_POPULATE_DWORD_1(dword, ...) \
	EFX_POPULATE_DWORD_2(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
#define EFX_ZERO_DWORD(dword) \
	EFX_POPULATE_DWORD_1(dword, EFX_DUMMY_FIELD, 0)
#define EFX_SET_DWORD(dword) \
	EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 0xffffffff)

/*
 * Modify a named field within an already-populated structure.  Used
 * for read-modify-write operations.
 *
 */
#define EFX_INVERT_OWORD(oword) do {		\
	(oword).u64[0] = ~((oword).u64[0]);	\
	(oword).u64[1] = ~((oword).u64[1]);	\
	} while (0)

B
Ben Hutchings 已提交
451 452 453 454 455 456
#define EFX_AND_OWORD(oword, from, mask)			\
	do {							\
		(oword).u64[0] = (from).u64[0] & (mask).u64[0];	\
		(oword).u64[1] = (from).u64[1] & (mask).u64[1];	\
	} while (0)

457 458 459
#define EFX_AND_QWORD(qword, from, mask)			\
		(qword).u64[0] = (from).u64[0] & (mask).u64[0]

B
Ben Hutchings 已提交
460 461 462 463 464 465
#define EFX_OR_OWORD(oword, from, mask)				\
	do {							\
		(oword).u64[0] = (from).u64[0] | (mask).u64[0];	\
		(oword).u64[1] = (from).u64[1] | (mask).u64[1];	\
	} while (0)

466 467
#define EFX_INSERT64(min, max, low, high, value)			\
	cpu_to_le64(EFX_INSERT_NATIVE(min, max, low, high, value))
468

469 470
#define EFX_INSERT32(min, max, low, high, value)			\
	cpu_to_le32(EFX_INSERT_NATIVE(min, max, low, high, value))
471

472
#define EFX_INPLACE_MASK64(min, max, low, high)				\
473
	EFX_INSERT64(min, max, low, high, EFX_MASK64((high) + 1 - (low)))
474

475
#define EFX_INPLACE_MASK32(min, max, low, high)				\
476
	EFX_INSERT32(min, max, low, high, EFX_MASK32((high) + 1 - (low)))
477

478
#define EFX_SET_OWORD64(oword, low, high, value) do {			\
479
	(oword).u64[0] = (((oword).u64[0]				\
480 481
			   & ~EFX_INPLACE_MASK64(0,  63, low, high))	\
			  | EFX_INSERT64(0,  63, low, high, value));	\
482
	(oword).u64[1] = (((oword).u64[1]				\
483 484
			   & ~EFX_INPLACE_MASK64(64, 127, low, high))	\
			  | EFX_INSERT64(64, 127, low, high, value));	\
485 486
	} while (0)

487
#define EFX_SET_QWORD64(qword, low, high, value) do {			\
488
	(qword).u64[0] = (((qword).u64[0]				\
489 490
			   & ~EFX_INPLACE_MASK64(0, 63, low, high))	\
			  | EFX_INSERT64(0, 63, low, high, value));	\
491 492
	} while (0)

493
#define EFX_SET_OWORD32(oword, low, high, value) do {			\
494
	(oword).u32[0] = (((oword).u32[0]				\
495 496
			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
			  | EFX_INSERT32(0, 31, low, high, value));	\
497
	(oword).u32[1] = (((oword).u32[1]				\
498 499
			   & ~EFX_INPLACE_MASK32(32, 63, low, high))	\
			  | EFX_INSERT32(32, 63, low, high, value));	\
500
	(oword).u32[2] = (((oword).u32[2]				\
501 502
			   & ~EFX_INPLACE_MASK32(64, 95, low, high))	\
			  | EFX_INSERT32(64, 95, low, high, value));	\
503
	(oword).u32[3] = (((oword).u32[3]				\
504 505
			   & ~EFX_INPLACE_MASK32(96, 127, low, high))	\
			  | EFX_INSERT32(96, 127, low, high, value));	\
506 507
	} while (0)

508
#define EFX_SET_QWORD32(qword, low, high, value) do {			\
509
	(qword).u32[0] = (((qword).u32[0]				\
510 511
			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
			  | EFX_INSERT32(0, 31, low, high, value));	\
512
	(qword).u32[1] = (((qword).u32[1]				\
513 514
			   & ~EFX_INPLACE_MASK32(32, 63, low, high))	\
			  | EFX_INSERT32(32, 63, low, high, value));	\
515 516
	} while (0)

517 518 519 520
#define EFX_SET_DWORD32(dword, low, high, value) do {			\
	(dword).u32[0] = (((dword).u32[0]				\
			   & ~EFX_INPLACE_MASK32(0, 31, low, high))	\
			  | EFX_INSERT32(0, 31, low, high, value));	\
521 522
	} while (0)

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
#define EFX_SET_OWORD_FIELD64(oword, field, value)			\
	EFX_SET_OWORD64(oword, EFX_LOW_BIT(field),			\
			 EFX_HIGH_BIT(field), value)

#define EFX_SET_QWORD_FIELD64(qword, field, value)			\
	EFX_SET_QWORD64(qword, EFX_LOW_BIT(field),			\
			 EFX_HIGH_BIT(field), value)

#define EFX_SET_OWORD_FIELD32(oword, field, value)			\
	EFX_SET_OWORD32(oword, EFX_LOW_BIT(field),			\
			 EFX_HIGH_BIT(field), value)

#define EFX_SET_QWORD_FIELD32(qword, field, value)			\
	EFX_SET_QWORD32(qword, EFX_LOW_BIT(field),			\
			 EFX_HIGH_BIT(field), value)

#define EFX_SET_DWORD_FIELD(dword, field, value)			\
	EFX_SET_DWORD32(dword, EFX_LOW_BIT(field),			\
			 EFX_HIGH_BIT(field), value)



545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
#if BITS_PER_LONG == 64
#define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD64
#define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD64
#else
#define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD32
#define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD32
#endif

/* Used to avoid compiler warnings about shift range exceeding width
 * of the data types when dma_addr_t is only 32 bits wide.
 */
#define DMA_ADDR_T_WIDTH	(8 * sizeof(dma_addr_t))
#define EFX_DMA_TYPE_WIDTH(width) \
	(((width) < DMA_ADDR_T_WIDTH) ? (width) : DMA_ADDR_T_WIDTH)

B
Ben Hutchings 已提交
560 561

/* Static initialiser */
B
Ben Hutchings 已提交
562 563
#define EFX_OWORD32(a, b, c, d)				\
	{ .u32 = { cpu_to_le32(a), cpu_to_le32(b),	\
564
		   cpu_to_le32(c), cpu_to_le32(d) } }
B
Ben Hutchings 已提交
565

566
#endif /* EFX_BITFIELD_H */