plca.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/phy.h>
  3. #include <linux/ethtool_netlink.h>
  4. #include "netlink.h"
  5. #include "common.h"
  6. struct plca_req_info {
  7. struct ethnl_req_info base;
  8. };
  9. struct plca_reply_data {
  10. struct ethnl_reply_data base;
  11. struct phy_plca_cfg plca_cfg;
  12. struct phy_plca_status plca_st;
  13. };
  14. // Helpers ------------------------------------------------------------------ //
  15. #define PLCA_REPDATA(__reply_base) \
  16. container_of(__reply_base, struct plca_reply_data, base)
  17. // PLCA get configuration message ------------------------------------------- //
  18. const struct nla_policy ethnl_plca_get_cfg_policy[] = {
  19. [ETHTOOL_A_PLCA_HEADER] =
  20. NLA_POLICY_NESTED(ethnl_header_policy_phy),
  21. };
  22. static void plca_update_sint(int *dst, struct nlattr **tb, u32 attrid,
  23. bool *mod)
  24. {
  25. const struct nlattr *attr = tb[attrid];
  26. if (!attr ||
  27. WARN_ON_ONCE(attrid >= ARRAY_SIZE(ethnl_plca_set_cfg_policy)))
  28. return;
  29. switch (ethnl_plca_set_cfg_policy[attrid].type) {
  30. case NLA_U8:
  31. *dst = nla_get_u8(attr);
  32. break;
  33. case NLA_U32:
  34. *dst = nla_get_u32(attr);
  35. break;
  36. default:
  37. WARN_ON_ONCE(1);
  38. }
  39. *mod = true;
  40. }
  41. static int plca_get_cfg_prepare_data(const struct ethnl_req_info *req_base,
  42. struct ethnl_reply_data *reply_base,
  43. const struct genl_info *info)
  44. {
  45. struct plca_reply_data *data = PLCA_REPDATA(reply_base);
  46. struct net_device *dev = reply_base->dev;
  47. const struct ethtool_phy_ops *ops;
  48. struct nlattr **tb = info->attrs;
  49. struct phy_device *phydev;
  50. int ret;
  51. phydev = ethnl_req_get_phydev(req_base, tb[ETHTOOL_A_PLCA_HEADER],
  52. info->extack);
  53. // check that the PHY device is available and connected
  54. if (IS_ERR_OR_NULL(phydev)) {
  55. ret = -EOPNOTSUPP;
  56. goto out;
  57. }
  58. // note: rtnl_lock is held already by ethnl_default_doit
  59. ops = ethtool_phy_ops;
  60. if (!ops || !ops->get_plca_cfg) {
  61. ret = -EOPNOTSUPP;
  62. goto out;
  63. }
  64. ret = ethnl_ops_begin(dev);
  65. if (ret < 0)
  66. goto out;
  67. memset(&data->plca_cfg, 0xff,
  68. sizeof_field(struct plca_reply_data, plca_cfg));
  69. ret = ops->get_plca_cfg(phydev, &data->plca_cfg);
  70. ethnl_ops_complete(dev);
  71. out:
  72. return ret;
  73. }
  74. static int plca_get_cfg_reply_size(const struct ethnl_req_info *req_base,
  75. const struct ethnl_reply_data *reply_base)
  76. {
  77. return nla_total_size(sizeof(u16)) + /* _VERSION */
  78. nla_total_size(sizeof(u8)) + /* _ENABLED */
  79. nla_total_size(sizeof(u32)) + /* _NODE_CNT */
  80. nla_total_size(sizeof(u32)) + /* _NODE_ID */
  81. nla_total_size(sizeof(u32)) + /* _TO_TIMER */
  82. nla_total_size(sizeof(u32)) + /* _BURST_COUNT */
  83. nla_total_size(sizeof(u32)); /* _BURST_TIMER */
  84. }
  85. static int plca_get_cfg_fill_reply(struct sk_buff *skb,
  86. const struct ethnl_req_info *req_base,
  87. const struct ethnl_reply_data *reply_base)
  88. {
  89. const struct plca_reply_data *data = PLCA_REPDATA(reply_base);
  90. const struct phy_plca_cfg *plca = &data->plca_cfg;
  91. if ((plca->version >= 0 &&
  92. nla_put_u16(skb, ETHTOOL_A_PLCA_VERSION, plca->version)) ||
  93. (plca->enabled >= 0 &&
  94. nla_put_u8(skb, ETHTOOL_A_PLCA_ENABLED, !!plca->enabled)) ||
  95. (plca->node_id >= 0 &&
  96. nla_put_u32(skb, ETHTOOL_A_PLCA_NODE_ID, plca->node_id)) ||
  97. (plca->node_cnt >= 0 &&
  98. nla_put_u32(skb, ETHTOOL_A_PLCA_NODE_CNT, plca->node_cnt)) ||
  99. (plca->to_tmr >= 0 &&
  100. nla_put_u32(skb, ETHTOOL_A_PLCA_TO_TMR, plca->to_tmr)) ||
  101. (plca->burst_cnt >= 0 &&
  102. nla_put_u32(skb, ETHTOOL_A_PLCA_BURST_CNT, plca->burst_cnt)) ||
  103. (plca->burst_tmr >= 0 &&
  104. nla_put_u32(skb, ETHTOOL_A_PLCA_BURST_TMR, plca->burst_tmr)))
  105. return -EMSGSIZE;
  106. return 0;
  107. };
  108. // PLCA set configuration message ------------------------------------------- //
  109. const struct nla_policy ethnl_plca_set_cfg_policy[] = {
  110. [ETHTOOL_A_PLCA_HEADER] =
  111. NLA_POLICY_NESTED(ethnl_header_policy_phy),
  112. [ETHTOOL_A_PLCA_ENABLED] = NLA_POLICY_MAX(NLA_U8, 1),
  113. [ETHTOOL_A_PLCA_NODE_ID] = NLA_POLICY_MAX(NLA_U32, 255),
  114. [ETHTOOL_A_PLCA_NODE_CNT] = NLA_POLICY_RANGE(NLA_U32, 1, 255),
  115. [ETHTOOL_A_PLCA_TO_TMR] = NLA_POLICY_MAX(NLA_U32, 255),
  116. [ETHTOOL_A_PLCA_BURST_CNT] = NLA_POLICY_MAX(NLA_U32, 255),
  117. [ETHTOOL_A_PLCA_BURST_TMR] = NLA_POLICY_MAX(NLA_U32, 255),
  118. };
  119. static int
  120. ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info)
  121. {
  122. const struct ethtool_phy_ops *ops;
  123. struct nlattr **tb = info->attrs;
  124. struct phy_plca_cfg plca_cfg;
  125. struct phy_device *phydev;
  126. bool mod = false;
  127. int ret;
  128. phydev = ethnl_req_get_phydev(req_info, tb[ETHTOOL_A_PLCA_HEADER],
  129. info->extack);
  130. // check that the PHY device is available and connected
  131. if (IS_ERR_OR_NULL(phydev))
  132. return -EOPNOTSUPP;
  133. ops = ethtool_phy_ops;
  134. if (!ops || !ops->set_plca_cfg)
  135. return -EOPNOTSUPP;
  136. memset(&plca_cfg, 0xff, sizeof(plca_cfg));
  137. plca_update_sint(&plca_cfg.enabled, tb, ETHTOOL_A_PLCA_ENABLED, &mod);
  138. plca_update_sint(&plca_cfg.node_id, tb, ETHTOOL_A_PLCA_NODE_ID, &mod);
  139. plca_update_sint(&plca_cfg.node_cnt, tb, ETHTOOL_A_PLCA_NODE_CNT, &mod);
  140. plca_update_sint(&plca_cfg.to_tmr, tb, ETHTOOL_A_PLCA_TO_TMR, &mod);
  141. plca_update_sint(&plca_cfg.burst_cnt, tb, ETHTOOL_A_PLCA_BURST_CNT,
  142. &mod);
  143. plca_update_sint(&plca_cfg.burst_tmr, tb, ETHTOOL_A_PLCA_BURST_TMR,
  144. &mod);
  145. if (!mod)
  146. return 0;
  147. ret = ops->set_plca_cfg(phydev, &plca_cfg, info->extack);
  148. return ret < 0 ? ret : 1;
  149. }
  150. const struct ethnl_request_ops ethnl_plca_cfg_request_ops = {
  151. .request_cmd = ETHTOOL_MSG_PLCA_GET_CFG,
  152. .reply_cmd = ETHTOOL_MSG_PLCA_GET_CFG_REPLY,
  153. .hdr_attr = ETHTOOL_A_PLCA_HEADER,
  154. .req_info_size = sizeof(struct plca_req_info),
  155. .reply_data_size = sizeof(struct plca_reply_data),
  156. .prepare_data = plca_get_cfg_prepare_data,
  157. .reply_size = plca_get_cfg_reply_size,
  158. .fill_reply = plca_get_cfg_fill_reply,
  159. .set = ethnl_set_plca,
  160. .set_ntf_cmd = ETHTOOL_MSG_PLCA_NTF,
  161. };
  162. // PLCA get status message -------------------------------------------------- //
  163. const struct nla_policy ethnl_plca_get_status_policy[] = {
  164. [ETHTOOL_A_PLCA_HEADER] =
  165. NLA_POLICY_NESTED(ethnl_header_policy_phy),
  166. };
  167. static int plca_get_status_prepare_data(const struct ethnl_req_info *req_base,
  168. struct ethnl_reply_data *reply_base,
  169. const struct genl_info *info)
  170. {
  171. struct plca_reply_data *data = PLCA_REPDATA(reply_base);
  172. struct net_device *dev = reply_base->dev;
  173. const struct ethtool_phy_ops *ops;
  174. struct nlattr **tb = info->attrs;
  175. struct phy_device *phydev;
  176. int ret;
  177. phydev = ethnl_req_get_phydev(req_base, tb[ETHTOOL_A_PLCA_HEADER],
  178. info->extack);
  179. // check that the PHY device is available and connected
  180. if (IS_ERR_OR_NULL(phydev)) {
  181. ret = -EOPNOTSUPP;
  182. goto out;
  183. }
  184. // note: rtnl_lock is held already by ethnl_default_doit
  185. ops = ethtool_phy_ops;
  186. if (!ops || !ops->get_plca_status) {
  187. ret = -EOPNOTSUPP;
  188. goto out;
  189. }
  190. ret = ethnl_ops_begin(dev);
  191. if (ret < 0)
  192. goto out;
  193. memset(&data->plca_st, 0xff,
  194. sizeof_field(struct plca_reply_data, plca_st));
  195. ret = ops->get_plca_status(phydev, &data->plca_st);
  196. ethnl_ops_complete(dev);
  197. out:
  198. return ret;
  199. }
  200. static int plca_get_status_reply_size(const struct ethnl_req_info *req_base,
  201. const struct ethnl_reply_data *reply_base)
  202. {
  203. return nla_total_size(sizeof(u8)); /* _STATUS */
  204. }
  205. static int plca_get_status_fill_reply(struct sk_buff *skb,
  206. const struct ethnl_req_info *req_base,
  207. const struct ethnl_reply_data *reply_base)
  208. {
  209. const struct plca_reply_data *data = PLCA_REPDATA(reply_base);
  210. const u8 status = data->plca_st.pst;
  211. if (nla_put_u8(skb, ETHTOOL_A_PLCA_STATUS, !!status))
  212. return -EMSGSIZE;
  213. return 0;
  214. };
  215. const struct ethnl_request_ops ethnl_plca_status_request_ops = {
  216. .request_cmd = ETHTOOL_MSG_PLCA_GET_STATUS,
  217. .reply_cmd = ETHTOOL_MSG_PLCA_GET_STATUS_REPLY,
  218. .hdr_attr = ETHTOOL_A_PLCA_HEADER,
  219. .req_info_size = sizeof(struct plca_req_info),
  220. .reply_data_size = sizeof(struct plca_reply_data),
  221. .prepare_data = plca_get_status_prepare_data,
  222. .reply_size = plca_get_status_reply_size,
  223. .fill_reply = plca_get_status_fill_reply,
  224. };