From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795993595; b=CtxOjmeLSUp86OKL4Ttv3rL47OR80o8krrcWCTZbbHBo+LJCTrYmAKDxMTvJcm4L0MOQ ySBBBoBKzucMBAOz83YWFBEWMuTVb/oFnjjXdG9xYtyhTpGQ63H2cMZrZOUXJKB+eyzNP DYE3I1T306wBG8Nfu7agkuvNx7SAMdJSK1JS8gYY0w28vJ+r4KEn4CQ8sywfKe9OqkIw3 O2dYVbEwQ/QG74IdBRjQtEqtB4Er/A7C79B5I+5hqORbbbsd/N1khf5tHuLTSNF1HpNna 1RNZpWb1yPSbFQbNn5eSvjrINe68zqQE9WsN2gPa9LDJhJUe0PUGyj71oyYxZyBVzag== 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=r9am0Pb1YLxDM5WDpcc+poBEKfDQnb5/q9arLj2uGKNxGPVv5IKoEO+fRX9TqwUrmvJu 3RmzlC+d+tMA/WbrSiXFb/X0FiZViqSEWvxNVFu5EqGh1k4sdq0L74+klSAhaebFf8ve7 dtcI6BKbe5KYYcWND9uQ/4I+8Zyke0lfT0qF8KcoOPeDpvqDu0959eunPXdDcet/JvYJn 7H0kjq2QsxlCMpytdsXrzkdP64X7DgBiYS4H2wLEy4EPvmIym/7rZXZvbg7Miq6N81Mlr tbNPut58vySJRFByyuSlhBfv+Q24jx3+jXPTF6aPuKDmzZ5gO6MM7QsvmgRgSpdXyaQ== 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=AieY80iZ; arc=none smtp.remote-ip=209.85.221.52 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 45441606A6A6 for ; Sun, 29 Nov 2026 23:06:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 45441606A6A6 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=AieY80iZ Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-49412eb1474so6499719f8f.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=lists.linux.dev; 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=AieY80iZsXwDJT64Z7hlES+dp0AcSpTsasE2hCz40DtFTHQjx4NiBw4wA8qjJrZw89 ViDBG1pAKUly8CfAHZHGLm/4ADn1z5BD85tGrGwJaYzCKTu+Gypm4Q2XgMuxPnFjU8/I ZE6ZQ8hK1jyhgg0WeMXYd1mrKxHML/+eI13TKrOKaOf5TJuSqVaowMpRkY+XUK5Us0So 0xY2Sf6setVS5nov7UJBKpWcLhjH+5eD0AIE3Edi3WDoeU2gSgCJM+Yx3WO3CuYhCXza vZS0N8+9Gcj/RJ5Wz7ubg3RqeYtI7BORJj8VeRm2I0YKwXhr1mu59SozgFjiZcqFPSX/ 2xig== 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=Az2DP4R0l5BjFeQckNaaVssGxLBx1N4W73c263eciNbBH1aVAXznSTDt35aGd/znv7 hHTIw1P8VQZodtgKqqw5OQa8FV0fx8cFXT/Vhx9srno2O5QU1CbjbVbONNGaRs2AmKpy SOZNise1XcZWAAIscTjR7iHMBOzbJoZgeD5aFJ+DENk5L/OjML3yzb6gQ3tiBEsZd8Mj otVDB7juzIrOM2rsYkYjOeY+5f+D/wx7Q5+IFfSyp9hlViL+lx/EnsWlwOhVFLk7nFCi t6u09k5nPxUGI3wbIiEY80rKhjd17IkqZS5WxMlor/f8qjerCAoHvFe8eo5WaGJuNEsJ s01g== X-Forwarded-Encrypted: i=1; AL+hErEh6T+KeKLA/BHWarjTsX7i1VirW/WPnbm5tmTXiXBxF/JLBohmGCLxhbisV/Bi9R+4P2BW@lists.linux.dev X-Gm-Message-State: APuU4Z9H8RELIwVrAcBG+ZpRdCwDXFlldpc2f4dvtP71/Fw6YEqEZAFc u6tRc5GsOYthYcQKGNKtP6gR8ysPpUzho8KUT+al17KXJlE8X2XRZZLU X-Gm-Gg: AZFdTZd86UBN/C3IdmNnTArL0X3PQWHR7mFEECVevqMb+w3O/jxLaM8AHHjuOJU61xI VbtqLmiHrB7oXUvCM/FgeUxgtOMMepQTE8VLVbDIpEb7/s4EmsyWOuU0hK+VNRhviF2FT34C7Bt 6Ap20p1xY+VbWSgjiPSVH29fdE4EYmfG1fh/lNhJzWQeFPjr+NRI7g8cPlvLqSICTIzhS8VXFnI 8OMFLGZaJCX6nUjzH0/paxXukpmn0ieP3HuMEkwrcjidLooG1BmjNLzoMUOxEGGeyGTtatkYZha 6fNjj6hvCe08/aGcBUTnGnqXRU2GyGaJPrfx9UUUQnVt22HZKYqOUsX8TWxEgJqWPFXWWo8EHFe DA8Rq915HpJ1yWcSUGqv+j2ZPq/YW8i/uu2EbDUdffP8enrStQq7qkoYZkqM/ASe7mPFlBMVe8u jumDVu485U91oW5M5ZgN/8YHVnIqyt+Q2GFWG4dNqiUwYRHLMoLvVRXysMBgvzORrZPfcNH33ox qKBZGOgYGtumgQzWazJ6Cm1+Kdky3OxpopK5ujsRpSjjFu459nZiNNCFTxZQQdULovv7fa9cwYZ NH7r1BGaQ2KUiwXMxQyn6OGRQvprhlsOXUq186HWqqvdMNgF5Y2IOLJUKYpt0omas+n/9xmRyW7 plpdOQqtmbjh2NGHb5A== 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: llvm@lists.linux.dev 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