The PHY does not properly support the Clause 45 auto-negotiation registers that phylib writes from genphy_c45_config_aneg(). As a consequence, restricting the advertisement from userspace does not have the expected outcome on the wire. For example, setting it to 2500baseT/Full will confuse the peer, as 5000baseT/Full still shows up in the link partner advertised link modes. Even worse, setting it to 100baseT/Full + 1000baseT/Full will end up with a broken link, because the peer would only see the unrelated 2500baseT/Full + 5000baseT/Full modes. Since the firmware owns the advertisement and is reachable over IPC, implement the .config_aneg callback and pass the highest advertised speed. Cache it and only restart autoneg when it changes, as genphy_c45_config_aneg() does, so that phy_start_aneg() does not take the link down unnecessarily. Forced mode invalidates the cache so that autoneg is restarted once it is re-enabled. So does .config_init, since the firmware might have lost the setting, e.g. across a suspend/resume cycle. Note that the related IPC command only takes a top speed, and every slower speed the PHY supports is advertised along with it. An arbitrary subset cannot be described yet, so a link can still be negotiated below the slowest speed that was asked for. Add those slower speeds to phydev->advertising so that it reflects what is actually advertised on the wire. If no BASE-T speed is advertised, which ethtool still allows, fall back to the slowest supported one rather than leaving the PHY in the error state. Fixes: 830877d89edc ("net: phy: Add support for Aeonsemi AS21xxx PHYs") Signed-off-by: Cristian Ciocaltea --- drivers/net/phy/as21xxx.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) diff --git a/drivers/net/phy/as21xxx.c b/drivers/net/phy/as21xxx.c index 3423448a0cb1..967ff3020814 100644 --- a/drivers/net/phy/as21xxx.c +++ b/drivers/net/phy/as21xxx.c @@ -112,6 +112,18 @@ #define IPC_CFG_PARAM_DIRECT_TEMP_MON 0x11 #define IPC_CFG_PARAM_DIRECT_WOL 0x12 +/* Sub command of IPC_CFG_PARAM_DIRECT_CU_AN */ +#define IPC_CU_AN_RESTART 0xa +#define IPC_CU_AN_TOP_SPD 0xc + +/* Argument of IPC_CU_AN_TOP_SPD */ +#define IPC_CU_AN_TOP_SPD_10 BIT(1) +#define IPC_CU_AN_TOP_SPD_100 BIT(2) +#define IPC_CU_AN_TOP_SPD_1000 BIT(3) +#define IPC_CU_AN_TOP_SPD_2500 BIT(4) +#define IPC_CU_AN_TOP_SPD_5000 BIT(5) +#define IPC_CU_AN_TOP_SPD_10000 BIT(6) + /* Sub command of CMD_TEMP_MON */ #define IPC_CMD_TEMP_MON_GET 0x4 @@ -186,6 +198,11 @@ struct as21xxx_priv { bool parity_status; /* Protect concurrent IPC access */ struct mutex ipc_lock; + /* Top speed last handed to the firmware, 0 when it has to be sent + * again. Updated under phydev->lock, or while the state machine is + * stopped. + */ + u16 top_spd; }; static struct as21xxx_led_pattern_info as21xxx_led_supported_pattern[] = { @@ -841,6 +858,138 @@ static int as21xxx_read_status(struct phy_device *phydev) return 0; } +static int aeon_restart_aneg(struct phy_device *phydev) +{ + u16 data[3]; + + data[0] = IPC_CFG_PARAM_DIRECT; + data[1] = IPC_CFG_PARAM_DIRECT_CU_AN; + data[2] = IPC_CU_AN_RESTART; + + return aeon_ipc_send_msg(phydev, IPC_CMD_CFG_PARAM, data, + sizeof(data), NULL); +} + +static int aeon_set_top_speed(struct phy_device *phydev, u16 top_spd) +{ + u16 data[4]; + + data[0] = IPC_CFG_PARAM_DIRECT; + data[1] = IPC_CFG_PARAM_DIRECT_CU_AN; + data[2] = IPC_CU_AN_TOP_SPD; + data[3] = top_spd; + + return aeon_ipc_send_msg(phydev, IPC_CMD_CFG_PARAM, data, + sizeof(data), NULL); +} + +/* Highest speed first, the firmware is given the fastest advertised one */ +static const struct { + u16 top_spd; + int linkmode; +} aeon_top_speeds[] = { + { IPC_CU_AN_TOP_SPD_10000, ETHTOOL_LINK_MODE_10000baseT_Full_BIT }, + { IPC_CU_AN_TOP_SPD_5000, ETHTOOL_LINK_MODE_5000baseT_Full_BIT }, + { IPC_CU_AN_TOP_SPD_2500, ETHTOOL_LINK_MODE_2500baseT_Full_BIT }, + { IPC_CU_AN_TOP_SPD_1000, ETHTOOL_LINK_MODE_1000baseT_Full_BIT }, + { IPC_CU_AN_TOP_SPD_1000, ETHTOOL_LINK_MODE_1000baseT_Half_BIT }, + { IPC_CU_AN_TOP_SPD_100, ETHTOOL_LINK_MODE_100baseT_Full_BIT }, + { IPC_CU_AN_TOP_SPD_100, ETHTOOL_LINK_MODE_100baseT_Half_BIT }, + { IPC_CU_AN_TOP_SPD_10, ETHTOOL_LINK_MODE_10baseT_Full_BIT }, + { IPC_CU_AN_TOP_SPD_10, ETHTOOL_LINK_MODE_10baseT_Half_BIT }, +}; + +static int as21xxx_config_init(struct phy_device *phydev) +{ + struct as21xxx_priv *priv = phydev->priv; + + /* The firmware might have lost the top speed, e.g. across a + * suspend/resume cycle, make sure the next .config_aneg sends it. + */ + priv->top_spd = 0; + + return 0; +} + +static int as21xxx_config_aneg(struct phy_device *phydev) +{ + struct as21xxx_priv *priv = phydev->priv; + u16 top_spd; + int ret, i; + + if (phydev->autoneg == AUTONEG_DISABLE) { + /* Make sure autoneg is restarted once enabled again */ + priv->top_spd = 0; + + return genphy_c45_pma_setup_forced(phydev); + } + + /* The firmware doesn't properly support the C45 autoneg registers, so + * it has to be told over the IPC instead. Only the highest speed can + * be described at the moment, the slower ones the PHY supports are + * implicitly advertised along with it. + */ + for (i = 0; i < ARRAY_SIZE(aeon_top_speeds); i++) + if (linkmode_test_bit(aeon_top_speeds[i].linkmode, + phydev->advertising)) + break; + + /* None of the BASE-T speeds is advertised, e.g. after + * "ethtool -s ... autoneg on speed 10" on a PHY lacking 10M. The IPC + * cannot advertise nothing, and an error would only put the PHY in + * the error state, so fall back to the slowest supported speed. + */ + if (i == ARRAY_SIZE(aeon_top_speeds)) { + while (--i >= 0 && + !linkmode_test_bit(aeon_top_speeds[i].linkmode, + phydev->supported)) + ; + + if (i < 0) + return -EINVAL; + } + + top_spd = aeon_top_speeds[i].top_spd; + + /* Let the advertisement reflect the slower speeds which go on the + * wire along with the highest one. + */ + for (i = 0; i < ARRAY_SIZE(aeon_top_speeds); i++) + if (aeon_top_speeds[i].top_spd <= top_spd && + linkmode_test_bit(aeon_top_speeds[i].linkmode, + phydev->supported)) + linkmode_set_bit(aeon_top_speeds[i].linkmode, + phydev->advertising); + + /* Restarting autoneg takes the link down, so only do it when the + * advertisement changed, or has to be sent again. + */ + if (top_spd == priv->top_spd) + return 0; + + ret = aeon_set_top_speed(phydev, top_spd); + if (ret) + return ret; + + if (!priv->top_spd) { + /* genphy_c45_pma_setup_forced() clears MDIO_AN_CTRL1_ENABLE, + * which restarting autoneg over the IPC doesn't set back. + */ + ret = phy_set_bits_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, + MDIO_AN_CTRL1_ENABLE); + if (ret) + return ret; + } + + ret = aeon_restart_aneg(phydev); + if (ret) + return ret; + + priv->top_spd = top_spd; + + return 0; +} + static int as21xxx_led_brightness_set(struct phy_device *phydev, u8 index, enum led_brightness value) { @@ -1037,6 +1186,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, @@ -1050,6 +1201,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, @@ -1063,6 +1216,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, @@ -1076,6 +1231,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, @@ -1089,6 +1246,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, @@ -1102,6 +1261,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, @@ -1115,6 +1276,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, @@ -1128,6 +1291,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, @@ -1141,6 +1306,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, @@ -1154,6 +1321,8 @@ static struct phy_driver as21xxx_drivers[] = { .match_phy_device = as21xxx_match_phy_device, .read_status = as21xxx_read_status, .get_features = as21xxx_get_features, + .config_init = as21xxx_config_init, + .config_aneg = as21xxx_config_aneg, .led_brightness_set = as21xxx_led_brightness_set, .led_hw_is_supported = as21xxx_led_hw_is_supported, .led_hw_control_set = as21xxx_led_hw_control_set, -- 2.56.0