pse-pd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // ethtool interface for Ethernet PSE (Power Sourcing Equipment)
  4. // and PD (Powered Device)
  5. //
  6. // Copyright (c) 2022 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
  7. //
  8. #include "common.h"
  9. #include "linux/pse-pd/pse.h"
  10. #include "netlink.h"
  11. #include <linux/ethtool_netlink.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/export.h>
  14. #include <linux/phy.h>
  15. struct pse_req_info {
  16. struct ethnl_req_info base;
  17. };
  18. struct pse_reply_data {
  19. struct ethnl_reply_data base;
  20. struct ethtool_pse_control_status status;
  21. };
  22. #define PSE_REPDATA(__reply_base) \
  23. container_of(__reply_base, struct pse_reply_data, base)
  24. /* PSE_GET */
  25. const struct nla_policy ethnl_pse_get_policy[ETHTOOL_A_PSE_HEADER + 1] = {
  26. [ETHTOOL_A_PSE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy_phy),
  27. };
  28. static int pse_get_pse_attributes(struct phy_device *phydev,
  29. struct netlink_ext_ack *extack,
  30. struct pse_reply_data *data)
  31. {
  32. if (!phydev) {
  33. NL_SET_ERR_MSG(extack, "No PHY found");
  34. return -EOPNOTSUPP;
  35. }
  36. if (!phydev->psec) {
  37. NL_SET_ERR_MSG(extack, "No PSE is attached");
  38. return -EOPNOTSUPP;
  39. }
  40. memset(&data->status, 0, sizeof(data->status));
  41. return pse_ethtool_get_status(phydev->psec, extack, &data->status);
  42. }
  43. static int pse_prepare_data(const struct ethnl_req_info *req_base,
  44. struct ethnl_reply_data *reply_base,
  45. const struct genl_info *info)
  46. {
  47. struct pse_reply_data *data = PSE_REPDATA(reply_base);
  48. struct net_device *dev = reply_base->dev;
  49. struct nlattr **tb = info->attrs;
  50. struct phy_device *phydev;
  51. int ret;
  52. ret = ethnl_ops_begin(dev);
  53. if (ret < 0)
  54. return ret;
  55. phydev = ethnl_req_get_phydev(req_base, tb, ETHTOOL_A_PSE_HEADER,
  56. info->extack);
  57. if (IS_ERR(phydev))
  58. return -ENODEV;
  59. ret = pse_get_pse_attributes(phydev, info->extack, data);
  60. ethnl_ops_complete(dev);
  61. return ret;
  62. }
  63. static int pse_reply_size(const struct ethnl_req_info *req_base,
  64. const struct ethnl_reply_data *reply_base)
  65. {
  66. const struct pse_reply_data *data = PSE_REPDATA(reply_base);
  67. const struct ethtool_pse_control_status *st = &data->status;
  68. int len = 0;
  69. if (st->pw_d_id)
  70. len += nla_total_size(sizeof(u32)); /* _PSE_PW_D_ID */
  71. if (st->podl_admin_state > 0)
  72. len += nla_total_size(sizeof(u32)); /* _PODL_PSE_ADMIN_STATE */
  73. if (st->podl_pw_status > 0)
  74. len += nla_total_size(sizeof(u32)); /* _PODL_PSE_PW_D_STATUS */
  75. if (st->c33_admin_state > 0)
  76. len += nla_total_size(sizeof(u32)); /* _C33_PSE_ADMIN_STATE */
  77. if (st->c33_pw_status > 0)
  78. len += nla_total_size(sizeof(u32)); /* _C33_PSE_PW_D_STATUS */
  79. if (st->c33_pw_class > 0)
  80. len += nla_total_size(sizeof(u32)); /* _C33_PSE_PW_CLASS */
  81. if (st->c33_actual_pw > 0)
  82. len += nla_total_size(sizeof(u32)); /* _C33_PSE_ACTUAL_PW */
  83. if (st->c33_ext_state_info.c33_pse_ext_state > 0) {
  84. len += nla_total_size(sizeof(u32)); /* _C33_PSE_EXT_STATE */
  85. if (st->c33_ext_state_info.__c33_pse_ext_substate > 0)
  86. /* _C33_PSE_EXT_SUBSTATE */
  87. len += nla_total_size(sizeof(u32));
  88. }
  89. if (st->c33_avail_pw_limit > 0)
  90. /* _C33_AVAIL_PSE_PW_LIMIT */
  91. len += nla_total_size(sizeof(u32));
  92. if (st->c33_pw_limit_nb_ranges > 0)
  93. /* _C33_PSE_PW_LIMIT_RANGES */
  94. len += st->c33_pw_limit_nb_ranges *
  95. (nla_total_size(0) +
  96. nla_total_size(sizeof(u32)) * 2);
  97. if (st->prio_max)
  98. /* _PSE_PRIO_MAX + _PSE_PRIO */
  99. len += nla_total_size(sizeof(u32)) * 2;
  100. return len;
  101. }
  102. static int pse_put_pw_limit_ranges(struct sk_buff *skb,
  103. const struct ethtool_pse_control_status *st)
  104. {
  105. const struct ethtool_c33_pse_pw_limit_range *pw_limit_ranges;
  106. int i;
  107. pw_limit_ranges = st->c33_pw_limit_ranges;
  108. for (i = 0; i < st->c33_pw_limit_nb_ranges; i++) {
  109. struct nlattr *nest;
  110. nest = nla_nest_start(skb, ETHTOOL_A_C33_PSE_PW_LIMIT_RANGES);
  111. if (!nest)
  112. return -EMSGSIZE;
  113. if (nla_put_u32(skb, ETHTOOL_A_C33_PSE_PW_LIMIT_MIN,
  114. pw_limit_ranges->min) ||
  115. nla_put_u32(skb, ETHTOOL_A_C33_PSE_PW_LIMIT_MAX,
  116. pw_limit_ranges->max)) {
  117. nla_nest_cancel(skb, nest);
  118. return -EMSGSIZE;
  119. }
  120. nla_nest_end(skb, nest);
  121. pw_limit_ranges++;
  122. }
  123. return 0;
  124. }
  125. static int pse_fill_reply(struct sk_buff *skb,
  126. const struct ethnl_req_info *req_base,
  127. const struct ethnl_reply_data *reply_base)
  128. {
  129. const struct pse_reply_data *data = PSE_REPDATA(reply_base);
  130. const struct ethtool_pse_control_status *st = &data->status;
  131. if (st->pw_d_id &&
  132. nla_put_u32(skb, ETHTOOL_A_PSE_PW_D_ID,
  133. st->pw_d_id))
  134. return -EMSGSIZE;
  135. if (st->podl_admin_state > 0 &&
  136. nla_put_u32(skb, ETHTOOL_A_PODL_PSE_ADMIN_STATE,
  137. st->podl_admin_state))
  138. return -EMSGSIZE;
  139. if (st->podl_pw_status > 0 &&
  140. nla_put_u32(skb, ETHTOOL_A_PODL_PSE_PW_D_STATUS,
  141. st->podl_pw_status))
  142. return -EMSGSIZE;
  143. if (st->c33_admin_state > 0 &&
  144. nla_put_u32(skb, ETHTOOL_A_C33_PSE_ADMIN_STATE,
  145. st->c33_admin_state))
  146. return -EMSGSIZE;
  147. if (st->c33_pw_status > 0 &&
  148. nla_put_u32(skb, ETHTOOL_A_C33_PSE_PW_D_STATUS,
  149. st->c33_pw_status))
  150. return -EMSGSIZE;
  151. if (st->c33_pw_class > 0 &&
  152. nla_put_u32(skb, ETHTOOL_A_C33_PSE_PW_CLASS,
  153. st->c33_pw_class))
  154. return -EMSGSIZE;
  155. if (st->c33_actual_pw > 0 &&
  156. nla_put_u32(skb, ETHTOOL_A_C33_PSE_ACTUAL_PW,
  157. st->c33_actual_pw))
  158. return -EMSGSIZE;
  159. if (st->c33_ext_state_info.c33_pse_ext_state > 0) {
  160. if (nla_put_u32(skb, ETHTOOL_A_C33_PSE_EXT_STATE,
  161. st->c33_ext_state_info.c33_pse_ext_state))
  162. return -EMSGSIZE;
  163. if (st->c33_ext_state_info.__c33_pse_ext_substate > 0 &&
  164. nla_put_u32(skb, ETHTOOL_A_C33_PSE_EXT_SUBSTATE,
  165. st->c33_ext_state_info.__c33_pse_ext_substate))
  166. return -EMSGSIZE;
  167. }
  168. if (st->c33_avail_pw_limit > 0 &&
  169. nla_put_u32(skb, ETHTOOL_A_C33_PSE_AVAIL_PW_LIMIT,
  170. st->c33_avail_pw_limit))
  171. return -EMSGSIZE;
  172. if (st->c33_pw_limit_nb_ranges > 0 &&
  173. pse_put_pw_limit_ranges(skb, st))
  174. return -EMSGSIZE;
  175. if (st->prio_max &&
  176. (nla_put_u32(skb, ETHTOOL_A_PSE_PRIO_MAX, st->prio_max) ||
  177. nla_put_u32(skb, ETHTOOL_A_PSE_PRIO, st->prio)))
  178. return -EMSGSIZE;
  179. return 0;
  180. }
  181. static void pse_cleanup_data(struct ethnl_reply_data *reply_base)
  182. {
  183. const struct pse_reply_data *data = PSE_REPDATA(reply_base);
  184. kfree(data->status.c33_pw_limit_ranges);
  185. }
  186. /* PSE_SET */
  187. const struct nla_policy ethnl_pse_set_policy[ETHTOOL_A_PSE_MAX + 1] = {
  188. [ETHTOOL_A_PSE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy_phy),
  189. [ETHTOOL_A_PODL_PSE_ADMIN_CONTROL] =
  190. NLA_POLICY_RANGE(NLA_U32, ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED,
  191. ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED),
  192. [ETHTOOL_A_C33_PSE_ADMIN_CONTROL] =
  193. NLA_POLICY_RANGE(NLA_U32, ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED,
  194. ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED),
  195. [ETHTOOL_A_C33_PSE_AVAIL_PW_LIMIT] = { .type = NLA_U32 },
  196. [ETHTOOL_A_PSE_PRIO] = { .type = NLA_U32 },
  197. };
  198. static int
  199. ethnl_set_pse_validate(struct phy_device *phydev, struct genl_info *info)
  200. {
  201. struct nlattr **tb = info->attrs;
  202. if (IS_ERR_OR_NULL(phydev)) {
  203. NL_SET_ERR_MSG(info->extack, "No PHY is attached");
  204. return -EOPNOTSUPP;
  205. }
  206. if (!phydev->psec) {
  207. NL_SET_ERR_MSG(info->extack, "No PSE is attached");
  208. return -EOPNOTSUPP;
  209. }
  210. if (tb[ETHTOOL_A_PODL_PSE_ADMIN_CONTROL] &&
  211. !pse_has_podl(phydev->psec)) {
  212. NL_SET_ERR_MSG_ATTR(info->extack,
  213. tb[ETHTOOL_A_PODL_PSE_ADMIN_CONTROL],
  214. "setting PoDL PSE admin control not supported");
  215. return -EOPNOTSUPP;
  216. }
  217. if (tb[ETHTOOL_A_C33_PSE_ADMIN_CONTROL] &&
  218. !pse_has_c33(phydev->psec)) {
  219. NL_SET_ERR_MSG_ATTR(info->extack,
  220. tb[ETHTOOL_A_C33_PSE_ADMIN_CONTROL],
  221. "setting C33 PSE admin control not supported");
  222. return -EOPNOTSUPP;
  223. }
  224. return 0;
  225. }
  226. static int
  227. ethnl_set_pse(struct ethnl_req_info *req_info, struct genl_info *info)
  228. {
  229. struct nlattr **tb = info->attrs;
  230. struct phy_device *phydev;
  231. int ret;
  232. phydev = ethnl_req_get_phydev(req_info, tb, ETHTOOL_A_PSE_HEADER,
  233. info->extack);
  234. ret = ethnl_set_pse_validate(phydev, info);
  235. if (ret)
  236. return ret;
  237. if (tb[ETHTOOL_A_PSE_PRIO]) {
  238. unsigned int prio;
  239. prio = nla_get_u32(tb[ETHTOOL_A_PSE_PRIO]);
  240. ret = pse_ethtool_set_prio(phydev->psec, info->extack, prio);
  241. if (ret)
  242. return ret;
  243. }
  244. if (tb[ETHTOOL_A_C33_PSE_AVAIL_PW_LIMIT]) {
  245. unsigned int pw_limit;
  246. pw_limit = nla_get_u32(tb[ETHTOOL_A_C33_PSE_AVAIL_PW_LIMIT]);
  247. ret = pse_ethtool_set_pw_limit(phydev->psec, info->extack,
  248. pw_limit);
  249. if (ret)
  250. return ret;
  251. }
  252. /* These values are already validated by the ethnl_pse_set_policy */
  253. if (tb[ETHTOOL_A_PODL_PSE_ADMIN_CONTROL] ||
  254. tb[ETHTOOL_A_C33_PSE_ADMIN_CONTROL]) {
  255. struct pse_control_config config = {};
  256. if (tb[ETHTOOL_A_PODL_PSE_ADMIN_CONTROL])
  257. config.podl_admin_control = nla_get_u32(tb[ETHTOOL_A_PODL_PSE_ADMIN_CONTROL]);
  258. if (tb[ETHTOOL_A_C33_PSE_ADMIN_CONTROL])
  259. config.c33_admin_control = nla_get_u32(tb[ETHTOOL_A_C33_PSE_ADMIN_CONTROL]);
  260. /* pse_ethtool_set_config() will do nothing if the config
  261. * is zero
  262. */
  263. ret = pse_ethtool_set_config(phydev->psec, info->extack,
  264. &config);
  265. if (ret)
  266. return ret;
  267. }
  268. /* Return errno or zero - PSE has no notification */
  269. return ret;
  270. }
  271. const struct ethnl_request_ops ethnl_pse_request_ops = {
  272. .request_cmd = ETHTOOL_MSG_PSE_GET,
  273. .reply_cmd = ETHTOOL_MSG_PSE_GET_REPLY,
  274. .hdr_attr = ETHTOOL_A_PSE_HEADER,
  275. .req_info_size = sizeof(struct pse_req_info),
  276. .reply_data_size = sizeof(struct pse_reply_data),
  277. .prepare_data = pse_prepare_data,
  278. .reply_size = pse_reply_size,
  279. .fill_reply = pse_fill_reply,
  280. .cleanup_data = pse_cleanup_data,
  281. .set = ethnl_set_pse,
  282. /* PSE has no notification */
  283. };
  284. void ethnl_pse_send_ntf(struct net_device *netdev, unsigned long notifs)
  285. {
  286. void *reply_payload;
  287. struct sk_buff *skb;
  288. int reply_len;
  289. int ret;
  290. ASSERT_RTNL();
  291. if (!netdev || !notifs)
  292. return;
  293. reply_len = ethnl_reply_header_size() +
  294. nla_total_size(sizeof(u32)); /* _PSE_NTF_EVENTS */
  295. skb = genlmsg_new(reply_len, GFP_KERNEL);
  296. if (!skb)
  297. return;
  298. reply_payload = ethnl_bcastmsg_put(skb, ETHTOOL_MSG_PSE_NTF);
  299. if (!reply_payload)
  300. goto err_skb;
  301. ret = ethnl_fill_reply_header(skb, netdev, ETHTOOL_A_PSE_NTF_HEADER);
  302. if (ret < 0)
  303. goto err_skb;
  304. if (nla_put_uint(skb, ETHTOOL_A_PSE_NTF_EVENTS, notifs))
  305. goto err_skb;
  306. genlmsg_end(skb, reply_payload);
  307. ethnl_multicast(skb, netdev);
  308. return;
  309. err_skb:
  310. nlmsg_free(skb);
  311. }
  312. EXPORT_SYMBOL_GPL(ethnl_pse_send_ntf);