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 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
275 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
277 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u8 val)\
279 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
282 #define MLXSW_ITEM8_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
283 _step, _instepoffset, _norealshift) \
284 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
287 .in_step_offset = _instepoffset, \
289 .no_real_shift = _norealshift, \
290 .size = {.bits = _sizebits,}, \
291 .name = #_type "_" #_cname "_" #_iname, \
294 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
296 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), \
300 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
303 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), \
307 #define MLXSW_ITEM16(_type, _cname, _iname, _offset, _shift, _sizebits) \
308 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
311 .size = {.bits = _sizebits,}, \
312 .name = #_type "_" #_cname "_" #_iname, \
314 static inline u16 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
316 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
318 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 val)\
320 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
323 #define MLXSW_ITEM16_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
324 _step, _instepoffset, _norealshift) \
325 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
328 .in_step_offset = _instepoffset, \
330 .no_real_shift = _norealshift, \
331 .size = {.bits = _sizebits,}, \
332 .name = #_type "_" #_cname "_" #_iname, \
335 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
337 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), \
341 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
344 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), \
348 #define MLXSW_ITEM32(_type, _cname, _iname, _offset, _shift, _sizebits) \
349 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
352 .size = {.bits = _sizebits,}, \
353 .name = #_type "_" #_cname "_" #_iname, \
355 static inline u32 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
357 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
359 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u32 val)\
361 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
364 #define MLXSW_ITEM32_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
365 _step, _instepoffset, _norealshift) \
366 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
369 .in_step_offset = _instepoffset, \
371 .no_real_shift = _norealshift, \
372 .size = {.bits = _sizebits,}, \
373 .name = #_type "_" #_cname "_" #_iname, \
376 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
378 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), \
382 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
385 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), \
389 #define MLXSW_ITEM64(_type, _cname, _iname, _offset, _shift, _sizebits) \
390 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
393 .size = {.bits = _sizebits,}, \
394 .name = #_type "_" #_cname "_" #_iname, \
396 static inline u64 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
398 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
400 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u64 val)\
402 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
405 #define MLXSW_ITEM64_INDEXED(_type, _cname, _iname, _offset, _shift, \
406 _sizebits, _step, _instepoffset, _norealshift) \
407 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
410 .in_step_offset = _instepoffset, \
412 .no_real_shift = _norealshift, \
413 .size = {.bits = _sizebits,}, \
414 .name = #_type "_" #_cname "_" #_iname, \
417 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
419 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), \
423 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
426 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), \
430 #define MLXSW_ITEM_BUF(_type, _cname, _iname, _offset, _sizebytes) \
431 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
433 .size = {.bytes = _sizebytes,}, \
434 .name = #_type "_" #_cname "_" #_iname, \
437 mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, char *dst) \
439 __mlxsw_item_memcpy_from(buf, dst, \
440 &__ITEM_NAME(_type, _cname, _iname), 0); \
443 mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, const char *src) \
445 __mlxsw_item_memcpy_to(buf, src, \
446 &__ITEM_NAME(_type, _cname, _iname), 0); \
448 static inline char * \
449 mlxsw_##_type##_##_cname##_##_iname##_data(char *buf) \
451 return __mlxsw_item_data(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
454 #define MLXSW_ITEM_BUF_INDEXED(_type, _cname, _iname, _offset, _sizebytes, \
455 _step, _instepoffset) \
456 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
459 .in_step_offset = _instepoffset, \
460 .size = {.bytes = _sizebytes,}, \
461 .name = #_type "_" #_cname "_" #_iname, \
464 mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, \
465 unsigned short index, \
468 __mlxsw_item_memcpy_from(buf, dst, \
469 &__ITEM_NAME(_type, _cname, _iname), index); \
472 mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, \
473 unsigned short index, \
476 __mlxsw_item_memcpy_to(buf, src, \
477 &__ITEM_NAME(_type, _cname, _iname), index); \
479 static inline char * \
480 mlxsw_##_type##_##_cname##_##_iname##_data(char *buf, unsigned short index) \
482 return __mlxsw_item_data(buf, \
483 &__ITEM_NAME(_type, _cname, _iname), index); \
486 #define MLXSW_ITEM_BIT_ARRAY(_type, _cname, _iname, _offset, _sizebytes, \
488 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
490 .element_size = _element_size, \
491 .size = {.bytes = _sizebytes,}, \
492 .name = #_type "_" #_cname "_" #_iname, \
495 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, u16 index) \
497 return __mlxsw_item_bit_array_get(buf, \
498 &__ITEM_NAME(_type, _cname, _iname), \
502 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 index, u8 val) \
504 return __mlxsw_item_bit_array_set(buf, \
505 &__ITEM_NAME(_type, _cname, _iname), \