From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BE57606A325 for ; Sun, 29 Nov 2026 23:08:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=wWvwukQ2; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=j+Kg1n1I; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=DPtD2Tnn DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:To:From:Reply-To: Cc:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=g1bESwzhTQxCbhWX6h2MnbWCJs5OhDCjOmRWAPYTHyk=; b=wWvwukQ2rRHrT8 b7U2UkAp/IFq5u3zPXX6uAI3E4u/nzNeA70CgaI18jSmTbDyGKuJSnwk7/VuMijNXJqjgNkHLPxYF g2PkVvAyrP+ilkGfZxvAnZfU0u/JHrmiAhaATtFdZNvkx3ZS9cPrJUySxAthdG+wkVyveqeh9XlfJ XzT+VJWhejSayj6dLCChXBaKZnTdwje71quBGK11unrTakb6DwnYq+dOLU8qoASIBFI3JhgRYdAF2 Wnz7Rd/M0M4vNmA16AhMqewOkEyF8d2FXzwnZypcmv32A4VtNXuct+q7L9V+HbaoEzL6FeHV7k8Jy sI6JNbCDWicT3aQDKy7A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXo01-00000002aFF-2xc5; Sun, 29 Nov 2026 23:08:01 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXnyl-00000002ZZF-2cVD for linux-mediatek@bombadil.infradead.org; Sun, 29 Nov 2026 23:06:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc: Content-Type:Content-ID:Content-Description; bh=g1bESwzhTQxCbhWX6h2MnbWCJs5OhDCjOmRWAPYTHyk=; b=j+Kg1n1ICKPEip65GHN2ZnYkfk odDSPZ07xf7HhGPbcaSsULpq9/FptbVBPUV1AvID3014isoC8+bvVeXBly2YQxVjUxwLKgCh8ZUiK 5ie8Db4adYeDYg0e0JFDNa+2dXFkOje+WESCrasTWiWRZ78QpxEH/PKVj6NIxHtjBqEOXLgTXBzAz ZMFPf9HLe2dK5TVtRBgfi/KbhV9ipxuFLULQHaPzcSDIKHEVOu5v3I2IsTNPG67mBs2WFZHqtQMTd 3n8k4BtlAh8lEycBYTct00SCrB0fI3YGqObgGYxl1yXhsoJY/6uEJAbgQJ7zXAdkyNmcnq1/HbJb8 WrNA40Hg==; Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by casper.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXnyh-00000003dhJ-2KGY for linux-mediatek@lists.infradead.org; Sun, 29 Nov 2026 23:06:42 +0000 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-498f5fa4f70so8245913f8f.0 for ; Sun, 29 Nov 2026 15:06:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795993597; x=1796598397; darn=lists.infradead.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=g1bESwzhTQxCbhWX6h2MnbWCJs5OhDCjOmRWAPYTHyk=; b=DPtD2Tnnfl8NBwn7b38gq/hf1JzbS13ftDsiu9CMoyjoSPlY19cRds71+9zrpUZSog v0eeO+vzmK80XnY0k8YJwJiRQMiRTCIAH9sesDipCai/CsGoWHwvp2dCJovAjxR6ZjQW c94xPYQ81QyQaXjzuIw+rbimYvPYkY7XkBxRB1v0DPVKwT3Vr0DIu9GU3jq0MSpy4JeD J4fLiZLxWuDA7kudCvQ/MnhLh4YgAU2GGJW2twoU6zOxlVELGmYDbUnDEsi8atMIfDop fTYwnfrvuY51SRZnKC9yK297I0yp6Dx/+yKWcLL6mt9j9g+mf8VdNFrME2Cj0mX2DVuk qemQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795993597; x=1796598397; 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=g1bESwzhTQxCbhWX6h2MnbWCJs5OhDCjOmRWAPYTHyk=; b=DUb8iXG/q9Ry1ly/VvZWzaODE3OrtwO/N9KSpYxYgKFwJf6SKxNFFKJCp2ui2ELsZf m6e6vJi5D22ZM/nHkz6edR5t6Kl2gu6DeM3jAVUdn0d+8ICVc20An007d6PirKhjfAWh ZEAxe1xaPvYPrVJmsWotLFyVt7TwzAoIuRpJqK4n7niP/Qp1SA9QWLjDAJH99E3Nq1kB wHKboLQkFUmrt9sxeYkHKuRVKqRXu5VvIdVkpuRa0Xu1RarRq/WzabcKc5p4KcOsuPJz aG7Ccp0nGPt1aff8XrSYgtLhp301WMGg7ip8h31AoFQeXwDz0N3ubt4lTQsOk0OR+ao9 KOlA== X-Forwarded-Encrypted: i=1; AL+hErHvPuuXC06JYe3Xo28GlCcJM0O4Bg70WkI4opdOmyFI3RKYwN5ccgQFv/U3kozIkX5vp9VZlEqwzfTDcxHHNQ==@lists.infradead.org X-Gm-Message-State: APuU4Z/EZNZApwhnevajNvtRelwyfK3/dqYyoHubudkjccmm7JNlTJHK JxAYZEs0I4YPLaZr2fdLh7n+SfAJW9mZ9hgK4HJKjPyEZb5XfNYLv2/8 X-Gm-Gg: AZFdTZd3UDOcWt5wfexBOAQq3LekEEWsgUSIgdwM8m0X7K4cRi5cT5+9JIq+ImnDpIz 3lF+I0nrJVIl37U8PSqL0CKpj7UOxGXZyDHeuFjNWdc4rBBELCyAZVatLZUXVjS2Vgr/X9beuQX pxhtG1gOtUFWnn+yItgxUBv+krq6gERiJSWovcuEW0XSzkYmrguBfHwHABqU9RAp+2YjI/kQXHf x46kyGFS7nN8WYQGihX+jFjGh8xfsPFbmO06UpU/GnHnA14XrGIFjYIzlt7L5pVXcNTFLbJoeZJ GJ1Cm47wzrBOENWhNyaH7LlWpuCMqOTBlDCdFi2AmwEktvt5nweNg/6gx0pGmAwL8+NXx91G/7m NmJElX1UsAYPNYvXL+CGad+AwXYHxglB5N6eapNSjeyZKwrydl7zGeQZYVLZnNSUa0pMGUuB7xl fQb53kQ4rCMA6o76+A3foFbdNT13W/0QIyqoME2tvzbHpji7hNHf8Qi17CDS6daQKlDPtL5LQ0h eUq7OkZmY+DqTbALTBz33CyC0zQZ+c4MBCZLBPyPMbOl88idhFqSc7/ZjSZO87dwV56TfvygG4J auMqEP+UZhwUQPiPFqGTwUEr+VNSFvlZpcxcTgRQhj9IZ5tyXQkHugm/GQAb59EGGyhwmXNXJEI N9QupK9dx394P5+u0mA== X-Received: by 2002:a05:6000:1847:b0:4c6:e973:cf27 with SMTP id ffacd0b85a97d-4c6e973cf96mr755252f8f.35.1795993597296; Sun, 29 Nov 2026 15:06:37 -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.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 15:06:36 -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 08/15] net: phylink: support PCS provider release Date: Mon, 30 Nov 2026 00:05:38 +0100 Message-ID: <20261129230558.612526-9-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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261129_230639_756611_491448B1 X-CRM114-Status: GOOD ( 28.38 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Add support for release of fwnode PCS from a PCS provider. Use a global blocking notifier chain and subscribe each phylink instance that uses internal PCS handling before populating its PCS list. The PCS notifier emits FWNODE_PCS_PROVIDER_DEL after the provider is removed from the global provider list. Phylink detaches the provider's PCS synchronously and may call their operations during detach. Call fwnode_pcs_del_provider() with RTNL unlocked and keep the PCS and its hardware resources available until it returns. Phylink uses fwnode_pcs_matches_provider() to check whether any PCS in its list belongs to the provider being released. If so, refresh the PCS list through fill_available_pcs() and detach PCS that are no longer available. If the refresh fails, remove the provider's PCS directly. Track additions and removals separately and wait for an RCU grace period after clearing the removed PCS pointers to phylink. When the list changes, rebuild supported_interfaces and recalculate the link capabilities. Defer these updates until initialization finishes if the notification arrives during phylink_create(). For optical SFPs, rebuild the usable interfaces from the module's original interface mask and retain its EEPROM limits when recalculating in-band capabilities. Clear stale supported modes and advertising if no modes remain usable. If the current interface becomes unavailable, retain supported modes for alternate interfaces. During major configuration, check that the selected interface is usable and that the selected PCS supports the requested in-band autonegotiation mode. If the active PCS is removed, bring the link down if needed, stop in-band polling, disable the PCS as needed and clear the active PCS pointer. Set force_major_config when phylink is enabled so resolve can reconfigure the link even if the interface mode is unchanged or the PHY link is down in in-band mode. If an SFP link lost its PCS while the signal was down, force reconfiguration when the signal returns. Finally, trigger phylink resolve for an enabled instance after updating the PCS list and capabilities. Serialize PCS list updates and teardown with provider notifications using pcs_list_mutex. Reuse the existing PCS ownership and state locking. Unregister the notifier during phylink teardown and on creation failure. Signed-off-by: Christian Marangi --- drivers/net/pcs/pcs.c | 31 +++ drivers/net/phy/phylink.c | 441 ++++++++++++++++++++++++++----- include/linux/pcs/pcs-provider.h | 12 + include/linux/pcs/pcs.h | 67 +++++ 4 files changed, 487 insertions(+), 64 deletions(-) diff --git a/drivers/net/pcs/pcs.c b/drivers/net/pcs/pcs.c index a0b3681db54b..f04888432a42 100644 --- a/drivers/net/pcs/pcs.c +++ b/drivers/net/pcs/pcs.c @@ -23,6 +23,19 @@ struct fwnode_pcs_provider { static LIST_HEAD(fwnode_pcs_providers); static DEFINE_MUTEX(fwnode_pcs_mutex); +static BLOCKING_NOTIFIER_HEAD(fwnode_pcs_notify_list); + +int register_fwnode_pcs_notifier(struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&fwnode_pcs_notify_list, nb); +} +EXPORT_SYMBOL_GPL(register_fwnode_pcs_notifier); + +int unregister_fwnode_pcs_notifier(struct notifier_block *nb) +{ + return blocking_notifier_chain_unregister(&fwnode_pcs_notify_list, nb); +} +EXPORT_SYMBOL_GPL(unregister_fwnode_pcs_notifier); struct phylink_pcs *fwnode_pcs_simple_xlate(struct fwnode_reference_args *pcsspec, void *data) @@ -91,6 +104,11 @@ void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp) mutex_unlock(&fwnode_pcs_mutex); + /* Signal phylink to release any PCS from this provider */ + blocking_notifier_call_chain(&fwnode_pcs_notify_list, + FWNODE_PCS_PROVIDER_DEL, + pp); + fwnode_pcs_free_provider(pp); } EXPORT_SYMBOL_GPL(fwnode_pcs_del_provider); @@ -220,6 +238,19 @@ struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode, unsigned } EXPORT_SYMBOL_GPL(fwnode_pcs_get); +bool fwnode_pcs_matches_provider(struct fwnode_pcs_provider *provider, + const struct phylink_pcs *pcs) +{ + bool matches; + + mutex_lock(&fwnode_pcs_mutex); + matches = pcs->provider == provider; + mutex_unlock(&fwnode_pcs_mutex); + + return matches; +} +EXPORT_SYMBOL_GPL(fwnode_pcs_matches_provider); + unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode) { struct fwnode_reference_args out_args; diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index 877a0a30ec22..6dde0eae0e73 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -11,8 +11,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -41,6 +43,9 @@ enum { PCS_STATE_DOWN = 0, PCS_STATE_STARTING, PCS_STATE_STARTED, + PCS_STATE_DETACHING, + PCS_STATE_INITIALIZING, + PCS_STATE_INIT_PENDING, }; /** @@ -72,6 +77,7 @@ struct phylink { /* List of available PCS */ struct list_head pcs_list; + struct notifier_block fwnode_pcs_nb; /* * What interface are supported by the current link. @@ -87,6 +93,8 @@ struct phylink { struct timer_list link_poll; struct mutex state_mutex; + /* Serialize PCS list updates with provider notifications */ + struct mutex pcs_list_mutex; /* Serialize updates to pl->phydev with phylink_resolve() */ struct mutex phydev_mutex; struct phylink_link_state phy_state; @@ -1084,6 +1092,24 @@ static unsigned int phylink_pcs_inband_caps(struct phylink_pcs *pcs, return 0; } +static bool phylink_inband_autoneg_valid(unsigned int caps, + const unsigned long *adv) +{ + unsigned int mode; + + /* If the PCS doesn't implement inband support, be permissive */ + if (!caps) + return true; + + if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv)) + mode = LINK_INBAND_ENABLE; + else + mode = LINK_INBAND_DISABLE; + + /* Check whether the PCS implements the required mode */ + return caps & mode; +} + static void phylink_pcs_pre_config(struct phylink_pcs *pcs, phy_interface_t interface) { @@ -1504,6 +1530,19 @@ static void phylink_major_config(struct phylink *pl, bool restart, } } + /* A replacement PCS may require a different autonegotiation mode */ + if (!pl->phydev && !phy_interface_empty(pl->sfp_module_interfaces)) { + unsigned int caps = phylink_pcs_inband_caps(pcs, state->interface); + + if (!test_bit(state->interface, pl->sfp_interfaces) || + !phylink_inband_autoneg_valid(caps, state->advertising)) { + phylink_err(pl, "incompatible SFP configuration for %s\n", + phy_modes(state->interface)); + pl->major_config_failed = true; + return; + } + } + pcs_changed = pl->pcs != pcs; phylink_pcs_neg_mode(pl, pcs, state->interface, state->advertising); @@ -2183,32 +2222,6 @@ static bool phylink_add_pcs(struct phylink *pl, struct phylink_pcs *pcs) return added; } -static int phylink_fill_available_pcs(struct phylink *pl, - struct phylink_config *config) -{ - struct phylink_pcs **pcss; - int i, ret; - - if (!config->num_possible_pcs) - return 0; - - pcss = kzalloc_objs(*pcss, config->num_possible_pcs); - if (!pcss) - return -ENOMEM; - - ret = config->fill_available_pcs(config, pcss, config->num_possible_pcs); - if (ret < 0) - goto out; - - for (i = 0; i < config->num_possible_pcs; i++) - phylink_add_pcs(pl, pcss[i]); - -out: - kfree(pcss); - - return ret; -} - /** * phylink_update_pause_state() - Update the phylink pause frame configuration * @pl: a pointer to a &struct phylink instance @@ -2385,35 +2398,46 @@ static void phylink_update_capabilities(struct phylink *pl, } /* - * Revalidate SFP modes before generic in-band capabilities, so module - * EEPROM limits are retained for in-band SFP configurations. + * Revalidate SFP modes against the current MAC and PCS interfaces + * before generic in-band capabilities to preserve the module's + * EEPROM limits. Keep the module's original interface mask so a + * later PCS addition can make an interface usable again. */ - if (pl->sfp_bus && config->interface != PHY_INTERFACE_MODE_NA && - !phy_interface_empty(pl->sfp_module_interfaces)) { + if (pl->sfp_bus && !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_zero(pl->advertising); + 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); + pl->sfp_interfaces)) { + /* No SFP modes remain usable with the current interfaces */ + linkmode_zero(pl->supported); + phy_interface_zero(pl->sfp_interfaces); + goto exit; + } /* * Keep alternate modes available for interface selection, but * only advertise modes that the current interface can use. */ + linkmode_copy(pl->supported, supported); + if (!test_bit(config->interface, pl->sfp_interfaces)) + goto exit; + linkmode_copy(active_supported, pl->sfp_support); - if (__phylink_validate(pl, active_supported, &sfp_config)) - return; + if (__phylink_validate(pl, active_supported, &sfp_config)) { + __clear_bit(config->interface, pl->sfp_interfaces); + goto exit; + } - linkmode_copy(pl->supported, supported); + phylink_sfp_update_capabilities(pl, pl->supported, &sfp_config, + config->interface); linkmode_copy(pl->advertising, sfp_config.advertising); goto exit; } @@ -2494,6 +2518,42 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac } EXPORT_SYMBOL_GPL(phylink_update_mac_pause_capabilities); +static void phylink_del_pcs(struct phylink *pl, struct phylink_pcs *pcs) +{ + ASSERT_RTNL(); + + lockdep_assert_held(&pl->state_mutex); + + /* + * Check if we are removing the PCS currently + * in use by this phylink instance. If this is the case, + * tear down the link, force phylink resolve to reconfigure the + * interface mode, disable the current PCS and set the + * phylink PCS to NULL. + */ + if (pl->pcs == pcs) { + if (pl->old_link_state) { + phylink_link_down(pl); + pl->old_link_state = false; + } + if (pl->cfg_link_an_mode == MLO_AN_INBAND) + timer_delete_sync(&pl->link_poll); + + if (pl->pcs_state != PCS_STATE_DOWN) + phylink_pcs_disable(pl->pcs); + + if (!pl->phylink_disable_state) + pl->force_major_config = true; + + WRITE_ONCE(pl->pcs, NULL); + } + + mutex_lock(&phylink_pcs_ownership_mutex); + rcu_assign_pointer(pcs->phylink, NULL); + list_del(&pcs->list); + mutex_unlock(&phylink_pcs_ownership_mutex); +} + static void phylink_parse_supported_interfaces(struct phylink *pl) { struct phylink_pcs *pcs; @@ -2523,12 +2583,243 @@ static void phylink_parse_supported_interfaces(struct phylink *pl) } } +static bool phylink_pcs_changed(struct phylink *pl) +{ + lockdep_assert_held(&pl->state_mutex); + + /* Defer capability updates until creation finishes */ + if (pl->pcs_state == PCS_STATE_INITIALIZING || + pl->pcs_state == PCS_STATE_INIT_PENDING) { + pl->pcs_state = PCS_STATE_INIT_PENDING; + return false; + } + + /* Skip forcing a major config if we are disabled */ + if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state) && + (pl->force_major_config || pl->major_config_failed)) { + pl->force_major_config = true; + pl->link_failed = true; + } + + return true; +} + +static int phylink_update_pcs(struct phylink *pl, bool rescan) +{ + struct phylink_config *config = pl->config; + struct phy_device *phydev = NULL; + struct phylink_pcs *pcs, *tmp; + struct phylink_pcs **pcss; + unsigned int num_pcs, i; + bool removed = false; + bool added = false; + int ret; + + lockdep_assert_held(&pl->pcs_list_mutex); + + num_pcs = config->num_possible_pcs; + pcss = kzalloc_objs(*pcss, num_pcs); + if (!pcss) + return -ENOMEM; + + ret = config->fill_available_pcs(config, pcss, num_pcs); + if (ret < 0) { + kfree(pcss); + return ret; + } + + if (rescan) { + rtnl_lock(); + mutex_lock(&pl->phydev_mutex); + phydev = pl->phydev; + if (phydev) + mutex_lock(&phydev->lock); + } + mutex_lock(&pl->state_mutex); + + /* Detach unavailable PCS and clear retained PCS from the array */ + list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list) { + bool keep = false; + + for (i = 0; i < num_pcs; i++) { + if (pcss[i] != pcs) + continue; + + pcss[i] = NULL; + keep = true; + } + if (keep) + continue; + + phylink_del_pcs(pl, pcs); + removed = true; + } + + /* Wait for readers that obtained phylink through a removed PCS */ + if (removed) + synchronize_rcu(); + + /* Attach the remaining PCS to this instance */ + for (i = 0; i < num_pcs; i++) + if (phylink_add_pcs(pl, pcss[i])) + added = true; + + if (rescan) + ret = (added || removed) && phylink_pcs_changed(pl); + if (!rescan || ret) { + phylink_parse_supported_interfaces(pl); + phylink_update_capabilities(pl, &pl->link_config); + } + + mutex_unlock(&pl->state_mutex); + if (rescan) { + if (phydev) + mutex_unlock(&phydev->lock); + mutex_unlock(&pl->phydev_mutex); + rtnl_unlock(); + } + + kfree(pcss); + + return ret; +} + +static bool phylink_has_provider_pcs(struct phylink *pl, + struct fwnode_pcs_provider *provider) +{ + struct phylink_pcs *pcs; + + lockdep_assert_held(&pl->state_mutex); + + list_for_each_entry(pcs, &pl->pcs_list, list) + if (fwnode_pcs_matches_provider(provider, pcs)) + return true; + + return false; +} + +/* Only used when refreshing the PCS list fails during provider removal */ +static bool phylink_remove_provider_pcs(struct phylink *pl, + struct fwnode_pcs_provider *provider) +{ + struct phylink_pcs *pcs, *tmp; + struct phy_device *phydev; + bool removed = false; + bool resolve = false; + + lockdep_assert_held(&pl->pcs_list_mutex); + + rtnl_lock(); + mutex_lock(&pl->phydev_mutex); + phydev = pl->phydev; + if (phydev) + mutex_lock(&phydev->lock); + mutex_lock(&pl->state_mutex); + + list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list) { + if (!fwnode_pcs_matches_provider(provider, pcs)) + continue; + + phylink_del_pcs(pl, pcs); + removed = true; + } + if (removed) { + synchronize_rcu(); + resolve = phylink_pcs_changed(pl); + } + if (resolve) { + phylink_parse_supported_interfaces(pl); + phylink_update_capabilities(pl, &pl->link_config); + } + + mutex_unlock(&pl->state_mutex); + if (phydev) + mutex_unlock(&phydev->lock); + mutex_unlock(&pl->phydev_mutex); + rtnl_unlock(); + + return resolve; +} + +static int pcs_provider_notify(struct notifier_block *self, + unsigned long val, void *data) +{ + struct phylink *pl = container_of(self, struct phylink, fwnode_pcs_nb); + struct fwnode_pcs_provider *provider = data; + bool resolve; + bool skip; + int ret; + + if (val != FWNODE_PCS_PROVIDER_DEL) + return NOTIFY_DONE; + + mutex_lock(&pl->pcs_list_mutex); + mutex_lock(&pl->state_mutex); + skip = pl->pcs_state == PCS_STATE_DETACHING || + !phylink_has_provider_pcs(pl, provider); + mutex_unlock(&pl->state_mutex); + + if (skip) { + mutex_unlock(&pl->pcs_list_mutex); + return NOTIFY_DONE; + } + + ret = phylink_update_pcs(pl, true); + resolve = ret > 0; + if (ret < 0) { + phylink_warn(pl, "failed to refresh available PCS: %pe\n", + ERR_PTR(ret)); + /* + * Handle case where a PCS refresh failed and we still have to make + * sure to remove any PCS provided by the provider that is getting + * removed. Signal to user and forcefully remove them. + */ + resolve = phylink_remove_provider_pcs(pl, provider); + } + mutex_unlock(&pl->pcs_list_mutex); + + if (resolve) + phylink_run_resolve(pl); + + return ret > 0 ? NOTIFY_OK : NOTIFY_DONE; +} + +static void phylink_pcs_finish_init(struct phylink *pl) +{ + struct phy_device *phy; + + /* SFP module callbacks can run once the upstream is registered */ + if (pl->sfp_bus) + rtnl_lock(); + mutex_lock(&pl->phydev_mutex); + phy = pl->phydev; + if (phy) + mutex_lock(&phy->lock); + mutex_lock(&pl->state_mutex); + + if (pl->pcs_state == PCS_STATE_INIT_PENDING) { + phylink_parse_supported_interfaces(pl); + phylink_update_capabilities(pl, &pl->link_config); + } + pl->pcs_state = PCS_STATE_DOWN; + + mutex_unlock(&pl->state_mutex); + if (phy) + mutex_unlock(&phy->lock); + mutex_unlock(&pl->phydev_mutex); + if (pl->sfp_bus) + rtnl_unlock(); +} + static void phylink_detach_pcss(struct phylink *pl) { struct phylink_pcs *pcs, *tmp; + mutex_lock(&pl->pcs_list_mutex); mutex_lock(&pl->state_mutex); + pl->pcs_state = PCS_STATE_DETACHING; + mutex_lock(&phylink_pcs_ownership_mutex); /* Remove every PCS from phylink PCS list */ list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list) { @@ -2550,6 +2841,11 @@ static void phylink_detach_pcss(struct phylink *pl) synchronize_rcu(); mutex_unlock(&pl->state_mutex); + mutex_unlock(&pl->pcs_list_mutex); + + /* Unregister notifier for late PCS attach */ + if (pl->fwnode_pcs_nb.notifier_call) + unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb); } /** @@ -2604,6 +2900,7 @@ struct phylink *phylink_create(struct phylink_config *config, return ERR_PTR(-ENOMEM); mutex_init(&pl->phydev_mutex); + mutex_init(&pl->pcs_list_mutex); mutex_init(&pl->state_mutex); INIT_WORK(&pl->resolve, phylink_resolve); INIT_LIST_HEAD(&pl->pcs_list); @@ -2646,19 +2943,30 @@ struct phylink *phylink_create(struct phylink_config *config, __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); timer_setup(&pl->link_poll, phylink_fixed_poll, 0); - /* Fill the PCS list with available PCS from phylink config */ - ret = phylink_fill_available_pcs(pl, config); - if (ret < 0) - goto free_pl; - - mutex_lock(&pl->state_mutex); - phylink_parse_supported_interfaces(pl); - phylink_update_capabilities(pl, &pl->link_config); - mutex_unlock(&pl->state_mutex); + if (has_pcs_interfaces) { + /* Register the notifier before filling the PCS list */ + pl->pcs_state = PCS_STATE_INITIALIZING; + pl->fwnode_pcs_nb.notifier_call = pcs_provider_notify; + register_fwnode_pcs_notifier(&pl->fwnode_pcs_nb); + + mutex_lock(&pl->pcs_list_mutex); + ret = phylink_update_pcs(pl, false); + mutex_unlock(&pl->pcs_list_mutex); + if (ret < 0) { + phylink_err(pl, "failed to populate initial PCS list: %pe\n", + ERR_PTR(ret)); + goto unregister_pcs_notify; + } + } else { + mutex_lock(&pl->state_mutex); + phylink_parse_supported_interfaces(pl); + phylink_update_capabilities(pl, &pl->link_config); + mutex_unlock(&pl->state_mutex); + } ret = phylink_parse_mode(pl, fwnode); if (ret < 0) - goto unlink_pcs_list; + goto unregister_pcs_notify; if (pl->cfg_link_an_mode == MLO_AN_FIXED) { ret = phylink_parse_fixedlink(pl, fwnode); @@ -2672,13 +2980,18 @@ struct phylink *phylink_create(struct phylink_config *config, if (ret < 0) goto release_link_gpio; + if (pl->fwnode_pcs_nb.notifier_call) + phylink_pcs_finish_init(pl); + return pl; release_link_gpio: if (pl->link_gpio) gpiod_put(pl->link_gpio); -unlink_pcs_list: +unregister_pcs_notify: phylink_detach_pcss(pl); + /* previous PCS notifier might have queued a resolve, cancel it */ + cancel_work_sync(&pl->resolve); free_pl: kfree(pl); return ERR_PTR(ret); @@ -2702,6 +3015,7 @@ void phylink_destroy(struct phylink *pl) gpiod_put(pl->link_gpio); phylink_detach_pcss(pl); + cancel_work_sync(&pl->resolve); kfree(pl); @@ -3627,20 +3941,8 @@ static bool phylink_validate_pcs_inband_autoneg(struct phylink *pl, phy_interface_t interface, unsigned long *adv) { - unsigned int inband = phylink_inband_caps(pl, interface); - unsigned int mask; - - /* If the PCS doesn't implement inband support, be permissive. */ - if (!inband) - return true; - - if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv)) - mask = LINK_INBAND_ENABLE; - else - mask = LINK_INBAND_DISABLE; - - /* Check whether the PCS implements the required mode */ - return !!(inband & mask); + return phylink_inband_autoneg_valid(phylink_inband_caps(pl, interface), + adv); } /** @@ -4632,7 +4934,18 @@ static void phylink_sfp_link_up(void *upstream) ASSERT_RTNL(); - phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK); + mutex_lock(&pl->state_mutex); + /* The PCS may have been removed while the signal was lost */ + if (pl->phylink_disable_state == BIT(PHYLINK_DISABLE_LINK) && + !pl->pcs && test_bit(pl->link_config.interface, + pl->config->pcs_interfaces)) { + pl->force_major_config = true; + pl->link_failed = true; + } + clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state); + mutex_unlock(&pl->state_mutex); + + phylink_run_resolve(pl); } static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy) diff --git a/include/linux/pcs/pcs-provider.h b/include/linux/pcs/pcs-provider.h index 6b03a635f73c..2505c280946f 100644 --- a/include/linux/pcs/pcs-provider.h +++ b/include/linux/pcs/pcs-provider.h @@ -43,6 +43,12 @@ fwnode_pcs_add_provider(struct fwnode_handle *fwnode, /** * fwnode_pcs_del_provider - Removes a PCS provider * @pp: PCS provider returned by fwnode_pcs_add_provider() + * + * Phylink detaches the provider's PCS instances synchronously and may call + * their operations during detach. Keep the PCS instances and the resources + * needed to access their hardware available until this function returns. + * + * Call without holding RTNL. */ void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp); @@ -60,6 +66,12 @@ void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp); * On driver detach, release function is invoked on the devres data, * then, devres data is freed. * + * Register the provider after acquiring all resources its PCS operations + * need, so devres unregisters it before releasing those resources. Devres + * cleanup runs after the driver's remove callback. If remove needs to make + * the PCS hardware inaccessible, use fwnode_pcs_add_provider() and call + * fwnode_pcs_del_provider() before shutting down the hardware. + * * Returns: A pointer to the registered PCS provider on success, or * an ERR_PTR() encoded error code on failure. */ diff --git a/include/linux/pcs/pcs.h b/include/linux/pcs/pcs.h index b3bb158002d5..e7d1fcd65357 100644 --- a/include/linux/pcs/pcs.h +++ b/include/linux/pcs/pcs.h @@ -4,7 +4,42 @@ #include +enum fwnode_pcs_notify_event { + FWNODE_PCS_PROVIDER_DEL, +}; + +struct fwnode_pcs_provider; + #if IS_ENABLED(CONFIG_FWNODE_PCS) +/** + * register_fwnode_pcs_notifier - Register a notifier block for fwnode + * PCS events + * @nb: pointer to the notifier block + * + * Registers a notifier block to the fwnode_pcs_notify_list blocking + * notifier chain. This allows phylink instance to subscribe for + * PCS provider events. + * + * Call without holding RTNL. + * + * Returns: 0 or a negative error. + */ +int register_fwnode_pcs_notifier(struct notifier_block *nb); + +/** + * unregister_fwnode_pcs_notifier - Unregister a notifier block for fwnode + * PCS events + * @nb: pointer to the notifier block + * + * Unregisters a notifier block to the fwnode_pcs_notify_list blocking + * notifier chain. + * + * Call without holding RTNL. + * + * Returns: 0 or a negative error. + */ +int unregister_fwnode_pcs_notifier(struct notifier_block *nb); + /** * fwnode_pcs_get - Retrieves a PCS from a firmware node * @fwnode: firmware node @@ -20,6 +55,22 @@ struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode, unsigned int index); +/** + * fwnode_pcs_matches_provider() - Check whether a PCS belongs to a provider + * @provider: PCS provider to check + * @pcs: PCS to check + * + * Check the provider recorded when the PCS was first looked up. + * + * This function is intended to be used when handling provider removal + * notifications, where the provider is already known and its PCS need to be + * identified among the PCS associated with a phylink instance. + * + * Returns: true if pcs belongs to provider, false otherwise. + */ +bool fwnode_pcs_matches_provider(struct fwnode_pcs_provider *provider, + const struct phylink_pcs *pcs); + /** * fwnode_phylink_pcs_count - Count PCS entries described in firmware node * @fwnode: firmware node @@ -52,12 +103,28 @@ int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode, struct phylink_pcs **available_pcs, unsigned int num_pcs); #else +static inline int register_fwnode_pcs_notifier(struct notifier_block *nb) +{ + return -EOPNOTSUPP; +} + +static inline int unregister_fwnode_pcs_notifier(struct notifier_block *nb) +{ + return -EOPNOTSUPP; +} + static inline struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode, unsigned int index) { return ERR_PTR(-ENOENT); } +static inline bool fwnode_pcs_matches_provider(struct fwnode_pcs_provider *provider, + const struct phylink_pcs *pcs) +{ + return false; +} + static inline unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode) { return 0; -- 2.55.0