1 // SPDX-License-Identifier: GPL-2.0-only
9 struct linkmodes_req_info {
10 struct ethnl_req_info base;
13 struct linkmodes_reply_data {
14 struct ethnl_reply_data base;
15 struct ethtool_link_ksettings ksettings;
16 struct ethtool_link_settings *lsettings;
20 #define LINKMODES_REPDATA(__reply_base) \
21 container_of(__reply_base, struct linkmodes_reply_data, base)
23 const struct nla_policy ethnl_linkmodes_get_policy[] = {
24 [ETHTOOL_A_LINKMODES_HEADER] =
25 NLA_POLICY_NESTED(ethnl_header_policy),
28 static int linkmodes_prepare_data(const struct ethnl_req_info *req_base,
29 struct ethnl_reply_data *reply_base,
30 const struct genl_info *info)
32 struct linkmodes_reply_data *data = LINKMODES_REPDATA(reply_base);
33 struct net_device *dev = reply_base->dev;
36 data->lsettings = &data->ksettings.base;
38 ret = ethnl_ops_begin(dev);
42 ret = __ethtool_get_link_ksettings(dev, &data->ksettings);
43 if (ret < 0 && info) {
44 GENL_SET_ERR_MSG(info, "failed to retrieve link settings");
48 if (!dev->ethtool_ops->cap_link_lanes_supported)
49 data->ksettings.lanes = 0;
52 bitmap_empty(data->ksettings.link_modes.lp_advertising,
53 __ETHTOOL_LINK_MODE_MASK_NBITS);
56 ethnl_ops_complete(dev);
60 static int linkmodes_reply_size(const struct ethnl_req_info *req_base,
61 const struct ethnl_reply_data *reply_base)
63 const struct linkmodes_reply_data *data = LINKMODES_REPDATA(reply_base);
64 const struct ethtool_link_ksettings *ksettings = &data->ksettings;
65 const struct ethtool_link_settings *lsettings = &ksettings->base;
66 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
69 len = nla_total_size(sizeof(u8)) /* LINKMODES_AUTONEG */
70 + nla_total_size(sizeof(u32)) /* LINKMODES_SPEED */
71 + nla_total_size(sizeof(u32)) /* LINKMODES_LANES */
72 + nla_total_size(sizeof(u8)) /* LINKMODES_DUPLEX */
73 + nla_total_size(sizeof(u8)) /* LINKMODES_RATE_MATCHING */
75 ret = ethnl_bitset_size(ksettings->link_modes.advertising,
76 ksettings->link_modes.supported,
77 __ETHTOOL_LINK_MODE_MASK_NBITS,
78 link_mode_names, compact);
82 if (!data->peer_empty) {
83 ret = ethnl_bitset_size(ksettings->link_modes.lp_advertising,
84 NULL, __ETHTOOL_LINK_MODE_MASK_NBITS,
85 link_mode_names, compact);
91 if (lsettings->master_slave_cfg != MASTER_SLAVE_CFG_UNSUPPORTED)
92 len += nla_total_size(sizeof(u8));
94 if (lsettings->master_slave_state != MASTER_SLAVE_STATE_UNSUPPORTED)
95 len += nla_total_size(sizeof(u8));
100 static int linkmodes_fill_reply(struct sk_buff *skb,
101 const struct ethnl_req_info *req_base,
102 const struct ethnl_reply_data *reply_base)
104 const struct linkmodes_reply_data *data = LINKMODES_REPDATA(reply_base);
105 const struct ethtool_link_ksettings *ksettings = &data->ksettings;
106 const struct ethtool_link_settings *lsettings = &ksettings->base;
107 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
110 if (nla_put_u8(skb, ETHTOOL_A_LINKMODES_AUTONEG, lsettings->autoneg))
113 ret = ethnl_put_bitset(skb, ETHTOOL_A_LINKMODES_OURS,
114 ksettings->link_modes.advertising,
115 ksettings->link_modes.supported,
116 __ETHTOOL_LINK_MODE_MASK_NBITS, link_mode_names,
120 if (!data->peer_empty) {
121 ret = ethnl_put_bitset(skb, ETHTOOL_A_LINKMODES_PEER,
122 ksettings->link_modes.lp_advertising,
123 NULL, __ETHTOOL_LINK_MODE_MASK_NBITS,
124 link_mode_names, compact);
129 if (nla_put_u32(skb, ETHTOOL_A_LINKMODES_SPEED, lsettings->speed) ||
130 nla_put_u8(skb, ETHTOOL_A_LINKMODES_DUPLEX, lsettings->duplex))
133 if (ksettings->lanes &&
134 nla_put_u32(skb, ETHTOOL_A_LINKMODES_LANES, ksettings->lanes))
137 if (lsettings->master_slave_cfg != MASTER_SLAVE_CFG_UNSUPPORTED &&
138 nla_put_u8(skb, ETHTOOL_A_LINKMODES_MASTER_SLAVE_CFG,
139 lsettings->master_slave_cfg))
142 if (lsettings->master_slave_state != MASTER_SLAVE_STATE_UNSUPPORTED &&
143 nla_put_u8(skb, ETHTOOL_A_LINKMODES_MASTER_SLAVE_STATE,
144 lsettings->master_slave_state))
147 if (nla_put_u8(skb, ETHTOOL_A_LINKMODES_RATE_MATCHING,
148 lsettings->rate_matching))
156 const struct nla_policy ethnl_linkmodes_set_policy[] = {
157 [ETHTOOL_A_LINKMODES_HEADER] =
158 NLA_POLICY_NESTED(ethnl_header_policy),
159 [ETHTOOL_A_LINKMODES_AUTONEG] = { .type = NLA_U8 },
160 [ETHTOOL_A_LINKMODES_OURS] = { .type = NLA_NESTED },
161 [ETHTOOL_A_LINKMODES_SPEED] = { .type = NLA_U32 },
162 [ETHTOOL_A_LINKMODES_DUPLEX] = { .type = NLA_U8 },
163 [ETHTOOL_A_LINKMODES_MASTER_SLAVE_CFG] = { .type = NLA_U8 },
164 [ETHTOOL_A_LINKMODES_LANES] = NLA_POLICY_RANGE(NLA_U32, 1, 8),
167 /* Set advertised link modes to all supported modes matching requested speed,
168 * lanes and duplex values. Called when autonegotiation is on, speed, lanes or
169 * duplex is requested but no link mode change. This is done in userspace with
170 * ioctl() interface, move it into kernel for netlink.
171 * Returns true if advertised modes bitmap was modified.
173 static bool ethnl_auto_linkmodes(struct ethtool_link_ksettings *ksettings,
174 bool req_speed, bool req_lanes, bool req_duplex)
176 unsigned long *advertising = ksettings->link_modes.advertising;
177 unsigned long *supported = ksettings->link_modes.supported;
178 DECLARE_BITMAP(old_adv, __ETHTOOL_LINK_MODE_MASK_NBITS);
181 bitmap_copy(old_adv, advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
183 for (i = 0; i < __ETHTOOL_LINK_MODE_MASK_NBITS; i++) {
184 const struct link_mode_info *info = &link_mode_params[i];
186 if (info->speed == SPEED_UNKNOWN)
188 if (test_bit(i, supported) &&
189 (!req_speed || info->speed == ksettings->base.speed) &&
190 (!req_lanes || info->lanes == ksettings->lanes) &&
191 (!req_duplex || info->duplex == ksettings->base.duplex))
192 set_bit(i, advertising);
194 clear_bit(i, advertising);
197 return !bitmap_equal(old_adv, advertising,
198 __ETHTOOL_LINK_MODE_MASK_NBITS);
201 static bool ethnl_validate_master_slave_cfg(u8 cfg)
204 case MASTER_SLAVE_CFG_MASTER_PREFERRED:
205 case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
206 case MASTER_SLAVE_CFG_MASTER_FORCE:
207 case MASTER_SLAVE_CFG_SLAVE_FORCE:
214 static int ethnl_check_linkmodes(struct genl_info *info, struct nlattr **tb)
216 const struct nlattr *master_slave_cfg, *lanes_cfg;
218 master_slave_cfg = tb[ETHTOOL_A_LINKMODES_MASTER_SLAVE_CFG];
219 if (master_slave_cfg &&
220 !ethnl_validate_master_slave_cfg(nla_get_u8(master_slave_cfg))) {
221 NL_SET_ERR_MSG_ATTR(info->extack, master_slave_cfg,
222 "master/slave value is invalid");
226 lanes_cfg = tb[ETHTOOL_A_LINKMODES_LANES];
227 if (lanes_cfg && !is_power_of_2(nla_get_u32(lanes_cfg))) {
228 NL_SET_ERR_MSG_ATTR(info->extack, lanes_cfg,
229 "lanes value is invalid");
236 static int ethnl_update_linkmodes(struct genl_info *info, struct nlattr **tb,
237 struct ethtool_link_ksettings *ksettings,
238 bool *mod, const struct net_device *dev)
240 struct ethtool_link_settings *lsettings = &ksettings->base;
241 bool req_speed, req_lanes, req_duplex;
242 const struct nlattr *master_slave_cfg, *lanes_cfg;
245 master_slave_cfg = tb[ETHTOOL_A_LINKMODES_MASTER_SLAVE_CFG];
246 if (master_slave_cfg) {
247 if (lsettings->master_slave_cfg == MASTER_SLAVE_CFG_UNSUPPORTED) {
248 NL_SET_ERR_MSG_ATTR(info->extack, master_slave_cfg,
249 "master/slave configuration not supported by device");
255 req_speed = tb[ETHTOOL_A_LINKMODES_SPEED];
256 req_lanes = tb[ETHTOOL_A_LINKMODES_LANES];
257 req_duplex = tb[ETHTOOL_A_LINKMODES_DUPLEX];
259 ethnl_update_u8(&lsettings->autoneg, tb[ETHTOOL_A_LINKMODES_AUTONEG],
262 lanes_cfg = tb[ETHTOOL_A_LINKMODES_LANES];
264 /* If autoneg is off and lanes parameter is not supported by the
265 * driver, return an error.
267 if (!lsettings->autoneg &&
268 !dev->ethtool_ops->cap_link_lanes_supported) {
269 NL_SET_ERR_MSG_ATTR(info->extack, lanes_cfg,
270 "lanes configuration not supported by device");
273 } else if (!lsettings->autoneg && ksettings->lanes) {
274 /* If autoneg is off and lanes parameter is not passed from user but
275 * it was defined previously then set the lanes parameter to 0.
277 ksettings->lanes = 0;
281 ret = ethnl_update_bitset(ksettings->link_modes.advertising,
282 __ETHTOOL_LINK_MODE_MASK_NBITS,
283 tb[ETHTOOL_A_LINKMODES_OURS], link_mode_names,
287 ethnl_update_u32(&lsettings->speed, tb[ETHTOOL_A_LINKMODES_SPEED],
289 ethnl_update_u32(&ksettings->lanes, lanes_cfg, mod);
290 ethnl_update_u8(&lsettings->duplex, tb[ETHTOOL_A_LINKMODES_DUPLEX],
292 ethnl_update_u8(&lsettings->master_slave_cfg, master_slave_cfg, mod);
294 if (!tb[ETHTOOL_A_LINKMODES_OURS] && lsettings->autoneg &&
295 (req_speed || req_lanes || req_duplex) &&
296 ethnl_auto_linkmodes(ksettings, req_speed, req_lanes, req_duplex))
303 ethnl_set_linkmodes_validate(struct ethnl_req_info *req_info,
304 struct genl_info *info)
306 const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
309 ret = ethnl_check_linkmodes(info, info->attrs);
313 if (!ops->get_link_ksettings || !ops->set_link_ksettings)
319 ethnl_set_linkmodes(struct ethnl_req_info *req_info, struct genl_info *info)
321 struct ethtool_link_ksettings ksettings = {};
322 struct net_device *dev = req_info->dev;
323 struct nlattr **tb = info->attrs;
327 ret = __ethtool_get_link_ksettings(dev, &ksettings);
329 GENL_SET_ERR_MSG(info, "failed to retrieve link settings");
333 ret = ethnl_update_linkmodes(info, tb, &ksettings, &mod, dev);
339 ret = dev->ethtool_ops->set_link_ksettings(dev, &ksettings);
341 GENL_SET_ERR_MSG(info, "link settings update failed");
348 const struct ethnl_request_ops ethnl_linkmodes_request_ops = {
349 .request_cmd = ETHTOOL_MSG_LINKMODES_GET,
350 .reply_cmd = ETHTOOL_MSG_LINKMODES_GET_REPLY,
351 .hdr_attr = ETHTOOL_A_LINKMODES_HEADER,
352 .req_info_size = sizeof(struct linkmodes_req_info),
353 .reply_data_size = sizeof(struct linkmodes_reply_data),
355 .prepare_data = linkmodes_prepare_data,
356 .reply_size = linkmodes_reply_size,
357 .fill_reply = linkmodes_fill_reply,
359 .set_validate = ethnl_set_linkmodes_validate,
360 .set = ethnl_set_linkmodes,
361 .set_ntf_cmd = ETHTOOL_MSG_LINKMODES_NTF,