item.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxsw/item.h
  3. * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
  5. * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright holders nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * Alternatively, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") version 2 as published by the Free
  21. * Software Foundation.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #ifndef _MLXSW_ITEM_H
  36. #define _MLXSW_ITEM_H
  37. #include <linux/types.h>
  38. #include <linux/string.h>
  39. #include <linux/bitops.h>
  40. struct mlxsw_item {
  41. unsigned short offset; /* bytes in container */
  42. unsigned short step; /* step in bytes for indexed items */
  43. unsigned short in_step_offset; /* offset within one step */
  44. unsigned char shift; /* shift in bits */
  45. unsigned char element_size; /* size of element in bit array */
  46. bool no_real_shift;
  47. union {
  48. unsigned char bits;
  49. unsigned short bytes;
  50. } size;
  51. const char *name;
  52. };
  53. static inline unsigned int
  54. __mlxsw_item_offset(struct mlxsw_item *item, unsigned short index,
  55. size_t typesize)
  56. {
  57. BUG_ON(index && !item->step);
  58. if (item->offset % typesize != 0 ||
  59. item->step % typesize != 0 ||
  60. item->in_step_offset % typesize != 0) {
  61. pr_err("mlxsw: item bug (name=%s,offset=%x,step=%x,in_step_offset=%x,typesize=%zx)\n",
  62. item->name, item->offset, item->step,
  63. item->in_step_offset, typesize);
  64. BUG();
  65. }
  66. return ((item->offset + item->step * index + item->in_step_offset) /
  67. typesize);
  68. }
  69. static inline u16 __mlxsw_item_get16(char *buf, struct mlxsw_item *item,
  70. unsigned short index)
  71. {
  72. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u16));
  73. __be16 *b = (__be16 *) buf;
  74. u16 tmp;
  75. tmp = be16_to_cpu(b[offset]);
  76. tmp >>= item->shift;
  77. tmp &= GENMASK(item->size.bits - 1, 0);
  78. if (item->no_real_shift)
  79. tmp <<= item->shift;
  80. return tmp;
  81. }
  82. static inline void __mlxsw_item_set16(char *buf, struct mlxsw_item *item,
  83. unsigned short index, u16 val)
  84. {
  85. unsigned int offset = __mlxsw_item_offset(item, index,
  86. sizeof(u16));
  87. __be16 *b = (__be16 *) buf;
  88. u16 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
  89. u16 tmp;
  90. if (!item->no_real_shift)
  91. val <<= item->shift;
  92. val &= mask;
  93. tmp = be16_to_cpu(b[offset]);
  94. tmp &= ~mask;
  95. tmp |= val;
  96. b[offset] = cpu_to_be16(tmp);
  97. }
  98. static inline u32 __mlxsw_item_get32(char *buf, 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, 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(char *buf, struct mlxsw_item *item,
  128. unsigned short index)
  129. {
  130. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u64));
  131. __be64 *b = (__be64 *) buf;
  132. u64 tmp;
  133. tmp = be64_to_cpu(b[offset]);
  134. tmp >>= item->shift;
  135. tmp &= GENMASK_ULL(item->size.bits - 1, 0);
  136. if (item->no_real_shift)
  137. tmp <<= item->shift;
  138. return tmp;
  139. }
  140. static inline void __mlxsw_item_set64(char *buf, struct mlxsw_item *item,
  141. unsigned short index, u64 val)
  142. {
  143. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u64));
  144. __be64 *b = (__be64 *) buf;
  145. u64 mask = GENMASK_ULL(item->size.bits - 1, 0) << item->shift;
  146. u64 tmp;
  147. if (!item->no_real_shift)
  148. val <<= item->shift;
  149. val &= mask;
  150. tmp = be64_to_cpu(b[offset]);
  151. tmp &= ~mask;
  152. tmp |= val;
  153. b[offset] = cpu_to_be64(tmp);
  154. }
  155. static inline void __mlxsw_item_memcpy_from(char *buf, char *dst,
  156. struct mlxsw_item *item,
  157. unsigned short index)
  158. {
  159. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
  160. memcpy(dst, &buf[offset], item->size.bytes);
  161. }
  162. static inline void __mlxsw_item_memcpy_to(char *buf, const char *src,
  163. struct mlxsw_item *item,
  164. unsigned short index)
  165. {
  166. unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
  167. memcpy(&buf[offset], src, item->size.bytes);
  168. }
  169. static inline u16
  170. __mlxsw_item_bit_array_offset(struct mlxsw_item *item, u16 index, u8 *shift)
  171. {
  172. u16 max_index, be_index;
  173. u16 offset; /* byte offset inside the array */
  174. u8 in_byte_index;
  175. BUG_ON(index && !item->element_size);
  176. if (item->offset % sizeof(u32) != 0 ||
  177. BITS_PER_BYTE % item->element_size != 0) {
  178. pr_err("mlxsw: item bug (name=%s,offset=%x,element_size=%x)\n",
  179. item->name, item->offset, item->element_size);
  180. BUG();
  181. }
  182. max_index = (item->size.bytes << 3) / item->element_size - 1;
  183. be_index = max_index - index;
  184. offset = be_index * item->element_size >> 3;
  185. in_byte_index = index % (BITS_PER_BYTE / item->element_size);
  186. *shift = in_byte_index * item->element_size;
  187. return item->offset + offset;
  188. }
  189. static inline u8 __mlxsw_item_bit_array_get(char *buf, struct mlxsw_item *item,
  190. u16 index)
  191. {
  192. u8 shift, tmp;
  193. u16 offset = __mlxsw_item_bit_array_offset(item, index, &shift);
  194. tmp = buf[offset];
  195. tmp >>= shift;
  196. tmp &= GENMASK(item->element_size - 1, 0);
  197. return tmp;
  198. }
  199. static inline void __mlxsw_item_bit_array_set(char *buf, struct mlxsw_item *item,
  200. u16 index, u8 val)
  201. {
  202. u8 shift, tmp;
  203. u16 offset = __mlxsw_item_bit_array_offset(item, index, &shift);
  204. u8 mask = GENMASK(item->element_size - 1, 0) << shift;
  205. val <<= shift;
  206. val &= mask;
  207. tmp = buf[offset];
  208. tmp &= ~mask;
  209. tmp |= val;
  210. buf[offset] = tmp;
  211. }
  212. #define __ITEM_NAME(_type, _cname, _iname) \
  213. mlxsw_##_type##_##_cname##_##_iname##_item
  214. /* _type: cmd_mbox, reg, etc.
  215. * _cname: containter name (e.g. command name, register name)
  216. * _iname: item name within the container
  217. */
  218. #define MLXSW_ITEM16(_type, _cname, _iname, _offset, _shift, _sizebits) \
  219. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  220. .offset = _offset, \
  221. .shift = _shift, \
  222. .size = {.bits = _sizebits,}, \
  223. .name = #_type "_" #_cname "_" #_iname, \
  224. }; \
  225. static inline u16 mlxsw_##_type##_##_cname##_##_iname##_get(char *buf) \
  226. { \
  227. return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
  228. } \
  229. static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 val)\
  230. { \
  231. __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
  232. }
  233. #define MLXSW_ITEM16_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
  234. _step, _instepoffset, _norealshift) \
  235. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  236. .offset = _offset, \
  237. .step = _step, \
  238. .in_step_offset = _instepoffset, \
  239. .shift = _shift, \
  240. .no_real_shift = _norealshift, \
  241. .size = {.bits = _sizebits,}, \
  242. .name = #_type "_" #_cname "_" #_iname, \
  243. }; \
  244. static inline u16 \
  245. mlxsw_##_type##_##_cname##_##_iname##_get(char *buf, unsigned short index) \
  246. { \
  247. return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), \
  248. index); \
  249. } \
  250. static inline void \
  251. mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
  252. u16 val) \
  253. { \
  254. __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), \
  255. index, val); \
  256. }
  257. #define MLXSW_ITEM32(_type, _cname, _iname, _offset, _shift, _sizebits) \
  258. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  259. .offset = _offset, \
  260. .shift = _shift, \
  261. .size = {.bits = _sizebits,}, \
  262. .name = #_type "_" #_cname "_" #_iname, \
  263. }; \
  264. static inline u32 mlxsw_##_type##_##_cname##_##_iname##_get(char *buf) \
  265. { \
  266. return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
  267. } \
  268. static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u32 val)\
  269. { \
  270. __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
  271. }
  272. #define MLXSW_ITEM32_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
  273. _step, _instepoffset, _norealshift) \
  274. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  275. .offset = _offset, \
  276. .step = _step, \
  277. .in_step_offset = _instepoffset, \
  278. .shift = _shift, \
  279. .no_real_shift = _norealshift, \
  280. .size = {.bits = _sizebits,}, \
  281. .name = #_type "_" #_cname "_" #_iname, \
  282. }; \
  283. static inline u32 \
  284. mlxsw_##_type##_##_cname##_##_iname##_get(char *buf, unsigned short index) \
  285. { \
  286. return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), \
  287. index); \
  288. } \
  289. static inline void \
  290. mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
  291. u32 val) \
  292. { \
  293. __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), \
  294. index, val); \
  295. }
  296. #define MLXSW_ITEM64(_type, _cname, _iname, _offset, _shift, _sizebits) \
  297. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  298. .offset = _offset, \
  299. .shift = _shift, \
  300. .size = {.bits = _sizebits,}, \
  301. .name = #_type "_" #_cname "_" #_iname, \
  302. }; \
  303. static inline u64 mlxsw_##_type##_##_cname##_##_iname##_get(char *buf) \
  304. { \
  305. return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
  306. } \
  307. static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u64 val)\
  308. { \
  309. __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
  310. }
  311. #define MLXSW_ITEM64_INDEXED(_type, _cname, _iname, _offset, _shift, \
  312. _sizebits, _step, _instepoffset, _norealshift) \
  313. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  314. .offset = _offset, \
  315. .step = _step, \
  316. .in_step_offset = _instepoffset, \
  317. .shift = _shift, \
  318. .no_real_shift = _norealshift, \
  319. .size = {.bits = _sizebits,}, \
  320. .name = #_type "_" #_cname "_" #_iname, \
  321. }; \
  322. static inline u64 \
  323. mlxsw_##_type##_##_cname##_##_iname##_get(char *buf, unsigned short index) \
  324. { \
  325. return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), \
  326. index); \
  327. } \
  328. static inline void \
  329. mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
  330. u64 val) \
  331. { \
  332. __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), \
  333. index, val); \
  334. }
  335. #define MLXSW_ITEM_BUF(_type, _cname, _iname, _offset, _sizebytes) \
  336. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  337. .offset = _offset, \
  338. .size = {.bytes = _sizebytes,}, \
  339. .name = #_type "_" #_cname "_" #_iname, \
  340. }; \
  341. static inline void \
  342. mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(char *buf, char *dst) \
  343. { \
  344. __mlxsw_item_memcpy_from(buf, dst, \
  345. &__ITEM_NAME(_type, _cname, _iname), 0); \
  346. } \
  347. static inline void \
  348. mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, const char *src) \
  349. { \
  350. __mlxsw_item_memcpy_to(buf, src, \
  351. &__ITEM_NAME(_type, _cname, _iname), 0); \
  352. }
  353. #define MLXSW_ITEM_BUF_INDEXED(_type, _cname, _iname, _offset, _sizebytes, \
  354. _step, _instepoffset) \
  355. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  356. .offset = _offset, \
  357. .step = _step, \
  358. .in_step_offset = _instepoffset, \
  359. .size = {.bytes = _sizebytes,}, \
  360. .name = #_type "_" #_cname "_" #_iname, \
  361. }; \
  362. static inline void \
  363. mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(char *buf, \
  364. unsigned short index, \
  365. char *dst) \
  366. { \
  367. __mlxsw_item_memcpy_from(buf, dst, \
  368. &__ITEM_NAME(_type, _cname, _iname), index); \
  369. } \
  370. static inline void \
  371. mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, \
  372. unsigned short index, \
  373. const char *src) \
  374. { \
  375. __mlxsw_item_memcpy_to(buf, src, \
  376. &__ITEM_NAME(_type, _cname, _iname), index); \
  377. }
  378. #define MLXSW_ITEM_BIT_ARRAY(_type, _cname, _iname, _offset, _sizebytes, \
  379. _element_size) \
  380. static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
  381. .offset = _offset, \
  382. .element_size = _element_size, \
  383. .size = {.bytes = _sizebytes,}, \
  384. .name = #_type "_" #_cname "_" #_iname, \
  385. }; \
  386. static inline u8 \
  387. mlxsw_##_type##_##_cname##_##_iname##_get(char *buf, u16 index) \
  388. { \
  389. return __mlxsw_item_bit_array_get(buf, \
  390. &__ITEM_NAME(_type, _cname, _iname), \
  391. index); \
  392. } \
  393. static inline void \
  394. mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 index, u8 val) \
  395. { \
  396. return __mlxsw_item_bit_array_set(buf, \
  397. &__ITEM_NAME(_type, _cname, _iname), \
  398. index, val); \
  399. } \
  400. #endif