net: phy: phylink: add helper to modify pause

For Realtek nics, when we enable jumbo, pause are not supported. So we
must check the pause capabilities from ourself and lp.

Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
Link: https://patch.msgid.link/20260831053745.1197-3-javen_xu@realsil.com.cn
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Javen Xu 2026-08-31 13:37:40 +08:00 committed by Jakub Kicinski
parent ddffc67d89
commit 9f8cb9cbcd
2 changed files with 123 additions and 50 deletions

View File

@ -1829,6 +1829,126 @@ int phylink_set_fixed_link(struct phylink *pl,
}
EXPORT_SYMBOL_GPL(phylink_set_fixed_link);
/**
* 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
*
* Update the MAC pause frame (flow control) state for the phylink instance.
*/
static void phylink_update_pause_state(struct phylink *pl, int pause_state)
{
struct phylink_link_state *config = &pl->link_config;
bool tx_pause = !!(pause_state & MLO_PAUSE_TX);
bool rx_pause = !!(pause_state & MLO_PAUSE_RX);
bool manual_changed;
mutex_lock(&pl->state_mutex);
/*
* See the comments for linkmode_set_pause(), wrt the deficiencies
* with the current implementation. A solution to this issue would
* be:
* ethtool Local device
* rx tx Pause AsymDir
* 0 0 0 0
* 1 0 1 1
* 0 1 0 1
* 1 1 1 1
* and then use the ethtool rx/tx enablement status to mask the
* rx/tx pause resolution.
*/
linkmode_set_pause(config->advertising, tx_pause,
rx_pause);
manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
(!(pause_state & MLO_PAUSE_AN) &&
(config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
config->pause = pause_state;
/* Update our in-band advertisement, triggering a renegotiation if
* the advertisement changed.
*/
if (!pl->phydev)
phylink_change_inband_advert(pl);
mutex_unlock(&pl->state_mutex);
/* If we have a PHY, a change of the pause frame advertisement will
* cause phylib to renegotiate (if AN is enabled) which will in turn
* call our phylink_phy_change() and trigger a resolve. Note that
* we can't hold our state mutex while calling phy_set_asym_pause().
*/
if (pl->phydev)
phy_set_asym_pause(pl->phydev, rx_pause, tx_pause);
/* If the manual pause settings changed, make sure we trigger a
* resolve to update their state; we can not guarantee that the
* link will cycle.
*/
if (manual_changed) {
pl->link_failed = true;
phylink_run_resolve(pl);
}
}
/**
* phylink_update_mac_pause_capabilities() - Dynamically update MAC pause
* @pl: a pointer to a &struct phylink returned from phylink_create()
* @mac_pause: the new MAC pause capabilities mask
*
* This function allows a MAC driver to dynamically change its pause state,
* such as losing/gaining Pause frame support based on MTU size.
* It recalculates supported link modes and triggers renegotiation if needed.
*/
void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac_pause)
{
struct phylink_link_state *config = &pl->link_config;
unsigned long old_pause;
int pause_state;
ASSERT_RTNL();
if (mac_pause & ~(MAC_SYM_PAUSE | MAC_ASYM_PAUSE)) {
phylink_err(pl, "Attempted to dynamically change non-pause MAC capabilities\n");
return;
}
old_pause = pl->config->mac_capabilities & (MAC_SYM_PAUSE | MAC_ASYM_PAUSE);
if (old_pause == mac_pause)
return;
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);
pause_state = config->pause;
if (!phylink_test(pl->supported, Pause)) {
pause_state &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
} else if (!phylink_test(pl->supported, Asym_Pause)) {
if ((pause_state & MLO_PAUSE_RX) ^ (pause_state & MLO_PAUSE_TX))
pause_state &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
}
mutex_unlock(&pl->state_mutex);
phylink_update_pause_state(pl, pause_state);
}
EXPORT_SYMBOL_GPL(phylink_update_mac_pause_capabilities);
/**
* phylink_create() - create a phylink instance
* @config: a pointer to the target &struct phylink_config
@ -3188,8 +3308,6 @@ EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
int phylink_ethtool_set_pauseparam(struct phylink *pl,
struct ethtool_pauseparam *pause)
{
struct phylink_link_state *config = &pl->link_config;
bool manual_changed;
int pause_state;
ASSERT_RTNL();
@ -3213,54 +3331,7 @@ int phylink_ethtool_set_pauseparam(struct phylink *pl,
if (pause->tx_pause)
pause_state |= MLO_PAUSE_TX;
mutex_lock(&pl->state_mutex);
/*
* See the comments for linkmode_set_pause(), wrt the deficiencies
* with the current implementation. A solution to this issue would
* be:
* ethtool Local device
* rx tx Pause AsymDir
* 0 0 0 0
* 1 0 1 1
* 0 1 0 1
* 1 1 1 1
* and then use the ethtool rx/tx enablement status to mask the
* rx/tx pause resolution.
*/
linkmode_set_pause(config->advertising, pause->tx_pause,
pause->rx_pause);
manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
(!(pause_state & MLO_PAUSE_AN) &&
(config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
config->pause = pause_state;
/* Update our in-band advertisement, triggering a renegotiation if
* the advertisement changed.
*/
if (!pl->phydev)
phylink_change_inband_advert(pl);
mutex_unlock(&pl->state_mutex);
/* If we have a PHY, a change of the pause frame advertisement will
* cause phylib to renegotiate (if AN is enabled) which will in turn
* call our phylink_phy_change() and trigger a resolve. Note that
* we can't hold our state mutex while calling phy_set_asym_pause().
*/
if (pl->phydev)
phy_set_asym_pause(pl->phydev, pause->rx_pause,
pause->tx_pause);
/* If the manual pause settings changed, make sure we trigger a
* resolve to update their state; we can not guarantee that the
* link will cycle.
*/
if (manual_changed) {
pl->link_failed = true;
phylink_run_resolve(pl);
}
phylink_update_pause_state(pl, pause_state);
return 0;
}

View File

@ -843,4 +843,6 @@ void phylink_replay_link_begin(struct phylink *pl);
void phylink_replay_link_end(struct phylink *pl);
void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac_pause);
#endif