0001
0002
0003 #include "netlink.h"
0004 #include "common.h"
0005 #include "bitset.h"
0006
0007 #define EEE_MODES_COUNT \
0008 (sizeof_field(struct ethtool_eee, supported) * BITS_PER_BYTE)
0009
0010 struct eee_req_info {
0011 struct ethnl_req_info base;
0012 };
0013
0014 struct eee_reply_data {
0015 struct ethnl_reply_data base;
0016 struct ethtool_eee eee;
0017 };
0018
0019 #define EEE_REPDATA(__reply_base) \
0020 container_of(__reply_base, struct eee_reply_data, base)
0021
0022 const struct nla_policy ethnl_eee_get_policy[] = {
0023 [ETHTOOL_A_EEE_HEADER] =
0024 NLA_POLICY_NESTED(ethnl_header_policy),
0025 };
0026
0027 static int eee_prepare_data(const struct ethnl_req_info *req_base,
0028 struct ethnl_reply_data *reply_base,
0029 struct genl_info *info)
0030 {
0031 struct eee_reply_data *data = EEE_REPDATA(reply_base);
0032 struct net_device *dev = reply_base->dev;
0033 int ret;
0034
0035 if (!dev->ethtool_ops->get_eee)
0036 return -EOPNOTSUPP;
0037 ret = ethnl_ops_begin(dev);
0038 if (ret < 0)
0039 return ret;
0040 ret = dev->ethtool_ops->get_eee(dev, &data->eee);
0041 ethnl_ops_complete(dev);
0042
0043 return ret;
0044 }
0045
0046 static int eee_reply_size(const struct ethnl_req_info *req_base,
0047 const struct ethnl_reply_data *reply_base)
0048 {
0049 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
0050 const struct eee_reply_data *data = EEE_REPDATA(reply_base);
0051 const struct ethtool_eee *eee = &data->eee;
0052 int len = 0;
0053 int ret;
0054
0055 BUILD_BUG_ON(sizeof(eee->advertised) * BITS_PER_BYTE !=
0056 EEE_MODES_COUNT);
0057 BUILD_BUG_ON(sizeof(eee->lp_advertised) * BITS_PER_BYTE !=
0058 EEE_MODES_COUNT);
0059
0060
0061 ret = ethnl_bitset32_size(&eee->advertised, &eee->supported,
0062 EEE_MODES_COUNT, link_mode_names, compact);
0063 if (ret < 0)
0064 return ret;
0065 len += ret;
0066
0067 ret = ethnl_bitset32_size(&eee->lp_advertised, NULL,
0068 EEE_MODES_COUNT, link_mode_names, compact);
0069 if (ret < 0)
0070 return ret;
0071 len += ret;
0072
0073 len += nla_total_size(sizeof(u8)) +
0074 nla_total_size(sizeof(u8)) +
0075 nla_total_size(sizeof(u8)) +
0076 nla_total_size(sizeof(u32));
0077
0078 return len;
0079 }
0080
0081 static int eee_fill_reply(struct sk_buff *skb,
0082 const struct ethnl_req_info *req_base,
0083 const struct ethnl_reply_data *reply_base)
0084 {
0085 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
0086 const struct eee_reply_data *data = EEE_REPDATA(reply_base);
0087 const struct ethtool_eee *eee = &data->eee;
0088 int ret;
0089
0090 ret = ethnl_put_bitset32(skb, ETHTOOL_A_EEE_MODES_OURS,
0091 &eee->advertised, &eee->supported,
0092 EEE_MODES_COUNT, link_mode_names, compact);
0093 if (ret < 0)
0094 return ret;
0095 ret = ethnl_put_bitset32(skb, ETHTOOL_A_EEE_MODES_PEER,
0096 &eee->lp_advertised, NULL, EEE_MODES_COUNT,
0097 link_mode_names, compact);
0098 if (ret < 0)
0099 return ret;
0100
0101 if (nla_put_u8(skb, ETHTOOL_A_EEE_ACTIVE, !!eee->eee_active) ||
0102 nla_put_u8(skb, ETHTOOL_A_EEE_ENABLED, !!eee->eee_enabled) ||
0103 nla_put_u8(skb, ETHTOOL_A_EEE_TX_LPI_ENABLED,
0104 !!eee->tx_lpi_enabled) ||
0105 nla_put_u32(skb, ETHTOOL_A_EEE_TX_LPI_TIMER, eee->tx_lpi_timer))
0106 return -EMSGSIZE;
0107
0108 return 0;
0109 }
0110
0111 const struct ethnl_request_ops ethnl_eee_request_ops = {
0112 .request_cmd = ETHTOOL_MSG_EEE_GET,
0113 .reply_cmd = ETHTOOL_MSG_EEE_GET_REPLY,
0114 .hdr_attr = ETHTOOL_A_EEE_HEADER,
0115 .req_info_size = sizeof(struct eee_req_info),
0116 .reply_data_size = sizeof(struct eee_reply_data),
0117
0118 .prepare_data = eee_prepare_data,
0119 .reply_size = eee_reply_size,
0120 .fill_reply = eee_fill_reply,
0121 };
0122
0123
0124
0125 const struct nla_policy ethnl_eee_set_policy[] = {
0126 [ETHTOOL_A_EEE_HEADER] =
0127 NLA_POLICY_NESTED(ethnl_header_policy),
0128 [ETHTOOL_A_EEE_MODES_OURS] = { .type = NLA_NESTED },
0129 [ETHTOOL_A_EEE_ENABLED] = { .type = NLA_U8 },
0130 [ETHTOOL_A_EEE_TX_LPI_ENABLED] = { .type = NLA_U8 },
0131 [ETHTOOL_A_EEE_TX_LPI_TIMER] = { .type = NLA_U32 },
0132 };
0133
0134 int ethnl_set_eee(struct sk_buff *skb, struct genl_info *info)
0135 {
0136 struct ethnl_req_info req_info = {};
0137 struct nlattr **tb = info->attrs;
0138 const struct ethtool_ops *ops;
0139 struct ethtool_eee eee = {};
0140 struct net_device *dev;
0141 bool mod = false;
0142 int ret;
0143
0144 ret = ethnl_parse_header_dev_get(&req_info,
0145 tb[ETHTOOL_A_EEE_HEADER],
0146 genl_info_net(info), info->extack,
0147 true);
0148 if (ret < 0)
0149 return ret;
0150 dev = req_info.dev;
0151 ops = dev->ethtool_ops;
0152 ret = -EOPNOTSUPP;
0153 if (!ops->get_eee || !ops->set_eee)
0154 goto out_dev;
0155
0156 rtnl_lock();
0157 ret = ethnl_ops_begin(dev);
0158 if (ret < 0)
0159 goto out_rtnl;
0160 ret = ops->get_eee(dev, &eee);
0161 if (ret < 0)
0162 goto out_ops;
0163
0164 ret = ethnl_update_bitset32(&eee.advertised, EEE_MODES_COUNT,
0165 tb[ETHTOOL_A_EEE_MODES_OURS],
0166 link_mode_names, info->extack, &mod);
0167 if (ret < 0)
0168 goto out_ops;
0169 ethnl_update_bool32(&eee.eee_enabled, tb[ETHTOOL_A_EEE_ENABLED], &mod);
0170 ethnl_update_bool32(&eee.tx_lpi_enabled,
0171 tb[ETHTOOL_A_EEE_TX_LPI_ENABLED], &mod);
0172 ethnl_update_u32(&eee.tx_lpi_timer, tb[ETHTOOL_A_EEE_TX_LPI_TIMER],
0173 &mod);
0174 ret = 0;
0175 if (!mod)
0176 goto out_ops;
0177
0178 ret = dev->ethtool_ops->set_eee(dev, &eee);
0179 if (ret < 0)
0180 goto out_ops;
0181 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
0182
0183 out_ops:
0184 ethnl_ops_complete(dev);
0185 out_rtnl:
0186 rtnl_unlock();
0187 out_dev:
0188 ethnl_parse_header_dev_put(&req_info);
0189 return ret;
0190 }