]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/ethernet/mellanox/mlxsw/item.h
09f35de0a371097bc6b6305a75a972706c7a5e96
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 unsigned 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
);
229 __mlxsw_item_bit_array_offset(const struct mlxsw_item
*item
,
230 u16 index
, u8
*shift
)
232 u16 max_index
, be_index
;
233 u16 offset
; /* byte offset inside the array */
236 BUG_ON(index
&& !item
->element_size
);
237 if (item
->offset
% sizeof(u32
) != 0 ||
238 BITS_PER_BYTE
% item
->element_size
!= 0) {
239 pr_err("mlxsw: item bug (name=%s,offset=%x,element_size=%x)\n",
240 item
->name
, item
->offset
, item
->element_size
);
244 max_index
= (item
->size
.bytes
<< 3) / item
->element_size
- 1;
245 be_index
= max_index
- index
;
246 offset
= be_index
* item
->element_size
>> 3;
247 in_byte_index
= index
% (BITS_PER_BYTE
/ item
->element_size
);
248 *shift
= in_byte_index
* item
->element_size
;
250 return item
->offset
+ offset
;
253 static inline u8
__mlxsw_item_bit_array_get(const char *buf
,
254 const struct mlxsw_item
*item
,
258 u16 offset
= __mlxsw_item_bit_array_offset(item
, index
, &shift
);
262 tmp
&= GENMASK(item
->element_size
- 1, 0);
266 static inline void __mlxsw_item_bit_array_set(char *buf
,
267 const struct mlxsw_item
*item
,
271 u16 offset
= __mlxsw_item_bit_array_offset(item
, index
, &shift
);
272 u8 mask
= GENMASK(item
->element_size
- 1, 0) << shift
;
282 #define __ITEM_NAME(_type, _cname, _iname) \
283 mlxsw_##_type##_##_cname##_##_iname##_item
285 /* _type: cmd_mbox, reg, etc.
286 * _cname: containter name (e.g. command name, register name)
287 * _iname: item name within the container
290 #define MLXSW_ITEM8(_type, _cname, _iname, _offset, _shift, _sizebits) \
291 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
294 .size = {.bits = _sizebits,}, \
295 .name = #_type "_" #_cname "_" #_iname, \
297 static inline u8 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
299 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
301 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u8 val)\
303 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
306 #define MLXSW_ITEM8_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
307 _step, _instepoffset, _norealshift) \
308 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
311 .in_step_offset = _instepoffset, \
313 .no_real_shift = _norealshift, \
314 .size = {.bits = _sizebits,}, \
315 .name = #_type "_" #_cname "_" #_iname, \
318 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
320 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), \
324 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
327 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), \
331 #define MLXSW_ITEM16(_type, _cname, _iname, _offset, _shift, _sizebits) \
332 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
335 .size = {.bits = _sizebits,}, \
336 .name = #_type "_" #_cname "_" #_iname, \
338 static inline u16 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
340 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
342 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 val)\
344 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
347 #define MLXSW_ITEM16_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
348 _step, _instepoffset, _norealshift) \
349 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
352 .in_step_offset = _instepoffset, \
354 .no_real_shift = _norealshift, \
355 .size = {.bits = _sizebits,}, \
356 .name = #_type "_" #_cname "_" #_iname, \
359 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
361 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), \
365 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
368 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), \
372 #define MLXSW_ITEM32(_type, _cname, _iname, _offset, _shift, _sizebits) \
373 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
376 .size = {.bits = _sizebits,}, \
377 .name = #_type "_" #_cname "_" #_iname, \
379 static inline u32 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
381 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
383 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u32 val)\
385 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
388 #define MLXSW_ITEM32_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
389 _step, _instepoffset, _norealshift) \
390 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
393 .in_step_offset = _instepoffset, \
395 .no_real_shift = _norealshift, \
396 .size = {.bits = _sizebits,}, \
397 .name = #_type "_" #_cname "_" #_iname, \
400 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
402 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), \
406 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
409 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), \
413 #define MLXSW_ITEM64(_type, _cname, _iname, _offset, _shift, _sizebits) \
414 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
417 .size = {.bits = _sizebits,}, \
418 .name = #_type "_" #_cname "_" #_iname, \
420 static inline u64 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
422 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
424 static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u64 val)\
426 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
429 #define MLXSW_ITEM64_INDEXED(_type, _cname, _iname, _offset, _shift, \
430 _sizebits, _step, _instepoffset, _norealshift) \
431 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
434 .in_step_offset = _instepoffset, \
436 .no_real_shift = _norealshift, \
437 .size = {.bits = _sizebits,}, \
438 .name = #_type "_" #_cname "_" #_iname, \
441 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
443 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), \
447 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
450 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), \
454 #define MLXSW_ITEM_BUF(_type, _cname, _iname, _offset, _sizebytes) \
455 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
457 .size = {.bytes = _sizebytes,}, \
458 .name = #_type "_" #_cname "_" #_iname, \
461 mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, char *dst) \
463 __mlxsw_item_memcpy_from(buf, dst, \
464 &__ITEM_NAME(_type, _cname, _iname), 0); \
467 mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, const char *src) \
469 __mlxsw_item_memcpy_to(buf, src, \
470 &__ITEM_NAME(_type, _cname, _iname), 0); \
473 #define MLXSW_ITEM_BUF_INDEXED(_type, _cname, _iname, _offset, _sizebytes, \
474 _step, _instepoffset) \
475 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
478 .in_step_offset = _instepoffset, \
479 .size = {.bytes = _sizebytes,}, \
480 .name = #_type "_" #_cname "_" #_iname, \
483 mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, \
484 unsigned short index, \
487 __mlxsw_item_memcpy_from(buf, dst, \
488 &__ITEM_NAME(_type, _cname, _iname), index); \
491 mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, \
492 unsigned short index, \
495 __mlxsw_item_memcpy_to(buf, src, \
496 &__ITEM_NAME(_type, _cname, _iname), index); \
499 #define MLXSW_ITEM_BIT_ARRAY(_type, _cname, _iname, _offset, _sizebytes, \
501 static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
503 .element_size = _element_size, \
504 .size = {.bytes = _sizebytes,}, \
505 .name = #_type "_" #_cname "_" #_iname, \
508 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, u16 index) \
510 return __mlxsw_item_bit_array_get(buf, \
511 &__ITEM_NAME(_type, _cname, _iname), \
515 mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 index, u8 val) \
517 return __mlxsw_item_bit_array_set(buf, \
518 &__ITEM_NAME(_type, _cname, _iname), \