rocker_tlv.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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>
  5. *
  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.
  10. */
  11. #ifndef _ROCKER_TLV_H
  12. #define _ROCKER_TLV_H
  13. #include <linux/types.h>
  14. #include "rocker_hw.h"
  15. #include "rocker.h"
  16. #define ROCKER_TLV_ALIGNTO 8U
  17. #define ROCKER_TLV_ALIGN(len) \
  18. (((len) + ROCKER_TLV_ALIGNTO - 1) & ~(ROCKER_TLV_ALIGNTO - 1))
  19. #define ROCKER_TLV_HDRLEN ROCKER_TLV_ALIGN(sizeof(struct rocker_tlv))
  20. /* <------- ROCKER_TLV_HDRLEN -------> <--- ROCKER_TLV_ALIGN(payload) --->
  21. * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
  22. * | Header | Pad | Payload | Pad |
  23. * | (struct rocker_tlv) | ing | | ing |
  24. * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
  25. * <--------------------------- tlv->len -------------------------->
  26. */
  27. static inline struct rocker_tlv *rocker_tlv_next(const struct rocker_tlv *tlv,
  28. int *remaining)
  29. {
  30. int totlen = ROCKER_TLV_ALIGN(tlv->len);
  31. *remaining -= totlen;
  32. return (struct rocker_tlv *) ((char *) tlv + totlen);
  33. }
  34. static inline int rocker_tlv_ok(const struct rocker_tlv *tlv, int remaining)
  35. {
  36. return remaining >= (int) ROCKER_TLV_HDRLEN &&
  37. tlv->len >= ROCKER_TLV_HDRLEN &&
  38. tlv->len <= remaining;
  39. }
  40. #define rocker_tlv_for_each(pos, head, len, rem) \
  41. for (pos = head, rem = len; \
  42. rocker_tlv_ok(pos, rem); \
  43. pos = rocker_tlv_next(pos, &(rem)))
  44. #define rocker_tlv_for_each_nested(pos, tlv, rem) \
  45. rocker_tlv_for_each(pos, rocker_tlv_data(tlv), \
  46. rocker_tlv_len(tlv), rem)
  47. static inline int rocker_tlv_attr_size(int payload)
  48. {
  49. return ROCKER_TLV_HDRLEN + payload;
  50. }
  51. static inline int rocker_tlv_total_size(int payload)
  52. {
  53. return ROCKER_TLV_ALIGN(rocker_tlv_attr_size(payload));
  54. }
  55. static inline int rocker_tlv_padlen(int payload)
  56. {
  57. return rocker_tlv_total_size(payload) - rocker_tlv_attr_size(payload);
  58. }
  59. static inline int rocker_tlv_type(const struct rocker_tlv *tlv)
  60. {
  61. return tlv->type;
  62. }
  63. static inline void *rocker_tlv_data(const struct rocker_tlv *tlv)
  64. {
  65. return (char *) tlv + ROCKER_TLV_HDRLEN;
  66. }
  67. static inline int rocker_tlv_len(const struct rocker_tlv *tlv)
  68. {
  69. return tlv->len - ROCKER_TLV_HDRLEN;
  70. }
  71. static inline u8 rocker_tlv_get_u8(const struct rocker_tlv *tlv)
  72. {
  73. return *(u8 *) rocker_tlv_data(tlv);
  74. }
  75. static inline u16 rocker_tlv_get_u16(const struct rocker_tlv *tlv)
  76. {
  77. return *(u16 *) rocker_tlv_data(tlv);
  78. }
  79. static inline __be16 rocker_tlv_get_be16(const struct rocker_tlv *tlv)
  80. {
  81. return *(__be16 *) rocker_tlv_data(tlv);
  82. }
  83. static inline u32 rocker_tlv_get_u32(const struct rocker_tlv *tlv)
  84. {
  85. return *(u32 *) rocker_tlv_data(tlv);
  86. }
  87. static inline u64 rocker_tlv_get_u64(const struct rocker_tlv *tlv)
  88. {
  89. return *(u64 *) rocker_tlv_data(tlv);
  90. }
  91. void rocker_tlv_parse(const struct rocker_tlv **tb, int maxtype,
  92. const char *buf, int buf_len);
  93. static inline void rocker_tlv_parse_nested(const struct rocker_tlv **tb,
  94. int maxtype,
  95. const struct rocker_tlv *tlv)
  96. {
  97. rocker_tlv_parse(tb, maxtype, rocker_tlv_data(tlv),
  98. rocker_tlv_len(tlv));
  99. }
  100. static inline void
  101. rocker_tlv_parse_desc(const struct rocker_tlv **tb, int maxtype,
  102. const struct rocker_desc_info *desc_info)
  103. {
  104. rocker_tlv_parse(tb, maxtype, desc_info->data,
  105. desc_info->desc->tlv_size);
  106. }
  107. static inline struct rocker_tlv *
  108. rocker_tlv_start(struct rocker_desc_info *desc_info)
  109. {
  110. return (struct rocker_tlv *) ((char *) desc_info->data +
  111. desc_info->tlv_size);
  112. }
  113. int rocker_tlv_put(struct rocker_desc_info *desc_info,
  114. int attrtype, int attrlen, const void *data);
  115. static inline int
  116. rocker_tlv_put_u8(struct rocker_desc_info *desc_info, int attrtype, u8 value)
  117. {
  118. u8 tmp = value; /* work around GCC PR81715 */
  119. return rocker_tlv_put(desc_info, attrtype, sizeof(u8), &tmp);
  120. }
  121. static inline int
  122. rocker_tlv_put_u16(struct rocker_desc_info *desc_info, int attrtype, u16 value)
  123. {
  124. u16 tmp = value;
  125. return rocker_tlv_put(desc_info, attrtype, sizeof(u16), &tmp);
  126. }
  127. static inline int
  128. rocker_tlv_put_be16(struct rocker_desc_info *desc_info, int attrtype, __be16 value)
  129. {
  130. __be16 tmp = value;
  131. return rocker_tlv_put(desc_info, attrtype, sizeof(__be16), &tmp);
  132. }
  133. static inline int
  134. rocker_tlv_put_u32(struct rocker_desc_info *desc_info, int attrtype, u32 value)
  135. {
  136. u32 tmp = value;
  137. return rocker_tlv_put(desc_info, attrtype, sizeof(u32), &tmp);
  138. }
  139. static inline int
  140. rocker_tlv_put_be32(struct rocker_desc_info *desc_info, int attrtype, __be32 value)
  141. {
  142. __be32 tmp = value;
  143. return rocker_tlv_put(desc_info, attrtype, sizeof(__be32), &tmp);
  144. }
  145. static inline int
  146. rocker_tlv_put_u64(struct rocker_desc_info *desc_info, int attrtype, u64 value)
  147. {
  148. u64 tmp = value;
  149. return rocker_tlv_put(desc_info, attrtype, sizeof(u64), &tmp);
  150. }
  151. static inline struct rocker_tlv *
  152. rocker_tlv_nest_start(struct rocker_desc_info *desc_info, int attrtype)
  153. {
  154. struct rocker_tlv *start = rocker_tlv_start(desc_info);
  155. if (rocker_tlv_put(desc_info, attrtype, 0, NULL) < 0)
  156. return NULL;
  157. return start;
  158. }
  159. static inline void rocker_tlv_nest_end(struct rocker_desc_info *desc_info,
  160. struct rocker_tlv *start)
  161. {
  162. start->len = (char *) rocker_tlv_start(desc_info) - (char *) start;
  163. }
  164. static inline void rocker_tlv_nest_cancel(struct rocker_desc_info *desc_info,
  165. const struct rocker_tlv *start)
  166. {
  167. desc_info->tlv_size = (const char *) start - desc_info->data;
  168. }
  169. #endif