Skip to contentSkip to conversation

net: pcs: Introduce support for fwnode PCS

PATCH Superseded Christian Marangi 4 participants 27 messages Jun 9 – Jun 12

Subsystems (9)

v6 of 22 Superseded Latest is v22 →

lore
Share & tools
lore
#0 lore raw

v6 cover net: pcs: Introduce support for fwnode PCS

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_get.

A PCS provider on driver removal should first call fwnode_pcs_del_provider() to delete itself as a provider and then release the PCS from phylink with phylink_release_pcs() under rtnl lock.

---
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.

It's suggested to read patch 0003 of this series for more info, here a brief explaination of the idea:

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 available_pcs and num_available_pcs in phylink_config. A helper to parse PCS from fwnode is provided fwnode_phylink_pcs_parse() that will fill a preallocated array of PCS. (the same function can be used to get the number of PCS defined in DT, more info in patch 0005)

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 avialable.

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.

---

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 (12):
net: phylink: keep and use MAC supported_interfaces in phylink struct
net: phylink: introduce internal phylink PCS handling
net: phylink: add phylink_release_pcs() to externally release a PCS
net: pcs: implement Firmware node support for PCS driver
net: phylink: support late PCS provider attach
net: Document PCS subsystem
MAINTAINERS: add myself as PCS subsystem maintainer
of: property: fw_devlink: Add support for "pcs-handle"
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          |  260 ++
 Documentation/networking/index.rst            |    1 +
 Documentation/networking/pcs.rst              |  228 ++
 MAINTAINERS                                   |    9 +
 drivers/net/ethernet/airoha/Kconfig           |    1 +
 drivers/net/ethernet/airoha/airoha_eth.c      |  167 +-
 drivers/net/ethernet/airoha/airoha_eth.h      |    3 +
 drivers/net/ethernet/airoha/airoha_regs.h     |   12 +
 drivers/net/pcs/Kconfig                       |    8 +
 drivers/net/pcs/Makefile                      |    3 +
 drivers/net/pcs/airoha/Kconfig                |   12 +
 drivers/net/pcs/airoha/Makefile               |    7 +
 drivers/net/pcs/airoha/pcs-airoha-common.c    | 1313 +++++++++++
 drivers/net/pcs/airoha/pcs-airoha.h           | 1309 +++++++++++
 drivers/net/pcs/airoha/pcs-an7581.c           | 2093 +++++++++++++++++
 drivers/net/pcs/pcs.c                         |  241 ++
 drivers/net/phy/phylink.c                     |  316 ++-
 drivers/of/property.c                         |    2 +
 include/linux/pcs/pcs-provider.h              |   41 +
 include/linux/pcs/pcs.h                       |  104 +
 include/linux/phylink.h                       |   20 +
 21 files changed, 6123 insertions(+), 27 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 include/linux/pcs/pcs-provider.h
 create mode 100644 include/linux/pcs/pcs.h
Signature · 1 line
2.53.0
#1 lore raw

PATCH v6 1/12 net: phylink: keep and use MAC supported_interfaces in phylink struct

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.

---
 drivers/net/phy/phylink.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/net/phy/phylink.c +15 -7
index 087ac63f9193..4d59c0dd78db 100644
@@ -60,6 +60,11 @@ 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;
 
@@ -629,7 +634,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,
@@ -1868,6 +1873,9 @@ struct phylink *phylink_create(struct phylink_config *config,
 	mutex_init(&pl->state_mutex);
 	INIT_WORK(&pl->resolve, phylink_resolve);
 
+	phy_interface_copy(pl->supported_interfaces,
+			   config->supported_interfaces);
+
 	pl->config = config;
 	if (config->type == PHYLINK_NETDEV) {
 		pl->netdev = to_net_dev(config->dev);
@@ -2026,7 +2034,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");
@@ -2828,12 +2836,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;
 	}
 
@@ -3761,14 +3769,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");
@@ -3939,7 +3947,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);
 
 	/* Do the initial configuration */
 	return phylink_sfp_config_phy(pl, phy);
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH net-next v6 01/12] net: phylink: keep and use MAC supported_interfaces in phylink struct

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Late modification to `config->supported_interfaces` by drivers is silently ignored due to by-value…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Late modification to config->supported_interfaces by drivers is silently ignored due to by-value copying in phylink_create(), breaking support for devices like MTK GMAC2. --

commit eaea53b185dbdc4c75406c542c97543c4c451c9d
Author: Christian Marangi <[email protected]>

net: phylink: keep and use MAC supported_interfaces in phylink struct

This patch copies supported_interfaces from phylink_config into the phylink structure to prepare for internal PCS handling. The internal copy is then used in place of the config structure's bitmap.

Quoted 4 lines
drivers/net/phy/phylink.c +0 -0
index 087ac63f9193d..4d59c0dd78dbf 100644

[ ... ]

drivers/net/phy/phylink.c struct phylink *phylink_create(struct phylink_config *config, · line 1878

Quoted 7 lines
drivers/net/phy/phylink.c +3 -0
@@ -1868,6 +1873,9 @@ struct phylink *phylink_create(struct phylink_config *config,
 	mutex_init(&pl->state_mutex);
 	INIT_WORK(&pl->resolve, phylink_resolve);
 
+	phy_interface_copy(pl->supported_interfaces,
+			   config->supported_interfaces);
+

[Severity: High]
Will this by-value copy break drivers that modify supported_interfaces after calling phylink_create()?

For example, looking at the mtk_eth_soc driver in mtk_add_mac(), the supported_interfaces bitmap is modified after phylink_create() returns:

drivers/net/ethernet/mediatek/mtk_eth_soc.c:mtk_add_mac() {
        ...
        mac->phylink = phylink_create(&mac->phylink_config, ...);
        ...
        if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_2P5GPHY) &&
            id == MTK_GMAC2_ID)
                __set_bit(PHY_INTERFACE_MODE_INTERNAL,
                          mac->phylink_config.supported_interfaces);
        ...
}

Since phylink_create() has already taken a snapshot of the interfaces, this late addition of PHY_INTERFACE_MODE_INTERNAL will be dropped. Could this cause phylink_validate() to incorrectly reject the internal PHY capability for the MTK GMAC2?

Quoted 3 lines
drivers/net/phy/phylink.c +0 -0
 	pl->config = config;
 	if (config->type == PHYLINK_NETDEV) {
 		pl->netdev = to_net_dev(config->dev);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH net-next v6 01/12] net: phylink: keep and use MAC supported_interfaces in phylink struct

Quoted 44 lines · On Wed, Jun 10, 2026 at 04:16:20PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Late modification to config->supported_interfaces by drivers is silently ignored due to by-value copying in phylink_create(), breaking support for devices like MTK GMAC2. --

commit eaea53b185dbdc4c75406c542c97543c4c451c9d
Author: Christian Marangi <[email protected]>

net: phylink: keep and use MAC supported_interfaces in phylink struct

This patch copies supported_interfaces from phylink_config into the phylink structure to prepare for internal PCS handling. The internal copy is then used in place of the config structure's bitmap.

Quoted 4 lines
drivers/net/phy/phylink.c +0 -0
index 087ac63f9193d..4d59c0dd78dbf 100644

[ ... ]

drivers/net/phy/phylink.c struct phylink *phylink_create(struct phylink_config *config, · line 1878

Quoted 7 lines
drivers/net/phy/phylink.c +3 -0
@@ -1868,6 +1873,9 @@ struct phylink *phylink_create(struct phylink_config *config,
 	mutex_init(&pl->state_mutex);
 	INIT_WORK(&pl->resolve, phylink_resolve);
 
+	phy_interface_copy(pl->supported_interfaces,
+			   config->supported_interfaces);
+

[Severity: High]
Will this by-value copy break drivers that modify supported_interfaces after calling phylink_create()?

For example, looking at the mtk_eth_soc driver in mtk_add_mac(), the supported_interfaces bitmap is modified after phylink_create() returns:

drivers/net/ethernet/mediatek/mtk_eth_soc.c:mtk_add_mac() {
        ...
        mac->phylink = phylink_create(&mac->phylink_config, ...);
        ...
        if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_2P5GPHY) &&
            id == MTK_GMAC2_ID)
                __set_bit(PHY_INTERFACE_MODE_INTERNAL,
                          mac->phylink_config.supported_interfaces);
        ...
}
Quoted 9 lines

Since phylink_create() has already taken a snapshot of the interfaces, this late addition of PHY_INTERFACE_MODE_INTERNAL will be dropped. Could this cause phylink_validate() to incorrectly reject the internal PHY capability for the MTK GMAC2?

Quoted 3 lines
drivers/net/phy/phylink.c +0 -0
 	pl->config = config;
 	if (config->type == PHYLINK_NETDEV) {
 		pl->netdev = to_net_dev(config->dev);

I was with the idea that value of phylink_config should be used only for phylink_create and then in theory they should not be changed.

Isn't this a bug in MTK code to modify the phylink_config AFTER phylink_create?

It was never a problem since it's it's INTERNAL interface mode and a PCS is not needed for it but still I feel this should be fixed and this change doesn't introduce some kind of regression.

Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Signature · 1 line
	Ansuel
#4 lore raw

PATCH v6 2/12 net: phylink: introduce internal phylink PCS handling

In reply to Christian Marangi

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_interface value in the PCS struct.

MAC should now provide a callback to fill the available PCS in phylink_config in .fill_available_pcs and fill the .num_available_pcs with the number of elements in the array. MAC should also define a new bitmap, pcs_interfaces, in phylink_config to define for what interface mode a dedicated PCS is required.

On phylink_create(), an array of PCS pointer is allocated of size .num_available_pcs from phylink_config and .fill_available_pcs from phylink_config is called passing as args the just allocated array and the number of available element in it.

MAC will fill this passed array with all the available PCS.

This array is then parsed and a linked list of PCS is created based on the allocated PCS array filled by MAC via .fill_available_pcs(). Also the supported_interface value in phylink struct is updated with the new supported_interface from the provided PCS.

On phylink_start() every PCS in phylink PCS list gets attached to the phylink instance. This is done by setting the phylink value in phylink_pcs struct to the phylink instance.

On phylink_stop(), every PCS in phylink PCS list is detached from the phylink instance. This is done by setting the phylink value in phylink_pcs struct to NULL.

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.

They will make use of phylink_validate_pcs_interface() that will loop for every PCS in the phylink PCS available list and find one that supports the passed interface.

phylink_validate_pcs_interface() applies the same logic of .mac_select_pcs where if a supported_interface value is not set for the PCS struct, then it's assumed every interface is supported.

A MAC is required to implement either a .mac_select_pcs or make use of the PCS list implementation. Implementing both will result in a fail on MAC/PCS validation.

A MAC defining .num_available_pcs in phylink_config MUST also define a .fill_available_pcs or phylink_create() will fail with an negative error.

phylink value in phylink_pcs struct with this implementation is used to track from PCS side when it's attached to a phylink instance. PCS driver will make use of this information to correctly detach from a phylink instance if needed.

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.

---
 drivers/net/phy/phylink.c | 185 ++++++++++++++++++++++++++++++++++----
 include/linux/phylink.h   |  16 ++++
 2 files changed, 183 insertions(+), 18 deletions(-)
drivers/net/phy/phylink.c +167 -18
index 4d59c0dd78db..4d6ffda0cdd6 100644
@@ -60,6 +60,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.
 	 */
@@ -154,6 +157,8 @@ static const phy_interface_t phylink_sfp_interface_preference[] = {
 
 static DECLARE_PHY_INTERFACE_MASK(phylink_sfp_interfaces);
 
+static void phylink_run_resolve(struct phylink *pl);
+
 /**
  * phylink_set_port_modes() - set the port type modes in the ethtool mask
  * @mask: ethtool link mode mask
@@ -518,22 +523,59 @@ 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)
 {
-	struct phylink_pcs *pcs = NULL;
 	unsigned long capabilities;
+	struct phylink_pcs *pcs;
+	bool pcs_found = false;
 	int ret;
 
 	/* Get the PCS for this interface mode */
 	if (pl->mac_ops->mac_select_pcs) {
+		/* Make sure either PCS internal validation or .mac_select_pcs
+		 * is used. Return error if both are defined.
+		 */
+		if (!list_empty(&pl->pcs_list)) {
+			phylink_err(pl, "either phylink_pcs_add() or .mac_select_pcs must be used\n");
+			return -EINVAL;
+		}
+
 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
 		if (IS_ERR(pcs))
 			return PTR_ERR(pcs);
+
+		pcs_found = !!pcs;
+	} else {
+		/* Check every assigned PCS and search for one that supports
+		 * the interface.
+		 */
+		list_for_each_entry(pcs, &pl->pcs_list, list) {
+			if (!phylink_validate_pcs_interface(pcs, state->interface)) {
+				pcs_found = true;
+				break;
+			}
+		}
 	}
 
-	if (pcs) {
+	if (pcs_found) {
 		/* The PCS, if present, must be setup before phylink_create()
 		 * has been called. If the ops is not initialised, print an
 		 * error and backtrace rather than oopsing the kernel.
@@ -545,13 +587,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;
 		}
@@ -965,12 +1004,22 @@ static unsigned int phylink_inband_caps(struct phylink *pl,
 					 phy_interface_t interface)
 {
 	struct phylink_pcs *pcs;
+	bool pcs_found = false;
 
-	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);
+		pcs_found = !!pcs;
+	} else {
+		list_for_each_entry(pcs, &pl->pcs_list, list) {
+			if (!phylink_validate_pcs_interface(pcs, interface)) {
+				pcs_found = true;
+				break;
+			}
+		}
+	}
 
-	pcs = pl->mac_ops->mac_select_pcs(pl->config, interface);
-	if (!pcs)
+	if (!pcs_found)
 		return 0;
 
 	return phylink_pcs_inband_caps(pcs, interface);
@@ -1265,10 +1314,36 @@ 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 dedicaed PCS for
+	 * the requested interface.
+	 */
+	} else if (test_bit(state->interface, pl->config->pcs_interfaces)) {
+		bool pcs_found = false;
+
+		list_for_each_entry(pcs, &pl->pcs_list, list) {
+			if (!phylink_validate_pcs_interface(pcs,
+							    state->interface)) {
+				pcs_found = true;
+				break;
+			}
+		}
 
-		pcs_changed = pl->pcs != pcs;
+		if (!pcs_found) {
+			phylink_err(pl,
+				    "couldn't find a PCS 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);
 
 	phylink_dbg(pl, "major config, active %s/%s/%s\n",
@@ -1295,11 +1370,13 @@ static void phylink_major_config(struct phylink *pl, bool restart,
 	if (pcs_changed) {
 		phylink_pcs_disable(pl->pcs);
 
-		if (pl->pcs)
-			pl->pcs->phylink = NULL;
+		if (pl->mac_ops->mac_select_pcs) {
+			if (pl->pcs)
+				pl->pcs->phylink = NULL;
 
-		if (pcs)
-			pcs->phylink = pl;
+			if (pcs)
+				pcs->phylink = pl;
+		}
 
 		pl->pcs = pcs;
 	}
@@ -1834,6 +1911,44 @@ int phylink_set_fixed_link(struct phylink *pl,
 }
 EXPORT_SYMBOL_GPL(phylink_set_fixed_link);
 
+static int phylink_fill_available_pcs(struct phylink *pl,
+				      struct phylink_config *config)
+{
+	struct phylink_pcs **pcss;
+	int i, ret;
+
+	if (!config->num_available_pcs)
+		return 0;
+
+	if (!config->fill_available_pcs) {
+		dev_err(config->dev,
+			"phylink: error: num_available_pcs defined but no fill_available_pcs\n");
+		return -EINVAL;
+	}
+
+	pcss = kzalloc_objs(*pcss, config->num_available_pcs);
+	if (!pcss)
+		return -ENOMEM;
+
+	ret = config->fill_available_pcs(config, pcss, config->num_available_pcs);
+	if (ret)
+		goto out;
+
+	for (i = 0; i < config->num_available_pcs; i++) {
+		struct phylink_pcs *pcs = pcss[i];
+
+		if (!pcs)
+			continue;
+
+		list_add(&pcs->list, &pl->pcs_list);
+	}
+
+out:
+	kfree(pcss);
+
+	return ret;
+}
+
 /**
  * phylink_create() - create a phylink instance
  * @config: a pointer to the target &struct phylink_config
@@ -1855,6 +1970,7 @@ struct phylink *phylink_create(struct phylink_config *config,
 			       phy_interface_t iface,
 			       const struct phylink_mac_ops *mac_ops)
 {
+	struct phylink_pcs *pcs;
 	struct phylink *pl;
 	int ret;
 
@@ -1872,9 +1988,21 @@ 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);
+
+	/* Fill the PCS list with available PCS from phylink config */
+	ret = phylink_fill_available_pcs(pl, config);
+	if (ret) {
+		kfree(pl);
+		return ERR_PTR(ret);
+	}
 
 	phy_interface_copy(pl->supported_interfaces,
 			   config->supported_interfaces);
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		phy_interface_or(pl->supported_interfaces,
+				 pl->supported_interfaces,
+				 pcs->supported_interfaces);
 
 	pl->config = config;
 	if (config->type == PHYLINK_NETDEV) {
@@ -1953,10 +2081,16 @@ EXPORT_SYMBOL_GPL(phylink_create);
  */
 void phylink_destroy(struct phylink *pl)
 {
+	struct phylink_pcs *pcs, *tmp;
+
 	sfp_bus_del_upstream(pl->sfp_bus);
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
 
+	/* Remove every PCS from phylink PCS list */
+	list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list)
+		list_del(&pcs->list);
+
 	cancel_work_sync(&pl->resolve);
 	kfree(pl);
 }
@@ -2437,6 +2571,7 @@ static irqreturn_t phylink_link_handler(int irq, void *data)
  */
 void phylink_start(struct phylink *pl)
 {
+	struct phylink_pcs *pcs;
 	bool poll = false;
 
 	ASSERT_RTNL();
@@ -2463,6 +2598,10 @@ void phylink_start(struct phylink *pl)
 
 	pl->pcs_state = PCS_STATE_STARTED;
 
+	/* link available PCS to phylink struct */
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		pcs->phylink = pl;
+
 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
 
 	if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
@@ -2507,6 +2646,8 @@ EXPORT_SYMBOL_GPL(phylink_start);
  */
 void phylink_stop(struct phylink *pl)
 {
+	struct phylink_pcs *pcs;
+
 	ASSERT_RTNL();
 
 	if (pl->sfp_bus)
@@ -2524,6 +2665,14 @@ void phylink_stop(struct phylink *pl)
 	pl->pcs_state = PCS_STATE_DOWN;
 
 	phylink_pcs_disable(pl->pcs);
+
+	/* Drop link between phylink and PCS */
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		pcs->phylink = NULL;
+
+	/* Restore original supported interfaces */
+	phy_interface_copy(pl->supported_interfaces,
+			   pl->config->supported_interfaces);
 }
 EXPORT_SYMBOL_GPL(phylink_stop);
 
include/linux/phylink.h +16 -0
index 2bc0db3d52ac..3387d308c4ad 100644
@@ -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_available_pcs: num of available 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_available_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_available_pcs;
+	int (*fill_available_pcs)(struct phylink_config *config,
+				  struct phylink_pcs **available_pcs,
+				  unsigned int num_available_pcs);
 };
 
 void phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed);
@@ -497,6 +510,9 @@ struct phylink_pcs {
 	struct phylink *phylink;
 	bool poll;
 	bool rxc_always_on;
+
+	/* private: */
+	struct list_head list;
 };
 
 /**
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH net-next v6 02/12] net: phylink: introduce internal phylink PCS handling

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

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

New issues:
- [High] phylink_stop() permanently deletes PCS-supported interfaces from the phylink mask.
- [High] Inactive PCSs attached to the phylink instance can spuriously trigger link changes.
- [Medium] PCS selection logic inconsistently applies pcs_interfaces, causing validation and configuration mismatches.
- [Low] Mutual exclusion check for PCS methods is placed in a validation loop, causing excessive log spam.

Pre-existing issues:
- [High] Missing cleanup of pcs->phylink for legacy .mac_select_pcs code paths leads to Use-After-Free.
- [High] Missing IS_ERR check on mac_select_pcs return value in phylink_inband_caps. --

commit 0ed3c25ac9713ad466d414b45a844576296e0982
Author: Christian Marangi <[email protected]>
Subject: net: phylink: introduce internal phylink PCS handling
Summary: This patch introduces internal PCS handling for phylink as an alternative to the MAC .mac_select_pcs callback. It allows MAC drivers to provide an array of available PCSs during creation, which phylink manages as a linked list to dynamically select and validate the appropriate PCS.

Quoted 4 lines
drivers/net/phy/phylink.c +0 -0
index 4d59c0dd78dbf..4d6ffda0cdd61 100644

[ ... ]

Quoted 29 lines
drivers/net/phy/phylink.c +18 -1
@@ -518,22 +523,59 @@ 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)
 {
-	struct phylink_pcs *pcs = NULL;
 	unsigned long capabilities;
+	struct phylink_pcs *pcs;
+	bool pcs_found = false;
 	int ret;

drivers/net/phy/phylink.c static void phylink_validate_mask_caps(unsigned long *supported, · line 559

Quoted 10 lines
drivers/net/phy/phylink.c +7 -0
 
 	/* Get the PCS for this interface mode */
 	if (pl->mac_ops->mac_select_pcs) {
+		/* Make sure either PCS internal validation or .mac_select_pcs
+		 * is used. Return error if both are defined.
+		 */
+		if (!list_empty(&pl->pcs_list)) {
+			phylink_err(pl, "either phylink_pcs_add() or .mac_select_pcs must be used\n");
+			return -EINVAL;
+		}

[Severity: Low]
Is this the best place for this structural check? Since phylink_validate_mac_and_pcs() is called iteratively for every supported interface mode during mask validation, won't a misconfigured driver trigger this error message dozens of times? Could this check be moved to a one-time initialization path like phylink_create()?

Quoted 7 lines
drivers/net/phy/phylink.c +4 -0
+
 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
 		if (IS_ERR(pcs))
 			return PTR_ERR(pcs);
+
+		pcs_found = !!pcs;
+	} else {

drivers/net/phy/phylink.c static void phylink_validate_mask_caps(unsigned long *supported, · line 576

Quoted 10 lines
drivers/net/phy/phylink.c +9 -0
+		/* Check every assigned PCS and search for one that supports
+		 * the interface.
+		 */
+		list_for_each_entry(pcs, &pl->pcs_list, list) {
+			if (!phylink_validate_pcs_interface(pcs, state->interface)) {
+				pcs_found = true;
+				break;
+			}
+		}
 	}

[Severity: Medium]
Does this validation logic properly account for the pcs_interfaces mask? In phylink_major_config(), the PCS list is only searched if the interface requires a dedicated PCS via test_bit(state->interface, pl->config->pcs_interfaces). Since this loop unconditionally checks the PCS list, could it incorrectly constrain validation by the PCS capabilities even if the MAC handles the interface directly?

[ ... ]

drivers/net/phy/phylink.c static unsigned int phylink_inband_caps(struct phylink *pl, · line 1012

Quoted 12 lines
drivers/net/phy/phylink.c +5 -2
@@ -965,12 +1004,22 @@ static unsigned int phylink_inband_caps(struct phylink *pl,
 					 phy_interface_t interface)
 {
 	struct phylink_pcs *pcs;
+	bool pcs_found = false;
 
-	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);
+		pcs_found = !!pcs;

[Severity: High]
This is a pre-existing issue, but does this correctly handle error pointers returned by mac_select_pcs()? If it returns an ERR_PTR on failure, !!pcs evaluates to true, causing phylink_pcs_inband_caps() to dereference the error pointer later in this function. Could we add an IS_ERR() check here?

drivers/net/phy/phylink.c static unsigned int phylink_inband_caps(struct phylink *pl, · line 1020

Quoted 8 lines
drivers/net/phy/phylink.c +8 -0
+	} else {
+		list_for_each_entry(pcs, &pl->pcs_list, list) {
+			if (!phylink_validate_pcs_interface(pcs, interface)) {
+				pcs_found = true;
+				break;
+			}
+		}
+	}

[Severity: Medium]
Similar to phylink_validate_mac_and_pcs(), this iterates the PCS list without checking if the interface requires a PCS via pl->config->pcs_interfaces. Is it possible for this to return inaccurate inband capabilities if a MAC supports an interface directly without using an available PCS?

[ ... ]

Quoted 7 lines
drivers/net/phy/phylink.c +2 -0
@@ -1953,10 +2081,16 @@ EXPORT_SYMBOL_GPL(phylink_create);
  */
 void phylink_destroy(struct phylink *pl)
 {
+	struct phylink_pcs *pcs, *tmp;
+
 	sfp_bus_del_upstream(pl->sfp_bus);

drivers/net/phy/phylink.c EXPORT_SYMBOL_GPL(phylink_create); · line 2096

Quoted 10 lines
drivers/net/phy/phylink.c +4 -0
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
 
+	/* Remove every PCS from phylink PCS list */
+	list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list)
+		list_del(&pcs->list);
+
 	cancel_work_sync(&pl->resolve);
 	kfree(pl);
 }

[Severity: High]
This is a pre-existing issue, but is it possible for a legacy PCS to access freed memory here? For MAC drivers using the legacy .mac_select_pcs callback, phylink_major_config() sets pcs->phylink = pl, but that pointer is never cleared during teardown. If the PCS driver outlives the phylink instance and triggers phylink_pcs_change(), it would dereference the freed phylink pointer.

[ ... ]

Quoted 9 lines
drivers/net/phy/phylink.c +1 -0
@@ -2437,6 +2571,7 @@ static irqreturn_t phylink_link_handler(int irq, void *data)
  */
 void phylink_start(struct phylink *pl)
 {
+	struct phylink_pcs *pcs;
 	bool poll = false;
 
 	ASSERT_RTNL();

drivers/net/phy/phylink.c void phylink_start(struct phylink *pl) · line 2604

Quoted 7 lines
drivers/net/phy/phylink.c +4 -0
@@ -2463,6 +2598,10 @@ void phylink_start(struct phylink *pl)
 
 	pl->pcs_state = PCS_STATE_STARTED;
 
+	/* link available PCS to phylink struct */
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		pcs->phylink = pl;
+

[Severity: High]
Does attaching all available PCSs to the phylink instance create a risk of spurious link changes? Since all PCSs in the list receive the phylink pointer, any of them can call phylink_pcs_change(). Because phylink_pcs_change() does not verify if the calling PCS is the actively selected one (pl->pcs), couldn't an interrupt from an inactive PCS (like a loss of signal) falsely trigger a link state change and disrupt the active connection?

[ ... ]

Quoted 12 lines
drivers/net/phy/phylink.c +2 -0
@@ -2507,6 +2646,8 @@ EXPORT_SYMBOL_GPL(phylink_start);
  */
 void phylink_stop(struct phylink *pl)
 {
+	struct phylink_pcs *pcs;
+
 	ASSERT_RTNL();
 
 	if (pl->sfp_bus)
@@ -2524,6 +2665,14 @@ void phylink_stop(struct phylink *pl)
 	pl->pcs_state = PCS_STATE_DOWN;
 

drivers/net/phy/phylink.c void phylink_stop(struct phylink *pl) · line 2676

Quoted 10 lines
drivers/net/phy/phylink.c +8 -0
 	phylink_pcs_disable(pl->pcs);
+
+	/* Drop link between phylink and PCS */
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		pcs->phylink = NULL;
+
+	/* Restore original supported interfaces */
+	phy_interface_copy(pl->supported_interfaces,
+			   pl->config->supported_interfaces);
 }

[Severity: High]
Will unconditionally clearing the supported interfaces permanently break PCS capabilities on a link down/up cycle? During phylink_create(), pl->supported_interfaces aggregates capabilities from all attached PCSs. When phylink_stop() resets this to the base MAC capabilities, the PCS capabilities are lost because phylink_start() does not reconstruct the aggregated mask. Could this prevent the link from coming back up in modes that rely on a PCS after the interface is bounced?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

PATCH v6 3/12 net: phylink: add phylink_release_pcs() to externally release a PCS

In reply to Christian Marangi

Add phylink_release_pcs() to externally release a PCS from a phylink instance. This can be used to handle case when a single PCS needs to be removed and the phylink instance needs to be refreshed.

On calling phylink_release_pcs(), the PCS will be removed from the phylink internal PCS list and the phylink supported_interfaces value is reparsed with the remaining PCS interfaces.

Also a phylink resolve is triggered to handle the PCS removal.

The flag force_major_config is set to make phylink resolve reconfigure the interface (even if it didn't change) is also added.
This is needed to handle the special case when the current PCS used by phylink is removed and a major_config is needed to propagae the configuration change. With this option enabled we also force mac_config even if the PHY link is not up for the in-band case.

---
 drivers/net/phy/phylink.c | 49 +++++++++++++++++++++++++++++++++++++++
 include/linux/phylink.h   |  2 ++
 2 files changed, 51 insertions(+)
drivers/net/phy/phylink.c +49 -0
index 4d6ffda0cdd6..b45bee927300 100644
@@ -923,6 +923,55 @@ static void phylink_resolve_an_pause(struct phylink_link_state *state)
 	}
 }
 
+/**
+ * phylink_release_pcs - Removes a PCS from the phylink PCS available list
+ * @pcs: a pointer to the phylink_pcs struct to be released
+ *
+ * This function release a PCS from the phylink PCS available list if
+ * actually in use. It also refreshes the supported interfaces of the
+ * phylink instance by copying the supported interfaces from the phylink
+ * conf and merging the supported interfaces of the remaining available PCS
+ * in the list and trigger a resolve.
+ */
+void phylink_release_pcs(struct phylink_pcs *pcs)
+{
+	struct phylink *pl;
+
+	ASSERT_RTNL();
+
+	pl = pcs->phylink;
+	if (!pl)
+		return;
+
+	list_del(&pcs->list);
+	pcs->phylink = NULL;
+
+	/* Check if we are removing the PCS currently
+	 * in use by phylink. If this is the case,
+	 * force phylink resolve to reconfigure the interface
+	 * mode and set the phylink PCS to NULL.
+	 */
+	if (pl->pcs == pcs) {
+		mutex_lock(&pl->state_mutex);
+
+		pl->force_major_config = true;
+		pl->pcs = NULL;
+
+		mutex_unlock(&pl->state_mutex);
+	}
+
+	/* Refresh supported interfaces */
+	phy_interface_copy(pl->supported_interfaces,
+			   pl->config->supported_interfaces);
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		phy_interface_or(pl->supported_interfaces,
+				 pl->supported_interfaces,
+				 pcs->supported_interfaces);
+
+	phylink_run_resolve(pl);
+}
+EXPORT_SYMBOL_GPL(phylink_release_pcs);
+
 static unsigned int phylink_pcs_inband_caps(struct phylink_pcs *pcs,
 				    phy_interface_t interface)
 {
include/linux/phylink.h +2 -0
index 3387d308c4ad..b65f91c78d8c 100644
@@ -751,6 +751,8 @@ void phylink_disconnect_phy(struct phylink *);
 int phylink_set_fixed_link(struct phylink *,
 			   const struct phylink_link_state *);
 
+void phylink_release_pcs(struct phylink_pcs *pcs);
+
 void phylink_mac_change(struct phylink *, bool up);
 void phylink_pcs_change(struct phylink_pcs *, bool up);
 
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH net-next v6 03/12] net: phylink: add phylink_release_pcs() to externally release a PCS

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Unsynchronized modification of `pl->pcs_list` and TOCTOU access to `pl->pcs` in…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Unsynchronized modification of pl->pcs_list and TOCTOU access to pl->pcs in phylink_release_pcs() leads to data races, list corruption, and use-after-free.
- [High] phylink_release_pcs() silently fails to remove the PCS from pl->pcs_list if the interface is stopped, leading to a Use-After-Free.
- [Medium] Releasing an actively running PCS bypasses phylink_pcs_disable(), leaving the hardware permanently enabled.
--

commit ef373ddee33a54496ae5ebacb3521d9195428672
Author: Christian Marangi <[email protected]>

net: phylink: add phylink_release_pcs() to externally release a PCS

Add phylink_release_pcs() to externally release a PCS from a phylink instance. This can be used to handle case when a single PCS needs to be removed and the phylink instance needs to be refreshed.

Quoted 17 lines
drivers/net/phy/phylink.c +9 -0
index 4d6ffda0cdd61..b45bee9273007 100644
@@ -923,6 +923,55 @@ static void phylink_resolve_an_pause(struct phylink_link_state *state)
 	}
 }
 
+/**
+ * phylink_release_pcs - Removes a PCS from the phylink PCS available list
+ * @pcs: a pointer to the phylink_pcs struct to be released
+ *
+ * This function release a PCS from the phylink PCS available list if
+ * actually in use. It also refreshes the supported interfaces of the
+ * phylink instance by copying the supported interfaces from the phylink
+ * conf and merging the supported interfaces of the remaining available PCS
+ * in the list and trigger a resolve.

drivers/net/phy/phylink.c static void phylink_resolve_an_pause(struct phylink_link_state *state) · line 944

Quoted 10 lines
drivers/net/phy/phylink.c +10 -0
+ */
+void phylink_release_pcs(struct phylink_pcs *pcs)
+{
+	struct phylink *pl;
+
+	ASSERT_RTNL();
+
+	pl = pcs->phylink;
+	if (!pl)
+		return;

[Severity: High]
Does this early return cause a use-after-free if the interface is stopped?

When the interface is brought down, phylink_stop() clears pcs->phylink = NULL for all available PCS instances, while keeping them linked in pl->pcs_list.

If phylink_release_pcs() is called while the interface is stopped, it encounters a NULL pointer here and returns immediately without executing list_del(&pcs->list). The caller likely assumes the PCS was successfully detached and frees its memory, leaving a dangling pointer in pl->pcs_list.

When the interface is later brought up, phylink_start() will iterate over pl->pcs_list and dereference the freed memory.

drivers/net/phy/phylink.c static void phylink_resolve_an_pause(struct phylink_link_state *state) · line 954

Quoted 10 lines
drivers/net/phy/phylink.c +10 -0
+
+	list_del(&pcs->list);
+	pcs->phylink = NULL;
+
+	/* Check if we are removing the PCS currently
+	 * in use by phylink. If this is the case,
+	 * force phylink resolve to reconfigure the interface
+	 * mode and set the phylink PCS to NULL.
+	 */
+	if (pl->pcs == pcs) {

[Severity: High]
Can modifying the list and checking pl->pcs outside the state_mutex race with the workqueue?

While this function enforces the RTNL lock, pl->pcs_list is concurrently traversed by phylink_major_config() from the phylink_resolve workqueue, which holds only pl->state_mutex. Modifying the list outside of pl->state_mutex could cause the workqueue to dereference LIST_POISON1.

Additionally, checking if (pl->pcs == pcs) before acquiring state_mutex creates a Time-of-Check to Time-of-Use race. A concurrent phylink_resolve could update pl->pcs between the lockless check and acquiring state_mutex, causing this code to inadvertently clear a newly configured PCS.

drivers/net/phy/phylink.c static void phylink_resolve_an_pause(struct phylink_link_state *state) · line 958

Quoted 4 lines
drivers/net/phy/phylink.c +4 -0
+		mutex_lock(&pl->state_mutex);
+
+		pl->force_major_config = true;
+		pl->pcs = NULL;

[Severity: Medium]
If pl->pcs is cleared to NULL here, will the released PCS ever be cleanly disabled?

During the subsequent reconfiguration triggered by phylink_run_resolve(), phylink_major_config() detects a PCS change and attempts to disable the old PCS by calling phylink_pcs_disable(pl->pcs).

However, since pl->pcs was already cleared to NULL here, the pcs->ops->pcs_disable() callback will never be invoked for the released PCS, leaving it hardware-enabled.

Quoted 15 lines
drivers/net/phy/phylink.c +15 -0
+
+		mutex_unlock(&pl->state_mutex);
+	}
+
+	/* Refresh supported interfaces */
+	phy_interface_copy(pl->supported_interfaces,
+			   pl->config->supported_interfaces);
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		phy_interface_or(pl->supported_interfaces,
+				 pl->supported_interfaces,
+				 pcs->supported_interfaces);
+
+	phylink_run_resolve(pl);
+}
+EXPORT_SYMBOL_GPL(phylink_release_pcs);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#8 lore raw

PATCH v6 4/12 net: pcs: implement Firmware node support for PCS driver

In reply to Christian Marangi

Implement the foundation of Firmware node support for PCS driver.

To support this, implement a simple Provider API where a PCS driver can expose multiple PCS with an xlate .get function.

PCS driver will have to call fwnode_pcs_add_provider() and pass the firmware node pointer and a xlate function to return the correct PCS for the passed #pcs-cells.

This will register the PCS in a global list of providers so that consumer can access it.

The consumer will then use fwnode_pcs_get() to get the actual PCS by passing the firmware node pointer and the index for #pcs-cells.

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

For an advanced implementation a custom xlate function is required.

One removal the PCS driver should first delete itself from the provider list using fwnode_pcs_del_provider() and then call phylink_release_pcs() on every PCS the driver provides.

A generic function fwnode_phylink_pcs_parse() is provided for MAC driver that will declare PCS in DT (or ACPI).
This function will parse "pcs-handle" property and fill the passed array with the parsed PCS in available_pcs up to the passed num_pcs value. It's also possible to pass NULL as array to only parse the PCS and update the num_pcs value with the count of scanned PCS.

---
 drivers/net/pcs/Kconfig          |   6 +
 drivers/net/pcs/Makefile         |   1 +
 drivers/net/pcs/pcs.c            | 201 +++++++++++++++++++++++++++++++
 include/linux/pcs/pcs-provider.h |  41 +++++++
 include/linux/pcs/pcs.h          |  56 +++++++++
 5 files changed, 305 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
drivers/net/pcs/Kconfig +6 -0
index e417fd66f660..2ce89d4bff6b 100644
@@ -5,6 +5,12 @@
 
 menu "PCS device drivers"
 
+config FWNODE_PCS
+	bool "PCS Firmware Node"
+	depends on (ACPI || OF)
+	help
+		Firmware node PCS accessors
+
 config PCS_XPCS
 	tristate "Synopsys DesignWare Ethernet XPCS"
 	select PHYLINK
drivers/net/pcs/Makefile +1 -0
index 4f7920618b90..3005cdd89ab7 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 # Makefile for Linux PCS drivers
 
+obj-$(CONFIG_FWNODE_PCS)	+= pcs.o
 pcs_xpcs-$(CONFIG_PCS_XPCS)	:= pcs-xpcs.o pcs-xpcs-plat.o \
 				   pcs-xpcs-nxp.o pcs-xpcs-wx.o
 
drivers/net/pcs/pcs.c new file +201 -0

201 lines not on this page · raw message

include/linux/pcs/pcs-provider.h new file +41 -0

41 lines not on this page · raw message

include/linux/pcs/pcs.h new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH net-next v6 04/12] net: pcs: implement Firmware node support for PCS driver

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Unprotected list modification and iteration in phylink PCS list handling causes race conditions.…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Unprotected list modification and iteration in phylink PCS list handling causes race conditions.
- [Medium] fwnode_pcs_get_from_pcsspec() incorrectly returns -ENODEV instead of -EPROBE_DEFER when a provider is not found.
- [Low] fwnode_parse_pcsspec() passes a negative error code as an unsigned index, masking the original error and causing out-of-bounds lookups. --

commit 436ed2d38da7824de31cb0cb85a006f98cbdeb4b
Author: Christian Marangi <[email protected]>

net: pcs: implement Firmware node support for PCS driver

This commit implements the foundation for firmware node support in PCS drivers. It introduces a simple provider API where a PCS driver can expose multiple PCS instances with a get function. It also adds a generic parsing function for MAC drivers to declare PCS in DT or ACPI.

Quoted 5 lines
drivers/net/pcs/pcs.c new file +0 -0
new file mode 100644
index 0000000000000..72f56f55d198d

[ ... ]

Quoted 6 lines
drivers/net/pcs/pcs.c new file +6 -0
+static int fwnode_parse_pcsspec(const struct fwnode_handle *fwnode, int index,
+				const char *name,
+				struct fwnode_reference_args *out_args)
+{
+	int ret;
+

drivers/net/pcs/pcs.c line 98

Quoted 10 lines
drivers/net/pcs/pcs.c new file +10 -0
+	if (!fwnode)
+		return -ENOENT;
+
+	if (name)
+		index = fwnode_property_match_string(fwnode, "pcs-names",
+						     name);
+
+	ret = fwnode_property_get_reference_args(fwnode, "pcs-handle",
+						 "#pcs-cells",
+						 -1, index, out_args);

[Severity: Low]
If name is provided but fwnode_property_match_string() fails, it returns a negative error code. Are we passing this negative value directly into fwnode_property_get_reference_args(), which expects an unsigned int?

This could wrap around to a large positive integer, causing an out-of-bounds fetch attempt that internally fails with -ENOENT. The original error code would therefore be masked by -ENOENT.

Should this function check if (index < 0) return index; before proceeding?

drivers/net/pcs/pcs.c line 109

Quoted 11 lines
drivers/net/pcs/pcs.c new file +11 -0
+	if (ret || (name && index < 0))
+		return ret;
+
+	return 0;
+}
+
+static struct phylink_pcs *
+fwnode_pcs_get_from_pcsspec(struct fwnode_reference_args *pcsspec)
+{
+	struct fwnode_pcs_provider *provider;
+	struct phylink_pcs *pcs = ERR_PTR(-ENODEV);

[Severity: Medium]
As stated in the kernel doc for fwnode_pcs_get(), it must return -EPROBE_DEFER if the PCS is not present in the global providers list. Is it intentional that fwnode_pcs_get_from_pcsspec() defaults to returning -ENODEV here when a provider is not found?

This could break the standard kernel probe deferral mechanism for the consumer driver, preventing it from deferring its probe to wait for the PCS driver to load if fw_devlink is disabled or bypassed.

Quoted 6 lines
drivers/net/pcs/pcs.c new file +6 -0
+
+	if (!pcsspec)
+		return ERR_PTR(-EINVAL);
+
+	mutex_lock(&fwnode_pcs_mutex);
+	list_for_each_entry(provider, &fwnode_pcs_providers, link) {

drivers/net/pcs/pcs.c line 125

Quoted 10 lines
drivers/net/pcs/pcs.c new file +10 -0
+		if (provider->fwnode == pcsspec->fwnode) {
+			pcs = provider->get(pcsspec, provider->data);
+			if (!IS_ERR(pcs))
+				break;
+		}
+	}
+	mutex_unlock(&fwnode_pcs_mutex);
+
+	return pcs;
+}

Regarding the integration of this feature with the phylink subsystem, is there a risk of unprotected list modification and iteration causing a race condition?

[Severity: High]
The pl->pcs_list appears to be protected by mismatched locks. In phylink_release_pcs() and pcs_provider_notify(), list modifications are performed under rtnl_lock() but without holding pl->state_mutex:

drivers/net/phy/phylink.c:phylink_release_pcs() {
    ...
    list_del(&pcs->list);
    ...
}

Concurrently, the phylink_resolve() workqueue executes phylink_major_config(), which iterates over pl->pcs_list using list_for_each_entry() while holding pl->state_mutex but without holding rtnl_lock():

drivers/net/phy/phylink.c:phylink_major_config() {
    ...
    list_for_each_entry(pcs, &pl->pcs_list, list) {
    ...
}

Since list modification and iteration are not synchronized by a common lock, could this lead to linked-list corruption, torn reads, and use-after-free crashes if the workqueue accesses a PCS that is being removed?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#10 lore raw

PATCH v6 5/12 net: phylink: support late PCS provider attach

In reply to Christian Marangi

Add support for late PCS provider attachment to a phylink instance. This works by creating a global notifier for the PCS provider and making each phylink instance that makes use of fwnode subscribe to this notifier.

The PCS notifier will emit the event FWNODE_PCS_PROVIDER_ADD every time a new PCS provider is added.

phylink will then react to this event and will call the new function fwnode_phylink_pcs_get_from_fwnode() that will check if the PCS fwnode provided by the event is present in the pcs-handle property of the phylink instance.

If a related PCS is found, then such PCS is added to the phylink instance PCS list.

Then we link the PCS to the phylink instance if it's not disabled and we refresh the supported interfaces of the phylink instance.

Finally we check if we are in a major_config_failed scenario and trigger an interface reconfiguration in the next phylink resolve.

In the example scenario where the link was previously torn down due to removal of PCS, the link will be established again as the PCS came back and is now available to phylink.

---
 drivers/net/pcs/pcs.c     | 40 ++++++++++++++++++++++++++++++
 drivers/net/phy/phylink.c | 52 +++++++++++++++++++++++++++++++++++++++
 include/linux/pcs/pcs.h   | 48 ++++++++++++++++++++++++++++++++++++
 3 files changed, 140 insertions(+)
drivers/net/pcs/pcs.c +40 -0
index 72f56f55d198..911d95cf1b09 100644
@@ -22,6 +22,13 @@ 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);
 
 struct phylink_pcs *fwnode_pcs_simple_get(struct fwnode_reference_args *pcsspec,
 					  void *data)
@@ -55,6 +62,10 @@ int fwnode_pcs_add_provider(struct fwnode_handle *fwnode,
 
 	fwnode_dev_initialized(fwnode, true);
 
+	blocking_notifier_call_chain(&fwnode_pcs_notify_list,
+				     FWNODE_PCS_PROVIDER_ADD,
+				     fwnode);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(fwnode_pcs_add_provider);
@@ -147,6 +158,35 @@ struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, int index)
 }
 EXPORT_SYMBOL_GPL(fwnode_pcs_get);
 
+struct phylink_pcs *
+fwnode_phylink_pcs_get_from_fwnode(struct fwnode_handle *fwnode,
+				   struct fwnode_handle *pcs_fwnode)
+{
+	struct fwnode_reference_args pcsspec;
+	int index = 0;
+	int ret;
+
+	/* Loop until we find a matching PCS node or
+	 * fwnode_parse_pcsspec() returns error
+	 * if we don't have any other PCS reference to check.
+	 */
+	while (true) {
+		ret = fwnode_parse_pcsspec(fwnode, index, NULL, &pcsspec);
+		if (ret)
+			return ERR_PTR(ret);
+
+		/* Exit loop if we found the matching PCS node */
+		if (pcsspec.fwnode == pcs_fwnode)
+			break;
+
+		/* Check the next PCS reference */
+		index++;
+	}
+
+	return fwnode_pcs_get(fwnode, index);
+}
+EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_get_from_fwnode);
+
 static int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode,
 				    unsigned int *num_pcs)
 {
drivers/net/phy/phylink.c +52 -0

76 lines not on this page · raw message

include/linux/pcs/pcs.h +48 -0

73 lines not on this page · raw message

Signature · 1 line
2.53.0
#11 lore raw

Re: [PATCH net-next v6 05/12] net: phylink: support late PCS provider attach

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Use-After-Free due to missing notifier unregistration when phylink instance is destroyed. - [High]…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Use-After-Free due to missing notifier unregistration when phylink instance is destroyed.
- [High] Use-After-Free due to incomplete PCS detachment logic when the phylink instance is stopped.
- [High] Data race between pcs_provider_notify() and asynchronous readers leads to pl->pcs_list corruption.
- [Medium] Memory leak of fwnode_handle references in fwnode_phylink_pcs_get_from_fwnode(). --

commit e4be489886d6b3bd45151876412cae4ab8aa7956
Author: Christian Marangi <[email protected]>

net: phylink: support late PCS provider attach

This commit adds support for late PCS provider attachment to a phylink instance. It implements a global notifier for the PCS provider, allowing phylink instances to subscribe and attach the PCS when it becomes available.

Quoted 4 lines
drivers/net/pcs/pcs.c +0 -0
index 72f56f55d198d..911d95cf1b09c 100644

[ ... ]

Quoted 14 lines
drivers/net/pcs/pcs.c +10 -0
@@ -147,6 +158,35 @@ struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, int index)
 }
 EXPORT_SYMBOL_GPL(fwnode_pcs_get);
 
+struct phylink_pcs *
+fwnode_phylink_pcs_get_from_fwnode(struct fwnode_handle *fwnode,
+				   struct fwnode_handle *pcs_fwnode)
+{
+	struct fwnode_reference_args pcsspec;
+	int index = 0;
+	int ret;
+
+	/* Loop until we find a matching PCS node or
+	 * fwnode_parse_pcsspec() returns error

drivers/net/pcs/pcs.c struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, int index) · line 180

Quoted 10 lines
drivers/net/pcs/pcs.c +10 -0
+	 * if we don't have any other PCS reference to check.
+	 */
+	while (true) {
+		ret = fwnode_parse_pcsspec(fwnode, index, NULL, &pcsspec);
+		if (ret)
+			return ERR_PTR(ret);
+
+		/* Exit loop if we found the matching PCS node */
+		if (pcsspec.fwnode == pcs_fwnode)
+			break;

[Severity: Medium]
Does this leak a fwnode_handle reference?

fwnode_parse_pcsspec() internally acquires a reference to pcsspec.fwnode. However, on both the break path (when a match is found) and the loop continuation path, this reference is never released with fwnode_handle_put().

Will this leak a reference for every successfully parsed PCS specification?

Quoted 8 lines
drivers/net/pcs/pcs.c +8 -0
+
+		/* Check the next PCS reference */
+		index++;
+	}
+
+	return fwnode_pcs_get(fwnode, index);
+}
+EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_get_from_fwnode);

[ ... ]

Quoted 4 lines
drivers/net/phy/phylink.c +0 -0
index b45bee9273007..65b67c33e12fd 100644

[ ... ]

Quoted 13 lines
drivers/net/phy/phylink.c +9 -0
@@ -1998,6 +2000,51 @@ static int phylink_fill_available_pcs(struct phylink *pl,
 	return ret;
 }
 
+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_handle *pcs_fwnode = data;
+	struct phylink_pcs *pcs;
+
+	/* Check if the just added PCS provider is
+	 * in the phylink instance pcs-handle property.

drivers/net/phy/phylink.c static int phylink_fill_available_pcs(struct phylink *pl, · line 2021

Quoted 10 lines
drivers/net/phy/phylink.c +10 -0
+	 */
+	pcs = fwnode_phylink_pcs_get_from_fwnode(dev_fwnode(pl->config->dev),
+						 pcs_fwnode);
+	if (IS_ERR(pcs))
+		return NOTIFY_DONE;
+
+	/* Add the PCS */
+	rtnl_lock();
+
+	list_add(&pcs->list, &pl->pcs_list);

[Severity: High]
Is it safe to mutate this list while only holding the rtnl_lock?

pcs_provider_notify() adds a new PCS to pl->pcs_list under the rtnl_lock().

However, asynchronous readers like phylink_major_config() executed from the phylink_resolve workqueue traverse this same list using list_for_each_entry() while only holding pl->state_mutex.

Since list_add() is not atomic and the locks are disjoint, can concurrent access cause readers to traverse a partially updated list structure?

drivers/net/phy/phylink.c static int phylink_fill_available_pcs(struct phylink *pl, · line 2025

Quoted 4 lines
drivers/net/phy/phylink.c +4 -0
+
+	/* Link phylink if we are started */
+	if (!pl->phylink_disable_state)
+		pcs->phylink = pl;

[Severity: High]
Does this create a use-after-free when a phylink instance is stopped?

When the interface is stopped, phylink_stop() clears pcs->phylink by setting it to NULL for every entry in pl->pcs_list.

If the underlying PCS driver is unbound while stopped, it calls phylink_release_pcs(), which checks if pcs->phylink is NULL and returns early, failing to remove the PCS from pl->pcs_list:

phylink_release_pcs() {
    pl = pcs->phylink;
    if (!pl)
        return;
    list_del(&pcs->list);
}

Will this leave a freed PCS structure dangling in pl->pcs_list which could be traversed when phylink_start() is called later?

Quoted 8 lines
drivers/net/phy/phylink.c +8 -0
+
+	/* Refresh supported interfaces */
+	phy_interface_copy(pl->supported_interfaces,
+			   pl->config->supported_interfaces);
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		phy_interface_or(pl->supported_interfaces,
+				 pl->supported_interfaces,
+				 pcs->supported_interfaces);

[ ... ]

drivers/net/phy/phylink.c struct phylink *phylink_create(struct phylink_config *config, · line 2106

Quoted 8 lines
drivers/net/phy/phylink.c +4 -0
@@ -2053,6 +2100,11 @@ struct phylink *phylink_create(struct phylink_config *config,
 				 pl->supported_interfaces,
 				 pcs->supported_interfaces);
 
+	if (!phy_interface_empty(config->pcs_interfaces)) {
+		pl->fwnode_pcs_nb.notifier_call = pcs_provider_notify;
+		register_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);
+	}

[Severity: High]
If the phylink instance is destroyed, is this notifier ever unregistered?

If phylink_destroy() frees the phylink instance but doesn't unregister the notifier block, will it leave a dangling pointer in the global fwnode_pcs_notify_list? If so, won't this cause a kernel panic when the next PCS provider is added?

Quoted 4 lines
drivers/net/phy/phylink.c +1 -0
+
 	pl->config = config;
 	if (config->type == PHYLINK_NETDEV) {
 		pl->netdev = to_net_dev(config->dev);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#12 lore raw

PATCH v6 6/12 net: Document PCS subsystem

In reply to Christian Marangi

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

---
 Documentation/networking/index.rst |   1 +
 Documentation/networking/pcs.rst   | 228 +++++++++++++++++++++++++++++
 2 files changed, 229 insertions(+)
 create mode 100644 Documentation/networking/pcs.rst
Documentation/networking/index.rst +1 -0
index 44a422ad3b05..3fce8f6ac089 100644
@@ -28,6 +28,7 @@ Contents:
    net_failover
    page_pool
    phy
+   pcs
    sfp-phylink
    alias
    bridge
Documentation/networking/pcs.rst new file +228 -0

228 lines not on this page · raw message

Signature · 1 line
2.53.0
#13 lore raw

Re: [PATCH net-next v6 06/12] net: Document PCS subsystem

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Late PCS registration handling is structurally broken and causes `phylink_create` to fail…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Late PCS registration handling is structurally broken and causes phylink_create to fail permanently.
- [Low] Multiple grammatical and spelling errors in the PCS subsystem documentation.
--

commit 84279427a07ae5692e53d4ed76f418a074b2aea9
Author: Christian Marangi <[email protected]>

net: Document PCS subsystem

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

Quoted 5 lines
Documentation/networking/pcs.rst new file +0 -0
new file mode 100644
index 0000000000000..9436ba43cebdf

[ ... ]

Documentation/networking/pcs.rst line 39

Quoted 6 lines
Documentation/networking/pcs.rst new file +6 -0
+Looking up PCS Devices (fwnode implementation)
+-----------------------------------------------
+
+The lookup of a PCS device follows the common producer/consumer implementation
+used by similar subsystem with a ``#pcs-cells`` on the producer and a
+``pcs-handle`` property on the consumer::

[Severity: Low]
This isn't a bug, but there are a few minor grammatical and spelling errors in the newly added documentation. Should "subsystem" be "subsystems" here?

Additionally, a few other minor typos are present in this document:
- "The fwnode implementation expose" -> "exposes"
- "should be used to relase them" -> "release"
- "takes three arg" -> "takes three arguments"

[ ... ]

Quoted 4 lines
Documentation/networking/pcs.rst new file +4 -0
+Late PCS registration handling
+------------------------------
+
+It's possible that a PCS becomes available after the MAC finished probing.

Documentation/networking/pcs.rst line 201

Quoted 10 lines
Documentation/networking/pcs.rst new file +10 -0
+Contrary to the usual producer/consumer implementation, when a PCS is not
+registered and can't be found, the fwnode parser helper returns ``-EINVAL``
+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.

[Severity: High]
Is this description of the failure state accurate with the current implementation?

Looking at phylink_create() in drivers/net/phy/phylink.c, if a PCS is missing, it appears to abort the creation entirely:

phylink_create() {
    ...
    ret = phylink_fill_available_pcs(pl, config);
    if (ret) {
        kfree(pl);
        return ERR_PTR(ret);
    }
    ...
}

Similarly, if fwnode_phylink_pcs_parse() in drivers/net/pcs/pcs.c cannot find a PCS, it returns an error:

fwnode_phylink_pcs_parse() {
    ...
    pcs = fwnode_pcs_get(fwnode, i);
    if (IS_ERR(pcs))
        return PTR_ERR(pcs);
    ...
}

If this error propagates up through the MAC driver's fill_available_pcs() callback to phylink_fill_available_pcs(), wouldn't phylink_create() immediately free the phylink instance and return the error before the late registration notifier is ever registered?

Could you clarify how the instance is kept in a fail condition if it gets freed during phylink_create()?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#14 lore raw

PATCH v6 7/12 MAINTAINERS: add myself as PCS subsystem maintainer

In reply to Christian Marangi

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

---
 MAINTAINERS | 9 +++++++++
 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
index eb8cdcc76324..8174f005ab43 100644
@@ -9593,6 +9593,15 @@ F:	include/uapi/linux/if_bridge.h
 F:	include/linux/netfilter_bridge/
 F:	net/bridge/
 
+ETHERNET PCS SUBSYSTEM
+M:	Christian Marangi <[email protected]>
+L:	[email protected]
+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 <[email protected]>
 M:	Heiner Kallweit <[email protected]>
Signature · 1 line
2.53.0
#15 lore raw

PATCH v6 8/12 of: property: fw_devlink: Add support for "pcs-handle"

In reply to Christian Marangi

Add support for parsing PCS binding so that fw_devlink can enforce the dependency with Ethernet port.

---
 drivers/of/property.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/of/property.c +2 -0
index 136946f8b746..e6584a2f705d 100644
@@ -1392,6 +1392,7 @@ DEFINE_SIMPLE_PROP(access_controllers, "access-controllers", "#access-controller
 DEFINE_SIMPLE_PROP(pses, "pses", "#pse-cells")
 DEFINE_SIMPLE_PROP(power_supplies, "power-supplies", NULL)
 DEFINE_SIMPLE_PROP(mmc_pwrseq, "mmc-pwrseq", NULL)
+DEFINE_SIMPLE_PROP(pcs_handle, "pcs-handle", "#pcs-cells")
 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
 
@@ -1548,6 +1549,7 @@ static const struct supplier_bindings of_supplier_bindings[] = {
 	{ .parse_prop = parse_interrupts, },
 	{ .parse_prop = parse_interrupt_map, },
 	{ .parse_prop = parse_access_controllers, },
+	{ .parse_prop = parse_pcs_handle, },
 	{ .parse_prop = parse_regulators, },
 	{ .parse_prop = parse_gpio, },
 	{ .parse_prop = parse_gpios, },
Signature · 1 line
2.53.0
maintainer
#16 lore raw

Re: [PATCH net-next v6 08/12] of: property: fw_devlink: Add support for "pcs-handle"

Quoted 14 lines · Christian Marangi

Add support for parsing PCS binding so that fw_devlink can enforce the dependency with Ethernet port.

---
 drivers/of/property.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/of/property.c +0 -0
index 136946f8b746..e6584a2f705d 100644

drivers/of/property.c DEFINE_SIMPLE_PROP(access_controllers, "access-controllers", "#access-controller · line 1395

Quoted 4 lines
drivers/of/property.c +1 -0
@@ -1392,6 +1392,7 @@ DEFINE_SIMPLE_PROP(access_controllers, "access-controllers", "#access-controller
 DEFINE_SIMPLE_PROP(pses, "pses", "#pse-cells")
 DEFINE_SIMPLE_PROP(power_supplies, "power-supplies", NULL)
 DEFINE_SIMPLE_PROP(mmc_pwrseq, "mmc-pwrseq", NULL)
+DEFINE_SIMPLE_PROP(pcs_handle, "pcs-handle", "#pcs-cells")

There is no such common property "#pcs-cells".

Quoted 14 lines
drivers/of/property.c +1 -1
 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
 
@@ -1548,6 +1549,7 @@ static const struct supplier_bindings of_supplier_bindings[] = {
        { .parse_prop = parse_interrupts, },
        { .parse_prop = parse_interrupt_map, },
        { .parse_prop = parse_access_controllers, },
+       { .parse_prop = parse_pcs_handle, },
        { .parse_prop = parse_regulators, },
        { .parse_prop = parse_gpio, },
        { .parse_prop = parse_gpios, },
--

2.53.0

#17 lore raw

PATCH v6 9/12 net: phylink: add .pcs_link_down PCS OP

In reply to Christian Marangi

Permit for PCS driver to define specific operation to torn 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 before .mac_link_down to torn down the link.

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

---
 drivers/net/phy/phylink.c | 8 ++++++++
 include/linux/phylink.h   | 2 ++
 2 files changed, 10 insertions(+)
drivers/net/phy/phylink.c +8 -0
index 65b67c33e12f..38428b375eca 100644
@@ -1036,6 +1036,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)
@@ -1735,6 +1741,8 @@ static void phylink_link_down(struct phylink *pl)
 
 	phylink_deactivate_lpi(pl);
 
+	phylink_pcs_link_down(pl->pcs);
+
 	pl->mac_ops->mac_link_down(pl->config, pl->act_link_an_mode,
 				   pl->cur_interface);
 	phylink_info(pl, "Link is Down\n");
include/linux/phylink.h +2 -0
index b65f91c78d8c..79a1496fa519 100644
@@ -528,6 +528,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: torn 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
@@ -555,6 +556,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);
Signature · 1 line
2.53.0
#18 lore raw

Re: [PATCH net-next v6 09/12] net: phylink: add .pcs_link_down PCS OP

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] Memory leak/Use-After-Free due to missing notifier unregistration. - [High] Hardware Teardown…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] Memory leak/Use-After-Free due to missing notifier unregistration.
- [High] Hardware Teardown Sequence Error violating symmetric LIFO teardown.
- [High] Data Race and list corruption in pcs_list.
- [Low] Incorrect grammar in callback documentation.
--

commit db8b84b7398b2a4cfe29ace55d1aefced615f8e2
Author: Christian Marangi <[email protected]>

net: phylink: add .pcs_link_down PCS OP

This commit introduces a new pcs_link_down callback for PCS drivers to define specific teardown operations. It updates phylink_link_down to invoke this new operation before taking down the MAC. This allows PCS drivers to perform necessary resets or counter clearing when the link goes down.

Quoted 4 lines
drivers/net/phy/phylink.c +0 -0
index 65b67c33e12fd..38428b375eca3 100644

[ ... ]

drivers/net/phy/phylink.c static void phylink_link_down(struct phylink *pl) · line 1747

Quoted 8 lines
drivers/net/phy/phylink.c +2 -0
@@ -1735,6 +1741,8 @@ static void phylink_link_down(struct phylink *pl)
 
 	phylink_deactivate_lpi(pl);
 
+	phylink_pcs_link_down(pl->pcs);
+
 	pl->mac_ops->mac_link_down(pl->config, pl->act_link_an_mode,
 				   pl->cur_interface);

[Severity: High]
Does this break the symmetric LIFO teardown order? During phylink_link_up(), the PCS is initialized before the MAC. Calling phylink_pcs_link_down() before mac_link_down() leaves the MAC nominally active while the underlying PCS connection is severed. Could this inconsistency lead to MAC hardware lockups, AXI bus hangs, or transmit queue timeouts?

include/linux/phylink.h struct phylink_pcs { · line 531

Quoted 9 lines
include/linux/phylink.h +1 -0
index b65f91c78d8cc..79a1496fa519c 100644
@@ -528,6 +528,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: torn down link between MAC and PCS.

[Severity: Low]
this isn't a bug, but there is a grammar mistake here (torn down instead of tear down).

[Severity: Critical]
Is there a missing notifier unregistration for the fwnode_pcs_nb block? Looking at phylink_create(), it registers the embedded notifier block:

drivers/net/phy/phylink.c:phylink_create() {
        ...
        register_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);
        ...
}

However, phylink_destroy() and the error paths in phylink_create() do not seem to unregister it. When the phylink struct is freed via kfree(pl), the embedded notifier remains linked in the global chain. Will any subsequent PCS provider registration system-wide iterate over the freed memory and cause a kernel panic? (Note: introduced by the earlier patch net: phylink: support late PCS provider attach in the same series).

[Severity: High]
Is there a data race when mutating pcs_list in phylink_destroy()?

The pcs_provider_notify() callback asynchronously adds newly discovered PCS providers to pl->pcs_list while holding the rtnl_lock(). However, phylink_destroy() iterates over and deletes entries from pl->pcs_list without holding the rtnl_lock():

drivers/net/phy/phylink.c:phylink_destroy() {
        ...
        /* Remove every PCS from phylink PCS list */
        list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list)
                list_del(&pcs->list);
        ...
}

If there is a concurrent PCS provider registration via the global notifier chain, could this lead to unsynchronized mutations and list corruption? (Note: introduced by the earlier patch net: phylink: support late PCS provider attach in the same series).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#19 lore raw

PATCH v6 10/12 dt-bindings: net: pcs: Document support for Airoha Ethernet PCS

In reply to Christian Marangi

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.

---
 .../bindings/net/pcs/airoha,pcs.yaml          | 260 ++++++++++++++++++
 1 file changed, 260 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +260 -0
new file mode 100644
index 000000000000..98d8ab7a90f5
@@ -0,0 +1,260 @@
+# 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 <[email protected]>
+
+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: true
+
+  "#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>;
+    };
Signature · 1 line
2.53.0
#20 lore raw

Re: [PATCH net-next v6 10/12] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The `phys` property is defined without a `maxItems` or `items` constraint, allowing an unbounded…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The phys property is defined without a maxItems or items constraint, allowing an unbounded number of PHY phandles. --

commit 4a128a4fb15024e87f0aaa51566224ca71799731
Author: Christian Marangi <[email protected]>

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

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. This follow the new PCS provider with the use of #pcs-cells property.

Quoted 5 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +0 -0
new file mode 100644
index 0000000000000..98d8ab7a90f5f

[ ... ]

Quoted 12 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +12 -0
+properties:
+  compatible:
+    enum:
+      - airoha,an7581-pcs-eth
+      - airoha,an7581-pcs-pon
+      - airoha,an7581-pcs-pcie
+      - airoha,an7581-pcs-usb
+
+  reg:
+    minItems: 6
+    maxItems: 15
+

Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml line 38

Quoted 10 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +10 -0
+  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: true

[Severity: Low]
Does this phys declaration need a length constraint?

In dt-schema, standard array properties like phys require explicit length constraints (e.g., maxItems: 1 or an items list) to ensure the device tree node provides the exact number of resources the hardware expects. Defining phys: true permits the property but fails to enforce any bounds on the number of entries, allowing a device tree node with extraneous PHY phandles to silently pass schema validation.

Quoted 2 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +2 -0
+
+  "#pcs-cells": true
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#21 lore raw

PATCH v6 11/12 net: pcs: airoha: add PCS driver for Airoha AN7581 SoC

In reply to Christian Marangi

Add PCS driver for Airoha AN7581 SoC for Ethernet/PON/PCIe/USB SERDES and permit usage of external PHY or connected SFP cage. Supported modes are USXGMII, 10G-BASER, 2500BASE-X, 1000BASE-X and SGMII.

The driver probe and register the various needed registers and register as a PCS provider for fwnode usage.

---
 drivers/net/pcs/Kconfig                    |    2 +
 drivers/net/pcs/Makefile                   |    2 +
 drivers/net/pcs/airoha/Kconfig             |   12 +
 drivers/net/pcs/airoha/Makefile            |    7 +
 drivers/net/pcs/airoha/pcs-airoha-common.c | 1313 ++++++++++++
 drivers/net/pcs/airoha/pcs-airoha.h        | 1309 ++++++++++++
 drivers/net/pcs/airoha/pcs-an7581.c        | 2093 ++++++++++++++++++++
 7 files changed, 4738 insertions(+)
 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
drivers/net/pcs/Kconfig +2 -0
index 2ce89d4bff6b..10ac9af04594 100644
@@ -41,4 +41,6 @@ config PCS_RZN1_MIIC
 	  Renesas RZ/N1, RZ/N2H, and RZ/T2H SoCs. This PCS converts MII to
 	  RMII/RGMII, or can be set in pass-through mode for MII.
 
+source "drivers/net/pcs/airoha/Kconfig"
+
 endmenu
drivers/net/pcs/Makefile +2 -0
index 3005cdd89ab7..91593aa8d926 100644
@@ -9,3 +9,5 @@ obj-$(CONFIG_PCS_XPCS)		+= pcs_xpcs.o
 obj-$(CONFIG_PCS_LYNX)		+= pcs-lynx.o
 obj-$(CONFIG_PCS_MTK_LYNXI)	+= pcs-mtk-lynxi.o
 obj-$(CONFIG_PCS_RZN1_MIIC)	+= pcs-rzn1-miic.o
+
+obj-$(CONFIG_PCS_AIROHA)	+= airoha/
drivers/net/pcs/airoha/Kconfig new file +12 -0
new file mode 100644
index 000000000000..4ddd527b365f
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config PCS_AIROHA
+	tristate
+	select FWNODE_PCS
+
+config PCS_AIROHA_AN7581
+	tristate "Airoha AN7581 PCS driver"
+	select PCS_AIROHA
+	help
+	  This module provides helper to phylink for managing the Airoha
+	  AN7581 PCS for SoC Ethernet and PON SERDES.
drivers/net/pcs/airoha/Makefile new file +7 -0
new file mode 100644
index 000000000000..25cb8f090c21
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-y				:= pcs-airoha.o
+pcs-airoha-objs			:= pcs-airoha-common.o
+ifdef CONFIG_PCS_AIROHA_AN7581
+pcs-airoha-objs			+= pcs-an7581.o
+endif
drivers/net/pcs/airoha/pcs-airoha-common.c new file +1313 -0

1,313 lines not on this page · raw message

drivers/net/pcs/airoha/pcs-airoha.h new file +1309 -0

1,309 lines not on this page · raw message

drivers/net/pcs/airoha/pcs-an7581.c new file +2093 -0

348 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.53.0

Part of the diff is not shown here. View the raw message.

#22 lore raw

Re: [PATCH net-next v6 11/12] net: pcs: airoha: add PCS driver for Airoha AN7581 SoC

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [Critical] Passing the address of a local stack variable (`&priv`) to `fwnode_pcs_add_provider()` causes…

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [Critical] Passing the address of a local stack variable (&priv) to fwnode_pcs_add_provider() causes a fatal use-after-free when dereferenced later.
- [High] A division by zero will occur during RX PR calibration if the frequency output perfectly matches the target.
- [High] Integer underflow and misuse of the abs() macro on unsigned types breaks frequency calibration.
- [High] The pcs-airoha module is silently not generated when configured as a loadable module (M).
- [High] Shared device-level hardware resets and PLL configurations disrupt other active ports on multi-port devices.
- [High] Missing bounds check on user-provided device tree arguments leads to out-of-bounds array access.
- [High] Missing FORCE_SEL bits in regmap_update_bits values causes the hardware to ignore the forced configurations.
- [Medium] regmap_set_bits performs a read-modify-write on Write-1-to-Clear (W1C) interrupt status registers, silently dropping unhandled interrupts.
- [Low] Missing fallthrough; or break; annotation in switch statement causes an implicit fallthrough compiler warning.
--

commit a1f0ba4cb5341a223ae17b71c3dfd06228567ad5

Author: Christian Marangi <[email protected]>
Date:   Tue Jun 9 15:12:07 2026 +0000

net: pcs: airoha: add PCS driver for Airoha AN7581 SoC

This commit introduces the PCS driver for the Airoha AN7581 SoC, supporting Ethernet, PON, PCIe, and USB SERDES. It implements phylink PCS operations and handles the configuration of the PCS and PMA blocks.

drivers/net/pcs/airoha/Makefile line 3

Quoted 9 lines
drivers/net/pcs/airoha/Makefile new file +3 -0
new file mode 100644
index 0000000000000..25cb8f090c214
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-y                          := pcs-airoha.o

[Severity: High]
Will this correctly build as a loadable module? It appears that using obj-y forces static inclusion, which means the module won't be generated when CONFIG_PCS_AIROHA is set to 'm'. Should this use obj-$(CONFIG_PCS_AIROHA) instead?

Quoted 5 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +0 -0
new file mode 100644
index 0000000000000..ead0bc8083967

[ ... ]

Quoted 3 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +3 -0
+static int airoha_pcs_setup_scu(struct airoha_pcs_priv *priv,
+                               int index, phy_interface_t interface)
+{

[ ... ]

drivers/net/pcs/airoha/pcs-airoha-common.c line 136

Quoted 3 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +3 -0
+       /* TODO better handle reset from MAC */
+       ret = reset_control_bulk_assert(ARRAY_SIZE(priv->rsts),
+                                       priv->rsts);

[Severity: High]
Does unconditionally triggering these global reset lines disrupt other active ports on multi-port devices like PCIe? It seems that initializing a second port will assert these resets and could drop the link on an already active first port.

[ ... ]

Quoted 3 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +3 -0
+static void airoha_pcs_interrupt_init_usxgmii(struct airoha_pcs_priv *priv,
+                                             int index)
+{

[ ... ]

drivers/net/pcs/airoha/pcs-airoha-common.c line 282

Quoted 6 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +6 -0
+       /* Clear any pending interrupt */
+       regmap_set_bits(maps->usxgmii_pcs, AIROHA_PCS_USXGMII_PCS_INT_STA_2,
+                       AIROHA_PCS_USXGMII_RPCS_FSM_DEC_ERR_INT |
+                       AIROHA_PCS_USXGMII_R_TYPE_E_INT |
+                       AIROHA_PCS_USXGMII_R_TYPE_T_INT |
+                       AIROHA_PCS_USXGMII_R_TYPE_D_INT);

[Severity: Medium]
Could using regmap_set_bits() on a Write-1-to-Clear (W1C) interrupt status register cause unhandled interrupts to be silently dropped? This performs a read-modify-write, which might write back 1s to pending interrupts not targeted by this bitmask. Should this use a direct regmap_write() instead?

[ ... ]

Quoted 2 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +2 -0
+static void airoha_pcs_an_restart(struct phylink_pcs *pcs)
+{

[ ... ]

drivers/net/pcs/airoha/pcs-airoha-common.c line 683

Quoted 8 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +8 -0
+       case PHY_INTERFACE_MODE_USXGMII:
+               regmap_set_bits(maps->usxgmii_pcs, AIROHA_PCS_USXGMII_PCS_AN_CONTROL_0,
+                               AIROHA_PCS_USXGMII_AN_RESTART);
+               udelay(3);
+               regmap_clear_bits(maps->usxgmii_pcs, AIROHA_PCS_USXGMII_PCS_AN_CONTROL_0,
+                                 AIROHA_PCS_USXGMII_AN_RESTART);
+       default:
+               return;

[Severity: Low]
Does this missing break or fallthrough annotation trigger compiler warnings? It looks like the PHY_INTERFACE_MODE_USXGMII case falls through to default without explicitly declaring it.

[ ... ]

Quoted 3 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +3 -0
+static struct phylink_pcs *airoha_pcs_get(struct fwnode_reference_args *pcsspec,
+                                         void *data)
+{

[ ... ]

drivers/net/pcs/airoha/pcs-airoha-common.c line 1117

Quoted 4 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +4 -0
+       if (pcsspec->nargs)
+               index = pcsspec->args[0];
+
+       return &priv->ports[index].pcs;

[Severity: High]
Is there a risk of an out-of-bounds array access here? The index is extracted directly from the device tree via pcsspec->args[0] without verifying if it is less than priv->data->num_port.

[ ... ]

Quoted 2 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +2 -0
+static int airoha_pcs_probe(struct platform_device *pdev)
+{

[ ... ]

Quoted 1 line

+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);

[ ... ]

drivers/net/pcs/airoha/pcs-airoha-common.c line 1239

Quoted 2 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +2 -0
+       return fwnode_pcs_add_provider(dev_fwnode(dev), airoha_pcs_get,
+                                      &priv);

[Severity: Critical]
Could passing the address of the local stack variable &priv cause a use-after-free? When the MAC driver later calls airoha_pcs_get(), it will dereference this stack pointer after airoha_pcs_probe() has returned. Should this pass 'priv' instead of '&priv'?

Quoted 5 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +0 -0
new file mode 100644
index 0000000000000..22cf7b4b0108f

[ ... ]

Quoted 3 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +3 -0
+static void an7581_pcs_jcpll_bringup(struct airoha_pcs_priv *priv,
+                                    int index, phy_interface_t interface)
+{

[ ... ]

drivers/net/pcs/airoha/pcs-an7581.c line 701

Quoted 8 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +8 -0
+       /* This comment only apply to Serdes PCIe that expose
+        * 2 PCS.
+        *
+        * The Serdes PCIe expose 2 PCS but always require
+        * the PMA for the first PCS to be configured
+        * for correct functionality for JCPLL.
+        */
+       pcs_pma = priv->pcs_pma[0];

[Severity: High]
Does forcing pcs_pma to index 0 clobber the active PLL configuration of Port 0 when Port 1 is being initialized? It appears this might disrupt the active connection on the first port.

[ ... ]

Quoted 3 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +3 -0
+static void an7581_pcs_tx_bringup(struct airoha_pcs_priv *priv,
+                                 int index, phy_interface_t interface)
+{

[ ... ]

drivers/net/pcs/airoha/pcs-an7581.c line 1044

Quoted 9 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +9 -0
+       regmap_update_bits(pcs_pma, AIROHA_PCS_PMA_PXP_TX_TERM_SEL,
+                          AIROHA_PCS_PMA_FORCE_SEL_DA_TX_CKIN_DIVISOR |
+                          AIROHA_PCS_PMA_FORCE_DA_TX_CKIN_DIVISOR |
+                          AIROHA_PCS_PMA_FORCE_SEL_DA_TX_TERM_SEL |
+                          AIROHA_PCS_PMA_FORCE_DA_TX_TERM_SEL,
+                          AIROHA_PCS_PMA_FORCE_SEL_DA_TX_CKIN_DIVISOR |
+                          FIELD_PREP(AIROHA_PCS_PMA_FORCE_DA_TX_CKIN_DIVISOR,
+                                     ckin_divisor) |
+                          FIELD_PREP(AIROHA_PCS_PMA_FORCE_DA_TX_TERM_SEL, 0x0));

[Severity: High]
Will the hardware ignore this forced configuration? The mask includes AIROHA_PCS_PMA_FORCE_SEL_DA_TX_TERM_SEL, but the value part omits it, which might clear the force override bit.

[ ... ]

Quoted 3 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +3 -0
+static u32 an7581_pcs_rx_prcal_idac_major(struct airoha_pcs_priv *priv,
+                                         int index, u32 target_fl_out)
+{

[ ... ]

drivers/net/pcs/airoha/pcs-an7581.c line 1501

Quoted 4 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +4 -0
+               fl_out = an7581_pcs_apply_cdr_pr_idac(priv, index, cdr_pr_idac_tmp);
+
+               /* Use absolute values to find the closest one to target */
+               fl_out_diff_new = abs(fl_out - target_fl_out);

[Severity: High]
Since fl_out and target_fl_out are both unsigned integers, doesn't this subtraction cause an underflow if fl_out is less than target_fl_out? Also, the abs() macro generally expects signed types. Could this cause the calibration loop to become inaccurate?

[ ... ]

Quoted 3 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +3 -0
+static u32 an7581_pcs_rx_prcal_idac_minor(struct airoha_pcs_priv *priv, int index,
+                                         u32 target_fl_out, u32 cdr_pr_idac_major)
+{

[ ... ]

Quoted 1 line

+ fl_out_diff_new = abs(fl_out - target_fl_out);

[ ... ]

drivers/net/pcs/airoha/pcs-an7581.c line 1578

Quoted 2 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +2 -0
+               if (fl_out_diff_new > fl_out_diff &&
+                   (abs(fl_out_diff_new - fl_out_diff) * 100) / fl_out_diff > 10) {

[Severity: High]
Is there a risk of a divide-by-zero condition here? If the initial fl_out reading perfectly matches target_fl_out, fl_out_diff will evaluate to 0, which would cause the division to panic the kernel.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#23 lore raw

PATCH v6 12/12 net: airoha: add phylink support

In reply to Christian Marangi

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.

---
 drivers/net/ethernet/airoha/Kconfig       |   1 +
 drivers/net/ethernet/airoha/airoha_eth.c  | 167 +++++++++++++++++++++-
 drivers/net/ethernet/airoha/airoha_eth.h  |   3 +
 drivers/net/ethernet/airoha/airoha_regs.h |  12 ++
 4 files changed, 181 insertions(+), 2 deletions(-)
drivers/net/ethernet/airoha/Kconfig +1 -0
index ad3ce501e7a5..38dcc76e5998 100644
@@ -20,6 +20,7 @@ config NET_AIROHA
 	depends on NET_DSA || !NET_DSA
 	select NET_AIROHA_NPU
 	select PAGE_POOL
+	select PHYLINK
 	help
 	  This driver supports the gigabit ethernet MACs in the
 	  Airoha SoC family.
drivers/net/ethernet/airoha/airoha_eth.c +165 -2

210 lines not on this page · raw message

drivers/net/ethernet/airoha/airoha_eth.h +3 -0
index 8f42973f9cf5..1b25603dc64d 100644
@@ -554,6 +554,9 @@ struct airoha_gdm_dev {
 
 	u32 flags;
 	int nbq;
+
+	struct phylink *phylink;
+	struct phylink_config phylink_config;
 };
 
 struct airoha_gdm_port {
drivers/net/ethernet/airoha/airoha_regs.h +12 -0
index 436f3c8779c1..27f2583e143a 100644
@@ -358,6 +358,18 @@
 #define IP_FRAGMENT_PORT_MASK		GENMASK(8, 5)
 #define IP_FRAGMENT_NBQ_MASK		GENMASK(4, 0)
 
+#define REG_GDMA4_TMBI_FRAG		0x2028
+#define GDMA4_SGMII1_TX_WEIGHT_MASK	GENMASK(31, 26)
+#define GDMA4_SGMII1_TX_FRAG_SIZE_MASK	GENMASK(25, 16)
+#define GDMA4_SGMII0_TX_WEIGHT_MASK	GENMASK(15, 10)
+#define GDMA4_SGMII0_TX_FRAG_SIZE_MASK	GENMASK(9, 0)
+
+#define REG_GDMA4_RMBI_FRAG		0x202c
+#define GDMA4_SGMII1_RX_WEIGHT_MASK	GENMASK(31, 26)
+#define GDMA4_SGMII1_RX_FRAG_SIZE_MASK	GENMASK(25, 16)
+#define GDMA4_SGMII0_RX_WEIGHT_MASK	GENMASK(15, 10)
+#define GDMA4_SGMII0_RX_FRAG_SIZE_MASK	GENMASK(9, 0)
+
 #define REG_MC_VLAN_EN			0x2100
 #define MC_VLAN_EN_MASK			BIT(0)
 
Signature · 1 line
2.53.0
maintainer
#24 lore raw

Re: [PATCH net-next v6 12/12] net: airoha: add phylink support

Quoted 9 lines

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.

Hi Christian,

some nits inline.

Regards,
Lorenzo

Quoted 24 lines
---
 drivers/net/ethernet/airoha/Kconfig       |   1 +
 drivers/net/ethernet/airoha/airoha_eth.c  | 167 +++++++++++++++++++++-
 drivers/net/ethernet/airoha/airoha_eth.h  |   3 +
 drivers/net/ethernet/airoha/airoha_regs.h |  12 ++
 4 files changed, 181 insertions(+), 2 deletions(-)
drivers/net/ethernet/airoha/Kconfig +1 -0
index ad3ce501e7a5..38dcc76e5998 100644
@@ -20,6 +20,7 @@ config NET_AIROHA
 	depends on NET_DSA || !NET_DSA
 	select NET_AIROHA_NPU
 	select PAGE_POOL
+	select PHYLINK
 	help
 	  This driver supports the gigabit ethernet MACs in the
 	  Airoha SoC family.
drivers/net/ethernet/airoha/airoha_eth.c +0 -0
index 5a8e84fa9918..eabd7b058f82 100644

drivers/net/ethernet/airoha/airoha_eth.c line 11

Quoted 4 lines
drivers/net/ethernet/airoha/airoha_eth.c +1 -0
@@ -8,6 +8,7 @@
 #include <linux/of_reserved_mem.h>
 #include <linux/platform_device.h>
 #include <linux/tcp.h>
+#include <linux/pcs/pcs.h>

Can you please respect the alphabetic order?

drivers/net/ethernet/airoha/airoha_eth.c static int airoha_dev_open(struct net_device *netdev) · line 1786

Quoted 11 lines
drivers/net/ethernet/airoha/airoha_eth.c +4 -0
 #include <linux/u64_stats_sync.h>
 #include <net/dst_metadata.h>
 #include <net/page_pool/helpers.h>
@@ -1779,6 +1780,15 @@ static int airoha_dev_open(struct net_device *netdev)
 	u32 cur_len, pse_port = FE_PSE_PORT_PPE1;
 	struct airoha_qdma *qdma = dev->qdma;
 
+	err = phylink_of_phy_connect(dev->phylink, netdev->dev.of_node, 0);
+	if (err) {
+		netdev_err(netdev, "%s: could not attach PHY: %d\n", __func__,
+			   err);

Do we need specify the __func__ argument here?

Quoted 31 lines
drivers/net/ethernet/airoha/airoha_eth.c +17 -0
+		return err;
+	}
+
+	phylink_start(dev->phylink);
+
 	netif_tx_start_all_queues(netdev);
 	err = airoha_set_vip_for_gdm_port(dev, true);
 	if (err)
@@ -1876,6 +1886,9 @@ static int airoha_dev_stop(struct net_device *netdev)
 		}
 	}
 
+	phylink_stop(dev->phylink);
+	phylink_disconnect_phy(dev->phylink);
+
 	return 0;
 }
 
@@ -3148,6 +3161,153 @@ 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)

drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3182

Quoted 10 lines
drivers/net/ethernet/airoha/airoha_eth.c +10 -0
+{
+	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;
+	u32 mask, val;
+
+	/* TX/RX frag is configured only for GDM4 */
+	if (port->id != 4)
	if (port->id != AIROHA_GDM4_IDX)
		...
Quoted 70 lines
drivers/net/ethernet/airoha/airoha_eth.c +70 -0
+		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;
+	}
+
+	/* Configure TX/RX frag based on speed */
+	if (dev->nbq == 1) {
+		mask = GDMA4_SGMII1_TX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII1_TX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	}  else {
+		mask = GDMA4_SGMII0_TX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII0_TX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	}
+	airoha_fe_rmw(eth, REG_GDMA4_TMBI_FRAG, mask, val);
+
+	if (dev->nbq == 1) {
+		mask = GDMA4_SGMII1_RX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII1_RX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	} else {
+		mask = GDMA4_SGMII0_RX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII0_RX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	}
+	airoha_fe_rmw(eth, REG_GDMA4_RMBI_FRAG, mask, val);
+}
+
+/* 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_available_pcs)
+{
+	struct device *dev = config->dev;
+
+	return fwnode_phylink_pcs_parse(dev_fwnode(dev), available_pcs,
+					&num_available_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;

drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3262

Quoted 10 lines
drivers/net/ethernet/airoha/airoha_eth.c +10 -0
+	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;

remove new-line here.

drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3267

Quoted 5 lines
drivers/net/ethernet/airoha/airoha_eth.c +5 -0
+
+	config->dev = &netdev->dev;
+	config->type = PHYLINK_NETDEV;
+	config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |  MAC_10000FD;
+	if (port->id > AIROHA_GDM1_IDX)

maybe

	if (port->id != AIROHA_GDM1_IDX)
		...
Quoted 115 lines
drivers/net/ethernet/airoha/airoha_eth.c +48 -2
+		config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000 |
+					    MAC_2500FD | MAC_5000FD;
+
+	err = fwnode_phylink_pcs_parse(dev_fwnode(&netdev->dev), NULL,
+				       &config->num_available_pcs);
+	if (err)
+		return err;
+
+	config->fill_available_pcs = airoha_fill_available_pcs;
+
+	/*
+	 * GDM1 only supports internal for Embedded Switch
+	 * and doesn't require a PCS.
+	 */
+	if (port->id == AIROHA_GDM1_IDX) {
+		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
+			  config->supported_interfaces);
+	} else {
+		__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)
@@ -3210,7 +3370,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,
@@ -3435,8 +3595,10 @@ static int airoha_probe(struct platform_device *pdev)
 				continue;
 
 			netdev = netdev_from_priv(dev);
-			if (netdev->reg_state == NETREG_REGISTERED)
+			if (netdev->reg_state == NETREG_REGISTERED) {
+				phylink_destroy(dev->phylink);
 				unregister_netdev(netdev);
+			}
 			of_node_put(netdev->dev.of_node);
 		}
 		airoha_metadata_dst_free(port);
@@ -3472,6 +3634,7 @@ static void airoha_remove(struct platform_device *pdev)
 				continue;
 
 			netdev = netdev_from_priv(dev);
+			phylink_destroy(dev->phylink);
 			unregister_netdev(netdev);
 			of_node_put(netdev->dev.of_node);
 		}
drivers/net/ethernet/airoha/airoha_eth.h +3 -0
index 8f42973f9cf5..1b25603dc64d 100644
@@ -554,6 +554,9 @@ struct airoha_gdm_dev {
 
 	u32 flags;
 	int nbq;
+
+	struct phylink *phylink;
+	struct phylink_config phylink_config;
 };
 
 struct airoha_gdm_port {
drivers/net/ethernet/airoha/airoha_regs.h +12 -0
index 436f3c8779c1..27f2583e143a 100644
@@ -358,6 +358,18 @@
 #define IP_FRAGMENT_PORT_MASK		GENMASK(8, 5)
 #define IP_FRAGMENT_NBQ_MASK		GENMASK(4, 0)
 
+#define REG_GDMA4_TMBI_FRAG		0x2028
+#define GDMA4_SGMII1_TX_WEIGHT_MASK	GENMASK(31, 26)
+#define GDMA4_SGMII1_TX_FRAG_SIZE_MASK	GENMASK(25, 16)
+#define GDMA4_SGMII0_TX_WEIGHT_MASK	GENMASK(15, 10)
+#define GDMA4_SGMII0_TX_FRAG_SIZE_MASK	GENMASK(9, 0)
+
+#define REG_GDMA4_RMBI_FRAG		0x202c
+#define GDMA4_SGMII1_RX_WEIGHT_MASK	GENMASK(31, 26)
+#define GDMA4_SGMII1_RX_FRAG_SIZE_MASK	GENMASK(25, 16)
+#define GDMA4_SGMII0_RX_WEIGHT_MASK	GENMASK(15, 10)
+#define GDMA4_SGMII0_RX_FRAG_SIZE_MASK	GENMASK(9, 0)
+
 #define REG_MC_VLAN_EN			0x2100
 #define MC_VLAN_EN_MASK			BIT(0)
 
Signature · 1 line
2.53.0
#25 lore raw

Re: [PATCH net-next v6 12/12] net: airoha: add phylink support

Quoted 55 lines · Lorenzo Bianconi
Quoted 9 lines

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.

Hi Christian,

some nits inline.

Regards,
Lorenzo

Quoted 24 lines
---
 drivers/net/ethernet/airoha/Kconfig       |   1 +
 drivers/net/ethernet/airoha/airoha_eth.c  | 167 +++++++++++++++++++++-
 drivers/net/ethernet/airoha/airoha_eth.h  |   3 +
 drivers/net/ethernet/airoha/airoha_regs.h |  12 ++
 4 files changed, 181 insertions(+), 2 deletions(-)
drivers/net/ethernet/airoha/Kconfig +1 -0
index ad3ce501e7a5..38dcc76e5998 100644
@@ -20,6 +20,7 @@ config NET_AIROHA
 	depends on NET_DSA || !NET_DSA
 	select NET_AIROHA_NPU
 	select PAGE_POOL
+	select PHYLINK
 	help
 	  This driver supports the gigabit ethernet MACs in the
 	  Airoha SoC family.
drivers/net/ethernet/airoha/airoha_eth.c +0 -0
index 5a8e84fa9918..eabd7b058f82 100644

drivers/net/ethernet/airoha/airoha_eth.c line 11

Quoted 4 lines
drivers/net/ethernet/airoha/airoha_eth.c +1 -0
@@ -8,6 +8,7 @@
 #include <linux/of_reserved_mem.h>
 #include <linux/platform_device.h>
 #include <linux/tcp.h>
+#include <linux/pcs/pcs.h>

Can you please respect the alphabetic order?

Quoted 7 lines
drivers/net/ethernet/airoha/airoha_eth.c +0 -0
 #include <linux/u64_stats_sync.h>
 #include <net/dst_metadata.h>
 #include <net/page_pool/helpers.h>
@@ -1779,6 +1780,15 @@ static int airoha_dev_open(struct net_device *netdev)
 	u32 cur_len, pse_port = FE_PSE_PORT_PPE1;
 	struct airoha_qdma *qdma = dev->qdma;
 
Quoted 7 lines

drivers/net/ethernet/airoha/airoha_eth.c static int airoha_dev_open(struct net_device *netdev) · line 1786

Quoted 4 lines
drivers/net/ethernet/airoha/airoha_eth.c +4 -0
+	err = phylink_of_phy_connect(dev->phylink, netdev->dev.of_node, 0);
+	if (err) {
+		netdev_err(netdev, "%s: could not attach PHY: %d\n", __func__,
+			   err);

Do we need specify the __func__ argument here?

I was following a pattern also used in other driver. Maybe we should stop following it?

Quoted 253 lines
Quoted 31 lines
drivers/net/ethernet/airoha/airoha_eth.c +17 -0
+		return err;
+	}
+
+	phylink_start(dev->phylink);
+
 	netif_tx_start_all_queues(netdev);
 	err = airoha_set_vip_for_gdm_port(dev, true);
 	if (err)
@@ -1876,6 +1886,9 @@ static int airoha_dev_stop(struct net_device *netdev)
 		}
 	}
 
+	phylink_stop(dev->phylink);
+	phylink_disconnect_phy(dev->phylink);
+
 	return 0;
 }
 
@@ -3148,6 +3161,153 @@ 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)

drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3182

Quoted 10 lines
drivers/net/ethernet/airoha/airoha_eth.c +10 -0
+{
+	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;
+	u32 mask, val;
+
+	/* TX/RX frag is configured only for GDM4 */
+	if (port->id != 4)
	if (port->id != AIROHA_GDM4_IDX)
		...
Quoted 70 lines
drivers/net/ethernet/airoha/airoha_eth.c +70 -0
+		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;
+	}
+
+	/* Configure TX/RX frag based on speed */
+	if (dev->nbq == 1) {
+		mask = GDMA4_SGMII1_TX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII1_TX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	}  else {
+		mask = GDMA4_SGMII0_TX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII0_TX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	}
+	airoha_fe_rmw(eth, REG_GDMA4_TMBI_FRAG, mask, val);
+
+	if (dev->nbq == 1) {
+		mask = GDMA4_SGMII1_RX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII1_RX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	} else {
+		mask = GDMA4_SGMII0_RX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII0_RX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	}
+	airoha_fe_rmw(eth, REG_GDMA4_RMBI_FRAG, mask, val);
+}
+
+/* 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_available_pcs)
+{
+	struct device *dev = config->dev;
+
+	return fwnode_phylink_pcs_parse(dev_fwnode(dev), available_pcs,
+					&num_available_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;

drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3262

Quoted 10 lines
drivers/net/ethernet/airoha/airoha_eth.c +10 -0
+	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;

remove new-line here.

drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3267

Quoted 5 lines
drivers/net/ethernet/airoha/airoha_eth.c +5 -0
+
+	config->dev = &netdev->dev;
+	config->type = PHYLINK_NETDEV;
+	config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |  MAC_10000FD;
+	if (port->id > AIROHA_GDM1_IDX)

maybe

	if (port->id != AIROHA_GDM1_IDX)
		...
Quoted 115 lines
drivers/net/ethernet/airoha/airoha_eth.c +48 -2
+		config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000 |
+					    MAC_2500FD | MAC_5000FD;
+
+	err = fwnode_phylink_pcs_parse(dev_fwnode(&netdev->dev), NULL,
+				       &config->num_available_pcs);
+	if (err)
+		return err;
+
+	config->fill_available_pcs = airoha_fill_available_pcs;
+
+	/*
+	 * GDM1 only supports internal for Embedded Switch
+	 * and doesn't require a PCS.
+	 */
+	if (port->id == AIROHA_GDM1_IDX) {
+		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
+			  config->supported_interfaces);
+	} else {
+		__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)
@@ -3210,7 +3370,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,
@@ -3435,8 +3595,10 @@ static int airoha_probe(struct platform_device *pdev)
 				continue;
 
 			netdev = netdev_from_priv(dev);
-			if (netdev->reg_state == NETREG_REGISTERED)
+			if (netdev->reg_state == NETREG_REGISTERED) {
+				phylink_destroy(dev->phylink);
 				unregister_netdev(netdev);
+			}
 			of_node_put(netdev->dev.of_node);
 		}
 		airoha_metadata_dst_free(port);
@@ -3472,6 +3634,7 @@ static void airoha_remove(struct platform_device *pdev)
 				continue;
 
 			netdev = netdev_from_priv(dev);
+			phylink_destroy(dev->phylink);
 			unregister_netdev(netdev);
 			of_node_put(netdev->dev.of_node);
 		}
drivers/net/ethernet/airoha/airoha_eth.h +3 -0
index 8f42973f9cf5..1b25603dc64d 100644
@@ -554,6 +554,9 @@ struct airoha_gdm_dev {
 
 	u32 flags;
 	int nbq;
+
+	struct phylink *phylink;
+	struct phylink_config phylink_config;
 };
 
 struct airoha_gdm_port {
drivers/net/ethernet/airoha/airoha_regs.h +12 -0
index 436f3c8779c1..27f2583e143a 100644
@@ -358,6 +358,18 @@
 #define IP_FRAGMENT_PORT_MASK		GENMASK(8, 5)
 #define IP_FRAGMENT_NBQ_MASK		GENMASK(4, 0)
 
+#define REG_GDMA4_TMBI_FRAG		0x2028
+#define GDMA4_SGMII1_TX_WEIGHT_MASK	GENMASK(31, 26)
+#define GDMA4_SGMII1_TX_FRAG_SIZE_MASK	GENMASK(25, 16)
+#define GDMA4_SGMII0_TX_WEIGHT_MASK	GENMASK(15, 10)
+#define GDMA4_SGMII0_TX_FRAG_SIZE_MASK	GENMASK(9, 0)
+
+#define REG_GDMA4_RMBI_FRAG		0x202c
+#define GDMA4_SGMII1_RX_WEIGHT_MASK	GENMASK(31, 26)
+#define GDMA4_SGMII1_RX_FRAG_SIZE_MASK	GENMASK(25, 16)
+#define GDMA4_SGMII0_RX_WEIGHT_MASK	GENMASK(15, 10)
+#define GDMA4_SGMII0_RX_FRAG_SIZE_MASK	GENMASK(9, 0)
+
 #define REG_MC_VLAN_EN			0x2100
 #define MC_VLAN_EN_MASK			BIT(0)
 
Signature · 1 line
2.53.0
Signature · 1 line
	Ansuel
#26 lore raw

Re: [PATCH net-next v6 12/12] net: airoha: add phylink support

In reply to Christian Marangi
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Calling `phylink_destroy()` before `unregister_netdev()` causes a Use-After-Free during device…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Calling phylink_destroy() before unregister_netdev() causes a Use-After-Free during device teardown.
- [High] Unconditional PCS parsing in airoha_setup_phylink() causes probe failure for the GDM1 port.
- [Medium] The RX fragment size is incorrectly configured using the TX fragment size variable due to a copy-paste error.
- [Medium] The phylink instance is leaked in the airoha_probe() error path for successfully allocated but unregistered devices. --

commit 7158f1ddaf486067bb751bd3d43f1a8bfc87794f
Author: Christian Marangi <[email protected]>

net: airoha: add phylink support

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.

[ ... ]

Quoted 14 lines
drivers/net/ethernet/airoha/airoha_eth.c +10 -0
@@ -3148,6 +3161,153 @@ 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)
+{

[ ... ]

drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3220

Quoted 9 lines
drivers/net/ethernet/airoha/airoha_eth.c +9 -0
+	if (dev->nbq == 1) {
+		mask = GDMA4_SGMII1_RX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII1_RX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	} else {
+		mask = GDMA4_SGMII0_RX_FRAG_SIZE_MASK;
+		val = FIELD_PREP(GDMA4_SGMII0_RX_FRAG_SIZE_MASK,
+				 frag_size_tx);
+	}

[Severity: Medium]
Should this use frag_size_rx instead of frag_size_tx when configuring the RX fragment size? This looks like a typo that could cause the hardware to be configured incorrectly when the RX and TX fragment sizes differ.

Quoted 2 lines
(quoted diff) +2 -0
+	airoha_fe_rmw(eth, REG_GDMA4_RMBI_FRAG, mask, val);
+}

[ ... ]

Quoted 19 lines
drivers/net/ethernet/airoha/airoha_eth.c +19 -0
+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;

drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3274

Quoted 10 lines
drivers/net/ethernet/airoha/airoha_eth.c +10 -0
+	config->type = PHYLINK_NETDEV;
+	config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |  MAC_10000FD;
+	if (port->id > AIROHA_GDM1_IDX)
+		config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000 |
+					    MAC_2500FD | MAC_5000FD;
+
+	err = fwnode_phylink_pcs_parse(dev_fwnode(&netdev->dev), NULL,
+				       &config->num_available_pcs);
+	if (err)
+		return err;

[Severity: High]
Does this unconditional call to fwnode_phylink_pcs_parse() cause probe to fail for the GDM1 port?

The commit message mentions that GDM1 does not require a PCS. If its device tree node lacks a pcs-handle property, fwnode_phylink_pcs_parse() will return -ENODEV and abort the probe. Should this parsing logic be conditionally skipped if port->id == AIROHA_GDM1_IDX?

Quoted 7 lines
drivers/net/ethernet/airoha/airoha_eth.c +7 -0
+
+	config->fill_available_pcs = airoha_fill_available_pcs;
+
+	/*
+	 * GDM1 only supports internal for Embedded Switch
+	 * and doesn't require a PCS.
+	 */

[ ... ]

drivers/net/ethernet/airoha/airoha_eth.c static int airoha_probe(struct platform_device *pdev) · line 3601

Quoted 9 lines
drivers/net/ethernet/airoha/airoha_eth.c +3 -1
@@ -3435,8 +3595,10 @@ static int airoha_probe(struct platform_device *pdev)
 				continue;
 
 			netdev = netdev_from_priv(dev);
-			if (netdev->reg_state == NETREG_REGISTERED)
+			if (netdev->reg_state == NETREG_REGISTERED) {
+				phylink_destroy(dev->phylink);
 				unregister_netdev(netdev);
+			}

[Severity: Medium]
Does this leak the phylink instance for devices that were successfully allocated but failed to register?

In airoha_alloc_gdm_device(), the phylink structure is successfully allocated prior to device registration. If airoha_probe() fails before or during device registration, the NETREG_REGISTERED check here skips calling phylink_destroy() for unregistered devices, which would leave the memory leaked.

[Severity: High]
Is it safe to destroy the phylink instance before calling unregister_netdev()?

If the network interface is running (IFF_UP), unregister_netdev() will synchronously bring the device down by invoking its ndo_stop callback, which is airoha_dev_stop().

Inside airoha_dev_stop(), the driver attempts to halt phylink:

	phylink_stop(dev->phylink);
	phylink_disconnect_phy(dev->phylink);

Will this result in a use-after-free since dev->phylink was already destroyed here?

Quoted 3 lines
drivers/net/ethernet/airoha/airoha_eth.c +0 -0
 			of_node_put(netdev->dev.of_node);
 		}
 		airoha_metadata_dst_free(port);

[ ... ]

drivers/net/ethernet/airoha/airoha_eth.c static void airoha_remove(struct platform_device *pdev) · line 3638

Quoted 6 lines
drivers/net/ethernet/airoha/airoha_eth.c +1 -0
@@ -3472,6 +3634,7 @@ static void airoha_remove(struct platform_device *pdev)
 				continue;
 
 			netdev = netdev_from_priv(dev);
+			phylink_destroy(dev->phylink);
 			unregister_netdev(netdev);

[Severity: High]
Similarly to the error path in airoha_probe(), does destroying the phylink instance before calling unregister_netdev() cause a use-after-free when airoha_dev_stop() tries to use dev->phylink?

Quoted 2 lines
(quoted diff) +0 -0
 			of_node_put(netdev->dev.of_node);
 		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12