Skip to contentSkip to conversation

net: pcs: Introduce support for fwnode PCS

PATCH Superseded Christian Marangi 4 participants 33 messages Jun 15 – Jun 16

Subsystems (9)

v7 of 22 Superseded Latest is v22 →

lore
Share & tools
lore
#0 lore raw

v7 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 .fill_available_pcs and .num_possible_pcs in phylink_config. Helpers to parse PCS from fwnode are provided fwnode_phylink_pcs_count() that will return the count of PCS entries described in the firmware node and fwnode_phylink_pcs_parse() that will fill a preallocated array of PCS pointer with the actual available PCS (ignoring the one that still needs to be probed).

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

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

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

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

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

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

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

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

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

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

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

---

Changes v7:
- Address all the bug from the Sashiko bot
- Rename .num_available_pcs to .num_possible_pcs
- Link PCS in phylink_create()
- Correctly unregister the notifier on phylink_destroy()
- Introduce fwnode_phylink_pcs_count()
- Better handle locking in phylink for PCS handling
- Better handle unavailable PCS at phylink_create() time
- Improve Documentation file
- Other minor fixes to address suggestion from bot
- Rebase on top of net-next
Changes v6:
- Rebase on top of net-next
- Add Documentation files
- Add fw_devlink patch
- Fix some comments typo
- Rework the airoha_eth.c implementation with new multi serdes code
- Extend PCS code with PCIe and USB support
- Align schema to new property
Changes v5:
- Rebase on top of net-next
- Use the new force_major_config
- Reword some comments and commit description
- Return -ENODEV instead of -EPROBE_DEFER to perevent race condition
- Drop phy_interface_copy patch (Russell pushed an equivalent version) Changes v4:
- Move patch 0002 phy_interface_copy to 0002 (fix bisectability problem)
- Address review from Lorenzo for Airoha ethernet driver
- Fix kdoc error with missing Return (actually missing : before Return)
- Fix UNMET dependency reported error for CONFIG_FWNODE_PCS
- Revert to pcs.c instead of core.c (due to name conflict with other kmod)
- Fix clang compilation error for Airoha PCS driver
- Add missing inline function to pcs.h function
Changes v3:
- Out of RFC
- Fix various spelling mistake
- Drop circular dependency patch
- Complete Airoha Ethernet phylink integration
- Introduce .pcs_link_down PCS OP
Changes v2:
- Switch to fwnode
- Implement PCS provider notifier
- Better split changes
- Move supported_interfaces to phylink
- Add circular dependency patch
- Rework handling with indirect addition/removal and
trigger of phylink_resolve()

Christian Marangi (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          |  261 ++
 Documentation/networking/index.rst            |    1 +
 Documentation/networking/pcs.rst              |  229 ++
 MAINTAINERS                                   |    9 +
 drivers/net/ethernet/airoha/Kconfig           |    1 +
 drivers/net/ethernet/airoha/airoha_eth.c      |  161 +-
 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    | 1318 +++++++++++
 drivers/net/pcs/airoha/pcs-airoha.h           | 1309 +++++++++++
 drivers/net/pcs/airoha/pcs-an7581.c           | 2093 +++++++++++++++++
 drivers/net/pcs/pcs.c                         |  257 ++
 drivers/net/phy/phylink.c                     |  337 ++-
 drivers/of/property.c                         |    2 +
 include/linux/pcs/pcs-provider.h              |   41 +
 include/linux/pcs/pcs.h                       |  135 ++
 include/linux/phylink.h                       |   20 +
 21 files changed, 6191 insertions(+), 28 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 v7 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 v7 01/12] net: phylink: keep and use MAC supported_interfaces in phylink struct

Hello Christian,

Quoted 19 lines · Christian Marangi

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 +0 -0
index 087ac63f9193..4d59c0dd78db 100644

drivers/net/phy/phylink.c struct phylink { · line 66

Quoted 7 lines
drivers/net/phy/phylink.c +4 -0
@@ -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);

Can you clarify a bit what you mean here ? Is that the combination of the interfaces the MAC supports AND the currently in-use PCS ?

Maxime

#3 lore raw

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

Quoted 25 lines · Maxime Chevallier

Hello Christian,

Quoted 22 lines · Christian Marangi

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 +0 -0
index 087ac63f9193..4d59c0dd78db 100644
@@ -60,6 +60,11 @@ struct phylink {
 	/* The link configuration settings */
 	struct phylink_link_state link_config;
 
Quoted 8 lines

drivers/net/phy/phylink.c struct phylink { · line 66

Quoted 4 lines
drivers/net/phy/phylink.c +4 -0
+	/* What interface are supported by the current link.
+	 * Can change on removal or addition of new PCS.
+	 */
+	DECLARE_PHY_INTERFACE_MASK(supported_interfaces);

Can you clarify a bit what you mean here ? Is that the combination of the interfaces the MAC supports AND the currently in-use PCS ?

Combination of interface the MAC supports and the currently attached PCS (not the current one in use)

The fact that it can change is due to the fact that PCS can be attached later and supported_interfaces can be updated accordingly.

Signature · 1 line
	Ansuel
#4 lore raw

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

In reply to Christian Marangi
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The patch makes a one-time copy of `supported_interfaces` during `phylink_create()`, which breaks…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The patch makes a one-time copy of supported_interfaces during phylink_create(), which breaks drivers like mtk_eth_soc that modify phylink_config.supported_interfaces after calling phylink_create(). --

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

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

This patch introduces a cached copy of the supported_interfaces mask in the phylink struct. It initializes this copy during phylink_create() and transitions the phylink core to validate against this internal mask rather than the original phylink_config.

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]
Does this one-time copy in phylink_create() break drivers that modify their supported_interfaces after initialization?

For example, in drivers/net/ethernet/mediatek/mtk_eth_soc.c:mtk_add_mac(), the driver appends PHY_INTERFACE_MODE_INTERNAL to mac->phylink_config.supported_interfaces after phylink_create() has already been called:

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 now validates against the cached pl->supported_interfaces mask, will it reject PHY_INTERFACE_MODE_INTERNAL as unsupported and prevent the internal 2.5G PHY from linking successfully on affected Mediatek platforms?

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
#5 lore raw

PATCH v7 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_possible_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_possible_pcs from phylink_config and .fill_available_pcs from phylink_config is called passing as args the just allocated array and the number of possible 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().

Every PCS in phylink PCS list gets then linked to the phylink instance by setting the phylink value in phylink_pcs struct to the phylink instance. Also the supported_interface value in phylink struct is updated with the new supported_interface from the provided PCS.

On phylink_destroy(), every PCS in phylink PCS list is unlinked from the phylink instance by setting the phylink value in phylink_pcs struct to NULL and removed from the PCS list.

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 phylink_create().

A MAC defining .num_possible_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.

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

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

---
 drivers/net/phy/phylink.c | 196 ++++++++++++++++++++++++++++++++++----
 include/linux/phylink.h   |  16 ++++
 2 files changed, 192 insertions(+), 20 deletions(-)
drivers/net/phy/phylink.c +176 -20
index 4d59c0dd78db..cb07184ce82f 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,12 +523,29 @@ 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 */
@@ -531,9 +553,24 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl,
 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
 		if (IS_ERR(pcs))
 			return PTR_ERR(pcs);
+
+		pcs_found = !!pcs;
+	/*
+	 * Find a PCS in available PCS list for the requested interface.
+	 *
+	 * Skip searching if the MAC doesn't require a dedicated PCS for
+	 * the requested interface.
+	 */
+	} else if (test_bit(state->interface, pl->config->pcs_interfaces)) {
+		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 +582,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 +999,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 if (test_bit(interface, pl->config->pcs_interfaces)) {
+		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 +1309,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 dedicated 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 +1365,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 +1906,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_possible_pcs)
+		return 0;
+
+	if (!config->fill_available_pcs) {
+		dev_err(config->dev,
+			"phylink: error: num_possible_pcs defined but no fill_available_pcs\n");
+		return -EINVAL;
+	}
+
+	pcss = kzalloc_objs(*pcss, config->num_possible_pcs);
+	if (!pcss)
+		return -ENOMEM;
+
+	ret = config->fill_available_pcs(config, pcss, config->num_possible_pcs);
+	if (ret < 0)
+		goto out;
+
+	for (i = 0; i < config->num_possible_pcs; i++) {
+		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 +1965,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;
 
@@ -1865,6 +1976,16 @@ struct phylink *phylink_create(struct phylink_config *config,
 		return ERR_PTR(-EINVAL);
 	}
 
+	/*
+	 * Make sure either PCS internal validation or .mac_select_pcs
+	 * is used. Return error if both are defined.
+	 */
+	if (config->num_possible_pcs && pl->mac_ops->mac_select_pcs) {
+		dev_err(config->dev,
+			"phylink: error: either phylink_config .num_possible_pcs or .mac_select_pcs must be used\n");
+		return ERR_PTR(-EINVAL);
+	}
+
 	pl = kzalloc_obj(*pl);
 	if (!pl)
 		return ERR_PTR(-ENOMEM);
@@ -1872,10 +1993,28 @@ 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 < 0) {
+		kfree(pl);
+		return ERR_PTR(ret);
+	}
+
+	/* Link available PCS to phylink */
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		pcs->phylink = pl;
 
 	phy_interface_copy(pl->supported_interfaces,
 			   config->supported_interfaces);
 
+	/* Update 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) {
 		pl->netdev = to_net_dev(config->dev);
@@ -1953,10 +2092,20 @@ 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);
 
+	/* Drop link between PCS and phylink */
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		pcs->phylink = NULL;
+
+	/* 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);
 }
@@ -2413,8 +2562,15 @@ void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
 {
 	struct phylink *pl = pcs->phylink;
 
-	if (pl)
-		phylink_link_changed(pl, up, "pcs");
+	/*
+	 * Ignore PCS link state change if the PCS is not
+	 * attached to a phylink instance or the phylink
+	 * instance is not currently using this PCS.
+	 */
+	if (!pl || pl->pcs != pcs)
+		return;
+
+	phylink_link_changed(pl, up, "pcs");
 }
 EXPORT_SYMBOL_GPL(phylink_pcs_change);
 
include/linux/phylink.h +16 -0
index 2bc0db3d52ac..ca9dfc142388 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_possible_pcs: num of possible phylink_pcs PCS
+ * @fill_available_pcs: callback to fill the available PCS in the passed
+ *			array struct of phylink_pcs PCS available_pcs up to
+ *			num_possible_pcs.
  */
 struct phylink_config {
 	struct device *dev;
@@ -172,6 +179,7 @@ struct phylink_config {
 	void (*get_fixed_state)(struct phylink_config *config,
 				struct phylink_link_state *state);
 	DECLARE_PHY_INTERFACE_MASK(supported_interfaces);
+	DECLARE_PHY_INTERFACE_MASK(pcs_interfaces);
 	DECLARE_PHY_INTERFACE_MASK(lpi_interfaces);
 	unsigned long mac_capabilities;
 	unsigned long lpi_capabilities;
@@ -182,6 +190,11 @@ struct phylink_config {
 	bool wol_phy_legacy;
 	bool wol_phy_speed_ctrl;
 	u32 wol_mac_support;
+
+	unsigned int num_possible_pcs;
+	int (*fill_available_pcs)(struct phylink_config *config,
+				  struct phylink_pcs **available_pcs,
+				  unsigned int num_possible_pcs);
 };
 
 void phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed);
@@ -497,6 +510,9 @@ struct phylink_pcs {
 	struct phylink *phylink;
 	bool poll;
 	bool rxc_always_on;
+
+	/* private: */
+	struct list_head list;
 };
 
 /**
Signature · 1 line
2.53.0
#6 lore raw

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

Hi Christian,

Quoted 63 lines · 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_possible_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_possible_pcs from phylink_config and .fill_available_pcs from phylink_config is called passing as args the just allocated array and the number of possible 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().

Every PCS in phylink PCS list gets then linked to the phylink instance by setting the phylink value in phylink_pcs struct to the phylink instance. Also the supported_interface value in phylink struct is updated with the new supported_interface from the provided PCS.

On phylink_destroy(), every PCS in phylink PCS list is unlinked from the phylink instance by setting the phylink value in phylink_pcs struct to NULL and removed from the PCS list.

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 phylink_create().

A MAC defining .num_possible_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.

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

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

---

[...]

Quoted 16 lines
drivers/net/phy/phylink.c +12 -0
@@ -1872,10 +1993,28 @@ 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 < 0) {
+		kfree(pl);
+		return ERR_PTR(ret);
+	}
+
+	/* Link available PCS to phylink */
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		pcs->phylink = pl;

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

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

I'm not entirely sure about that, we may need to restrict the supported_interfaces from the MAC.

As an example, take mvpp2. We have 2 PCSs, one for BaseX/SGMII, one for BaseR. But if we don't have a comphy (generic PHY) device, then we can't use all the combination of modes our PCSs can provide :

https://elixir.bootlin.com/linux/v7.1-rc7/source/drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c#L7074

These aren't external PCS IPs, but from what I understand you'd like to handle these the same way as purely external PCSs, right ?

I'd say the MAC driver utltimately has the knowledge of all possible interfaces.

The way I see it, it's probably safer to let the MAC give a wide range of interfaces, and filter that down with what the PCSs can provide (i.e. turn that or into an and, while handling the case where the pcs supported interfaces is empty).

What do you think ?

Maxime

#7 lore raw

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

Quoted 108 lines · Maxime Chevallier

Hi Christian,

Quoted 63 lines · 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_possible_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_possible_pcs from phylink_config and .fill_available_pcs from phylink_config is called passing as args the just allocated array and the number of possible 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().

Every PCS in phylink PCS list gets then linked to the phylink instance by setting the phylink value in phylink_pcs struct to the phylink instance. Also the supported_interface value in phylink struct is updated with the new supported_interface from the provided PCS.

On phylink_destroy(), every PCS in phylink PCS list is unlinked from the phylink instance by setting the phylink value in phylink_pcs struct to NULL and removed from the PCS list.

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 phylink_create().

A MAC defining .num_possible_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.

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

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

---

[...]

Quoted 16 lines
drivers/net/phy/phylink.c +12 -0
@@ -1872,10 +1993,28 @@ 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 < 0) {
+		kfree(pl);
+		return ERR_PTR(ret);
+	}
+
+	/* Link available PCS to phylink */
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		pcs->phylink = pl;

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

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

I'm not entirely sure about that, we may need to restrict the supported_interfaces from the MAC.

As an example, take mvpp2. We have 2 PCSs, one for BaseX/SGMII, one for BaseR. But if we don't have a comphy (generic PHY) device, then we can't use all the combination of modes our PCSs can provide :

https://elixir.bootlin.com/linux/v7.1-rc7/source/drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c#L7074

These aren't external PCS IPs, but from what I understand you'd like to handle these the same way as purely external PCSs, right ?

Quoted 8 lines

I'd say the MAC driver utltimately has the knowledge of all possible interfaces.

The way I see it, it's probably safer to let the MAC give a wide range of interfaces, and filter that down with what the PCSs can provide (i.e. turn that or into an and, while handling the case where the pcs supported interfaces is empty).

What do you think ?

The idea is that supported_interface is a mask of every possible interface from MAC and PCS. Then it's phylink_validate_mac_and_pcs that actually use that mask and validates it on both MAC and PCS.

This is why the OR was used instead of AND. The idea is to have the PCS as external standalone entry (even if they are internal to the MAC). So each entry should have they own set of supported mask.

The previous patch and this try to address this problem where phylink is actually clueless of what is actually supported exactly because it's has been given MAC too much freedom of modelling limitation internally.

I feel limitation should be handled by their dedicated function with .pcs_validate and .mac_get_caps.

Just my idea on this, if needed it's totally ok to simplify this and let MAC entirely handle the mask. (but I feel the current idea of phylink code was to have a generic mask in supported_interfaces and then verify MAC and PCS in phylink_validate_mac_and_pcs())

But by thinking on it more, following your case of mvpp2, with this new PCS:

- You need a PCS for the .get_state.
- And such PCS will have the supported interface set 1000baseX and 2500BaseX (as that is what is actually supported in HW)

Either some magic is done in .pcs_validate to deny changing the interface that was initially configured or this gets limited at the supported_interface configured by the MAC.

I need to check if this might be problematic for the other driver where this is being used on OpenWrt but maybe changing the logic to an AND might be sensible for these kind of case.

(for the other it shouldn't change anything)

Signature · 1 line
	Ansuel
#8 lore raw

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

In reply to Christian Marangi
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] Dereferencing the uninitialized pointer `pl` in `phylink_create()` causes a kernel panic.…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] Dereferencing the uninitialized pointer pl in phylink_create() causes a kernel panic.
- [High] Error paths in phylink_create() fail to unlink PCS objects and unregister notifiers, leading to Use-After-Free and list corruption.
- [Medium] phylink_validate_mac_and_pcs() falsely reports an interface as supported when a required PCS is not found.
- [Low] Lockless data race on pl->pcs between process context and interrupt context.
--

commit 5bb4efcfcad2f214630e951f75ab343ec05780b5
Author: Christian Marangi <[email protected]>
Subject: net: phylink: introduce internal phylink PCS handling

This commit introduces internal handling of PCS for phylink, moving the selection logic of the PCS entirely to phylink. MAC drivers can now provide a callback to fill the available PCS into an array which is then parsed into a linked list.

Quoted 17 lines
drivers/net/phy/phylink.c +9 -0
index 4d59c0dd78dbf..cb07184ce82f3 100644
@@ -531,9 +553,24 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl,
 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
 		if (IS_ERR(pcs))
 			return PTR_ERR(pcs);
+
+		pcs_found = !!pcs;
+	/*
+	 * Find a PCS in available PCS list for the requested interface.
+	 *
+	 * Skip searching if the MAC doesn't require a dedicated PCS for
+	 * the requested interface.
+	 */
+	} else if (test_bit(state->interface, pl->config->pcs_interfaces)) {

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

Quoted 10 lines
drivers/net/phy/phylink.c +7 -1
+		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) {

[Severity: Medium]
If the MAC explicitly requires a dedicated PCS for the requested interface mode but the loop doesn't find a compatible one, pcs_found remains false. Because the code then skips the pcs_found block and validates only the MAC capabilities, it seems it could return 0 (success) instead of an error like -EINVAL.

Does this unintentionally mark the interface as supported despite missing a required hardware component?

[ ... ]

Quoted 7 lines
drivers/net/phy/phylink.c +1 -2
@@ -1295,11 +1365,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) {

drivers/net/phy/phylink.c static void phylink_major_config(struct phylink *pl, bool restart, · line 1376

Quoted 10 lines
drivers/net/phy/phylink.c +5 -2
+			if (pl->pcs)
+				pl->pcs->phylink = NULL;
 
-		if (pcs)
-			pcs->phylink = pl;
+			if (pcs)
+				pcs->phylink = pl;
+		}
 
 		pl->pcs = pcs;

[Severity: Low]
Is it possible for the compiler to optimize this assignment inappropriately? Since phylink_pcs_change() reads this pointer locklessly from interrupt context, should we use WRITE_ONCE() here to prevent a potential data race?

[ ... ]

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

Quoted 9 lines
drivers/net/phy/phylink.c +5 -0
@@ -1865,6 +1976,16 @@ struct phylink *phylink_create(struct phylink_config *config,
 		return ERR_PTR(-EINVAL);
 	}
 
+	/*
+	 * Make sure either PCS internal validation or .mac_select_pcs
+	 * is used. Return error if both are defined.
+	 */
+	if (config->num_possible_pcs && pl->mac_ops->mac_select_pcs) {

[Severity: Critical]
Does this dereference an uninitialized pointer? The local variable pl has not been allocated yet. Should this use the function argument mac_ops->mac_select_pcs instead of pl->mac_ops->mac_select_pcs?

Quoted 27 lines
drivers/net/phy/phylink.c +17 -0
+		dev_err(config->dev,
+			"phylink: error: either phylink_config .num_possible_pcs or .mac_select_pcs must be used\n");
+		return ERR_PTR(-EINVAL);
+	}
+
 	pl = kzalloc_obj(*pl);
 	if (!pl)
 		return ERR_PTR(-ENOMEM);
@@ -1872,10 +1993,28 @@ 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 < 0) {
+		kfree(pl);
+		return ERR_PTR(ret);
+	}
+
+	/* Link available PCS to phylink */
+	list_for_each_entry(pcs, &pl->pcs_list, list)
+		pcs->phylink = pl;
 
 	phy_interface_copy(pl->supported_interfaces,
 			   config->supported_interfaces);

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

Quoted 10 lines
drivers/net/phy/phylink.c +6 -0
 
+	/* Update 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) {
 		pl->netdev = to_net_dev(config->dev);

[Severity: High]
If the configuration validation fails later in phylink_create() (for example, if config->type is invalid or phylink_parse_mode() fails), the error paths call kfree(pl) and return.

Does this leave the previously populated PCS objects with dangling phylink pointers and corrupted list nodes? If a MAC driver defers probe and retries, could this result in list corruption or a use-after-free when accessing the dangling pcs->phylink pointer?

[ ... ]

drivers/net/phy/phylink.c void phylink_pcs_change(struct phylink_pcs *pcs, bool up) · line 2571

Quoted 13 lines
drivers/net/phy/phylink.c +7 -2
@@ -2413,8 +2562,15 @@ void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
 {
 	struct phylink *pl = pcs->phylink;
 
-	if (pl)
-		phylink_link_changed(pl, up, "pcs");
+	/*
+	 * Ignore PCS link state change if the PCS is not
+	 * attached to a phylink instance or the phylink
+	 * instance is not currently using this PCS.
+	 */
+	if (!pl || pl->pcs != pcs)
+		return;

[Severity: Low]
Since pl->pcs is updated in process context without WRITE_ONCE(), and we read it locklessly here in interrupt context, should this use READ_ONCE(pl->pcs) to avoid a data race? Even if it's benign, the compiler might perform unexpected optimizations.

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

PATCH v7 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).
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 | 53 +++++++++++++++++++++++++++++++++++++++
 include/linux/phylink.h   |  2 ++
 2 files changed, 55 insertions(+)
drivers/net/phy/phylink.c +53 -0
index cb07184ce82f..ca4dad4b140a 100644
@@ -158,6 +158,7 @@ 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);
+static void phylink_pcs_disable(struct phylink_pcs *pcs);
 
 /**
  * phylink_set_port_modes() - set the port type modes in the ethtool mask
@@ -918,6 +919,58 @@ 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;
+
+	mutex_lock(&pl->state_mutex);
+
+	/* 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, disable the current PCS and set the
+	 * phylink PCS to NULL.
+	 */
+	if (pl->pcs == pcs) {
+		phylink_pcs_disable(pl->pcs);
+
+		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 ca9dfc142388..15e6b1a39dfe 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
#10 lore raw

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

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Lockless modification of `pl->pcs_list` races with the `phylink_resolve` workqueue, leading to…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Lockless modification of pl->pcs_list races with the phylink_resolve workqueue, leading to list corruption and use-after-free.
- [High] The active PCS is disabled prematurely before the MAC link is brought down, violating the standard teardown sequence.
- [Medium] pl->supported link modes are not updated when pl->supported_interfaces shrinks, leading to stale ethtool capabilities.
- [Medium] pl->sfp_interfaces is not updated when a PCS is released.
--

commit 0240edfdad0af399cf16b6105ab8d1a7a62254fb
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 4 lines
drivers/net/phy/phylink.c +0 -0
index cb07184ce82f3..ca4dad4b140a0 100644

[ ... ]

Quoted 18 lines
drivers/net/phy/phylink.c +14 -0
@@ -918,6 +919,58 @@ 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;
+

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

Quoted 10 lines
drivers/net/phy/phylink.c +10 -0
+	ASSERT_RTNL();
+
+	pl = pcs->phylink;
+	if (!pl)
+		return;
+
+	list_del(&pcs->list);
+	pcs->phylink = NULL;
+
+	mutex_lock(&pl->state_mutex);

[Severity: High]
Does this list_del() race with the phylink_resolve() workqueue?

Since phylink_resolve() does not hold rtnl_lock, it takes pl->state_mutex and traverses pl->pcs_list in phylink_major_config().

If list_del() executes completely locklessly with respect to the phylink_resolve() traversal, will the iterator follow poisoned list pointers, resulting in a page fault and kernel panic?

Also, in phylink_destroy(), does clearing the list before canceling the resolve workqueue without state_mutex open another identical race window?

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

Quoted 9 lines
drivers/net/phy/phylink.c +9 -0
+
+	/* 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, disable the current PCS and set the
+	 * phylink PCS to NULL.
+	 */
+	if (pl->pcs == pcs) {
+		phylink_pcs_disable(pl->pcs);

[Severity: High]
Is it safe to disable the active PCS here before the MAC link is brought down?

At this point, the MAC link has not been brought down (i.e. netif_carrier_off and MAC teardown haven't occurred) because phylink_link_down() is deferred to run inside phylink_resolve().

Could disabling the PCS while the MAC is still actively transmitting/receiving violate hardware sequencing and trigger bus stalls, dropped packets, or hardware hangs?

Quoted 4 lines
drivers/net/phy/phylink.c +4 -0
+
+		pl->force_major_config = true;
+		pl->pcs = NULL;
+	}

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

Quoted 10 lines
drivers/net/phy/phylink.c +10 -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);

[Severity: Medium]
Will ethtool advertise stale link modes since pl->supported is not updated?

This code correctly shrinks pl->supported_interfaces to exclude the released PCS's capabilities, but fails to correspondingly re-evaluate the global pl->supported link mode mask.

[Severity: Medium]
Should pl->sfp_interfaces also be updated when a PCS is released?

sfp_interfaces represents the intersection between the currently inserted SFP module's capabilities and pl->supported_interfaces. Since pl->supported_interfaces shrinks, not updating pl->sfp_interfaces might lead phylink to incorrectly validate or attempt to configure an SFP link interface mode that is now impossible to satisfy with the remaining hardware.

Quoted 4 lines
drivers/net/phy/phylink.c +4 -0
+
+	phylink_run_resolve(pl);
+}
+EXPORT_SYMBOL_GPL(phylink_release_pcs);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#11 lore raw

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

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

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

Function fwnode_phylink_pcs_count() will parse "pcs-handle" property and will return the number of PCS entries described in the passed firmware node.

Function fwnode_phylink_pcs_parse() will parse "pcs-handle" property and fill the passed available_pcs array with the available PCS found up to passed num_pcs value. It's worth to mention that this function will ignore PCS that still needs to be probed (returning -ENODEV) and such PCS won't be added to the available_pcs array.

---
 drivers/net/pcs/Kconfig          |   6 +
 drivers/net/pcs/Makefile         |   1 +
 drivers/net/pcs/pcs.c            | 208 +++++++++++++++++++++++++++++++
 include/linux/pcs/pcs-provider.h |  41 ++++++
 include/linux/pcs/pcs.h          |  75 +++++++++++
 5 files changed, 331 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 +208 -0

208 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 +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.53.0
#12 lore raw

Re: [PATCH net-next v7 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: - [Medium] Type mismatch in `fwnode_parse_pcsspec()` where a negative error code is assigned to an unsigned…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Type mismatch in fwnode_parse_pcsspec() where a negative error code is assigned to an unsigned integer, making the error check dead code.
- [Medium] fwnode_phylink_pcs_parse() incorrectly aborts on out-of-bounds instead of returning the parsed count, violating its 'up to num_pcs' contract.
- [Low] The fallback stub for fwnode_pcs_get() uses an incorrect parameter type.
--

commit 5e32951da9dab6acca331c331c1e41d55a580883
Author: Christian Marangi <[email protected]>

net: pcs: implement Firmware node support for PCS driver

This commit implements the foundation of Firmware node support for PCS drivers. It introduces a simple Provider API where a PCS driver can expose multiple PCS and consumers can access them using fwnode_pcs_get().

Quoted 4 lines
drivers/net/pcs/pcs.c new file +0 -0
new file mode 100644

[ ... ]

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

drivers/net/pcs/pcs.c line 96

Quoted 10 lines
drivers/net/pcs/pcs.c new file +10 -0
+	int ret;
+
+	if (!fwnode)
+		return -EINVAL;
+
+	if (name) {
+		index = fwnode_property_match_string(fwnode, "pcs-names",
+						     name);
+		if (index < 0)
+			return index;

[Severity: Medium]
Does this error check work as intended? fwnode_property_match_string() returns negative error codes like -ENODATA, but it is assigned to index which is an unsigned int. The check for index < 0 will be dead code, potentially leading to an out-of-bounds query that returns -ENOENT.

Quoted 5 lines
drivers/net/pcs/pcs.c new file +5 -0
+	}
+
+	ret = fwnode_property_get_reference_args(fwnode, "pcs-handle",
+						 "#pcs-cells",
+						 -1, index, out_args);

[ ... ]

Quoted 15 lines
drivers/net/pcs/pcs.c new file +15 -0
+int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode,
+			     struct phylink_pcs **available_pcs,
+			     unsigned int num_pcs)
+{
+	unsigned int i, found = 0;
+
+	if (!available_pcs)
+		return -EINVAL;
+
+	if (!fwnode_property_present(fwnode, "pcs-handle"))
+		return -ENODEV;
+
+	for (i = 0; i < num_pcs; i++) {
+		struct phylink_pcs *pcs;
+

drivers/net/pcs/pcs.c line 199

Quoted 10 lines
drivers/net/pcs/pcs.c new file +10 -0
+		pcs = fwnode_pcs_get(fwnode, i);
+		if (IS_ERR(pcs)) {
+			/*
+			 * Ignore -ENODEV error for PCS that still
+			 * needs to probe.
+			 */
+			if (PTR_ERR(pcs) == -ENODEV)
+				continue;
+
+			return PTR_ERR(pcs);

[Severity: Medium]
If the firmware node contains fewer pcs-handle properties than num_pcs, won't fwnode_pcs_get() eventually return -ENOENT? Since this loop only ignores -ENODEV, does this cause the function to abort entirely and return an error instead of returning the successfully parsed count?

Quoted 9 lines
drivers/net/pcs/pcs.c new file +9 -0
+		}
+
+		available_pcs[found] = pcs;
+		found++;
+	}
+
+	return found;
+}
+EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_parse);

[ ... ]

Quoted 4 lines
include/linux/pcs/pcs.h new file +0 -0
new file mode 100644

[ ... ]

include/linux/pcs/pcs.h line 57

Quoted 3 lines
include/linux/pcs/pcs.h new file +3 -0
+#else
+static inline struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode,
+						 int index)

[Severity: Low]
Is this intentional? The fallback stub for fwnode_pcs_get() uses int index, but the active declaration and implementation use unsigned int index.

Quoted 3 lines
include/linux/pcs/pcs.h new file +3 -0
+{
+	return ERR_PTR(-ENOENT);
+}

[ ... ]

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

PATCH v7 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 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     | 49 ++++++++++++++++++++++++++++++++
 drivers/net/phy/phylink.c | 57 +++++++++++++++++++++++++++++++++++++
 include/linux/pcs/pcs.h   | 60 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 166 insertions(+)
drivers/net/pcs/pcs.c +49 -0
index 67f9716f48fd..713b2ec22c97 100644
@@ -22,6 +22,19 @@ struct fwnode_pcs_provider {
 
 static LIST_HEAD(fwnode_pcs_providers);
 static DEFINE_MUTEX(fwnode_pcs_mutex);
+static BLOCKING_NOTIFIER_HEAD(fwnode_pcs_notify_list);
+
+int register_fwnode_pcs_notifier(struct notifier_block *nb)
+{
+	return blocking_notifier_chain_register(&fwnode_pcs_notify_list, nb);
+}
+EXPORT_SYMBOL_GPL(register_fwnode_pcs_notifier);
+
+int unregister_fwnode_pcs_notifier(struct notifier_block *nb)
+{
+	return blocking_notifier_chain_unregister(&fwnode_pcs_notify_list, nb);
+}
+EXPORT_SYMBOL_GPL(unregister_fwnode_pcs_notifier);
 
 struct phylink_pcs *fwnode_pcs_simple_get(struct fwnode_reference_args *pcsspec,
 					  void *data)
@@ -55,6 +68,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);
@@ -150,6 +167,38 @@ struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, unsigned int in
 }
 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) {
+			fwnode_handle_put(pcsspec.fwnode);
+			break;
+		}
+
+		/* Check the next PCS reference */
+		fwnode_handle_put(pcsspec.fwnode);
+		index++;
+	}
+
+	return fwnode_pcs_get(fwnode, index);
+}
+EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_get_from_fwnode);
+
 unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode)
 {
 	struct fwnode_reference_args out_args;
drivers/net/phy/phylink.c +57 -0

87 lines not on this page · raw message

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

85 lines not on this page · raw message

Signature · 1 line
2.53.0
#14 lore raw

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

Hi Christian,

Quoted 28 lines · 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 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/phy/phylink.c void phylink_destroy(struct phylink *pl) · line 2209

Quoted 7 lines
drivers/net/phy/phylink.c +3 -0
@@ -2151,6 +2204,10 @@ void phylink_destroy(struct phylink *pl)
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
 
+	/* Unregister notifier for late PCS attach */
+	if (pl->fwnode_pcs_nb.notifier_call)
+		unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);

I wanted to try this out, but I get :

drivers/net/phy/phylink.c:2218:17: error: implicit declaration of function ‘unregister_fwnode_pcs_notifier’; did you mean ‘register_fwnode_pcs_notifier’? [-Werror=implicit-function-declaration]

 2218 |                 unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                 register_fwnode_pcs_notifier

I guess you either need to stub this, or there's a missing Kconfig dependency somewhere

Maxime

#15 lore raw

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

Quoted 44 lines · Maxime Chevallier

Hi Christian,

Quoted 28 lines · 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 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/phy/phylink.c void phylink_destroy(struct phylink *pl) · line 2209

Quoted 7 lines
drivers/net/phy/phylink.c +3 -0
@@ -2151,6 +2204,10 @@ void phylink_destroy(struct phylink *pl)
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
 
+	/* Unregister notifier for late PCS attach */
+	if (pl->fwnode_pcs_nb.notifier_call)
+		unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);

I wanted to try this out, but I get :

Quoted 8 lines

drivers/net/phy/phylink.c:2218:17: error: implicit declaration of function ‘unregister_fwnode_pcs_notifier’; did you mean ‘register_fwnode_pcs_notifier’? [-Werror=implicit-function-declaration]

 2218 |                 unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                 register_fwnode_pcs_notifier

I guess you either need to stub this, or there's a missing Kconfig dependency somewhere

Hi yes if you want toi test just enable CONFIG_FWNODE_PCS. I forgot to add the static declaration for unregister_fwnode_pcs_notifier.

Signature · 1 line
	Ansuel
#16 lore raw

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

Quoted 50 lines · Christian Marangi
Quoted 49 lines · Maxime Chevallier

Hi Christian,

Quoted 28 lines · 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 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/phy/phylink.c void phylink_destroy(struct phylink *pl) · line 2209

Quoted 7 lines
drivers/net/phy/phylink.c +3 -0
@@ -2151,6 +2204,10 @@ void phylink_destroy(struct phylink *pl)
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
 
+	/* Unregister notifier for late PCS attach */
+	if (pl->fwnode_pcs_nb.notifier_call)
+		unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);

I wanted to try this out, but I get :

drivers/net/phy/phylink.c:2218:17: error: implicit declaration of function ‘unregister_fwnode_pcs_notifier’; did you mean ‘register_fwnode_pcs_notifier’? [-Werror=implicit-function-declaration]

 2218 |                 unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                 register_fwnode_pcs_notifier
Quoted 6 lines
Quoted 3 lines

I guess you either need to stub this, or there's a missing Kconfig dependency somewhere

Hi yes if you want toi test just enable CONFIG_FWNODE_PCS. I forgot to add the static declaration for unregister_fwnode_pcs_notifier.

I'll give it a go with this yeah, I have a few devices here I'd like to try this on.

Can you CC me for the next rounds ?

Maxime

Quoted 1 line
#17 lore raw

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

Quoted 57 lines · Maxime Chevallier
Quoted 54 lines · Christian Marangi
Quoted 52 lines · Maxime Chevallier

Hi Christian,

Quoted 28 lines · 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 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/phy/phylink.c void phylink_destroy(struct phylink *pl) · line 2209

Quoted 7 lines
drivers/net/phy/phylink.c +3 -0
@@ -2151,6 +2204,10 @@ void phylink_destroy(struct phylink *pl)
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
 
+	/* Unregister notifier for late PCS attach */
+	if (pl->fwnode_pcs_nb.notifier_call)
+		unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);

I wanted to try this out, but I get :

drivers/net/phy/phylink.c:2218:17: error: implicit declaration of function ‘unregister_fwnode_pcs_notifier’; did you mean ‘register_fwnode_pcs_notifier’? [-Werror=implicit-function-declaration]

 2218 |                 unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                 register_fwnode_pcs_notifier

I guess you either need to stub this, or there's a missing Kconfig dependency somewhere

Quoted 8 lines
Quoted 2 lines

Hi yes if you want toi test just enable CONFIG_FWNODE_PCS. I forgot to add the static declaration for unregister_fwnode_pcs_notifier.

I'll give it a go with this yeah, I have a few devices here I'd like to try this on.

Can you CC me for the next rounds ?

Sure, if you can would be good to check also the other commit if everything is logically correct.

BTW by checking test from patchwork I can see also another fix is needed if you want to test this revision.

phylink_create()

-if (config->num_possible_pcs && pl->mac_ops->mac_select_pcs) { +if (config->num_possible_pcs && mac_ops->mac_select_pcs) {

Aside from these change I expect the current legacy code to be not affected by these change.

Signature · 1 line
	Ansuel
#18 lore raw

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

Quoted 61 lines · Christian Marangi
Quoted 60 lines · Maxime Chevallier
Quoted 56 lines · Christian Marangi
Quoted 44 lines · Maxime Chevallier

Hi Christian,

Quoted 28 lines · 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 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/phy/phylink.c void phylink_destroy(struct phylink *pl) · line 2209

Quoted 7 lines
drivers/net/phy/phylink.c +3 -0
@@ -2151,6 +2204,10 @@ void phylink_destroy(struct phylink *pl)
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
 
+	/* Unregister notifier for late PCS attach */
+	if (pl->fwnode_pcs_nb.notifier_call)
+		unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);

I wanted to try this out, but I get :

Quoted 8 lines

drivers/net/phy/phylink.c:2218:17: error: implicit declaration of function ‘unregister_fwnode_pcs_notifier’; did you mean ‘register_fwnode_pcs_notifier’? [-Werror=implicit-function-declaration]

 2218 |                 unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                 register_fwnode_pcs_notifier

I guess you either need to stub this, or there's a missing Kconfig dependency somewhere

Hi yes if you want toi test just enable CONFIG_FWNODE_PCS. I forgot to add the static declaration for unregister_fwnode_pcs_notifier.

Quoted 8 lines
Quoted 5 lines

I'll give it a go with this yeah, I have a few devices here I'd like to try this on.

Can you CC me for the next rounds ?

Sure, if you can would be good to check also the other commit if everything is logically correct.

Absolutely, I'm planning on doing this :) This is quite the series though, I'll try to give it some deeper look in the upcoming days, I expect net-next to close soon anyways, so that's as good as it gets for reviewing this :)

Also, it's probably a good idea to have Sean in CC as well, I know a few people interested in the Xilinx PCS his series brought, hopefully he can follow-up on this series with the other new PCS drivers.

Review

Quoted 8 lines

BTW by checking test from patchwork I can see also another fix is needed if you want to test this revision.

phylink_create()

(quoted diff) +1 -1
-if (config->num_possible_pcs && pl->mac_ops->mac_select_pcs) {
+if (config->num_possible_pcs && mac_ops->mac_select_pcs) {

Ack, I'll test it with this change, thanks :)

Maxime

Quoted 3 lines

Aside from these change I expect the current legacy code to be not affected by these change.

I'll probably have things to say about this "legacy" naming :)

Thanks for driving this work forwards anyway, that's

Quoted 1 line
#19 lore raw

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

In reply to Christian Marangi
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Use-After-Free in PCS global notifier list due to missing unregister in `phylink_create()` error…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Use-After-Free in PCS global notifier list due to missing unregister in phylink_create() error paths.
- [High] NULL pointer dereference due to uninitialized pl->config in the PCS notifier callback.
- [High] List corruption race between phylink_create() and fwnode_pcs_add_provider().
- [High] Use-After-Free of phylink_pcs structure in pcs_provider_notify() due to missing reference counting.
- [Medium] TOCTOU race condition in phylink_create() causes missed PCS attachments.
--

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

net: phylink: support late PCS provider attach

This patch adds support for late PCS provider attachment by introducing a global notifier. Phylink instances subscribe to this notifier and react to FWNODE_PCS_PROVIDER_ADD events by linking the new PCS and refreshing supported interfaces.

Quoted 4 lines
drivers/net/phy/phylink.c +0 -0
index ca4dad4b140a0..0734c98498a99 100644

[ ... ]

Quoted 6 lines
drivers/net/phy/phylink.c +2 -0
@@ -1997,6 +1999,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)

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

Quoted 10 lines
drivers/net/phy/phylink.c +10 -0
+{
+	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.
+	 */
+	pcs = fwnode_phylink_pcs_get_from_fwnode(dev_fwnode(pl->config->dev),
+						 pcs_fwnode);

[Severity: High]
Could this cause a NULL pointer dereference?

The notifier is registered in phylink_create() before pl->config is initialized. If a concurrent thread adds a PCS provider immediately after registration, won't pcs_provider_notify() attempt to access pl->config->dev while it is still NULL?

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

Quoted 5 lines
drivers/net/phy/phylink.c +5 -0
+	if (IS_ERR(pcs))
+		return NOTIFY_DONE;
+
+	/* Add the PCS */
+	rtnl_lock();

[Severity: High]
Does this code risk a use-after-free?

The phylink_pcs pointer is retrieved without incrementing a reference count, and then the thread can sleep while waiting for rtnl_lock().

If the module providing the PCS is concurrently unbound during this wait, freeing the phylink_pcs structure, will this code dereference the freed pointer once the lock is acquired?

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

Quoted 5 lines
drivers/net/phy/phylink.c +5 -0
+
+	mutex_lock(&pl->state_mutex);
+
+	/* Link PCS with phylink */
+	list_add(&pcs->list, &pl->pcs_list);

[Severity: High]
Is it possible for the list to become corrupted here?

If phylink_fill_available_pcs() finds the newly added PCS and links it to pl->pcs_list during phylink_create(), and then the delayed FWNODE_PCS_PROVIDER_ADD notification fires right after, won't pcs_provider_notify() unconditionally add the exact same pcs structure to the list again?

[ ... ]

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

Quoted 10 lines
drivers/net/phy/phylink.c +6 -0
@@ -2068,6 +2115,12 @@ struct phylink *phylink_create(struct phylink_config *config,
 				 pl->supported_interfaces,
 				 pcs->supported_interfaces);
 
+	/* Register notifier for late PCS attach */
+	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: Medium]
Is there a race condition window here?

If a new PCS provider is added concurrently between the earlier call to phylink_fill_available_pcs() and the registration of the notifier here, will the FWNODE_PCS_PROVIDER_ADD event fire before the notifier is active, causing the PCS to be permanently missed?

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

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

[Severity: High]
If phylink_create() encounters an error later in the function (like an invalid config->type or phylink_parse_mode() failing), does this leave a dangling pointer in the global notifier chain since unregister_fwnode_pcs_notifier() isn't called before kfree(pl)?

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

PATCH v7 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   | 229 +++++++++++++++++++++++++++++
 2 files changed, 230 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 +229 -0

229 lines not on this page · raw message

Signature · 1 line
2.53.0
#21 lore raw

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

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Error paths in `phylink_create()` free the `phylink` struct without unregistering the PCS…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Error paths in phylink_create() free the phylink struct without unregistering the PCS notifier.
- [High] The PCS notifier is registered before the phylink instance is fully initialized, allowing concurrent notifications to dereference a NULL pointer.
- [Medium] Always-false unsigned comparison in fwnode_parse_pcsspec() masks actual error returns.
- [Medium] fwnode_phylink_pcs_parse() fails completely if num_pcs exceeds the actual number of entries in the device tree.
--

commit 8a4a6df4b5091984f40b8415d9bca38cf005d9f4
Author: Christian Marangi <[email protected]>

net: Document PCS subsystem

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

Quoted 6 lines
drivers/net/pcs/pcs.c new file +0 -0
new file mode 100644
index 0000000000000..713b2ec22c97e
@@ -0,0 +1,257 @@

[ ... ]

drivers/net/pcs/pcs.c

Quoted 13 lines
drivers/net/pcs/pcs.c new file +13 -0
+static int fwnode_parse_pcsspec(const struct fwnode_handle *fwnode,
+				unsigned int index, const char *name,
+				struct fwnode_reference_args *out_args)
+{
+	int ret;
+
+	if (!fwnode)
+		return -EINVAL;
+
+	if (name) {
+		index = fwnode_property_match_string(fwnode, "pcs-names",
+						     name);
+		if (index < 0)

[Severity: Medium]
Since index is declared as an unsigned int, will this check for < 0 always be false, potentially masking error returns from fwnode_property_match_string()?

Quoted 11 lines
drivers/net/pcs/pcs.c new file +11 -0
+			return index;
+	}
+
+	ret = fwnode_property_get_reference_args(fwnode, "pcs-handle",
+						 "#pcs-cells",
+						 -1, index, out_args);
+	if (ret || (name && index < 0))
+		return ret;
+
+	return 0;
+}

[ ... ]

Quoted 15 lines
drivers/net/pcs/pcs.c new file +15 -0
+int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode,
+			     struct phylink_pcs **available_pcs,
+			     unsigned int num_pcs)
+{
+	unsigned int i, found = 0;
+
+	if (!available_pcs)
+		return -EINVAL;
+
+	if (!fwnode_property_present(fwnode, "pcs-handle"))
+		return -ENODEV;
+
+	for (i = 0; i < num_pcs; i++) {
+		struct phylink_pcs *pcs;
+

drivers/net/pcs/pcs.c

Quoted 10 lines
drivers/net/pcs/pcs.c new file +10 -0
+		pcs = fwnode_pcs_get(fwnode, i);
+		if (IS_ERR(pcs)) {
+			/*
+			 * Ignore -ENODEV error for PCS that still
+			 * needs to probe.
+			 */
+			if (PTR_ERR(pcs) == -ENODEV)
+				continue;
+
+			return PTR_ERR(pcs);

[Severity: Medium]
If num_pcs exceeds the actual number of entries in the device tree array, fwnode_pcs_get() will return -ENOENT for the out-of-bounds index.

Will returning the error here cause the entire function to abort instead of breaking gracefully and returning the valid entries found so far?

Quoted 14 lines
drivers/net/pcs/pcs.c new file +9 -0
+		}
+
+		available_pcs[found] = pcs;
+		found++;
+	}
+
+	return found;
+}
+EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_parse);
drivers/net/phy/phylink.c +0 -0
index 087ac63f9193d..0734c98498a99 100644

[ ... ]

Quoted 1 line
drivers/net/phy/phylink.c +0 -0
@@ -1867,6 +2093,33 @@ struct phylink *phylink_create(struct phylink_config *config,

[ ... ]

drivers/net/phy/phylink.c

Quoted 7 lines
drivers/net/phy/phylink.c +5 -0
+	/* Register notifier for late PCS attach */
+	if (!phy_interface_empty(config->pcs_interfaces)) {
+		pl->fwnode_pcs_nb.notifier_call = pcs_provider_notify;
+		register_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);
+	}
 
 	pl->config = config;

[Severity: High]
If a PCS provider is registered concurrently right after the notifier is added, pcs_provider_notify() could execute before pl->config is initialized here.

Will the callback attempt to dereference a NULL pl->config->dev and crash?

[Severity: High]
If an error occurs later in phylink_create() (for example, if phylink_parse_mode(), phylink_parse_fixedlink(), or phylink_register_sfp() fail), the function calls kfree(pl) and returns.

Does this error path leave the notifier registered to freed memory, leading to a use-after-free regression when the next PCS provider is added?

Quoted 1 line

if (config->type == PHYLINK_NETDEV) {

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

PATCH v7 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 cc1dde0c9067..ef3ef5096d08 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
#23 lore raw

PATCH v7 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
#24 lore raw

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

In reply to Christian Marangi

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

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

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

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

---
 drivers/net/phy/phylink.c | 9 +++++++++
 include/linux/phylink.h   | 2 ++
 2 files changed, 11 insertions(+)
drivers/net/phy/phylink.c +9 -0
index 0734c98498a9..1bedac517d21 100644
@@ -1035,6 +1035,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)
@@ -1736,6 +1742,9 @@ static void phylink_link_down(struct phylink *pl)
 
 	pl->mac_ops->mac_link_down(pl->config, pl->act_link_an_mode,
 				   pl->cur_interface);
+
+	phylink_pcs_link_down(pl->pcs);
+
 	phylink_info(pl, "Link is Down\n");
 }
 
include/linux/phylink.h +2 -0
index 15e6b1a39dfe..eb6c6ca34147 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: tear down link between MAC and PCS.
  * @pcs_disable_eee: optional notification to PCS that EEE has been disabled
  *		     at the MAC.
  * @pcs_enable_eee: optional notification to PCS that EEE will be enabled at
@@ -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
#25 lore raw

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

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Data Race and Use-After-Free in `phylink_destroy()` due to incorrect cancellation order - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Data Race and Use-After-Free in phylink_destroy() due to incorrect cancellation order
- [Medium] Missing Link Teardown Callback (pcs_link_down) on PCS release
- [Low] Missing Kernel-Doc for pcs_link_down
--

commit 33892cef9bd3c637bb8d5d91bf19b186872ff8bf
Author: Christian Marangi <[email protected]>

net: phylink: add .pcs_link_down PCS OP

Permit for PCS driver to define specific operation to tear down the link between the MAC and the PCS. This might be needed for some PCS that reset counter or require special reset to correctly work if the link needs to be restored later.

Quoted 4 lines
drivers/net/phy/phylink.c +0 -0
index 0734c98498a99..1bedac517d213 100644

[ ... ]

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

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

[Severity: Medium]
Will this callback be missed if a PCS is released?

In phylink_release_pcs(), pl->pcs is disabled and set to NULL while the link is logically UP:

	if (pl->pcs == pcs) {
		phylink_pcs_disable(pl->pcs);
		pl->force_major_config = true;
		pl->pcs = NULL;
	}

When this triggers phylink_resolve(), which attempts to bring the link down using phylink_link_down(), pl->pcs is already NULL. The released PCS never receives its .pcs_link_down() callback, which might break link lifecycle symmetry and fail to reset internal counters.

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

Quoted 13 lines
drivers/net/phy/phylink.c +1 -0
+
 	phylink_info(pl, "Link is Down\n");
 }
 
include/linux/phylink.h +1 -0
index 15e6b1a39dfe2..eb6c6ca34147f 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: tear down link between MAC and PCS.

[Severity: Low]
Is there a missing kernel-doc entry for the function signature of pcs_link_down?

The @pcs_link_down callback is added here, but the detailed function signature appears to be missing from the corresponding "#if 0" block in include/linux/phylink.h (for kernel-doc purposes only), where other PCS callbacks like pcs_link_up() are fully documented.

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

Quoted 3 lines
include/linux/phylink.h +0 -0
  * @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

[ ... ]

[Severity: High]
Does the incorrect cancellation order in phylink_destroy() cause a data race and use-after-free?

In phylink_destroy(), pl->pcs_list is modified without holding pl->state_mutex:

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

If the background work pl->resolve is currently running, the worker thread executes phylink_major_config() and iterates over pl->pcs_list while holding pl->state_mutex. Because phylink_destroy() modifies the list concurrently without synchronization and before canceling the work, the worker thread can traverse corrupted list pointers, resulting in a use-after-free. Should cancel_work_sync(&pl->resolve) be called before modifying the list?

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

PATCH v7 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          | 261 ++++++++++++++++++
 1 file changed, 261 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +261 -0
new file mode 100644
index 000000000000..9c1d116c1b01
@@ -0,0 +1,261 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/pcs/airoha,pcs.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Airoha Ethernet PCS and Serdes
+
+maintainers:
+  - Christian Marangi <[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:
+    maxItems: 1
+
+  "#pcs-cells": true
+
+required:
+  - compatible
+  - reg
+  - reg-names
+  - "#pcs-cells"
+
+allOf:
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - airoha,an7581-pcs-eth
+              - airoha,an7581-pcs-pon
+
+    then:
+      properties:
+        reg:
+          items:
+            - description: PCS MAC reg
+            - description: HSGMII AN reg
+            - description: HSGMII PCS reg
+            - description: MULTI SGMII reg
+            - description: USXGMII reg
+            - description: HSGMII rate adaption reg
+            - description: PCS Analog register
+            - description: PCS PMA (Physical Medium Attachment) register
+
+        reg-names:
+          items:
+            - const: pcs_mac
+            - const: hsgmii_an
+            - const: hsgmii_pcs
+            - const: multi_sgmii
+            - const: usxgmii
+            - const: hsgmii_rate_adp
+            - const: pcs_ana
+            - const: pcs_pma
+
+        phys: false
+
+        "#pcs-cells":
+          const: 0
+
+      required:
+        - airoha,scu
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: airoha,an7581-pcs-pcie
+
+    then:
+      properties:
+        reg:
+          items:
+            - description: PCS MAC 0 reg
+            - description: HSGMII AN 0 reg
+            - description: HSGMII PCS 0 reg
+            - description: MULTI SGMII 0 reg
+            - description: USXGMII 0 reg
+            - description: HSGMII rate adaption 0 reg
+            - description: PCS MAC 1 reg
+            - description: HSGMII AN 1 reg
+            - description: HSGMII PCS 1 reg
+            - description: MULTI SGMII 1 reg
+            - description: USXGMII 1 reg
+            - description: HSGMII rate adaption 1 reg
+            - description: PCS Analog register
+            - description: PCS PMA (Physical Medium Attachment) 0 register
+            - description: PCS PMA (Physical Medium Attachment) 1 register
+
+        reg-names:
+          items:
+            - const: pcs_mac0
+            - const: hsgmii_an0
+            - const: hsgmii_pcs0
+            - const: multi_sgmii0
+            - const: usxgmii0
+            - const: hsgmii_rate_adp0
+            - const: pcs_mac1
+            - const: hsgmii_an1
+            - const: hsgmii_pcs1
+            - const: multi_sgmii1
+            - const: usxgmii1
+            - const: hsgmii_rate_adp1
+            - const: pcs_ana
+            - const: pcs_pma0
+            - const: pcs_pma1
+
+        phys: false
+
+        "#pcs-cells":
+          const: 1
+
+      required:
+        - airoha,scu
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: airoha,an7581-pcs-usb
+
+    then:
+      properties:
+        reg:
+          items:
+            - description: PCS MAC reg
+            - description: HSGMII AN reg
+            - description: HSGMII PCS reg
+            - description: MULTI SGMII reg
+            - description: HSGMII rate adaption reg
+            - description: PCS Analog register
+
+        reg-names:
+          items:
+            - const: pcs_mac
+            - const: hsgmii_an
+            - const: hsgmii_pcs
+            - const: multi_sgmii
+            - const: hsgmii_rate_adp
+            - const: pcs_ana
+
+        airoha,scu: false
+
+        "#pcs-cells":
+          const: 0
+
+      required:
+        - phys
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/phy/phy.h>
+
+    pcs@1fa08000 {
+      compatible = "airoha,an7581-pcs-pon";
+      reg = <0x1fa08000 0x1000>,
+            <0x1fa80000 0x60>,
+            <0x1fa80a00 0x164>,
+            <0x1fa84000 0x450>,
+            <0x1fa85900 0x338>,
+            <0x1fa86000 0x300>,
+            <0x1fa8a000 0x1000>,
+            <0x1fa8b000 0x1000>;
+      reg-names = "pcs_mac", "hsgmii_an", "hsgmii_pcs",
+                  "multi_sgmii", "usxgmii",
+                  "hsgmii_rate_adp", "pcs_ana", "pcs_pma";
+
+      airoha,scu = <&scuclk>;
+      #pcs-cells = <0>;
+    };
+
+    pcs@1fa09000 {
+      compatible = "airoha,an7581-pcs-eth";
+      reg = <0x1fa09000 0x1000>,
+            <0x1fa70000 0x60>,
+            <0x1fa70a00 0x164>,
+            <0x1fa74000 0x450>,
+            <0x1fa75900 0x338>,
+            <0x1fa76000 0x300>,
+            <0x1fa7a000 0x1000>,
+            <0x1fa7b000 0x1000>;
+      reg-names = "pcs_mac", "hsgmii_an", "hsgmii_pcs",
+                  "multi_sgmii", "usxgmii",
+                  "hsgmii_rate_adp", "pcs_ana", "pcs_pma";
+
+      airoha,scu = <&scuclk>;
+      #pcs-cells = <0>;
+    };
+
+    pcs@1fa04000 {
+      compatible = "airoha,an7581-pcs-pcie";
+      reg = <0x1fa04000 0x1000>,
+            <0x1fa50000 0x60>,
+            <0x1fa50a00 0x164>,
+            <0x1fa54000 0x450>,
+            <0x1fa55900 0x338>,
+            <0x1fa56000 0x300>,
+            <0x1fa05000 0x1000>,
+            <0x1fa60000 0x60>,
+            <0x1fa60a00 0x164>,
+            <0x1fa64000 0x450>,
+            <0x1fa65900 0x338>,
+            <0x1fa66000 0x300>,
+            <0x1fa5a000 0x1000>,
+            <0x1fa5b000 0x1000>,
+            <0x1fa5c000 0x1000>;
+      reg-names = "pcs_mac0", "hsgmii_an0", "hsgmii_pcs0",
+                  "multi_sgmii0", "usxgmii0",
+                  "hsgmii_rate_adp0",
+                  "pcs_mac1", "hsgmii_an1", "hsgmii_pcs1",
+                  "multi_sgmii1", "usxgmii1",
+                  "hsgmii_rate_adp1",
+                  "pcs_ana", "pcs_pma0", "pcs_pma1";
+
+      airoha,scu = <&scuclk>;
+      #pcs-cells = <1>;
+    };
+
+    pcs@1fa07000 {
+      compatible = "airoha,an7581-pcs-usb";
+      reg = <0x1fa07000 0x1000>,
+            <0x1fa90000 0x60>,
+            <0x1fa90a00 0x164>,
+            <0x1fa94000 0x450>,
+            <0x1fa96000 0x300>,
+            <0x1fa9a000 0x600>;
+      reg-names = "pcs_mac", "hsgmii_an", "hsgmii_pcs",
+                  "multi_sgmii", "hsgmii_rate_adp","pcs_ana";
+
+      phys = <&usb0_phy PHY_TYPE_USB3>;
+
+      #pcs-cells = <0>;
+    };
Signature · 1 line
2.53.0
#27 lore raw

PATCH v7 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 | 1318 ++++++++++++
 drivers/net/pcs/airoha/pcs-airoha.h        | 1309 ++++++++++++
 drivers/net/pcs/airoha/pcs-an7581.c        | 2093 ++++++++++++++++++++
 7 files changed, 4743 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..c5201d5cc356
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_PCS_AIROHA)	:= 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 +1318 -0

1,318 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

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

#28 lore raw

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

Hi.

Quoted 17 lines · 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 | 1318 ++++++++++++
  drivers/net/pcs/airoha/pcs-airoha.h        | 1309 ++++++++++++
  drivers/net/pcs/airoha/pcs-an7581.c        | 2093 ++++++++++++++++++++
  7 files changed, 4743 insertions(+)
Quoted 5 lines

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

Most likely there will be pcs drivers for the EN7523 platform also. Can the common code for an7581 have an7581 in the name instead of airoha?

MvH

Benjamin Larsson

#29 lore raw

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

In reply to Christian Marangi
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider: - [High] Off-by-one error and lack of signedness check in `airoha_pcs_get` allows out-of-bounds array…

Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
- [High] Off-by-one error and lack of signedness check in airoha_pcs_get allows out-of-bounds array access.
- [High] Unconditional dereference of match_data during probe can cause a kernel panic if the driver is bound without a device tree node.
- [High] Switch statements for link speed leave critical register configuration variables completely uninitialized for unexpected speeds.
- [High] The acquired generic PHY for USB is never initialized or powered on.
- [High] Per-port interface mode is stored in the shared priv->interface global state, causing race conditions in multi-port devices.
- [High] Incorrect TX constant used for RX mode configuration, which breaks SGMII and 1000BASE-X on PCIe.
- [High] Per-port PCS operations modify shared device-level hardware state without synchronization, disrupting active ports.
- [Medium] Multiple state-reading functions fail to check the return value of regmap_read(), resulting in the use of uninitialized stack variables.
- [Low] The signal detection loop executes one more iteration than intended, potentially causing false positives.
- [Low] Assignments are separated by commas instead of semicolons, creating an unintended massive expression statement.
- [Low] The result of airoha_pcs_setup_scu, which performs critical reset assertions and deassertions, is silently ignored by airoha_pcs_pre_config. --

commit 7fccf4b912ca13e1f4f141393b6432c311a781e1
Author: Christian Marangi <[email protected]>

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

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.

Quoted 6 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +0 -0
new file mode 100644
index 0000000000000..5d3b136ed2b59
@@ -0,0 +1,1318 @@

[ ... ]

Quoted 13 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +13 -0
+static int airoha_pcs_setup_scu(struct airoha_pcs_priv *priv,
+				int index, phy_interface_t interface)
+{
+	const struct airoha_pcs_match_data *data = priv->data;
+	int ret;
+
+	switch (data->port_type) {
+	case AIROHA_PCS_ETH:
+		airoha_pcs_setup_scu_eth(priv, interface);
+		break;
+	case AIROHA_PCS_PON:
+		airoha_pcs_setup_scu_pon(priv, interface);
+		break;

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10 -0
+	case AIROHA_PCS_PCIE:
+		airoha_pcs_setup_scu_pcie(priv, index, interface);
+		break;
+	case AIROHA_PCS_USB:
+		break;
+	}
+
+	/* TODO better handle reset from MAC */
+	ret = reset_control_bulk_assert(ARRAY_SIZE(priv->rsts),
+					priv->rsts);

[Severity: High]
Does this assert the global bulk reset lines for the entire block regardless of which port is being configured?

Since the hardware supports up to two ports for PCIe, bringing up the second port will assert the shared MAC and PHY block reset lines, which could disrupt the already active port.

Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10 -0
+	if (ret)
+		return ret;
+
+	ret = reset_control_bulk_deassert(ARRAY_SIZE(priv->rsts),
+					  priv->rsts);
+	if (ret)
+		return ret;
+
+	return 0;
+}

[ ... ]

Quoted 15 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +15 -0
+static void airoha_pcs_get_state_usxgmii(struct airoha_pcs_priv *priv, int index,
+					 struct phylink_link_state *state)
+{
+	const struct airoha_pcs_match_data *data = priv->data;
+	struct airoha_pcs_maps *maps = &priv->maps[index];
+	u32 an_done, lpa;
+
+	/* Trigger HW workaround if needed. If an error is reported,
+	 * consider link down and test again later.
+	 */
+	if (data->rxlock_workaround && data->rxlock_workaround(priv, index)) {
+		state->link = false;
+		return;
+	}
+

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10 -0
+	/* Toggle AN Status */
+	regmap_set_bits(maps->usxgmii_pcs, AIROHA_PCS_USXGMII_PCS_AN_CONTROL_6,
+			AIROHA_PCS_USXGMII_TOG_PCS_AUTONEG_STS);
+	regmap_clear_bits(maps->usxgmii_pcs, AIROHA_PCS_USXGMII_PCS_AN_CONTROL_6,
+			  AIROHA_PCS_USXGMII_TOG_PCS_AUTONEG_STS);
+
+	regmap_read(maps->usxgmii_pcs, AIROHA_PCS_USXGMII_PCS_AN_STATS_0, &lpa);
+	regmap_read(maps->usxgmii_pcs, AIROHA_PCS_USXGMII_PCS_AN_STATS_2, &an_done);
+
+	state->link = !!(lpa & MDIO_USXGMII_LINK);

[Severity: Medium]
Can regmap_read() fail and leave lpa and an_done completely uninitialized?

The return values of the read operations are not checked. If a read fails, the uninitialized stack memory in these variables is then used in bitwise operations to report the link state, leading to unpredictable results.

Quoted 4 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +4 -0
+	state->an_complete = !!(an_done & AIROHA_PCS_USXGMII_PCS_AN_COMPLETE);
+
+	phylink_decode_usxgmii_word(state, lpa);
+}

[ ... ]

Quoted 5 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +5 -0
+static int airoha_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
+			     phy_interface_t interface,
+			     const unsigned long *advertising,
+			     bool permit_pause_to_mac)
+{

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10 -0
+	struct airoha_pcs_port *port = to_airoha_pcs_port(pcs);
+	struct airoha_pcs_priv *priv = port->priv;
+	const struct airoha_pcs_match_data *data;
+	struct airoha_pcs_maps *maps;
+	int index = port->index;
+	u32 rate_adapt;
+	int ret;
+
+	maps = &priv->maps[port->index];
+	priv->interface = interface;

[Severity: High]
Does storing the interface mode in the shared priv structure cause race conditions or state corruption for multi-port devices?

Since PCIe configuration uses two ports, configuring the second port will overwrite the mode for the first port. When airoha_pcs_an_restart() is later called, it will read the wrong interface mode. Should this state be stored per-port in the airoha_pcs_port structure instead?

Quoted 1 line
drivers/net/pcs/airoha/pcs-airoha-common.c new file +1 -0
+	data = priv->data;

[ ... ]

Quoted 12 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +12 -0
+static void airoha_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
+			       phy_interface_t interface, int speed, int duplex)
+{
+	struct airoha_pcs_port *port = to_airoha_pcs_port(pcs);
+	struct airoha_pcs_priv *priv = port->priv;
+	const struct airoha_pcs_match_data *data;
+	struct airoha_pcs_maps *maps;
+
+	maps = &priv->maps[port->index];
+	data = priv->data;
+
+	if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {

[ ... ]

Quoted 22 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +22 -0
+	} else {
+		if (interface == PHY_INTERFACE_MODE_USXGMII ||
+		    interface == PHY_INTERFACE_MODE_10GBASER) {
+			u32 mode;
+			u32 rate_adapt;
+
+			switch (speed) {
+			case SPEED_10000:
+				rate_adapt = AIROHA_PCS_HSGMII_RATE_ADPT_FORCE_RATE_ADAPT_MODE_10000;
+				mode = AIROHA_PCS_USXGMII_MODE_10000;
+				break;
+			case SPEED_5000:
+				rate_adapt = AIROHA_PCS_HSGMII_RATE_ADPT_FORCE_RATE_ADAPT_MODE_5000;
+				mode = AIROHA_PCS_USXGMII_MODE_5000;
+				break;
+			case SPEED_2500:
+				rate_adapt = AIROHA_PCS_HSGMII_RATE_ADPT_FORCE_RATE_ADAPT_MODE_2500;
+				mode = AIROHA_PCS_USXGMII_MODE_2500;
+				break;
+			case SPEED_1000:
+				rate_adapt = AIROHA_PCS_HSGMII_RATE_ADPT_FORCE_RATE_ADAPT_MODE_1000;
+				mode = AIROHA_PCS_USXGMII_MODE_1000;

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10 -0
+				break;
+			case SPEED_100:
+				rate_adapt = AIROHA_PCS_HSGMII_RATE_ADPT_FORCE_RATE_ADAPT_MODE_100;
+				mode = AIROHA_PCS_USXGMII_MODE_100;
+				break;
+			}
+
+			/* Force USXGMII to selected speed */
+			regmap_update_bits(maps->usxgmii_pcs, AIROHA_PCS_USXGMII_PCS_AN_CONTROL_7,
+					   AIROHA_PCS_USXGMII_MODE, mode);

[Severity: High]
Can an unhandled speed leave these local variables completely uninitialized?

The switch statements for speed lack a default case. For example, if SPEED_10 is passed in USXGMII mode, variables like mode and rate_adapt are never set. The code then proceeds to write these uninitialized stack variables into hardware control registers via regmap_update_bits().

Quoted 3 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +3 -0
+
+			if (interface == PHY_INTERFACE_MODE_10GBASER)
+				regmap_update_bits(maps->hsgmii_rate_adp, AIROHA_PCS_HSGMII_RATE_ADAPT_CTRL_11,

[ ... ]

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

Quoted 11 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +11 -0
+static void airoha_pcs_pre_config(struct phylink_pcs *pcs,
+				  phy_interface_t interface)
+{
+	struct airoha_pcs_port *port = to_airoha_pcs_port(pcs);
+	struct airoha_pcs_priv *priv = port->priv;
+	struct airoha_pcs_maps *maps;
+
+	maps = &priv->maps[port->index];
+
+	/* Select HSGMII or USXGMII in SCU regs */
+	airoha_pcs_setup_scu(priv, port->index, interface);

[Severity: Low]
Is it safe to ignore the return value of airoha_pcs_setup_scu() here?

That function performs important reset assertions and deassertions. If reset_control_bulk_deassert() fails, the hardware stays in reset, but this function will silently proceed to perform register writes against a reset block.

Quoted 3 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +3 -0
+
+	/* MPI MBI disable */
+	regmap_set_bits(maps->pcs_mac, AIROHA_PCS_XFI_MAC_XFI_GIB_CFG,

[ ... ]

Quoted 4 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +4 -0
+static int airoha_pcs_init_named_regmap(struct platform_device *pdev,
+					const char *name, struct regmap **regmap)
+{
+	struct regmap_config regmap_config = { };

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10 -0
+	void *base;
+
+	base = devm_platform_ioremap_resource_byname(pdev, name);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+
+	regmap_config.name = name;
+	regmap_config.reg_bits = 32,
+	regmap_config.val_bits = 32,
+	regmap_config.reg_stride = 4,

[Severity: Low]
Are these comma operators intentional?

The regmap_config fields are being initialized using commas instead of semicolons. This creates a single expression rather than distinct statements, which might confuse human readers and static analysis tools.

Quoted 5 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +5 -0
+
+	*regmap = devm_regmap_init_mmio(&pdev->dev, base, &regmap_config);
+
+	return PTR_ERR_OR_ZERO(*regmap);
+}

[ ... ]

Quoted 25 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +25 -0
+static struct phylink_pcs *airoha_pcs_get(struct fwnode_reference_args *pcsspec,
+					  void *data)
+{
+	struct airoha_pcs_priv *priv = data;
+	struct device *dev = priv->dev;
+	int index = 0;
+
+	switch (priv->data->port_type) {
+	case AIROHA_PCS_ETH:
+	case AIROHA_PCS_PON:
+	case AIROHA_PCS_USB:
+		if (pcsspec->nargs) {
+			dev_err(dev, "invalid number of cells in 'pcs-handle' property\n");
+			return ERR_PTR(-EINVAL);
+		}
+
+		break;
+	case AIROHA_PCS_PCIE:
+		if (pcsspec->nargs != 1) {
+			dev_err(dev, "invalid number of cells in 'pcs-handle' property\n");
+			return ERR_PTR(-EINVAL);
+		}
+
+		break;
+	}

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10 -0
+
+	if (pcsspec->nargs)
+		index = pcsspec->args[0];
+
+	if (index > priv->data->num_port) {
+		dev_err(dev, "invalid index cell in 'pcs-handle' property\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	return &priv->ports[index].pcs;

[Severity: High]
Does this check allow an out-of-bounds array access?

The priv->ports array is sized by data->num_port. The check evaluates to false when index equals data->num_port, allowing access one element past the end of the array.

Also, since index is a signed integer initialized from a u64, could a very large unsigned value truncate to a negative number and bypass this check entirely?

Quoted 6 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +6 -0
+}
+
+static int airoha_pcs_probe(struct platform_device *pdev)
+{
+	const struct airoha_pcs_match_data *data;
+	struct device *dev = &pdev->dev;

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10 -0
+	struct airoha_pcs_priv *priv;
+	int index, ret;
+
+	data = of_device_get_match_data(dev);
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->ports = devm_kcalloc(dev, data->num_port,

[Severity: High]
Can of_device_get_match_data() return NULL here?

If the driver is bound manually via sysfs instead of through a device tree node, this will return NULL. The code then unconditionally dereferences data->num_port, which would cause a kernel panic. Should this include a NULL check?

Quoted 5 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +5 -0
+				   sizeof(*priv->ports), GFP_KERNEL);
+	if (!priv->ports)
+		return -ENOMEM;
+
+	priv->dev = dev;

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10 -0
+	priv->data = data;
+
+	if (data->port_type == AIROHA_PCS_USB) {
+		struct phy *phy;
+
+		phy = devm_phy_get(dev, NULL);
+		if (IS_ERR(phy))
+			return dev_err_probe(dev, PTR_ERR(phy), "failed to get phy\n");
+
+		priv->phy = phy;

[Severity: High]
Does this code acquire the PHY without ever initializing or powering it on?

The driver calls devm_phy_get() but there are no corresponding calls to phy_init() or phy_power_on(). Without these, the PHY hardware might remain unclocked or powered down, which could cause bus hangs when phy_set_mode_ext() is called later in an7581_pcs_usb_bringup().

Quoted 3 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +3 -0
+	}
+
+	switch (data->port_type) {

[ ... ]

Quoted 6 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +0 -0
new file mode 100644
index 0000000000000..638e20cac97ea
@@ -0,0 +1,2093 @@

[ ... ]

Quoted 19 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +19 -0
+static void an7581_pcs_jcpll_bringup(struct airoha_pcs_priv *priv,
+				     int index, phy_interface_t interface)
+{
+	struct regmap_field **pcs_ana_fields = priv->pcs_ana_fields[index];
+	struct regmap *pcs_pma;
+	u32 kband_vref;
+
+	switch (interface) {
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_1000BASEX:
+	case PHY_INTERFACE_MODE_2500BASEX:
+		kband_vref = 0x10;
+		break;
+	case PHY_INTERFACE_MODE_USXGMII:
+	case PHY_INTERFACE_MODE_10GBASER:
+		kband_vref = 0xf;
+		break;
+	default:
+		return;

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +10 -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]
Is it intentional to hardcode priv->pcs_pma[0] here even when configuring port 1?

This re-initializes the shared PLL whenever any port is brought up. For a dual-port PCIe configuration, initializing the second port will disrupt the shared PLL and cause clock loss for the first port.

Quoted 11 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +11 -0
+
+	/* Setup LDO */
+	usleep_range(200, 300);
+
+	regmap_field_set_bits(pcs_ana_fields[AN7581_PCS_JCPLL_SPARE_L],
+			      AIROHA_PCS_ANA_JCPLL_SPARE_L_LDO);
+
+	/* Setup RSTB */
+	regmap_field_write(pcs_ana_fields[AN7581_PCS_JCPLL_RST_DLY],
+			   AIROHA_PCS_ANA_JCPLL_RST_DLY_150_200);
+	regmap_field_write(pcs_ana_fields[AN7581_PCS_JCPLL_PLL_RSTB], 0x1);

[ ... ]

Quoted 16 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +16 -0
+static void an7581_pcs_rx_bringup(struct airoha_pcs_priv *priv,
+				  int index, phy_interface_t interface)
+{
+	struct regmap_field **pcs_ana_fields = priv->pcs_ana_fields[index];
+	struct regmap *pcs_pma = priv->pcs_pma[index];
+	u32 phyck_div, phyck_sel;
+	u32 pr_cdr_beta_dac;
+	u32 cdr_pr_buf_in_sr;
+	bool cdr_pr_cap_en;
+	u32 sigdet_vth_sel;
+	u32 rx_rate_ctrl;
+	u32 xfi_rx_mode;
+	u32 osr;
+
+	switch (interface) {
+	case PHY_INTERFACE_MODE_SGMII:

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

Quoted 10 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +10 -0
+	case PHY_INTERFACE_MODE_1000BASEX:
+		osr = BIT(1) | BIT(0); /* 1.25G */
+		pr_cdr_beta_dac = BIT(3);
+		rx_rate_ctrl = 0;
+		cdr_pr_cap_en = false;
+		cdr_pr_buf_in_sr = BIT(2) | BIT(1) | BIT(0);
+		sigdet_vth_sel = BIT(2) | BIT(1);
+		phyck_div = BIT(5) | BIT(3) | BIT(0);
+		phyck_sel = BIT(0);
+		xfi_rx_mode = AIROHA_PCS_PMA_XFI_TX_MODE_1G25;

[Severity: High]
Is this using a TX mode constant for an RX configuration?

AIROHA_PCS_PMA_XFI_TX_MODE_1G25 has a different bit layout (shifted to bit 3) compared to the RX mode mask which operates on bits 11 to 9.

When this is applied, the TX bits fall outside the RX mask and get filtered out. This will write 0 to the RX mode field, improperly configuring it to 10G3 instead of 1G25.

Quoted 2 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +2 -0
+		break;
+	case PHY_INTERFACE_MODE_2500BASEX:

[ ... ]

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

Quoted 12 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +12 -0
+static bool an7581_pcs_have_rx_signal(struct airoha_pcs_priv *priv, int index)
+{
+	struct regmap *pcs_pma = priv->pcs_pma[index];
+	unsigned int count = 0;
+	u32 val;
+	int i;
+
+	regmap_write(pcs_pma, AIROHA_PCS_PMA_DIG_RESERVE_0,
+		     AIROHA_PCS_TRIGGER_RX_SIDGET_SCAN);
+
+	/* Scan 5 times for RX sigdet module to detect RX signal */
+	for (i = 0; i <= 5; i++) {

[Severity: Low]
Does this loop execute six times instead of the five times mentioned in the comment?

The loop uses i <= 5, which iterates for 0 through 5. Since the logic looks for count >= 4 to determine a valid signal, checking six times lowers the threshold percentage and might return true on marginal or false signals.

Quoted 9 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +9 -0
+		regmap_read(pcs_pma, AIROHA_PCS_PMA_DIG_RO_RESERVE_2,
+			    &val);
+		if (val & AIROHA_PCS_RX_SIGDET)
+			count++;
+	}
+
+	/* Consider signal presence if we detect signal at least 4 times */
+	return count >= 4;
+}

[ ... ]

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

PATCH v7 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  | 161 +++++++++++++++++++++-
 drivers/net/ethernet/airoha/airoha_eth.h  |   3 +
 drivers/net/ethernet/airoha/airoha_regs.h |  12 ++
 4 files changed, 176 insertions(+), 1 deletion(-)
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 +160 -1

203 lines not on this page · raw message

drivers/net/ethernet/airoha/airoha_eth.h +3 -0
index 46b1c31939de..f4488da42f81 100644
@@ -554,6 +554,9 @@ struct airoha_gdm_dev {
 	int nbq;
 
 	struct airoha_hw_stats stats;
+
+	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
#31 lore raw

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

Hi.

Quoted 98 lines · 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  | 161 +++++++++++++++++++++-
  drivers/net/ethernet/airoha/airoha_eth.h  |   3 +
  drivers/net/ethernet/airoha/airoha_regs.h |  12 ++
  4 files changed, 176 insertions(+), 1 deletion(-)
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 +41 -0
index 5f1a118875fb..9a42fb991bd7 100644
@@ -8,6 +8,7 @@
  #include <linux/of_reserved_mem.h>
  #include <linux/platform_device.h>
  #include <linux/tcp.h>
+#include <linux/pcs/pcs.h>
  #include <linux/u64_stats_sync.h>
  #include <net/dst_metadata.h>
  #include <net/page_pool/helpers.h>
@@ -1810,6 +1811,14 @@ 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, "could not attach PHY: %d\n", err);
+		return err;
+	}
+
+	phylink_start(dev->phylink);
+
  	netif_tx_start_all_queues(netdev);
  	err = airoha_set_vip_for_gdm_port(dev, true);
  	if (err)
@@ -1907,6 +1916,9 @@ static int airoha_dev_stop(struct net_device *netdev)
  		}
  	}
 
+	phylink_stop(dev->phylink);
+	phylink_disconnect_phy(dev->phylink);
+
  	return 0;
  }
 
@@ -3168,6 +3180,151 @@ bool airoha_is_valid_gdm_dev(struct airoha_eth *eth,
  	return false;
  }
 
+/* Nothing to do in MAC, everything is handled in PCS */
+static void airoha_mac_config(struct phylink_config *config, unsigned int mode,
+			      const struct phylink_link_state *state)
+{
+}
+
+static void airoha_mac_link_up(struct phylink_config *config, struct phy_device *phy,
+			       unsigned int mode, phy_interface_t interface,
+			       int speed, int duplex, bool tx_pause, bool rx_pause)
+{
+	struct airoha_gdm_dev *dev = container_of(config, struct airoha_gdm_dev,
+						  phylink_config);
+	struct airoha_gdm_port *port = dev->port;
+	struct airoha_eth *eth = dev->eth;
+	u32 frag_size_tx, frag_size_rx;
+	u32 mask, val;
+
+	/* TX/RX frag is configured only for GDM4 */
+	if (port->id != AIROHA_GDM4_IDX)
+		return;
+
+	switch (speed) {
+	case SPEED_10000:
+	case SPEED_5000:
+		frag_size_tx = 8;
+		frag_size_rx = 8;
+		break;
+	case SPEED_2500:
+		frag_size_tx = 2;

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

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

Can the naming be consistently GDM4 without the A?

MvH

Benjamin Larsson

#32 lore raw

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

In reply to Christian Marangi
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] NULL pointer dereference in `phylink_destroy()` on probe error path. - [High] Data race on shared…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] NULL pointer dereference in phylink_destroy() on probe error path.
- [High] Data race on shared GDM4 fragment size registers causes configuration corruption. --

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

Quoted 4 lines
drivers/net/ethernet/airoha/airoha_eth.c +0 -0
index 5f1a118875fbe..9a42fb991bd72 100644

[ ... ]

Quoted 15 lines
drivers/net/ethernet/airoha/airoha_eth.c +14 -0
@@ -3168,6 +3180,151 @@ bool airoha_is_valid_gdm_dev(struct airoha_eth *eth,
+static void airoha_mac_link_up(struct phylink_config *config, struct phy_device *phy,
+			       unsigned int mode, phy_interface_t interface,
+			       int speed, int duplex, bool tx_pause, bool rx_pause)
+{
+	struct airoha_gdm_dev *dev = container_of(config, struct airoha_gdm_dev,
+						  phylink_config);
+	struct airoha_gdm_port *port = dev->port;
+	struct airoha_eth *eth = dev->eth;
+	u32 frag_size_tx, frag_size_rx;
+	u32 mask, val;
+
+	/* TX/RX frag is configured only for GDM4 */
+	if (port->id != AIROHA_GDM4_IDX)
+		return;

[ ... ]

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

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

[Severity: High]
Does this lockless read-modify-write on shared GDM4 registers cause data corruption?

Because the SGMII lanes of the GDM4 port operate as separate phylink instances, their link states are resolved independently.

If both lanes undergo a link state change simultaneously, phylink's workqueues can execute airoha_mac_link_up() concurrently on different CPUs.

Without a lock around airoha_fe_rmw(), these concurrent updates to REG_GDMA4_TMBI_FRAG and REG_GDMA4_RMBI_FRAG could overwrite and corrupt each other's settings.

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

[ ... ]

Quoted 7 lines
drivers/net/ethernet/airoha/airoha_eth.c +1 -1
@@ -3231,7 +3388,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);
 }

[ ... ]

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

Quoted 5 lines
drivers/net/ethernet/airoha/airoha_eth.c +1 -0
@@ -3457,6 +3614,7 @@ static int airoha_probe(struct platform_device *pdev)
 			netdev = netdev_from_priv(dev);
 			if (netdev->reg_state == NETREG_REGISTERED)
 				unregister_netdev(netdev);
+			phylink_destroy(dev->phylink);

[Severity: High]
Could this cause a NULL pointer dereference during a probe failure?

In airoha_alloc_gdm_device(), the port->devs[index] = dev assignment happens before calling airoha_setup_phylink(). If airoha_setup_phylink() fails, dev->phylink remains NULL.

The error propagates back up to airoha_probe(), which then unconditionally calls phylink_destroy() on all populated port->devs slots.

Because phylink_destroy() dereferences its argument without checking if it is NULL, this will result in a kernel panic.

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