From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795993600; b=yf8JppXJ/YcHz7/AQ+Dd65xZp9ppMX+WI+OKqULCOuYWzepMQokRLDEixHw81D0xkrKG Sa7CEMtOZ9kHZEMXUP6nWXT0HWCUBHPrj3LlPfGxLgRZjQNZDY4t5hbWcIPzN0Tl39XmH oepVpCijQHJBKTRlVCL9qoKT5np9+6PzbI+3BXwV1Vo9HxROKUITEPgjGseT1bVOnCtXl qhViENUjZqBLHnScXAx9RepnAWW+eO8Kcl8wggGHzGGrq04mr9za2zzgpszHecTah6/WP fGnQ0nLzCPvNs3yC0tBoK7dg+J3+R5PlHiwi4BIk8Mrm7F73emsieZIyaIc/u119Ccg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795993600; 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=g1bESwzhTQxCbhWX6h2MnbWCJs5OhDCjOmRWAPYTHyk=; b=Z2vCKFsjomiEKDixb7ezkIAFlVEa1Fhz95UsbyByE2b1Bc3w/Q92Bm3zUv2hE+4agNnu apzaxTONQn3y+41Y1n73DX3BGTFli09ocd7xpjq1pzVYZfyOlWNsR+IkUG8TQpWJO3WUy JwqqKsbt1heSDP59dFDb1yIlkv0NNi+kptN1THSymPwgsdApNU964qtY0zx1CaSafrjyC Ea6F1eL6IFsCDvkguan3znhDwCQWjup0/JhDSQ22jlzAw5ED0cAwquK/o7isum5lHTxHD QuxeQ8tfYw1Q3pHm5TlKbUDOC20WeHbXnXa3MDw3Gu8Rfjs6+EaG2D8Y3fIyr16ZcrQ== 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="d3/zkYgp"; arc=none smtp.remote-ip=209.85.221.51 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 1D08460A260E for ; Sun, 29 Nov 2026 23:06:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1D08460A260E 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=d3/zkYgp Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-4ac08f4a0b2so6906247f8f.3 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=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=g1bESwzhTQxCbhWX6h2MnbWCJs5OhDCjOmRWAPYTHyk=; b=d3/zkYgpwPMcbG+zD2GAF1k735gxdIe9WhADAmLkAt6CXGcdXOyx5TM8Lyw3MvHErb l84VDqV16pmukFbiKrSsjna6lQEmZWYwv8HLxKVlsSMSnz04BGBUMUi55atx/Z2gFWfl ZalfXf3u3AToeVqDGHQipCVPHX9ZpkVi0DQrtt1tjdH7bzCuj0My0d41N8XAK5UCaF5H 3CdJTp7wskvoBzOtlSNi7Gv4PdmlvmL6dx6JvkYOqbV1GIwVZdN6N0iV2PtETfNK5UJ2 WlGu7pvKcKy5tOOP/Pxk+ninF4K044vPVyEV0pUnrNjFhw6dblIWl0W2M1C+PtR/iTTg bzJg== 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=lrIr/MlvrkdkWgB9DuJwHJAlMYUEPDLwKxa7dqVOYFcKL+YKLFgP+OtbYcM/6tAMph tW+CfmQlz3jPaOUNFRBMgqaSEasYtol2VoG1ky+zBo8fyNinFBVQ0RWvq4S2r/7j/Py8 qyg7p4dAeVoCvuT4j3wMvUVsweDBl20S2igkX4lPBIzog5pyV8F5kCLOGa8TyXGgZb6x 4SJ5lu2XjJQNFxhqkEga0Xgy/CAZpnHUoiKUtjnOj96pyo0OYybF6EHLCL6ylQhIWOEj x02c9DaiWBqZ2KidjXBBxuJRxCCyzWfc1bJmePJtWLFxE+oDsLTQmDPWrLUL/iR76ruA MmbQ== X-Forwarded-Encrypted: i=1; AL+hErH88tGu1PekOXJqyV5fhfbhyjnegVQlN3EfRhcZAlD4d3xxgW+5iESPRb3YtaygdxyvpnIRpFI=@vger.kernel.org X-Gm-Message-State: APuU4Z+Pvqgwg/czatrjxsj0SmwDjKtUmBywZzLxnDwy042LSkO8RyHo zPVTLoUIXvTIGG1pzuKEY05vLeJ7T9fYA9NGKnN0NTsrgeyG1SI3TMVB X-Gm-Gg: AZFdTZc5H4sjwtzH9Afh1UBpkCcUesE7HeFrTP6GJ1m3vQZVOCuDbHKWZ4imZHHKyvI OXNzbkMzhYZsnbVbwoM9d/PfA9Bfqx/MnaV6CO0mt314z/uG/sfW/8/FsY+r4+9WY/hdoKONu1I DJ3ZupEa2f/a0fGABb2eHhiCZkI0izpqlM3oVUWRfQguaOd6PzxYufefvHW80NjLbAGyuMeIuub Nz9vVW4H55jSxpL5ZdWDxfSw2VGDTMenwOnkUL0AqwWmMrxrXlt98KP3gbg7JVYMPLwFzvl8sXT dYzdGsS377NuUSrYULC0tzQOFs+Xyh5rzo+yVfRuSsVD5rI4jbNxDMUOtIxJnkZcD3uWKYT2tlI nRcrWyFqErDm8JQM6PbKqwUp/J8UMyLeiSAp5UkE8E+IvPOZ9H6M3SF9MyxiWQplCGuy4ETAEvr 9yJVgH48CkUw3815Z3RPYCcKNYpmygWvuhUeo5xQ3MugaMzWG6JnV6JV/POT7zG0SCkJRigvHIy 1p6srvZKdS05RUGLUFsdZW4dzG2988OvZBB5SmaK8uVuwqeRhzPb1r+vNQ/L8GhQwHyN2zJxwf3 xoj+FhFmtmpRQUlsD0R7RE87HNPajVxJUD+KyC4yUNZneaIepHgBq9Msrrib+cyv3nTd7Afjr+V quIs6TJZ088u1k5xPJQ== 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-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 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