item.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
  2. /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
  3. #ifndef _MLXSW_ITEM_H
  4. #define _MLXSW_ITEM_H
  5. #include <linux/types.h>
  6. #include <linux/string.h>
  7. #include <linux/bitops.h>
  8. struct mlxsw_item {
  9. unsigned short offset; /* bytes in container */
  10. short step; /* step in bytes for indexed items */
  11. unsigned short in_step_offset; /* offset within one step */
  12. unsigned char shift; /* shift in bits */
  13. unsigned char element_size; /* size of element in bit array */
  14. bool no_real_shift;
  15. union {
  16. unsigned char bits;
  17. unsigned short bytes;
  18. } size;
  19. const char *name;
  20. };
  21. static inline unsigned int
  22. __mlxsw_item_offset(const struct mlxsw_item *item, unsigned short index,
  23. size_t typesize)
  24. {
  25. BUG_ON(index && !item->step);
  26. if (item->offset % typesize != 0 ||
  27. item->step % typesize != 0 ||
  28. item->in_step_offset % typesize != 0) {
  29. pr_err("mlxsw: item bug (name=%s,offset=%x,step=%x,in_step_offset=%x,typesize=%zx)\n",
  30. item->name, item->offset, item->step,
  31. item->in_step_offset, typesize);
  32. BUG();
  33. }
  34. return ((item->offset + item->step * index + item->in_step_offset) /
  35. typesize);
  36. }
  37. static inline u8 __mlxsw_item_get8(const char *buf,
  38. const struct mlxsw_item *item,
  39. unsigned short index)
  40. {
  41. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u8));
  42. u8 *b = (u8 *) buf;
  43. u8 tmp;
  44. tmp = b[offset];
  45. tmp >>= item->shift;
  46. tmp &= GENMASK(item->size.bits - 1, 0);
  47. if (item->no_real_shift)
  48. tmp <<= item->shift;
  49. return tmp;
  50. }
  51. static inline void __mlxsw_item_set8(char *buf, const struct mlxsw_item *item,
  52. unsigned short index, u8 val)
  53. {
  54. unsigned int offset = __mlxsw_item_offset(item, index,
  55. sizeof(u8));
  56. u8 *b = (u8 *) buf;
  57. u8 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
  58. u8 tmp;
  59. if (!item->no_real_shift)
  60. val <<= item->shift;
  61. val &= mask;
  62. tmp = b[offset];
  63. tmp &= ~mask;
  64. tmp |= val;
  65. b[offset] = tmp;
  66. }
  67. static inline u16 __mlxsw_item_get16(const char *buf,
  68. const struct mlxsw_item *item,
  69. unsigned short index)
  70. {
  71. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u16));
  72. __be16 *b = (__be16 *) buf;
  73. u16 tmp;
  74. tmp = be16_to_cpu(b[offset]);
  75. tmp >>= item->shift;
  76. tmp &= GENMASK(item->size.bits - 1, 0);
  77. if (item->no_real_shift)
  78. tmp <<= item->shift;
  79. return tmp;
  80. }
  81. static inline void __mlxsw_item_set16(char *buf, const struct mlxsw_item *item,
  82. unsigned short index, u16 val)
  83. {
  84. unsigned int offset = __mlxsw_item_offset(item, index,
  85. sizeof(u16));
  86. __be16 *b = (__be16 *) buf;
  87. u16 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
  88. u16 tmp;
  89. if (!item->no_real_shift)
  90. val <<= item->shift;
  91. val &= mask;
  92. tmp = be16_to_cpu(b[offset]);
  93. tmp &= ~mask;
  94. tmp |= val;
  95. b[offset] = cpu_to_be16(tmp);
  96. }
  97. static inline u32 __mlxsw_item_get32(const char *buf,
  98. const struct mlxsw_item *item,
  99. unsigned short index)
  100. {
  101. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u32));
  102. __be32 *b = (__be32 *) buf;
  103. u32 tmp;
  104. tmp = be32_to_cpu(b[offset]);
  105. tmp >>= item->shift;
  106. tmp &= GENMASK(item->size.bits - 1, 0);
  107. if (item->no_real_shift)
  108. tmp <<= item->shift;
  109. return tmp;
  110. }
  111. static inline void __mlxsw_item_set32(char *buf, const struct mlxsw_item *item,
  112. unsigned short index, u32 val)
  113. {
  114. unsigned int offset = __mlxsw_item_offset(item, index,
  115. sizeof(u32));
  116. __be32 *b = (__be32 *) buf;
  117. u32 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
  118. u32 tmp;
  119. if (!item->no_real_shift)
  120. val <<= item->shift;
  121. val &= mask;
  122. tmp = be32_to_cpu(b[offset]);
  123. tmp &= ~mask;
  124. tmp |= val;
  125. b[offset] = cpu_to_be32(tmp);
  126. }
  127. static inline u64 __mlxsw_item_get64(const char *buf,
  128. const struct mlxsw_item *item,
  129. unsigned short index)
  130. {
  131. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u64));
  132. __be64 *b = (__be64 *) buf;
  133. u64 tmp;
  134. tmp = be64_to_cpu(b[offset]);
  135. tmp >>= item->shift;
  136. tmp &= GENMASK_ULL(item->size.bits - 1, 0);
  137. if (item->no_real_shift)
  138. tmp <<= item->shift;
  139. return tmp;
  140. }
  141. static inline void __mlxsw_item_set64(char *buf, const struct mlxsw_item *item,
  142. unsigned short index, u64 val)
  143. {
  144. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u64));
  145. __be64 *b = (__be64 *) buf;
  146. u64 mask = GENMASK_ULL(item->size.bits - 1, 0) << item->shift;
  147. u64 tmp;
  148. if (!item->no_real_shift)
  149. val <<= item->shift;
  150. val &= mask;
  151. tmp = be64_to_cpu(b[offset]);
  152. tmp &= ~mask;
  153. tmp |= val;
  154. b[offset] = cpu_to_be64(tmp);
  155. }
  156. static inline void __mlxsw_item_memcpy_from(const char *buf, char *dst,
  157. const struct mlxsw_item *item,
  158. unsigned short index)
  159. {
  160. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
  161. memcpy(dst, &buf[offset], item->size.bytes);
  162. }
  163. static inline void __mlxsw_item_memcpy_to(char *buf, const char *src,
  164. const struct mlxsw_item *item,
  165. unsigned short index)
  166. {
  167. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
  168. memcpy(&buf[offset], src, item->size.bytes);
  169. }
  170. static inline char *__mlxsw_item_data(char *buf, const struct mlxsw_item *item,
  171. unsigned short index)
  172. {
  173. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
  174. return &buf[offset];
  175. }
  176. static inline u16
  177. __mlxsw_item_bit_array_offset(const struct mlxsw_item *item,
  178. u16 index, u8 *shift)
  179. {
  180. u16 max_index, be_index;
  181. u16 offset; /* byte offset inside the array */
  182. u8 in_byte_index;
  183. BUG_ON(index && !item->element_size);
  184. if (item->offset % sizeof(u32) != 0 ||
  185. BITS_PER_BYTE % item->element_size != 0) {
  186. pr_err("mlxsw: item bug (name=%s,offset=%x,element_size=%x)\n",
  187. item->name, item->offset, item->element_size);
  188. BUG();
  189. }
  190. max_index = (item->size.bytes << 3) / item->element_size - 1;
  191. be_index = max_index - index;
  192. offset = be_index * item->element_size >> 3;
  193. in_byte_index = index % (BITS_PER_BYTE / item->element_size);
  194. *shift = in_byte_index * item->element_size;
  195. return item->offset + offset;
  196. }
  197. static inline u8 __mlxsw_item_bit_array_get(const char *buf,
  198. const struct mlxsw_item *item,
  199. u16 index)
  200. {
  201. u8 shift, tmp;
  202. u16 offset = __mlxsw_item_bit_array_offset(item, index, &shift);
  203. tmp = buf[offset];
  204. tmp >>= shift;
  205. tmp &= GENMASK(item->element_size - 1, 0);
  206. return tmp;
  207. }
  208. static inline void __mlxsw_item_bit_array_set(char *buf,
  209. const struct mlxsw_item *item,
  210. u16 index, u8 val)
  211. {
  212. u8 shift, tmp;
  213. u16 offset = __mlxsw_item_bit_array_offset(item, index, &shift);
  214. u8 mask = GENMASK(item->element_size - 1, 0) << shift;
  215. val <<= shift;
  216. val &= mask;
  217. tmp = buf[offset];
  218. tmp &= ~mask;
  219. tmp |= val;
  220. buf[offset] = tmp;
  221. }
  222. #define __ITEM_NAME(_type, _cname, _iname) \
  223. mlxsw_##_type##_##_cname##_##_iname##_item
  224. /* _type: cmd_mbox, reg, etc.
  225. * _cname: containter name (e.g. command name, register name)
  226. * _iname: item name within the container
  227. */
  228. #define MLXSW_ITEM8(_type, _cname, _iname, _offset, _shift, _sizebits) \
  229. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  230. .offset = _offset, \
  231. .shift = _shift, \
  232. .size = {.bits = _sizebits,}, \
  233. .name = #_type "_" #_cname "_" #_iname, \
  234. }; \
  235. static inline u8 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
  236. { \
  237. return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
  238. } \
  239. static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u8 val)\
  240. { \
  241. __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
  242. }
  243. #define MLXSW_ITEM8_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
  244. _step, _instepoffset, _norealshift) \
  245. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  246. .offset = _offset, \
  247. .step = _step, \
  248. .in_step_offset = _instepoffset, \
  249. .shift = _shift, \
  250. .no_real_shift = _norealshift, \
  251. .size = {.bits = _sizebits,}, \
  252. .name = #_type "_" #_cname "_" #_iname, \
  253. }; \
  254. static inline u8 \
  255. mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
  256. { \
  257. return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), \
  258. index); \
  259. } \
  260. static inline void \
  261. mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
  262. u8 val) \
  263. { \
  264. __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), \
  265. index, val); \
  266. }
  267. #define MLXSW_ITEM16(_type, _cname, _iname, _offset, _shift, _sizebits) \
  268. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  269. .offset = _offset, \
  270. .shift = _shift, \
  271. .size = {.bits = _sizebits,}, \
  272. .name = #_type "_" #_cname "_" #_iname, \
  273. }; \
  274. static inline u16 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
  275. { \
  276. return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
  277. } \
  278. static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 val)\
  279. { \
  280. __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
  281. }
  282. #define MLXSW_ITEM16_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
  283. _step, _instepoffset, _norealshift) \
  284. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  285. .offset = _offset, \
  286. .step = _step, \
  287. .in_step_offset = _instepoffset, \
  288. .shift = _shift, \
  289. .no_real_shift = _norealshift, \
  290. .size = {.bits = _sizebits,}, \
  291. .name = #_type "_" #_cname "_" #_iname, \
  292. }; \
  293. static inline u16 \
  294. mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
  295. { \
  296. return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), \
  297. index); \
  298. } \
  299. static inline void \
  300. mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
  301. u16 val) \
  302. { \
  303. __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), \
  304. index, val); \
  305. }
  306. #define MLXSW_ITEM32(_type, _cname, _iname, _offset, _shift, _sizebits) \
  307. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  308. .offset = _offset, \
  309. .shift = _shift, \
  310. .size = {.bits = _sizebits,}, \
  311. .name = #_type "_" #_cname "_" #_iname, \
  312. }; \
  313. static inline u32 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
  314. { \
  315. return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
  316. } \
  317. static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u32 val)\
  318. { \
  319. __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
  320. }
  321. #define MLXSW_ITEM32_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
  322. _step, _instepoffset, _norealshift) \
  323. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  324. .offset = _offset, \
  325. .step = _step, \
  326. .in_step_offset = _instepoffset, \
  327. .shift = _shift, \
  328. .no_real_shift = _norealshift, \
  329. .size = {.bits = _sizebits,}, \
  330. .name = #_type "_" #_cname "_" #_iname, \
  331. }; \
  332. static inline u32 \
  333. mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
  334. { \
  335. return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), \
  336. index); \
  337. } \
  338. static inline void \
  339. mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
  340. u32 val) \
  341. { \
  342. __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), \
  343. index, val); \
  344. }
  345. #define MLXSW_ITEM64(_type, _cname, _iname, _offset, _shift, _sizebits) \
  346. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  347. .offset = _offset, \
  348. .shift = _shift, \
  349. .size = {.bits = _sizebits,}, \
  350. .name = #_type "_" #_cname "_" #_iname, \
  351. }; \
  352. static inline u64 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
  353. { \
  354. return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
  355. } \
  356. static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u64 val)\
  357. { \
  358. __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
  359. }
  360. #define MLXSW_ITEM64_INDEXED(_type, _cname, _iname, _offset, _shift, \
  361. _sizebits, _step, _instepoffset, _norealshift) \
  362. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  363. .offset = _offset, \
  364. .step = _step, \
  365. .in_step_offset = _instepoffset, \
  366. .shift = _shift, \
  367. .no_real_shift = _norealshift, \
  368. .size = {.bits = _sizebits,}, \
  369. .name = #_type "_" #_cname "_" #_iname, \
  370. }; \
  371. static inline u64 \
  372. mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
  373. { \
  374. return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), \
  375. index); \
  376. } \
  377. static inline void \
  378. mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
  379. u64 val) \
  380. { \
  381. __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), \
  382. index, val); \
  383. }
  384. #define MLXSW_ITEM_BUF(_type, _cname, _iname, _offset, _sizebytes) \
  385. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  386. .offset = _offset, \
  387. .size = {.bytes = _sizebytes,}, \
  388. .name = #_type "_" #_cname "_" #_iname, \
  389. }; \
  390. static inline void \
  391. mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, char *dst) \
  392. { \
  393. __mlxsw_item_memcpy_from(buf, dst, \
  394. &__ITEM_NAME(_type, _cname, _iname), 0); \
  395. } \
  396. static inline void \
  397. mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, const char *src) \
  398. { \
  399. __mlxsw_item_memcpy_to(buf, src, \
  400. &__ITEM_NAME(_type, _cname, _iname), 0); \
  401. } \
  402. static inline char * \
  403. mlxsw_##_type##_##_cname##_##_iname##_data(char *buf) \
  404. { \
  405. return __mlxsw_item_data(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
  406. }
  407. #define MLXSW_ITEM_BUF_INDEXED(_type, _cname, _iname, _offset, _sizebytes, \
  408. _step, _instepoffset) \
  409. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  410. .offset = _offset, \
  411. .step = _step, \
  412. .in_step_offset = _instepoffset, \
  413. .size = {.bytes = _sizebytes,}, \
  414. .name = #_type "_" #_cname "_" #_iname, \
  415. }; \
  416. static inline void \
  417. mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, \
  418. unsigned short index, \
  419. char *dst) \
  420. { \
  421. __mlxsw_item_memcpy_from(buf, dst, \
  422. &__ITEM_NAME(_type, _cname, _iname), index); \
  423. } \
  424. static inline void \
  425. mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, \
  426. unsigned short index, \
  427. const char *src) \
  428. { \
  429. __mlxsw_item_memcpy_to(buf, src, \
  430. &__ITEM_NAME(_type, _cname, _iname), index); \
  431. } \
  432. static inline char * \
  433. mlxsw_##_type##_##_cname##_##_iname##_data(char *buf, unsigned short index) \
  434. { \
  435. return __mlxsw_item_data(buf, \
  436. &__ITEM_NAME(_type, _cname, _iname), index); \
  437. }
  438. #define MLXSW_ITEM_BIT_ARRAY(_type, _cname, _iname, _offset, _sizebytes, \
  439. _element_size) \
  440. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  441. .offset = _offset, \
  442. .element_size = _element_size, \
  443. .size = {.bytes = _sizebytes,}, \
  444. .name = #_type "_" #_cname "_" #_iname, \
  445. }; \
  446. static inline u8 \
  447. mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, u16 index) \
  448. { \
  449. return __mlxsw_item_bit_array_get(buf, \
  450. &__ITEM_NAME(_type, _cname, _iname), \
  451. index); \
  452. } \
  453. static inline void \
  454. mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 index, u8 val) \
  455. { \
  456. return __mlxsw_item_bit_array_set(buf, \
  457. &__ITEM_NAME(_type, _cname, _iname), \
  458. index, val); \
  459. } \
  460. #endif