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.
15 #include <linux/types.h>
17 #include "rocker_hw.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,
36 int totlen = ROCKER_TLV_ALIGN(tlv->len);
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)
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,
118 const struct rocker_tlv *tlv)
120 rocker_tlv_parse(tb, maxtype, rocker_tlv_data(tlv),
121 rocker_tlv_len(tlv));
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);
143 rocker_tlv_put_u8(struct rocker_desc_info *desc_info, int attrtype, u8 value)
145 u8 tmp = value; /* work around GCC PR81715 */
147 return rocker_tlv_put(desc_info, attrtype, sizeof(u8), &tmp);
151 rocker_tlv_put_u16(struct rocker_desc_info *desc_info, int attrtype, u16 value)
155 return rocker_tlv_put(desc_info, attrtype, sizeof(u16), &tmp);
159 rocker_tlv_put_be16(struct rocker_desc_info *desc_info, int attrtype, __be16 value)
163 return rocker_tlv_put(desc_info, attrtype, sizeof(__be16), &tmp);
167 rocker_tlv_put_u32(struct rocker_desc_info *desc_info, int attrtype, u32 value)
171 return rocker_tlv_put(desc_info, attrtype, sizeof(u32), &tmp);
175 rocker_tlv_put_be32(struct rocker_desc_info *desc_info, int attrtype, __be32 value)
179 return rocker_tlv_put(desc_info, attrtype, sizeof(__be32), &tmp);
183 rocker_tlv_put_u64(struct rocker_desc_info *desc_info, int attrtype, u64 value)
187 return rocker_tlv_put(desc_info, attrtype, sizeof(u64), &tmp);
190 static inline struct rocker_tlv *
191 rocker_tlv_nest_start(struct rocker_desc_info *desc_info, int attrtype)
193 struct rocker_tlv *start = rocker_tlv_start(desc_info);
195 if (rocker_tlv_put(desc_info, attrtype, 0, NULL) < 0)
201 static inline void rocker_tlv_nest_end(struct rocker_desc_info *desc_info,
202 struct rocker_tlv *start)
204 start->len = (char *) rocker_tlv_start(desc_info) - (char *) start;
207 static inline void rocker_tlv_nest_cancel(struct rocker_desc_info *desc_info,
208 const struct rocker_tlv *start)
210 desc_info->tlv_size = (const char *) start - desc_info->data;