bitfield.h 19 KB

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