pvrusb2: reduce stack usage pvr2_eeprom_analyze()
[linux/fpc-iii.git] / drivers / net / ethernet / rocker / rocker_tlv.h
bloba63ef82e7c72d079bb831ab433c2c43af173d2c0
1 /*
2 * drivers/net/ethernet/rocker/rocker_tlv.h - Rocker switch device driver
3 * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
4 * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #ifndef _ROCKER_TLV_H
13 #define _ROCKER_TLV_H
15 #include <linux/types.h>
17 #include "rocker_hw.h"
18 #include "rocker.h"
20 #define ROCKER_TLV_ALIGNTO 8U
21 #define ROCKER_TLV_ALIGN(len) \
22 (((len) + ROCKER_TLV_ALIGNTO - 1) & ~(ROCKER_TLV_ALIGNTO - 1))
23 #define ROCKER_TLV_HDRLEN ROCKER_TLV_ALIGN(sizeof(struct rocker_tlv))
25 /* <------- ROCKER_TLV_HDRLEN -------> <--- ROCKER_TLV_ALIGN(payload) --->
26 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
27 * | Header | Pad | Payload | Pad |
28 * | (struct rocker_tlv) | ing | | ing |
29 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
30 * <--------------------------- tlv->len -------------------------->
33 static inline struct rocker_tlv *rocker_tlv_next(const struct rocker_tlv *tlv,
34 int *remaining)
36 int totlen = ROCKER_TLV_ALIGN(tlv->len);
38 *remaining -= totlen;
39 return (struct rocker_tlv *) ((char *) tlv + totlen);
42 static inline int rocker_tlv_ok(const struct rocker_tlv *tlv, int remaining)
44 return remaining >= (int) ROCKER_TLV_HDRLEN &&
45 tlv->len >= ROCKER_TLV_HDRLEN &&
46 tlv->len <= remaining;
49 #define rocker_tlv_for_each(pos, head, len, rem) \
50 for (pos = head, rem = len; \
51 rocker_tlv_ok(pos, rem); \
52 pos = rocker_tlv_next(pos, &(rem)))
54 #define rocker_tlv_for_each_nested(pos, tlv, rem) \
55 rocker_tlv_for_each(pos, rocker_tlv_data(tlv), \
56 rocker_tlv_len(tlv), rem)
58 static inline int rocker_tlv_attr_size(int payload)
60 return ROCKER_TLV_HDRLEN + payload;
63 static inline int rocker_tlv_total_size(int payload)
65 return ROCKER_TLV_ALIGN(rocker_tlv_attr_size(payload));
68 static inline int rocker_tlv_padlen(int payload)
70 return rocker_tlv_total_size(payload) - rocker_tlv_attr_size(payload);
73 static inline int rocker_tlv_type(const struct rocker_tlv *tlv)
75 return tlv->type;
78 static inline void *rocker_tlv_data(const struct rocker_tlv *tlv)
80 return (char *) tlv + ROCKER_TLV_HDRLEN;
83 static inline int rocker_tlv_len(const struct rocker_tlv *tlv)
85 return tlv->len - ROCKER_TLV_HDRLEN;
88 static inline u8 rocker_tlv_get_u8(const struct rocker_tlv *tlv)
90 return *(u8 *) rocker_tlv_data(tlv);
93 static inline u16 rocker_tlv_get_u16(const struct rocker_tlv *tlv)
95 return *(u16 *) rocker_tlv_data(tlv);
98 static inline __be16 rocker_tlv_get_be16(const struct rocker_tlv *tlv)
100 return *(__be16 *) rocker_tlv_data(tlv);
103 static inline u32 rocker_tlv_get_u32(const struct rocker_tlv *tlv)
105 return *(u32 *) rocker_tlv_data(tlv);
108 static inline u64 rocker_tlv_get_u64(const struct rocker_tlv *tlv)
110 return *(u64 *) rocker_tlv_data(tlv);
113 void rocker_tlv_parse(const struct rocker_tlv **tb, int maxtype,
114 const char *buf, int buf_len);
116 static inline void rocker_tlv_parse_nested(const struct rocker_tlv **tb,
117 int maxtype,
118 const struct rocker_tlv *tlv)
120 rocker_tlv_parse(tb, maxtype, rocker_tlv_data(tlv),
121 rocker_tlv_len(tlv));
124 static inline void
125 rocker_tlv_parse_desc(const struct rocker_tlv **tb, int maxtype,
126 const struct rocker_desc_info *desc_info)
128 rocker_tlv_parse(tb, maxtype, desc_info->data,
129 desc_info->desc->tlv_size);
132 static inline struct rocker_tlv *
133 rocker_tlv_start(struct rocker_desc_info *desc_info)
135 return (struct rocker_tlv *) ((char *) desc_info->data +
136 desc_info->tlv_size);
139 int rocker_tlv_put(struct rocker_desc_info *desc_info,
140 int attrtype, int attrlen, const void *data);
142 static inline int rocker_tlv_put_u8(struct rocker_desc_info *desc_info,
143 int attrtype, u8 value)
145 return rocker_tlv_put(desc_info, attrtype, sizeof(u8), &value);
148 static inline int rocker_tlv_put_u16(struct rocker_desc_info *desc_info,
149 int attrtype, u16 value)
151 return rocker_tlv_put(desc_info, attrtype, sizeof(u16), &value);
154 static inline int rocker_tlv_put_be16(struct rocker_desc_info *desc_info,
155 int attrtype, __be16 value)
157 return rocker_tlv_put(desc_info, attrtype, sizeof(__be16), &value);
160 static inline int rocker_tlv_put_u32(struct rocker_desc_info *desc_info,
161 int attrtype, u32 value)
163 return rocker_tlv_put(desc_info, attrtype, sizeof(u32), &value);
166 static inline int rocker_tlv_put_be32(struct rocker_desc_info *desc_info,
167 int attrtype, __be32 value)
169 return rocker_tlv_put(desc_info, attrtype, sizeof(__be32), &value);
172 static inline int rocker_tlv_put_u64(struct rocker_desc_info *desc_info,
173 int attrtype, u64 value)
175 return rocker_tlv_put(desc_info, attrtype, sizeof(u64), &value);
178 static inline struct rocker_tlv *
179 rocker_tlv_nest_start(struct rocker_desc_info *desc_info, int attrtype)
181 struct rocker_tlv *start = rocker_tlv_start(desc_info);
183 if (rocker_tlv_put(desc_info, attrtype, 0, NULL) < 0)
184 return NULL;
186 return start;
189 static inline void rocker_tlv_nest_end(struct rocker_desc_info *desc_info,
190 struct rocker_tlv *start)
192 start->len = (char *) rocker_tlv_start(desc_info) - (char *) start;
195 static inline void rocker_tlv_nest_cancel(struct rocker_desc_info *desc_info,
196 const struct rocker_tlv *start)
198 desc_info->tlv_size = (const char *) start - desc_info->data;
201 #endif