1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
7 #include <linux/types.h>
8 #include <linux/string.h>
9 #include <linux/bitops.h>
12 unsigned short offset; /* bytes in container */
13 short step; /* step in bytes for indexed items */
14 unsigned short in_step_offset; /* offset within one step */
15 unsigned char shift; /* shift in bits */
16 unsigned char element_size; /* size of element in bit array */
25 static inline unsigned int
26 __mlxsw_item_offset(const struct mlxsw_item *item, unsigned short index,
29 BUG_ON(index && !item->step);
30 if (item->offset % typesize != 0 ||
31 item->step % typesize != 0 ||
32 item->in_step_offset % typesize != 0) {
33 pr_err("mlxsw: item bug (name=%s,offset=%x,step=%x,in_step_offset=%x,typesize=%zx)\n",
34 item->name, item->offset, item->step,
35 item->in_step_offset, typesize);
39 return ((item->offset + item->step * index + item->in_step_offset) /
43 static inline u8 __mlxsw_item_get8(const char *buf,
44 const struct mlxsw_item *item,
47 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u8));
53 tmp &= GENMASK(item->size.bits - 1, 0);
54 if (item->no_real_shift)
59 static inline void __mlxsw_item_set8(char *buf, const struct mlxsw_item *item,
60 unsigned short index, u8 val)
62 unsigned int offset = __mlxsw_item_offset(item, index,
65 u8 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
68 if (!item->no_real_shift)
77 static inline u16 __mlxsw_item_get16(const char *buf,
78 const struct mlxsw_item *item,
81 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u16));
82 __be16 *b = (__be16 *) buf;
85 tmp = be16_to_cpu(b[offset]);
87 tmp &= GENMASK(item->size.bits - 1, 0);
88 if (item->no_real_shift)
93 static inline void __mlxsw_item_set16(char *buf, const struct mlxsw_item *item,
94 unsigned short index, u16 val)
96 unsigned int offset = __mlxsw_item_offset(item, index,
98 __be16 *b = (__be16 *) buf;
99 u16 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
102 if (!item->no_real_shift)
105 tmp = be16_to_cpu(b[offset]);
108 b[offset] = cpu_to_be16(tmp);
111 static inline u32 __mlxsw_item_get32(const char *buf,
112 const struct mlxsw_item *item,
113 unsigned short index)
115 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u32));
116 __be32 *b = (__be32 *) buf;
119 tmp = be32_to_cpu(b[offset]);
121 tmp &= GENMASK(item->size.bits - 1, 0);
122 if (item->no_real_shift)
127 static inline void __mlxsw_item_set32(char *buf, const struct mlxsw_item *item,
128 unsigned short index, u32 val)
130 unsigned int offset = __mlxsw_item_offset(item, index,
132 __be32 *b = (__be32 *) buf;
133 u32 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
136 if (!item->no_real_shift)
139 tmp = be32_to_cpu(b[offset]);
142 b[offset] = cpu_to_be32(tmp);
145 static inline u64 __mlxsw_item_get64(const char *buf,
146 const struct mlxsw_item *item,
147 unsigned short index)
149 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u64));
150 __be64 *b = (__be64 *) buf;
153 tmp = be64_to_cpu(b[offset]);
155 tmp &= GENMASK_ULL(item->size.bits - 1, 0);
156 if (item->no_real_shift)
161 static inline void __mlxsw_item_set64(char *buf, const struct mlxsw_item *item,
162 unsigned short index, u64 val)
164 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u64));
165 __be64 *b = (__be64 *) buf;
166 u64 mask = GENMASK_ULL(item->size.bits - 1, 0) << item->shift;
169 if (!item->no_real_shift)
172 tmp = be64_to_cpu(b[offset]);
175 b[offset] = cpu_to_be64(tmp);
178 static inline void __mlxsw_item_memcpy_from(const char *buf, char *dst,
179 const struct mlxsw_item *item,
180 unsigned short index)
182 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
184 memcpy(dst, &buf[offset], item->size.bytes);
187 static inline void __mlxsw_item_memcpy_to(char *buf, const char *src,
188 const struct mlxsw_item *item,
189 unsigned short index)
191 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
193 memcpy(&buf[offset], src, item->size.bytes);
196 static inline char *__mlxsw_item_data(char *buf, const struct mlxsw_item *item,
197 unsigned short index)
199 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
205 __mlxsw_item_bit_array_offset(const struct mlxsw_item *item,
206 u16 index, u8 *shift)
208 u16 max_index, be_index;
209 u16 offset; /* byte offset inside the array */
212 BUG_ON(index && !item->element_size);
213 if (item->offset % sizeof(u32) != 0 ||
214 BITS_PER_BYTE % item->element_size != 0) {
215 pr_err("mlxsw: item bug (name=%s,offset=%x,element_size=%x)\n",
216 item->name, item->offset, item->element_size);
220 max_index = (item->size.bytes << 3) / item->element_size - 1;
221 be_index = max_index - index;
222 offset = be_index * item->element_size >> 3;
223 in_byte_index = index % (BITS_PER_BYTE / item->element_size);
224 *shift = in_byte_index * item->element_size;
226 return item->offset + offset;
229 static inline u8 __mlxsw_item_bit_array_get(const char *buf,
230 const struct mlxsw_item *item,
234 u16 offset = __mlxsw_item_bit_array_offset(item, index, &shift);
238 tmp &= GENMASK(item->element_size - 1, 0);
242 static inline void __mlxsw_item_bit_array_set(char *buf,
243 const struct mlxsw_item *item,
247 u16 offset = __mlxsw_item_bit_array_offset(item, index, &shift);
248 u8 mask = GENMASK(item->element_size - 1, 0) << shift;
258 #define __ITEM_NAME(_type, _cname, _iname) \
259 mlxsw_##_type##_##_cname##_##_iname##_item
261 /* _type: cmd_mbox, reg, etc.
262 * _cname: containter name (e.g. command name, register name)
263 * _iname: item name within the container
266 #define MLXSW_ITEM8(_type, _cname, _iname, _offset, _shift, _sizebits) \
267 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
270 .size = {.bits = _sizebits,}, \
271 .name = #_type "_" #_cname "_" #_iname, \
273 static inline u8 __maybe_unused \
274 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
276 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
278 static inline void __maybe_unused \
279 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u8 val) \
281 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
284 #define MLXSW_ITEM8_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
285 _step, _instepoffset, _norealshift) \
286 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
289 .in_step_offset = _instepoffset, \
291 .no_real_shift = _norealshift, \
292 .size = {.bits = _sizebits,}, \
293 .name = #_type "_" #_cname "_" #_iname, \
295 static inline u8 __maybe_unused \
296 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
298 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), \
301 static inline void __maybe_unused \
302 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
305 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), \
309 #define MLXSW_ITEM16(_type, _cname, _iname, _offset, _shift, _sizebits) \
310 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
313 .size = {.bits = _sizebits,}, \
314 .name = #_type "_" #_cname "_" #_iname, \
316 static inline u16 __maybe_unused \
317 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
319 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
321 static inline void __maybe_unused \
322 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 val) \
324 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
327 #define MLXSW_ITEM16_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
328 _step, _instepoffset, _norealshift) \
329 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
332 .in_step_offset = _instepoffset, \
334 .no_real_shift = _norealshift, \
335 .size = {.bits = _sizebits,}, \
336 .name = #_type "_" #_cname "_" #_iname, \
338 static inline u16 __maybe_unused \
339 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
341 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), \
344 static inline void __maybe_unused \
345 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
348 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), \
352 #define MLXSW_ITEM32(_type, _cname, _iname, _offset, _shift, _sizebits) \
353 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
356 .size = {.bits = _sizebits,}, \
357 .name = #_type "_" #_cname "_" #_iname, \
359 static inline u32 __maybe_unused \
360 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
362 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
364 static inline void __maybe_unused \
365 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u32 val) \
367 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
370 #define LOCAL_PORT_LSB_SIZE 8
371 #define LOCAL_PORT_MSB_SIZE 2
373 #define MLXSW_ITEM32_LP(_type, _cname, _offset1, _shift1, _offset2, _shift2) \
374 static struct mlxsw_item __ITEM_NAME(_type, _cname, local_port) = { \
375 .offset = _offset1, \
377 .size = {.bits = LOCAL_PORT_LSB_SIZE,}, \
378 .name = #_type "_" #_cname "_local_port", \
380 static struct mlxsw_item __ITEM_NAME(_type, _cname, lp_msb) = { \
381 .offset = _offset2, \
383 .size = {.bits = LOCAL_PORT_MSB_SIZE,}, \
384 .name = #_type "_" #_cname "_lp_msb", \
386 static inline u32 __maybe_unused \
387 mlxsw_##_type##_##_cname##_local_port_get(const char *buf) \
389 u32 local_port, lp_msb; \
391 local_port = __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, \
393 lp_msb = __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, lp_msb), \
395 return (lp_msb << LOCAL_PORT_LSB_SIZE) + local_port; \
397 static inline void __maybe_unused \
398 mlxsw_##_type##_##_cname##_local_port_set(char *buf, u32 val) \
400 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, local_port), 0, \
401 val & ((1 << LOCAL_PORT_LSB_SIZE) - 1)); \
402 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, lp_msb), 0, \
403 val >> LOCAL_PORT_LSB_SIZE); \
406 #define MLXSW_ITEM32_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
407 _step, _instepoffset, _norealshift) \
408 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
411 .in_step_offset = _instepoffset, \
413 .no_real_shift = _norealshift, \
414 .size = {.bits = _sizebits,}, \
415 .name = #_type "_" #_cname "_" #_iname, \
417 static inline u32 __maybe_unused \
418 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
420 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), \
423 static inline void __maybe_unused \
424 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
427 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), \
431 #define MLXSW_ITEM64(_type, _cname, _iname, _offset, _shift, _sizebits) \
432 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
435 .size = {.bits = _sizebits,}, \
436 .name = #_type "_" #_cname "_" #_iname, \
438 static inline u64 __maybe_unused \
439 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
441 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
443 static inline void __maybe_unused \
444 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u64 val) \
446 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
449 #define MLXSW_ITEM64_INDEXED(_type, _cname, _iname, _offset, _shift, \
450 _sizebits, _step, _instepoffset, _norealshift) \
451 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
454 .in_step_offset = _instepoffset, \
456 .no_real_shift = _norealshift, \
457 .size = {.bits = _sizebits,}, \
458 .name = #_type "_" #_cname "_" #_iname, \
460 static inline u64 __maybe_unused \
461 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
463 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), \
466 static inline void __maybe_unused \
467 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
470 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), \
474 #define MLXSW_ITEM_BUF(_type, _cname, _iname, _offset, _sizebytes) \
475 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
477 .size = {.bytes = _sizebytes,}, \
478 .name = #_type "_" #_cname "_" #_iname, \
480 static inline void __maybe_unused \
481 mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, char *dst) \
483 __mlxsw_item_memcpy_from(buf, dst, \
484 &__ITEM_NAME(_type, _cname, _iname), 0); \
486 static inline void __maybe_unused \
487 mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, const char *src) \
489 __mlxsw_item_memcpy_to(buf, src, \
490 &__ITEM_NAME(_type, _cname, _iname), 0); \
492 static inline char * __maybe_unused \
493 mlxsw_##_type##_##_cname##_##_iname##_data(char *buf) \
495 return __mlxsw_item_data(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
498 #define MLXSW_ITEM_BUF_INDEXED(_type, _cname, _iname, _offset, _sizebytes, \
499 _step, _instepoffset) \
500 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
503 .in_step_offset = _instepoffset, \
504 .size = {.bytes = _sizebytes,}, \
505 .name = #_type "_" #_cname "_" #_iname, \
507 static inline void __maybe_unused \
508 mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, \
509 unsigned short index, \
512 __mlxsw_item_memcpy_from(buf, dst, \
513 &__ITEM_NAME(_type, _cname, _iname), index); \
515 static inline void __maybe_unused \
516 mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, \
517 unsigned short index, \
520 __mlxsw_item_memcpy_to(buf, src, \
521 &__ITEM_NAME(_type, _cname, _iname), index); \
523 static inline char * __maybe_unused \
524 mlxsw_##_type##_##_cname##_##_iname##_data(char *buf, unsigned short index) \
526 return __mlxsw_item_data(buf, \
527 &__ITEM_NAME(_type, _cname, _iname), index); \
530 #define MLXSW_ITEM_BIT_ARRAY(_type, _cname, _iname, _offset, _sizebytes, \
532 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
534 .element_size = _element_size, \
535 .size = {.bytes = _sizebytes,}, \
536 .name = #_type "_" #_cname "_" #_iname, \
538 static inline u8 __maybe_unused \
539 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, u16 index) \
541 return __mlxsw_item_bit_array_get(buf, \
542 &__ITEM_NAME(_type, _cname, _iname), \
545 static inline void __maybe_unused \
546 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 index, u8 val) \
548 return __mlxsw_item_bit_array_set(buf, \
549 &__ITEM_NAME(_type, _cname, _iname), \