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