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; 16+ 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] 16+ 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-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, 0 replies; 16+ 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] 16+ 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; 16+ 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] 16+ 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-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, 0 replies; 16+ 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] 16+ 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-29 23:05 ` [PATCH net-next v23 05/15] net: phylink: make phylink_validate state locked Christian Marangi
                   ` (10 subsequent siblings)
  14 siblings, 0 replies; 16+ 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] 16+ 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-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, 0 replies; 16+ 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] 16+ 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-29 23:05 ` [PATCH net-next v23 07/15] net: sfp: support deferred module insertion Christian Marangi
                   ` (8 subsequent siblings)
  14 siblings, 0 replies; 16+ 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] 16+ 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-29 23:05 ` [PATCH net-next v23 08/15] net: phylink: support PCS provider release Christian Marangi
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 16+ 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] 16+ 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-29 23:05 ` [PATCH net-next v23 09/15] net: phylink: support late PCS provider attach Christian Marangi
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 16+ 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] 16+ 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-29 23:05 ` [PATCH net-next v23 10/15] net: Document PCS subsystem Christian Marangi
                   ` (5 subsequent siblings)
  14 siblings, 0 replies; 16+ 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] 16+ 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-29 23:05 ` [PATCH net-next v23 11/15] MAINTAINERS: add myself as PCS subsystem maintainer Christian Marangi
                   ` (4 subsequent siblings)
  14 siblings, 0 replies; 16+ 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] 16+ 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; 16+ 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] 16+ 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-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, 0 replies; 16+ 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] 16+ 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-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, 0 replies; 16+ 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] 16+ 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-29 23:09 ` [PATCH net-next v23 00/15] net: pcs: Introduce support for fwnode PCS netdev-bot+sinfo
  14 siblings, 0 replies; 16+ 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] 16+ 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; 16+ 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] 16+ messages in thread

end of thread, other threads:[~2026-11-29 23:25 UTC | newest]

Thread overview: 16+ 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-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-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 ` [PATCH net-next v23 05/15] net: phylink: make phylink_validate state locked Christian Marangi
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 ` [PATCH net-next v23 07/15] net: sfp: support deferred module insertion Christian Marangi
2026-11-29 23:05 ` [PATCH net-next v23 08/15] net: phylink: support PCS provider release Christian Marangi
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 ` [PATCH net-next v23 10/15] net: Document PCS subsystem Christian Marangi
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-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 ` [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

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