With autoneg enabled, as21xxx_read_status() derives the result purely in software via phy_resolve_aneg_linkmode(). That does not work reliably here: the advertisement is driven by the firmware over IPC, which ignores the Clause 45 AN registers phylib writes except for the maximum speed, and arbitrary subsets of speeds are not supported. The speed the firmware actually settles on can therefore differ from what resolving the advertised and link partner abilities yields. Use VEND1_SPEED_STATUS, the authoritative source, on the autoneg path too. Since aeon_config_aneg() makes phydev->advertising reflect what the firmware puts on the wire, it is still reliable for picking the duplex mode at that speed: resolve duplex from the link partner advertisement (2.5GBASE-T and above are full duplex only), then resolve pause. Parallel detection at 10/100M implies half duplex, unless the PHY lacks it. The link partner abilities are read from the Clause 22 registers mapped in the AN MMD (base page and 1000BASE-T status), plus the 10GBASE-T status register for 2.5G/5G/10G. Rename as21xxx_read_c22_lpa() to as21xxx_read_lpa() accordingly, as it no longer covers Clause 22 only. Speed and link partner abilities are only read once the link is up and are reported unknown until then. An unrecognised speed status holds the link down instead of failing read_status, which would move the PHY to PHY_ERROR. I/O errors are still propagated. With autoneg disabled, report the settings programmed by genphy_c45_pma_setup_forced() via genphy_c45_read_pma(), regardless of link state. Otherwise phy_sanitize_settings() sees unknown values on the next autoneg restart with the link down and falls back to the slowest mode. As a result, the BMCR_FULLDPLX check goes away, as it was never set in Clause 45 forced mode, which is full duplex only. With the link up, the speed is still taken from VEND1_SPEED_STATUS. Fixes: 830877d89edc ("net: phy: Add support for Aeonsemi AS21xxx PHYs") Signed-off-by: Cristian Ciocaltea --- drivers/net/phy/as21xxx.c | 200 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 141 insertions(+), 59 deletions(-) diff --git a/drivers/net/phy/as21xxx.c b/drivers/net/phy/as21xxx.c index 967ff3020814..5eea6baa75bf 100644 --- a/drivers/net/phy/as21xxx.c +++ b/drivers/net/phy/as21xxx.c @@ -716,22 +716,22 @@ static int as21xxx_get_features(struct phy_device *phydev) return 0; } -static int as21xxx_read_link(struct phy_device *phydev, int *bmcr) +static int as21xxx_read_link(struct phy_device *phydev) { - int status; + int bmcr, status; /* Normal C22 BMCR report inconsistent data, use * the mapped C22 in C45 to have more consistent link info. */ - *bmcr = phy_read_mmd(phydev, MDIO_MMD_AN, - AS21XXX_MDIO_AN_C22 + MII_BMCR); - if (*bmcr < 0) - return *bmcr; + bmcr = phy_read_mmd(phydev, MDIO_MMD_AN, + AS21XXX_MDIO_AN_C22 + MII_BMCR); + if (bmcr < 0) + return bmcr; /* Autoneg is being started, therefore disregard current * link status and report link as down. */ - if (*bmcr & BMCR_ANRESTART) { + if (bmcr & BMCR_ANRESTART) { phydev->link = 0; return 0; } @@ -752,9 +752,9 @@ static int as21xxx_read_link(struct phy_device *phydev, int *bmcr) return 0; } -static int as21xxx_read_c22_lpa(struct phy_device *phydev) +static int as21xxx_read_lpa(struct phy_device *phydev) { - int lpagb; + int lpagb, lpa; /* MII_STAT1000 are only filled in the mapped C22 * in C45, use that to fill lpagb values and check. @@ -781,15 +781,107 @@ static int as21xxx_read_c22_lpa(struct phy_device *phydev) mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, lpagb); + /* Same goes for the base page of the link partner */ + lpa = phy_read_mmd(phydev, MDIO_MMD_AN, + AS21XXX_MDIO_AN_C22 + MII_LPA); + if (lpa < 0) + return lpa; + + mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); + + lpagb = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_STAT); + if (lpagb < 0) + return lpagb; + + mii_10gbt_stat_mod_linkmode_lpa_t(phydev->lp_advertising, lpagb); + return 0; } +static int as21xxx_read_speed(struct phy_device *phydev) +{ + int val; + + val = phy_read_mmd(phydev, MDIO_MMD_VEND1, VEND1_SPEED_STATUS); + if (val < 0) + return val; + + switch (val & VEND1_SPEED_MASK) { + case VEND1_SPEED_10000: + phydev->speed = SPEED_10000; + break; + case VEND1_SPEED_5000: + phydev->speed = SPEED_5000; + break; + case VEND1_SPEED_2500: + phydev->speed = SPEED_2500; + break; + case VEND1_SPEED_1000: + phydev->speed = SPEED_1000; + break; + case VEND1_SPEED_100: + phydev->speed = SPEED_100; + break; + case VEND1_SPEED_10: + phydev->speed = SPEED_10; + break; + default: + dev_warn_once(&phydev->mdio.dev, + "Unknown speed status 0x%04x\n", val); + phydev->speed = SPEED_UNKNOWN; + break; + } + + return 0; +} + +/* Pick the duplex mode negotiated for the speed the link came up at */ +static void as21xxx_resolve_duplex(struct phy_device *phydev) +{ + __ETHTOOL_DECLARE_LINK_MODE_MASK(common); + int full, half; + + switch (phydev->speed) { + case SPEED_1000: + full = ETHTOOL_LINK_MODE_1000baseT_Full_BIT; + half = ETHTOOL_LINK_MODE_1000baseT_Half_BIT; + break; + case SPEED_100: + full = ETHTOOL_LINK_MODE_100baseT_Full_BIT; + half = ETHTOOL_LINK_MODE_100baseT_Half_BIT; + break; + case SPEED_10: + full = ETHTOOL_LINK_MODE_10baseT_Full_BIT; + half = ETHTOOL_LINK_MODE_10baseT_Half_BIT; + break; + default: + /* 2.5GBASE-T and above are full duplex only */ + phydev->duplex = DUPLEX_FULL; + return; + } + + linkmode_and(common, phydev->lp_advertising, phydev->advertising); + + if (linkmode_test_bit(full, common)) { + phydev->duplex = DUPLEX_FULL; + } else if (linkmode_test_bit(half, common)) { + phydev->duplex = DUPLEX_HALF; + } else { + /* No partner abilities, i.e. parallel detection at 10/100M, + * which implies half duplex, unless the PHY lacks it. + */ + phydev->duplex = phydev->speed != SPEED_1000 && + linkmode_test_bit(half, phydev->supported) ? + DUPLEX_HALF : DUPLEX_FULL; + } +} + static int as21xxx_read_status(struct phy_device *phydev) { - int bmcr, old_link = phydev->link; + int old_link = phydev->link; int ret; - ret = as21xxx_read_link(phydev, &bmcr); + ret = as21xxx_read_link(phydev); if (ret) return ret; @@ -797,64 +889,54 @@ static int as21xxx_read_status(struct phy_device *phydev) if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) return 0; - phydev->speed = SPEED_UNKNOWN; - phydev->duplex = DUPLEX_UNKNOWN; phydev->pause = 0; phydev->asym_pause = 0; - if (phydev->autoneg == AUTONEG_ENABLE) { - ret = genphy_c45_read_lpa(phydev); + if (phydev->autoneg == AUTONEG_DISABLE) { + ret = genphy_c45_read_pma(phydev); if (ret) return ret; - ret = as21xxx_read_c22_lpa(phydev); - if (ret) - return ret; + if (phydev->link) { + ret = as21xxx_read_speed(phydev); - phy_resolve_aneg_linkmode(phydev); - } else { - int speed; - - linkmode_zero(phydev->lp_advertising); - - speed = phy_read_mmd(phydev, MDIO_MMD_VEND1, - VEND1_SPEED_STATUS); - if (speed < 0) - return speed; - - switch (speed & VEND1_SPEED_MASK) { - case VEND1_SPEED_10000: - phydev->speed = SPEED_10000; - phydev->duplex = DUPLEX_FULL; - break; - case VEND1_SPEED_5000: - phydev->speed = SPEED_5000; - phydev->duplex = DUPLEX_FULL; - break; - case VEND1_SPEED_2500: - phydev->speed = SPEED_2500; - phydev->duplex = DUPLEX_FULL; - break; - case VEND1_SPEED_1000: - phydev->speed = SPEED_1000; - if (bmcr & BMCR_FULLDPLX) - phydev->duplex = DUPLEX_FULL; - else - phydev->duplex = DUPLEX_HALF; - break; - case VEND1_SPEED_100: - phydev->speed = SPEED_100; - phydev->duplex = DUPLEX_FULL; - break; - case VEND1_SPEED_10: - phydev->speed = SPEED_10; - phydev->duplex = DUPLEX_FULL; - break; - default: - return -EINVAL; + /* Hold the link down until the speed status settles */ + if (!ret && phydev->speed == SPEED_UNKNOWN) + phydev->link = 0; } + + return ret; } + phydev->speed = SPEED_UNKNOWN; + phydev->duplex = DUPLEX_UNKNOWN; + linkmode_zero(phydev->lp_advertising); + + /* as21xxx_read_link() reports link up only once autoneg completed */ + if (!phydev->link) + return 0; + + /* The firmware advertises every speed up to the highest one in + * phydev->advertising, hence resolving the advertisements may not + * match the link. Take the speed from VEND1_SPEED_STATUS instead. + */ + ret = as21xxx_read_speed(phydev); + if (ret) + return ret; + + /* Hold the link down until the speed status settles */ + if (phydev->speed == SPEED_UNKNOWN) { + phydev->link = 0; + return 0; + } + + ret = as21xxx_read_lpa(phydev); + if (ret) + return ret; + + as21xxx_resolve_duplex(phydev); + phy_resolve_aneg_pause(phydev); + return 0; } -- 2.56.0