From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795993595; b=YOtduAonG5zbfzUgEXW3pZrYuWHiZ4+n68fgADqOg/WaFmBNr81If66qzOtXtB6hiLo2 iVzE8JE73yobLR1VtfD6EVegs++gGMHNINMnvbdM4aQSY+Tp6GWV6W2+2p5iB5vyk26bz L5EdzuKG3Q7+iuEmUF0+/2LyOb+4r9ynCwhxKXalAuxx3ogN7rLx7ITcnIglQQsOR0O5O SSMI9lOouvIflUhrAlmJ8vx5VW/BW9BUb4PbERCiCNPVM776ybkLvBlAfzKGbFAWNQBV4 8LxHE3gqvceULluXM0s/BW/ZO0XDIbl/L20gy61QfoOS+sMw6dvXnwmw07gpb3jJkEQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795993595; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=/E+pHy+qIVryt3MGsVvyepZ/BBtwy53L/nna2Nt2XJI=; b=qGkHKdObBCLpY9Q77fy8kJ4pDekb1TSanTaUAh1Enz/2ZI97gN85v3yXEWNyYOe6oxaV Tk5vgk9gWNLhEWDCrfdQ+TZFnOZ8vTpjT8I7kxi+L2yNyr6l0O4M5zTcX7v5GgyWp/w2b Vva6M21iz9ijfU00xoB7xk8fo6lzmhPUfgcNlcwgIAFkCuGgIXTGBVeFE/LKBj8V2ZJhT sTvRB8gAMFq/0rMDHCJA9fZc2Idl4byFnRKXSIC1qJnCjX8qkujfPu4GF3LKRPSi4MPBQ DGmKuTF4/el2fh9pmNBYh+bxrXI8RIyjuW2i4Hky8POxVKp3ZfBPnqhMKpx18PzM8yA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=0iKIJDiD; arc=none smtp.remote-ip=209.85.221.50 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 285CD606A33D for ; Sun, 29 Nov 2026 23:06:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 285CD606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=0iKIJDiD Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-49412eb1474so6499717f8f.3 for ; Sun, 29 Nov 2026 15:06:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795993592; x=1796598392; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=/E+pHy+qIVryt3MGsVvyepZ/BBtwy53L/nna2Nt2XJI=; b=0iKIJDiDN67MLz++SuTkTWuVvdTixZFJ97TCvblwXY30K5aEm4M00SxUZfzJG5xBBn 6u4evl6VjaqvaRkcKpp18qCaHr3MEF8MMVjih0eaJMqqzgj75Z5/W3vCwyZAzd+We0oj u/1bHp9isW/2yqaVQs6AWLQfvMqTc+//A6avNHskKSWi5IEnV1LG0WtKcz0qqATII5f+ Yc74O+WYE7uu4LvG+KBPa2ZbAsF/6Yc1XsqudI2vh8OBFX8VKQsQKWs0yl5SzemzVTkb jjf2uhwXYrvQAzphDvfZLmupdCPaFkMj7Y0exiNErVA0sb5p7lQVXcENpEXTeqI4bLgJ Clhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795993592; x=1796598392; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=/E+pHy+qIVryt3MGsVvyepZ/BBtwy53L/nna2Nt2XJI=; b=J6PY6wpAM5VqBxYEvCD1Ms+9WG/DcRoSgjsflJDPSq7GR2ZRB4bsJt+0OuHw13Q636 50vkSaERMCWhogbzbp0UlGKNMv4DSytvi3XOaqVyk4Vgepc5QeEWB2L35CWwBYhLLlZl Jc/aPSIKLXQcBXEV60LAJorl1ARk4DGHOSDuRCGrTMox/ZncCo743IbQeeYcnx/VmEtp KeKkpMl3uv8r2w/QONxp75ZKL5aYY6Lwe27XrzqOsqwz99wMnm1BDB5WeuEey6f0HE4l PzF+hW7xWV9OWx+WI+1YKNGdR+u4Z6fiVxswlBgY2mQRWaHrzx6dVA0P/mzc9Q+QLsQv 83iA== X-Forwarded-Encrypted: i=1; AL+hErFKcnojRJ2oyL2q2weRVHi/4TWMnLQhDSwrukYLny5tOePwRX829l6xmExUF+KFVgnoZlf2iWA=@vger.kernel.org X-Gm-Message-State: APuU4Z9mN3uzTjGMamGFoHUvQay5XioYAaGaoZH5EyxU7aMqBA7B6Lly ZX80weRRaQgVEUEb7CLoMhzzzdOPcI7egqatPafAtTjHTBN9ZpjvLQ6O X-Gm-Gg: AZFdTZfkmb2oz1gtnLgO7olMVkabYMjtejW20I1IGDbI7v7RgCVSXTXhYar33nYx5d6 9tvJNf2KjZv47O54FiHP5vREkS/n/k3TQHwedaltc3DrT29xl+nITIs9fNcSPz1pHg3JiLqd/JX YvKpdmev3POWATZugUXiqWs9f8eMHO4+dXFMtTciH6RgB2j3hSkyLNl+xS3iNG7zGwKWa2BMPmn +wSzhBKCP3C6wODJfsuQQ4SVFYl19shxco48UvH+NgV2aYfo6vjh+9oStEwXTZQQCYJtWYczHjn f5Ijr7BRdbf+Pc3raNb5FdU17Ul2KUrTkfHOG8HHk8HxA7ywTj15ZDl7TsqE+zBWRUm6awPO9ii AeCvYczsuTXCQo0yz/8GIJCUmGPdNNz1tS+zp8zy+6u0DuieM76I5XGolCl7zrjubx09bse08ah QCNhsksyfv7AAuIGGaQvqI/6BXP1vappun6Gf/pPyggWwx/DubYuy3/zyZWxOSq/ynB9bAlfZ6l cAwzEzpbseaFJR2PxUy0oo9Su9ZNmx+UB26dejYN3wZ7Od0xgAhyCxyotvgB5F2bO72GTauG1Wq s5TYVsGEruMDgUudsFIHTJiZKBbOivIWLv1wHp9uE8fx7qagtbYMiMPn7FAe7A9IhJACmmejYFn e4zEplx8UpMAIJo/uQQ== X-Received: by 2002:a05:6000:299c:20b0:4a7:ed95:7ca2 with SMTP id ffacd0b85a97d-4abb7b5c76bmr44856854f8f.52.1795993592356; Sun, 29 Nov 2026 15:06:32 -0800 (PST) Received: from Ansuel-XPS24 (93-34-92-103.ip49.fastwebnet.it. [93.34.92.103]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-4ab6c4353d6sm58448894f8f.56.2026.11.29.15.06.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 15:06:31 -0800 (PST) From: Christian Marangi To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Willem de Bruijn , Simon Horman , Jonathan Corbet , Shuah Khan , Randy Dunlap , Christian Marangi , Lorenzo Bianconi , Heiner Kallweit , Russell King , Maxime Chevallier , Philipp Zabel , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , "Russell King (Oracle)" , 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 Message-ID: <20261129230558.612526-7-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261129230558.612526-1-ansuelsmth@gmail.com> References: <20261129230558.612526-1-ansuelsmth@gmail.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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