1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#ifndef _MLXSW_ITEM_H
37#define _MLXSW_ITEM_H
38
39#include <linux/types.h>
40#include <linux/string.h>
41#include <linux/bitops.h>
42
43struct mlxsw_item {
44 unsigned short offset;
45 short step;
46 unsigned short in_step_offset;
47 unsigned char shift;
48 unsigned char element_size;
49 bool no_real_shift;
50 union {
51 unsigned char bits;
52 unsigned short bytes;
53 } size;
54 const char *name;
55};
56
57static inline unsigned int
58__mlxsw_item_offset(const struct mlxsw_item *item, unsigned short index,
59 size_t typesize)
60{
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);
68 BUG();
69 }
70
71 return ((item->offset + item->step * index + item->in_step_offset) /
72 typesize);
73}
74
75static inline u8 __mlxsw_item_get8(const char *buf,
76 const struct mlxsw_item *item,
77 unsigned short index)
78{
79 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u8));
80 u8 *b = (u8 *) buf;
81 u8 tmp;
82
83 tmp = b[offset];
84 tmp >>= item->shift;
85 tmp &= GENMASK(item->size.bits - 1, 0);
86 if (item->no_real_shift)
87 tmp <<= item->shift;
88 return tmp;
89}
90
91static inline void __mlxsw_item_set8(char *buf, const struct mlxsw_item *item,
92 unsigned short index, u8 val)
93{
94 unsigned int offset = __mlxsw_item_offset(item, index,
95 sizeof(u8));
96 u8 *b = (u8 *) buf;
97 u8 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
98 u8 tmp;
99
100 if (!item->no_real_shift)
101 val <<= item->shift;
102 val &= mask;
103 tmp = b[offset];
104 tmp &= ~mask;
105 tmp |= val;
106 b[offset] = tmp;
107}
108
109static inline u16 __mlxsw_item_get16(const char *buf,
110 const struct mlxsw_item *item,
111 unsigned short index)
112{
113 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u16));
114 __be16 *b = (__be16 *) buf;
115 u16 tmp;
116
117 tmp = be16_to_cpu(b[offset]);
118 tmp >>= item->shift;
119 tmp &= GENMASK(item->size.bits - 1, 0);
120 if (item->no_real_shift)
121 tmp <<= item->shift;
122 return tmp;
123}
124
125static inline void __mlxsw_item_set16(char *buf, const struct mlxsw_item *item,
126 unsigned short index, u16 val)
127{
128 unsigned int offset = __mlxsw_item_offset(item, index,
129 sizeof(u16));
130 __be16 *b = (__be16 *) buf;
131 u16 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
132 u16 tmp;
133
134 if (!item->no_real_shift)
135 val <<= item->shift;
136 val &= mask;
137 tmp = be16_to_cpu(b[offset]);
138 tmp &= ~mask;
139 tmp |= val;
140 b[offset] = cpu_to_be16(tmp);
141}
142
143static inline u32 __mlxsw_item_get32(const char *buf,
144 const struct mlxsw_item *item,
145 unsigned short index)
146{
147 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u32));
148 __be32 *b = (__be32 *) buf;
149 u32 tmp;
150
151 tmp = be32_to_cpu(b[offset]);
152 tmp >>= item->shift;
153 tmp &= GENMASK(item->size.bits - 1, 0);
154 if (item->no_real_shift)
155 tmp <<= item->shift;
156 return tmp;
157}
158
159static inline void __mlxsw_item_set32(char *buf, const struct mlxsw_item *item,
160 unsigned short index, u32 val)
161{
162 unsigned int offset = __mlxsw_item_offset(item, index,
163 sizeof(u32));
164 __be32 *b = (__be32 *) buf;
165 u32 mask = GENMASK(item->size.bits - 1, 0) << item->shift;
166 u32 tmp;
167
168 if (!item->no_real_shift)
169 val <<= item->shift;
170 val &= mask;
171 tmp = be32_to_cpu(b[offset]);
172 tmp &= ~mask;
173 tmp |= val;
174 b[offset] = cpu_to_be32(tmp);
175}
176
177static inline u64 __mlxsw_item_get64(const char *buf,
178 const struct mlxsw_item *item,
179 unsigned short index)
180{
181 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(u64));
182 __be64 *b = (__be64 *) buf;
183 u64 tmp;
184
185 tmp = be64_to_cpu(b[offset]);
186 tmp >>= item->shift;
187 tmp &= GENMASK_ULL(item->size.bits - 1, 0);
188 if (item->no_real_shift)
189 tmp <<= item->shift;
190 return tmp;
191}
192
193static inline void __mlxsw_item_set64(char *buf, const struct mlxsw_item *item,
194 unsigned short index, u64 val)
195{
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;
199 u64 tmp;
200
201 if (!item->no_real_shift)
202 val <<= item->shift;
203 val &= mask;
204 tmp = be64_to_cpu(b[offset]);
205 tmp &= ~mask;
206 tmp |= val;
207 b[offset] = cpu_to_be64(tmp);
208}
209
210static inline void __mlxsw_item_memcpy_from(const char *buf, char *dst,
211 const struct mlxsw_item *item,
212 unsigned short index)
213{
214 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
215
216 memcpy(dst, &buf[offset], item->size.bytes);
217}
218
219static inline void __mlxsw_item_memcpy_to(char *buf, const char *src,
220 const struct mlxsw_item *item,
221 unsigned short index)
222{
223 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
224
225 memcpy(&buf[offset], src, item->size.bytes);
226}
227
228static inline char *__mlxsw_item_data(char *buf, const struct mlxsw_item *item,
229 unsigned short index)
230{
231 unsigned int offset = __mlxsw_item_offset(item, index, sizeof(char));
232
233 return &buf[offset];
234}
235
236static inline u16
237__mlxsw_item_bit_array_offset(const struct mlxsw_item *item,
238 u16 index, u8 *shift)
239{
240 u16 max_index, be_index;
241 u16 offset;
242 u8 in_byte_index;
243
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);
249 BUG();
250 }
251
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;
257
258 return item->offset + offset;
259}
260
261static inline u8 __mlxsw_item_bit_array_get(const char *buf,
262 const struct mlxsw_item *item,
263 u16 index)
264{
265 u8 shift, tmp;
266 u16 offset = __mlxsw_item_bit_array_offset(item, index, &shift);
267
268 tmp = buf[offset];
269 tmp >>= shift;
270 tmp &= GENMASK(item->element_size - 1, 0);
271 return tmp;
272}
273
274static inline void __mlxsw_item_bit_array_set(char *buf,
275 const struct mlxsw_item *item,
276 u16 index, u8 val)
277{
278 u8 shift, tmp;
279 u16 offset = __mlxsw_item_bit_array_offset(item, index, &shift);
280 u8 mask = GENMASK(item->element_size - 1, 0) << shift;
281
282 val <<= shift;
283 val &= mask;
284 tmp = buf[offset];
285 tmp &= ~mask;
286 tmp |= val;
287 buf[offset] = tmp;
288}
289
290#define __ITEM_NAME(_type, _cname, _iname) \
291 mlxsw_##_type##_##_cname##_##_iname##_item
292
293
294
295
296
297
298#define MLXSW_ITEM8(_type, _cname, _iname, _offset, _shift, _sizebits) \
299static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
300 .offset = _offset, \
301 .shift = _shift, \
302 .size = {.bits = _sizebits,}, \
303 .name = #_type "_" #_cname "_" #_iname, \
304}; \
305static inline u8 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
306{ \
307 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
308} \
309static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u8 val)\
310{ \
311 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
312}
313
314#define MLXSW_ITEM8_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
315 _step, _instepoffset, _norealshift) \
316static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
317 .offset = _offset, \
318 .step = _step, \
319 .in_step_offset = _instepoffset, \
320 .shift = _shift, \
321 .no_real_shift = _norealshift, \
322 .size = {.bits = _sizebits,}, \
323 .name = #_type "_" #_cname "_" #_iname, \
324}; \
325static inline u8 \
326mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
327{ \
328 return __mlxsw_item_get8(buf, &__ITEM_NAME(_type, _cname, _iname), \
329 index); \
330} \
331static inline void \
332mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
333 u8 val) \
334{ \
335 __mlxsw_item_set8(buf, &__ITEM_NAME(_type, _cname, _iname), \
336 index, val); \
337}
338
339#define MLXSW_ITEM16(_type, _cname, _iname, _offset, _shift, _sizebits) \
340static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
341 .offset = _offset, \
342 .shift = _shift, \
343 .size = {.bits = _sizebits,}, \
344 .name = #_type "_" #_cname "_" #_iname, \
345}; \
346static inline u16 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
347{ \
348 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
349} \
350static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 val)\
351{ \
352 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
353}
354
355#define MLXSW_ITEM16_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
356 _step, _instepoffset, _norealshift) \
357static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
358 .offset = _offset, \
359 .step = _step, \
360 .in_step_offset = _instepoffset, \
361 .shift = _shift, \
362 .no_real_shift = _norealshift, \
363 .size = {.bits = _sizebits,}, \
364 .name = #_type "_" #_cname "_" #_iname, \
365}; \
366static inline u16 \
367mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
368{ \
369 return __mlxsw_item_get16(buf, &__ITEM_NAME(_type, _cname, _iname), \
370 index); \
371} \
372static inline void \
373mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
374 u16 val) \
375{ \
376 __mlxsw_item_set16(buf, &__ITEM_NAME(_type, _cname, _iname), \
377 index, val); \
378}
379
380#define MLXSW_ITEM32(_type, _cname, _iname, _offset, _shift, _sizebits) \
381static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
382 .offset = _offset, \
383 .shift = _shift, \
384 .size = {.bits = _sizebits,}, \
385 .name = #_type "_" #_cname "_" #_iname, \
386}; \
387static inline u32 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
388{ \
389 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
390} \
391static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u32 val)\
392{ \
393 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
394}
395
396#define MLXSW_ITEM32_INDEXED(_type, _cname, _iname, _offset, _shift, _sizebits, \
397 _step, _instepoffset, _norealshift) \
398static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
399 .offset = _offset, \
400 .step = _step, \
401 .in_step_offset = _instepoffset, \
402 .shift = _shift, \
403 .no_real_shift = _norealshift, \
404 .size = {.bits = _sizebits,}, \
405 .name = #_type "_" #_cname "_" #_iname, \
406}; \
407static inline u32 \
408mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
409{ \
410 return __mlxsw_item_get32(buf, &__ITEM_NAME(_type, _cname, _iname), \
411 index); \
412} \
413static inline void \
414mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
415 u32 val) \
416{ \
417 __mlxsw_item_set32(buf, &__ITEM_NAME(_type, _cname, _iname), \
418 index, val); \
419}
420
421#define MLXSW_ITEM64(_type, _cname, _iname, _offset, _shift, _sizebits) \
422static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
423 .offset = _offset, \
424 .shift = _shift, \
425 .size = {.bits = _sizebits,}, \
426 .name = #_type "_" #_cname "_" #_iname, \
427}; \
428static inline u64 mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf) \
429{ \
430 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
431} \
432static inline void mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u64 val)\
433{ \
434 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), 0, val); \
435}
436
437#define MLXSW_ITEM64_INDEXED(_type, _cname, _iname, _offset, _shift, \
438 _sizebits, _step, _instepoffset, _norealshift) \
439static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
440 .offset = _offset, \
441 .step = _step, \
442 .in_step_offset = _instepoffset, \
443 .shift = _shift, \
444 .no_real_shift = _norealshift, \
445 .size = {.bits = _sizebits,}, \
446 .name = #_type "_" #_cname "_" #_iname, \
447}; \
448static inline u64 \
449mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, unsigned short index)\
450{ \
451 return __mlxsw_item_get64(buf, &__ITEM_NAME(_type, _cname, _iname), \
452 index); \
453} \
454static inline void \
455mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, unsigned short index, \
456 u64 val) \
457{ \
458 __mlxsw_item_set64(buf, &__ITEM_NAME(_type, _cname, _iname), \
459 index, val); \
460}
461
462#define MLXSW_ITEM_BUF(_type, _cname, _iname, _offset, _sizebytes) \
463static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
464 .offset = _offset, \
465 .size = {.bytes = _sizebytes,}, \
466 .name = #_type "_" #_cname "_" #_iname, \
467}; \
468static inline void \
469mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, char *dst) \
470{ \
471 __mlxsw_item_memcpy_from(buf, dst, \
472 &__ITEM_NAME(_type, _cname, _iname), 0); \
473} \
474static inline void \
475mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, const char *src) \
476{ \
477 __mlxsw_item_memcpy_to(buf, src, \
478 &__ITEM_NAME(_type, _cname, _iname), 0); \
479} \
480static inline char * \
481mlxsw_##_type##_##_cname##_##_iname##_data(char *buf) \
482{ \
483 return __mlxsw_item_data(buf, &__ITEM_NAME(_type, _cname, _iname), 0); \
484}
485
486#define MLXSW_ITEM_BUF_INDEXED(_type, _cname, _iname, _offset, _sizebytes, \
487 _step, _instepoffset) \
488static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
489 .offset = _offset, \
490 .step = _step, \
491 .in_step_offset = _instepoffset, \
492 .size = {.bytes = _sizebytes,}, \
493 .name = #_type "_" #_cname "_" #_iname, \
494}; \
495static inline void \
496mlxsw_##_type##_##_cname##_##_iname##_memcpy_from(const char *buf, \
497 unsigned short index, \
498 char *dst) \
499{ \
500 __mlxsw_item_memcpy_from(buf, dst, \
501 &__ITEM_NAME(_type, _cname, _iname), index); \
502} \
503static inline void \
504mlxsw_##_type##_##_cname##_##_iname##_memcpy_to(char *buf, \
505 unsigned short index, \
506 const char *src) \
507{ \
508 __mlxsw_item_memcpy_to(buf, src, \
509 &__ITEM_NAME(_type, _cname, _iname), index); \
510} \
511static inline char * \
512mlxsw_##_type##_##_cname##_##_iname##_data(char *buf, unsigned short index) \
513{ \
514 return __mlxsw_item_data(buf, \
515 &__ITEM_NAME(_type, _cname, _iname), index); \
516}
517
518#define MLXSW_ITEM_BIT_ARRAY(_type, _cname, _iname, _offset, _sizebytes, \
519 _element_size) \
520static struct mlxsw_item __ITEM_NAME(_type, _cname, _iname) = { \
521 .offset = _offset, \
522 .element_size = _element_size, \
523 .size = {.bytes = _sizebytes,}, \
524 .name = #_type "_" #_cname "_" #_iname, \
525}; \
526static inline u8 \
527mlxsw_##_type##_##_cname##_##_iname##_get(const char *buf, u16 index) \
528{ \
529 return __mlxsw_item_bit_array_get(buf, \
530 &__ITEM_NAME(_type, _cname, _iname), \
531 index); \
532} \
533static inline void \
534mlxsw_##_type##_##_cname##_##_iname##_set(char *buf, u16 index, u8 val) \
535{ \
536 return __mlxsw_item_bit_array_set(buf, \
537 &__ITEM_NAME(_type, _cname, _iname), \
538 index, val); \
539} \
540
541#endif
542