eee.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include "netlink.h"
  3. #include "common.h"
  4. #include "bitset.h"
  5. #define EEE_MODES_COUNT \
  6. (sizeof_field(struct ethtool_eee, supported) * BITS_PER_BYTE)
  7. struct eee_req_info {
  8. struct ethnl_req_info base;
  9. };
  10. struct eee_reply_data {
  11. struct ethnl_reply_data base;
  12. struct ethtool_eee eee;
  13. };
  14. #define EEE_REPDATA(__reply_base) \
  15. container_of(__reply_base, struct eee_reply_data, base)
  16. const struct nla_policy ethnl_eee_get_policy[] = {
  17. [ETHTOOL_A_EEE_HEADER] =
  18. NLA_POLICY_NESTED(ethnl_header_policy),
  19. };
  20. static int eee_prepare_data(const struct ethnl_req_info *req_base,
  21. struct ethnl_reply_data *reply_base,
  22. struct genl_info *info)
  23. {
  24. struct eee_reply_data *data = EEE_REPDATA(reply_base);
  25. struct net_device *dev = reply_base->dev;
  26. int ret;
  27. if (!dev->ethtool_ops->get_eee)
  28. return -EOPNOTSUPP;
  29. ret = ethnl_ops_begin(dev);
  30. if (ret < 0)
  31. return ret;
  32. ret = dev->ethtool_ops->get_eee(dev, &data->eee);
  33. ethnl_ops_complete(dev);
  34. return ret;
  35. }
  36. static int eee_reply_size(const struct ethnl_req_info *req_base,
  37. const struct ethnl_reply_data *reply_base)
  38. {
  39. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  40. const struct eee_reply_data *data = EEE_REPDATA(reply_base);
  41. const struct ethtool_eee *eee = &data->eee;
  42. int len = 0;
  43. int ret;
  44. BUILD_BUG_ON(sizeof(eee->advertised) * BITS_PER_BYTE !=
  45. EEE_MODES_COUNT);
  46. BUILD_BUG_ON(sizeof(eee->lp_advertised) * BITS_PER_BYTE !=
  47. EEE_MODES_COUNT);
  48. /* MODES_OURS */
  49. ret = ethnl_bitset32_size(&eee->advertised, &eee->supported,
  50. EEE_MODES_COUNT, link_mode_names, compact);
  51. if (ret < 0)
  52. return ret;
  53. len += ret;
  54. /* MODES_PEERS */
  55. ret = ethnl_bitset32_size(&eee->lp_advertised, NULL,
  56. EEE_MODES_COUNT, link_mode_names, compact);
  57. if (ret < 0)
  58. return ret;
  59. len += ret;
  60. len += nla_total_size(sizeof(u8)) + /* _EEE_ACTIVE */
  61. nla_total_size(sizeof(u8)) + /* _EEE_ENABLED */
  62. nla_total_size(sizeof(u8)) + /* _EEE_TX_LPI_ENABLED */
  63. nla_total_size(sizeof(u32)); /* _EEE_TX_LPI_TIMER */
  64. return len;
  65. }
  66. static int eee_fill_reply(struct sk_buff *skb,
  67. const struct ethnl_req_info *req_base,
  68. const struct ethnl_reply_data *reply_base)
  69. {
  70. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  71. const struct eee_reply_data *data = EEE_REPDATA(reply_base);
  72. const struct ethtool_eee *eee = &data->eee;
  73. int ret;
  74. ret = ethnl_put_bitset32(skb, ETHTOOL_A_EEE_MODES_OURS,
  75. &eee->advertised, &eee->supported,
  76. EEE_MODES_COUNT, link_mode_names, compact);
  77. if (ret < 0)
  78. return ret;
  79. ret = ethnl_put_bitset32(skb, ETHTOOL_A_EEE_MODES_PEER,
  80. &eee->lp_advertised, NULL, EEE_MODES_COUNT,
  81. link_mode_names, compact);
  82. if (ret < 0)
  83. return ret;
  84. if (nla_put_u8(skb, ETHTOOL_A_EEE_ACTIVE, !!eee->eee_active) ||
  85. nla_put_u8(skb, ETHTOOL_A_EEE_ENABLED, !!eee->eee_enabled) ||
  86. nla_put_u8(skb, ETHTOOL_A_EEE_TX_LPI_ENABLED,
  87. !!eee->tx_lpi_enabled) ||
  88. nla_put_u32(skb, ETHTOOL_A_EEE_TX_LPI_TIMER, eee->tx_lpi_timer))
  89. return -EMSGSIZE;
  90. return 0;
  91. }
  92. const struct ethnl_request_ops ethnl_eee_request_ops = {
  93. .request_cmd = ETHTOOL_MSG_EEE_GET,
  94. .reply_cmd = ETHTOOL_MSG_EEE_GET_REPLY,
  95. .hdr_attr = ETHTOOL_A_EEE_HEADER,
  96. .req_info_size = sizeof(struct eee_req_info),
  97. .reply_data_size = sizeof(struct eee_reply_data),
  98. .prepare_data = eee_prepare_data,
  99. .reply_size = eee_reply_size,
  100. .fill_reply = eee_fill_reply,
  101. };
  102. /* EEE_SET */
  103. const struct nla_policy ethnl_eee_set_policy[] = {
  104. [ETHTOOL_A_EEE_HEADER] =
  105. NLA_POLICY_NESTED(ethnl_header_policy),
  106. [ETHTOOL_A_EEE_MODES_OURS] = { .type = NLA_NESTED },
  107. [ETHTOOL_A_EEE_ENABLED] = { .type = NLA_U8 },
  108. [ETHTOOL_A_EEE_TX_LPI_ENABLED] = { .type = NLA_U8 },
  109. [ETHTOOL_A_EEE_TX_LPI_TIMER] = { .type = NLA_U32 },
  110. };
  111. int ethnl_set_eee(struct sk_buff *skb, struct genl_info *info)
  112. {
  113. struct ethnl_req_info req_info = {};
  114. struct nlattr **tb = info->attrs;
  115. const struct ethtool_ops *ops;
  116. struct ethtool_eee eee = {};
  117. struct net_device *dev;
  118. bool mod = false;
  119. int ret;
  120. ret = ethnl_parse_header_dev_get(&req_info,
  121. tb[ETHTOOL_A_EEE_HEADER],
  122. genl_info_net(info), info->extack,
  123. true);
  124. if (ret < 0)
  125. return ret;
  126. dev = req_info.dev;
  127. ops = dev->ethtool_ops;
  128. ret = -EOPNOTSUPP;
  129. if (!ops->get_eee || !ops->set_eee)
  130. goto out_dev;
  131. rtnl_lock();
  132. ret = ethnl_ops_begin(dev);
  133. if (ret < 0)
  134. goto out_rtnl;
  135. ret = ops->get_eee(dev, &eee);
  136. if (ret < 0)
  137. goto out_ops;
  138. ret = ethnl_update_bitset32(&eee.advertised, EEE_MODES_COUNT,
  139. tb[ETHTOOL_A_EEE_MODES_OURS],
  140. link_mode_names, info->extack, &mod);
  141. if (ret < 0)
  142. goto out_ops;
  143. ethnl_update_bool32(&eee.eee_enabled, tb[ETHTOOL_A_EEE_ENABLED], &mod);
  144. ethnl_update_bool32(&eee.tx_lpi_enabled,
  145. tb[ETHTOOL_A_EEE_TX_LPI_ENABLED], &mod);
  146. ethnl_update_u32(&eee.tx_lpi_timer, tb[ETHTOOL_A_EEE_TX_LPI_TIMER],
  147. &mod);
  148. ret = 0;
  149. if (!mod)
  150. goto out_ops;
  151. ret = dev->ethtool_ops->set_eee(dev, &eee);
  152. if (ret < 0)
  153. goto out_ops;
  154. ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
  155. out_ops:
  156. ethnl_ops_complete(dev);
  157. out_rtnl:
  158. rtnl_unlock();
  159. out_dev:
  160. dev_put(dev);
  161. return ret;
  162. }