2 * drivers/net/ethernet/mellanox/mlxsw/item.h
3 * Copyright (c) 2015-2017 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2015-2017 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
39 #include <linux/types.h>
40 #include <linux/string.h>
41 #include <linux/bitops.h>
44 unsigned short offset
; /* bytes in container */
45 short step
; /* step in bytes for indexed items */
46 unsigned short in_step_offset
; /* offset within one step */
47 unsigned char shift
; /* shift in bits */
48 unsigned char element_size
; /* size of element in bit array */
57 static inline unsigned int
58 __mlxsw_item_offset(const struct mlxsw_item
*item
, unsigned short index
,
61 BUG_ON(index
&& !item
->step
);
62 if (item
->offset
% typesize
!= 0 ||
63 item
->step
% typesize
!= 0 ||
64 item
->in_step_offset
% typesize
!= 0) {
65 pr_err("mlxsw: item bug (name=%s,offset=%x,step=%x,in_step_offset=%x,typesize=%zx)\n",
66 item
->name
, item
->offset
, item
->step
,
67 item
->in_step_offset
, typesize
);
71 return ((item
->offset
+ item
->step
* index
+ item
->in_step_offset
) /
75 static inline u8
__mlxsw_item_get8(const char *buf
,
76 const struct mlxsw_item
*item
,
79 unsigned int offset
= __mlxsw_item_offset(item
, index
, sizeof(u8
));
85 tmp
&= GENMASK(item
->size
.bits
- 1, 0);
86 if (item
->no_real_shift
)
91 static inline void __mlxsw_item_set8(char *buf
, const struct mlxsw_item
*item
,
92 unsigned short index
, u8 val
)
94 unsigned int offset
= __mlxsw_item_offset(item
, index
,
97 u8 mask
= GENMASK(item
->size
.bits
- 1, 0) << item
->shift
;
100 if (!item
->no_real_shift
)
109 static inline u16
__mlxsw_item_get16(const char *buf
,
110 const struct mlxsw_item
*item
,
111 unsigned short index
)
113 unsigned int offset
= __mlxsw_item_offset(item
, index
, sizeof(u16
));
114 __be16
*b
= (__be16
*) buf
;
117 tmp
= be16_to_cpu(b
[offset
]);
119 tmp
&= GENMASK(item
->size
.bits
- 1, 0);
120 if (item
->no_real_shift
)
125 static inline void __mlxsw_item_set16(char *buf
, const struct mlxsw_item
*item
,
126 unsigned short index
, u16 val
)
128 unsigned int offset
= __mlxsw_item_offset(item
, index
,
130 __be16
*b
= (__be16
*) buf
;
131 u16 mask
= GENMASK(item
->size
.bits
- 1, 0) << item
->shift
;
134 if (!item
->no_real_shift
)
137 tmp
= be16_to_cpu(b
[offset
]);
140 b
[offset
] = cpu_to_be16(tmp
);
143 static inline u32
__mlxsw_item_get32(const char *buf
,
144 const struct mlxsw_item
*item
,
145 unsigned short index
)
147 unsigned int offset
= __mlxsw_item_offset(item
, index
, sizeof(u32
));
148 __be32
*b
= (__be32
*) buf
;
151 tmp
= be32_to_cpu(b
[offset
]);
153 tmp
&= GENMASK(item
->size
.bits
- 1, 0);
154 if (item
->no_real_shift
)
159 static inline void __mlxsw_item_set32(char *buf
, const struct mlxsw_item
*item
,
160 unsigned short index
, u32 val
)
162 unsigned int offset
= __mlxsw_item_offset(item
, index
,
164 __be32
*b
= (__be32
*) buf
;
165 u32 mask
= GENMASK(item
->size
.bits
- 1, 0) << item
->shift
;
168 if (!item
->no_real_shift
)
171 tmp
= be32_to_cpu(b
[offset
]);
174 b
[offset
] = cpu_to_be32(tmp
);
177 static inline u64
__mlxsw_item_get64(const char *buf
,
178 const struct mlxsw_item
*item
,
179 unsigned short index
)
181 unsigned int offset
= __mlxsw_item_offset(item
, index
, sizeof(u64
));
182 __be64
*b
= (__be64
*) buf
;
185 tmp
= be64_to_cpu(b
[offset
]);
187 tmp
&= GENMASK_ULL(item
->size
.bits
- 1, 0);
188 if (item
->no_real_shift
)
193 static inline void __mlxsw_item_set64(char *buf
, const struct mlxsw_item
*item
,
194 unsigned short index
, u64 val
)
196 unsigned int offset
= __mlxsw_item_offset(item
, index
, sizeof(u64
));
197 __be64
*b
= (__be64
*) buf
;
198 u64 mask
= GENMASK_ULL(item
->size
.bits
- 1, 0) << item
->shift
;
201 if (!item
->no_real_shift
)
204 tmp
= be64_to_cpu(b
[offset
]);
207 b
[offset
] = cpu_to_be64(tmp
);
210 static inline void __mlxsw_item_memcpy_from(const char *buf
, char *dst
,
211 const struct mlxsw_item
*item
,
212 unsigned short index
)
214 unsigned int offset
= __mlxsw_item_offset(item
, index
, sizeof(char));
216 memcpy(dst
, &buf
[offset
], item
->size
.bytes
);
219 static inline void __mlxsw_item_memcpy_to(char *buf
, const char *src
,
220 const struct mlxsw_item
*item
,
221 unsigned short index
)
223 unsigned int offset
= __mlxsw_item_offset(item
, index
, sizeof(char));
225 memcpy(&buf
[offset
], src
, item
->size
.bytes
);
228 static inline char *__mlxsw_item_data(char *buf
, const struct mlxsw_item
*item
,
229 unsigned short index
)
231 unsigned int offset
= __mlxsw_item_offset(item
, index
, sizeof(char));
237 __mlxsw_item_bit_array_offset(const struct mlxsw_item
*item
,
238 u16 index
, u8
*shift
)
240 u16 max_index
, be_index
;
241 u16 offset
; /* byte offset inside the array */
244 BUG_ON(index
&& !item
->element_size
);
245 if (item
->offset
% sizeof(u32
) != 0 ||
246 BITS_PER_BYTE
% item
->element_size
!= 0) {
247 pr_err("mlxsw: item bug (name=%s,offset=%x,element_size=%x)\n",
248 item
->name
, item
->offset
, item
->element_size
);
252 max_index
= (item
->size
.bytes
<< 3) / item
->element_size
- 1;
253 be_index
= max_index
- index
;
254 offset
= be_index
* item
->element_size
>> 3;
255 in_byte_index
= index
% (BITS_PER_BYTE
/ item
->element_size
);
256 *shift
= in_byte_index
* item
->element_size
;
258 return item
->offset
+ offset
;
261 static inline u8
__mlxsw_item_bit_array_get(const char *buf
,
262 const struct mlxsw_item
*item
,
266 u16 offset
= __mlxsw_item_bit_array_offset(item
, index
, &shift
);
270 tmp
&= GENMASK(item
->element_size
- 1, 0);
274 static inline void __mlxsw_item_bit_array_set(char *buf
,
275 const struct mlxsw_item
*item
,
279 u16 offset
= __mlxsw_item_bit_array_offset(item
, index
, &shift
);
280 u8 mask
= GENMASK(item
->element_size
- 1, 0) << shift
;
290 #define __ITEM_NAME(_type, _cname, _iname) \
291 mlxsw_##_type##_##_cname##_##_iname##_item
293 /* _type: cmd_mbox, reg, etc.
294 * _cname: containter name (e.g. command name, register name)
295 * _iname: item name within the container
298 #define MLXSW_ITEM8(_type, _cname, _iname, _offset, _shift, _sizebits) \
299 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
302 .size = {.bits = _sizebits,}, \
303 .name = #_type "_" #_cname "_" #_iname, \
305 static inline u8 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
307 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
309 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u8 val)\
311 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
314 #define MLXSW_ITEM8_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
315 _step, _instepoffset, _norealshift) \
316 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
319 .in_step_offset = _instepoffset, \
321 .no_real_shift = _norealshift, \
322 .size = {.bits = _sizebits,}, \
323 .name = #_type "_" #_cname "_" #_iname, \
326 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
328 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), \
332 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
335 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), \
339 #define MLXSW_ITEM16(_type, _cname, _iname, _offset, _shift, _sizebits) \
340 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
343 .size = {.bits = _sizebits,}, \
344 .name = #_type "_" #_cname "_" #_iname, \
346 static inline u16 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
348 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
350 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 val)\
352 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
355 #define MLXSW_ITEM16_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
356 _step, _instepoffset, _norealshift) \
357 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
360 .in_step_offset = _instepoffset, \
362 .no_real_shift = _norealshift, \
363 .size = {.bits = _sizebits,}, \
364 .name = #_type "_" #_cname "_" #_iname, \
367 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
369 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), \
373 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
376 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), \
380 #define MLXSW_ITEM32(_type, _cname, _iname, _offset, _shift, _sizebits) \
381 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
384 .size = {.bits = _sizebits,}, \
385 .name = #_type "_" #_cname "_" #_iname, \
387 static inline u32 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
389 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
391 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u32 val)\
393 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
396 #define MLXSW_ITEM32_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
397 _step, _instepoffset, _norealshift) \
398 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
401 .in_step_offset = _instepoffset, \
403 .no_real_shift = _norealshift, \
404 .size = {.bits = _sizebits,}, \
405 .name = #_type "_" #_cname "_" #_iname, \
408 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
410 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), \
414 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
417 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), \
421 #define MLXSW_ITEM64(_type, _cname, _iname, _offset, _shift, _sizebits) \
422 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
425 .size = {.bits = _sizebits,}, \
426 .name = #_type "_" #_cname "_" #_iname, \
428 static inline u64 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
430 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
432 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u64 val)\
434 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
437 #define MLXSW_ITEM64_INDEXED(_type, _cname, _iname, _offset, _shift, \
438 _sizebits, _step, _instepoffset, _norealshift) \
439 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
442 .in_step_offset = _instepoffset, \
444 .no_real_shift = _norealshift, \
445 .size = {.bits = _sizebits,}, \
446 .name = #_type "_" #_cname "_" #_iname, \
449 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
451 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), \
455 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
458 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), \
462 #define MLXSW_ITEM_BUF(_type, _cname, _iname, _offset, _sizebytes) \
463 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
465 .size = {.bytes = _sizebytes,}, \
466 .name = #_type "_" #_cname "_" #_iname, \
469 mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, char *dst) \
471 __mlxsw_item_memcpy_from(buf, dst, \
472 &__ITEM_NAME(_type, _cname, _iname), 0); \
475 mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, const char *src) \
477 __mlxsw_item_memcpy_to(buf, src, \
478 &__ITEM_NAME(_type, _cname, _iname), 0); \
480 static inline char * \
481 mlxsw_##_type##_##_cname##_##_iname##_data(char *buf) \
483 return __mlxsw_item_data(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
486 #define MLXSW_ITEM_BUF_INDEXED(_type, _cname, _iname, _offset, _sizebytes, \
487 _step, _instepoffset) \
488 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
491 .in_step_offset = _instepoffset, \
492 .size = {.bytes = _sizebytes,}, \
493 .name = #_type "_" #_cname "_" #_iname, \
496 mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, \
497 unsigned short index, \
500 __mlxsw_item_memcpy_from(buf, dst, \
501 &__ITEM_NAME(_type, _cname, _iname), index); \
504 mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, \
505 unsigned short index, \
508 __mlxsw_item_memcpy_to(buf, src, \
509 &__ITEM_NAME(_type, _cname, _iname), index); \
511 static inline char * \
512 mlxsw_##_type##_##_cname##_##_iname##_data(char *buf, unsigned short index) \
514 return __mlxsw_item_data(buf, \
515 &__ITEM_NAME(_type, _cname, _iname), index); \
518 #define MLXSW_ITEM_BIT_ARRAY(_type, _cname, _iname, _offset, _sizebytes, \
520 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
522 .element_size = _element_size, \
523 .size = {.bytes = _sizebytes,}, \
524 .name = #_type "_" #_cname "_" #_iname, \
527 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, u16 index) \
529 return __mlxsw_item_bit_array_get(buf, \
530 &__ITEM_NAME(_type, _cname, _iname), \
534 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 index, u8 val) \
536 return __mlxsw_item_bit_array_set(buf, \
537 &__ITEM_NAME(_type, _cname, _iname), \