Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS
@ 2026-11-29 23:05 Christian Marangi
  2026-11-29 23:05 ` [PATCH net-next v23 01/15] net: phylink: make PCS phylink instance access RCU protected Christian Marangi
                   ` (14 more replies)
  0 siblings, 15 replies; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

This series introduce a most awaited feature that is correctly
provide PCS with fwnode without having to use specific export symbol
and additional handling of PCS in phylink.

At times there were 2 different implementation (this and the one
from Sean) but Sean agreed that this can be picked and used in favor
of his implementation as long as his case with race condition is
correctly handled.

---
First the PCS fwnode:

The concept is to implement a producer-consumer API similar to other
subsystem like clock or PHY.

That seems to be the best solution to the problem as PCS driver needs
to be detached from phylink and implement a simple way to provide a
PCS while maintaining support for probe defer or driver removal.

To keep the implementation simple, the PCS driver devs needs some
collaboration to correctly implement this. This is O.K. as helper
to correctly implement this are provided hence it's really a matter
of following a pattern to correct follow removal of a PCS driver.

A PCS provider have to implement and call fwnode_pcs_add_provider() in
probe function and define an xlate function to define how the PCS
should be provided based on the requested interface and phandle spec
defined in fwnode (based on the #pcs-cells)

fwnode_pcs_get() is provided to provide a specific PCS declared in
fwnode at index.

A simple xlate function is provided for simple single PCS
implementation, fwnode_pcs_simple_xlate.

A PCS provider on driver removal must call fwnode_pcs_del_provider()
to delete itself as a provider.

---
Second PCS handling in phylink:

We have the PCS problem for the only reason that in initial
implementation, we permitted way too much flexibility to MAC driver
and things started to deviate. At times we couldn't think SoC
would start to put PCS outside the MAC hence it was OK to assume
they would live in the same driver. With the introduction of
10g in more consumer devices, we are observing a rapid growth
of this pattern with multiple PCS external to MAC.

To put a stop on this, the only solution is to give back to phylink
control on PCS handling and enforce more robust supported interface
definition from both MAC and PCS side.

This series introduce handling of PCS in phylink and try to deprecate
.mac_select_pcs.

Phylink now might contain a linked list of available PCS and
those will be used for PCS selection on phylink_major_config.

MAC driver needs to define pcs_interfaces mask in phylink_config
for every interface that needs a dedicated PCS.

These PCS needs to be provided to phylink at phylink_create time
by setting the .fill_available_pcs and .num_possible_pcs in phylink_config.
Helpers to parse PCS from fwnode are provided
fwnode_phylink_pcs_count() that will return the count of PCS entries
described in the firmware node and fwnode_phylink_pcs_parse() that will
fill a preallocated array of PCS pointer with the actual available PCS
(ignoring the one that still needs to be probed).

phylink_create() will fill the internal PCS list with the passed
array of PCS. phylink_major_config and other user of .mac_select_pcs
are adapted to make use of this new PCS list.

The supported interface value is also moved internally to phylink
struct. This is to handle late removal and addition of PCS.
(the bonus effect to this is giving phylink a clear idea of what
is actually supported by the MAC and his constraint with PCS)

The supported interface mask in phylink is done by OR the
supported_interfaces in phylink_config with every PCS in PCS list.

PCS removal is supported by forcing a mac_config, refresh the
supported interfaces and run a phy_resolve().

PCS late addition is supported by introducing a global notifier
for PCS provider. If a phylink have the pcs_interfaces mask not
zero, it's registered to this notifier.

PCS provider will emit a global PCS add event to signal any
interface that a new PCS might be available.

The function will then check if the PCS is related to the MAC
fwnode and add it accordingly.

A user for this new implementation is provided as an Airoha PCS
driver. This was also tested downstream with the IPQ95xx QCOM SoC
and with the help of Daniel also on the various Mediatek MT7988
SoC with both SFP cage implementation and DSA attached.

Lots of tests were done with driver unbind/bind and with interface
up/down also by adding print to make sure major_config_fail gets
correctly triggered and reset once the PCS comes back.

The dedicated commits have longer description on the implementation
so it's suggested to also check there for additional info.

It's worth to mention that OpenWrt is currently using this on
Mediatek SoC and QCOM ipq807x/ipq60xx/ipq50xx and Airoha are
already ported in staging tree for testing.

Also realtek is using this and proved to correctly work.

---
Due to the introduction of Sashiko and the introduction of
the notifier, a major reorg of the different scenario and capabilities
parsing was needed. This is to make the bot happy (in theory, but
it never is) and try to improve the state of the code by
moving the capabilitis parsing to a centralized place trying to
address all the scenario in one place.

---

---

Changes v23:
- Rebase onto net-next at 8b5aba6d328e.
- Wait for RCU readers on teardown even if the active PCS
  was already cleared. Share PCS attachment and teardown helpers and
  serialize ownership changes across phylink instances.
- Validate pcs_interfaces, num_possible_pcs and fill_available_pcs
  together in phylink_create(). Limit PCS additions to the supported
  interface mask to modes selected by pcs_interfaces.
- Track provider ownership on first lookup, require stable PCS instances
  from the xlate callback, use unsigned reference indexes and document
  lookup errors, RTNL requirements and provider resource lifetime.
- Drop the saved phylink firmware node and fwnode_pcs_get_from_provider().
  Rescan with fill_available_pcs() on provider add and remove. Serialize
  list updates with initialization and teardown, and detach departing
  PCS synchronously even when a rescan fails.
- Move PCS list and interface helpers into the initial PCS patch. Put
  in-band capability queries and initial major configuration under the
  state mutex, and validate an attached PHY with its mutex taken first.
- Add deferred SFP insertion and retry optical module insertion when a
  PCS becomes available.
- Retain raw SFP capabilities and EEPROM limits during recalculation.
  Clear cached capabilities after failed insertion, invalidate stale
  modes after PCS removal and check a replacement PCS's in-band
  autonegotiation mode. Reconfigure when the SFP signal returns after
  losing the PCS while the signal was down.
- Track user pause restrictions on links without a PHY. With a PHY,
  revalidate phylib's current advertisement, retain pause changes across
  PHY speed down and speed up, and complete EEE clock setup before
  publishing PHY-derived state.
- Update the PCS documentation for rescans, deferred insertion and
  advertisement ownership. Update commit descriptions, local variable
  ordering and networking comment formatting.
v22: https://lore.kernel.org/r/20261117123956.11981-1-ansuelsmth@gmail.com/

Changes v22:
- Rebase on top of net-next (no idea why patchwork reported failed to apply...)
Changes v21:
- Add fixes tag for the RCU patch
- Preserve user-disabled ethtool advertising during capability
  updates and SFP interface selection.
- Add additional kconfig select for Airoha PCS and Airoha Ethernet
- Fix wrong rcu_assign_pointer NULL in RCU patch
- Use "pcs-handle-names" as already defined (instead of "pcs-names")
- Skip phylink_pcs_disable if already disabled
- Skip phylink_major_config if phylink disabled
- Better handle capabilites from phylink_parse_mode and phylink_parse_fixedlink
- Set link_failed on late attach so that resolve is triggered 2 times
- Improve description of fwnode_pcs_get_from_provider()
- Improve capabilities tracking for PHY and SFP scenario
- Add variant for __phy_get_rate_matching
Changes v20:
- Fix double declaration error in Documentation
- Drop mutex from patch 8 for validate_mac_and_pcs
- Correctly use non lock version in __phylink_validate
Changes v19:
- Rebase on top of net-next
- RCU protect phylink from pcs struct
- Fix dead-lock on phylink_validate
- Handle possible PCS provider returning NULL (protective coding)
- Improve capabilities handling refresh
Changes v18:
- Improve handling on late PCS addition
- Fix wrong comment on airoha PCS driver
- Rebase on top of net-next
Changes v17:
- Rebase on top of net-next
- Add Review tag for DT patch
- Recalculate capabilities on late removal/attach
Changes v16:
- Rebase on top of net-next
- Add Tested-by tag
- Add ACK for airoha ethernet patch + nit fix
Changes v15:
- Rebase on top of net-next
- Fix Coccicheck  warning
Changes v14:
- Fix deadlock when fill_available_pcs fails
- Move notifier and fill down after resolve disable bit is set
- Fix kconfig description
Changes v13:
- Fix regression in fwnode_pcs_get_from_pcsspec from v12
- Fix reverse lock for phy_lock and state_mutex
- Better handle lock in phylink_validate_mac_and_pcs and inband
- Fix wrong pointer to fwnode_pcs_add_provider notifier
- Fix compilation error for new C type in pcs_provider_notify
Changes v12:
- Fix kdoc for function
- Rework PCS provider add and delete to be provider driven (instead of
  relaying on scan of fwnode every time) (this also handle
  multi PCS scenario)
- Even further rework state mutex locking for concurrent pcs list
- Restore MAC supported_interfaces validation in phylink_create()
  wrongly dropped.
- Use get/put for fwnode reference
- Fix possible unment depndency for FWNODE_PCS
- Fix almost impossible problem for PCS calibration
- Hold rtnl with PCS deletion
Changes v11:
- Rework PCS release to notifier way (fix race as pointed out by bot)
- Add Ack from Lorenzo for Airoha Ethernet driver
- Introduce devm variant for pcs_provider_add
- Better support firmware node reference for PCS late attach
- Better protect list iteration with state mutex
- Minor fixup to airoha pcs driver (flow control fixup)
- Rebase on top of net-next
Changes v10:
- Minor code cleanup and fixes as suggested by bot
- Drop devlink patch
- Add reviewed by tag
- Configure old_link_state on PCS release
- Unlink PCS on error in phylink_create()
- Use list_add_tail
- Fix typo in Airoha PCS driver
- Update phylink migration guide
- Better handle late PCS attach (idempotent function and
  add unser state_mutex)
- Use dedicated lock in Airoha ethernet driver
Changes v9:
- Rebase on top of net-next/main
- Drop early verification of supported_interface
  (it can also be provided by get_capabilites MAC OP)
Changes v8:
- Back to RFC (net-next closed)
- Address additional bug reported by Sashiko bot
  - Better handle priv interface for Airoha PCS driver
  - Better handle locking for modifying the PCS list in
    phylink code
  - Improve fwnode_phylink_pcs_parse() parsing on -ENOENT
  - Better handle error condition in phylink_create()
  - Turn down the link when current PCS is released
- Fix compilation warning caused by copy paste error
Changes v7:
- Address all the bug from the Sashiko bot
- Rename .num_available_pcs to .num_possible_pcs
- Link PCS in phylink_create()
- Correctly unregister the notifier on phylink_destroy()
- Introduce fwnode_phylink_pcs_count()
- Better handle locking in phylink for PCS handling
- Better handle unavailable PCS at phylink_create() time
- Improve Documentation file
- Other minor fixes to address suggestion from bot
- Rebase on top of net-next
Changes v6:
- Rebase on top of net-next
- Add Documentation files
- Add fw_devlink patch
- Fix some comments typo
- Rework the airoha_eth.c implementation with new multi serdes code
- Extend PCS code with PCIe and USB support
- Align schema to new property
Changes v5:
- Rebase on top of net-next
- Use the new force_major_config
- Reword some comments and commit description
- Return -ENODEV instead of -EPROBE_DEFER to perevent race condition
- Drop phy_interface_copy patch (Russell pushed an equivalent version)
Changes v4:
- Move patch 0002 phy_interface_copy to 0002 (fix bisectability
  problem)
- Address review from Lorenzo for Airoha ethernet driver
- Fix kdoc error with missing Return (actually missing : before Return)
- Fix UNMET dependency reported error for CONFIG_FWNODE_PCS
- Revert to pcs.c instead of core.c (due to name conflict with other kmod)
- Fix clang compilation error for Airoha PCS driver
- Add missing inline function to pcs.h function
Changes v3:
- Out of RFC
- Fix various spelling mistake
- Drop circular dependency patch
- Complete Airoha Ethernet phylink integration
- Introduce .pcs_link_down PCS OP
Changes v2:
- Switch to fwnode
- Implement PCS provider notifier
- Better split changes
- Move supported_interfaces to phylink
- Add circular dependency patch
- Rework handling with indirect addition/removal and
  trigger of phylink_resolve()

Christian Marangi (15):
  net: phylink: make PCS phylink instance access RCU protected
  net: phylink: keep and use MAC supported_interfaces in phylink struct
  net: phylink: introduce internal phylink PCS handling
  net: pcs: implement Firmware node support for PCS driver
  net: phylink: make phylink_validate state locked
  net: phylink: add capabilities helpers and track modified advertising
  net: sfp: support deferred module insertion
  net: phylink: support PCS provider release
  net: phylink: support late PCS provider attach
  net: Document PCS subsystem
  MAINTAINERS: add myself as PCS subsystem maintainer
  net: phylink: add .pcs_link_down PCS OP
  dt-bindings: net: pcs: Document support for Airoha Ethernet PCS
  net: pcs: airoha: add PCS driver for Airoha AN7581 SoC
  net: airoha: add phylink support

 .../bindings/net/pcs/airoha,pcs.yaml          |  261 ++
 Documentation/networking/index.rst            |    1 +
 Documentation/networking/pcs.rst              |  257 ++
 Documentation/networking/sfp-phylink.rst      |   35 +-
 MAINTAINERS                                   |    9 +
 drivers/net/ethernet/airoha/Kconfig           |    2 +
 drivers/net/ethernet/airoha/airoha_eth.c      |  197 +-
 drivers/net/ethernet/airoha/airoha_eth.h      |    5 +
 drivers/net/ethernet/airoha/airoha_regs.h     |   12 +
 drivers/net/pcs/Kconfig                       |    8 +
 drivers/net/pcs/Makefile                      |    3 +
 drivers/net/pcs/airoha/Kconfig                |   17 +
 drivers/net/pcs/airoha/Makefile               |    7 +
 drivers/net/pcs/airoha/pcs-airoha-common.c    | 1312 ++++++++++
 drivers/net/pcs/airoha/pcs-airoha.h           | 1311 ++++++++++
 drivers/net/pcs/airoha/pcs-an7581.c           | 2115 +++++++++++++++++
 drivers/net/pcs/pcs.c                         |  314 +++
 drivers/net/phy/phy-internal.h                |   22 +
 drivers/net/phy/phy.c                         |   11 +-
 drivers/net/phy/phy_device.c                  |    8 +-
 drivers/net/phy/phylink.c                     | 1498 +++++++++---
 drivers/net/phy/sfp-bus.c                     |   16 +
 drivers/net/phy/sfp.c                         |   17 +
 drivers/net/phy/sfp.h                         |    1 +
 include/linux/pcs/pcs-provider.h              |   88 +
 include/linux/pcs/pcs.h                       |  142 ++
 include/linux/phylink.h                       |   33 +-
 include/linux/sfp.h                           |   10 +-
 28 files changed, 7346 insertions(+), 366 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml
 create mode 100644 Documentation/networking/pcs.rst
 create mode 100644 drivers/net/pcs/airoha/Kconfig
 create mode 100644 drivers/net/pcs/airoha/Makefile
 create mode 100644 drivers/net/pcs/airoha/pcs-airoha-common.c
 create mode 100644 drivers/net/pcs/airoha/pcs-airoha.h
 create mode 100644 drivers/net/pcs/airoha/pcs-an7581.c
 create mode 100644 drivers/net/pcs/pcs.c
 create mode 100644 drivers/net/phy/phy-internal.h
 create mode 100644 include/linux/pcs/pcs-provider.h
 create mode 100644 include/linux/pcs/pcs.h

-- 
2.55.0



^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 01/15] net: phylink: make PCS phylink instance access RCU protected
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 02/15] net: phylink: keep and use MAC supported_interfaces in phylink struct Christian Marangi
                   ` (13 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

In the context of phylink_pcs_change(), phylink instance is dereferenced
from the pcs pointer present in the phylink_pcs struct.

There is currently a fragility where this pcs pointer is never set NULL in
phylink_destroy() and would reference invalid memory after phylink instance
is freed.

In this scenario, if a PCS driver calls phylink_pcs_change(), invalid
memory can be dereferenced and phylink_link_changed() is called on freed
memory.

To address this, make the phylink pointer saved in phylink_pcs struct RCU
protected and access it under an RCU read lock. Use rcu_assign_pointer()
to update the pointer in phylink_major_config().

Also correctly reset the phylink instance pointer on phylink_destroy() and
unconditionally synchronize RCU to wait for any in-flight
phylink_pcs_change() calls, even if phylink_major_config() already cleared
the association.

Fixes: 24699cc1ff3e ("net: phylink: add support for PCS link change notifications")
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/net/phy/phylink.c | 22 +++++++++++++++++++---
 include/linux/phylink.h   |  2 +-
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index d1f1142a98a4..d736604b86f4 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -17,6 +17,7 @@
 #include <linux/phy_link_topology.h>
 #include <linux/phy_port.h>
 #include <linux/phylink.h>
+#include <linux/rcupdate.h>
 #include <linux/rtnetlink.h>
 #include <linux/spinlock.h>
 #include <linux/timer.h>
@@ -1295,10 +1296,10 @@ static void phylink_major_config(struct phylink *pl, bool restart,
 		phylink_pcs_disable(pl->pcs);
 
 		if (pl->pcs)
-			pl->pcs->phylink = NULL;
+			rcu_assign_pointer(pl->pcs->phylink, NULL);
 
 		if (pcs)
-			pcs->phylink = pl;
+			rcu_assign_pointer(pcs->phylink, pl);
 
 		pl->pcs = pcs;
 	}
@@ -2157,6 +2158,16 @@ void phylink_destroy(struct phylink *pl)
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
 
+	if (pl->pcs)
+		rcu_assign_pointer(pl->pcs->phylink, NULL);
+
+	/*
+	 * Unconditionally wait for readers that may have obtained this
+	 * phylink through a PCS while phylink_major_config() was clearing
+	 * the association.
+	 */
+	synchronize_rcu();
+
 	cancel_work_sync(&pl->resolve);
 	kfree(pl);
 }
@@ -2621,10 +2632,15 @@ EXPORT_SYMBOL_GPL(phylink_mac_change);
  */
 void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
 {
-	struct phylink *pl = pcs->phylink;
+	struct phylink *pl;
 
+	rcu_read_lock();
+
+	pl = rcu_dereference(pcs->phylink);
 	if (pl)
 		phylink_link_changed(pl, up, "pcs");
+
+	rcu_read_unlock();
 }
 EXPORT_SYMBOL_GPL(phylink_pcs_change);
 
diff --git a/include/linux/phylink.h b/include/linux/phylink.h
index a7b6294cc4a0..27515d0bc57e 100644
--- a/include/linux/phylink.h
+++ b/include/linux/phylink.h
@@ -494,7 +494,7 @@ struct phylink_pcs_ops;
 struct phylink_pcs {
 	DECLARE_PHY_INTERFACE_MASK(supported_interfaces);
 	const struct phylink_pcs_ops *ops;
-	struct phylink *phylink;
+	struct phylink __rcu *phylink;
 	bool poll;
 	bool rxc_always_on;
 };
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 02/15] net: phylink: keep and use MAC supported_interfaces in phylink struct
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
  2026-11-29 23:05 ` [PATCH net-next v23 01/15] net: phylink: make PCS phylink instance access RCU protected Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-29 23:05 ` [PATCH net-next v23 03/15] net: phylink: introduce internal phylink PCS handling Christian Marangi
                   ` (12 subsequent siblings)
  14 siblings, 0 replies; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

Add in phylink struct a copy of supported_interfaces from phylink_config
and make use of that instead of relying on phylink_config value.

This in preparation for support of PCS handling internally to phylink
where a PCS can be removed or added after the phylink is created and we
need both a reference of the supported_interfaces value from
phylink_config and an internal value that can be updated with the new
PCS info.

Tested-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Reviewed-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/net/phy/phylink.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index d736604b86f4..20759fe47eaa 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -63,6 +63,12 @@ struct phylink {
 	/* The link configuration settings */
 	struct phylink_link_state link_config;
 
+	/*
+	 * What interface are supported by the current link.
+	 * Can change on removal or addition of new PCS.
+	 */
+	DECLARE_PHY_INTERFACE_MASK(supported_interfaces);
+
 	/* The current settings */
 	phy_interface_t cur_interface;
 
@@ -633,7 +639,7 @@ static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy,
 static int phylink_validate(struct phylink *pl, unsigned long *supported,
 			    struct phylink_link_state *state)
 {
-	const unsigned long *interfaces = pl->config->supported_interfaces;
+	const unsigned long *interfaces = pl->supported_interfaces;
 
 	if (state->interface == PHY_INTERFACE_MODE_NA)
 		return phylink_validate_mask(pl, NULL, supported, state,
@@ -1808,7 +1814,7 @@ static int phylink_create_sfp_cage_port(struct phylink *pl)
 	port->is_mii = true;
 	port->active = true;
 
-	phy_interface_and(port->interfaces, pl->config->supported_interfaces,
+	phy_interface_and(port->interfaces, pl->supported_interfaces,
 			  phylink_sfp_interfaces);
 	phy_port_update_supported(port);
 
@@ -2111,6 +2117,9 @@ 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);
 
+	phy_interface_copy(pl->supported_interfaces,
+			   pl->config->supported_interfaces);
+
 	linkmode_fill(pl->supported);
 	linkmode_copy(pl->link_config.advertising, pl->supported);
 	phylink_validate(pl, pl->supported, &pl->link_config);
@@ -2243,7 +2252,7 @@ static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
 		 * those which the host supports.
 		 */
 		phy_interface_and(interfaces, phy->possible_interfaces,
-				  pl->config->supported_interfaces);
+				  pl->supported_interfaces);
 
 		if (phy_interface_empty(interfaces)) {
 			phylink_err(pl, "PHY has no common interfaces\n");
@@ -3062,12 +3071,12 @@ static phy_interface_t phylink_sfp_select_interface(struct phylink *pl,
 		return interface;
 	}
 
-	if (!test_bit(interface, pl->config->supported_interfaces)) {
+	if (!test_bit(interface, pl->supported_interfaces)) {
 		phylink_err(pl,
 			    "selection of interface failed, SFP selected %s (%u) but MAC supports %*pbl\n",
 			    phy_modes(interface), interface,
 			    (int)PHY_INTERFACE_MODE_MAX,
-			    pl->config->supported_interfaces);
+			    pl->supported_interfaces);
 		return PHY_INTERFACE_MODE_NA;
 	}
 
@@ -3946,14 +3955,14 @@ static int phylink_sfp_config_optical(struct phylink *pl)
 
 	phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n",
 		    (int)PHY_INTERFACE_MODE_MAX,
-		    pl->config->supported_interfaces,
+		    pl->supported_interfaces,
 		    (int)PHY_INTERFACE_MODE_MAX,
 		    pl->sfp_interfaces);
 
 	/* Find the union of the supported interfaces by the PCS/MAC and
 	 * the SFP module.
 	 */
-	phy_interface_and(pl->sfp_interfaces, pl->config->supported_interfaces,
+	phy_interface_and(pl->sfp_interfaces, pl->supported_interfaces,
 			  pl->sfp_interfaces);
 	if (phy_interface_empty(pl->sfp_interfaces)) {
 		phylink_err(pl, "unsupported SFP module: no common interface modes\n");
@@ -4191,7 +4200,7 @@ static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
 
 	/* Set the PHY's host supported interfaces */
 	phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
-			  pl->config->supported_interfaces);
+			  pl->supported_interfaces);
 
 	phy_set_upstream_port(phy, pl->sfp_cage_port);
 
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 03/15] net: phylink: introduce internal phylink PCS handling
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
  2026-11-29 23:05 ` [PATCH net-next v23 01/15] net: phylink: make PCS phylink instance access RCU protected Christian Marangi
  2026-11-29 23:05 ` [PATCH net-next v23 02/15] net: phylink: keep and use MAC supported_interfaces in phylink struct Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 04/15] net: pcs: implement Firmware node support for PCS driver Christian Marangi
                   ` (11 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

Introduce internal handling of PCS for phylink. This is an alternative
way to .mac_select_pcs that moves the selection logic of the PCS entirely
to phylink with the usage of the supported_interfaces bitmap in the PCS
struct.

A MAC using internal PCS handling should provide a callback to fill
the available PCS in .fill_available_pcs in phylink_config and set
.num_possible_pcs to the number of elements in the array. The MAC should
also define a new bitmap, pcs_interfaces, in phylink_config to define
which interface modes require a dedicated PCS.

In phylink_create(), when .num_possible_pcs is nonzero, an array of PCS
pointers is allocated with .num_possible_pcs entries and
.fill_available_pcs is called with the phylink_config, the array and
its size.

The MAC will fill this array with pointers to the available PCS.

This array is then parsed and a linked list of PCS is created based on
the allocated PCS array filled by the MAC via .fill_available_pcs(),
skipping empty entries and PCS instances that are already attached.

Link each PCS to its phylink instance with rcu_assign_pointer(). Use a
global mutex to serialize the ownership check and updates across phylink
instances. Rebuild phylink's supported_interfaces from the MAC bitmap and
the PCS modes that are also set in pcs_interfaces.

Share PCS teardown between phylink_destroy() and the phylink_create()
error path. Clear the PCS associations, unlink the list entries and wait
for an RCU grace period before freeing phylink.

phylink_validate_mac_and_pcs(), phylink_major_config() and
phylink_inband_caps() are updated to support this new implementation
with the PCS list stored in phylink.

For interface modes set in pcs_interfaces, they walk the list and use
phylink_validate_pcs_interface() to find the first PCS that supports
the requested interface. phylink_major_config() fails if it cannot
find a PCS for that interface.

phylink_validate_pcs_interface() applies the same validation previously
used for a PCS returned by .mac_select_pcs. An empty supported_interfaces
bitmap in the PCS struct means that all interfaces are supported.

A MAC using the PCS list must set pcs_interfaces, .num_possible_pcs and
.fill_available_pcs together. phylink_create() returns -EINVAL if the
configuration is incomplete or if the PCS list is combined with
.mac_select_pcs.

The phylink pointer in struct phylink_pcs tracks when a PCS is attached
to a phylink instance. With internal PCS handling, the pointer stays
set while the PCS is on the list, even when another PCS is selected.

phylink_pcs_change() is also changed to verify that the PCS that triggered
a link change is the one that is currently used by the phylink instance.

The .mac_select_pcs implementation is not changed but it's expected that
every MAC driver migrates to the new implementation to later deprecate
and remove .mac_select_pcs.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/net/phy/phylink.c | 259 +++++++++++++++++++++++++++++++++-----
 include/linux/phylink.h   |  16 +++
 2 files changed, 244 insertions(+), 31 deletions(-)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 20759fe47eaa..35e82cccd39a 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -27,6 +27,9 @@
 #include "sfp.h"
 #include "swphy.h"
 
+/* Serializes changes to PCS list ownership across phylink instances */
+static DEFINE_MUTEX(phylink_pcs_ownership_mutex);
+
 enum {
 	PHYLINK_DISABLE_STOPPED,
 	PHYLINK_DISABLE_LINK,
@@ -63,6 +66,9 @@ struct phylink {
 	/* The link configuration settings */
 	struct phylink_link_state link_config;
 
+	/* List of available PCS */
+	struct list_head pcs_list;
+
 	/*
 	 * What interface are supported by the current link.
 	 * Can change on removal or addition of new PCS.
@@ -523,6 +529,23 @@ static void phylink_validate_mask_caps(unsigned long *supported,
 	linkmode_and(state->advertising, state->advertising, mask);
 }
 
+static int phylink_validate_pcs_interface(struct phylink_pcs *pcs,
+					  phy_interface_t interface)
+{
+	/*
+	 * If PCS define an empty supported_interfaces value, assume
+	 * all interface are supported.
+	 */
+	if (phy_interface_empty(pcs->supported_interfaces))
+		return 0;
+
+	/* Ensure that this PCS supports the interface mode */
+	if (!test_bit(interface, pcs->supported_interfaces))
+		return -EINVAL;
+
+	return 0;
+}
+
 static int phylink_validate_mac_and_pcs(struct phylink *pl,
 					unsigned long *supported,
 					struct phylink_link_state *state)
@@ -536,6 +559,21 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl,
 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
 		if (IS_ERR(pcs))
 			return PTR_ERR(pcs);
+	/*
+	 * Find a PCS in available PCS list for the requested interface.
+	 *
+	 * Skip searching if the MAC doesn't require a dedicated PCS for
+	 * the requested interface.
+	 */
+	} else if (test_bit(state->interface, pl->config->pcs_interfaces)) {
+		struct phylink_pcs *tmp;
+
+		list_for_each_entry(tmp, &pl->pcs_list, list) {
+			if (!phylink_validate_pcs_interface(tmp, state->interface)) {
+				pcs = tmp;
+				break;
+			}
+		}
 	}
 
 	if (pcs) {
@@ -550,13 +588,10 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl,
 			return -EINVAL;
 		}
 
-		/* Ensure that this PCS supports the interface which the MAC
-		 * returned it for. It is an error for the MAC to return a PCS
-		 * that does not support the interface mode.
-		 */
-		if (!phy_interface_empty(pcs->supported_interfaces) &&
-		    !test_bit(state->interface, pcs->supported_interfaces)) {
-			phylink_err(pl, "MAC returned PCS which does not support %s\n",
+		/* Recheck PCS to handle legacy way for .mac_select_pcs */
+		ret = phylink_validate_pcs_interface(pcs, state->interface);
+		if (ret) {
+			phylink_err(pl, "selected PCS does not support %s\n",
 				    phy_modes(state->interface));
 			return -EINVAL;
 		}
@@ -969,12 +1004,22 @@ static void phylink_pcs_enable_eee(struct phylink_pcs *pcs)
 static unsigned int phylink_inband_caps(struct phylink *pl,
 					 phy_interface_t interface)
 {
-	struct phylink_pcs *pcs;
+	struct phylink_pcs *pcs = NULL;
 
-	if (!pl->mac_ops->mac_select_pcs)
-		return 0;
+	if (pl->mac_ops->mac_select_pcs) {
+		pcs = pl->mac_ops->mac_select_pcs(pl->config,
+						  interface);
+	} else if (test_bit(interface, pl->config->pcs_interfaces)) {
+		struct phylink_pcs *tmp;
+
+		list_for_each_entry(tmp, &pl->pcs_list, list) {
+			if (!phylink_validate_pcs_interface(tmp, interface)) {
+				pcs = tmp;
+				break;
+			}
+		}
+	}
 
-	pcs = pl->mac_ops->mac_select_pcs(pl->config, interface);
 	if (IS_ERR_OR_NULL(pcs))
 		return 0;
 
@@ -1271,10 +1316,37 @@ static void phylink_major_config(struct phylink *pl, bool restart,
 			pl->major_config_failed = true;
 			return;
 		}
+	/*
+	 * Find a PCS in available PCS list for the requested interface.
+	 * This doesn't overwrite the previous .mac_select_pcs as either
+	 * .mac_select_pcs or PCS list implementation are permitted.
+	 *
+	 * Skip searching if the MAC doesn't require a dedicated PCS for
+	 * the requested interface.
+	 */
+	} else if (test_bit(state->interface, pl->config->pcs_interfaces)) {
+		struct phylink_pcs *tmp;
+
+		list_for_each_entry(tmp, &pl->pcs_list, list) {
+			if (!phylink_validate_pcs_interface(tmp,
+							    state->interface)) {
+				pcs = tmp;
+				break;
+			}
+		}
+
+		if (!pcs) {
+			phylink_err(pl,
+				    "couldn't find a PCS for %s\n",
+				    phy_modes(state->interface));
 
-		pcs_changed = pl->pcs != pcs;
+			pl->major_config_failed = true;
+			return;
+		}
 	}
 
+	pcs_changed = pl->pcs != pcs;
+
 	phylink_pcs_neg_mode(pl, pcs, state->interface, state->advertising);
 
 	phylink_dbg(pl, "major config, active %s/%s/%s\n",
@@ -1301,13 +1373,15 @@ static void phylink_major_config(struct phylink *pl, bool restart,
 	if (pcs_changed) {
 		phylink_pcs_disable(pl->pcs);
 
-		if (pl->pcs)
-			rcu_assign_pointer(pl->pcs->phylink, NULL);
+		if (pl->mac_ops->mac_select_pcs) {
+			if (pl->pcs)
+				rcu_assign_pointer(pl->pcs->phylink, NULL);
 
-		if (pcs)
-			rcu_assign_pointer(pcs->phylink, pl);
+			if (pcs)
+				rcu_assign_pointer(pcs->phylink, pl);
+		}
 
-		pl->pcs = pcs;
+		WRITE_ONCE(pl->pcs, pcs);
 	}
 
 	if (pl->pcs)
@@ -1922,6 +1996,58 @@ int phylink_set_fixed_link(struct phylink *pl,
 }
 EXPORT_SYMBOL_GPL(phylink_set_fixed_link);
 
+static bool phylink_add_pcs(struct phylink *pl, struct phylink_pcs *pcs)
+{
+	struct phylink *owner;
+	bool added = false;
+
+	if (!pcs)
+		return false;
+
+	mutex_lock(&phylink_pcs_ownership_mutex);
+	owner = rcu_access_pointer(pcs->phylink);
+	if (owner == pl)
+		goto out;
+
+	if (WARN(owner, "%s: PCS already attached to a phylink instance\n",
+		 __func__))
+		goto out;
+
+	list_add_tail(&pcs->list, &pl->pcs_list);
+	rcu_assign_pointer(pcs->phylink, pl);
+	added = true;
+
+out:
+	mutex_unlock(&phylink_pcs_ownership_mutex);
+	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
@@ -2042,6 +2168,62 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac
 }
 EXPORT_SYMBOL_GPL(phylink_update_mac_pause_capabilities);
 
+static void phylink_parse_supported_interfaces(struct phylink *pl)
+{
+	struct phylink_pcs *pcs;
+
+	phy_interface_copy(pl->supported_interfaces,
+			   pl->config->supported_interfaces);
+
+	/*
+	 * Only add PCS-supported modes that phylink will select a PCS for.
+	 * Otherwise, phylink may accept a PCS-only mode and configure it without
+	 * a PCS.
+	 */
+	list_for_each_entry(pcs, &pl->pcs_list, list) {
+		DECLARE_PHY_INTERFACE_MASK(pcs_interfaces);
+
+		phy_interface_copy(pcs_interfaces, pl->config->pcs_interfaces);
+
+		if (!phy_interface_empty(pcs->supported_interfaces))
+			phy_interface_and(pcs_interfaces, pcs_interfaces,
+					  pcs->supported_interfaces);
+
+		phy_interface_or(pl->supported_interfaces,
+				 pl->supported_interfaces,
+				 pcs_interfaces);
+	}
+}
+
+static void phylink_detach_pcss(struct phylink *pl)
+{
+	struct phylink_pcs *pcs, *tmp;
+
+	mutex_lock(&pl->state_mutex);
+
+	mutex_lock(&phylink_pcs_ownership_mutex);
+	/* Remove every PCS from phylink PCS list */
+	list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list) {
+		rcu_assign_pointer(pcs->phylink, NULL);
+		list_del(&pcs->list);
+	}
+
+	if (pl->pcs) {
+		rcu_assign_pointer(pl->pcs->phylink, NULL);
+		WRITE_ONCE(pl->pcs, NULL);
+	}
+	mutex_unlock(&phylink_pcs_ownership_mutex);
+
+	/*
+	 * Unconditionally wait for readers that may have obtained this
+	 * phylink through a PCS while phylink_major_config() was clearing
+	 * the association.
+	 */
+	synchronize_rcu();
+
+	mutex_unlock(&pl->state_mutex);
+}
+
 /**
  * phylink_create() - create a phylink instance
  * @config: a pointer to the target &struct phylink_config
@@ -2063,9 +2245,25 @@ struct phylink *phylink_create(struct phylink_config *config,
 			       phy_interface_t iface,
 			       const struct phylink_mac_ops *mac_ops)
 {
+	bool has_pcs_interfaces = !phy_interface_empty(config->pcs_interfaces);
+	bool has_pcs_callback = config->fill_available_pcs;
+	bool has_pcs_count = config->num_possible_pcs > 0;
 	struct phylink *pl;
 	int ret;
 
+	if (has_pcs_interfaces != has_pcs_callback ||
+	    has_pcs_interfaces != has_pcs_count) {
+		dev_err(config->dev,
+			"phylink: error: pcs_interfaces, num_possible_pcs and fill_available_pcs must be set together\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	if (mac_ops->mac_select_pcs && has_pcs_interfaces) {
+		dev_err(config->dev,
+			"phylink: error: either phylink_config .mac_select_pcs or pcs_interfaces must be used\n");
+		return ERR_PTR(-EINVAL);
+	}
+
 	/* Validate the supplied configuration */
 	if (phy_interface_empty(config->supported_interfaces)) {
 		dev_err(config->dev,
@@ -2080,6 +2278,7 @@ struct phylink *phylink_create(struct phylink_config *config,
 	mutex_init(&pl->phydev_mutex);
 	mutex_init(&pl->state_mutex);
 	INIT_WORK(&pl->resolve, phylink_resolve);
+	INIT_LIST_HEAD(&pl->pcs_list);
 
 	pl->config = config;
 	if (config->type == PHYLINK_NETDEV) {
@@ -2117,8 +2316,12 @@ 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);
 
-	phy_interface_copy(pl->supported_interfaces,
-			   pl->config->supported_interfaces);
+	/* Fill the PCS list with available PCS from phylink config */
+	ret = phylink_fill_available_pcs(pl, config);
+	if (ret < 0)
+		goto free_pl;
+
+	phylink_parse_supported_interfaces(pl);
 
 	linkmode_fill(pl->supported);
 	linkmode_copy(pl->link_config.advertising, pl->supported);
@@ -2126,7 +2329,7 @@ struct phylink *phylink_create(struct phylink_config *config,
 
 	ret = phylink_parse_mode(pl, fwnode);
 	if (ret < 0)
-		goto free_pl;
+		goto unlink_pcs_list;
 
 	if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
 		ret = phylink_parse_fixedlink(pl, fwnode);
@@ -2145,6 +2348,8 @@ struct phylink *phylink_create(struct phylink_config *config,
 release_link_gpio:
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
+unlink_pcs_list:
+	phylink_detach_pcss(pl);
 free_pl:
 	kfree(pl);
 	return ERR_PTR(ret);
@@ -2167,17 +2372,9 @@ void phylink_destroy(struct phylink *pl)
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
 
-	if (pl->pcs)
-		rcu_assign_pointer(pl->pcs->phylink, NULL);
-
-	/*
-	 * Unconditionally wait for readers that may have obtained this
-	 * phylink through a PCS while phylink_major_config() was clearing
-	 * the association.
-	 */
-	synchronize_rcu();
-
+	phylink_detach_pcss(pl);
 	cancel_work_sync(&pl->resolve);
+
 	kfree(pl);
 }
 EXPORT_SYMBOL_GPL(phylink_destroy);
@@ -2646,7 +2843,7 @@ void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
 	rcu_read_lock();
 
 	pl = rcu_dereference(pcs->phylink);
-	if (pl)
+	if (pl && READ_ONCE(pl->pcs) == pcs)
 		phylink_link_changed(pl, up, "pcs");
 
 	rcu_read_unlock();
diff --git a/include/linux/phylink.h b/include/linux/phylink.h
index 27515d0bc57e..a56289ecee58 100644
--- a/include/linux/phylink.h
+++ b/include/linux/phylink.h
@@ -12,6 +12,7 @@ struct ethtool_cmd;
 struct fwnode_handle;
 struct net_device;
 struct phylink;
+struct phylink_pcs;
 
 enum {
 	MLO_PAUSE_NONE,
@@ -151,6 +152,8 @@ enum phylink_op_type {
  *		     if MAC link is at %MLO_AN_FIXED mode.
  * @supported_interfaces: bitmap describing which PHY_INTERFACE_MODE_xxx
  *                        are supported by the MAC/PCS.
+ * @pcs_interfaces: bitmap describing for which PHY_INTERFACE_MODE_xxx a
+ *		    dedicated PCS is required.
  * @lpi_interfaces: bitmap describing which PHY interface modes can support
  *		    LPI signalling.
  * @mac_capabilities: MAC pause/speed/duplex capabilities.
@@ -160,6 +163,10 @@ enum phylink_op_type {
  * @wol_phy_legacy: Use Wake-on-Lan with PHY even if phy_can_wakeup() is false
  * @wol_phy_speed_ctrl: Use phy speed control on suspend/resume
  * @wol_mac_support: Bitmask of MAC supported %WAKE_* options
+ * @num_possible_pcs: num of possible phylink_pcs PCS
+ * @fill_available_pcs: callback to fill the available PCS in the passed
+ *			array struct of phylink_pcs PCS available_pcs up to
+ *			num_possible_pcs.
  */
 struct phylink_config {
 	struct device *dev;
@@ -172,6 +179,7 @@ struct phylink_config {
 	void (*get_fixed_state)(struct phylink_config *config,
 				struct phylink_link_state *state);
 	DECLARE_PHY_INTERFACE_MASK(supported_interfaces);
+	DECLARE_PHY_INTERFACE_MASK(pcs_interfaces);
 	DECLARE_PHY_INTERFACE_MASK(lpi_interfaces);
 	unsigned long mac_capabilities;
 	unsigned long lpi_capabilities;
@@ -182,6 +190,11 @@ struct phylink_config {
 	bool wol_phy_legacy;
 	bool wol_phy_speed_ctrl;
 	u32 wol_mac_support;
+
+	unsigned int num_possible_pcs;
+	int (*fill_available_pcs)(struct phylink_config *config,
+				  struct phylink_pcs **available_pcs,
+				  unsigned int num_possible_pcs);
 };
 
 void phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed);
@@ -497,6 +510,9 @@ struct phylink_pcs {
 	struct phylink __rcu *phylink;
 	bool poll;
 	bool rxc_always_on;
+
+	/* private: */
+	struct list_head list;
 };
 
 /**
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 04/15] net: pcs: implement Firmware node support for PCS driver
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (2 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 03/15] net: phylink: introduce internal phylink PCS handling Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 05/15] net: phylink: make phylink_validate state locked Christian Marangi
                   ` (10 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm
  Cc: Daniel Golle

Implement the foundation of firmware node support for PCS drivers.

To support this, implement a simple provider API where a PCS driver can
expose multiple PCS with a fwnode_xlate callback.

The PCS driver calls fwnode_pcs_add_provider() and passes the firmware node
pointer, an xlate function and context data. The xlate function returns the
appropriate PCS for the firmware reference and its #pcs-cells arguments. It
must return the same preallocated PCS for a given reference and arguments,
and the PCS must remain valid until the provider is removed.

This registers the provider in a global list so that consumers can access
its PCS. The first successful lookup associates the PCS with its provider.

The consumer uses fwnode_pcs_get() to get the actual PCS by passing the
consumer's firmware node pointer and the index in the "pcs-handle"
property. The lookup returns -ENODEV if no provider supplies a PCS for that
reference.

For a simple implementation where #pcs-cells is 0 and the PCS driver
exposes a single PCS, the xlate function fwnode_pcs_simple_xlate() is
provided.

For an advanced implementation, a custom xlate function is required.

On removal, the PCS driver calls fwnode_pcs_del_provider() to remove an
unmanaged provider from the provider list and clear its association with
its PCS.

A devm variant, devm_fwnode_pcs_add_provider(), is provided to automate the
release procedure.

Generic functions fwnode_phylink_pcs_count() and fwnode_phylink_pcs_parse()
are provided for MAC drivers that declare PCS in DT or ACPI.

Function fwnode_phylink_pcs_count() parses the "pcs-handle" property and
returns the number of PCS references in the passed firmware node,
regardless of whether the corresponding PCS are available.

Function fwnode_phylink_pcs_parse() parses up to num_pcs entries in the
"pcs-handle" property, fills the passed available_pcs array with the PCS it
finds and returns the number stored. It skips lookups that return -ENODEV,
stops at -ENOENT and propagates other lookup errors. It returns -ENODEV if
the "pcs-handle" property is absent.

Co-developed-by: Daniel Golle <daniel@makrotopia.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/net/pcs/Kconfig          |   6 +
 drivers/net/pcs/Makefile         |   1 +
 drivers/net/pcs/pcs.c            | 279 +++++++++++++++++++++++++++++++
 include/linux/pcs/pcs-provider.h |  72 ++++++++
 include/linux/pcs/pcs.h          |  74 ++++++++
 include/linux/phylink.h          |   3 +
 6 files changed, 435 insertions(+)
 create mode 100644 drivers/net/pcs/pcs.c
 create mode 100644 include/linux/pcs/pcs-provider.h
 create mode 100644 include/linux/pcs/pcs.h

diff --git a/drivers/net/pcs/Kconfig b/drivers/net/pcs/Kconfig
index 108d47257bee..3d4d81c8e098 100644
--- a/drivers/net/pcs/Kconfig
+++ b/drivers/net/pcs/Kconfig
@@ -5,6 +5,12 @@
 
 menu "PCS device drivers"
 
+config FWNODE_PCS
+	bool "PCS Firmware Node"
+	depends on ACPI || OF || COMPILE_TEST
+	help
+	  Firmware node PCS accessors
+
 config PCS_XPCS
 	tristate "Synopsys DesignWare Ethernet XPCS"
 	select PHYLINK
diff --git a/drivers/net/pcs/Makefile b/drivers/net/pcs/Makefile
index f9f6cf2578d7..53ba1d560f69 100644
--- a/drivers/net/pcs/Makefile
+++ b/drivers/net/pcs/Makefile
@@ -3,6 +3,7 @@
 
 pcs_xpcs-$(CONFIG_PCS_XPCS)	:= pcs-xpcs.o pcs-xpcs-nxp.o pcs-xpcs-wx.o
 
+obj-$(CONFIG_FWNODE_PCS)	+= pcs.o
 obj-$(CONFIG_PCS_XPCS)		+= pcs_xpcs.o
 obj-$(CONFIG_PCS_XPCS_PLATFORM) += pcs-xpcs-plat.o
 obj-$(CONFIG_PCS_XPCS_ROCKCHIP) += pcs-xpcs-rk.o
diff --git a/drivers/net/pcs/pcs.c b/drivers/net/pcs/pcs.c
new file mode 100644
index 000000000000..a0b3681db54b
--- /dev/null
+++ b/drivers/net/pcs/pcs.c
@@ -0,0 +1,279 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <linux/mutex.h>
+#include <linux/property.h>
+#include <linux/phylink.h>
+#include <linux/pcs/pcs.h>
+#include <linux/pcs/pcs-provider.h>
+
+MODULE_DESCRIPTION("PCS library");
+MODULE_AUTHOR("Christian Marangi <ansuelsmth@gmail.com>");
+MODULE_LICENSE("GPL");
+
+struct fwnode_pcs_provider {
+	struct list_head link;
+	struct list_head pcss;
+
+	struct fwnode_handle *fwnode;
+	struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec,
+					    void *data);
+
+	void *data;
+};
+
+static LIST_HEAD(fwnode_pcs_providers);
+static DEFINE_MUTEX(fwnode_pcs_mutex);
+
+struct phylink_pcs *fwnode_pcs_simple_xlate(struct fwnode_reference_args *pcsspec,
+					    void *data)
+{
+	return data;
+}
+EXPORT_SYMBOL_GPL(fwnode_pcs_simple_xlate);
+
+struct fwnode_pcs_provider *
+fwnode_pcs_add_provider(struct fwnode_handle *fwnode,
+			struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec,
+							    void *data),
+			void *data)
+{
+	struct fwnode_pcs_provider *pp;
+
+	if (!fwnode)
+		return ERR_PTR(-EINVAL);
+
+	pp = kzalloc_obj(*pp);
+	if (!pp)
+		return ERR_PTR(-ENOMEM);
+
+	pp->fwnode = fwnode_handle_get(fwnode);
+	pp->data = data;
+	pp->fwnode_xlate = fwnode_xlate;
+	INIT_LIST_HEAD(&pp->pcss);
+
+	mutex_lock(&fwnode_pcs_mutex);
+
+	list_add(&pp->link, &fwnode_pcs_providers);
+	fwnode_dev_initialized(fwnode, true);
+
+	mutex_unlock(&fwnode_pcs_mutex);
+
+	pr_debug("Added pcs provider from %pfwf\n", fwnode);
+
+	return pp;
+}
+EXPORT_SYMBOL_GPL(fwnode_pcs_add_provider);
+
+static void fwnode_pcs_free_provider(struct fwnode_pcs_provider *pp)
+{
+	struct phylink_pcs *pcs, *tmp;
+
+	mutex_lock(&fwnode_pcs_mutex);
+	list_for_each_entry_safe(pcs, tmp, &pp->pcss, provider_list) {
+		list_del(&pcs->provider_list);
+		pcs->provider = NULL;
+	}
+	mutex_unlock(&fwnode_pcs_mutex);
+
+	fwnode_handle_put(pp->fwnode);
+	kfree(pp);
+}
+
+void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp)
+{
+	if (IS_ERR_OR_NULL(pp))
+		return;
+
+	mutex_lock(&fwnode_pcs_mutex);
+
+	list_del(&pp->link);
+	fwnode_dev_initialized(pp->fwnode, false);
+
+	mutex_unlock(&fwnode_pcs_mutex);
+
+	fwnode_pcs_free_provider(pp);
+}
+EXPORT_SYMBOL_GPL(fwnode_pcs_del_provider);
+
+static void devm_fwnode_pcs_del(struct device *dev, void *res)
+{
+	struct fwnode_pcs_provider *pp = *(struct fwnode_pcs_provider **)res;
+
+	fwnode_pcs_del_provider(pp);
+}
+
+struct fwnode_pcs_provider *
+devm_fwnode_pcs_add_provider(struct device *dev, struct fwnode_handle *fwnode,
+			     struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec,
+								 void *data),
+			     void *data)
+{
+	struct fwnode_pcs_provider **ptr, *pp;
+
+	ptr = devres_alloc(devm_fwnode_pcs_del, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	pp = fwnode_pcs_add_provider(fwnode, fwnode_xlate, data);
+
+	if (!IS_ERR(pp)) {
+		*ptr = pp;
+		devres_add(dev, ptr);
+	} else {
+		devres_free(ptr);
+	}
+
+	return pp;
+}
+EXPORT_SYMBOL_GPL(devm_fwnode_pcs_add_provider);
+
+static int fwnode_parse_pcsspec(const struct fwnode_handle *fwnode,
+				unsigned int index, const char *name,
+				struct fwnode_reference_args *out_args)
+{
+	if (!fwnode)
+		return -EINVAL;
+
+	if (name) {
+		int ret;
+
+		ret = fwnode_property_match_string(fwnode, "pcs-handle-names",
+						   name);
+		if (ret < 0)
+			return ret;
+
+		index = ret;
+	}
+
+	return fwnode_property_get_reference_args(fwnode, "pcs-handle",
+						  "#pcs-cells", 0, index,
+						  out_args);
+}
+
+static struct phylink_pcs *
+__fwnode_pcs_get_from_pcsspec_provider(struct fwnode_reference_args *pcsspec,
+				       struct fwnode_pcs_provider *provider)
+{
+	struct phylink_pcs *pcs;
+
+	lockdep_assert_held(&fwnode_pcs_mutex);
+
+	if (provider->fwnode != pcsspec->fwnode)
+		return ERR_PTR(-EINVAL);
+
+	pcs = provider->fwnode_xlate(pcsspec, provider->data);
+	if (IS_ERR_OR_NULL(pcs))
+		return pcs;
+
+	/* Track provider ownership on first lookup */
+	if (pcs->provider != provider) {
+		if (pcs->provider)
+			return ERR_PTR(-EBUSY);
+
+		pcs->provider = provider;
+		list_add_tail(&pcs->provider_list, &provider->pcss);
+	}
+
+	return pcs;
+}
+
+static struct phylink_pcs *
+fwnode_pcs_get_from_pcsspec(struct fwnode_reference_args *pcsspec)
+{
+	struct fwnode_pcs_provider *provider;
+	struct phylink_pcs *pcs = NULL;
+
+	if (!pcsspec)
+		return ERR_PTR(-EINVAL);
+
+	mutex_lock(&fwnode_pcs_mutex);
+	list_for_each_entry(provider, &fwnode_pcs_providers, link) {
+		pcs = __fwnode_pcs_get_from_pcsspec_provider(pcsspec, provider);
+		if (!IS_ERR(pcs))
+			break;
+	}
+	mutex_unlock(&fwnode_pcs_mutex);
+
+	return !IS_ERR_OR_NULL(pcs) ? pcs : ERR_PTR(-ENODEV);
+}
+
+static struct phylink_pcs *__fwnode_pcs_get(const struct fwnode_handle *fwnode,
+					    unsigned int index, const char *con_id)
+{
+	struct fwnode_reference_args pcsspec;
+	struct phylink_pcs *pcs;
+	int ret;
+
+	ret = fwnode_parse_pcsspec(fwnode, index, con_id, &pcsspec);
+	if (ret)
+		return ERR_PTR(ret);
+
+	pcs = fwnode_pcs_get_from_pcsspec(&pcsspec);
+	fwnode_handle_put(pcsspec.fwnode);
+
+	return pcs;
+}
+
+struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode, unsigned int index)
+{
+	return __fwnode_pcs_get(fwnode, index, NULL);
+}
+EXPORT_SYMBOL_GPL(fwnode_pcs_get);
+
+unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode)
+{
+	struct fwnode_reference_args out_args;
+	unsigned int index = 0;
+	int ret;
+
+	while (true) {
+		ret = fwnode_property_get_reference_args(fwnode, "pcs-handle",
+							 "#pcs-cells", 0, index,
+							 &out_args);
+		/* We expect to reach an -ENOENT error while counting */
+		if (ret)
+			break;
+
+		fwnode_handle_put(out_args.fwnode);
+		index++;
+	}
+
+	return index;
+}
+EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_count);
+
+int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode,
+			     struct phylink_pcs **available_pcs,
+			     unsigned int num_pcs)
+{
+	unsigned int i, found = 0;
+
+	if (!available_pcs)
+		return -EINVAL;
+
+	if (!fwnode_property_present(fwnode, "pcs-handle"))
+		return -ENODEV;
+
+	for (i = 0; i < num_pcs; i++) {
+		struct phylink_pcs *pcs;
+
+		pcs = fwnode_pcs_get(fwnode, i);
+		if (IS_ERR(pcs)) {
+			/* Exit early if no PCS remain */
+			if (PTR_ERR(pcs) == -ENOENT)
+				break;
+
+			/* Skip references that did not resolve to a PCS */
+			if (PTR_ERR(pcs) == -ENODEV)
+				continue;
+
+			return PTR_ERR(pcs);
+		}
+
+		available_pcs[found] = pcs;
+		found++;
+	}
+
+	return found;
+}
+EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_parse);
diff --git a/include/linux/pcs/pcs-provider.h b/include/linux/pcs/pcs-provider.h
new file mode 100644
index 000000000000..6b03a635f73c
--- /dev/null
+++ b/include/linux/pcs/pcs-provider.h
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __LINUX_PCS_PROVIDER_H
+#define __LINUX_PCS_PROVIDER_H
+
+struct fwnode_pcs_provider;
+
+/**
+ * fwnode_pcs_simple_xlate - Simple xlate function to retrieve PCS
+ * @pcsspec: reference arguments
+ * @data: Context data (assumed assigned to the single PCS)
+ *
+ * Returns: the PCS pointed by data.
+ */
+struct phylink_pcs *fwnode_pcs_simple_xlate(struct fwnode_reference_args *pcsspec,
+					    void *data);
+
+/**
+ * fwnode_pcs_add_provider - Registers a new PCS provider
+ * @fwnode: Firmware node
+ * @fwnode_xlate: xlate function to retrieve the PCS
+ * @data: Context data
+ *
+ * Register and add a new PCS provider to the global providers list
+ * for the firmware node. The relevant PCS from the PCS provider
+ * is retrieved from the passed fwnode_xlate function.
+ *
+ * The xlate function MUST return a pointer to an existing, already-allocated
+ * struct phylink_pcs and MUST NOT dynamically allocate a new PCS.
+ * The same PCS object must be returned for a given firmware reference and args,
+ * as PCS pointer identity is used to associate PCS objects with their provider.
+ * A PCS object belongs to one provider and must remain valid until that
+ * provider is removed.
+ *
+ * Returns: A pointer to the registered PCS provider on success, or
+ * an ERR_PTR() encoded error code on failure.
+ */
+struct fwnode_pcs_provider *
+fwnode_pcs_add_provider(struct fwnode_handle *fwnode,
+			struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec,
+							    void *data),
+			void *data);
+
+/**
+ * fwnode_pcs_del_provider - Removes a PCS provider
+ * @pp: PCS provider returned by fwnode_pcs_add_provider()
+ */
+void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp);
+
+/**
+ * devm_fwnode_pcs_add_provider - Registers a new PCS provider
+ * @dev: Device of the PCS provider
+ * @fwnode: Firmware node
+ * @fwnode_xlate: xlate function to retrieve the PCS
+ * @data: Context data
+ *
+ * Register and add a new PCS provider to the global providers list
+ * for the firmware node. The relevant PCS from the PCS provider
+ * is retrieved from the passed fwnode_xlate function. While at that, it
+ * also associates the device with the PCS provider using devres.
+ * On driver detach, release function is invoked on the devres data,
+ * then, devres data is freed.
+ *
+ * Returns: A pointer to the registered PCS provider on success, or
+ * an ERR_PTR() encoded error code on failure.
+ */
+struct fwnode_pcs_provider *
+devm_fwnode_pcs_add_provider(struct device *dev, struct fwnode_handle *fwnode,
+			     struct phylink_pcs *(*fwnode_xlate)(struct fwnode_reference_args *pcsspec,
+								 void *data),
+			     void *data);
+
+#endif /* __LINUX_PCS_PROVIDER_H */
diff --git a/include/linux/pcs/pcs.h b/include/linux/pcs/pcs.h
new file mode 100644
index 000000000000..b3bb158002d5
--- /dev/null
+++ b/include/linux/pcs/pcs.h
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __LINUX_PCS_H
+#define __LINUX_PCS_H
+
+#include <linux/phylink.h>
+
+#if IS_ENABLED(CONFIG_FWNODE_PCS)
+/**
+ * fwnode_pcs_get - Retrieves a PCS from a firmware node
+ * @fwnode: firmware node
+ * @index: index fwnode PCS handle in firmware node
+ *
+ * Get a PCS from the firmware node at index.
+ *
+ * Returns: A pointer to the PCS on success, or ERR_PTR(-ENODEV) if
+ * no registered provider supplies a PCS for the reference. Provider
+ * translation errors are reported to the caller as -ENODEV. Errors
+ * parsing the firmware reference are returned unchanged.
+ */
+struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode,
+				   unsigned int index);
+
+/**
+ * fwnode_phylink_pcs_count - Count PCS entries described in firmware node
+ * @fwnode: firmware node
+ *
+ * Helper function to count the number of PCS entries referenced by the
+ * "pcs-handle" property in a firmware node.
+ *
+ * Note that this function counts all PCS references in the firmware node,
+ * regardless of whether the corresponding PCS devices are already probed.
+ *
+ * Returns: number of PCS entries described in the firmware node.
+ */
+unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode);
+
+/**
+ * fwnode_phylink_pcs_parse - Parse available PCS from firmware node
+ * @fwnode: firmware node
+ * @available_pcs: pointer to preallocated array of PCS
+ * @num_pcs: maximum number of PCS entries to scan
+ *
+ * Helper function that parses PCS references from the "pcs-handle"
+ * property of a firmware node and fills @available_pcs with PCS that are
+ * currently available up to @num_pcs.
+ *
+ * References for which fwnode_pcs_get() returns -ENODEV are skipped.
+ *
+ * Returns: number of PCS stored in @available_pcs, or negative error code.
+ */
+int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode,
+			     struct phylink_pcs **available_pcs,
+			     unsigned int num_pcs);
+#else
+static inline struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode,
+						 unsigned int index)
+{
+	return ERR_PTR(-ENOENT);
+}
+
+static inline unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode)
+{
+	return 0;
+}
+
+static inline int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode,
+					   struct phylink_pcs **available_pcs,
+					   unsigned int num_pcs)
+{
+	return -EOPNOTSUPP;
+}
+#endif
+
+#endif /* __LINUX_PCS_H */
diff --git a/include/linux/phylink.h b/include/linux/phylink.h
index a56289ecee58..9ce66b034437 100644
--- a/include/linux/phylink.h
+++ b/include/linux/phylink.h
@@ -10,6 +10,7 @@
 struct device_node;
 struct ethtool_cmd;
 struct fwnode_handle;
+struct fwnode_pcs_provider;
 struct net_device;
 struct phylink;
 struct phylink_pcs;
@@ -513,6 +514,8 @@ struct phylink_pcs {
 
 	/* private: */
 	struct list_head list;
+	struct fwnode_pcs_provider *provider;
+	struct list_head provider_list;
 };
 
 /**
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 05/15] net: phylink: make phylink_validate state locked
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (3 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 04/15] net: pcs: implement Firmware node support for PCS driver Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 06/15] net: phylink: add capabilities helpers and track modified advertising Christian Marangi
                   ` (9 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

In preparation for support for PCS release and late attach, rework the
phylink validation functions to run under the state mutex.

The PCS notifier can recalculate supported interfaces and capabilities
while phylink_create() or interface mode validation is running. Validation
can then see inconsistent state.

To handle this, make phylink_validate() and phylink_validate_mask() take
the phylink state mutex. Add __phylink_validate() and
__phylink_validate_mask() for callers that already hold the mutex, and add
lockdep assertions to the inner validation functions.

Update phylink_update_mac_pause_capabilities() to use __phylink_validate()
since it already holds the state mutex.

Take the state mutex when querying PCS in-band capabilities and when
applying the initial major configuration.

When validating an attached PHY, take the PHY mutex before the state
mutex and call __phylink_validate_phy(). Add __phy_get_rate_matching()
in phy-internal.h for callers that already hold the PHY mutex.
phy_get_rate_matching() takes that mutex before calling the same helper.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/net/phy/phy-internal.h |  22 +++
 drivers/net/phy/phy.c          |  11 +-
 drivers/net/phy/phylink.c      | 238 ++++++++++++++++++++-------------
 3 files changed, 175 insertions(+), 96 deletions(-)
 create mode 100644 drivers/net/phy/phy-internal.h

diff --git a/drivers/net/phy/phy-internal.h b/drivers/net/phy/phy-internal.h
new file mode 100644
index 000000000000..56d87495e1f0
--- /dev/null
+++ b/drivers/net/phy/phy-internal.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef __PHY_INTERNAL_H
+#define __PHY_INTERNAL_H
+
+#include <linux/lockdep.h>
+#include <linux/phy.h>
+
+static inline int __phy_get_rate_matching(struct phy_device *phydev,
+					  phy_interface_t iface)
+{
+	int ret = RATE_MATCH_NONE;
+
+	lockdep_assert_held(&phydev->lock);
+
+	if (phydev->drv->get_rate_matching)
+		return phydev->drv->get_rate_matching(phydev, iface);
+
+	return ret;
+}
+
+#endif /* __PHY_INTERNAL_H */
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 079c53516f42..a6d6a7c1a96d 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -38,6 +38,7 @@
 
 #include "phylib-internal.h"
 #include "phy-caps.h"
+#include "phy-internal.h"
 
 #define PHY_STATE_TIME	HZ
 
@@ -146,13 +147,11 @@ EXPORT_SYMBOL(phy_print_status);
 int phy_get_rate_matching(struct phy_device *phydev,
 			  phy_interface_t iface)
 {
-	int ret = RATE_MATCH_NONE;
+	int ret;
 
-	if (phydev->drv->get_rate_matching) {
-		mutex_lock(&phydev->lock);
-		ret = phydev->drv->get_rate_matching(phydev, iface);
-		mutex_unlock(&phydev->lock);
-	}
+	mutex_lock(&phydev->lock);
+	ret = __phy_get_rate_matching(phydev, iface);
+	mutex_unlock(&phydev->lock);
 
 	return ret;
 }
diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 35e82cccd39a..141da617b24b 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -9,6 +9,7 @@
 #include <linux/ethtool.h>
 #include <linux/export.h>
 #include <linux/gpio/consumer.h>
+#include <linux/lockdep.h>
 #include <linux/netdevice.h>
 #include <linux/of.h>
 #include <linux/of_mdio.h>
@@ -24,6 +25,7 @@
 #include <linux/workqueue.h>
 
 #include "phy-caps.h"
+#include "phy-internal.h"
 #include "sfp.h"
 #include "swphy.h"
 
@@ -554,6 +556,8 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl,
 	unsigned long capabilities;
 	int ret;
 
+	lockdep_assert_held(&pl->state_mutex);
+
 	/* Get the PCS for this interface mode */
 	if (pl->mac_ops->mac_select_pcs) {
 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
@@ -632,13 +636,15 @@ static void phylink_validate_one(struct phylink *pl, struct phy_device *phy,
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp_supported);
 	struct phylink_link_state tmp_state;
 
+	lockdep_assert_held(&pl->state_mutex);
+
 	linkmode_copy(tmp_supported, supported);
 
 	tmp_state = *state;
 	tmp_state.interface = interface;
 
 	if (phy)
-		tmp_state.rate_matching = phy_get_rate_matching(phy, interface);
+		tmp_state.rate_matching = __phy_get_rate_matching(phy, interface);
 
 	if (!phylink_validate_mac_and_pcs(pl, tmp_supported, &tmp_state)) {
 		phylink_dbg(pl, " interface %u (%s) rate match %s supports %*pbl\n",
@@ -652,15 +658,17 @@ static void phylink_validate_one(struct phylink *pl, struct phy_device *phy,
 	}
 }
 
-static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy,
-				 unsigned long *supported,
-				 struct phylink_link_state *state,
-				 const unsigned long *interfaces)
+static int __phylink_validate_mask(struct phylink *pl, struct phy_device *phy,
+				   unsigned long *supported,
+				   struct phylink_link_state *state,
+				   const unsigned long *interfaces)
 {
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
 	int interface;
 
+	lockdep_assert_held(&pl->state_mutex);
+
 	for_each_set_bit(interface, interfaces, PHY_INTERFACE_MODE_MAX)
 		phylink_validate_one(pl, phy, supported, state, interface,
 				     all_s, all_adv);
@@ -671,14 +679,30 @@ static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy,
 	return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
 }
 
-static int phylink_validate(struct phylink *pl, unsigned long *supported,
-			    struct phylink_link_state *state)
+static int phylink_validate_mask(struct phylink *pl,
+				 unsigned long *supported,
+				 struct phylink_link_state *state,
+				 const unsigned long *interfaces)
+{
+	int ret;
+
+	mutex_lock(&pl->state_mutex);
+	ret = __phylink_validate_mask(pl, NULL, supported, state, interfaces);
+	mutex_unlock(&pl->state_mutex);
+
+	return ret;
+}
+
+static int __phylink_validate(struct phylink *pl, unsigned long *supported,
+			      struct phylink_link_state *state)
 {
 	const unsigned long *interfaces = pl->supported_interfaces;
 
+	lockdep_assert_held(&pl->state_mutex);
+
 	if (state->interface == PHY_INTERFACE_MODE_NA)
-		return phylink_validate_mask(pl, NULL, supported, state,
-					     interfaces);
+		return __phylink_validate_mask(pl, NULL, supported, state,
+					       interfaces);
 
 	if (!test_bit(state->interface, interfaces))
 		return -EINVAL;
@@ -686,6 +710,96 @@ static int phylink_validate(struct phylink *pl, unsigned long *supported,
 	return phylink_validate_mac_and_pcs(pl, supported, state);
 }
 
+static int phylink_validate(struct phylink *pl, unsigned long *supported,
+			    struct phylink_link_state *state)
+{
+	int ret;
+
+	mutex_lock(&pl->state_mutex);
+	ret = __phylink_validate(pl, supported, state);
+	mutex_unlock(&pl->state_mutex);
+
+	return ret;
+}
+
+static int __phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
+				  unsigned long *supported,
+				  struct phylink_link_state *state)
+{
+	DECLARE_PHY_INTERFACE_MASK(interfaces);
+
+	/*
+	 * If the PHY provides a bitmap of the interfaces it will be using
+	 * depending on the negotiated media speeds, use this to validate
+	 * which ethtool link modes can be used.
+	 */
+	if (!phy_interface_empty(phy->possible_interfaces)) {
+		/*
+		 * We only care about the union of the PHY's interfaces and
+		 * those which the host supports.
+		 */
+		phy_interface_and(interfaces, phy->possible_interfaces,
+				  pl->supported_interfaces);
+
+		if (phy_interface_empty(interfaces)) {
+			phylink_err(pl, "PHY has no common interfaces\n");
+			return -EINVAL;
+		}
+
+		if (phy_on_sfp(phy)) {
+			/*
+			 * If the PHY is on a SFP, limit the interfaces to
+			 * those that can be used with a SFP module.
+			 */
+			phy_interface_and(interfaces, interfaces,
+					  phylink_sfp_interfaces);
+
+			if (phy_interface_empty(interfaces)) {
+				phylink_err(pl, "SFP PHY's possible interfaces becomes empty\n");
+				return -EINVAL;
+			}
+		}
+
+		phylink_dbg(pl, "PHY %s uses interfaces %*pbl, validating %*pbl\n",
+			    phydev_name(phy),
+			    (int)PHY_INTERFACE_MODE_MAX,
+			    phy->possible_interfaces,
+			    (int)PHY_INTERFACE_MODE_MAX, interfaces);
+
+		return __phylink_validate_mask(pl, phy, supported, state,
+					      interfaces);
+	}
+
+	phylink_dbg(pl, "PHY %s doesn't supply possible interfaces\n",
+		    phydev_name(phy));
+
+	/*
+	 * Check whether we would use rate matching for the proposed interface
+	 * mode.
+	 */
+	state->rate_matching = __phy_get_rate_matching(phy, state->interface);
+
+	/*
+	 * Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
+	 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
+	 * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching
+	 * their Serdes is either unnecessary or not reasonable.
+	 *
+	 * For these which switch interface modes, we really need to know which
+	 * interface modes the PHY supports to properly work out which ethtool
+	 * linkmodes can be supported. For now, as a work-around, we validate
+	 * against all interface modes, which may lead to more ethtool link
+	 * modes being advertised than are actually supported.
+	 */
+	if (phy->is_c45 && state->rate_matching == RATE_MATCH_NONE &&
+	    state->interface != PHY_INTERFACE_MODE_RXAUI &&
+	    state->interface != PHY_INTERFACE_MODE_XAUI &&
+	    state->interface != PHY_INTERFACE_MODE_USXGMII)
+		state->interface = PHY_INTERFACE_MODE_NA;
+
+	return __phylink_validate(pl, supported, state);
+}
+
 static void phylink_fill_fixedlink_supported(unsigned long *supported)
 {
 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported);
@@ -1005,6 +1119,9 @@ static unsigned int phylink_inband_caps(struct phylink *pl,
 					 phy_interface_t interface)
 {
 	struct phylink_pcs *pcs = NULL;
+	int ret = 0;
+
+	mutex_lock(&pl->state_mutex);
 
 	if (pl->mac_ops->mac_select_pcs) {
 		pcs = pl->mac_ops->mac_select_pcs(pl->config,
@@ -1021,9 +1138,14 @@ static unsigned int phylink_inband_caps(struct phylink *pl,
 	}
 
 	if (IS_ERR_OR_NULL(pcs))
-		return 0;
+		goto exit;
+
+	ret = phylink_pcs_inband_caps(pcs, interface);
 
-	return phylink_pcs_inband_caps(pcs, interface);
+exit:
+	mutex_unlock(&pl->state_mutex);
+
+	return ret;
 }
 
 static void phylink_pcs_poll_stop(struct phylink *pl)
@@ -1565,7 +1687,9 @@ static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
 	phylink_apply_manual_flow(pl, &link_state);
 	if (phy)
 		mutex_lock(&phy->lock);
+	mutex_lock(&pl->state_mutex);
 	phylink_major_config(pl, force_restart, &link_state);
+	mutex_unlock(&pl->state_mutex);
 	if (phy)
 		mutex_unlock(&phy->lock);
 }
@@ -2151,7 +2275,7 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac
 	else if (pl->sfp_bus)
 		linkmode_and(pl->supported, pl->supported, pl->sfp_support);
 
-	phylink_validate(pl, pl->supported, config);
+	__phylink_validate(pl, pl->supported, config);
 
 	pause_state = config->pause;
 
@@ -2172,6 +2296,8 @@ static void phylink_parse_supported_interfaces(struct phylink *pl)
 {
 	struct phylink_pcs *pcs;
 
+	lockdep_assert_held(&pl->state_mutex);
+
 	phy_interface_copy(pl->supported_interfaces,
 			   pl->config->supported_interfaces);
 
@@ -2321,11 +2447,12 @@ struct phylink *phylink_create(struct phylink_config *config,
 	if (ret < 0)
 		goto free_pl;
 
+	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_validate(pl, pl->supported, &pl->link_config);
+	mutex_unlock(&pl->state_mutex);
 
 	ret = phylink_parse_mode(pl, fwnode);
 	if (ret < 0)
@@ -2434,84 +2561,11 @@ static void phylink_phy_change(struct phy_device *phydev, bool up)
 		    phydev->enable_tx_lpi ? "" : "no");
 }
 
-static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
-				unsigned long *supported,
-				struct phylink_link_state *state)
-{
-	DECLARE_PHY_INTERFACE_MASK(interfaces);
-
-	/* If the PHY provides a bitmap of the interfaces it will be using
-	 * depending on the negotiated media speeds, use this to validate
-	 * which ethtool link modes can be used.
-	 */
-	if (!phy_interface_empty(phy->possible_interfaces)) {
-		/* We only care about the union of the PHY's interfaces and
-		 * those which the host supports.
-		 */
-		phy_interface_and(interfaces, phy->possible_interfaces,
-				  pl->supported_interfaces);
-
-		if (phy_interface_empty(interfaces)) {
-			phylink_err(pl, "PHY has no common interfaces\n");
-			return -EINVAL;
-		}
-
-		if (phy_on_sfp(phy)) {
-			/* If the PHY is on a SFP, limit the interfaces to
-			 * those that can be used with a SFP module.
-			 */
-			phy_interface_and(interfaces, interfaces,
-					  phylink_sfp_interfaces);
-
-			if (phy_interface_empty(interfaces)) {
-				phylink_err(pl, "SFP PHY's possible interfaces becomes empty\n");
-				return -EINVAL;
-			}
-		}
-
-		phylink_dbg(pl, "PHY %s uses interfaces %*pbl, validating %*pbl\n",
-			    phydev_name(phy),
-			    (int)PHY_INTERFACE_MODE_MAX,
-			    phy->possible_interfaces,
-			    (int)PHY_INTERFACE_MODE_MAX, interfaces);
-
-		return phylink_validate_mask(pl, phy, supported, state,
-					     interfaces);
-	}
-
-	phylink_dbg(pl, "PHY %s doesn't supply possible interfaces\n",
-		    phydev_name(phy));
-
-	/* Check whether we would use rate matching for the proposed interface
-	 * mode.
-	 */
-	state->rate_matching = phy_get_rate_matching(phy, state->interface);
-
-	/* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
-	 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
-	 * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching
-	 * their Serdes is either unnecessary or not reasonable.
-	 *
-	 * For these which switch interface modes, we really need to know which
-	 * interface modes the PHY supports to properly work out which ethtool
-	 * linkmodes can be supported. For now, as a work-around, we validate
-	 * against all interface modes, which may lead to more ethtool link
-	 * modes being advertised than are actually supported.
-	 */
-	if (phy->is_c45 && state->rate_matching == RATE_MATCH_NONE &&
-	    state->interface != PHY_INTERFACE_MODE_RXAUI &&
-	    state->interface != PHY_INTERFACE_MODE_XAUI &&
-	    state->interface != PHY_INTERFACE_MODE_USXGMII)
-		state->interface = PHY_INTERFACE_MODE_NA;
-
-	return phylink_validate(pl, supported, state);
-}
-
 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
 			       phy_interface_t interface)
 {
-	struct phylink_link_state config;
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
+	struct phylink_link_state config;
 	char *irq_str;
 	int ret;
 
@@ -2524,12 +2578,16 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
 	 */
 	phy_support_asym_pause(phy);
 
+	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);
+	ret = __phylink_validate_phy(pl, phy, supported, &config);
+	mutex_unlock(&pl->state_mutex);
+	mutex_unlock(&phy->lock);
 	if (ret) {
 		phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
 			     phy_modes(config.interface),
@@ -4176,7 +4234,7 @@ static int phylink_sfp_config_optical(struct phylink *pl)
 	/* For all the interfaces that are supported, reduce the sfp_support
 	 * mask to only those link modes that can be supported.
 	 */
-	ret = phylink_validate_mask(pl, NULL, pl->sfp_support, &config,
+	ret = phylink_validate_mask(pl, pl->sfp_support, &config,
 				    pl->sfp_interfaces);
 	if (ret) {
 		phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 06/15] net: phylink: add capabilities helpers and track modified advertising
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (4 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 05/15] net: phylink: make phylink_validate state locked Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 07/15] net: sfp: support deferred module insertion Christian Marangi
                   ` (8 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

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 <ansuelsmth@gmail.com>
---
 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



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 07/15] net: sfp: support deferred module insertion
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (5 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 06/15] net: phylink: add capabilities helpers and track modified advertising Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 08/15] net: phylink: support PCS provider release Christian Marangi
                   ` (7 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

Add support for deferring SFP module insertion when the upstream
device needs a dependency that is not yet available. If module_insert()
returns -EAGAIN, keep the module in SFP_MOD_WAITDEV with its transmitter
disabled. Other insertion errors still move the module to SFP_MOD_ERROR.

Add sfp_upstream_retry_module() to let the upstream retry insertion
once the dependency becomes available. A successful retry continues
through the normal module power and link startup sequence.

This is needed by phylink when an optical SFP is inserted before the
PCS provider for its host interface registers.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/net/phy/sfp-bus.c | 16 ++++++++++++++++
 drivers/net/phy/sfp.c     | 17 +++++++++++++++++
 drivers/net/phy/sfp.h     |  1 +
 include/linux/sfp.h       | 10 ++++++++--
 4 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/drivers/net/phy/sfp-bus.c b/drivers/net/phy/sfp-bus.c
index 8679d27bf1ba..66ac02f91bc0 100644
--- a/drivers/net/phy/sfp-bus.c
+++ b/drivers/net/phy/sfp-bus.c
@@ -563,6 +563,22 @@ void sfp_upstream_stop(struct sfp_bus *bus)
 }
 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
 
+/**
+ * sfp_upstream_retry_module() - Retry deferred module insertion
+ * @bus: a pointer to the &struct sfp_bus for the module
+ *
+ * Ask the socket to retry insertion of a module whose upstream
+ * module_insert() callback returned -EAGAIN. Call with RTNL held.
+ */
+void sfp_upstream_retry_module(struct sfp_bus *bus)
+{
+	ASSERT_RTNL();
+
+	if (bus->registered && bus->socket_ops->retry_module)
+		bus->socket_ops->retry_module(bus->sfp);
+}
+EXPORT_SYMBOL_GPL(sfp_upstream_retry_module);
+
 static void sfp_upstream_clear(struct sfp_bus *bus)
 {
 	bus->upstream_ops = NULL;
diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
index 915156743701..e5ddd62318bf 100644
--- a/drivers/net/phy/sfp.c
+++ b/drivers/net/phy/sfp.c
@@ -43,6 +43,7 @@ enum {
 	SFP_E_DEV_DETACH,
 	SFP_E_DEV_DOWN,
 	SFP_E_DEV_UP,
+	SFP_E_UPSTREAM_RETRY,
 	SFP_E_TX_FAULT,
 	SFP_E_TX_CLEAR,
 	SFP_E_LOS_HIGH,
@@ -111,6 +112,7 @@ static const char * const event_strings[] = {
 	[SFP_E_DEV_DETACH] = "dev_detach",
 	[SFP_E_DEV_DOWN] = "dev_down",
 	[SFP_E_DEV_UP] = "dev_up",
+	[SFP_E_UPSTREAM_RETRY] = "upstream_retry",
 	[SFP_E_TX_FAULT] = "tx_fault",
 	[SFP_E_TX_CLEAR] = "tx_clear",
 	[SFP_E_LOS_HIGH] = "los_high",
@@ -2823,6 +2825,10 @@ static void sfp_sm_module(struct sfp *sfp, unsigned int event)
 		sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
 		fallthrough;
 	case SFP_MOD_WAITDEV:
+		if (event != SFP_E_TIMEOUT && event != SFP_E_DEV_ATTACH &&
+		    event != SFP_E_UPSTREAM_RETRY)
+			break;
+
 		/* Ensure that the device is attached before proceeding */
 		if (sfp->sm_dev_state < SFP_DEV_DOWN)
 			break;
@@ -2830,6 +2836,8 @@ static void sfp_sm_module(struct sfp *sfp, unsigned int event)
 		/* Report the module insertion to the upstream device */
 		err = sfp_module_insert(sfp->sfp_bus, &sfp->id,
 					sfp->quirk);
+		if (err == -EAGAIN)
+			break;
 		if (err < 0) {
 			sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
 			break;
@@ -3091,6 +3099,14 @@ static void sfp_stop(struct sfp *sfp)
 	sfp_sm_event(sfp, SFP_E_DEV_DOWN);
 }
 
+static void sfp_retry_module(struct sfp *sfp)
+{
+	mutex_lock(&sfp->sm_mutex);
+	if (sfp->sm_mod_state == SFP_MOD_WAITDEV)
+		__sfp_sm_event(sfp, SFP_E_UPSTREAM_RETRY);
+	mutex_unlock(&sfp->sm_mutex);
+}
+
 static void sfp_set_signal_rate(struct sfp *sfp, unsigned int rate_kbd)
 {
 	unsigned int set;
@@ -3189,6 +3205,7 @@ static const struct sfp_socket_ops sfp_module_ops = {
 	.detach = sfp_detach,
 	.start = sfp_start,
 	.stop = sfp_stop,
+	.retry_module = sfp_retry_module,
 	.set_signal_rate = sfp_set_signal_rate,
 	.module_info = sfp_module_info,
 	.module_eeprom = sfp_module_eeprom,
diff --git a/drivers/net/phy/sfp.h b/drivers/net/phy/sfp.h
index 19fe29d844a1..de0b89f6fbca 100644
--- a/drivers/net/phy/sfp.h
+++ b/drivers/net/phy/sfp.h
@@ -20,6 +20,7 @@ struct sfp_socket_ops {
 	void (*detach)(struct sfp *sfp);
 	void (*start)(struct sfp *sfp);
 	void (*stop)(struct sfp *sfp);
+	void (*retry_module)(struct sfp *sfp);
 	void (*set_signal_rate)(struct sfp *sfp, unsigned int rate_kbd);
 	int (*module_info)(struct sfp *sfp, struct ethtool_modinfo *modinfo);
 	int (*module_eeprom)(struct sfp *sfp, struct ethtool_eeprom *ee,
diff --git a/include/linux/sfp.h b/include/linux/sfp.h
index 5c71945a5e4d..7fd4ebf1c74f 100644
--- a/include/linux/sfp.h
+++ b/include/linux/sfp.h
@@ -550,8 +550,9 @@ struct sfp_module_caps {
  * @detach: called when the sfp socket driver is unbound from the upstream
  *   (mandatory).
  * @module_insert: called after a module has been detected to determine
- *   whether the module is supported for the upstream device.
- * @module_remove: called after the module has been removed.
+ *   whether the module is supported for the upstream device. Return
+ *   -EAGAIN to defer insertion until sfp_upstream_retry_module() is called.
+ * @module_remove: called after a successfully inserted module is removed.
  * @module_start: called after the PHY probe step
  * @module_stop: called before the PHY is removed
  * @link_down: called when the link is non-operational for whatever
@@ -588,6 +589,7 @@ int sfp_get_module_eeprom_by_page(struct sfp_bus *bus,
 				  struct netlink_ext_ack *extack);
 void sfp_upstream_start(struct sfp_bus *bus);
 void sfp_upstream_stop(struct sfp_bus *bus);
+void sfp_upstream_retry_module(struct sfp_bus *bus);
 void sfp_upstream_set_signal_rate(struct sfp_bus *bus, unsigned int rate_kbd);
 void sfp_bus_put(struct sfp_bus *bus);
 struct sfp_bus *sfp_bus_find_fwnode(const struct fwnode_handle *fwnode);
@@ -635,6 +637,10 @@ static inline void sfp_upstream_stop(struct sfp_bus *bus)
 {
 }
 
+static inline void sfp_upstream_retry_module(struct sfp_bus *bus)
+{
+}
+
 static inline void sfp_upstream_set_signal_rate(struct sfp_bus *bus,
 						unsigned int rate_kbd)
 {
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 08/15] net: phylink: support PCS provider release
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (6 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 07/15] net: sfp: support deferred module insertion Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 09/15] net: phylink: support late PCS provider attach Christian Marangi
                   ` (6 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

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 <ansuelsmth@gmail.com>
---
 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 <linux/gpio/consumer.h>
 #include <linux/lockdep.h>
 #include <linux/netdevice.h>
+#include <linux/notifier.h>
 #include <linux/of.h>
 #include <linux/of_mdio.h>
+#include <linux/pcs/pcs.h>
 #include <linux/phy.h>
 #include <linux/phy_fixed.h>
 #include <linux/phy_link_topology.h>
@@ -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 <linux/phylink.h>
 
+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



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 09/15] net: phylink: support late PCS provider attach
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (7 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 08/15] net: phylink: support PCS provider release Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 10/15] net: Document PCS subsystem Christian Marangi
                   ` (5 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

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@gmail.com>
---
 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



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 10/15] net: Document PCS subsystem
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (8 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 09/15] net: phylink: support late PCS provider attach Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 11/15] MAINTAINERS: add myself as PCS subsystem maintainer Christian Marangi
                   ` (4 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

Add extensive documentation of the new PCS subsystem and the fwnode
implementation with producer/consumer API.

Also update the sfp-phylink migration guide.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 Documentation/networking/index.rst       |   1 +
 Documentation/networking/pcs.rst         | 257 +++++++++++++++++++++++
 Documentation/networking/sfp-phylink.rst |  35 +--
 3 files changed, 269 insertions(+), 24 deletions(-)
 create mode 100644 Documentation/networking/pcs.rst

diff --git a/Documentation/networking/index.rst b/Documentation/networking/index.rst
index 44a422ad3b05..3fce8f6ac089 100644
--- a/Documentation/networking/index.rst
+++ b/Documentation/networking/index.rst
@@ -28,6 +28,7 @@ Contents:
    net_failover
    page_pool
    phy
+   pcs
    sfp-phylink
    alias
    bridge
diff --git a/Documentation/networking/pcs.rst b/Documentation/networking/pcs.rst
new file mode 100644
index 000000000000..03b7de73a106
--- /dev/null
+++ b/Documentation/networking/pcs.rst
@@ -0,0 +1,257 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=============
+PCS Subsystem
+=============
+
+The PCS (Physical Coding Sublayer) subsystem handles the registration and lookup
+of PCS devices. These devices contain the upper sublayers of the Ethernet
+physical layer, generally handling framing, scrambling, and encoding tasks. PCS
+devices may also include PMA (Physical Medium Attachment) components. PCS
+devices transfer data between the Link-layer MAC device, and the rest of the
+physical layer, typically via a serdes. The output of the serdes may be
+connected more-or-less directly to the medium when using fiber-optic or
+backplane connections (1000BASE-SX, 1000BASE-KX, etc). It may also communicate
+with a separate PHY (such as over SGMII) which handles the connection to the
+medium (such as 1000BASE-T).
+
+Remark on usage of .mac_select_pcs and fw_node PCS
+--------------------------------------------------
+
+There are generally two ways to look up a PCS device.
+
+1. MAC OP struct .mac_select_pcs (considered deprecated)
+2. Internal PCS handling with ``num_possible_pcs`` and ``fill_available_pcs``
+
+Implementation 1 leaves the entire handling of the PCS to the MAC
+driver with the selection of the PCS driven by .mac_select_pcs.
+Custom implementations are required if the PCS is external to the MAC
+and needs to be handled by a separate driver.
+
+Implementation 2 makes the phylink core code to select the PCS
+provided by ``num_possible_pcs`` and ``fill_available_pcs`` either
+via internal MAC handling or firmware node (fwnode)
+
+Implementation 1 is considered deprecated and it's suggested to
+switch to implementation 2 and where possible switch to firmware
+node design.
+
+.. _pcs_fwnode:
+
+Looking up PCS Devices (fwnode implementation)
+-----------------------------------------------
+
+The lookup of a PCS device follows the common producer/consumer implementation
+used by similar subsystems with a ``#pcs-cells`` on the producer and a
+``pcs-handle`` property on the consumer::
+
+    pcs: pcs {
+        // ...
+        #pcs-cells = <0>;
+    };
+
+    ethernet-controller {
+        // ...
+        pcs-handle = <&pcs>;
+    };
+
+On :c:func:`phylink_create`, phylink will use the ``num_possible_pcs``
+value and ``fill_available_pcs`` helper function in
+:c:struct:`phylink_config` to compose the list of available PCS that can be
+used for the phylink instance.
+
+Phylink will then internally handle the selection of the correct PCS for
+the requested interface mode based on the interface modes configured in
+``pcs_interfaces`` in :c:struct:`phylink_config` struct and
+``supported_interfaces`` in :c:struct:`phylink_pcs` struct.
+
+A PCS is considered eligible when the requested interface mode is present
+in both ``pcs_interfaces`` in :c:struct:`phylink_config` struct and
+``supported_interfaces`` in :c:struct:`phylink_pcs` struct.
+
+``supported_interfaces`` describes all interface modes supported by the MAC,
+whereas ``pcs_interfaces`` identifies the subset that require PCS selection.
+
+For the special implementation where the PCS is internal or part of the MAC
+and a dedicated driver is not needed, it's possible to leave the implementation
+of the PCS to the MAC driver and just implement the ``num_possible_pcs``
+value and ``fill_available_pcs`` helper  function in
+:c:struct:`phylink_config` referencing the local :c:struct:`phylink_pcs`
+struct allocated from the MAC driver.
+
+.. _pcs_consumer:
+
+Using PCS Devices
+-----------------
+
+It's mandatory to either implement the ``mac_select_pcs`` callback
+of :c:struct:`phylink_mac_ops` or ``num_possible_pcs`` and ``fill_available_pcs``
+of :c:struct:`phylink_config` to use a PCS for a MAC.
+
+The fwnode implementation exposes simple helpers to parse the PCS from
+the fwnode :c:func:`fwnode_phylink_pcs_count` and
+:c:func:`fwnode_phylink_pcs_parse`. The :c:func:`fwnode_phylink_pcs_count` helper
+takes the fwnode where the ``pcs-handle`` should be parsed and return the
+number of PCS entries described in the fwnode.
+The :c:func:`fwnode_phylink_pcs_parse` helper takes three arguments,
+the fwnode where the ``pcs-handle`` should be parsed, an allocated array
+of :c:struct:`phylink_pcs` pointer where to put the parsed PCS from the fwnode
+and the maximum number of PCS to parse.
+Contrary to :c:func:`fwnode_phylink_pcs_count`, :c:func:`fwnode_phylink_pcs_parse`
+helper fills the allocated array with ONLY the available PCS and return the
+number of available PCS found. PCS that returns -ENODEV will be skipped and
+won't be inserted in the allocated array.
+
+A phylink instance may use multiple PCS devices. The maximum number is reported
+through ``num_possible_pcs``.
+
+It's mandatory to specify for what interface a PCS is needed. This can be done
+by filling the ``pcs_interfaces`` in :c:struct:`phylink_config` struct.
+If the requested interface mode is not present in this bitmask, phylink does
+not search for a PCS for  that specific mode. (example MAC doesn't need a PCS
+for SGMII but require one for USXGMII)
+
+With the use of the :c:func:`fwnode_phylink_pcs_parse` a common implementation
+is the following::
+
+   static int mac_fill_available_pcs(struct phylink_config *config,
+   				                      struct phylink_pcs **available_pcs,
+					                      unsigned int num_possible_pcs)
+   {
+   	struct device *dev = config->dev;
+
+   	return fwnode_phylink_pcs_parse(dev_fwnode(dev), available_pcs,
+						                    num_possible_pcs);
+   }
+
+   static int mac_setup_phylink(struct net_device *netdev)
+   {
+      struct phylink_config *config;
+
+      // ...
+
+      config->dev = &netdev->dev;
+
+      // ...
+
+      // Parse possible PCS and fill num_possible_pcs
+      config->num_possible_pcs = fwnode_phylink_pcs_count(dev_fwnode(&netdev->dev));
+      config->fill_available_pcs = mac_fill_available_pcs;
+
+      __set_bit(PHY_INTERFACE_MODE_INTERNAL, config->supported_interfaces);
+      __set_bit(PHY_INTERFACE_MODE_SGMII, config->supported_interfaces);
+      __set_bit(PHY_INTERFACE_MODE_1000BASEX, config->supported_interfaces);
+      __set_bit(PHY_INTERFACE_MODE_USXGMII, config->supported_interfaces);
+
+      // PCS required only for USXGMII
+      __set_bit(PHY_INTERFACE_MODE_USXGMII, config->pcs_interfaces);
+
+      phylink = phylink_create(config, //...
+
+It's worth to mention that it's phylink code that takes care of allocating
+the array of :c:struct:`phylink_pcs` pointer for ``fill_available_pcs``
+callback based on the value set in ``num_possible_pcs`` for
+:c:struct:`phylink_config` struct.
+
+The ``fill_available_pcs`` callback must not write more than
+``num_possible_pcs`` entries. The third argument may be used to validate
+that there is enough space to fill all the available PCS in the passed array
+of :c:struct:`phylink_pcs` pointer.
+
+The ``fill_available_pcs`` callback is called on :c:func:`phylink_create`
+and on provider notification and can be called multiple times.
+It's used to compose the available PCS list at the time of calling and
+will be called again when a PCS provider is released or a new one is attached.
+Ownership of PCS is held by phylink.
+
+.. _pcs_producer:
+
+Writing PCS Drivers
+-------------------
+
+To write a PCS driver, first implement :c:struct:`phylink_pcs_ops`. Then,
+register your PCS in your probe function using :c:func:`fwnode_pcs_add_provider`.
+The :c:func:`fwnode_pcs_add_provider` takes three arguments, the fwnode where
+the PCS provider should be registered to, a xlate function to return the requested
+PCS based on ``#pcs-cells`` and a pointer to reference private data for the xlate
+function.
+
+The PCS will then be registered to a global list of PCS provider that the
+PCS fwnode implementation will use to parse it.
+
+For the simple case where the PCS driver expose a single PCS,
+:c:func:`fwnode_pcs_simple_xlate` can be used as the xlate function.
+
+You must call :c:func:`fwnode_pcs_del_provider` from your remove function
+on driver detach.
+
+A devm variant, :c:func:`devm_fwnode_pcs_add_provider`, is available to
+automatically release the PCS provider on driver detach.
+
+It's worth to mention that xlate function MUST reference already allocated
+:c:struct:`phylink_pcs` pointer and MUST NOT dynamically allocate new PCS.
+The returned pointer is used to identify the PCS across provider lookup and
+removal notifications.
+
+Late PCS registration handling
+------------------------------
+
+It's possible that a PCS becomes available after the MAC finished probing.
+Contrary to the usual producer/consumer implementation, when a PCS is not
+registered and can't be found, the fwnode parser helper returns ``-ENODEV``
+instead of ``-EPROBE_DEFER``.
+
+This is to prevent race condition with particular devices that register
+MAC and PCS with USB or PCIe and require the MAC to be registered before
+the PCS.
+
+The phylink logic correctly handle this special case and keep the phylink
+instance in a fail condition.
+
+The PCS fwnode implementation provides a notifier to which each phylink
+instance with a non-empty ``pcs_interfaces`` in :c:type:`phylink_config`
+registers. When a new PCS provider is registered, the notifier is called
+triggering the :c:func:`pcs_provider_notify` function.
+
+On receiving a PCS provider event, :c:func:`pcs_provider_notify` will
+reprase the available PCS by calling the ``fill_available_pcs`` callback
+and update the internal PCS list for the phylink instance with both the
+removed and the newly available PCS and a phylink major config is forced.
+
+If a phylink instance was in a failure state, with the just added PCS
+now part of the available PCS internal phylink list, provided all other
+conditions are satisfied, the configuration is retried and the failure
+condition is cleared.
+
+In an SFP scenario, with no currently usable interface, phylink defers module
+insertion with ``-EAGAIN`` if the module supports an interface listed in
+``pcs_interfaces`` and the SFP socket waits with its transmitter disabled.
+After phylink adds a PCS to its list and refreshes its interface mask, it
+calls ``sfp_upstream_retry_module()`` and if there is usable interface,
+the SFP the module insertion is retried.
+
+Advertisement handling
+----------------------
+
+For links without a PHY, phylink preserves user advertisement restrictions
+when it rebuilds capabilities after a PCS provider change. This includes link
+modes restricted through ``ethtool -s`` and pause modes restricted through
+``ethtool -A``. Removing an SFP module clears the restrictions for that
+module.
+
+With an attached PHY, phylib owns the active advertisement. Phylink limits
+the initial PHY advertisement to the validated link modes. A PCS provider
+refresh subsequently revalidates phylib's current advertisement for
+phylink's own state. The ethtool settings path filters the request against
+the currently supported modes before passing it to phylib. Modes filtered
+out of a request while the PCS is unavailable must be requested again once
+those modes are available.
+
+API Reference
+-------------
+
+.. kernel-doc:: include/linux/pcs/pcs.h
+   :internal:
+
+.. kernel-doc:: include/linux/pcs/pcs-provider.h
+   :internal:
diff --git a/Documentation/networking/sfp-phylink.rst b/Documentation/networking/sfp-phylink.rst
index 5bf285d73e8a..306505118eba 100644
--- a/Documentation/networking/sfp-phylink.rst
+++ b/Documentation/networking/sfp-phylink.rst
@@ -311,33 +311,20 @@ this documentation.
 
         priv->pcs = lynx_pcs_create_mdiodev(bus, 0);
 
-    Some PCS can be recovered based on firmware information:
+    PCS that should be recovered based on firmware information should implement
+    the external PCS as a :ref:`PCS provider <pcs_producer>` and reference it
+    with the use of PCS fwnode helper :ref:`PCS fwnode helper <pcs_fwnode>`.
 
-    .. code-block:: c
-
-        priv->pcs = lynx_pcs_create_fwnode(of_fwnode_handle(node));
-
-12. Populate the :c:func:`mac_select_pcs` callback and add it to your
-    :c:type:`struct phylink_mac_ops <phylink_mac_ops>` set of ops. This function
-    must return a pointer to the relevant :c:type:`struct phylink_pcs <phylink_pcs>`
-    that will be used for the requested link configuration:
-
-    .. code-block:: c
-
-        static struct phylink_pcs *foo_select_pcs(struct phylink_config *config,
-                                                  phy_interface_t interface)
-        {
-                struct foo_priv *priv = container_of(config, struct foo_priv,
-                                                     phylink_config);
-
-                if ( /* 'interface' needs a PCS to function */ )
-                        return priv->pcs;
+12. Populate the :c:var:`num_possible_pcs` value and
+    :c:func:`fill_available_pcs` callback and add it to your
+    :c:type:`struct phylink_config <phylink_config>`.
 
-                return NULL;
-        }
+    Refer to :ref:`Documentation/networking/pcs.rst <pcs_consumer>` for
+    further details and examples.
 
-    See :c:func:`mvpp2_select_pcs` for an example of a driver that has multiple
-    internal PCS.
+    Notice that the previous implementation of populating
+    :c:func:`mac_select_pcs` is considered deprecated in favor of the new
+    described above.
 
 13. Fill-in all the :c:type:`phy_interface_t <phy_interface_t>` (i.e. all MAC to
     PHY link modes) that your MAC can output. The following example shows a
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 11/15] MAINTAINERS: add myself as PCS subsystem maintainer
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (9 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 10/15] net: Document PCS subsystem Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-29 23:05 ` [PATCH net-next v23 12/15] net: phylink: add .pcs_link_down PCS OP Christian Marangi
                   ` (3 subsequent siblings)
  14 siblings, 0 replies; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

List all the files of the Ethernet PCS subsystem and add myself as
maintainer.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 MAINTAINERS | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 68d5736ca3cc..835f789b49d8 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10036,6 +10036,15 @@ F:	include/uapi/linux/if_bridge.h
 F:	include/linux/netfilter_bridge/
 F:	net/bridge/
 
+ETHERNET PCS SUBSYSTEM
+M:	Christian Marangi <ansuelsmth@gmail.com>
+L:	netdev@vger.kernel.org
+S:	Maintained
+F:	Documentation/networking/pcs.rst
+F:	drivers/net/pcs/pcs.c
+F:	include/linux/pcs/pcs-provider.h
+F:	include/linux/pcs/pcs.h
+
 ETHERNET PHY LIBRARY
 M:	Andrew Lunn <andrew@lunn.ch>
 M:	Heiner Kallweit <hkallweit1@gmail.com>
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 12/15] net: phylink: add .pcs_link_down PCS OP
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (10 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 11/15] MAINTAINERS: add myself as PCS subsystem maintainer Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 13/15] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS Christian Marangi
                   ` (2 subsequent siblings)
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

Permit for PCS driver to define specific operation to tear down the link
between the MAC and the PCS.

This might be needed for some PCS that reset counter or require special
reset to correctly work if the link needs to be restored later.

On phylink_link_down() call, the additional phylink_pcs_link_down() will
be called after .mac_link_down to tear down the link.

PCS driver will need to define .pcs_link_down to make use of this.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Reviewed-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phylink.c |  9 +++++++++
 include/linux/phylink.h   | 12 ++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 3471ba8d5ba6..b57a7de058b8 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -1163,6 +1163,12 @@ static void phylink_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
 		pcs->ops->pcs_link_up(pcs, neg_mode, interface, speed, duplex);
 }
 
+static void phylink_pcs_link_down(struct phylink_pcs *pcs)
+{
+	if (pcs && pcs->ops->pcs_link_down)
+		pcs->ops->pcs_link_down(pcs);
+}
+
 static void phylink_pcs_disable_eee(struct phylink_pcs *pcs)
 {
 	if (pcs && pcs->ops->pcs_disable_eee)
@@ -1889,6 +1895,9 @@ static void phylink_link_down(struct phylink *pl)
 
 	pl->mac_ops->mac_link_down(pl->config, pl->act_link_an_mode,
 				   pl->cur_interface);
+
+	phylink_pcs_link_down(pl->pcs);
+
 	phylink_info(pl, "Link is Down\n");
 }
 
diff --git a/include/linux/phylink.h b/include/linux/phylink.h
index 9ce66b034437..d53319e67f77 100644
--- a/include/linux/phylink.h
+++ b/include/linux/phylink.h
@@ -531,6 +531,7 @@ struct phylink_pcs {
  * @pcs_an_restart: restart 802.3z BaseX autonegotiation.
  * @pcs_link_up: program the PCS for the resolved link configuration
  *               (where necessary).
+ * @pcs_link_down: tear down link between MAC and PCS.
  * @pcs_disable_eee: optional notification to PCS that EEE has been disabled
  *		     at the MAC.
  * @pcs_enable_eee: optional notification to PCS that EEE will be enabled at
@@ -558,6 +559,7 @@ struct phylink_pcs_ops {
 	void (*pcs_an_restart)(struct phylink_pcs *pcs);
 	void (*pcs_link_up)(struct phylink_pcs *pcs, unsigned int neg_mode,
 			    phy_interface_t interface, int speed, int duplex);
+	void (*pcs_link_down)(struct phylink_pcs *pcs);
 	void (*pcs_disable_eee)(struct phylink_pcs *pcs);
 	void (*pcs_enable_eee)(struct phylink_pcs *pcs);
 	int (*pcs_pre_init)(struct phylink_pcs *pcs);
@@ -693,6 +695,16 @@ void pcs_an_restart(struct phylink_pcs *pcs);
 void pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
 		 phy_interface_t interface, int speed, int duplex);
 
+/**
+ * pcs_link_down() - tear down link between MAC and PCS
+ * @pcs: a pointer to a &struct phylink_pcs.
+ *
+ * This call will be made just after mac_link_down() to inform the PCS the
+ * link has gone down. PCS should be configured to stop processing packets
+ * for transmission and reception.
+ */
+void pcs_link_down(struct phylink_pcs *pcs);
+
 /**
  * pcs_disable_eee() - Disable EEE at the PCS
  * @pcs: a pointer to a &struct phylink_pcs
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 13/15] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (11 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 12/15] net: phylink: add .pcs_link_down PCS OP Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:47   ` netdev-bot+sashiko
  2026-11-29 23:05 ` [PATCH net-next v23 15/15] net: airoha: add phylink support Christian Marangi
  2026-11-29 23:09 ` [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS netdev-bot+sinfo
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

Document support for Airoha Ethernet PCS for AN7581 SoC.

Airoha AN7581 SoC expose multiple Physical Coding Sublayer (PCS) for
the various Serdes port supporting different Media Independent Interface
(10BASE-R, USXGMII, 2500BASE-X, 1000BASE-X, SGMII).

This follow the new PCS provider with the use of #pcs-cells property.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
---
 .../bindings/net/pcs/airoha,pcs.yaml          | 261 ++++++++++++++++++
 1 file changed, 261 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml

diff --git a/Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml b/Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml
new file mode 100644
index 000000000000..9c1d116c1b01
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml
@@ -0,0 +1,261 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/pcs/airoha,pcs.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Airoha Ethernet PCS and Serdes
+
+maintainers:
+  - Christian Marangi <ansuelsmth@gmail.com>
+
+description:
+  Airoha AN7581 SoC expose multiple Physical Coding Sublayer (PCS) for
+  the various Serdes port supporting different Media Independent Interface
+  (10BASE-R, USXGMII, 2500BASE-X, 1000BASE-X, SGMII).
+
+properties:
+  compatible:
+    enum:
+      - airoha,an7581-pcs-eth
+      - airoha,an7581-pcs-pon
+      - airoha,an7581-pcs-pcie
+      - airoha,an7581-pcs-usb
+
+  reg:
+    minItems: 6
+    maxItems: 15
+
+  reg-names:
+    minItems: 6
+    maxItems: 15
+
+  airoha,scu:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description: phandle to the SCU node required to configure
+      the serdes line to the correct interface mode.
+
+  phys:
+    maxItems: 1
+
+  "#pcs-cells": true
+
+required:
+  - compatible
+  - reg
+  - reg-names
+  - "#pcs-cells"
+
+allOf:
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - airoha,an7581-pcs-eth
+              - airoha,an7581-pcs-pon
+
+    then:
+      properties:
+        reg:
+          items:
+            - description: PCS MAC reg
+            - description: HSGMII AN reg
+            - description: HSGMII PCS reg
+            - description: MULTI SGMII reg
+            - description: USXGMII reg
+            - description: HSGMII rate adaption reg
+            - description: PCS Analog register
+            - description: PCS PMA (Physical Medium Attachment) register
+
+        reg-names:
+          items:
+            - const: pcs_mac
+            - const: hsgmii_an
+            - const: hsgmii_pcs
+            - const: multi_sgmii
+            - const: usxgmii
+            - const: hsgmii_rate_adp
+            - const: pcs_ana
+            - const: pcs_pma
+
+        phys: false
+
+        "#pcs-cells":
+          const: 0
+
+      required:
+        - airoha,scu
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: airoha,an7581-pcs-pcie
+
+    then:
+      properties:
+        reg:
+          items:
+            - description: PCS MAC 0 reg
+            - description: HSGMII AN 0 reg
+            - description: HSGMII PCS 0 reg
+            - description: MULTI SGMII 0 reg
+            - description: USXGMII 0 reg
+            - description: HSGMII rate adaption 0 reg
+            - description: PCS MAC 1 reg
+            - description: HSGMII AN 1 reg
+            - description: HSGMII PCS 1 reg
+            - description: MULTI SGMII 1 reg
+            - description: USXGMII 1 reg
+            - description: HSGMII rate adaption 1 reg
+            - description: PCS Analog register
+            - description: PCS PMA (Physical Medium Attachment) 0 register
+            - description: PCS PMA (Physical Medium Attachment) 1 register
+
+        reg-names:
+          items:
+            - const: pcs_mac0
+            - const: hsgmii_an0
+            - const: hsgmii_pcs0
+            - const: multi_sgmii0
+            - const: usxgmii0
+            - const: hsgmii_rate_adp0
+            - const: pcs_mac1
+            - const: hsgmii_an1
+            - const: hsgmii_pcs1
+            - const: multi_sgmii1
+            - const: usxgmii1
+            - const: hsgmii_rate_adp1
+            - const: pcs_ana
+            - const: pcs_pma0
+            - const: pcs_pma1
+
+        phys: false
+
+        "#pcs-cells":
+          const: 1
+
+      required:
+        - airoha,scu
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: airoha,an7581-pcs-usb
+
+    then:
+      properties:
+        reg:
+          items:
+            - description: PCS MAC reg
+            - description: HSGMII AN reg
+            - description: HSGMII PCS reg
+            - description: MULTI SGMII reg
+            - description: HSGMII rate adaption reg
+            - description: PCS Analog register
+
+        reg-names:
+          items:
+            - const: pcs_mac
+            - const: hsgmii_an
+            - const: hsgmii_pcs
+            - const: multi_sgmii
+            - const: hsgmii_rate_adp
+            - const: pcs_ana
+
+        airoha,scu: false
+
+        "#pcs-cells":
+          const: 0
+
+      required:
+        - phys
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/phy/phy.h>
+
+    pcs@1fa08000 {
+      compatible = "airoha,an7581-pcs-pon";
+      reg = <0x1fa08000 0x1000>,
+            <0x1fa80000 0x60>,
+            <0x1fa80a00 0x164>,
+            <0x1fa84000 0x450>,
+            <0x1fa85900 0x338>,
+            <0x1fa86000 0x300>,
+            <0x1fa8a000 0x1000>,
+            <0x1fa8b000 0x1000>;
+      reg-names = "pcs_mac", "hsgmii_an", "hsgmii_pcs",
+                  "multi_sgmii", "usxgmii",
+                  "hsgmii_rate_adp", "pcs_ana", "pcs_pma";
+
+      airoha,scu = <&scuclk>;
+      #pcs-cells = <0>;
+    };
+
+    pcs@1fa09000 {
+      compatible = "airoha,an7581-pcs-eth";
+      reg = <0x1fa09000 0x1000>,
+            <0x1fa70000 0x60>,
+            <0x1fa70a00 0x164>,
+            <0x1fa74000 0x450>,
+            <0x1fa75900 0x338>,
+            <0x1fa76000 0x300>,
+            <0x1fa7a000 0x1000>,
+            <0x1fa7b000 0x1000>;
+      reg-names = "pcs_mac", "hsgmii_an", "hsgmii_pcs",
+                  "multi_sgmii", "usxgmii",
+                  "hsgmii_rate_adp", "pcs_ana", "pcs_pma";
+
+      airoha,scu = <&scuclk>;
+      #pcs-cells = <0>;
+    };
+
+    pcs@1fa04000 {
+      compatible = "airoha,an7581-pcs-pcie";
+      reg = <0x1fa04000 0x1000>,
+            <0x1fa50000 0x60>,
+            <0x1fa50a00 0x164>,
+            <0x1fa54000 0x450>,
+            <0x1fa55900 0x338>,
+            <0x1fa56000 0x300>,
+            <0x1fa05000 0x1000>,
+            <0x1fa60000 0x60>,
+            <0x1fa60a00 0x164>,
+            <0x1fa64000 0x450>,
+            <0x1fa65900 0x338>,
+            <0x1fa66000 0x300>,
+            <0x1fa5a000 0x1000>,
+            <0x1fa5b000 0x1000>,
+            <0x1fa5c000 0x1000>;
+      reg-names = "pcs_mac0", "hsgmii_an0", "hsgmii_pcs0",
+                  "multi_sgmii0", "usxgmii0",
+                  "hsgmii_rate_adp0",
+                  "pcs_mac1", "hsgmii_an1", "hsgmii_pcs1",
+                  "multi_sgmii1", "usxgmii1",
+                  "hsgmii_rate_adp1",
+                  "pcs_ana", "pcs_pma0", "pcs_pma1";
+
+      airoha,scu = <&scuclk>;
+      #pcs-cells = <1>;
+    };
+
+    pcs@1fa07000 {
+      compatible = "airoha,an7581-pcs-usb";
+      reg = <0x1fa07000 0x1000>,
+            <0x1fa90000 0x60>,
+            <0x1fa90a00 0x164>,
+            <0x1fa94000 0x450>,
+            <0x1fa96000 0x300>,
+            <0x1fa9a000 0x600>;
+      reg-names = "pcs_mac", "hsgmii_an", "hsgmii_pcs",
+                  "multi_sgmii", "hsgmii_rate_adp","pcs_ana";
+
+      phys = <&usb0_phy PHY_TYPE_USB3>;
+
+      #pcs-cells = <0>;
+    };
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH net-next v23 15/15] net: airoha: add phylink support
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (12 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 13/15] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS Christian Marangi
@ 2026-11-29 23:05 ` Christian Marangi
  2026-11-30 23:48   ` netdev-bot+sashiko
  2026-11-29 23:09 ` [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS netdev-bot+sinfo
  14 siblings, 1 reply; 28+ messages in thread
From: Christian Marangi @ 2026-11-29 23:05 UTC (permalink / raw)
  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, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm
  Cc: Zhengping Zhang

Add phylink support for each GDM port. For GDM1 add the internal interface
mode as the only supported mode. For GDM2/3/4 add the required
configuration of the PCS to make the external PHY or attached SFP cage
work.

These needs to be defined in the GDM port node using the pcs-handle
property.

Update and provide a .get/set_link_ksettings function that use phylink
for ethtool OPs now that we fully support phylink.

Tested-by: Zhengping Zhang <zhengping.zhang@airoha.com>
Acked-by: Lorenzo Bianconi <lorenzo@kernel.org>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/net/ethernet/airoha/Kconfig       |   2 +
 drivers/net/ethernet/airoha/airoha_eth.c  | 197 +++++++++++++++++++++-
 drivers/net/ethernet/airoha/airoha_eth.h  |   5 +
 drivers/net/ethernet/airoha/airoha_regs.h |  12 ++
 4 files changed, 213 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/airoha/Kconfig b/drivers/net/ethernet/airoha/Kconfig
index 1f6640a15fc9..3c7403870aa8 100644
--- a/drivers/net/ethernet/airoha/Kconfig
+++ b/drivers/net/ethernet/airoha/Kconfig
@@ -20,6 +20,8 @@ config NET_AIROHA
 	depends on NET_DSA || !NET_DSA
 	select NET_AIROHA_NPU
 	select PAGE_POOL
+	select FWNODE_PCS
+	select PHYLINK
 	help
 	  This driver supports the gigabit ethernet MACs in the
 	  Airoha SoC family.
diff --git a/drivers/net/ethernet/airoha/airoha_eth.c b/drivers/net/ethernet/airoha/airoha_eth.c
index 044ad0302ef3..e41dfad58b25 100644
--- a/drivers/net/ethernet/airoha/airoha_eth.c
+++ b/drivers/net/ethernet/airoha/airoha_eth.c
@@ -6,6 +6,7 @@
 #include <linux/of.h>
 #include <linux/of_net.h>
 #include <linux/of_reserved_mem.h>
+#include <linux/pcs/pcs.h>
 #include <linux/platform_device.h>
 #include <linux/tcp.h>
 #include <linux/u64_stats_sync.h>
@@ -2194,10 +2195,18 @@ static int airoha_dev_open(struct net_device *netdev)
 		return -EBUSY;
 	}
 
+	err = phylink_of_phy_connect(dev->phylink, netdev->dev.of_node, 0);
+	if (err) {
+		netdev_err(netdev, "could not attach PHY: %d\n", err);
+		return err;
+	}
+
+	phylink_start(dev->phylink);
+
 	netif_tx_start_all_queues(netdev);
 	err = airoha_set_vip_for_gdm_port(dev, true);
 	if (err)
-		return err;
+		goto err_phy_stop;
 
 	if (netdev_uses_dsa(netdev))
 		airoha_fe_set(qdma->eth, REG_GDM_INGRESS_CFG(port->id),
@@ -2219,6 +2228,12 @@ static int airoha_dev_open(struct net_device *netdev)
 		airoha_dev_lro_enable(dev);
 
 	return 0;
+
+err_phy_stop:
+	phylink_stop(dev->phylink);
+	phylink_disconnect_phy(dev->phylink);
+
+	return err;
 }
 
 static int airoha_dev_stop(struct net_device *netdev)
@@ -2226,6 +2241,8 @@ static int airoha_dev_stop(struct net_device *netdev)
 	struct airoha_gdm_dev *dev = netdev_priv(netdev);
 	struct airoha_gdm_port *port = dev->port;
 
+	phylink_stop(dev->phylink);
+
 	netif_tx_disable(netdev);
 	airoha_set_vip_for_gdm_port(dev, false);
 
@@ -2239,6 +2256,8 @@ static int airoha_dev_stop(struct net_device *netdev)
 	if (netdev->features & NETIF_F_LRO)
 		airoha_dev_lro_disable(dev);
 
+	phylink_disconnect_phy(dev->phylink);
+
 	return 0;
 }
 
@@ -2829,6 +2848,24 @@ airoha_ethtool_get_rmon_stats(struct net_device *netdev,
 	} while (u64_stats_fetch_retry(&dev->stats.syncp, start));
 }
 
+static int
+airoha_ethtool_get_link_ksettings(struct net_device *netdev,
+				  struct ethtool_link_ksettings *cmd)
+{
+	struct airoha_gdm_dev *dev = netdev_priv(netdev);
+
+	return phylink_ethtool_ksettings_get(dev->phylink, cmd);
+}
+
+static int
+airoha_ethtool_set_link_ksettings(struct net_device *netdev,
+				  const struct ethtool_link_ksettings *cmd)
+{
+	struct airoha_gdm_dev *dev = netdev_priv(netdev);
+
+	return phylink_ethtool_ksettings_set(dev->phylink, cmd);
+}
+
 static int airoha_qdma_set_chan_tx_sched(struct net_device *netdev,
 					 int channel, enum tx_sched_mode mode,
 					 const u16 *weights, u8 n_weights)
@@ -3696,7 +3733,8 @@ static const struct ethtool_ops airoha_ethtool_ops = {
 	.get_drvinfo		= airoha_ethtool_get_drvinfo,
 	.get_eth_mac_stats      = airoha_ethtool_get_mac_stats,
 	.get_rmon_stats		= airoha_ethtool_get_rmon_stats,
-	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
+	.get_link_ksettings	= airoha_ethtool_get_link_ksettings,
+	.set_link_ksettings	= airoha_ethtool_set_link_ksettings,
 	.get_link		= ethtool_op_get_link,
 };
 
@@ -3752,6 +3790,155 @@ bool airoha_is_valid_gdm_dev(struct airoha_eth *eth,
 	return false;
 }
 
+/* Nothing to do in MAC, everything is handled in PCS */
+static void airoha_mac_config(struct phylink_config *config, unsigned int mode,
+			      const struct phylink_link_state *state)
+{
+}
+
+static void airoha_mac_link_up(struct phylink_config *config, struct phy_device *phy,
+			       unsigned int mode, phy_interface_t interface,
+			       int speed, int duplex, bool tx_pause, bool rx_pause)
+{
+	struct airoha_gdm_dev *dev = container_of(config, struct airoha_gdm_dev,
+						  phylink_config);
+	struct airoha_gdm_port *port = dev->port;
+	struct airoha_eth *eth = dev->eth;
+	u32 frag_size_tx, frag_size_rx;
+
+	/* TX/RX frag is configured only for GDM4 */
+	if (port->id != AIROHA_GDM4_IDX)
+		return;
+
+	switch (speed) {
+	case SPEED_10000:
+	case SPEED_5000:
+		frag_size_tx = 8;
+		frag_size_rx = 8;
+		break;
+	case SPEED_2500:
+		frag_size_tx = 2;
+		frag_size_rx = 1;
+		break;
+	default:
+		frag_size_tx = 1;
+		frag_size_rx = 0;
+		break;
+	}
+
+	mutex_lock(&port->link_lock);
+
+	/* Configure TX/RX frag based on speed */
+	if (dev->nbq == 1) {
+		airoha_fe_rmw(eth, REG_FE_GDM4_TMBI_FRAG,
+			      GDM4_SGMII1_TX_FRAG_SIZE_MASK,
+			      FIELD_PREP(GDM4_SGMII1_TX_FRAG_SIZE_MASK,
+					 frag_size_tx));
+
+		airoha_fe_rmw(eth, REG_FE_GDM4_RMBI_FRAG,
+			      GDM4_SGMII1_RX_FRAG_SIZE_MASK,
+			      FIELD_PREP(GDM4_SGMII1_RX_FRAG_SIZE_MASK,
+					 frag_size_rx));
+	} else {
+		airoha_fe_rmw(eth, REG_FE_GDM4_TMBI_FRAG,
+			      GDM4_SGMII0_TX_FRAG_SIZE_MASK,
+			      FIELD_PREP(GDM4_SGMII0_TX_FRAG_SIZE_MASK,
+					 frag_size_tx));
+
+		airoha_fe_rmw(eth, REG_FE_GDM4_RMBI_FRAG,
+			      GDM4_SGMII0_RX_FRAG_SIZE_MASK,
+			      FIELD_PREP(GDM4_SGMII0_RX_FRAG_SIZE_MASK,
+					 frag_size_rx));
+	}
+
+	mutex_unlock(&port->link_lock);
+}
+
+/* Nothing to do in MAC, everything is handled in PCS */
+static void airoha_mac_link_down(struct phylink_config *config, unsigned int mode,
+				 phy_interface_t interface)
+{
+}
+
+static const struct phylink_mac_ops airoha_phylink_ops = {
+	.mac_config = airoha_mac_config,
+	.mac_link_up = airoha_mac_link_up,
+	.mac_link_down = airoha_mac_link_down,
+};
+
+static int airoha_fill_available_pcs(struct phylink_config *config,
+				     struct phylink_pcs **available_pcs,
+				     unsigned int num_possible_pcs)
+{
+	struct device *dev = config->dev;
+
+	return fwnode_phylink_pcs_parse(dev_fwnode(dev), available_pcs,
+					num_possible_pcs);
+}
+
+static int airoha_setup_phylink(struct net_device *netdev)
+{
+	struct airoha_gdm_dev *dev = netdev_priv(netdev);
+	struct device_node *np = netdev->dev.of_node;
+	struct airoha_gdm_port *port = dev->port;
+	struct phylink_config *config;
+	phy_interface_t phy_mode;
+	struct phylink *phylink;
+	int err;
+
+	err = of_get_phy_mode(np, &phy_mode);
+	if (err) {
+		dev_err(&netdev->dev, "incorrect phy-mode\n");
+		return err;
+	}
+
+	config = &dev->phylink_config;
+	config->dev = &netdev->dev;
+	config->type = PHYLINK_NETDEV;
+
+	/*
+	 * GDM1 only supports internal for Embedded Switch
+	 * and doesn't require a PCS.
+	 */
+	if (port->id == AIROHA_GDM1_IDX) {
+		config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
+					   MAC_10000FD;
+
+		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
+			  config->supported_interfaces);
+	} else {
+		config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
+					   MAC_10 | MAC_100 | MAC_1000 |
+					   MAC_2500FD | MAC_5000FD | MAC_10000FD;
+
+		config->num_possible_pcs = fwnode_phylink_pcs_count(dev_fwnode(config->dev));
+		config->fill_available_pcs = airoha_fill_available_pcs;
+
+		__set_bit(PHY_INTERFACE_MODE_SGMII,
+			  config->supported_interfaces);
+		__set_bit(PHY_INTERFACE_MODE_1000BASEX,
+			  config->supported_interfaces);
+		__set_bit(PHY_INTERFACE_MODE_2500BASEX,
+			  config->supported_interfaces);
+		__set_bit(PHY_INTERFACE_MODE_10GBASER,
+			  config->supported_interfaces);
+		__set_bit(PHY_INTERFACE_MODE_USXGMII,
+			  config->supported_interfaces);
+
+		phy_interface_copy(config->pcs_interfaces,
+				   config->supported_interfaces);
+	}
+
+	phylink = phylink_create(config, of_fwnode_handle(np),
+				 phy_mode, &airoha_phylink_ops);
+	if (IS_ERR(phylink))
+		return PTR_ERR(phylink);
+
+	dev->phylink = phylink;
+
+	return 0;
+}
+
 static int airoha_alloc_gdm_device(struct airoha_eth *eth,
 				   struct airoha_gdm_port *port,
 				   int nbq, struct device_node *np)
@@ -3815,7 +4002,7 @@ static int airoha_alloc_gdm_device(struct airoha_eth *eth,
 	dev->nbq = nbq;
 	port->devs[index] = dev;
 
-	return 0;
+	return airoha_setup_phylink(netdev);
 }
 
 static int airoha_alloc_gdm_port(struct airoha_eth *eth,
@@ -3851,6 +4038,7 @@ static int airoha_alloc_gdm_port(struct airoha_eth *eth,
 
 	port->id = id;
 	spin_lock_init(&port->stats_lock);
+	mutex_init(&port->link_lock);
 	eth->ports[p] = port;
 
 	err = airoha_metadata_dst_alloc(port);
@@ -4046,6 +4234,8 @@ static int airoha_probe(struct platform_device *pdev)
 			netdev = netdev_from_priv(dev);
 			if (netdev->reg_state == NETREG_REGISTERED)
 				unregister_netdev(netdev);
+			if (dev->phylink)
+				phylink_destroy(dev->phylink);
 			of_node_put(netdev->dev.of_node);
 		}
 		airoha_metadata_dst_free(port);
@@ -4084,6 +4274,7 @@ static void airoha_remove(struct platform_device *pdev)
 
 			netdev = netdev_from_priv(dev);
 			unregister_netdev(netdev);
+			phylink_destroy(dev->phylink);
 			of_node_put(netdev->dev.of_node);
 		}
 		airoha_metadata_dst_free(port);
diff --git a/drivers/net/ethernet/airoha/airoha_eth.h b/drivers/net/ethernet/airoha/airoha_eth.h
index d270729e22ff..909e75fec279 100644
--- a/drivers/net/ethernet/airoha/airoha_eth.h
+++ b/drivers/net/ethernet/airoha/airoha_eth.h
@@ -612,6 +612,9 @@ struct airoha_gdm_dev {
 	 * QDMA migration.
 	 */
 	spinlock_t txq_lock[AIROHA_NUM_NETDEV_TX_RINGS];
+
+	struct phylink *phylink;
+	struct phylink_config phylink_config;
 };
 
 struct airoha_gdm_port {
@@ -621,6 +624,8 @@ struct airoha_gdm_port {
 
 	/* protect concurrent hw_stats accesses */
 	spinlock_t stats_lock;
+	/* protect concurrent GDM4 register access */
+	struct mutex link_lock;
 
 	struct metadata_dst *dsa_meta[AIROHA_MAX_DSA_PORTS];
 };
diff --git a/drivers/net/ethernet/airoha/airoha_regs.h b/drivers/net/ethernet/airoha/airoha_regs.h
index 07dd04859fde..94d53d4c3ab7 100644
--- a/drivers/net/ethernet/airoha/airoha_regs.h
+++ b/drivers/net/ethernet/airoha/airoha_regs.h
@@ -371,6 +371,18 @@
 #define IP_FRAGMENT_PORT_MASK		GENMASK(8, 5)
 #define IP_FRAGMENT_NBQ_MASK		GENMASK(4, 0)
 
+#define REG_FE_GDM4_TMBI_FRAG		0x2028
+#define GDM4_SGMII1_TX_WEIGHT_MASK	GENMASK(31, 26)
+#define GDM4_SGMII1_TX_FRAG_SIZE_MASK	GENMASK(25, 16)
+#define GDM4_SGMII0_TX_WEIGHT_MASK	GENMASK(15, 10)
+#define GDM4_SGMII0_TX_FRAG_SIZE_MASK	GENMASK(9, 0)
+
+#define REG_FE_GDM4_RMBI_FRAG		0x202c
+#define GDM4_SGMII1_RX_WEIGHT_MASK	GENMASK(31, 26)
+#define GDM4_SGMII1_RX_FRAG_SIZE_MASK	GENMASK(25, 16)
+#define GDM4_SGMII0_RX_WEIGHT_MASK	GENMASK(15, 10)
+#define GDM4_SGMII0_RX_FRAG_SIZE_MASK	GENMASK(9, 0)
+
 #define REG_MC_VLAN_EN			0x2100
 #define MC_VLAN_EN_MASK			BIT(0)
 
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS
  2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
                   ` (13 preceding siblings ...)
  2026-11-29 23:05 ` [PATCH net-next v23 15/15] net: airoha: add phylink support Christian Marangi
@ 2026-11-29 23:09 ` netdev-bot+sinfo
  14 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sinfo @ 2026-11-29 23:09 UTC (permalink / raw)
  To: Christian Marangi
  Cc: 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, Lorenzo Bianconi, Heiner Kallweit, Russell King,
	Maxime Chevallier, Philipp Zabel, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt,
	Russell King (Oracle), netdev, devicetree, linux-doc,
	linux-kernel, linux-arm-kernel, linux-mediatek, llvm

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 01/15] net: phylink: make PCS phylink instance access RCU protected
  2026-11-29 23:05 ` [PATCH net-next v23 01/15] net: phylink: make PCS phylink instance access RCU protected Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] phylink_destroy() in drivers/net/phy/phylink.c now writes through
  pl->pcs with `rcu_assign_pointer(pl->pcs->phylink, NULL)` (lines…

--

net: phylink: make PCS phylink instance access RCU protected

This patch makes the phylink back-pointer in struct phylink_pcs RCU
protected. phylink_pcs_change() now reads it under rcu_read_lock().
phylink_destroy() also clears the pointer on the currently selected PCS
and calls synchronize_rcu() before freeing the phylink instance.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index d1f1142a98a4e..d736604b86f48 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -2157,6 +2158,16 @@ void phylink_destroy(struct phylink *pl)
>  	if (pl->link_gpio)
>  		gpiod_put(pl->link_gpio);
>  
> +	if (pl->pcs)
> +		rcu_assign_pointer(pl->pcs->phylink, NULL);

[Severity: Low]
Does this add a new lifetime requirement on the PCS for callers of
phylink_destroy()?

Before this patch, phylink_destroy() never touched the PCS. pl->pcs is
only reassigned in phylink_major_config() and is not cleared by
phylink_stop(). So at destroy time it still points at the last PCS
returned by mac_select_pcs().

If a MAC or DSA driver freed its PCS after phylink_stop() and before
phylink_destroy() (via xpcs_destroy_pcs(), lynx_pcs_destroy(),
mtk_pcs_lynxi_destroy() and so on), that order used to be harmless. With
this change it becomes a write into freed memory.

I couldn't find an in-tree caller that uses this order today. stmmac,
txgbe, altera_tse, enetc, dpaa and the DSA teardown paths all call
phylink_destroy() before they free the PCS. So this is about the API
contract rather than a current trigger.

The kernel-doc for phylink_destroy() still only says:

 * Destroy a phylink instance. Any PHY that has been attached must have been
 * cleaned up via phylink_disconnect_phy() prior to calling this function.

Should it also say that the last selected PCS must still be allocated
when phylink_destroy() is called? This is a Fixes: tagged patch, so
external drivers on stable kernels would pick up the new requirement.

The same requirement remains later in the series. There, phylink_destroy()
goes through phylink_detach_pcss(), which keeps the dereference for the
legacy mac_select_pcs path:

	if (pl->pcs) {
		rcu_assign_pointer(pl->pcs->phylink, NULL);
		WRITE_ONCE(pl->pcs, NULL);
	}

The phylink_destroy() kernel-doc is still unchanged at that point.

> +
> +	/*
> +	 * Unconditionally wait for readers that may have obtained this
> +	 * phylink through a PCS while phylink_major_config() was clearing
> +	 * the association.
> +	 */
> +	synchronize_rcu();
> +
>  	cancel_work_sync(&pl->resolve);
>  	kfree(pl);
>  }

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 03/15] net: phylink: introduce internal phylink PCS handling
  2026-11-29 23:05 ` [PATCH net-next v23 03/15] net: phylink: introduce internal phylink PCS handling Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] In PCS-list mode, phylink chooses a PCS in three places, and
  they disagree when no listed PCS supports a mode that needs one.
- [Medium] phylink_destroy() (drivers/net/phy/phylink.c:2375-2376) now
  calls phylink_detach_pcss() before cancel_work_sync(&pl->resolve).
- [Low] net: phylink: duplicated PCS lookup logic across three functions

--

net: phylink: introduce internal phylink PCS handling

This adds a PCS list to phylink as an alternative to .mac_select_pcs. The
MAC fills an array through .fill_available_pcs. The modes that need a
dedicated PCS are described by a new pcs_interfaces bitmap. PCS selection
in validation, inband caps and major config walks that list, and the PCS
teardown is shared between phylink_destroy() and the phylink_create()
error path.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 20759fe47eaa7..35e82cccd39a2 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -536,6 +559,21 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl,
>  		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
>  		if (IS_ERR(pcs))
>  			return PTR_ERR(pcs);
> +	/*
> +	 * Find a PCS in available PCS list for the requested interface.
> +	 *
> +	 * Skip searching if the MAC doesn't require a dedicated PCS for
> +	 * the requested interface.
> +	 */
> +	} else if (test_bit(state->interface, pl->config->pcs_interfaces)) {
> +		struct phylink_pcs *tmp;
> +
> +		list_for_each_entry(tmp, &pl->pcs_list, list) {
> +			if (!phylink_validate_pcs_interface(tmp, state->interface)) {
> +				pcs = tmp;
> +				break;
> +			}
> +		}
>  	}
>  
>  	if (pcs) {

[Severity: Low]
The same PCS selection is now open-coded three times: here in
phylink_validate_mac_and_pcs(), in phylink_inband_caps() and in
phylink_major_config(). Each copy tests pl->config->pcs_interfaces,
walks pl->pcs_list and takes the first entry that
phylink_validate_pcs_interface() accepts.

The copies already disagree on what a miss means.
phylink_major_config() fails the major config when no PCS on the list
matches. phylink_validate_mac_and_pcs() and phylink_inband_caps() leave
pcs as NULL and carry on as if the mode needed no PCS at all.

Any later change to the selection rule would also have to be repeated
in all three places. Preferring the currently active PCS would be one
example of such a change.

Could this be factored into a single helper, something like
phylink_find_pcs(pl, interface), that also covers the .mac_select_pcs
branch? The callers would then share one place that decides what a
missing PCS for a pcs_interfaces mode means.

[Severity: Medium]
What happens here when a mode is set in pcs_interfaces but no PCS on
pl->pcs_list supports it? pcs stays NULL, the check falls through to the
MAC capabilities only, and phylink_validate_mac_and_pcs() returns success.
phylink_inband_caps() does the same thing and returns 0.

phylink_major_config() treats the same case as fatal:

    if (!pcs) {
        phylink_err(pl, "couldn't find a PCS for %s\n", ...);
        pl->major_config_failed = true;
        return;
    }

The comment in phylink_parse_supported_interfaces() says it keeps phylink
from accepting a PCS-only mode without a PCS. However, the function starts
by copying config->supported_interfaces unchanged, and after that it only
ORs PCS modes in:

    phy_interface_copy(pl->supported_interfaces,
                       pl->config->supported_interfaces);

phylink_create() rejects an empty config->supported_interfaces before any
PCS is merged. The kernel-doc says supported_interfaces covers modes
"supported by the MAC/PCS". So PCS-required modes end up in
config->supported_interfaces anyway.

Later in the series, Documentation/networking/pcs.rst describes
pcs_interfaces as a subset of supported_interfaces. Also,
airoha_setup_phylink() sets pcs_interfaces equal to supported_interfaces.
That makes the filtering a no-op.

Suppose the PCS list is empty, for example because a fwnode PCS provider
has not probed yet, or it doesn't cover a given mode. Could ethtool then
advertise link modes that can't be configured, and could SFP/PHY
interface selection pick a mode with no PCS?

If that happens, the link would stay down with "couldn't find a PCS" on
every resolve. pl->pcs would still point at the previous PCS, while
phylink_resolve() goes on updating pl->link_config.interface.

The SFP -EAGAIN deferral added later in the series, in
phylink_sfp_module_insert(), tests caps->interfaces against
pl->supported_interfaces. For airoha that test would never fire.

Should pcs_interfaces modes that no listed PCS supports be removed from
pl->supported_interfaces? Or should validation fail in the same way
phylink_major_config() does?

[ ... ]

> @@ -2042,6 +2168,62 @@ EXPORT_SYMBOL_GPL(phylink_update_mac_pause_capabilities);

[ ... ]

> +static void phylink_detach_pcss(struct phylink *pl)
> +{
> +	struct phylink_pcs *pcs, *tmp;
> +
> +	mutex_lock(&pl->state_mutex);
> +
> +	mutex_lock(&phylink_pcs_ownership_mutex);
> +	/* Remove every PCS from phylink PCS list */
> +	list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list) {
> +		rcu_assign_pointer(pcs->phylink, NULL);
> +		list_del(&pcs->list);
> +	}
> +
> +	if (pl->pcs) {
> +		rcu_assign_pointer(pl->pcs->phylink, NULL);
> +		WRITE_ONCE(pl->pcs, NULL);
> +	}
> +	mutex_unlock(&phylink_pcs_ownership_mutex);
> +
> +	/*
> +	 * Unconditionally wait for readers that may have obtained this
> +	 * phylink through a PCS while phylink_major_config() was clearing
> +	 * the association.
> +	 */
> +	synchronize_rcu();
> +
> +	mutex_unlock(&pl->state_mutex);
> +}

[ ... ]

> @@ -2167,17 +2372,9 @@ void phylink_destroy(struct phylink *pl)
>  	if (pl->link_gpio)
>  		gpiod_put(pl->link_gpio);
>  
> -	if (pl->pcs)
> -		rcu_assign_pointer(pl->pcs->phylink, NULL);
> -
> -	/*
> -	 * Unconditionally wait for readers that may have obtained this
> -	 * phylink through a PCS while phylink_major_config() was clearing
> -	 * the association.
> -	 */
> -	synchronize_rcu();
> -
> +	phylink_detach_pcss(pl);
>  	cancel_work_sync(&pl->resolve);
> +
>  	kfree(pl);
>  }

[Severity: Medium]
Can the resolve work link a PCS back to pl after phylink_detach_pcss()
has run?

phylink_detach_pcss() clears every pcs->phylink and now also sets pl->pcs
to NULL. It then calls synchronize_rcu() and drops state_mutex. A resolve
work item that was pending, or blocked on state_mutex, runs after that.

phylink_resolve() calls phylink_major_config() whenever
pl->force_major_config is set, even with PHYLINK_DISABLE_STOPPED set. On
the .mac_select_pcs path pl->pcs is now NULL, so pcs_changed is true and
this runs:

    if (pcs)
        rcu_assign_pointer(pcs->phylink, pl);

cancel_work_sync() then waits for the worker to finish, and kfree(pl)
frees the instance while pcs->phylink still points to it. A later
phylink_pcs_change() on that PCS would rcu_dereference(pcs->phylink) and
read pl->pcs from freed memory.

Both preconditions appear reachable:

- force_major_config can stay set on a stopped port.
  phylink_replay_link_end() sets it, but no resolve is queued while STOPPED
  is set. sja1105_static_config_reload() runs replay begin/end on every
  available port, including ports that are down.

- The work can be queued after phylink_stop() calls flush_work(), because
  phylink_run_resolve() checks phylink_disable_state without locking.

Before this patch, pl->pcs stayed set during destroy. Re-selecting the
same PCS gave pcs_changed == false, so the PCS was not linked again.

I couldn't find an in-tree driver where all of these conditions hold at
once, so this may be theoretical for now.

Would it make sense to cancel the resolve work before calling
phylink_detach_pcss()? That would apply here and in the phylink_create()
error path, which uses the same order.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 04/15] net: pcs: implement Firmware node support for PCS driver
  2026-11-29 23:05 ` [PATCH net-next v23 04/15] net: pcs: implement Firmware node support for PCS driver Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 5

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The new helpers make `#pcs-cells` mandatory on every PCS node
  that `pcs-handle` points to.
- [Low] `fwnode_phylink_pcs_count()` (drivers/net/pcs/pcs.c:229-241)
  stops at the first non-zero return from…
- [Low] Concern: `fwnode_pcs_get()`/`fwnode_phylink_pcs_parse()` hand out
  raw `struct phylink_pcs` pointers with no reference count or put API,…
- [Low] The `!CONFIG_FWNODE_PCS` stubs in include/linux/pcs/pcs.h return
  different error codes from the real implementation for the same…
- [Low] `fwnode_parse_pcsspec()` has a name-based lookup branch (`if
  (name)` → `fwnode_property_match_string(fwnode, "pcs-handle-names",…
- [Low] include/linux/pcs/pcs-provider.h does not compile on its own.

--

net: pcs: implement Firmware node support for PCS driver

This adds a small provider API. PCS drivers can register PCS instances
against a firmware node with an xlate callback, and a devm variant is
included. Consumers get fwnode_pcs_get(), and MAC drivers get
fwnode_phylink_pcs_count() and fwnode_phylink_pcs_parse() for PCS
described through the "pcs-handle" property.

> diff --git a/drivers/net/pcs/pcs.c b/drivers/net/pcs/pcs.c
> new file mode 100644
> index 0000000000000..a0b3681db54bf
> --- /dev/null
> +++ b/drivers/net/pcs/pcs.c
> @@ -0,0 +1,279 @@

[ ... ]

> +static void fwnode_pcs_free_provider(struct fwnode_pcs_provider *pp)
> +{
> +	struct phylink_pcs *pcs, *tmp;
> +
> +	mutex_lock(&fwnode_pcs_mutex);
> +	list_for_each_entry_safe(pcs, tmp, &pp->pcss, provider_list) {
> +		list_del(&pcs->provider_list);
> +		pcs->provider = NULL;
> +	}
> +	mutex_unlock(&fwnode_pcs_mutex);

[Severity: Low]
fwnode_pcs_get() and fwnode_phylink_pcs_parse() return raw struct
phylink_pcs pointers. They take no reference and there is no put API.
On removal this only clears pcs->provider and provider_list.

Could a consumer that stored the pointer keep using it after the provider
frees its PCS?

This patch has no consumer yet. Later in the series, "net: phylink:
support PCS provider release" makes fwnode_pcs_del_provider() call a
FWNODE_PCS_PROVIDER_DEL notifier before fwnode_pcs_free_provider().
phylink handles that notifier under pcs_list_mutex, so the intended
consumer is covered.

That leaves a non-phylink caller of the exported fwnode_pcs_get(), and
there is none in the tree. Would it help to document that such callers
have to handle provider removal themselves?

[ ... ]

> +static int fwnode_parse_pcsspec(const struct fwnode_handle *fwnode,
> +				unsigned int index, const char *name,
> +				struct fwnode_reference_args *out_args)
> +{
> +	if (!fwnode)
> +		return -EINVAL;
> +
> +	if (name) {
> +		int ret;
> +
> +		ret = fwnode_property_match_string(fwnode, "pcs-handle-names",
> +						   name);

[Severity: Low]
This isn't a bug, but can this branch ever run? __fwnode_pcs_get() has
only one caller, fwnode_pcs_get(), and it always passes con_id as NULL:

    return __fwnode_pcs_get(fwnode, index, NULL);

Neither this patch nor the rest of the series adds a by-name lookup API,
and the commit message doesn't mention name-based lookup.

> +		if (ret < 0)
> +			return ret;
> +
> +		index = ret;
> +	}
> +
> +	return fwnode_property_get_reference_args(fwnode, "pcs-handle",
> +						  "#pcs-cells", 0, index,
> +						  out_args);
> +}

[Severity: Medium]
Does a non-NULL nargs_prop here make #pcs-cells mandatory on every node
that pcs-handle points to?

On OF, of_fwnode_get_reference_args() does:

	if (nargs_prop)
		ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
						 nargs_prop, index, &of_args);
	else
		ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
						       nargs, index, &of_args);

So the nargs = 0 fallback is never used. of_parse_phandle_with_args()
uses a cell_count of -1. If the target node has no #pcs-cells,
of_phandle_iterator_next() logs "could not get #pcs-cells" and returns
-EINVAL.

The generic binding in ethernet-controller.yaml describes pcs-handle as
bare phandles and doesn't mention #pcs-cells:

  pcs-handle:
    $ref: /schemas/types.yaml#/definitions/phandle-array
    items:
      maxItems: 1

None of the in-tree DTS files that use pcs-handle set #pcs-cells on the
referenced node.

On such a DT, wouldn't fwnode_pcs_get() return -EINVAL and
fwnode_phylink_pcs_count() return 0? fwnode_phylink_pcs_parse() would
pass the fwnode_property_present() check and then return -EINVAL to the
MAC.

The commit message says these helpers are:

    "provided for MAC drivers that declare PCS in DT or ACPI"

but no existing PCS description seems to work with them.

The custom xlate case also conflicts with the generic schema. Later in
the series the Airoha binding gives airoha,an7581-pcs-pcie
#pcs-cells = <1>, so a consumer needs pcs-handle = <&pcs N>. That is two
cells per item, but ethernet-controller.yaml still has items: maxItems: 1
at the end of the series.

Should the lookup fall back to 0 cells when #pcs-cells is absent? Or
should the generic binding be updated in a way that still covers
existing DTs?

[ ... ]

> +unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode)
> +{
> +	struct fwnode_reference_args out_args;
> +	unsigned int index = 0;
> +	int ret;
> +
> +	while (true) {
> +		ret = fwnode_property_get_reference_args(fwnode, "pcs-handle",
> +							 "#pcs-cells", 0, index,
> +							 &out_args);
> +		/* We expect to reach an -ENOENT error while counting */
> +		if (ret)
> +			break;

[Severity: Low]
Is any error supposed to end the count here, or only -ENOENT? A missing
#pcs-cells, a phandle that doesn't resolve, or a malformed cell count
gives -EINVAL. In that case this returns the partial index, and the
caller can't tell that anything went wrong.

fwnode_phylink_pcs_parse() returns that same -EINVAL for the same node,
so the two helpers disagree. The kernel-doc also says this counts all
PCS references in the firmware node.

Later in the series airoha_eth.c sets config->num_possible_pcs from
fwnode_phylink_pcs_count(). A short count means the later entries are
never scanned.

A count of 0 makes phylink_create() fail with "pcs_interfaces,
num_possible_pcs and fill_available_pcs must be set together" instead
of reporting the real parse error.

[ ... ]

> diff --git a/include/linux/pcs/pcs-provider.h b/include/linux/pcs/pcs-provider.h
> new file mode 100644
> index 0000000000000..6b03a635f73c0
> --- /dev/null
> +++ b/include/linux/pcs/pcs-provider.h
> @@ -0,0 +1,72 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +#ifndef __LINUX_PCS_PROVIDER_H
> +#define __LINUX_PCS_PROVIDER_H
> +
> +struct fwnode_pcs_provider;
> +

[ ... ]

> +struct phylink_pcs *fwnode_pcs_simple_xlate(struct fwnode_reference_args *pcsspec,
> +					    void *data);

[Severity: Low]
This header has no includes and forward-declares only struct
fwnode_pcs_provider. Its prototypes also use struct
fwnode_reference_args, struct fwnode_handle, struct device and struct
phylink_pcs.

If a provider includes it before linux/fwnode.h, linux/device.h and
linux/phylink.h, won't GCC warn that those structs are declared inside
the parameter list? It would then report conflicting types against the
real declarations.

pcs.h has !CONFIG_FWNODE_PCS stubs, but this header doesn't, and pcs.o
is only built for obj-$(CONFIG_FWNODE_PCS). Would a provider driver that
doesn't select FWNODE_PCS fail to link against
fwnode_pcs_add_provider(), devm_fwnode_pcs_add_provider() and
fwnode_pcs_simple_xlate()?

The users in this series only avoid both problems through their include
order and the select FWNODE_PCS in PCS_AIROHA.

[ ... ]

> diff --git a/include/linux/pcs/pcs.h b/include/linux/pcs/pcs.h
> new file mode 100644
> index 0000000000000..b3bb158002d5a
> --- /dev/null
> +++ b/include/linux/pcs/pcs.h
> @@ -0,0 +1,74 @@

[ ... ]

> +#else
> +static inline struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode,
> +						 unsigned int index)
> +{
> +	return ERR_PTR(-ENOENT);
> +}
> +
> +static inline unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode)
> +{
> +	return 0;
> +}
> +
> +static inline int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode,
> +					   struct phylink_pcs **available_pcs,
> +					   unsigned int num_pcs)
> +{
> +	return -EOPNOTSUPP;
> +}
> +#endif

[Severity: Low]
Should these stubs return the same error codes as the real
implementation?

The fwnode_pcs_get() stub returns -ENOENT. fwnode_phylink_pcs_parse()
treats that code as the end of the pcs-handle list, while the kernel-doc
gives -ENODEV for "no PCS available".

The fwnode_phylink_pcs_parse() stub returns -EOPNOTSUPP. The real
function returns -ENODEV when pcs-handle is absent:

	if (!fwnode_property_present(fwnode, "pcs-handle"))
		return -ENODEV;

With FWNODE_PCS=n, a caller that treats -ENODEV as "no PCS described"
would get -EOPNOTSUPP instead. The only in-tree consumer selects
FWNODE_PCS, so nothing is affected today.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 05/15] net: phylink: make phylink_validate state locked
  2026-11-29 23:05 ` [PATCH net-next v23 05/15] net: phylink: make phylink_validate state locked Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] This patch removes the last in-tree caller of the exported
  phy_get_rate_matching(), but leaves the export in place.

--

net: phylink: make phylink_validate state locked

This commit makes the phylink validation helpers run under
pl->state_mutex. It adds __phylink_validate(), __phylink_validate_mask()
and __phylink_validate_phy() for callers that already hold the mutex.
It also adds an internal __phy_get_rate_matching() helper in
phy-internal.h for callers that already hold phydev->lock.

> diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
> index 079c53516f424..a6d6a7c1a96dd 100644
> --- a/drivers/net/phy/phy.c
> +++ b/drivers/net/phy/phy.c

[ ... ]

> @@ -146,13 +147,11 @@ EXPORT_SYMBOL(phy_print_status);
>  int phy_get_rate_matching(struct phy_device *phydev,
>  			  phy_interface_t iface)
>  {
> -	int ret = RATE_MATCH_NONE;
> +	int ret;
>  
> -	if (phydev->drv->get_rate_matching) {
> -		mutex_lock(&phydev->lock);
> -		ret = phydev->drv->get_rate_matching(phydev, iface);
> -		mutex_unlock(&phydev->lock);
> -	}
> +	mutex_lock(&phydev->lock);
> +	ret = __phy_get_rate_matching(phydev, iface);
> +	mutex_unlock(&phydev->lock);
>  
>  	return ret;
>  }

[Severity: Low]
Does phy_get_rate_matching() still have any in-tree users after this
change?

phylink was its only caller. This patch moves both phylink call sites
to the new internal helper:

  phylink_validate_one():
      tmp_state.rate_matching = __phy_get_rate_matching(phy, interface);

  __phylink_validate_phy():
      state->rate_matching = __phy_get_rate_matching(phy, state->interface);

After this patch, grepping for phy_get_rate_matching finds only these:

  - the definition and kernel-doc in drivers/net/phy/phy.c
  - EXPORT_SYMBOL_GPL(phy_get_rate_matching)
  - the prototype in include/linux/phy.h
  - the two __phy_get_rate_matching() calls in phylink.c

This stays the same at the end of the series.

The same patch also changes the exported function's locking. It now
always takes phydev->lock, where before it only took the lock when
phydev->drv->get_rate_matching was set. The commit message does not
mention that the export is left with no callers.

Should phy_get_rate_matching(), its export and its prototype be removed
in this patch? If it is being kept on purpose, could the commit message
say why?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 06/15] net: phylink: add capabilities helpers and track modified advertising
  2026-11-29 23:05 ` [PATCH net-next v23 06/15] net: phylink: add capabilities helpers and track modified advertising Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 3 · Low: 2

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] net: phylink: unchecked validation failure in
  phylink_update_capabilities()
- [Medium] On SFP links, pl->advertising does not always hold "the full
  validated advertisement" that the commit message describes, so modes…
- [Medium] phylink_update_user_disabled_advertising() never clears a
  restriction for a mode that is outside the current pl->advertising,…
- [Medium] net: phylink: stale SFP caps after phylink_sfp_module_start()
  failure
- [Low] phylink_update_capabilities() falls back to
  phylink_parse_inband_capabilities() after an SFP module is removed,…
- [Low] net: phylink: phylink_set_fixed_link() leaves pl->advertising
  stale

--

net: phylink: add capabilities helpers and track modified advertising

This commit adds pl->advertising and pl->user_disabled_advertising masks
to struct phylink, along with helpers that recalculate the supported and
advertised link modes when a PCS is added or removed. It also factors the
fixed-link, in-band, PHY and SFP capability calculations into reusable
helpers. The user-disabled mask is applied during SFP interface selection.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 141da617b24bd..877a0a30ec222 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -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);
> +}

[Severity: Medium]
Can a restriction on a mode outside the current pl->advertising ever be
cleared here, even when the user explicitly requests that mode?

The result is D = (A & ~R) | (D_old & ~A). A mode that is in D_old and in
R but not in A stays disabled.

On optical SFP links, A only covers the current interface because
phylink_sfp_set_config() sets it. The request built in
phylink_ethtool_ksettings_set() is masked with pl->supported, which covers
every SFP interface.

Take two interfaces X and Y on a may_have_phy module where no PHY was
detected:

1. The link starts on X.
2. The user advertises only a Y mode. D gets the X speed modes and the
   interface switches to Y.
3. After a link down/up, phylink_sfp_module_start() calls
   phylink_sfp_config_optical(). That skips X, picks Y and sets A = A(Y).
4. The user advertises only an X mode m_X. D & ~A(Y) still contains m_X,
   and D also gains all the Y speed modes.
5. After another link down/up, phylink_choose_sfp_interface() finds every
   candidate empty after masking:

	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;

phylink_sfp_config_optical() then fails with "failed to select SFP
interface". sfp_module_start() fails too, and the SFP state machine moves
to SFP_S_FAIL with the link down.

Later in the series the same stale bits also reach
phylink_update_capabilities() on PCS add/remove. Its exit label sets
link_config.advertising = A & ~D, which drops the modes the user just
asked for.

> +
> +static void phylink_sfp_update_capabilities(struct phylink *pl,

[ ... ]

> +static void phylink_update_capabilities(struct phylink *pl,
> +					struct phylink_link_state *config)
> +{

[ ... ]

> +	/*
> +	 * 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)) {

[Severity: Medium]
Does the deferred optical path need the same cleanup as the error
path added here?

For a module with caps->may_have_phy set, phylink_sfp_module_insert()
caches pl->sfp_module_interfaces, pl->sfp_interfaces and
pl->sfp_support and returns 0. The optical configuration is left to
phylink_sfp_module_start(), which runs once sfp.c has probed for a PHY
and found none.

If phylink_sfp_config_optical() fails there, phylink_sfp_module_start()
only deletes the module port and returns the error. sfp_sm_main() then
moves to SFP_S_FAIL while the module stays SFP_MOD_PRESENT, so
phylink_sfp_module_remove() is not called until the module is pulled.
Nothing clears the cached masks on that path.

>From then on phylink_update_capabilities() sees a non-empty
pl->sfp_module_interfaces and takes the SFP branch whenever
link_config.interface is not PHY_INTERFACE_MODE_NA. pl->supported and
pl->advertising are rebuilt from the failed module's sfp_support. As a
result, ethtool reports, and the next major config uses, modes from a
module that phylink refused to configure.

For may_have_phy modules, the window between insert and
phylink_sfp_module_start() behaves the same way. In this commit the
branch is only reached through phylink_update_mac_pause_capabilities();
later in the series the PCS add/remove paths reach it as well.

Should phylink_sfp_module_start() clear the cached module capabilities
on failure as well, perhaps through a helper shared with the insert
path? And should the SFP branch act on a module that has not been
started yet?

[ ... ]

> +	if (pl->cfg_link_an_mode == MLO_AN_INBAND) {
> +		phylink_parse_inband_capabilities(pl, config);
> +		goto exit;
> +	}

[Severity: Low]
Does this branch handle links set up with phylink_set_fixed_link()?

phylink_set_fixed_link() derives link_config.advertising from the
MAC-validated pl->supported with phy_caps_lookup() and sets
cfg_link_an_mode to MLO_AN_FIXED. It does not touch pl->advertising,
which still holds the generic mask computed in phylink_create().

When phylink_update_capabilities() later runs on such a link,
phylink_parse_fixedlink_capabilities() rebuilds everything from
phylink_fill_fixedlink_supported(), which only covers 10baseT to
10000baseT. Take a 25G or 40G link configured this way (txgbe_aml.c
does this). phy_caps_lookup() returns NULL, pl->supported is reduced
to MII, Autoneg and the pause bits, and the advertisement has no speed
modes left. The -EINVAL is ignored, so the link silently loses its
speed modes.

Even for speeds in the BASE-T table, the result can differ from what
phylink_set_fixed_link() picked. That function used every mode at the
speed present in pl->supported.

In this commit only phylink_update_mac_pause_capabilities() reaches
this branch; later in the series the PCS add/remove paths do too.

Should phylink_set_fixed_link() record its result in pl->advertising
and keep the mask it chose? The fixed-link recalculation could then
reuse it rather than the firmware fixed-link table.

[Severity: Low]
After an SFP module is removed, should this branch still run with the
removed module's interface?

phylink_sfp_module_remove() clears pl->sfp_module_interfaces but leaves
pl->link_config.interface at the last SFP interface. On an in-band SFP
port the SFP branch above is skipped and this branch runs instead.

If the last module used PHY_INTERFACE_MODE_100BASEX, the parser reaches
its default case. 100BASEX is in phylink_sfp_interface_preference but not
in the in-band switch list:

	default:
		phylink_err(pl,
			    "incorrect link mode %s for in-band status\n",
			    phy_modes(config->interface));
		return -EINVAL;

By then pl->supported has already been reset to just MII, Autoneg, Pause
and Asym_Pause. The error is ignored here, and the exit label rebuilds
link_config.advertising from the removed module's stale pl->advertising.

In this commit the path can only be reached through
phylink_update_mac_pause_capabilities(). Later in the series it runs on
every PCS add/remove event while no module is inserted
(phylink_update_pcs(), phylink_remove_provider_pcs(),
phylink_pcs_finish_init()).

> +
> +	/* 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);
> +}

[Severity: High]
phylink_update_capabilities() returns void and drops every validation
result. What state is left behind when nothing validates?

In the generic path, pl->supported is filled and copied into
config->advertising before __phylink_validate() runs. If
config->interface is set but no longer in pl->supported_interfaces,
for example because the PCS providing it went away,
__phylink_validate() returns -EINVAL without masking anything.
pl->supported and pl->advertising stay all-ones, and the exit label
sets link_config.advertising to all-ones minus the user-disabled bits.
phylink_validate_mac_and_pcs() has similar early returns when
mac_select_pcs() or pcs_validate() fail.

The other helpers behave the same way:

- phylink_parse_fixedlink_capabilities() ignores the
  __phylink_validate() result after filling pl->supported with the
  fixed-link table.
- phylink_parse_inband_capabilities() has already overwritten
  pl->supported with the raw interface capabilities when it returns
  -EINVAL. The exit label then copies the previous pl->advertising
  back into link_config.advertising.
- phylink_phy_update_capabilities() returns early, leaving
  pl->supported, pl->advertising and link_config.advertising with the
  values from before the PCS change.

The unchecked fill and validate in phylink_create() predates this
patch. However, the helper now also runs on a configured link: through
phylink_update_mac_pause_capabilities() here, and on every PCS
add/remove later in the series. That is exactly when an interface is
likely to lose its PCS.

Relatedly, for an MLO_AN_INBAND link whose PCS attaches late,
phylink_parse_mode() still propagates the in-band validation failure
and phylink_create() fails. Is that intended, given the aim of the
series?

Could phylink_update_capabilities() return an error to its callers?
And on failure, should validation be done into temporaries and only
committed to pl->supported and pl->advertising on success, or should
the masks be cleared?

[ ... ]

> @@ -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;
>  

[Severity: Medium]
Is pl->advertising still the right reference here when this call also
switches the SFP interface?

phylink_sfp_select_interface() or phylink_sfp_select_interface_speed()
can move the link from, for example, 2500BASE-X to SGMII. Further down,
only link_config is updated:

	if (pl->link_config.interface != config.interface) {
		...
		pl->link_config.interface = config.interface;
		linkmode_copy(pl->link_config.advertising, config.advertising);
	}

pl->advertising keeps the previous interface's mask A(old), so the new
restrictions are computed as A(old) & ~requested. A mode of the new
interface that the user left out is not in A(old), so it is never added
to pl->user_disabled_advertising.

On the next recalculation those modes come back:

  phylink_sfp_module_start()
    phylink_sfp_config_optical()
      phylink_sfp_set_config()
        link_config.advertising = A(new) & ~user_disabled

Later in the series, phylink_update_capabilities() on PCS add/remove has
the same effect.

For example, run "ethtool -s ethX advertise 1000baseT_Full" on a module
that offers 2500BASE-X and SGMII. After a link down/up the port
advertises 10/100/1000baseT again.

A related point concerns the commit message:

    The advertising mask holds the full validated advertisement
    before applying the phylink user restrictions.

With a PHY attached, phylink_phy_update_capabilities() copies
pl->advertising from phy->advertising. That copy already includes
phylib's ethtool restrictions and any phy_speed_down() reduction. The
user-disabled mask is not used while a PHY is attached, so this part is
mostly a description mismatch. Could the description be adjusted to
match?

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 07/15] net: sfp: support deferred module insertion
  2026-11-29 23:05 ` [PATCH net-next v23 07/15] net: sfp: support deferred module insertion Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The new deferral only covers a PCS dependency that is missing
  when module_insert() runs.

--

net: sfp: support deferred module insertion

This lets an upstream device defer SFP module insertion. If its
module_insert() callback returns -EAGAIN, the module stays in
SFP_MOD_WAITDEV. A new sfp_upstream_retry_module() helper lets the
upstream retry the insertion once the missing dependency (e.g. a PCS
provider) has registered.

> diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
> index 915156743701b..e5ddd62318bf9 100644
> --- a/drivers/net/phy/sfp.c
> +++ b/drivers/net/phy/sfp.c

[ ... ]

> @@ -2830,6 +2836,8 @@ static void sfp_sm_module(struct sfp *sfp, unsigned int event)
>  		/* Report the module insertion to the upstream device */
>  		err = sfp_module_insert(sfp->sfp_bus, &sfp->id,
>  					sfp->quirk);
> +		if (err == -EAGAIN)
> +			break;
>  		if (err < 0) {
>  			sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
>  			break;

[ ... ]

> @@ -3091,6 +3099,14 @@ static void sfp_stop(struct sfp *sfp)
>  	sfp_sm_event(sfp, SFP_E_DEV_DOWN);
>  }
>
> +static void sfp_retry_module(struct sfp *sfp)
> +{
> +	mutex_lock(&sfp->sm_mutex);
> +	if (sfp->sm_mod_state == SFP_MOD_WAITDEV)
> +		__sfp_sm_event(sfp, SFP_E_UPSTREAM_RETRY);
> +	mutex_unlock(&sfp->sm_mutex);
> +}

[Severity: Medium]
Does this retry cover modules that have may_have_phy set?

sfp_retry_module() only acts while sm_mod_state is SFP_MOD_WAITDEV. The
only way to stay in that state is for sfp_module_insert() in
sfp_sm_module() to return -EAGAIN.

Copper modules have may_have_phy set. These are e1000_base_t modules, or
modules with the 10G/5G/2.5GBASE-T extended compliance codes that
sfp_module_parse_may_have_phy() checks for. For them,
phylink_sfp_module_insert() returns before any PCS-dependent
configuration:

drivers/net/phy/phylink.c:phylink_sfp_module_insert() {
    ...
	/* If this module may have a PHY connecting later, defer until later */
	if (pl->sfp_may_have_phy)
		return 0;
    ...
}

So the module moves to SFP_MOD_PRESENT, and the PCS dependency is only
checked later in sfp_sm_main().

If the PCS provider has not registered yet, both paths there seem to end
in SFP_S_FAIL.

When a PHY is found:

  sfp_sm_probe_for_phy()->sfp_add_phy()->phylink_sfp_connect_phy()
    ->phylink_sfp_config_phy()

This fails because the host interface (e.g. SGMII) is not yet in
pl->supported_interfaces.

When no PHY is found:

  sfp_module_start()->phylink_sfp_module_start()
    ->phylink_sfp_config_optical()

This returns -EINVAL with "no common interface modes".

Both failures lead here:

drivers/net/phy/sfp.c:sfp_sm_main() {
    ...
		} else if (ret) {
			sfp_sm_next(sfp, SFP_S_FAIL, 0);
			break;
		}
		if (sfp_module_start(sfp->sfp_bus)) {
			sfp_sm_next(sfp, SFP_S_FAIL, 0);
			break;
		}
    ...
}

Later, when the provider registers, this chain runs:

  phylink_update_pcs()->phylink_sfp_retry_module()
    ->sfp_upstream_retry_module()->sfp_retry_module()

sfp_retry_module() then sees sm_mod_state == SFP_MOD_PRESENT and does
nothing.

Would the link then stay down until the module is re-plugged, or the
interface is cycled with ifdown/ifup?

The later patch in this series, "net: phylink: support late PCS provider
attach", only returns -EAGAIN when !caps->may_have_phy. So the rest of
the series doesn't seem to cover this case either.

The commit message starts with a general description ("when the
upstream device needs a dependency that is not yet available"). Only the
last sentence narrows it to optical SFPs. If leaving out copper and
may_have_phy modules is intended, could the commit message say so?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 08/15] net: phylink: support PCS provider release
  2026-11-29 23:05 ` [PATCH net-next v23 08/15] net: phylink: support PCS provider release Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 6 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The new SFP check in phylink_major_config()
  (drivers/net/phy/phylink.c, starting at line 1533) runs for every…
- [Medium] The new fwnode_pcs_del_provider() kernel-doc in
  include/linux/pcs/pcs-provider.h says unconditionally that "Phylink…
- [Medium] If a PCS provider is removed while phylink_create() is
  running, the supported_interfaces/capability rebuild is deferred.
- [Medium] net: phylink: ethtool ksettings_set fails to recover after PCS
  autoneg failure
- [Medium] net: phylink: mishandled force_major_config with
  phylink_disable_state
- [Medium] net: phylink: broken capability recalculation in
  phylink_update_capabilities()
- [Low] In the phylink_create() error path, the comment "previous PCS
  notifier might have queued a resolve, cancel it" is followed by…

--

net: phylink: support PCS provider release

This adds a blocking notifier chain so that phylink instances using the
PCS list are notified when a fwnode PCS provider is removed. Each such
instance then detaches that provider's PCS synchronously. The PCS list is
refreshed via fill_available_pcs() and supported interfaces and
capabilities are rebuilt. If the active PCS goes away, the link is torn
down and reconfigured. SFP capability handling and major config are also
updated to cope with interfaces becoming unavailable.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 877a0a30ec222..6dde0eae0e73a 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -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;

[Severity: Medium]
Can the link recover from this early return when the user fixes the
autoneg setting with ethtool?

Take an optical SFP whose active PCS is removed. phylink_del_pcs()
clears pl->pcs and sets force_major_config. On the next resolve,
phylink_major_config() picks a replacement PCS for the same interface
from pl->pcs_list. Suppose the replacement only supports the other
in-band mode (LINK_INBAND_DISABLE vs LINK_INBAND_ENABLE). This check
then sets major_config_failed and returns before pl->pcs is assigned,
so pl->pcs stays NULL.

The obvious recovery is to change autoneg with ethtool to match the
replacement PCS. phylink_ethtool_ksettings_set() validates the new
advertisement against pl->pcs_list and accepts it. The interface is
unchanged, so it takes this branch instead of running a major config:

    } else if (!linkmode_equal(pl->link_config.advertising,
                               config.advertising)) {
        linkmode_copy(pl->link_config.advertising, config.advertising);
        phylink_change_inband_advert(pl);
    }

phylink_change_inband_advert() works on pl->pcs, which is still NULL,
so phylink_pcs_config() does nothing. The replacement PCS is never
attached, configured or enabled, and major_config_failed is never
cleared. phylink_resolve() keeps the link down while
major_config_failed is set.

Nothing else retries the major config while the interface stays the
same. The link stays down until the interface changes or the module
is reinserted.

Should phylink_ethtool_ksettings_set() force a major config when
major_config_failed is set, or when pl->pcs is NULL for an interface in
pcs_interfaces?

[Severity: Medium]
Does this check also fire after a copper module that may have a PHY has
been inserted, but before its PHY has attached?

The check runs for every phylink instance with an SFP bus. That includes
existing .mac_select_pcs drivers, not only PCS list users.

phylink_sfp_module_insert() copies caps->interfaces into both masks. It
then returns early for may-have-PHY modules without updating
link_config.interface:

    phy_interface_copy(pl->sfp_module_interfaces, caps->interfaces);
    phy_interface_copy(pl->sfp_interfaces, caps->interfaces);
    ...
    if (pl->sfp_may_have_phy)
        return 0;

The SFP state machine reports insertion from SFP_MOD_WAITDEV once
sm_dev_state >= SFP_DEV_DOWN. The module PHY is only probed later, once
the SFP device is up. phylink_start() calls phylink_mac_initial_config()
before sfp_upstream_start(). So the initial major config runs in this
window, and link_config.interface is still the DT phy-mode.

For example, take phy-mode = "10gbase-r" on an SFP+ cage with a
1000BASE-T module whose caps only contain SGMII. 10gbase-r is not in
sfp_interfaces, so this logs "incompatible SFP configuration" and returns
with major_config_failed set. Before this patch the configuration was
applied.

The link would normally recover once the module PHY attaches. However,
the early return also skips this step:

    if (pl->pcs_state == PCS_STATE_STARTING || pcs_changed)
        phylink_pcs_enable(pl->pcs);

Could the PCS end up never being re-enabled? Consider a module swapped
while the interface is down:

- phylink_stop() disables the PCS.
- The next phylink_start() hits this check and returns early.
- The later PHY attach selects the same pl->pcs, so pcs_changed is false.

The commit message only describes this check as part of the
replacement-PCS flow. Should it be limited to that case? The code looks
unchanged later in the series.

[ ... ]

> @@ -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;
> +	}

[Severity: Medium]
Can deferring the rebuild here leave an optical SFP stuck on an interface
that has no PCS?

Suppose a provider is removed during phylink_create(). The call chain
pcs_provider_notify() -> phylink_update_pcs(pl, true) -> phylink_del_pcs()
has already taken the PCS off pl->pcs_list. But
phylink_parse_supported_interfaces() is skipped, so
pl->supported_interfaces still includes the removed PCS's modes.

phylink_create() then calls phylink_register_sfp(). A module waiting in
SFP_MOD_WAITDEV is inserted synchronously:

phylink_register_sfp()
  sfp_bus_add_upstream()
    ... SFP_E_DEV_ATTACH
      phylink_sfp_module_insert()
        phylink_sfp_config_optical()

phylink_sfp_config_optical() computes sfp_interfaces from the stale
supported_interfaces.

When no listed PCS supports an interface, phylink_validate_mac_and_pcs()
leaves pcs NULL and still passes on MAC caps alone. So
phylink_choose_sfp_interface() can pick the interface whose PCS was just
removed. phylink_sfp_set_config() then stores it in
pl->link_config.interface.

phylink_pcs_finish_init() later rebuilds supported_interfaces and calls
phylink_update_capabilities(). That function zeroes pl->advertising and
then reaches:

    linkmode_copy(pl->supported, supported);
    if (!test_bit(config->interface, pl->sfp_interfaces))
        goto exit;

No alternate interface from sfp_interfaces is selected.

A later commit in the series ("net: phylink: support late PCS provider
attach") adds a phylink_sfp_retry_module() call to
phylink_pcs_finish_init(). That doesn't seem to help.
SFP_E_UPSTREAM_RETRY is only handled in SFP_MOD_WAITDEV, and a module
that was inserted successfully has already left that state.

On phylink_start(), phylink_major_config() then logs "couldn't find a
PCS" and sets major_config_failed. phylink_resolve() keeps the link down,
even if another interface of the module has a usable PCS.

Wouldn't the same thing happen at runtime when the provider of an optical
SFP's active PCS is removed? link_config.interface stays on the
now-unusable interface, so every forced major config fails.

[ ... ]

> @@ -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);

[Severity: Low]
This isn't a bug, but is this comment accurate?

PHYLINK_DISABLE_STOPPED is set before the notifier is registered and is
only cleared by phylink_start(). phylink_run_resolve() does nothing while
phylink_disable_state is non-zero:

    if (!pl->phylink_disable_state)
        queue_work(system_power_efficient_wq, &pl->resolve);

While pcs_state is INITIALIZING or INIT_PENDING, phylink_pcs_changed()
returns false. So neither phylink_update_pcs() nor
phylink_remove_provider_pcs() asks for a resolve on these error paths.

In-flight notifiers are already excluded by the
unregister_fwnode_pcs_notifier() call in phylink_detach_pcss(). That call
takes the chain rwsem for write.

The cancel_work_sync() looks like dead code here. This is still the same
at the end of the series.

[ ... ]

> diff --git a/include/linux/pcs/pcs-provider.h b/include/linux/pcs/pcs-provider.h
> index 6b03a635f73c0..2505c280946f0 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);

[Severity: Medium]
Does this guarantee hold for every phylink consumer?

phylink_create() only registers the notifier for PCS list users:

    if (has_pcs_interfaces) {
        ...
        register_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);

It also rejects .mac_select_pcs combined with pcs_interfaces.

fwnode_pcs_get() is exported in include/linux/pcs/pcs.h, and its
documentation doesn't restrict how it is used. A MAC driver that gets a
PCS with fwnode_pcs_get() and returns it from .mac_select_pcs() would
never be notified.

phylink_major_config() stores that PCS in pl->pcs and sets pcs->phylink.
This comment allows the provider to free the PCS once
fwnode_pcs_del_provider() returns. Would phylink then keep using the
freed PCS?

There is no in-tree user of that combination today. airoha_eth uses
fwnode_phylink_pcs_parse() with fill_available_pcs.

Should the documentation say that the synchronous detach only applies to
fill_available_pcs users? This looks unchanged later in the series.

[ ... ]

> @@ -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;

[Severity: Medium]
Does the capability rebuild after a PCS removal actually drop the
removed PCS's interfaces?

phylink_parse_supported_interfaces() starts from a copy of
pl->config->supported_interfaces and only ORs in PCS modes. Some MAC
drivers that use the PCS list appear to list the PCS modes in
config->supported_interfaces as well. airoha_setup_phylink() does, and
so does the example in Documentation/networking/pcs.rst. In that case
the pcs_interfaces modes stay in pl->supported_interfaces after their
PCS leaves pl->pcs_list.

phylink_validate_mac_and_pcs() doesn't catch it either. For an
interface in pcs_interfaces with no matching PCS in pl->pcs_list, pcs
stays NULL and validation passes on MAC capabilities alone. Unlike
phylink_major_config(), it doesn't return -EINVAL here.

So __phylink_validate_mask() and __phylink_validate() in this SFP
branch still succeed after the PCS is gone. The new handling for
unusable interfaces is never reached. This would also stop the
late-PCS -EAGAIN check, which a later patch in the series adds to
phylink_sfp_module_insert(), from firing.

A MAC driver might instead leave the PCS modes out of
config->supported_interfaces. Then the interface does drop out of
pl->supported_interfaces. __phylink_validate() returns -EINVAL from
its test_bit() check without touching the masks, and only the SFP
branch handles that:

- In the generic fallback, linkmode_fill() has just filled
  pl->supported and config->advertising. The return value is ignored,
  so pl->supported, pl->advertising and link_config.advertising keep
  every link mode bit set.
- phylink_parse_inband_capabilities() has already filled pl->supported
  with unvalidated modes when it returns -EINVAL. pl->advertising
  keeps the modes from the removed PCS.
- phylink_parse_fixedlink_capabilities() ignores the failure after
  phylink_fill_fixedlink_supported().
- phylink_phy_update_capabilities() returns early and keeps the old
  supported and advertising masks.

These masks are what phylink_ethtool_ksettings_get() reports. They are
also used to validate phylink_ethtool_ksettings_set() and to compute
user_disabled_advertising.

Should phylink_parse_supported_interfaces() remove pcs_interfaces from
the MAC mask before adding back the modes a listed PCS supports? And
should the remaining branches clear the masks when validation fails?

[Severity: Medium]
Is it safe to leave force_major_config pending once RTNL is dropped?

Before this patch, force_major_config was only set by
phylink_replay_link_end(). That runs under RTNL, right after clearing
PHYLINK_DISABLE_REPLAY, and flushes the resolve work before returning.
Here phylink_update_pcs() and phylink_remove_provider_pcs() set it and
drop RTNL. Only after that does pcs_provider_notify() call
phylink_run_resolve().

phylink_resolve() acts on the flag without looking at
phylink_disable_state:

    if ((mac_config &&
         link_state.interface != pl->link_config.interface) ||
        pl->force_major_config) {
        ...
        phylink_major_config(pl, false, &link_state);

Suppose phylink_stop() runs in that window, or before the queued work
executes:

- It stops the SFP upstream and the PHY.
- It calls timer_delete_sync(&pl->link_poll).
- phylink_run_resolve_and_disable() sets PHYLINK_DISABLE_STOPPED and
  flushes the work.

That resolve still sees force_major_config. phylink_major_config()
then reconfigures the MAC and PCS on a stopped interface. With
MLO_AN_INBAND and a polling replacement PCS, phylink_pcs_poll_start()
re-arms pl->link_poll after phylink_stop() deleted it.
phylink_destroy() doesn't delete the timer, so pl can be freed while
the timer is pending. phylink_fixed_poll() would then run on freed
memory.

phylink_del_pcs() has the opposite problem. It only sets
force_major_config when phylink_disable_state is zero, while
phylink_pcs_changed() only tests PHYLINK_DISABLE_STOPPED. Suppose the
active PCS is removed while PHYLINK_DISABLE_MAC_WOL is set, or while
PHYLINK_DISABLE_LINK is set together with another bit. pl->pcs
becomes NULL without forcing a reconfiguration:

- phylink_sfp_link_up() only handles DISABLE_LINK as the only bit set.
- phylink_pcs_changed() only sets the flag again if force_major_config
  or major_config_failed was already set.

Once the bit clears, a resolve with an unchanged interface never runs
a major config. The replacement PCS is never selected.

Finally, phylink_pcs_changed() and phylink_sfp_link_up() set both
force_major_config and link_failed. The next resolve pass takes the
link_failed branch. The forced major config therefore runs with a
link_state based on pl->link_config, not on pl->phy_state or
phylink_get_fixed_state(). force_major_config is cleared on that pass,
so the retrigger pass no longer sees it. Is that intended?

Would it work to set the flag whenever PHYLINK_DISABLE_STOPPED is
clear, and have phylink_resolve() only honour it once
phylink_disable_state is zero and link_failed is clear?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 09/15] net: phylink: support late PCS provider attach
  2026-11-29 23:05 ` [PATCH net-next v23 09/15] net: phylink: support late PCS provider attach Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 2 · Medium: 3 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] net: phylink: phylink_parse_supported_interfaces() keeps
  unavailable PCS modes
- [High] net: phylink: phylink_create() fails for in-band links with late
  PCS
- [Medium] The late-PCS SFP recovery added by this patch only covers
  modules with caps->may_have_phy == false.
- [Medium] phylink_sfp_module_insert()
  (drivers/net/phy/phylink.c:4820-4826) now returns -EAGAIN for a non-…
- [Medium] net: phylink: stale sfp_cage_port capabilities after late PCS
  attach

--

net: phylink: support late PCS provider attach

This adds a FWNODE_PCS_PROVIDER_ADD notification. With it, a phylink
instance can pick up a PCS provider that registers after phylink_create(),
and it then refreshes supported_interfaces and link capabilities. Optical
SFP insertion is deferred with -EAGAIN when the module's only usable
interfaces need a PCS, and the insertion is retried once a PCS is added.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 6dde0eae0e73a..3471ba8d5ba6f 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -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;

[Severity: High]
Can this condition ever evaluate to true?

phylink_parse_supported_interfaces() starts from a copy of
pl->config->supported_interfaces. It then ORs in pcs_interfaces modes
for each PCS on pl->pcs_list, and it never removes anything.

Documentation/networking/pcs.rst describes the two masks this way:
config->supported_interfaces lists every mode the MAC supports, and
config->pcs_interfaces is the subset of those that need a PCS.
airoha_setup_phylink() follows that and copies supported_interfaces
into pcs_interfaces.

If pcs_interfaces is a subset of config->supported_interfaces, every
PCS-required mode is always set in pl->supported_interfaces, whether
or not pl->pcs_list is empty. A module interface that intersects
pl->config->pcs_interfaces then also intersects
pl->supported_interfaces, so the -EAGAIN branch can never be taken.
An optical module inserted before its PCS provider probes is
configured immediately instead of being deferred.

Validation does not catch this either. phylink_validate_mac_and_pcs()
searches pl->pcs_list when the interface is in pcs_interfaces. If no
PCS matches, it leaves pcs NULL and validates against the MAC
capabilities alone. phylink_sfp_config_optical() can therefore select
and configure a PCS-required interface with no PCS attached.

The same applies when the attached PCS supports only some of the
modes. For example, the airoha USB PCS tops out at 2500BASE-X, while
the MAC sets 10GBASE-R and USXGMII in both masks.

The comment in phylink_parse_supported_interfaces() suggests the
intent is to advertise PCS-only modes only once a PCS can serve them.
Which contract is intended here? If pcs_interfaces may overlap
config->supported_interfaces, should that overlap be masked out of the
initial copy? And should phylink_validate_mac_and_pcs() fail when no
PCS on pl->pcs_list accepts a pcs_interfaces mode?

[Severity: Medium]
Does late PCS attach also cover modules that have caps->may_have_phy set?

Copper modules (1000BASE-T/SGMII, 10GBASE-T) and quirked fibre modules
skip this deferral and return 0. Their interface setup happens later in
SFP_S_INIT_PHY, after the netdev is up.

Suppose the PCS-only host mode (SGMII, for example) is still missing from
pl->supported_interfaces at that point. One of two things happens. In the
first case, phylink_sfp_connect_phy() takes a snapshot:

    phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
                      pl->supported_interfaces);

and phylink_sfp_config_phy() then fails. In the second case, no PHY is
found, so phylink_sfp_module_start() falls through to:

    ret = phylink_sfp_config_optical(pl);
    if (ret)
        goto del_mod_port;

which fails with "unsupported SFP module: no common interface modes".
Either way, sfp_sm_main() moves the socket to SFP_S_FAIL.

When the PCS provider registers later, the path is
phylink_update_pcs()->phylink_sfp_retry_module()->sfp_retry_module(),
and sfp_retry_module() only acts in WAITDEV:

    if (sfp->sm_mod_state == SFP_MOD_WAITDEV)
        __sfp_sm_event(sfp, SFP_E_UPSTREAM_RETRY);

These modules are already past WAITDEV, so nothing retries them. The link
stays down until ifdown/ifup or a re-plug.

These modules behave the same as before the patch, and the commit message
limits the deferral to optical modules. Is this gap intended, given that
the goal is late PCS provider attach?

[Severity: Medium]
What happens to the module if a suitable PCS never shows up?

This check only uses the static config->pcs_interfaces mask. It does not
check that a provider exists, or that any PCS supports the module's
interface. On -EAGAIN, sfp_sm_module() leaves the module in
SFP_MOD_WAITDEV:

    err = sfp_module_insert(sfp->sfp_bus, &sfp->id,
                            sfp->quirk);
    if (err == -EAGAIN)
        break;

WAITDEV is entered with sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0), so no
timer is armed. A retry can come from only two places. One is
phylink_update_pcs():

    if (rescan && added && ret > 0)
        phylink_sfp_retry_module(pl);

The other is phylink_pcs_finish_init() when an update is pending. Both
need a PCS to be added to this phylink instance.

A suitable PCS may never arrive. The PCS driver may not be built, may fail
to probe, or may be missing from DT. A PCS may also attach without
supporting the module's interface, and then every retry returns -EAGAIN
again.

In those cases the module stays in WAITDEV indefinitely, TX_DISABLE stays
asserted and high-power mode is never enabled. Nothing is logged.

Before this patch the same module failed in phylink_sfp_config_optical()
with "unsupported SFP module: no common interface modes" and moved to
SFP_MOD_ERROR.

This branch can only trigger when the MAC leaves PCS-required modes out of
config->supported_interfaces. A MAC such as airoha copies
supported_interfaces into pcs_interfaces, so it cannot reach it.

Could a message be logged when insertion is deferred here? Alternatively,
could the old error path be kept for the case where no PCS can provide the
interface?

[ ... ]

> @@ -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();
>  	}
>  

[Severity: Medium]
Is the SFP cage port refreshed when a late PCS extends
pl->supported_interfaces?

phylink_register_sfp() runs inside phylink_create() and calls
phylink_create_sfp_cage_port(). That function sets the cage port's
masks once, from the supported_interfaces that exist at that point:

    phy_interface_and(port->interfaces, pl->supported_interfaces,
                      phylink_sfp_interfaces);
    phy_port_update_supported(port);

This runs before phylink_pcs_finish_init() and before any
FWNODE_PCS_PROVIDER_ADD notification can be handled.

When the PCS appears later, phylink_update_pcs() and
phylink_pcs_finish_init() rebuild pl->supported_interfaces with
phylink_parse_supported_interfaces() and then call
phylink_sfp_retry_module(). The interfaces and supported masks of
pl->sfp_cage_port are never recomputed, so they keep the pre-PCS
values.

The module deferred by the new -EAGAIN check is, by construction, one
whose caps->interfaces do not intersect the old
pl->supported_interfaces. They therefore do not intersect the cage
port's interfaces either. When the retried module reaches
phylink_sfp_module_start(), phylink_add_sfp_mod_port() does:

    phy_interface_and(interfaces, pl->sfp_cage_port->interfaces,
                      caps->interfaces);
    phy_caps_linkmode_filter_ifaces(port->supported, caps->link_modes,
                                    interfaces);

The result is an empty interfaces mask, and pl->mod_port->supported
ends up with no link modes. The cage port also never gains the
PCS-backed interfaces and link modes. The phy_port topology that
userspace sees then does not match the link that is actually running.

Should the cage port masks be recomputed wherever
phylink_parse_supported_interfaces() runs after the SFP bus has been
registered? Or should phylink_add_sfp_mod_port() derive its mask from
the current pl->supported_interfaces instead?

[ ... ]

> @@ -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);
>  	}

[Severity: High]
What happens on an in-band link whose initial interface needs a PCS
that has not registered yet?

Consider a MAC that leaves the PCS-required mode out of
config->supported_interfaces, which is the case late attach is meant
for. phylink_create() calls phylink_update_pcs(pl, false) while
pl->pcs_list is still empty. As a result, pl->supported_interfaces
does not contain pl->link_config.interface.

phylink_create() then calls phylink_parse_mode(). For MLO_AN_INBAND
(managed = "in-band-status" or config->default_an_inband, the usual
setup for SFP cages), this reaches phylink_parse_inband_capabilities().
There, __phylink_validate() returns early:

    if (!test_bit(state->interface, interfaces))
        return -EINVAL;

phylink_parse_mode() propagates the error, and phylink_create() jumps
to unregister_pcs_notify. This happens before phylink_register_sfp(),
before phylink_pcs_finish_init(), and before any
FWNODE_PCS_PROVIDER_ADD can be handled. The INIT_PENDING handling
added here therefore never gets to run. If the provider probes after
the MAC, or concurrently with phylink_create(), the MAC probe simply
fails.

The same early return also affects the generic branch of
phylink_update_capabilities() during the initial phylink_update_pcs().
That branch fills pl->supported with linkmode_fill() and copies it
into config->advertising. __phylink_validate() then bails out before
masking anything, and pl->advertising is saved as all ones.

Should phylink_parse_mode() tolerate an interface that is in
config->pcs_interfaces but not yet backed by a PCS, and defer the
in-band capability parsing until a PCS attaches? Or is late PCS attach
not meant to support in-band links? If so, could the commit message
say that?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 10/15] net: Document PCS subsystem
  2026-11-29 23:05 ` [PATCH net-next v23 10/15] net: Document PCS subsystem Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The consumer example in Documentation/networking/pcs.rst
  (mac_setup_phylink, lines ~137-147) sets config->num_possible_pcs =…
- [Medium] pcs.rst (lines 72-73) says ``supported_interfaces`` "describes
  all interface modes supported by the MAC, whereas ``pcs_interfaces``…
- [Low] pcs.rst (lines 68-70) says "A PCS is considered eligible when the
  requested interface mode is present in both ``pcs_interfaces`` in…
- [Low] The 'Late PCS registration handling' section of pcs.rst
  misdescribes the implementation in several places, and some cross-…
- [Low] pcs.rst line 165 says "Ownership of PCS is held by phylink." Read
  as a lifetime statement, this contradicts the implementation and the…

--

net: Document PCS subsystem

This commit adds Documentation/networking/pcs.rst. It documents the new
PCS subsystem, the fwnode producer/consumer API and how phylink handles
late PCS registration. It also updates the sfp-phylink migration guide to
use num_possible_pcs and fill_available_pcs instead of mac_select_pcs.

> diff --git a/Documentation/networking/pcs.rst b/Documentation/networking/pcs.rst
> new file mode 100644
> index 0000000000000..03b7de73a1065
> --- /dev/null
> +++ b/Documentation/networking/pcs.rst
> @@ -0,0 +1,257 @@

[ ... ]

> +A PCS is considered eligible when the requested interface mode is present
> +in both ``pcs_interfaces`` in :c:struct:`phylink_config` struct and
> +``supported_interfaces`` in :c:struct:`phylink_pcs` struct.

[Severity: Low]
Is this eligibility rule complete? phylink_validate_pcs_interface() in
drivers/net/phy/phylink.c treats an empty PCS mask as a wildcard:

    if (phy_interface_empty(pcs->supported_interfaces))
        return 0;

phylink_parse_supported_interfaces() also skips the AND in that case:

    if (!phy_interface_empty(pcs->supported_interfaces))
        phy_interface_and(pcs_interfaces, pcs_interfaces,
                          pcs->supported_interfaces);

So a provider that leaves supported_interfaces empty gets selected for every
mode in pcs_interfaces, not for none of them.

Could this paragraph, and maybe the struct phylink_pcs kernel-doc, mention
the empty-mask behaviour?

> +``supported_interfaces`` describes all interface modes supported by the MAC,
> +whereas ``pcs_interfaces`` identifies the subset that require PCS selection.

[Severity: Medium]
Does this match how phylink builds pl->supported_interfaces?

The previous paragraph names supported_interfaces in struct phylink_pcs. The
kernel-doc in include/linux/phylink.h describes that field as the modes
"supported by this PCS", not by the MAC.

The model also seems to be the opposite of what the code expects.
phylink_parse_supported_interfaces() assumes PCS-only modes are absent from
config->supported_interfaces:

    phy_interface_copy(pl->supported_interfaces,
                       pl->config->supported_interfaces);

    /*
     * Only add PCS-supported modes that phylink will select a PCS for.
     * Otherwise, phylink may accept a PCS-only mode and configure it without
     * a PCS.
     */

phylink_sfp_module_insert() only defers when the module's interfaces miss
pl->supported_interfaces:

    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;

This paragraph makes pcs_interfaces a subset of config->supported_interfaces,
and the mac_setup_phylink() example below sets USXGMII in both masks. In
that case USXGMII is always in pl->supported_interfaces.

Wouldn't that mean three things?

- The -EAGAIN deferral and the sfp_upstream_retry_module() retry described
  in the late registration section can never trigger.
- phylink_validate_mac_and_pcs() accepts USXGMII without a PCS.
- phylink_major_config() then fails with "couldn't find a PCS".

Step 13 of sfp-phylink.rst still tells readers to fill supported_interfaces
with what "your MAC and all PCS associated with this MAC are capable of",
which points the same way.

[ ... ]

> +      // Parse possible PCS and fill num_possible_pcs
> +      config->num_possible_pcs = fwnode_phylink_pcs_count(dev_fwnode(&netdev->dev));
> +      config->fill_available_pcs = mac_fill_available_pcs;
> +
> +      __set_bit(PHY_INTERFACE_MODE_INTERNAL, config->supported_interfaces);
> +      __set_bit(PHY_INTERFACE_MODE_SGMII, config->supported_interfaces);
> +      __set_bit(PHY_INTERFACE_MODE_1000BASEX, config->supported_interfaces);
> +      __set_bit(PHY_INTERFACE_MODE_USXGMII, config->supported_interfaces);
> +
> +      // PCS required only for USXGMII
> +      __set_bit(PHY_INTERFACE_MODE_USXGMII, config->pcs_interfaces);
> +
> +      phylink = phylink_create(config, //...

[Severity: Medium]
What happens with this example when fwnode_phylink_pcs_count() returns 0?
That happens when the node has no pcs-handle. It also always happens with
CONFIG_FWNODE_PCS=n, where the stub is:

    static inline unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode)
    {
        return 0;
    }

fill_available_pcs and pcs_interfaces are still set unconditionally, so
phylink_create() rejects the configuration:

    if (has_pcs_interfaces != has_pcs_callback ||
        has_pcs_interfaces != has_pcs_count) {
        dev_err(config->dev,
                "phylink: error: pcs_interfaces, num_possible_pcs and fill_available_pcs must be set together\n");
        return ERR_PTR(-EINVAL);
    }

A driver that copies this example would then fail to probe, even for SGMII,
1000BASE-X and INTERNAL, which the example says need no PCS. PHYLINK does
not select FWNODE_PCS.

Should the example set the PCS fields only when the count is non-zero? Should
the text say that all three fields must be left unset when no PCS is
described?

The example also takes the fwnode from &netdev->dev. It doesn't say that the
netdev needs an fwnode assigned, so a count of 0 is more likely.

Step 12 in sfp-phylink.rst only asks for num_possible_pcs and
fill_available_pcs. Could it also mention pcs_interfaces? Following that
step alone hits the same -EINVAL.

[ ... ]

> +The ``fill_available_pcs`` callback is called on :c:func:`phylink_create`
> +and on provider notification and can be called multiple times.
> +It's used to compose the available PCS list at the time of calling and
> +will be called again when a PCS provider is released or a new one is attached.
> +Ownership of PCS is held by phylink.

[Severity: Low]
Is the last sentence accurate? The fwnode_pcs_add_provider() kernel-doc in
include/linux/pcs/pcs-provider.h, which this page renders in its API
Reference, says the opposite:

    * A PCS object belongs to one provider and must remain valid until that
    * provider is removed.

phylink never allocates, refcounts or frees struct phylink_pcs.
phylink_add_pcs() only links the PCS into pl->pcs_list and sets
pcs->phylink. fwnode_pcs_free_provider() only unlinks provider_list.

If the intent is that phylink tracks which instance a PCS is attached to,
could the sentence say that? As written, a provider author could free the PCS
before fwnode_pcs_del_provider() runs, which would leave a dangling pointer
in pl->pcs_list.

[ ... ]

> +Late PCS registration handling
> +------------------------------
> +
> +It's possible that a PCS becomes available after the MAC finished probing.
> +Contrary to the usual producer/consumer implementation, when a PCS is not
> +registered and can't be found, the fwnode parser helper returns ``-ENODEV``
> +instead of ``-EPROBE_DEFER``.

[ ... ]

> +The PCS fwnode implementation provides a notifier to which each phylink
> +instance with a non-empty ``pcs_interfaces`` in :c:type:`phylink_config`
> +registers. When a new PCS provider is registered, the notifier is called
> +triggering the :c:func:`pcs_provider_notify` function.
> +
> +On receiving a PCS provider event, :c:func:`pcs_provider_notify` will
> +reprase the available PCS by calling the ``fill_available_pcs`` callback
> +and update the internal PCS list for the phylink instance with both the
> +removed and the newly available PCS and a phylink major config is forced.

[ ... ]

> +In an SFP scenario, with no currently usable interface, phylink defers module
> +insertion with ``-EAGAIN`` if the module supports an interface listed in
> +``pcs_interfaces`` and the SFP socket waits with its transmitter disabled.
> +After phylink adds a PCS to its list and refreshes its interface mask, it
> +calls ``sfp_upstream_retry_module()`` and if there is usable interface,
> +the SFP the module insertion is retried.

[Severity: Low]
A few statements in this section don't seem to match the implementation.

fwnode_phylink_pcs_parse() doesn't return -ENODEV for an unregistered PCS.
It skips those references and returns the number it found. It returns
-ENODEV only when pcs-handle is absent, and the !CONFIG_FWNODE_PCS stub
returns -EOPNOTSUPP. Was fwnode_pcs_get() the helper meant here?

A major config isn't forced on every provider event. phylink_pcs_changed()
only forces one when a major config was already pending or had failed:

    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;
    }

During init it only marks PCS_STATE_INIT_PENDING, so adding a PCS to a
working link doesn't trigger a reconfiguration.

The SFP deferral in phylink_sfp_module_insert() also requires
!caps->may_have_phy, so modules that may carry a PHY are never deferred.
Should that condition be mentioned here?

Some cross-references won't resolve:

- pcs_provider_notify() is a static function in phylink.c, and no kernel-doc
  directive pulls it in.
- In step 12 of sfp-phylink.rst, num_possible_pcs and fill_available_pcs are
  members of struct phylink_config, so the :c:var: and :c:func: roles won't
  resolve them.

There are also some typos:

- "reprase" should be "reparse".
- "the SFP the module insertion is retried" has a doubled "the".
- There are doubled spaces in "helper  function" and "for  that".

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 12/15] net: phylink: add .pcs_link_down PCS OP
  2026-11-29 23:05 ` [PATCH net-next v23 12/15] net: phylink: add .pcs_link_down PCS OP Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The comment in rtl8365mb_phylink_mac_link_down()
  (drivers/net/dsa/realtek/rtl8365mb_main.c, lines 1839-1843) is now…
- [Low] The new pcs_link_down() kernel-doc
  (include/linux/phylink.h:698-706) copies the mac_link_down() contract…

--

net: phylink: add .pcs_link_down PCS OP

This commit adds an optional .pcs_link_down operation to struct
phylink_pcs_ops. phylink_link_down() calls it right after
.mac_link_down, so a PCS driver can tear down the link between the MAC
and the PCS.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 3471ba8d5ba6f..b57a7de058b8f 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -1889,6 +1895,9 @@ static void phylink_link_down(struct phylink *pl)
>  
>  	pl->mac_ops->mac_link_down(pl->config, pl->act_link_an_mode,
>  				   pl->cur_interface);
> +
> +	phylink_pcs_link_down(pl->pcs);
> +
>  	phylink_info(pl, "Link is Down\n");
>  }

[Severity: Low]
With this hook added, the comment in rtl8365mb_phylink_mac_link_down()
in drivers/net/dsa/realtek/rtl8365mb_main.c is no longer accurate:

	/* phylink has no pcs_link_down callback, so on the SerDes path only the
	 * MAC external interface force is reset here. Clearing the MAC force is
	 * enough to bring the link down; the SerDes keeps presenting its last
	 * forced state until the next pcs_link_up() reprograms it.
	 */

rtl8365mb_pcs_ops does not set .pcs_link_down, so runtime behaviour does
not change. The comment still says phylink lacks this callback, and it
uses that to explain the stale SerDes state. The comment is unchanged at
the end of the series.

Should this comment be updated in this series? Another option would be
to move the SerDes teardown into a .pcs_link_down implementation.

> diff --git a/include/linux/phylink.h b/include/linux/phylink.h
> index 9ce66b0344370..d53319e67f776 100644
> --- a/include/linux/phylink.h
> +++ b/include/linux/phylink.h

[ ... ]

> @@ -693,6 +695,16 @@ void pcs_an_restart(struct phylink_pcs *pcs);
>  void pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
>  		 phy_interface_t interface, int speed, int duplex);
>  
> +/**
> + * pcs_link_down() - tear down link between MAC and PCS
> + * @pcs: a pointer to a &struct phylink_pcs.
> + *
> + * This call will be made just after mac_link_down() to inform the PCS the
> + * link has gone down. PCS should be configured to stop processing packets
> + * for transmission and reception.
> + */
> +void pcs_link_down(struct phylink_pcs *pcs);

[Severity: Low]
This text follows the mac_link_down() contract but leaves out its
in-band caveat:

 * If @mode is %MLO_AN_INBAND, then this function must not prevent the
 * link coming up.

Should pcs_link_down() have the same caveat? With MLO_AN_INBAND,
phylink_resolve() gets link state from pcs_get_state(). The only call
that reverses pcs_link_down() is pcs_link_up(), and phylink_link_up()
calls it only after pcs_get_state() has reported link up:

phylink_link_down()
  phylink_pcs_link_down()        <- PCS stops RX/AN, as this doc allows
phylink_resolve()
  phylink_mac_pcs_get_state()    <- link stays false
  phylink_link_up()->pcs_link_up() is never reached

phylink_link_down() calls the hook on every link-down, including the
latched link-fail retrigger path. Could a PCS driver that follows this
doc literally and stops its receive or AN logic here leave an in-band
port down for good?

Unlike pcs_link_up(), the new callback gets no neg_mode or interface
argument. A driver cannot tell from the call alone whether in-band
detection has to stay active.

The only implementation in the series is airoha_pcs_link_down() in "net:
pcs: airoha: add PCS driver for Airoha AN7581 SoC". It is not affected.
It only sets the XFI MAC RX/TX MPI/MBI STOP bits, which
airoha_pcs_pre_config() also sets before link-up. airoha_pcs_get_state()
reads the hsgmii_an and usxgmii_pcs registers.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 13/15] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS
  2026-11-29 23:05 ` [PATCH net-next v23 13/15] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS Christian Marangi
@ 2026-11-30 23:47   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:47 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The airoha,an7581-pcs-pcie binding makes the PCS node own the
  PCIe SerDes analog and per-lane PMA blocks.
- [Medium] The binding defines no `resets`/`reset-names` and sets
  `additionalProperties: false` (airoha,pcs.yaml line 176), so any DT…
- [Low] The airoha,an7581-pcs-pcie variant sets "#pcs-cells" to const 1
  (Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml, line…
- [Low] The commit message and the binding `description` (airoha,pcs.yaml
  lines 12-15) list "10BASE-R" as a supported interface.
- [Low] No MAINTAINERS entry covers the new file
  Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml, at this…

--

dt-bindings: net: pcs: Document support for Airoha Ethernet PCS

This commit adds a YAML binding for the Airoha AN7581 PCS blocks, with
four compatibles for the Ethernet, PON, PCIe-shared and USB-shared SerDes
ports. The PCIe variant describes two PCS instances and uses
#pcs-cells = <1>. The other variants use #pcs-cells = <0>.

> diff --git a/Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml b/Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml
> new file mode 100644
> index 0000000000000..9c1d116c1b016
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml
> @@ -0,0 +1,261 @@

[ ... ]

> +maintainers:
> +  - Christian Marangi <ansuelsmth@gmail.com>

[Severity: Low]
Should this new file also get a MAINTAINERS entry?

The series adds an ETHERNET PCS SUBSYSTEM entry, but it only lists:

F:	Documentation/networking/pcs.rst
F:	drivers/net/pcs/pcs.c
F:	include/linux/pcs/pcs-provider.h
F:	include/linux/pcs/pcs.h

No entry covers airoha,pcs.yaml or drivers/net/pcs/airoha/. The other
bindings in this directory (renesas,rzn1-miic.yaml,
rockchip,rk3568-xpcs.yaml, snps,dw-xpcs.yaml) each have an F: line.
As it stands, checkpatch will warn, and get_maintainer.pl will not list
the author for later changes to this binding.

> +
> +description:
> +  Airoha AN7581 SoC expose multiple Physical Coding Sublayer (PCS) for
> +  the various Serdes port supporting different Media Independent Interface
> +  (10BASE-R, USXGMII, 2500BASE-X, 1000BASE-X, SGMII).

[Severity: Low]
This isn't a bug, but should 10BASE-R be 10GBASE-R? The driver sets
PHY_INTERFACE_MODE_10GBASER for this mode. The commit message has the
same text.

The description also reads as if every SerDes port supports the whole
list. Is that true for airoha,an7581-pcs-usb? That variant has no usxgmii
or pcs_pma regions, and airoha_pcs_probe() only sets the 10GBASE-R and
USXGMII bits for the ETH, PON and PCIE port types:

	case AIROHA_PCS_ETH:
	case AIROHA_PCS_PON:
	case AIROHA_PCS_PCIE:
		__set_bit(PHY_INTERFACE_MODE_10GBASER, ...);
		__set_bit(PHY_INTERFACE_MODE_USXGMII, ...);
		fallthrough;
	case AIROHA_PCS_USB:

The commit message also calls this an Ethernet PCS binding. It does not
mention the PCIe-shared variant (two PCS instances, #pcs-cells = <1>) or
the USB-shared variant (needs a USB PHY via phys, no SCU).

[ ... ]

> +  - if:
> +      properties:
> +        compatible:
> +          contains:
> +            const: airoha,an7581-pcs-pcie
> +
> +    then:
> +      properties:

[ ... ]

> +        reg-names:
> +          items:

[ ... ]

> +            - const: pcs_ana
> +            - const: pcs_pma0
> +            - const: pcs_pma1
> +
> +        phys: false

[Severity: Medium]
Do these regions overlap with the existing PCIe PHY node?

The example for this compatible puts pcs_ana, pcs_pma0 and pcs_pma1 at
0x1fa5a000, 0x1fa5b000 and 0x1fa5c000. The pciephy node in
arch/arm64/boot/dts/airoha/en7581.dtsi already claims the same blocks.
That node is not disabled by default:

	pciephy: phy@1fa5a000 {
		compatible = "airoha,en7581-pcie-phy";
		reg = <0x0 0x1fa5a000 0x0 0xfff>,
		      <0x0 0x1fa5b000 0x0 0xfff>,
		      <0x0 0x1fa5c000 0x0 0xfff>,

Both drivers map these regions with
devm_platform_ioremap_resource_byname():

- airoha_pcie_phy_probe() maps "csr-2l", "pma0" and "pma1".
- airoha_pcs_pcie_alloc_maps()->airoha_pcs_init_named_regmap() maps
  "pcs_ana", "pcs_pma0" and "pcs_pma1".

Suppose a board enables an airoha,an7581-pcs-pcie node laid out like the
example. Would the second device to probe fail with -EBUSY? That breaks
either the PCIe controllers or the Ethernet PCS.

Because of phys: false, the PCS node also cannot refer to the PCIe PHY.
The USB variant, by contrast, requires phys and leaves the PMA to the
USB PHY.

Could the binding model the SerDes as one resource owned by one node?
Or could it at least document that pciephy must be disabled when this
variant is used?

> +
> +        "#pcs-cells":
> +          const: 1

[Severity: Low]
What does the single #pcs-cells argument cell mean? The binding does not
say.

airoha_pcs_xlate() treats it as a PCS port index that must be below
num_port, so 0 or 1 for the pcs_mac0 and pcs_mac1 instances:

	case AIROHA_PCS_PCIE:
		if (pcsspec->nargs != 1) {
	...
	if (pcsspec->nargs)
		index = pcsspec->args[0];

	if (index >= priv->data->num_port) {

This becomes DT ABI once merged. Should the cell be described here?

On the consumer side, the series does not change pcs-handle in
Documentation/devicetree/bindings/net/ethernet-controller.yaml:

  pcs-handle:
    $ref: /schemas/types.yaml#/definitions/phandle-array
    items:
      maxItems: 1

Its description also still says the property points to a PCS PHY device
on an MDIO bus.

The airoha,en7581-eth GMAC ports reference ethernet-controller.yaml with
unevaluatedProperties: false. Would an entry like
pcs-handle = <&pcs_pcie 1>; then be checked against a schema that only
allows a bare phandle?

The runtime parser in drivers/net/pcs/pcs.c, used by fwnode_pcs_get() and
fwnode_phylink_pcs_count(), does expect argument cells:

	return fwnode_property_get_reference_args(fwnode, "pcs-handle",
						  "#pcs-cells", 0, index,
						  out_args);

I could not verify whether dt-validate actually rejects this, because
that depends on how dtschema splits pcs-handle phandle-arrays. Still,
the commit message says "This follow the new PCS provider with the use
of #pcs-cells property", while the documented contract only seems to
work for #pcs-cells = <0>.

> +
> +      required:
> +        - airoha,scu

[ ... ]

> +additionalProperties: false

[Severity: Medium]
Should the binding document resets and reset-names?

There is no resets property, and additionalProperties is false, so a
node that provides reset lines fails validation. The PCS driver added
later in this series requests them in airoha_pcs_probe():

	priv->rsts[0].id = "mac";
	priv->rsts[1].id = "phy";
	ret = devm_reset_control_bulk_get_optional_exclusive(dev, ARRAY_SIZE(priv->rsts),
							     priv->rsts);

airoha_pcs_setup_scu() then asserts and deasserts them after it switches
the SCU interface select, on every interface mode setup.

On a DT that follows this binding, the optional getter returns no-op
controls. Doesn't that mean the MAC/PHY reset around a mode change is
silently skipped?

A DT that adds the "mac" and "phy" resets the driver expects would
instead be rejected by dt_binding_check and dtbs_check.

include/dt-bindings/reset/airoha,en7581-reset.h already exports lines
that look suitable: EN7581_XPON_PHY_RST, EN7581_XPON_MAC_RST,
EN7581_XSI_MAC_RST, EN7581_XSI_PHY_RST, EN7581_DUAL_HSI0/1_RST,
EN7581_DUAL_HSI0/1_MAC_RST, EN7581_HSI_RST and EN7581_HSI_MAC_RST.

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH net-next v23 15/15] net: airoha: add phylink support
  2026-11-29 23:05 ` [PATCH net-next v23 15/15] net: airoha: add phylink support Christian Marangi
@ 2026-11-30 23:48   ` netdev-bot+sashiko
  0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 23:48 UTC (permalink / raw)
  To: ansuelsmth
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt,
	conor+dt, willemb, horms, corbet, skhan, rdunlap, lorenzo,
	hkallweit1, linux, maxime.chevallier, p.zabel, nathan,
	ndesaulniers, morbo, justinstitt, rmk+kernel, netdev, devicetree,
	linux-doc, linux-kernel, linux-arm-kernel, linux-mediatek, llvm,
	zhengping.zhang

Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 2 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] This patch turns three device-tree properties into hard
  requirements without updating the DT binding or saying so in the…
- [Medium] The hardware flow-control state does not follow the pause
  result that phylink negotiates and reports.
- [Medium] For GDM2/3/4, airoha_setup_phylink() sets mac_capabilities to
  include MAC_10 | MAC_100 | MAC_1000.
- [Low] The commit message does not fully describe what the patch does.

--

net: airoha: add phylink support

This adds phylink support to every GDM port of the airoha ethernet
driver. GDM1 uses the internal interface. GDM2/3/4 look up PCS instances
through pcs-handle. Phylink is connected and started in ndo_open, stopped
in ndo_stop, GDM4 MBI fragment sizes are programmed on link up, and
get/set_link_ksettings now go through phylink.

> Add phylink support for each GDM port. For GDM1 add the internal interface
> mode as the only supported mode. For GDM2/3/4 add the required
> configuration of the PCS to make the external PHY or attached SFP cage
> work.

[Severity: Low]
This isn't a bug, but is this description accurate for this patch?

The MAC driver change does not touch any PCS registers. It only fills
pcs_interfaces, num_possible_pcs and fill_available_pcs in phylink_config
so PCS instances can be looked up through pcs-handle. The PCS programming
itself is in the separate "net: pcs: airoha: add PCS driver for Airoha
AN7581 SoC" commit.

The message also leaves out the one MAC-side hardware change. On link up,
GDM4 now writes speed-dependent TX/RX fragment sizes into the SGMII0 or
SGMII1 field of the new REG_FE_GDM4_TMBI_FRAG and REG_FE_GDM4_RMBI_FRAG
registers, protected by a new per-port link_lock mutex.

The "Nothing to do in MAC, everything is handled in PCS" comments on
airoha_mac_config() and airoha_mac_link_down() are also misleading,
because airoha_mac_link_up() does program FE state for GDM4.

> diff --git a/drivers/net/ethernet/airoha/airoha_eth.c b/drivers/net/ethernet/airoha/airoha_eth.c
> index 044ad0302ef35..e41dfad58b258 100644
> --- a/drivers/net/ethernet/airoha/airoha_eth.c
> +++ b/drivers/net/ethernet/airoha/airoha_eth.c

[ ... ]

> @@ -3752,6 +3790,155 @@ bool airoha_is_valid_gdm_dev(struct airoha_eth *eth,
>  	return false;
>  }
>  
> +/* Nothing to do in MAC, everything is handled in PCS */
> +static void airoha_mac_config(struct phylink_config *config, unsigned int mode,
> +			      const struct phylink_link_state *state)
> +{
> +}
> +
> +static void airoha_mac_link_up(struct phylink_config *config, struct phy_device *phy,
> +			       unsigned int mode, phy_interface_t interface,
> +			       int speed, int duplex, bool tx_pause, bool rx_pause)
> +{
> +	struct airoha_gdm_dev *dev = container_of(config, struct airoha_gdm_dev,
> +						  phylink_config);
> +	struct airoha_gdm_port *port = dev->port;
> +	struct airoha_eth *eth = dev->eth;
> +	u32 frag_size_tx, frag_size_rx;
> +
> +	/* TX/RX frag is configured only for GDM4 */
> +	if (port->id != AIROHA_GDM4_IDX)
> +		return;

[Severity: Medium]
Should tx_pause and rx_pause be applied to the hardware somewhere?

airoha_setup_phylink() advertises MAC_SYM_PAUSE | MAC_ASYM_PAUSE on every
GDM port. Phylink will negotiate pause and pass the result here, but
airoha_mac_link_up() never uses it, and airoha_mac_config() and
airoha_mac_link_down() are empty. GDM_INGRESS_FC_EN_MASK in
REG_GDM_INGRESS_CFG is never written by the driver either.

The only flow-control programming is in the PCS driver:

drivers/net/pcs/airoha/pcs-airoha-common.c:airoha_pcs_config() {
    ...
	regmap_update_bits(maps->pcs_mac, AIROHA_PCS_XFI_MAC_XFI_GIB_CFG,
			   AIROHA_PCS_XFI_TX_FC_EN | AIROHA_PCS_XFI_RX_FC_EN,
			   permit_pause_to_mac ?
				AIROHA_PCS_XFI_TX_FC_EN | AIROHA_PCS_XFI_RX_FC_EN :
				0);
    ...
}

permit_pause_to_mac comes from link_config.pause & MLO_PAUSE_AN, and
phylink_create() defaults link_config.pause to MLO_PAUSE_AN. No
.get_pauseparam or .set_pauseparam is routed to phylink, so the user
cannot change this.

If the link partner advertises no pause, or only asymmetric pause, would
the XFI MAC still send and honour PAUSE frames in both directions?
ethtool would then report a different pause state from the one the
hardware is actually using.

[ ... ]

> +static int airoha_setup_phylink(struct net_device *netdev)
> +{

[ ... ]

> +	err = of_get_phy_mode(np, &phy_mode);
> +	if (err) {
> +		dev_err(&netdev->dev, "incorrect phy-mode\n");
> +		return err;
> +	}

[ ... ]

> +	if (port->id == AIROHA_GDM1_IDX) {
> +		config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
> +					   MAC_10000FD;
> +
> +		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
> +			  config->supported_interfaces);
> +	} else {
> +		config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
> +					   MAC_10 | MAC_100 | MAC_1000 |
> +					   MAC_2500FD | MAC_5000FD | MAC_10000FD;

[Severity: Medium]
Can the hardware actually do half duplex here?

MAC_10, MAC_100 and MAC_1000 include the HD variants, so phylink lets an
attached SGMII PHY advertise and resolve half duplex. Nothing seems to
program duplex, though:

- airoha_mac_link_up() ignores its duplex argument
- airoha_mac_config() is empty
- airoha_pcs_link_up() in the PCS driver also ignores duplex
- AIROHA_PCS_HSGMII_AN_DUPLEX_FORCE_MODE is defined but never used
- the PCS get_state helpers hard-code state->duplex = DUPLEX_FULL

Suppose an external SGMII PHY resolves 10/100/1000 half duplex, for
example to a hub. Would that leave phylink reporting half duplex while
the MAC/PCS still runs full duplex, giving a duplex mismatch?

If the datapath is full duplex only, MAC_10FD | MAC_100FD | MAC_1000FD
may be the right capabilities.

> +
> +		config->num_possible_pcs = fwnode_phylink_pcs_count(dev_fwnode(config->dev));
> +		config->fill_available_pcs = airoha_fill_available_pcs;
> +

[ ... ]

> +		phy_interface_copy(config->pcs_interfaces,
> +				   config->supported_interfaces);
> +	}
> +
> +	phylink = phylink_create(config, of_fwnode_handle(np),
> +				 phy_mode, &airoha_phylink_ops);
> +	if (IS_ERR(phylink))
> +		return PTR_ERR(phylink);

[Severity: High]
Does this break probe for device trees that follow the current binding?

phy-mode is now mandatory on every GDM node, because a failing
of_get_phy_mode() returns an error.

For GDM2/3/4, pcs_interfaces and fill_available_pcs are always set.
num_possible_pcs comes from fwnode_phylink_pcs_count(), which returns 0
when there is no pcs-handle or no #pcs-cells. phylink_create() then
rejects the config:

drivers/net/phy/phylink.c:phylink_create() {
    ...
	if (has_pcs_interfaces != has_pcs_callback ||
	    has_pcs_interfaces != has_pcs_count) {
		dev_err(config->dev,
			"phylink: error: pcs_interfaces, num_possible_pcs and fill_available_pcs must be set together\n");
		return ERR_PTR(-EINVAL);
	}
    ...
}

Errors from fwnode_pcs_get() other than -ENOENT/-ENODEV are also passed
back up.

airoha_alloc_gdm_device() now ends with "return
airoha_setup_phylink(netdev)", so the failure travels up this path:

airoha_probe()->airoha_alloc_gdm_port()->airoha_alloc_gdm_device()->
airoha_setup_phylink()

airoha_probe() then jumps to error_napi_stop and tears down every port,
including the GDM1 DSA conduit.

Documentation/devicetree/bindings/net/airoha,en7581-eth.yaml still
requires only reg and compatible for the ethernet@ nodes. Its own example
(ethernet@1, and ethernet@4 with eth-port children) has no phy-mode,
pcs-handle or fixed-link. The binding also does not say whether pcs-handle
goes on the GDM node or on the airoha,eth-port child.

The new PCS provider only matches airoha,an7581-pcs-* compatibles, but
this MAC driver also binds airoha,an7583-eth. Would AN7583 GDM2/3/4 nodes,
which probed before this patch, now fail probe for the whole controller?

For GDM1, if phy-mode is "internal" but there is no fixed-link or
phy-handle, phylink stays in MLO_AN_PHY. phylink_of_phy_connect() in
airoha_dev_open() then returns -ENODEV, so the conduit can no longer be
opened. Is fixed-link now required on GDM1 as well?

The in-tree en7581.dtsi gdm1 node already has phy-mode = "internal" and
fixed-link, so in-tree boards look unaffected.

The commit message only says:

    These needs to be defined in the GDM port node using the pcs-handle
    property.

It does not say that a missing pcs-handle fails the whole controller, and
it does not mention the phy-mode or fixed-link requirements.

Could these cases fail only the affected netdev, for example by setting
the PCS fields only when num_possible_pcs is non-zero? Could the binding
and commit message also document the new requirements?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129230558.612526-1-ansuelsmth%40gmail.com


^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2026-12-01  0:30 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 23:05 [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 01/15] net: phylink: make PCS phylink instance access RCU protected Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 02/15] net: phylink: keep and use MAC supported_interfaces in phylink struct Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 03/15] net: phylink: introduce internal phylink PCS handling Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 04/15] net: pcs: implement Firmware node support for PCS driver Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 05/15] net: phylink: make phylink_validate state locked Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 06/15] net: phylink: add capabilities helpers and track modified advertising Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 07/15] net: sfp: support deferred module insertion Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 08/15] net: phylink: support PCS provider release Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 09/15] net: phylink: support late PCS provider attach Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 10/15] net: Document PCS subsystem Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 11/15] MAINTAINERS: add myself as PCS subsystem maintainer Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 12/15] net: phylink: add .pcs_link_down PCS OP Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 13/15] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS Christian Marangi
2026-11-30 23:47   ` netdev-bot+sashiko
2026-11-29 23:05 ` [PATCH net-next v23 15/15] net: airoha: add phylink support Christian Marangi
2026-11-30 23:48   ` netdev-bot+sashiko
2026-11-29 23:09 ` [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox