1 // SPDX-License-Identifier: GPL-2.0-only
7 #define EEE_MODES_COUNT \
8 (sizeof_field(struct ethtool_eee, supported) * BITS_PER_BYTE)
11 struct ethnl_req_info base;
14 struct eee_reply_data {
15 struct ethnl_reply_data base;
16 struct ethtool_eee eee;
19 #define EEE_REPDATA(__reply_base) \
20 container_of(__reply_base, struct eee_reply_data, base)
22 static const struct nla_policy
23 eee_get_policy[ETHTOOL_A_EEE_MAX + 1] = {
24 [ETHTOOL_A_EEE_UNSPEC] = { .type = NLA_REJECT },
25 [ETHTOOL_A_EEE_HEADER] = { .type = NLA_NESTED },
26 [ETHTOOL_A_EEE_MODES_OURS] = { .type = NLA_REJECT },
27 [ETHTOOL_A_EEE_MODES_PEER] = { .type = NLA_REJECT },
28 [ETHTOOL_A_EEE_ACTIVE] = { .type = NLA_REJECT },
29 [ETHTOOL_A_EEE_ENABLED] = { .type = NLA_REJECT },
30 [ETHTOOL_A_EEE_TX_LPI_ENABLED] = { .type = NLA_REJECT },
31 [ETHTOOL_A_EEE_TX_LPI_TIMER] = { .type = NLA_REJECT },
34 static int eee_prepare_data(const struct ethnl_req_info *req_base,
35 struct ethnl_reply_data *reply_base,
36 struct genl_info *info)
38 struct eee_reply_data *data = EEE_REPDATA(reply_base);
39 struct net_device *dev = reply_base->dev;
42 if (!dev->ethtool_ops->get_eee)
44 ret = ethnl_ops_begin(dev);
47 ret = dev->ethtool_ops->get_eee(dev, &data->eee);
48 ethnl_ops_complete(dev);
53 static int eee_reply_size(const struct ethnl_req_info *req_base,
54 const struct ethnl_reply_data *reply_base)
56 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
57 const struct eee_reply_data *data = EEE_REPDATA(reply_base);
58 const struct ethtool_eee *eee = &data->eee;
62 BUILD_BUG_ON(sizeof(eee->advertised) * BITS_PER_BYTE !=
64 BUILD_BUG_ON(sizeof(eee->lp_advertised) * BITS_PER_BYTE !=
68 ret = ethnl_bitset32_size(&eee->advertised, &eee->supported,
69 EEE_MODES_COUNT, link_mode_names, compact);
74 ret = ethnl_bitset32_size(&eee->lp_advertised, NULL,
75 EEE_MODES_COUNT, link_mode_names, compact);
80 len += nla_total_size(sizeof(u8)) + /* _EEE_ACTIVE */
81 nla_total_size(sizeof(u8)) + /* _EEE_ENABLED */
82 nla_total_size(sizeof(u8)) + /* _EEE_TX_LPI_ENABLED */
83 nla_total_size(sizeof(u32)); /* _EEE_TX_LPI_TIMER */
88 static int eee_fill_reply(struct sk_buff *skb,
89 const struct ethnl_req_info *req_base,
90 const struct ethnl_reply_data *reply_base)
92 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
93 const struct eee_reply_data *data = EEE_REPDATA(reply_base);
94 const struct ethtool_eee *eee = &data->eee;
97 ret = ethnl_put_bitset32(skb, ETHTOOL_A_EEE_MODES_OURS,
98 &eee->advertised, &eee->supported,
99 EEE_MODES_COUNT, link_mode_names, compact);
102 ret = ethnl_put_bitset32(skb, ETHTOOL_A_EEE_MODES_PEER,
103 &eee->lp_advertised, NULL, EEE_MODES_COUNT,
104 link_mode_names, compact);
108 if (nla_put_u8(skb, ETHTOOL_A_EEE_ACTIVE, !!eee->eee_active) ||
109 nla_put_u8(skb, ETHTOOL_A_EEE_ENABLED, !!eee->eee_enabled) ||
110 nla_put_u8(skb, ETHTOOL_A_EEE_TX_LPI_ENABLED,
111 !!eee->tx_lpi_enabled) ||
112 nla_put_u32(skb, ETHTOOL_A_EEE_TX_LPI_TIMER, eee->tx_lpi_timer))
118 const struct ethnl_request_ops ethnl_eee_request_ops = {
119 .request_cmd = ETHTOOL_MSG_EEE_GET,
120 .reply_cmd = ETHTOOL_MSG_EEE_GET_REPLY,
121 .hdr_attr = ETHTOOL_A_EEE_HEADER,
122 .max_attr = ETHTOOL_A_EEE_MAX,
123 .req_info_size = sizeof(struct eee_req_info),
124 .reply_data_size = sizeof(struct eee_reply_data),
125 .request_policy = eee_get_policy,
127 .prepare_data = eee_prepare_data,
128 .reply_size = eee_reply_size,
129 .fill_reply = eee_fill_reply,
134 static const struct nla_policy
135 eee_set_policy[ETHTOOL_A_EEE_MAX + 1] = {
136 [ETHTOOL_A_EEE_UNSPEC] = { .type = NLA_REJECT },
137 [ETHTOOL_A_EEE_HEADER] = { .type = NLA_NESTED },
138 [ETHTOOL_A_EEE_MODES_OURS] = { .type = NLA_NESTED },
139 [ETHTOOL_A_EEE_MODES_PEER] = { .type = NLA_REJECT },
140 [ETHTOOL_A_EEE_ACTIVE] = { .type = NLA_REJECT },
141 [ETHTOOL_A_EEE_ENABLED] = { .type = NLA_U8 },
142 [ETHTOOL_A_EEE_TX_LPI_ENABLED] = { .type = NLA_U8 },
143 [ETHTOOL_A_EEE_TX_LPI_TIMER] = { .type = NLA_U32 },
146 int ethnl_set_eee(struct sk_buff *skb, struct genl_info *info)
148 struct nlattr *tb[ETHTOOL_A_EEE_MAX + 1];
149 struct ethtool_eee eee = {};
150 struct ethnl_req_info req_info = {};
151 const struct ethtool_ops *ops;
152 struct net_device *dev;
156 ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb, ETHTOOL_A_EEE_MAX,
157 eee_set_policy, info->extack);
160 ret = ethnl_parse_header_dev_get(&req_info,
161 tb[ETHTOOL_A_EEE_HEADER],
162 genl_info_net(info), info->extack,
167 ops = dev->ethtool_ops;
169 if (!ops->get_eee || !ops->set_eee)
173 ret = ethnl_ops_begin(dev);
176 ret = ops->get_eee(dev, &eee);
180 ret = ethnl_update_bitset32(&eee.advertised, EEE_MODES_COUNT,
181 tb[ETHTOOL_A_EEE_MODES_OURS],
182 link_mode_names, info->extack, &mod);
185 ethnl_update_bool32(&eee.eee_enabled, tb[ETHTOOL_A_EEE_ENABLED], &mod);
186 ethnl_update_bool32(&eee.tx_lpi_enabled,
187 tb[ETHTOOL_A_EEE_TX_LPI_ENABLED], &mod);
188 ethnl_update_bool32(&eee.tx_lpi_timer, tb[ETHTOOL_A_EEE_TX_LPI_TIMER],
194 ret = dev->ethtool_ops->set_eee(dev, &eee);
197 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
200 ethnl_ops_complete(dev);