2019-12-27 22:56:08 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
#include "netlink.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "bitset.h"
|
|
|
|
|
2021-02-03 02:06:07 +08:00
|
|
|
/* LINKMODES_GET */
|
|
|
|
|
2019-12-27 22:56:08 +08:00
|
|
|
struct linkmodes_req_info {
|
|
|
|
struct ethnl_req_info base;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct linkmodes_reply_data {
|
|
|
|
struct ethnl_reply_data base;
|
|
|
|
struct ethtool_link_ksettings ksettings;
|
|
|
|
struct ethtool_link_settings *lsettings;
|
|
|
|
bool peer_empty;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LINKMODES_REPDATA(__reply_base) \
|
|
|
|
container_of(__reply_base, struct linkmodes_reply_data, base)
|
|
|
|
|
2020-10-06 06:07:35 +08:00
|
|
|
const struct nla_policy ethnl_linkmodes_get_policy[] = {
|
2020-10-06 06:07:36 +08:00
|
|
|
[ETHTOOL_A_LINKMODES_HEADER] =
|
|
|
|
NLA_POLICY_NESTED(ethnl_header_policy),
|
2019-12-27 22:56:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int linkmodes_prepare_data(const struct ethnl_req_info *req_base,
|
|
|
|
struct ethnl_reply_data *reply_base,
|
|
|
|
struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct linkmodes_reply_data *data = LINKMODES_REPDATA(reply_base);
|
|
|
|
struct net_device *dev = reply_base->dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
data->lsettings = &data->ksettings.base;
|
|
|
|
|
|
|
|
ret = ethnl_ops_begin(dev);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = __ethtool_get_link_ksettings(dev, &data->ksettings);
|
|
|
|
if (ret < 0 && info) {
|
|
|
|
GENL_SET_ERR_MSG(info, "failed to retrieve link settings");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-02-03 02:06:08 +08:00
|
|
|
if (!dev->ethtool_ops->cap_link_lanes_supported)
|
|
|
|
data->ksettings.lanes = 0;
|
|
|
|
|
2019-12-27 22:56:08 +08:00
|
|
|
data->peer_empty =
|
|
|
|
bitmap_empty(data->ksettings.link_modes.lp_advertising,
|
|
|
|
__ETHTOOL_LINK_MODE_MASK_NBITS);
|
|
|
|
|
|
|
|
out:
|
|
|
|
ethnl_ops_complete(dev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int linkmodes_reply_size(const struct ethnl_req_info *req_base,
|
|
|
|
const struct ethnl_reply_data *reply_base)
|
|
|
|
{
|
|
|
|
const struct linkmodes_reply_data *data = LINKMODES_REPDATA(reply_base);
|
|
|
|
const struct ethtool_link_ksettings *ksettings = &data->ksettings;
|
2020-05-05 14:35:05 +08:00
|
|
|
const struct ethtool_link_settings *lsettings = &ksettings->base;
|
2019-12-27 22:56:08 +08:00
|
|
|
bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
|
|
|
|
int len, ret;
|
|
|
|
|
|
|
|
len = nla_total_size(sizeof(u8)) /* LINKMODES_AUTONEG */
|
|
|
|
+ nla_total_size(sizeof(u32)) /* LINKMODES_SPEED */
|
2021-02-03 02:06:08 +08:00
|
|
|
+ nla_total_size(sizeof(u32)) /* LINKMODES_LANES */
|
2019-12-27 22:56:08 +08:00
|
|
|
+ nla_total_size(sizeof(u8)) /* LINKMODES_DUPLEX */
|
|
|
|
+ 0;
|
|
|
|
ret = ethnl_bitset_size(ksettings->link_modes.advertising,
|
|
|
|
ksettings->link_modes.supported,
|
|
|
|
__ETHTOOL_LINK_MODE_MASK_NBITS,
|
|
|
|
link_mode_names, compact);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
len += ret;
|
|
|
|
if (!data->peer_empty) {
|
|
|
|
ret = ethnl_bitset_size(ksettings->link_modes.lp_advertising,
|
|
|
|
NULL, __ETHTOOL_LINK_MODE_MASK_NBITS,
|
|
|
|
link_mode_names, compact);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
len += ret;
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:35:05 +08:00
|
|
|
if (lsettings->master_slave_cfg != MASTER_SLAVE_CFG_UNSUPPORTED)
|
|
|
|
len += nla_total_size(sizeof(u8));
|
|
|
|
|
|
|
|
if (lsettings->master_slave_state != MASTER_SLAVE_STATE_UNSUPPORTED)
|
|
|
|
len += nla_total_size(sizeof(u8));
|
|
|
|
|
2019-12-27 22:56:08 +08:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int linkmodes_fill_reply(struct sk_buff *skb,
|
|
|
|
const struct ethnl_req_info *req_base,
|
|
|
|
const struct ethnl_reply_data *reply_base)
|
|
|
|
{
|
|
|
|
const struct linkmodes_reply_data *data = LINKMODES_REPDATA(reply_base);
|
|
|
|
const struct ethtool_link_ksettings *ksettings = &data->ksettings;
|
|
|
|
const struct ethtool_link_settings *lsettings = &ksettings->base;
|
|
|
|
bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (nla_put_u8(skb, ETHTOOL_A_LINKMODES_AUTONEG, lsettings->autoneg))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
|
|
|
ret = ethnl_put_bitset(skb, ETHTOOL_A_LINKMODES_OURS,
|
|
|
|
ksettings->link_modes.advertising,
|
|
|
|
ksettings->link_modes.supported,
|
|
|
|
__ETHTOOL_LINK_MODE_MASK_NBITS, link_mode_names,
|
|
|
|
compact);
|
|
|
|
if (ret < 0)
|
|
|
|
return -EMSGSIZE;
|
|
|
|
if (!data->peer_empty) {
|
|
|
|
ret = ethnl_put_bitset(skb, ETHTOOL_A_LINKMODES_PEER,
|
|
|
|
ksettings->link_modes.lp_advertising,
|
|
|
|
NULL, __ETHTOOL_LINK_MODE_MASK_NBITS,
|
|
|
|
link_mode_names, compact);
|
|
|
|
if (ret < 0)
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nla_put_u32(skb, ETHTOOL_A_LINKMODES_SPEED, lsettings->speed) ||
|
|
|
|
nla_put_u8(skb, ETHTOOL_A_LINKMODES_DUPLEX, lsettings->duplex))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2021-02-03 02:06:08 +08:00
|
|
|
if (ksettings->lanes &&
|
|
|
|
nla_put_u32(skb, ETHTOOL_A_LINKMODES_LANES, ksettings->lanes))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2020-05-05 14:35:05 +08:00
|
|
|
if (lsettings->master_slave_cfg != MASTER_SLAVE_CFG_UNSUPPORTED &&
|
|
|
|
nla_put_u8(skb, ETHTOOL_A_LINKMODES_MASTER_SLAVE_CFG,
|
|
|
|
lsettings->master_slave_cfg))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
|
|
|
if (lsettings->master_slave_state != MASTER_SLAVE_STATE_UNSUPPORTED &&
|
|
|
|
nla_put_u8(skb, ETHTOOL_A_LINKMODES_MASTER_SLAVE_STATE,
|
|
|
|
lsettings->master_slave_state))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2019-12-27 22:56:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct ethnl_request_ops ethnl_linkmodes_request_ops = {
|
|
|
|
.request_cmd = ETHTOOL_MSG_LINKMODES_GET,
|
|
|
|
.reply_cmd = ETHTOOL_MSG_LINKMODES_GET_REPLY,
|
|
|
|
.hdr_attr = ETHTOOL_A_LINKMODES_HEADER,
|
|
|
|
.req_info_size = sizeof(struct linkmodes_req_info),
|
|
|
|
.reply_data_size = sizeof(struct linkmodes_reply_data),
|
|
|
|
|
|
|
|
.prepare_data = linkmodes_prepare_data,
|
|
|
|
.reply_size = linkmodes_reply_size,
|
|
|
|
.fill_reply = linkmodes_fill_reply,
|
|
|
|
};
|
2019-12-27 22:56:13 +08:00
|
|
|
|
|
|
|
/* LINKMODES_SET */
|
|
|
|
|
2020-10-06 06:07:35 +08:00
|
|
|
const struct nla_policy ethnl_linkmodes_set_policy[] = {
|
2020-10-06 06:07:36 +08:00
|
|
|
[ETHTOOL_A_LINKMODES_HEADER] =
|
|
|
|
NLA_POLICY_NESTED(ethnl_header_policy),
|
2019-12-27 22:56:13 +08:00
|
|
|
[ETHTOOL_A_LINKMODES_AUTONEG] = { .type = NLA_U8 },
|
|
|
|
[ETHTOOL_A_LINKMODES_OURS] = { .type = NLA_NESTED },
|
|
|
|
[ETHTOOL_A_LINKMODES_SPEED] = { .type = NLA_U32 },
|
|
|
|
[ETHTOOL_A_LINKMODES_DUPLEX] = { .type = NLA_U8 },
|
2020-05-05 14:35:05 +08:00
|
|
|
[ETHTOOL_A_LINKMODES_MASTER_SLAVE_CFG] = { .type = NLA_U8 },
|
2021-02-03 02:06:06 +08:00
|
|
|
[ETHTOOL_A_LINKMODES_LANES] = NLA_POLICY_RANGE(NLA_U32, 1, 8),
|
2019-12-27 22:56:13 +08:00
|
|
|
};
|
|
|
|
|
2021-02-03 02:06:06 +08:00
|
|
|
/* Set advertised link modes to all supported modes matching requested speed,
|
|
|
|
* lanes and duplex values. Called when autonegotiation is on, speed, lanes or
|
|
|
|
* duplex is requested but no link mode change. This is done in userspace with
|
|
|
|
* ioctl() interface, move it into kernel for netlink.
|
2019-12-27 22:56:13 +08:00
|
|
|
* Returns true if advertised modes bitmap was modified.
|
|
|
|
*/
|
|
|
|
static bool ethnl_auto_linkmodes(struct ethtool_link_ksettings *ksettings,
|
2021-02-03 02:06:06 +08:00
|
|
|
bool req_speed, bool req_lanes, bool req_duplex)
|
2019-12-27 22:56:13 +08:00
|
|
|
{
|
|
|
|
unsigned long *advertising = ksettings->link_modes.advertising;
|
|
|
|
unsigned long *supported = ksettings->link_modes.supported;
|
|
|
|
DECLARE_BITMAP(old_adv, __ETHTOOL_LINK_MODE_MASK_NBITS);
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
bitmap_copy(old_adv, advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
|
|
|
|
|
|
|
|
for (i = 0; i < __ETHTOOL_LINK_MODE_MASK_NBITS; i++) {
|
|
|
|
const struct link_mode_info *info = &link_mode_params[i];
|
|
|
|
|
|
|
|
if (info->speed == SPEED_UNKNOWN)
|
|
|
|
continue;
|
|
|
|
if (test_bit(i, supported) &&
|
|
|
|
(!req_speed || info->speed == ksettings->base.speed) &&
|
2021-02-03 02:06:06 +08:00
|
|
|
(!req_lanes || info->lanes == ksettings->lanes) &&
|
2019-12-27 22:56:13 +08:00
|
|
|
(!req_duplex || info->duplex == ksettings->base.duplex))
|
|
|
|
set_bit(i, advertising);
|
|
|
|
else
|
|
|
|
clear_bit(i, advertising);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !bitmap_equal(old_adv, advertising,
|
|
|
|
__ETHTOOL_LINK_MODE_MASK_NBITS);
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:35:05 +08:00
|
|
|
static bool ethnl_validate_master_slave_cfg(u8 cfg)
|
|
|
|
{
|
|
|
|
switch (cfg) {
|
|
|
|
case MASTER_SLAVE_CFG_MASTER_PREFERRED:
|
|
|
|
case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
|
|
|
|
case MASTER_SLAVE_CFG_MASTER_FORCE:
|
|
|
|
case MASTER_SLAVE_CFG_SLAVE_FORCE:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-03 02:06:05 +08:00
|
|
|
static int ethnl_check_linkmodes(struct genl_info *info, struct nlattr **tb)
|
|
|
|
{
|
2021-02-03 02:06:06 +08:00
|
|
|
const struct nlattr *master_slave_cfg, *lanes_cfg;
|
2021-02-03 02:06:05 +08:00
|
|
|
|
|
|
|
master_slave_cfg = tb[ETHTOOL_A_LINKMODES_MASTER_SLAVE_CFG];
|
|
|
|
if (master_slave_cfg &&
|
|
|
|
!ethnl_validate_master_slave_cfg(nla_get_u8(master_slave_cfg))) {
|
|
|
|
NL_SET_ERR_MSG_ATTR(info->extack, master_slave_cfg,
|
|
|
|
"master/slave value is invalid");
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2021-02-03 02:06:06 +08:00
|
|
|
lanes_cfg = tb[ETHTOOL_A_LINKMODES_LANES];
|
|
|
|
if (lanes_cfg && !is_power_of_2(nla_get_u32(lanes_cfg))) {
|
|
|
|
NL_SET_ERR_MSG_ATTR(info->extack, lanes_cfg,
|
|
|
|
"lanes value is invalid");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-02-03 02:06:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-27 22:56:13 +08:00
|
|
|
static int ethnl_update_linkmodes(struct genl_info *info, struct nlattr **tb,
|
|
|
|
struct ethtool_link_ksettings *ksettings,
|
2021-02-03 02:06:06 +08:00
|
|
|
bool *mod, const struct net_device *dev)
|
2019-12-27 22:56:13 +08:00
|
|
|
{
|
|
|
|
struct ethtool_link_settings *lsettings = &ksettings->base;
|
2021-02-03 02:06:06 +08:00
|
|
|
bool req_speed, req_lanes, req_duplex;
|
|
|
|
const struct nlattr *master_slave_cfg, *lanes_cfg;
|
2019-12-27 22:56:13 +08:00
|
|
|
int ret;
|
|
|
|
|
2020-05-05 14:35:05 +08:00
|
|
|
master_slave_cfg = tb[ETHTOOL_A_LINKMODES_MASTER_SLAVE_CFG];
|
|
|
|
if (master_slave_cfg) {
|
|
|
|
if (lsettings->master_slave_cfg == MASTER_SLAVE_CFG_UNSUPPORTED) {
|
|
|
|
NL_SET_ERR_MSG_ATTR(info->extack, master_slave_cfg,
|
|
|
|
"master/slave configuration not supported by device");
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 22:56:13 +08:00
|
|
|
*mod = false;
|
|
|
|
req_speed = tb[ETHTOOL_A_LINKMODES_SPEED];
|
2021-02-03 02:06:06 +08:00
|
|
|
req_lanes = tb[ETHTOOL_A_LINKMODES_LANES];
|
2019-12-27 22:56:13 +08:00
|
|
|
req_duplex = tb[ETHTOOL_A_LINKMODES_DUPLEX];
|
|
|
|
|
|
|
|
ethnl_update_u8(&lsettings->autoneg, tb[ETHTOOL_A_LINKMODES_AUTONEG],
|
|
|
|
mod);
|
2021-02-03 02:06:06 +08:00
|
|
|
|
|
|
|
lanes_cfg = tb[ETHTOOL_A_LINKMODES_LANES];
|
|
|
|
if (lanes_cfg) {
|
|
|
|
/* If autoneg is off and lanes parameter is not supported by the
|
|
|
|
* driver, return an error.
|
|
|
|
*/
|
|
|
|
if (!lsettings->autoneg &&
|
|
|
|
!dev->ethtool_ops->cap_link_lanes_supported) {
|
|
|
|
NL_SET_ERR_MSG_ATTR(info->extack, lanes_cfg,
|
|
|
|
"lanes configuration not supported by device");
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
} else if (!lsettings->autoneg) {
|
|
|
|
/* If autoneg is off and lanes parameter is not passed from user,
|
|
|
|
* set the lanes parameter to 0.
|
|
|
|
*/
|
|
|
|
ksettings->lanes = 0;
|
|
|
|
}
|
|
|
|
|
2019-12-27 22:56:13 +08:00
|
|
|
ret = ethnl_update_bitset(ksettings->link_modes.advertising,
|
|
|
|
__ETHTOOL_LINK_MODE_MASK_NBITS,
|
|
|
|
tb[ETHTOOL_A_LINKMODES_OURS], link_mode_names,
|
|
|
|
info->extack, mod);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ethnl_update_u32(&lsettings->speed, tb[ETHTOOL_A_LINKMODES_SPEED],
|
|
|
|
mod);
|
2021-02-03 02:06:06 +08:00
|
|
|
ethnl_update_u32(&ksettings->lanes, lanes_cfg, mod);
|
2019-12-27 22:56:13 +08:00
|
|
|
ethnl_update_u8(&lsettings->duplex, tb[ETHTOOL_A_LINKMODES_DUPLEX],
|
|
|
|
mod);
|
2020-05-05 14:35:05 +08:00
|
|
|
ethnl_update_u8(&lsettings->master_slave_cfg, master_slave_cfg, mod);
|
2019-12-27 22:56:13 +08:00
|
|
|
|
|
|
|
if (!tb[ETHTOOL_A_LINKMODES_OURS] && lsettings->autoneg &&
|
2021-02-03 02:06:06 +08:00
|
|
|
(req_speed || req_lanes || req_duplex) &&
|
|
|
|
ethnl_auto_linkmodes(ksettings, req_speed, req_lanes, req_duplex))
|
2019-12-27 22:56:13 +08:00
|
|
|
*mod = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ethnl_set_linkmodes(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct ethtool_link_ksettings ksettings = {};
|
|
|
|
struct ethnl_req_info req_info = {};
|
2020-10-06 06:07:34 +08:00
|
|
|
struct nlattr **tb = info->attrs;
|
2019-12-27 22:56:13 +08:00
|
|
|
struct net_device *dev;
|
|
|
|
bool mod = false;
|
|
|
|
int ret;
|
|
|
|
|
2021-02-03 02:06:05 +08:00
|
|
|
ret = ethnl_check_linkmodes(info, tb);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2020-03-13 04:07:38 +08:00
|
|
|
ret = ethnl_parse_header_dev_get(&req_info,
|
|
|
|
tb[ETHTOOL_A_LINKMODES_HEADER],
|
|
|
|
genl_info_net(info), info->extack,
|
|
|
|
true);
|
2019-12-27 22:56:13 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
dev = req_info.dev;
|
2020-03-23 05:24:21 +08:00
|
|
|
ret = -EOPNOTSUPP;
|
2019-12-27 22:56:13 +08:00
|
|
|
if (!dev->ethtool_ops->get_link_ksettings ||
|
|
|
|
!dev->ethtool_ops->set_link_ksettings)
|
2020-03-23 05:24:21 +08:00
|
|
|
goto out_dev;
|
2019-12-27 22:56:13 +08:00
|
|
|
|
|
|
|
rtnl_lock();
|
|
|
|
ret = ethnl_ops_begin(dev);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_rtnl;
|
|
|
|
|
|
|
|
ret = __ethtool_get_link_ksettings(dev, &ksettings);
|
|
|
|
if (ret < 0) {
|
2020-07-31 12:58:44 +08:00
|
|
|
GENL_SET_ERR_MSG(info, "failed to retrieve link settings");
|
2019-12-27 22:56:13 +08:00
|
|
|
goto out_ops;
|
|
|
|
}
|
|
|
|
|
2021-02-03 02:06:06 +08:00
|
|
|
ret = ethnl_update_linkmodes(info, tb, &ksettings, &mod, dev);
|
2019-12-27 22:56:13 +08:00
|
|
|
if (ret < 0)
|
|
|
|
goto out_ops;
|
|
|
|
|
|
|
|
if (mod) {
|
|
|
|
ret = dev->ethtool_ops->set_link_ksettings(dev, &ksettings);
|
|
|
|
if (ret < 0)
|
|
|
|
GENL_SET_ERR_MSG(info, "link settings update failed");
|
2019-12-27 22:56:18 +08:00
|
|
|
else
|
|
|
|
ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
|
2019-12-27 22:56:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out_ops:
|
|
|
|
ethnl_ops_complete(dev);
|
|
|
|
out_rtnl:
|
|
|
|
rtnl_unlock();
|
2020-03-23 05:24:21 +08:00
|
|
|
out_dev:
|
2021-12-14 16:42:30 +08:00
|
|
|
ethnl_parse_header_dev_put(&req_info);
|
2019-12-27 22:56:13 +08:00
|
|
|
return ret;
|
|
|
|
}
|