[PATCH net-next v23 09/15] net: phylink: support late PCS provider attach
From: Christian Marangi
Date: Sun Nov 29 2026 - 18:07:34 EST
Add support for late PCS provider attachment to a phylink instance. Add
FWNODE_PCS_PROVIDER_ADD to handle the case where a PCS provider becomes
available after a phylink instance has been created.
The PCS notifier emits FWNODE_PCS_PROVIDER_ADD after registering a provider
and releasing the provider mutex. Call fwnode_pcs_add_provider() and its
devm variant with RTNL unlocked since the notifier can take RTNL.
On the add event, refresh the phylink PCS list through
fill_available_pcs(). Attach newly available PCS to the phylink instance
and refresh supported_interfaces and link capabilities when the list
changes. If the PCS list changes during creation, finish any pending
capability updates and SFP retries at the end of phylink_create().
Defer optical SFP insertion with -EAGAIN when none of the module's
interfaces is currently usable and at least one requires a PCS. Retry
deferred insertion when a PCS is added to the phylink instance.
If major_config_failed is set on a running instance, the next phylink
resolve retries interface configuration and reevaluates the autonegotiation
mode for the selected PCS. A follow-up resolve checks the link state with
the new configuration, allowing a link torn down by PCS removal to come
back up.
Signed-off-by: Christian Marangi <ansuelsmth@xxxxxxxxx>
---
drivers/net/pcs/pcs.c | 4 +++
drivers/net/phy/phylink.c | 50 +++++++++++++++++++++++++++-----
include/linux/pcs/pcs-provider.h | 4 +++
include/linux/pcs/pcs.h | 1 +
4 files changed, 52 insertions(+), 7 deletions(-)
diff --git a/drivers/net/pcs/pcs.c b/drivers/net/pcs/pcs.c
index f04888432a42..6e7df0c792d0 100644
--- a/drivers/net/pcs/pcs.c
+++ b/drivers/net/pcs/pcs.c
@@ -71,6 +71,10 @@ fwnode_pcs_add_provider(struct fwnode_handle *fwnode,
mutex_unlock(&fwnode_pcs_mutex);
+ blocking_notifier_call_chain(&fwnode_pcs_notify_list,
+ FWNODE_PCS_PROVIDER_ADD,
+ pp);
+
pr_debug("Added pcs provider from %pfwf\n", fwnode);
return pp;
diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 6dde0eae0e73..3471ba8d5ba6 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -2604,6 +2604,21 @@ static bool phylink_pcs_changed(struct phylink *pl)
return true;
}
+static void phylink_sfp_retry_module(struct phylink *pl)
+{
+ struct sfp_bus *bus;
+
+ ASSERT_RTNL();
+
+ if (!pl->netdev)
+ return;
+
+ /* The SFP callbacks publish the attached bus under RTNL */
+ bus = pl->netdev->sfp_bus;
+ if (bus && bus == pl->sfp_bus)
+ sfp_upstream_retry_module(bus);
+}
+
static int phylink_update_pcs(struct phylink *pl, bool rescan)
{
struct phylink_config *config = pl->config;
@@ -2613,6 +2628,7 @@ static int phylink_update_pcs(struct phylink *pl, bool rescan)
unsigned int num_pcs, i;
bool removed = false;
bool added = false;
+ bool resolve;
int ret;
lockdep_assert_held(&pl->pcs_list_mutex);
@@ -2628,7 +2644,9 @@ static int phylink_update_pcs(struct phylink *pl, bool rescan)
return ret;
}
- if (rescan) {
+ /* An add notification can fill the PCS list before the initial update */
+ resolve = rescan || !list_empty(&pl->pcs_list);
+ if (resolve) {
rtnl_lock();
mutex_lock(&pl->phydev_mutex);
phydev = pl->phydev;
@@ -2672,10 +2690,12 @@ static int phylink_update_pcs(struct phylink *pl, bool rescan)
}
mutex_unlock(&pl->state_mutex);
- if (rescan) {
+ if (resolve) {
if (phydev)
mutex_unlock(&phydev->lock);
mutex_unlock(&pl->phydev_mutex);
+ if (rescan && added && ret > 0)
+ phylink_sfp_retry_module(pl);
rtnl_unlock();
}
@@ -2750,13 +2770,15 @@ static int pcs_provider_notify(struct notifier_block *self,
bool skip;
int ret;
- if (val != FWNODE_PCS_PROVIDER_DEL)
+ if (val != FWNODE_PCS_PROVIDER_ADD &&
+ 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);
+ (val == FWNODE_PCS_PROVIDER_DEL &&
+ !phylink_has_provider_pcs(pl, provider));
mutex_unlock(&pl->state_mutex);
if (skip) {
@@ -2774,7 +2796,8 @@ static int pcs_provider_notify(struct notifier_block *self,
* 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);
+ if (val == FWNODE_PCS_PROVIDER_DEL)
+ resolve = phylink_remove_provider_pcs(pl, provider);
}
mutex_unlock(&pl->pcs_list_mutex);
@@ -2787,6 +2810,7 @@ static int pcs_provider_notify(struct notifier_block *self,
static void phylink_pcs_finish_init(struct phylink *pl)
{
struct phy_device *phy;
+ bool pending;
/* SFP module callbacks can run once the upstream is registered */
if (pl->sfp_bus)
@@ -2797,7 +2821,8 @@ static void phylink_pcs_finish_init(struct phylink *pl)
mutex_lock(&phy->lock);
mutex_lock(&pl->state_mutex);
- if (pl->pcs_state == PCS_STATE_INIT_PENDING) {
+ pending = pl->pcs_state == PCS_STATE_INIT_PENDING;
+ if (pending) {
phylink_parse_supported_interfaces(pl);
phylink_update_capabilities(pl, &pl->link_config);
}
@@ -2807,8 +2832,11 @@ static void phylink_pcs_finish_init(struct phylink *pl)
if (phy)
mutex_unlock(&phy->lock);
mutex_unlock(&pl->phydev_mutex);
- if (pl->sfp_bus)
+ if (pl->sfp_bus) {
+ if (pending)
+ phylink_sfp_retry_module(pl);
rtnl_unlock();
+ }
}
static void phylink_detach_pcss(struct phylink *pl)
@@ -4789,6 +4817,14 @@ static int phylink_sfp_module_insert(void *upstream,
ASSERT_RTNL();
caps = sfp_get_module_caps(pl->sfp_bus);
+ /* A late PCS may provide the module's only usable interface */
+ if (!caps->may_have_phy &&
+ !bitmap_intersects(caps->interfaces, pl->supported_interfaces,
+ PHY_INTERFACE_MODE_MAX) &&
+ bitmap_intersects(caps->interfaces, pl->config->pcs_interfaces,
+ PHY_INTERFACE_MODE_MAX))
+ return -EAGAIN;
+
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);
diff --git a/include/linux/pcs/pcs-provider.h b/include/linux/pcs/pcs-provider.h
index 2505c280946f..afcb7b878130 100644
--- a/include/linux/pcs/pcs-provider.h
+++ b/include/linux/pcs/pcs-provider.h
@@ -31,6 +31,8 @@ struct phylink_pcs *fwnode_pcs_simple_xlate(struct fwnode_reference_args *pcsspe
* A PCS object belongs to one provider and must remain valid until that
* provider is removed.
*
+ * Call without holding RTNL.
+ *
* Returns: A pointer to the registered PCS provider on success, or
* an ERR_PTR() encoded error code on failure.
*/
@@ -72,6 +74,8 @@ void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp);
* the PCS hardware inaccessible, use fwnode_pcs_add_provider() and call
* fwnode_pcs_del_provider() before shutting down the hardware.
*
+ * Call without holding RTNL.
+ *
* 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 e7d1fcd65357..6a5f0b6858ab 100644
--- a/include/linux/pcs/pcs.h
+++ b/include/linux/pcs/pcs.h
@@ -5,6 +5,7 @@
#include <linux/phylink.h>
enum fwnode_pcs_notify_event {
+ FWNODE_PCS_PROVIDER_ADD,
FWNODE_PCS_PROVIDER_DEL,
};
--
2.55.0