bitfield.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. #ifndef EFX_BITFIELD_H
  8. #define EFX_BITFIELD_H
  9. /*
  10. * Efx bitfield access
  11. *
  12. * Efx NICs make extensive use of bitfields up to 128 bits
  13. * wide. Since there is no native 128-bit datatype on most systems,
  14. * and since 64-bit datatypes are inefficient on 32-bit systems and
  15. * vice versa, we wrap accesses in a way that uses the most efficient
  16. * datatype.
  17. *
  18. * The NICs are PCI devices and therefore little-endian. Since most
  19. * of the quantities that we deal with are DMAed to/from host memory,
  20. * we define our datatypes (efx_oword_t, efx_qword_t and
  21. * efx_dword_t) to be little-endian.
  22. */
  23. /* Lowest bit numbers and widths */
  24. #define EFX_DUMMY_FIELD_LBN 0
  25. #define EFX_DUMMY_FIELD_WIDTH 0
  26. #define EFX_WORD_0_LBN 0
  27. #define EFX_WORD_0_WIDTH 16
  28. #define EFX_WORD_1_LBN 16
  29. #define EFX_WORD_1_WIDTH 16
  30. #define EFX_DWORD_0_LBN 0
  31. #define EFX_DWORD_0_WIDTH 32
  32. #define EFX_DWORD_1_LBN 32
  33. #define EFX_DWORD_1_WIDTH 32
  34. #define EFX_DWORD_2_LBN 64
  35. #define EFX_DWORD_2_WIDTH 32
  36. #define EFX_DWORD_3_LBN 96
  37. #define EFX_DWORD_3_WIDTH 32
  38. #define EFX_QWORD_0_LBN 0
  39. #define EFX_QWORD_0_WIDTH 64
  40. /* Specified attribute (e.g. LBN) of the specified field */
  41. #define EFX_VAL(field, attribute) field ## _ ## attribute
  42. /* Low bit number of the specified field */
  43. #define EFX_LOW_BIT(field) EFX_VAL(field, LBN)
  44. /* Bit width of the specified field */
  45. #define EFX_WIDTH(field) EFX_VAL(field, WIDTH)
  46. /* High bit number of the specified field */
  47. #define EFX_HIGH_BIT(field) (EFX_LOW_BIT(field) + EFX_WIDTH(field) - 1)
  48. /* Mask equal in width to the specified field.
  49. *
  50. * For example, a field with width 5 would have a mask of 0x1f.
  51. *
  52. * The maximum width mask that can be generated is 64 bits.
  53. */
  54. #define EFX_MASK64(width) \
  55. ((width) == 64 ? ~((u64) 0) : \
  56. (((((u64) 1) << (width))) - 1))
  57. /* Mask equal in width to the specified field.
  58. *
  59. * For example, a field with width 5 would have a mask of 0x1f.
  60. *
  61. * The maximum width mask that can be generated is 32 bits. Use
  62. * EFX_MASK64 for higher width fields.
  63. */
  64. #define EFX_MASK32(width) \
  65. ((width) == 32 ? ~((u32) 0) : \
  66. (((((u32) 1) << (width))) - 1))
  67. /* A doubleword (i.e. 4 byte) datatype - little-endian in HW */
  68. typedef union efx_dword {
  69. __le32 u32[1];
  70. } efx_dword_t;
  71. /* A quadword (i.e. 8 byte) datatype - little-endian in HW */
  72. typedef union efx_qword {
  73. __le64 u64[1];
  74. __le32 u32[2];
  75. efx_dword_t dword[2];
  76. } efx_qword_t;
  77. /* An octword (eight-word, i.e. 16 byte) datatype - little-endian in HW */
  78. typedef union efx_oword {
  79. __le64 u64[2];
  80. efx_qword_t qword[2];
  81. __le32 u32[4];
  82. efx_dword_t dword[4];
  83. } efx_oword_t;
  84. /* Format string and value expanders for printk */
  85. #define EFX_DWORD_FMT "%08x"
  86. #define EFX_QWORD_FMT "%08x:%08x"
  87. #define EFX_OWORD_FMT "%08x:%08x:%08x:%08x"
  88. #define EFX_DWORD_VAL(dword) \
  89. ((unsigned int) le32_to_cpu((dword).u32[0]))
  90. #define EFX_QWORD_VAL(qword) \
  91. ((unsigned int) le32_to_cpu((qword).u32[1])), \
  92. ((unsigned int) le32_to_cpu((qword).u32[0]))
  93. #define EFX_OWORD_VAL(oword) \
  94. ((unsigned int) le32_to_cpu((oword).u32[3])), \
  95. ((unsigned int) le32_to_cpu((oword).u32[2])), \
  96. ((unsigned int) le32_to_cpu((oword).u32[1])), \
  97. ((unsigned int) le32_to_cpu((oword).u32[0]))
  98. /*
  99. * Extract bit field portion [low,high) from the native-endian element
  100. * which contains bits [min,max).
  101. *
  102. * For example, suppose "element" represents the high 32 bits of a
  103. * 64-bit value, and we wish to extract the bits belonging to the bit
  104. * field occupying bits 28-45 of this 64-bit value.
  105. *
  106. * Then EFX_EXTRACT ( element, 32, 63, 28, 45 ) would give
  107. *
  108. * ( element ) << 4
  109. *
  110. * The result will contain the relevant bits filled in in the range
  111. * [0,high-low), with garbage in bits [high-low+1,...).
  112. */
  113. #define EFX_EXTRACT_NATIVE(native_element, min, max, low, high) \
  114. ((low) > (max) || (high) < (min) ? 0 : \
  115. (low) > (min) ? \
  116. (native_element) >> ((low) - (min)) : \
  117. (native_element) << ((min) - (low)))
  118. /*
  119. * Extract bit field portion [low,high) from the 64-bit little-endian
  120. * element which contains bits [min,max)
  121. */
  122. #define EFX_EXTRACT64(element, min, max, low, high) \
  123. EFX_EXTRACT_NATIVE(le64_to_cpu(element), min, max, low, high)
  124. /*
  125. * Extract bit field portion [low,high) from the 32-bit little-endian
  126. * element which contains bits [min,max)
  127. */
  128. #define EFX_EXTRACT32(element, min, max, low, high) \
  129. EFX_EXTRACT_NATIVE(le32_to_cpu(element), min, max, low, high)
  130. #define EFX_EXTRACT_OWORD64(oword, low, high) \
  131. ((EFX_EXTRACT64((oword).u64[0], 0, 63, low, high) | \
  132. EFX_EXTRACT64((oword).u64[1], 64, 127, low, high)) & \
  133. EFX_MASK64((high) + 1 - (low)))
  134. #define EFX_EXTRACT_QWORD64(qword, low, high) \
  135. (EFX_EXTRACT64((qword).u64[0], 0, 63, low, high) & \
  136. EFX_MASK64((high) + 1 - (low)))
  137. #define EFX_EXTRACT_OWORD32(oword, low, high) \
  138. ((EFX_EXTRACT32((oword).u32[0], 0, 31, low, high) | \
  139. EFX_EXTRACT32((oword).u32[1], 32, 63, low, high) | \
  140. EFX_EXTRACT32((oword).u32[2], 64, 95, low, high) | \
  141. EFX_EXTRACT32((oword).u32[3], 96, 127, low, high)) & \
  142. EFX_MASK32((high) + 1 - (low)))
  143. #define EFX_EXTRACT_QWORD32(qword, low, high) \
  144. ((EFX_EXTRACT32((qword).u32[0], 0, 31, low, high) | \
  145. EFX_EXTRACT32((qword).u32[1], 32, 63, low, high)) & \
  146. EFX_MASK32((high) + 1 - (low)))
  147. #define EFX_EXTRACT_DWORD(dword, low, high) \
  148. (EFX_EXTRACT32((dword).u32[0], 0, 31, low, high) & \
  149. EFX_MASK32((high) + 1 - (low)))
  150. #define EFX_OWORD_FIELD64(oword, field) \
  151. EFX_EXTRACT_OWORD64(oword, EFX_LOW_BIT(field), \
  152. EFX_HIGH_BIT(field))
  153. #define EFX_QWORD_FIELD64(qword, field) \
  154. EFX_EXTRACT_QWORD64(qword, EFX_LOW_BIT(field), \
  155. EFX_HIGH_BIT(field))
  156. #define EFX_OWORD_FIELD32(oword, field) \
  157. EFX_EXTRACT_OWORD32(oword, EFX_LOW_BIT(field), \
  158. EFX_HIGH_BIT(field))
  159. #define EFX_QWORD_FIELD32(qword, field) \
  160. EFX_EXTRACT_QWORD32(qword, EFX_LOW_BIT(field), \
  161. EFX_HIGH_BIT(field))
  162. #define EFX_DWORD_FIELD(dword, field) \
  163. EFX_EXTRACT_DWORD(dword, EFX_LOW_BIT(field), \
  164. EFX_HIGH_BIT(field))
  165. #define EFX_OWORD_IS_ZERO64(oword) \
  166. (((oword).u64[0] | (oword).u64[1]) == (__force __le64) 0)
  167. #define EFX_QWORD_IS_ZERO64(qword) \
  168. (((qword).u64[0]) == (__force __le64) 0)
  169. #define EFX_OWORD_IS_ZERO32(oword) \
  170. (((oword).u32[0] | (oword).u32[1] | (oword).u32[2] | (oword).u32[3]) \
  171. == (__force __le32) 0)
  172. #define EFX_QWORD_IS_ZERO32(qword) \
  173. (((qword).u32[0] | (qword).u32[1]) == (__force __le32) 0)
  174. #define EFX_DWORD_IS_ZERO(dword) \
  175. (((dword).u32[0]) == (__force __le32) 0)
  176. #define EFX_OWORD_IS_ALL_ONES64(oword) \
  177. (((oword).u64[0] & (oword).u64[1]) == ~((__force __le64) 0))
  178. #define EFX_QWORD_IS_ALL_ONES64(qword) \
  179. ((qword).u64[0] == ~((__force __le64) 0))
  180. #define EFX_OWORD_IS_ALL_ONES32(oword) \
  181. (((oword).u32[0] & (oword).u32[1] & (oword).u32[2] & (oword).u32[3]) \
  182. == ~((__force __le32) 0))
  183. #define EFX_QWORD_IS_ALL_ONES32(qword) \
  184. (((qword).u32[0] & (qword).u32[1]) == ~((__force __le32) 0))
  185. #define EFX_DWORD_IS_ALL_ONES(dword) \
  186. ((dword).u32[0] == ~((__force __le32) 0))
  187. #if BITS_PER_LONG == 64
  188. #define EFX_OWORD_FIELD EFX_OWORD_FIELD64
  189. #define EFX_QWORD_FIELD EFX_QWORD_FIELD64
  190. #define EFX_OWORD_IS_ZERO EFX_OWORD_IS_ZERO64
  191. #define EFX_QWORD_IS_ZERO EFX_QWORD_IS_ZERO64
  192. #define EFX_OWORD_IS_ALL_ONES EFX_OWORD_IS_ALL_ONES64
  193. #define EFX_QWORD_IS_ALL_ONES EFX_QWORD_IS_ALL_ONES64
  194. #else
  195. #define EFX_OWORD_FIELD EFX_OWORD_FIELD32
  196. #define EFX_QWORD_FIELD EFX_QWORD_FIELD32
  197. #define EFX_OWORD_IS_ZERO EFX_OWORD_IS_ZERO32
  198. #define EFX_QWORD_IS_ZERO EFX_QWORD_IS_ZERO32
  199. #define EFX_OWORD_IS_ALL_ONES EFX_OWORD_IS_ALL_ONES32
  200. #define EFX_QWORD_IS_ALL_ONES EFX_QWORD_IS_ALL_ONES32
  201. #endif
  202. /*
  203. * Construct bit field portion
  204. *
  205. * Creates the portion of the bit field [low,high) that lies within
  206. * the range [min,max).
  207. */
  208. #define EFX_INSERT_NATIVE64(min, max, low, high, value) \
  209. (((low > max) || (high < min)) ? 0 : \
  210. ((low > min) ? \
  211. (((u64) (value)) << (low - min)) : \
  212. (((u64) (value)) >> (min - low))))
  213. #define EFX_INSERT_NATIVE32(min, max, low, high, value) \
  214. (((low > max) || (high < min)) ? 0 : \
  215. ((low > min) ? \
  216. (((u32) (value)) << (low - min)) : \
  217. (((u32) (value)) >> (min - low))))
  218. #define EFX_INSERT_NATIVE(min, max, low, high, value) \
  219. ((((max - min) >= 32) || ((high - low) >= 32)) ? \
  220. EFX_INSERT_NATIVE64(min, max, low, high, value) : \
  221. EFX_INSERT_NATIVE32(min, max, low, high, value))
  222. /*
  223. * Construct bit field portion
  224. *
  225. * Creates the portion of the named bit field that lies within the
  226. * range [min,max).
  227. */
  228. #define EFX_INSERT_FIELD_NATIVE(min, max, field, value) \
  229. EFX_INSERT_NATIVE(min, max, EFX_LOW_BIT(field), \
  230. EFX_HIGH_BIT(field), value)
  231. /*
  232. * Construct bit field
  233. *
  234. * Creates the portion of the named bit fields that lie within the
  235. * range [min,max).
  236. */
  237. #define EFX_INSERT_FIELDS_NATIVE(min, max, \
  238. field1, value1, \
  239. field2, value2, \
  240. field3, value3, \
  241. field4, value4, \
  242. field5, value5, \
  243. field6, value6, \
  244. field7, value7, \
  245. field8, value8, \
  246. field9, value9, \
  247. field10, value10) \
  248. (EFX_INSERT_FIELD_NATIVE((min), (max), field1, (value1)) | \
  249. EFX_INSERT_FIELD_NATIVE((min), (max), field2, (value2)) | \
  250. EFX_INSERT_FIELD_NATIVE((min), (max), field3, (value3)) | \
  251. EFX_INSERT_FIELD_NATIVE((min), (max), field4, (value4)) | \
  252. EFX_INSERT_FIELD_NATIVE((min), (max), field5, (value5)) | \
  253. EFX_INSERT_FIELD_NATIVE((min), (max), field6, (value6)) | \
  254. EFX_INSERT_FIELD_NATIVE((min), (max), field7, (value7)) | \
  255. EFX_INSERT_FIELD_NATIVE((min), (max), field8, (value8)) | \
  256. EFX_INSERT_FIELD_NATIVE((min), (max), field9, (value9)) | \
  257. EFX_INSERT_FIELD_NATIVE((min), (max), field10, (value10)))
  258. #define EFX_INSERT_FIELDS64(...) \
  259. cpu_to_le64(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
  260. #define EFX_INSERT_FIELDS32(...) \
  261. cpu_to_le32(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
  262. #define EFX_POPULATE_OWORD64(oword, ...) do { \
  263. (oword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__); \
  264. (oword).u64[1] = EFX_INSERT_FIELDS64(64, 127, __VA_ARGS__); \
  265. } while (0)
  266. #define EFX_POPULATE_QWORD64(qword, ...) do { \
  267. (qword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__); \
  268. } while (0)
  269. #define EFX_POPULATE_OWORD32(oword, ...) do { \
  270. (oword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__); \
  271. (oword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__); \
  272. (oword).u32[2] = EFX_INSERT_FIELDS32(64, 95, __VA_ARGS__); \
  273. (oword).u32[3] = EFX_INSERT_FIELDS32(96, 127, __VA_ARGS__); \
  274. } while (0)
  275. #define EFX_POPULATE_QWORD32(qword, ...) do { \
  276. (qword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__); \
  277. (qword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__); \
  278. } while (0)
  279. #define EFX_POPULATE_DWORD(dword, ...) do { \
  280. (dword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__); \
  281. } while (0)
  282. #if BITS_PER_LONG == 64
  283. #define EFX_POPULATE_OWORD EFX_POPULATE_OWORD64
  284. #define EFX_POPULATE_QWORD EFX_POPULATE_QWORD64
  285. #else
  286. #define EFX_POPULATE_OWORD EFX_POPULATE_OWORD32
  287. #define EFX_POPULATE_QWORD EFX_POPULATE_QWORD32
  288. #endif
  289. /* Populate an octword field with various numbers of arguments */
  290. #define EFX_POPULATE_OWORD_10 EFX_POPULATE_OWORD
  291. #define EFX_POPULATE_OWORD_9(oword, ...) \
  292. EFX_POPULATE_OWORD_10(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  293. #define EFX_POPULATE_OWORD_8(oword, ...) \
  294. EFX_POPULATE_OWORD_9(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  295. #define EFX_POPULATE_OWORD_7(oword, ...) \
  296. EFX_POPULATE_OWORD_8(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  297. #define EFX_POPULATE_OWORD_6(oword, ...) \
  298. EFX_POPULATE_OWORD_7(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  299. #define EFX_POPULATE_OWORD_5(oword, ...) \
  300. EFX_POPULATE_OWORD_6(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  301. #define EFX_POPULATE_OWORD_4(oword, ...) \
  302. EFX_POPULATE_OWORD_5(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  303. #define EFX_POPULATE_OWORD_3(oword, ...) \
  304. EFX_POPULATE_OWORD_4(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  305. #define EFX_POPULATE_OWORD_2(oword, ...) \
  306. EFX_POPULATE_OWORD_3(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  307. #define EFX_POPULATE_OWORD_1(oword, ...) \
  308. EFX_POPULATE_OWORD_2(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  309. #define EFX_ZERO_OWORD(oword) \
  310. EFX_POPULATE_OWORD_1(oword, EFX_DUMMY_FIELD, 0)
  311. #define EFX_SET_OWORD(oword) \
  312. EFX_POPULATE_OWORD_4(oword, \
  313. EFX_DWORD_0, 0xffffffff, \
  314. EFX_DWORD_1, 0xffffffff, \
  315. EFX_DWORD_2, 0xffffffff, \
  316. EFX_DWORD_3, 0xffffffff)
  317. /* Populate a quadword field with various numbers of arguments */
  318. #define EFX_POPULATE_QWORD_10 EFX_POPULATE_QWORD
  319. #define EFX_POPULATE_QWORD_9(qword, ...) \
  320. EFX_POPULATE_QWORD_10(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  321. #define EFX_POPULATE_QWORD_8(qword, ...) \
  322. EFX_POPULATE_QWORD_9(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  323. #define EFX_POPULATE_QWORD_7(qword, ...) \
  324. EFX_POPULATE_QWORD_8(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  325. #define EFX_POPULATE_QWORD_6(qword, ...) \
  326. EFX_POPULATE_QWORD_7(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  327. #define EFX_POPULATE_QWORD_5(qword, ...) \
  328. EFX_POPULATE_QWORD_6(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  329. #define EFX_POPULATE_QWORD_4(qword, ...) \
  330. EFX_POPULATE_QWORD_5(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  331. #define EFX_POPULATE_QWORD_3(qword, ...) \
  332. EFX_POPULATE_QWORD_4(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  333. #define EFX_POPULATE_QWORD_2(qword, ...) \
  334. EFX_POPULATE_QWORD_3(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  335. #define EFX_POPULATE_QWORD_1(qword, ...) \
  336. EFX_POPULATE_QWORD_2(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  337. #define EFX_ZERO_QWORD(qword) \
  338. EFX_POPULATE_QWORD_1(qword, EFX_DUMMY_FIELD, 0)
  339. #define EFX_SET_QWORD(qword) \
  340. EFX_POPULATE_QWORD_2(qword, \
  341. EFX_DWORD_0, 0xffffffff, \
  342. EFX_DWORD_1, 0xffffffff)
  343. /* Populate a dword field with various numbers of arguments */
  344. #define EFX_POPULATE_DWORD_10 EFX_POPULATE_DWORD
  345. #define EFX_POPULATE_DWORD_9(dword, ...) \
  346. EFX_POPULATE_DWORD_10(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  347. #define EFX_POPULATE_DWORD_8(dword, ...) \
  348. EFX_POPULATE_DWORD_9(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  349. #define EFX_POPULATE_DWORD_7(dword, ...) \
  350. EFX_POPULATE_DWORD_8(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  351. #define EFX_POPULATE_DWORD_6(dword, ...) \
  352. EFX_POPULATE_DWORD_7(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  353. #define EFX_POPULATE_DWORD_5(dword, ...) \
  354. EFX_POPULATE_DWORD_6(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  355. #define EFX_POPULATE_DWORD_4(dword, ...) \
  356. EFX_POPULATE_DWORD_5(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  357. #define EFX_POPULATE_DWORD_3(dword, ...) \
  358. EFX_POPULATE_DWORD_4(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  359. #define EFX_POPULATE_DWORD_2(dword, ...) \
  360. EFX_POPULATE_DWORD_3(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  361. #define EFX_POPULATE_DWORD_1(dword, ...) \
  362. EFX_POPULATE_DWORD_2(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
  363. #define EFX_ZERO_DWORD(dword) \
  364. EFX_POPULATE_DWORD_1(dword, EFX_DUMMY_FIELD, 0)
  365. #define EFX_SET_DWORD(dword) \
  366. EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 0xffffffff)
  367. /*
  368. * Modify a named field within an already-populated structure. Used
  369. * for read-modify-write operations.
  370. *
  371. */
  372. #define EFX_INVERT_OWORD(oword) do { \
  373. (oword).u64[0] = ~((oword).u64[0]); \
  374. (oword).u64[1] = ~((oword).u64[1]); \
  375. } while (0)
  376. #define EFX_AND_OWORD(oword, from, mask) \
  377. do { \
  378. (oword).u64[0] = (from).u64[0] & (mask).u64[0]; \
  379. (oword).u64[1] = (from).u64[1] & (mask).u64[1]; \
  380. } while (0)
  381. #define EFX_AND_QWORD(qword, from, mask) \
  382. (qword).u64[0] = (from).u64[0] & (mask).u64[0]
  383. #define EFX_OR_OWORD(oword, from, mask) \
  384. do { \
  385. (oword).u64[0] = (from).u64[0] | (mask).u64[0]; \
  386. (oword).u64[1] = (from).u64[1] | (mask).u64[1]; \
  387. } while (0)
  388. #define EFX_INSERT64(min, max, low, high, value) \
  389. cpu_to_le64(EFX_INSERT_NATIVE(min, max, low, high, value))
  390. #define EFX_INSERT32(min, max, low, high, value) \
  391. cpu_to_le32(EFX_INSERT_NATIVE(min, max, low, high, value))
  392. #define EFX_INPLACE_MASK64(min, max, low, high) \
  393. EFX_INSERT64(min, max, low, high, EFX_MASK64((high) + 1 - (low)))
  394. #define EFX_INPLACE_MASK32(min, max, low, high) \
  395. EFX_INSERT32(min, max, low, high, EFX_MASK32((high) + 1 - (low)))
  396. #define EFX_SET_OWORD64(oword, low, high, value) do { \
  397. (oword).u64[0] = (((oword).u64[0] \
  398. & ~EFX_INPLACE_MASK64(0, 63, low, high)) \
  399. | EFX_INSERT64(0, 63, low, high, value)); \
  400. (oword).u64[1] = (((oword).u64[1] \
  401. & ~EFX_INPLACE_MASK64(64, 127, low, high)) \
  402. | EFX_INSERT64(64, 127, low, high, value)); \
  403. } while (0)
  404. #define EFX_SET_QWORD64(qword, low, high, value) do { \
  405. (qword).u64[0] = (((qword).u64[0] \
  406. & ~EFX_INPLACE_MASK64(0, 63, low, high)) \
  407. | EFX_INSERT64(0, 63, low, high, value)); \
  408. } while (0)
  409. #define EFX_SET_OWORD32(oword, low, high, value) do { \
  410. (oword).u32[0] = (((oword).u32[0] \
  411. & ~EFX_INPLACE_MASK32(0, 31, low, high)) \
  412. | EFX_INSERT32(0, 31, low, high, value)); \
  413. (oword).u32[1] = (((oword).u32[1] \
  414. & ~EFX_INPLACE_MASK32(32, 63, low, high)) \
  415. | EFX_INSERT32(32, 63, low, high, value)); \
  416. (oword).u32[2] = (((oword).u32[2] \
  417. & ~EFX_INPLACE_MASK32(64, 95, low, high)) \
  418. | EFX_INSERT32(64, 95, low, high, value)); \
  419. (oword).u32[3] = (((oword).u32[3] \
  420. & ~EFX_INPLACE_MASK32(96, 127, low, high)) \
  421. | EFX_INSERT32(96, 127, low, high, value)); \
  422. } while (0)
  423. #define EFX_SET_QWORD32(qword, low, high, value) do { \
  424. (qword).u32[0] = (((qword).u32[0] \
  425. & ~EFX_INPLACE_MASK32(0, 31, low, high)) \
  426. | EFX_INSERT32(0, 31, low, high, value)); \
  427. (qword).u32[1] = (((qword).u32[1] \
  428. & ~EFX_INPLACE_MASK32(32, 63, low, high)) \
  429. | EFX_INSERT32(32, 63, low, high, value)); \
  430. } while (0)
  431. #define EFX_SET_DWORD32(dword, low, high, value) do { \
  432. (dword).u32[0] = (((dword).u32[0] \
  433. & ~EFX_INPLACE_MASK32(0, 31, low, high)) \
  434. | EFX_INSERT32(0, 31, low, high, value)); \
  435. } while (0)
  436. #define EFX_SET_OWORD_FIELD64(oword, field, value) \
  437. EFX_SET_OWORD64(oword, EFX_LOW_BIT(field), \
  438. EFX_HIGH_BIT(field), value)
  439. #define EFX_SET_QWORD_FIELD64(qword, field, value) \
  440. EFX_SET_QWORD64(qword, EFX_LOW_BIT(field), \
  441. EFX_HIGH_BIT(field), value)
  442. #define EFX_SET_OWORD_FIELD32(oword, field, value) \
  443. EFX_SET_OWORD32(oword, EFX_LOW_BIT(field), \
  444. EFX_HIGH_BIT(field), value)
  445. #define EFX_SET_QWORD_FIELD32(qword, field, value) \
  446. EFX_SET_QWORD32(qword, EFX_LOW_BIT(field), \
  447. EFX_HIGH_BIT(field), value)
  448. #define EFX_SET_DWORD_FIELD(dword, field, value) \
  449. EFX_SET_DWORD32(dword, EFX_LOW_BIT(field), \
  450. EFX_HIGH_BIT(field), value)
  451. #if BITS_PER_LONG == 64
  452. #define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD64
  453. #define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD64
  454. #else
  455. #define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD32
  456. #define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD32
  457. #endif
  458. /* Used to avoid compiler warnings about shift range exceeding width
  459. * of the data types when dma_addr_t is only 32 bits wide.
  460. */
  461. #define DMA_ADDR_T_WIDTH (8 * sizeof(dma_addr_t))
  462. #define EFX_DMA_TYPE_WIDTH(width) \
  463. (((width) < DMA_ADDR_T_WIDTH) ? (width) : DMA_ADDR_T_WIDTH)
  464. /* Static initialiser */
  465. #define EFX_OWORD32(a, b, c, d) \
  466. { .u32 = { cpu_to_le32(a), cpu_to_le32(b), \
  467. cpu_to_le32(c), cpu_to_le32(d) } }
  468. #endif /* EFX_BITFIELD_H */