From: Christian Marangi <ansuelsmth@gmail.com>
To: Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Willem de Bruijn <willemb@google.com>,
Simon Horman <horms@kernel.org>, Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>,
Christian Marangi <ansuelsmth@gmail.com>,
Lorenzo Bianconi <lorenzo@kernel.org>,
Heiner Kallweit <hkallweit1@gmail.com>,
Russell King <linux@armlinux.org.uk>,
Maxime Chevallier <maxime.chevallier@bootlin.com>,
Philipp Zabel <p.zabel@pengutronix.de>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>,
"Russell King (Oracle)" <rmk+kernel@armlinux.org.uk>,
netdev@vger.kernel.org, devicetree@vger.kernel.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-mediatek@lists.infradead.org, llvm@lists.linux.dev
Subject: [PATCH net-next v23 06/15] net: phylink: add capabilities helpers and track modified advertising
Date: Mon, 30 Nov 2026 00:05:36 +0100 [thread overview]
Message-ID: <20261129230558.612526-7-ansuelsmth@gmail.com> (raw)
In-Reply-To: <20261129230558.612526-1-ansuelsmth@gmail.com>
In preparation for PCS release and late attach, introduce helpers to
recalculate capabilities when a PCS is removed or added and track which
advertising modes the user has disabled.
To achieve this, add advertising and user_disabled_advertising masks to
struct phylink. The advertising mask holds the full validated advertisement
before applying the phylink user restrictions. The
user_disabled_advertising mask records the modes the user has disabled.
Without an attached PHY, phylink_update_user_disabled_advertising() uses
the ethtool link settings request captured before interface-specific
validation. It preserves restrictions for modes absent from the current
full advertisement so they survive PCS removal and late attach.
User-requested pause changes also update the Pause and Asym_Pause bits in
the mask.
phylink_update_capabilities() recalculates the supported modes and the full
advertisement. For links without a PHY, it applies the user-disabled mask
when updating link_config.advertising.
phylink_create() initializes pl->advertising and link_config.advertising
and uses phylink_update_capabilities() for initial validation.
phylink_update_mac_pause_capabilities() uses the same helper and keeps the
user-disabled mask intact when updating the pause state.
Factor out the fixed-link, in-band, PHY and SFP capability calculations so
they can be reused after the initial link configuration.
For optical SFPs, keep the module's original interface and link-mode
masks. Derive sfp_interfaces from the module mask and current phylink
support. Validate a copy of sfp_support and pass the restricted mask to
phylink_sfp_set_config(). Clear cached module capabilities if insertion
fails. Apply the user-disabled mask during interface selection and clear
it when the SFP module is removed.
For PHYs, validate the advertisement currently applied by phylib. During
PHY bringup, complete EEE clock setup before committing the PHY-derived
link state and restrict the initial PHY advertisement to the validated
modes.
Update phy_set_asym_pause() under the PHY mutex and apply the new pause
settings to the advertisement saved by speed down so they are retained at
speed up.
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
drivers/net/phy/phy_device.c | 8 +-
drivers/net/phy/phylink.c | 590 +++++++++++++++++++++++------------
2 files changed, 403 insertions(+), 195 deletions(-)
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index b39e945cebd7..2bf57564cb40 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -3137,12 +3137,18 @@ void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
{
__ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
+ mutex_lock(&phydev->lock);
linkmode_copy(oldadv, phydev->advertising);
linkmode_set_pause(phydev->advertising, tx, rx);
+ /* Keep the new pause settings in the advertisement saved by speed down */
+ if (!linkmode_empty(phydev->adv_old))
+ linkmode_set_pause(phydev->adv_old, tx, rx);
+
if (!linkmode_equal(oldadv, phydev->advertising) &&
phydev->autoneg)
- phy_start_aneg(phydev);
+ _phy_start_aneg(phydev);
+ mutex_unlock(&phydev->lock);
}
EXPORT_SYMBOL(phy_set_asym_pause);
diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 141da617b24b..877a0a30ec22 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -64,6 +64,8 @@ struct phylink {
u8 link_port; /* The current non-phy ethtool port */
__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
__ETHTOOL_DECLARE_LINK_MODE_MASK(supported_lpi);
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(user_disabled_advertising);
/* The link configuration settings */
struct phylink_link_state link_config;
@@ -107,6 +109,7 @@ struct phylink {
struct sfp_bus *sfp_bus;
bool sfp_may_have_phy;
+ DECLARE_PHY_INTERFACE_MASK(sfp_module_interfaces);
DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
u8 sfp_port;
@@ -816,12 +819,51 @@ static void phylink_fill_fixedlink_supported(unsigned long *supported)
linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, supported);
}
-static int phylink_parse_fixedlink(struct phylink *pl,
- const struct fwnode_handle *fwnode)
+static int phylink_parse_fixedlink_capabilities(struct phylink *pl,
+ struct phylink_link_state *config)
{
__ETHTOOL_DECLARE_LINK_MODE_MASK(match) = { 0, };
__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
const struct link_capabilities *c;
+
+ lockdep_assert_held(&pl->state_mutex);
+
+ linkmode_zero(pl->supported);
+ phylink_fill_fixedlink_supported(pl->supported);
+
+ linkmode_copy(config->advertising, pl->supported);
+ __phylink_validate(pl, pl->supported, config);
+
+ linkmode_copy(pl->advertising, config->advertising);
+
+ c = phy_caps_lookup(config->speed, config->duplex,
+ pl->supported, true);
+ if (c)
+ linkmode_and(match, pl->supported, c->linkmodes);
+
+ linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, mask);
+ linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, mask);
+ linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mask);
+ linkmode_and(pl->supported, pl->supported, mask);
+
+ phylink_set(pl->supported, MII);
+
+ if (c) {
+ linkmode_or(pl->supported, pl->supported, match);
+ linkmode_or(config->lp_advertising,
+ config->lp_advertising, match);
+ }
+
+ linkmode_and(config->advertising, config->advertising,
+ pl->supported);
+ linkmode_copy(pl->advertising, config->advertising);
+
+ return c ? 0 : -EINVAL;
+}
+
+static int phylink_parse_fixedlink(struct phylink *pl,
+ const struct fwnode_handle *fwnode)
+{
struct fwnode_handle *fixed_node;
struct gpio_desc *desc;
u32 speed;
@@ -893,39 +935,77 @@ static int phylink_parse_fixedlink(struct phylink *pl,
phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
pl->link_config.speed);
- linkmode_zero(pl->supported);
- phylink_fill_fixedlink_supported(pl->supported);
+ mutex_lock(&pl->state_mutex);
+ ret = phylink_parse_fixedlink_capabilities(pl, &pl->link_config);
+ mutex_unlock(&pl->state_mutex);
+ if (ret)
+ phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
+ pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
+ pl->link_config.speed);
+
+ pl->link_config.link = 1;
+ pl->link_config.an_complete = 1;
- linkmode_copy(pl->link_config.advertising, pl->supported);
- phylink_validate(pl, pl->supported, &pl->link_config);
+ return 0;
+}
- c = phy_caps_lookup(pl->link_config.speed, pl->link_config.duplex,
- pl->supported, true);
- if (c)
- linkmode_and(match, pl->supported, c->linkmodes);
+static int phylink_parse_inband_capabilities(struct phylink *pl,
+ struct phylink_link_state *config)
+{
+ unsigned long caps;
- linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, mask);
- linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, mask);
- linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mask);
- linkmode_and(pl->supported, pl->supported, mask);
+ lockdep_assert_held(&pl->state_mutex);
+ linkmode_zero(pl->supported);
phylink_set(pl->supported, MII);
+ phylink_set(pl->supported, Autoneg);
+ phylink_set(pl->supported, Asym_Pause);
+ phylink_set(pl->supported, Pause);
- if (c) {
- linkmode_or(pl->supported, pl->supported, match);
- linkmode_or(pl->link_config.lp_advertising,
- pl->link_config.lp_advertising, match);
- } else {
- phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
- pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
- pl->link_config.speed);
+ switch (config->interface) {
+ case PHY_INTERFACE_MODE_SGMII:
+ case PHY_INTERFACE_MODE_PSGMII:
+ case PHY_INTERFACE_MODE_QSGMII:
+ case PHY_INTERFACE_MODE_QUSGMII:
+ case PHY_INTERFACE_MODE_RGMII:
+ case PHY_INTERFACE_MODE_RGMII_ID:
+ case PHY_INTERFACE_MODE_RGMII_RXID:
+ case PHY_INTERFACE_MODE_RGMII_TXID:
+ case PHY_INTERFACE_MODE_RTBI:
+ case PHY_INTERFACE_MODE_1000BASEX:
+ case PHY_INTERFACE_MODE_2500BASEX:
+ case PHY_INTERFACE_MODE_5GBASER:
+ case PHY_INTERFACE_MODE_25GBASER:
+ case PHY_INTERFACE_MODE_USXGMII:
+ case PHY_INTERFACE_MODE_10G_QXGMII:
+ case PHY_INTERFACE_MODE_10GKR:
+ case PHY_INTERFACE_MODE_10GBASER:
+ case PHY_INTERFACE_MODE_XLGMII:
+ case PHY_INTERFACE_MODE_50GBASER:
+ case PHY_INTERFACE_MODE_LAUI:
+ case PHY_INTERFACE_MODE_100GBASEP:
+ caps = ~(MAC_SYM_PAUSE | MAC_ASYM_PAUSE);
+ caps = phylink_get_capabilities(config->interface, caps,
+ RATE_MATCH_NONE);
+ phylink_caps_to_linkmodes(pl->supported, caps);
+ break;
+
+ default:
+ phylink_err(pl,
+ "incorrect link mode %s for in-band status\n",
+ phy_modes(config->interface));
+ return -EINVAL;
}
- linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
- pl->supported);
+ linkmode_copy(config->advertising, pl->supported);
- pl->link_config.link = 1;
- pl->link_config.an_complete = 1;
+ if (__phylink_validate(pl, pl->supported, config)) {
+ phylink_err(pl,
+ "failed to validate link configuration for in-band status\n");
+ return -EINVAL;
+ }
+
+ linkmode_copy(pl->advertising, config->advertising);
return 0;
}
@@ -935,7 +1015,7 @@ static int phylink_parse_mode(struct phylink *pl,
{
struct fwnode_handle *dn;
const char *managed;
- unsigned long caps;
+ int ret;
if (pl->config->default_an_inband)
pl->cfg_link_an_mode = MLO_AN_INBAND;
@@ -957,54 +1037,11 @@ static int phylink_parse_mode(struct phylink *pl,
}
if (pl->cfg_link_an_mode == MLO_AN_INBAND) {
- linkmode_zero(pl->supported);
- phylink_set(pl->supported, MII);
- phylink_set(pl->supported, Autoneg);
- phylink_set(pl->supported, Asym_Pause);
- phylink_set(pl->supported, Pause);
-
- switch (pl->link_config.interface) {
- case PHY_INTERFACE_MODE_SGMII:
- case PHY_INTERFACE_MODE_PSGMII:
- case PHY_INTERFACE_MODE_QSGMII:
- case PHY_INTERFACE_MODE_QUSGMII:
- case PHY_INTERFACE_MODE_RGMII:
- case PHY_INTERFACE_MODE_RGMII_ID:
- case PHY_INTERFACE_MODE_RGMII_RXID:
- case PHY_INTERFACE_MODE_RGMII_TXID:
- case PHY_INTERFACE_MODE_RTBI:
- case PHY_INTERFACE_MODE_1000BASEX:
- case PHY_INTERFACE_MODE_2500BASEX:
- case PHY_INTERFACE_MODE_5GBASER:
- case PHY_INTERFACE_MODE_25GBASER:
- case PHY_INTERFACE_MODE_USXGMII:
- case PHY_INTERFACE_MODE_10G_QXGMII:
- case PHY_INTERFACE_MODE_10GKR:
- case PHY_INTERFACE_MODE_10GBASER:
- case PHY_INTERFACE_MODE_XLGMII:
- case PHY_INTERFACE_MODE_50GBASER:
- case PHY_INTERFACE_MODE_LAUI:
- case PHY_INTERFACE_MODE_100GBASEP:
- caps = ~(MAC_SYM_PAUSE | MAC_ASYM_PAUSE);
- caps = phylink_get_capabilities(pl->link_config.interface, caps,
- RATE_MATCH_NONE);
- phylink_caps_to_linkmodes(pl->supported, caps);
- break;
-
- default:
- phylink_err(pl,
- "incorrect link mode %s for in-band status\n",
- phy_modes(pl->link_config.interface));
- return -EINVAL;
- }
-
- linkmode_copy(pl->link_config.advertising, pl->supported);
-
- if (phylink_validate(pl, pl->supported, &pl->link_config)) {
- phylink_err(pl,
- "failed to validate link configuration for in-band status\n");
- return -EINVAL;
- }
+ mutex_lock(&pl->state_mutex);
+ ret = phylink_parse_inband_capabilities(pl, &pl->link_config);
+ mutex_unlock(&pl->state_mutex);
+ if (ret)
+ return ret;
}
return 0;
@@ -2176,10 +2213,12 @@ static int phylink_fill_available_pcs(struct phylink *pl,
* phylink_update_pause_state() - Update the phylink pause frame configuration
* @pl: a pointer to a &struct phylink instance
* @pause_state: bitmask indicating the new pause state
+ * @user_config: whether the pause state was requested by the user
*
* Update the MAC pause frame (flow control) state for the phylink instance.
*/
-static void phylink_update_pause_state(struct phylink *pl, int pause_state)
+static void phylink_update_pause_state(struct phylink *pl, int pause_state,
+ bool user_config)
{
struct phylink_link_state *config = &pl->link_config;
bool tx_pause = !!(pause_state & MLO_PAUSE_TX);
@@ -2204,6 +2243,17 @@ static void phylink_update_pause_state(struct phylink *pl, int pause_state)
linkmode_set_pause(config->advertising, tx_pause,
rx_pause);
+ if (user_config && !pl->phydev) {
+ unsigned long *disabled = pl->user_disabled_advertising;
+
+ linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, disabled,
+ !linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
+ config->advertising));
+ linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, disabled,
+ !linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
+ config->advertising));
+ }
+
manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
(!(pause_state & MLO_PAUSE_AN) &&
(config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
@@ -2236,6 +2286,157 @@ static void phylink_update_pause_state(struct phylink *pl, int pause_state)
}
}
+static void phylink_update_user_disabled_advertising(struct phylink *pl,
+ const unsigned long *advertising)
+{
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(disabled);
+
+ /* Preserve restrictions for modes absent from the current full mask */
+ linkmode_andnot(disabled, pl->user_disabled_advertising,
+ pl->advertising);
+
+ /* Update restrictions for modes in the current full advertisement */
+ linkmode_andnot(pl->user_disabled_advertising, pl->advertising,
+ advertising);
+
+ /* Combine the updated restrictions with those preserved above */
+ linkmode_or(pl->user_disabled_advertising,
+ pl->user_disabled_advertising, disabled);
+}
+
+static void phylink_sfp_update_capabilities(struct phylink *pl,
+ unsigned long *supported,
+ struct phylink_link_state *config,
+ phy_interface_t interface)
+{
+ enum inband_type inband_type;
+
+ inband_type = phylink_get_inband_type(interface);
+ if (inband_type == INBAND_NONE) {
+ /*
+ * If this is the sole interface, and there is no inband
+ * support, clear the advertising mask and Autoneg bit in
+ * the support mask. Otherwise, just clear the Autoneg bit
+ * in the advertising mask.
+ */
+ if (phy_interface_weight(pl->sfp_interfaces) == 1) {
+ linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
+ supported);
+ linkmode_zero(config->advertising);
+ } else {
+ linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
+ config->advertising);
+ }
+ }
+}
+
+static int phylink_phy_update_capabilities(struct phylink *pl, struct phy_device *phy,
+ phy_interface_t interface)
+{
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
+ struct phylink_link_state config;
+ int ret;
+
+ lockdep_assert_held(&pl->phydev_mutex);
+ lockdep_assert_held(&phy->lock);
+ lockdep_assert_held(&pl->state_mutex);
+
+ memset(&config, 0, sizeof(config));
+ linkmode_copy(supported, phy->supported);
+ /* Use the advertisement phylib is currently applying */
+ linkmode_copy(config.advertising, phy->advertising);
+ config.interface = interface;
+
+ ret = __phylink_validate_phy(pl, phy, supported, &config);
+ if (ret) {
+ phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
+ phy_modes(config.interface),
+ __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
+ __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
+ ERR_PTR(ret));
+ return ret;
+ }
+
+ linkmode_copy(pl->supported, supported);
+ linkmode_copy(pl->link_config.advertising, config.advertising);
+ linkmode_copy(pl->advertising, pl->link_config.advertising);
+
+ return 0;
+}
+
+static void phylink_update_capabilities(struct phylink *pl,
+ struct phylink_link_state *config)
+{
+ if (pl->phydev) {
+ lockdep_assert_held(&pl->phydev_mutex);
+ lockdep_assert_held(&pl->phydev->lock);
+ }
+ lockdep_assert_held(&pl->state_mutex);
+
+ if (pl->phydev) {
+ phylink_phy_update_capabilities(pl, pl->phydev,
+ pl->phy_state.interface);
+ return;
+ }
+
+ if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
+ phylink_parse_fixedlink_capabilities(pl, config);
+ goto exit;
+ }
+
+ /*
+ * Revalidate SFP modes before generic in-band capabilities, so module
+ * EEPROM limits are retained for in-band SFP configurations.
+ */
+ if (pl->sfp_bus && config->interface != PHY_INTERFACE_MODE_NA &&
+ !phy_interface_empty(pl->sfp_module_interfaces)) {
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(active_supported);
+ struct phylink_link_state sfp_config = *config;
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
+
+ phy_interface_and(pl->sfp_interfaces, pl->sfp_module_interfaces,
+ pl->supported_interfaces);
+ linkmode_copy(supported, pl->sfp_support);
+ linkmode_copy(sfp_config.advertising, pl->sfp_support);
+ if (__phylink_validate_mask(pl, NULL, supported, &sfp_config,
+ pl->sfp_interfaces))
+ return;
+
+ phylink_sfp_update_capabilities(pl, supported, &sfp_config,
+ config->interface);
+
+ /*
+ * Keep alternate modes available for interface selection, but
+ * only advertise modes that the current interface can use.
+ */
+ linkmode_copy(active_supported, pl->sfp_support);
+ if (__phylink_validate(pl, active_supported, &sfp_config))
+ return;
+
+ linkmode_copy(pl->supported, supported);
+ linkmode_copy(pl->advertising, sfp_config.advertising);
+ goto exit;
+ }
+
+ if (pl->cfg_link_an_mode == MLO_AN_INBAND) {
+ phylink_parse_inband_capabilities(pl, config);
+ goto exit;
+ }
+
+ /* No media is attached yet. Recalculate the generic capabilities */
+ linkmode_fill(pl->supported);
+ linkmode_copy(config->advertising, pl->supported);
+ __phylink_validate(pl, pl->supported, config);
+
+ /* Save the newly calculated full advertisement */
+ linkmode_copy(pl->advertising, config->advertising);
+
+exit:
+ /* Keep user-disabled modes out of the active advertisement */
+ linkmode_andnot(config->advertising,
+ pl->advertising, pl->user_disabled_advertising);
+}
+
/**
* phylink_update_mac_pause_capabilities() - Dynamically update MAC pause
* @pl: a pointer to a &struct phylink returned from phylink_create()
@@ -2248,6 +2449,7 @@ static void phylink_update_pause_state(struct phylink *pl, int pause_state)
void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac_pause)
{
struct phylink_link_state *config = &pl->link_config;
+ struct phy_device *phydev;
unsigned long old_pause;
int pause_state;
@@ -2262,20 +2464,16 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac
if (old_pause == mac_pause)
return;
+ mutex_lock(&pl->phydev_mutex);
+ phydev = pl->phydev;
+ if (phydev)
+ mutex_lock(&phydev->lock);
mutex_lock(&pl->state_mutex);
pl->config->mac_capabilities &= ~(MAC_SYM_PAUSE | MAC_ASYM_PAUSE);
pl->config->mac_capabilities |= mac_pause;
- phylink_set(pl->supported, Pause);
- phylink_set(pl->supported, Asym_Pause);
-
- if (pl->phydev)
- linkmode_and(pl->supported, pl->supported, pl->phydev->supported);
- else if (pl->sfp_bus)
- linkmode_and(pl->supported, pl->supported, pl->sfp_support);
-
- __phylink_validate(pl, pl->supported, config);
+ phylink_update_capabilities(pl, config);
pause_state = config->pause;
@@ -2287,8 +2485,12 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac
}
mutex_unlock(&pl->state_mutex);
+ if (phydev)
+ mutex_unlock(&phydev->lock);
+ mutex_unlock(&pl->phydev_mutex);
- phylink_update_pause_state(pl, pause_state);
+ /* Let phylib detect the change against the current PHY advertisement */
+ phylink_update_pause_state(pl, pause_state, false);
}
EXPORT_SYMBOL_GPL(phylink_update_mac_pause_capabilities);
@@ -2437,6 +2639,8 @@ struct phylink *phylink_create(struct phylink_config *config,
pl->link_config.pause = MLO_PAUSE_AN;
pl->link_config.speed = SPEED_UNKNOWN;
pl->link_config.duplex = DUPLEX_UNKNOWN;
+ linkmode_fill(pl->advertising);
+ linkmode_fill(pl->link_config.advertising);
pl->pcs_state = PCS_STATE_DOWN;
pl->mac_ops = mac_ops;
__set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
@@ -2449,9 +2653,7 @@ struct phylink *phylink_create(struct phylink_config *config,
mutex_lock(&pl->state_mutex);
phylink_parse_supported_interfaces(pl);
- linkmode_fill(pl->supported);
- linkmode_copy(pl->link_config.advertising, pl->supported);
- __phylink_validate(pl, pl->supported, &pl->link_config);
+ phylink_update_capabilities(pl, &pl->link_config);
mutex_unlock(&pl->state_mutex);
ret = phylink_parse_mode(pl, fwnode);
@@ -2564,11 +2766,19 @@ static void phylink_phy_change(struct phy_device *phydev, bool up)
static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
phy_interface_t interface)
{
- __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
- struct phylink_link_state config;
+ bool tx_clk_stop;
char *irq_str;
int ret;
+ /* Finish clock setup before committing the PHY-derived link state */
+ tx_clk_stop = phy_eee_tx_clock_stop_capable(phy) > 0;
+ if (pl->mac_supports_eee_ops) {
+ ret = phy_eee_rx_clock_stop(phy,
+ pl->config->eee_rx_clk_stop_enable);
+ if (ret && ret != -EOPNOTSUPP)
+ return ret;
+ }
+
/*
* This is the new way of dealing with flow control for PHYs,
* as described by Timur Tabi in commit 529ed1275263 ("net: phy:
@@ -2578,25 +2788,21 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
*/
phy_support_asym_pause(phy);
+ mutex_lock(&pl->phydev_mutex);
mutex_lock(&phy->lock);
mutex_lock(&pl->state_mutex);
- memset(&config, 0, sizeof(config));
- linkmode_copy(supported, phy->supported);
- linkmode_copy(config.advertising, phy->advertising);
- config.interface = interface;
- ret = __phylink_validate_phy(pl, phy, supported, &config);
- mutex_unlock(&pl->state_mutex);
- mutex_unlock(&phy->lock);
+ ret = phylink_phy_update_capabilities(pl, phy, interface);
if (ret) {
- phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
- phy_modes(config.interface),
- __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
- __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
- ERR_PTR(ret));
+ mutex_unlock(&pl->state_mutex);
+ mutex_unlock(&phy->lock);
+ mutex_unlock(&pl->phydev_mutex);
return ret;
}
+ /* Restrict the initial PHY advertisement to the validated modes */
+ linkmode_copy(phy->advertising, pl->link_config.advertising);
+
phy->phylink = pl;
phy->phy_link_change = phylink_phy_change;
@@ -2606,19 +2812,11 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
kfree(irq_str);
- mutex_lock(&pl->phydev_mutex);
- mutex_lock(&phy->lock);
- mutex_lock(&pl->state_mutex);
pl->phy_state.interface = interface;
pl->phy_state.pause = MLO_PAUSE_NONE;
pl->phy_state.speed = SPEED_UNKNOWN;
pl->phy_state.duplex = DUPLEX_UNKNOWN;
pl->phy_state.rate_matching = RATE_MATCH_NONE;
- linkmode_copy(pl->supported, supported);
- linkmode_copy(pl->link_config.advertising, config.advertising);
-
- /* Restrict the phy advertisement according to the MAC support. */
- linkmode_copy(phy->advertising, config.advertising);
/* If the MAC supports phylink managed EEE, restrict the EEE
* advertisement according to the MAC's LPI capabilities.
@@ -2649,6 +2847,12 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
phy_disable_eee(phy);
}
+ if (pl->config->mac_managed_pm)
+ phy->mac_managed_pm = true;
+
+ pl->mac_tx_clk_stop = tx_clk_stop;
+ pl->phydev = phy;
+
mutex_unlock(&pl->state_mutex);
mutex_unlock(&phy->lock);
mutex_unlock(&pl->phydev_mutex);
@@ -2659,37 +2863,6 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
__ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
__ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
- if (pl->config->mac_managed_pm)
- phy->mac_managed_pm = true;
-
- /* Allow the MAC to stop its clock if the PHY has the capability */
- pl->mac_tx_clk_stop = phy_eee_tx_clock_stop_capable(phy) > 0;
-
- if (pl->mac_supports_eee_ops) {
- /* Explicitly configure whether the PHY is allowed to stop it's
- * receive clock.
- */
- ret = phy_eee_rx_clock_stop(phy,
- pl->config->eee_rx_clk_stop_enable);
- if (ret == -EOPNOTSUPP)
- ret = 0;
- }
-
- if (ret)
- return ret;
-
- /* Nothing below can fail, so the PHY can be recorded now. Doing it
- * here rather than above keeps a failed bringup from leaving
- * pl->phydev pointing at a PHY the caller is about to detach.
- */
- mutex_lock(&pl->phydev_mutex);
- mutex_lock(&phy->lock);
- mutex_lock(&pl->state_mutex);
- pl->phydev = phy;
- mutex_unlock(&pl->state_mutex);
- mutex_unlock(&phy->lock);
- mutex_unlock(&pl->phydev_mutex);
-
if (phy_interrupt_is_valid(phy))
phy_request_interrupt(phy);
@@ -3478,6 +3651,7 @@ static bool phylink_validate_pcs_inband_autoneg(struct phylink *pl,
int phylink_ethtool_ksettings_set(struct phylink *pl,
const struct ethtool_link_ksettings *kset)
{
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(requested_advertising);
__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
const struct link_capabilities *c;
struct phylink_link_state config;
@@ -3567,6 +3741,7 @@ int phylink_ethtool_ksettings_set(struct phylink *pl,
*/
linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
kset->base.autoneg == AUTONEG_ENABLE);
+ linkmode_copy(requested_advertising, config.advertising);
/* If this link is with an SFP, ensure that changes to advertised modes
* also cause the associated interface to be selected such that the
@@ -3614,6 +3789,7 @@ int phylink_ethtool_ksettings_set(struct phylink *pl,
return -EINVAL;
mutex_lock(&pl->state_mutex);
+ phylink_update_user_disabled_advertising(pl, requested_advertising);
pl->link_config.speed = config.speed;
pl->link_config.duplex = config.duplex;
@@ -3712,7 +3888,7 @@ int phylink_ethtool_set_pauseparam(struct phylink *pl,
if (pause->tx_pause)
pause_state |= MLO_PAUSE_TX;
- phylink_update_pause_state(pl, pause_state);
+ phylink_update_pause_state(pl, pause_state, true);
return 0;
}
@@ -4097,27 +4273,54 @@ static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
}
static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
- const unsigned long *intf)
+ const unsigned long *intf,
+ const unsigned long *supported,
+ const struct phylink_link_state *state)
{
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp_supported);
+ struct phylink_link_state tmp_state;
phy_interface_t interface;
size_t i;
- interface = PHY_INTERFACE_MODE_NA;
- for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++)
- if (test_bit(phylink_sfp_interface_preference[i], intf)) {
- interface = phylink_sfp_interface_preference[i];
- break;
- }
+ for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++) {
+ interface = phylink_sfp_interface_preference[i];
+ if (!test_bit(interface, intf))
+ continue;
- return interface;
+ tmp_state = *state;
+ tmp_state.interface = interface;
+ linkmode_copy(tmp_supported, supported);
+ if (phylink_validate(pl, tmp_supported, &tmp_state))
+ continue;
+
+ /* Skip this preferred interface if user-disabled in advertising */
+ linkmode_andnot(tmp_state.advertising, tmp_state.advertising,
+ pl->user_disabled_advertising);
+ if (!phylink_is_empty_linkmode(tmp_state.advertising))
+ return interface;
+ }
+
+ return PHY_INTERFACE_MODE_NA;
}
-static void phylink_sfp_set_config(struct phylink *pl, unsigned long *supported,
- struct phylink_link_state *state,
- bool changed)
+static int phylink_sfp_set_config(struct phylink *pl, unsigned long *supported,
+ struct phylink_link_state *state,
+ bool changed)
{
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
u8 mode = MLO_AN_INBAND;
+ linkmode_copy(advertising, state->advertising);
+ if (!pl->phydev) {
+ linkmode_andnot(advertising, advertising,
+ pl->user_disabled_advertising);
+ if (!phylink_validate_pcs_inband_autoneg(pl, state->interface,
+ advertising)) {
+ phylink_err(pl, "autoneg setting not compatible with PCS\n");
+ return -EINVAL;
+ }
+ }
+
phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
phylink_an_mode_str(mode), phy_modes(state->interface),
__ETHTOOL_LINK_MODE_MASK_NBITS, supported);
@@ -4127,8 +4330,10 @@ static void phylink_sfp_set_config(struct phylink *pl, unsigned long *supported,
changed = true;
}
- if (!linkmode_equal(pl->link_config.advertising, state->advertising)) {
- linkmode_copy(pl->link_config.advertising, state->advertising);
+ linkmode_copy(pl->advertising, state->advertising);
+
+ if (!linkmode_equal(pl->link_config.advertising, advertising)) {
+ linkmode_copy(pl->link_config.advertising, advertising);
changed = true;
}
@@ -4144,9 +4349,13 @@ static void phylink_sfp_set_config(struct phylink *pl, unsigned long *supported,
phy_modes(state->interface));
}
+ pl->link_port = pl->sfp_port;
+
if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
&pl->phylink_disable_state))
phylink_mac_initial_config(pl, false);
+
+ return 0;
}
static int phylink_sfp_config_phy(struct phylink *pl, struct phy_device *phy)
@@ -4157,6 +4366,7 @@ static int phylink_sfp_config_phy(struct phylink *pl, struct phy_device *phy)
/* We're not using pl->sfp_interfaces, so clear it. */
phy_interface_zero(pl->sfp_interfaces);
+ phy_interface_zero(pl->sfp_module_interfaces);
linkmode_copy(support, phy->supported);
memset(&config, 0, sizeof(config));
@@ -4193,18 +4403,14 @@ static int phylink_sfp_config_phy(struct phylink *pl, struct phy_device *phy)
return ret;
}
- pl->link_port = pl->sfp_port;
-
- phylink_sfp_set_config(pl, support, &config, true);
-
- return 0;
+ return phylink_sfp_set_config(pl, support, &config, true);
}
static int phylink_sfp_config_optical(struct phylink *pl)
{
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_supported);
__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
struct phylink_link_state config;
- enum inband_type inband_type;
phy_interface_t interface;
int ret;
@@ -4218,7 +4424,7 @@ static int phylink_sfp_config_optical(struct phylink *pl)
* the SFP module.
*/
phy_interface_and(pl->sfp_interfaces, pl->supported_interfaces,
- pl->sfp_interfaces);
+ pl->sfp_module_interfaces);
if (phy_interface_empty(pl->sfp_interfaces)) {
phylink_err(pl, "unsupported SFP module: no common interface modes\n");
return -EINVAL;
@@ -4226,15 +4432,17 @@ static int phylink_sfp_config_optical(struct phylink *pl)
memset(&config, 0, sizeof(config));
linkmode_copy(support, pl->sfp_support);
+ linkmode_copy(sfp_supported, support);
linkmode_copy(config.advertising, pl->sfp_support);
config.speed = SPEED_UNKNOWN;
config.duplex = DUPLEX_UNKNOWN;
config.pause = MLO_PAUSE_AN;
- /* For all the interfaces that are supported, reduce the sfp_support
+ /*
+ * For all the interfaces that are supported, reduce the temporary
* mask to only those link modes that can be supported.
*/
- ret = phylink_validate_mask(pl, pl->sfp_support, &config,
+ ret = phylink_validate_mask(pl, sfp_supported, &config,
pl->sfp_interfaces);
if (ret) {
phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
@@ -4242,7 +4450,8 @@ static int phylink_sfp_config_optical(struct phylink *pl)
return ret;
}
- interface = phylink_choose_sfp_interface(pl, pl->sfp_interfaces);
+ interface = phylink_choose_sfp_interface(pl, pl->sfp_interfaces,
+ sfp_supported, &config);
if (interface == PHY_INTERFACE_MODE_NA) {
phylink_err(pl, "failed to select SFP interface\n");
return -EINVAL;
@@ -4251,28 +4460,8 @@ static int phylink_sfp_config_optical(struct phylink *pl)
phylink_dbg(pl, "optical SFP: chosen %s interface\n",
phy_modes(interface));
- inband_type = phylink_get_inband_type(interface);
- if (inband_type == INBAND_NONE) {
- /* If this is the sole interface, and there is no inband
- * support, clear the advertising mask and Autoneg bit in
- * the support mask. Otherwise, just clear the Autoneg bit
- * in the advertising mask.
- */
- if (phy_interface_weight(pl->sfp_interfaces) == 1) {
- linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
- pl->sfp_support);
- linkmode_zero(config.advertising);
- } else {
- linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
- config.advertising);
- }
- }
-
- if (!phylink_validate_pcs_inband_autoneg(pl, interface,
- config.advertising)) {
- phylink_err(pl, "autoneg setting not compatible with PCS");
- return -EINVAL;
- }
+ phylink_sfp_update_capabilities(pl, sfp_supported, &config,
+ interface);
config.interface = interface;
@@ -4285,11 +4474,7 @@ static int phylink_sfp_config_optical(struct phylink *pl)
return ret;
}
- pl->link_port = pl->sfp_port;
-
- phylink_sfp_set_config(pl, pl->sfp_support, &config, false);
-
- return 0;
+ return phylink_sfp_set_config(pl, sfp_supported, &config, false);
}
static int phylink_sfp_module_insert(void *upstream,
@@ -4297,10 +4482,12 @@ static int phylink_sfp_module_insert(void *upstream,
{
const struct sfp_module_caps *caps;
struct phylink *pl = upstream;
+ int ret;
ASSERT_RTNL();
caps = sfp_get_module_caps(pl->sfp_bus);
+ phy_interface_copy(pl->sfp_module_interfaces, caps->interfaces);
phy_interface_copy(pl->sfp_interfaces, caps->interfaces);
linkmode_copy(pl->sfp_support, caps->link_modes);
pl->sfp_may_have_phy = caps->may_have_phy;
@@ -4310,14 +4497,29 @@ static int phylink_sfp_module_insert(void *upstream,
if (pl->sfp_may_have_phy)
return 0;
- return phylink_sfp_config_optical(pl);
+ ret = phylink_sfp_config_optical(pl);
+ if (ret) {
+ /* Only accepted modules get a removal callback */
+ mutex_lock(&pl->state_mutex);
+ phy_interface_zero(pl->sfp_interfaces);
+ phy_interface_zero(pl->sfp_module_interfaces);
+ linkmode_zero(pl->sfp_support);
+ mutex_unlock(&pl->state_mutex);
+ }
+
+ return ret;
}
static void phylink_sfp_module_remove(void *upstream)
{
struct phylink *pl = upstream;
+ mutex_lock(&pl->state_mutex);
phy_interface_zero(pl->sfp_interfaces);
+ phy_interface_zero(pl->sfp_module_interfaces);
+ /* Advertisement restrictions apply to the module being removed */
+ linkmode_zero(pl->user_disabled_advertising);
+ mutex_unlock(&pl->state_mutex);
}
static int phylink_add_sfp_mod_port(struct phylink *pl)
--
2.55.0
next prev parent reply other threads:[~2026-11-29 23:06 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 01/15] net: phylink: make PCS phylink instance access RCU protected Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 02/15] net: phylink: keep and use MAC supported_interfaces in phylink struct Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 03/15] net: phylink: introduce internal phylink PCS handling Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 04/15] net: pcs: implement Firmware node support for PCS driver Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 05/15] net: phylink: make phylink_validate state locked Christian Marangi
2026-11-29 23:05 ` Christian Marangi [this message]
2026-11-29 23:05 ` [PATCH net-next v23 07/15] net: sfp: support deferred module insertion Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 08/15] net: phylink: support PCS provider release Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 09/15] net: phylink: support late PCS provider attach Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 10/15] net: Document PCS subsystem Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 11/15] MAINTAINERS: add myself as PCS subsystem maintainer Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 12/15] net: phylink: add .pcs_link_down PCS OP Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 13/15] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 14/15] net: pcs: airoha: add PCS driver for Airoha AN7581 SoC Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 15/15] net: airoha: add phylink support Christian Marangi
2026-11-29 23:09 ` [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS netdev-bot+sinfo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129230558.612526-7-ansuelsmth@gmail.com \
--to=ansuelsmth@gmail.com \
--cc=andrew+netdev@lunn.ch \
--cc=conor+dt@kernel.org \
--cc=corbet@lwn.net \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=edumazet@kernel.org \
--cc=hkallweit1@gmail.com \
--cc=horms@kernel.org \
--cc=justinstitt@google.com \
--cc=krzk+dt@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=linux@armlinux.org.uk \
--cc=llvm@lists.linux.dev \
--cc=lorenzo@kernel.org \
--cc=maxime.chevallier@bootlin.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=netdev@vger.kernel.org \
--cc=p.zabel@pengutronix.de \
--cc=pabeni@redhat.com \
--cc=rdunlap@infradead.org \
--cc=rmk+kernel@armlinux.org.uk \
--cc=robh@kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox