net: pcs: Introduce support for fwnode PCS
Subsystems (8)
v22 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_xlate.
A PCS provider on driver removal must call fwnode_pcs_del_provider() to delete itself as a provider.
---
Second PCS handling in phylink:
We have the PCS problem for the only reason that in initial implementation, we permitted way too much flexibility to MAC driver and things started to deviate. At times we couldn't think SoC would start to put PCS outside the MAC hence it was OK to assume they would live in the same driver. With the introduction of 10g in more consumer devices, we are observing a rapid growth of this pattern with multiple PCS external to MAC.
To put a stop on this, the only solution is to give back to phylink control on PCS handling and enforce more robust supported interface definition from both MAC and PCS side.
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.
Also realtek is using this and proved to correctly work.
---
Due to the introduction of Sashiko and the introduction of the notifier, a major reorg of the different scenario and capabilities parsing was needed. This is to make the bot happy (in theory, but it never is) and try to improve the state of the code by moving the capabilitis parsing to a centralized place trying to address all the scenario in one place.
---
Changes v22:
- Rebase on top of net-next (no idea why patchwork reported failed to apply...) Changes v21:
- Add fixes tag for the RCU patch
- Preserve user-disabled ethtool advertising during capability updates and SFP interface selection.
- Add additional kconfig select for Airoha PCS and Airoha Ethernet
- Fix wrong rcu_assign_pointer NULL in RCU patch
- Use "pcs-handle-names" as already defined (instead of "pcs-names")
- Skip phylink_pcs_disable if already disabled
- Skip phylink_major_config if phylink disabled
- Better handle capabilites from phylink_parse_mode and phylink_parse_fixedlink
- Set link_failed on late attach so that resolve is triggered 2 times
- Improve description of fwnode_pcs_get_from_provider()
- Improve capabilities tracking for PHY and SFP scenario
- Add variant for __phy_get_rate_matching
Changes v20:
- Fix double declaration error in Documentation
- Drop mutex from patch 8 for validate_mac_and_pcs
- Correctly use non lock version in __phylink_validate
Changes v19:
- Rebase on top of net-next
- RCU protect phylink from pcs struct
- Fix dead-lock on phylink_validate
- Handle possible PCS provider returning NULL (protective coding)
- Improve capabilities handling refresh
Changes v18:
- Improve handling on late PCS addition
- Fix wrong comment on airoha PCS driver
- Rebase on top of net-next
Changes v17:
- Rebase on top of net-next
- Add Review tag for DT patch
- Recalculate capabilities on late removal/attach
Changes v16:
- Rebase on top of net-next
- Add Tested-by tag
- Add ACK for airoha ethernet patch + nit fix
Changes v15:
- Rebase on top of net-next
- Fix Coccicheck warning
Changes v14:
- Fix deadlock when fill_available_pcs fails
- Move notifier and fill down after resolve disable bit is set
- Fix kconfig description
Changes v13:
- Fix regression in fwnode_pcs_get_from_pcsspec from v12
- Fix reverse lock for phy_lock and state_mutex
- Better handle lock in phylink_validate_mac_and_pcs and inband
- Fix wrong pointer to fwnode_pcs_add_provider notifier
- Fix compilation error for new C type in pcs_provider_notify Changes v12:
- Fix kdoc for function
- Rework PCS provider add and delete to be provider driven (instead of relaying on scan of fwnode every time) (this also handle
multi PCS scenario)
- Even further rework state mutex locking for concurrent pcs list
- Restore MAC supported_interfaces validation in phylink_create() wrongly dropped.
- Use get/put for fwnode reference
- Fix possible unment depndency for FWNODE_PCS
- Fix almost impossible problem for PCS calibration
- Hold rtnl with PCS deletion
Changes v11:
- Rework PCS release to notifier way (fix race as pointed out by bot)
- Add Ack from Lorenzo for Airoha Ethernet driver
- Introduce devm variant for pcs_provider_add
- Better support firmware node reference for PCS late attach
- Better protect list iteration with state mutex
- Minor fixup to airoha pcs driver (flow control fixup)
- Rebase on top of net-next
Changes v10:
- Minor code cleanup and fixes as suggested by bot
- Drop devlink patch
- Add reviewed by tag
- Configure old_link_state on PCS release
- Unlink PCS on error in phylink_create()
- Use list_add_tail
- Fix typo in Airoha PCS driver
- Update phylink migration guide
- Better handle late PCS attach (idempotent function and
add unser state_mutex)
- Use dedicated lock in Airoha ethernet driver
Changes v9:
- Rebase on top of net-next/main
- Drop early verification of supported_interface
(it can also be provided by get_capabilites MAC OP)
Changes v8:
- Back to RFC (net-next closed)
- Address additional bug reported by Sashiko bot
- Better handle priv interface for Airoha PCS driver
- Better handle locking for modifying the PCS list in
phylink code
- Improve fwnode_phylink_pcs_parse() parsing on -ENOENT
- Better handle error condition in phylink_create()
- Turn down the link when current PCS is released
- Fix compilation warning caused by copy paste error
Changes v7:
- Address all the bug from the Sashiko bot
- Rename .num_available_pcs to .num_possible_pcs
- Link PCS in phylink_create()
- Correctly unregister the notifier on phylink_destroy()
- Introduce fwnode_phylink_pcs_count()
- Better handle locking in phylink for PCS handling
- Better handle unavailable PCS at phylink_create() time
- Improve Documentation file
- Other minor fixes to address suggestion from bot
- Rebase on top of net-next
Changes v6:
- Rebase on top of net-next
- Add Documentation files
- Add fw_devlink patch
- Fix some comments typo
- Rework the airoha_eth.c implementation with new multi serdes code
- Extend PCS code with PCIe and USB support
- Align schema to new property
Changes v5:
- Rebase on top of net-next
- Use the new force_major_config
- Reword some comments and commit description
- Return -ENODEV instead of -EPROBE_DEFER to perevent race condition
- Drop phy_interface_copy patch (Russell pushed an equivalent version) Changes v4:
- Move patch 0002 phy_interface_copy to 0002 (fix bisectability problem)
- Address review from Lorenzo for Airoha ethernet driver
- Fix kdoc error with missing Return (actually missing : before Return)
- Fix UNMET dependency reported error for CONFIG_FWNODE_PCS
- Revert to pcs.c instead of core.c (due to name conflict with other kmod)
- Fix clang compilation error for Airoha PCS driver
- Add missing inline function to pcs.h function
Changes v3:
- Out of RFC
- Fix various spelling mistake
- Drop circular dependency patch
- Complete Airoha Ethernet phylink integration
- Introduce .pcs_link_down PCS OP
Changes v2:
- Switch to fwnode
- Implement PCS provider notifier
- Better split changes
- Move supported_interfaces to phylink
- Add circular dependency patch
- Rework handling with indirect addition/removal and
trigger of phylink_resolve()
Christian Marangi (15):
net: phylink: make PCS phylink instance access RCU protected
net: phylink: keep and use MAC supported_interfaces in phylink struct
net: phylink: introduce internal phylink PCS handling
net: pcs: implement Firmware node support for PCS driver
net: phylink: save phylink instance fwnode on phylink_create
net: phylink: make phylink_validate state locked
net: phylink: add capabilities helpers and track modified advertising
net: phylink: support PCS provider release
net: phylink: support late PCS provider attach
net: Document PCS subsystem
MAINTAINERS: add myself as PCS subsystem maintainer
net: phylink: add .pcs_link_down PCS OP
dt-bindings: net: pcs: Document support for Airoha Ethernet PCS
net: pcs: airoha: add PCS driver for Airoha AN7581 SoC
net: airoha: add phylink support
.../bindings/net/pcs/airoha,pcs.yaml | 261 ++ Documentation/networking/index.rst | 1 + Documentation/networking/pcs.rst | 231 ++ Documentation/networking/sfp-phylink.rst | 35 +- MAINTAINERS | 9 + drivers/net/ethernet/airoha/Kconfig | 2 + drivers/net/ethernet/airoha/airoha_eth.c | 197 +- drivers/net/ethernet/airoha/airoha_eth.h | 5 + drivers/net/ethernet/airoha/airoha_regs.h | 12 + drivers/net/pcs/Kconfig | 8 + drivers/net/pcs/Makefile | 3 + drivers/net/pcs/airoha/Kconfig | 17 + drivers/net/pcs/airoha/Makefile | 7 + drivers/net/pcs/airoha/pcs-airoha-common.c | 1309 ++++++++++ drivers/net/pcs/airoha/pcs-airoha.h | 1311 ++++++++++ drivers/net/pcs/airoha/pcs-an7581.c | 2100 +++++++++++++++++ drivers/net/pcs/pcs.c | 314 +++ drivers/net/phy/phy-internal.h | 22 + drivers/net/phy/phy.c | 11 +- drivers/net/phy/phylink.c | 1168 ++++++--- include/linux/pcs/pcs-provider.h | 70 + include/linux/pcs/pcs.h | 174 ++ include/linux/phylink.h | 30 +- 23 files changed, 6994 insertions(+), 303 deletions(-) create mode 100644 Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml create mode 100644 Documentation/networking/pcs.rst create mode 100644 drivers/net/pcs/airoha/Kconfig create mode 100644 drivers/net/pcs/airoha/Makefile create mode 100644 drivers/net/pcs/airoha/pcs-airoha-common.c create mode 100644 drivers/net/pcs/airoha/pcs-airoha.h create mode 100644 drivers/net/pcs/airoha/pcs-an7581.c create mode 100644 drivers/net/pcs/pcs.c create mode 100644 drivers/net/phy/phy-internal.h create mode 100644 include/linux/pcs/pcs-provider.h create mode 100644 include/linux/pcs/pcs.h
Signature · 1 line
2.55.0
PATCH v22 1/15 net: phylink: make PCS phylink instance access RCU protected
In the context of phylink_pcs_change(), phylink instance is dereferenced from the pcs pointer present in the phylink_pcs struct.
There is currently a fragility where this pcs pointer is never set NULL in phylink_destroy() and would reference invalid memory after phylink instance is freed.
In this scenario, if a PCS driver calls phylink_pcs_change(), invalid memory can be dereferenced and phylink_link_changed() is called on freed memory.
To address this, make the phylink instance saved in phylink_pcs struct RCU protected and access it under RCU lock.
Also correctly reset the phylink instance pointer on phylink_destroy() and syncronyze RCU to catch an execution of phylink_pcs_change().
--- drivers/net/phy/phylink.c | 17 ++++++++++++++--- include/linux/phylink.h | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-)
drivers/net/phy/phylink.c +14 -3
@@ -17,6 +17,7 @@#include <linux/phy_link_topology.h> #include <linux/phy_port.h> #include <linux/phylink.h> +#include <linux/rcupdate.h> #include <linux/rtnetlink.h> #include <linux/spinlock.h> #include <linux/timer.h>
@@ -1295,10 +1296,10 @@ static void phylink_major_config(struct phylink *pl, bool restart,phylink_pcs_disable (pl->pcs);if (pl->pcs) - pl->pcs->phylink =NULL ; +rcu_assign_pointer (pl->pcs->phylink,NULL );if (pcs) - pcs->phylink = pl; +rcu_assign_pointer (pcs->phylink, pl); pl->pcs = pcs; }
@@ -2137,6 +2138,11 @@ void phylink_destroy(struct phylink *pl)if (pl->link_gpio)gpiod_put (pl->link_gpio); +if (pl->pcs) { +rcu_assign_pointer (pl->pcs->phylink,NULL ); +synchronize_rcu (); + } +cancel_work_sync (&pl->resolve);kfree (pl); }
@@ -2601,10 +2607,15 @@ EXPORT_SYMBOL_GPL(phylink_mac_change);*/ void phylink_pcs_change (struct phylink_pcs *pcs,bool up) { -struct phylink *pl = pcs->phylink; +struct phylink *pl; + +rcu_read_lock (); + pl =rcu_dereference (pcs->phylink);if (pl)phylink_link_changed (pl, up,"pcs" ); + +rcu_read_unlock (); }EXPORT_SYMBOL_GPL (phylink_pcs_change);
include/linux/phylink.h +1 -1
@@ -494,7 +494,7 @@ struct phylink_pcs_ops;struct phylink_pcs {DECLARE_PHY_INTERFACE_MASK (supported_interfaces);const struct phylink_pcs_ops *ops; -struct phylink *phylink; +struct phylink __rcu *phylink;bool poll;bool rxc_always_on; };
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH net-next v22 01/15] net: phylink: make PCS phylink instance access RCU protected
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net: phylink: missing RCU grace period when pl->pcs becomes NULL before destroy --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net: phylink: missing RCU grace period when pl->pcs becomes NULL before destroy
--
commit 68e1704bb1480c2bf9abea1eeb40960aa3c24786
Author: Christian Marangi <[email protected]>
net: phylink: make PCS phylink instance access RCU protected
This commit protects pcs->phylink with RCU so that phylink_pcs_change() safely dereferences the phylink instance under rcu_read_lock() and phylink_destroy() clears the pointer and waits for a grace period before freeing the phylink structure.
Quoted 4 lines
drivers/net/phy/phylink.c +0
-0
[ ... ]
Quoted 15 lines
drivers/net/phy/phylink.c +2
-2@@ -1295,10 +1296,10 @@ static void phylink_major_config(struct phylink *pl, bool restart,phylink_pcs_disable (pl->pcs);if (pl->pcs) - pl->pcs->phylink =NULL ; +rcu_assign_pointer (pl->pcs->phylink,NULL );if (pcs) - pcs->phylink = pl; +rcu_assign_pointer (pcs->phylink, pl); pl->pcs = pcs; }@@ -2137,6 +2138,11 @@ void phylink_destroy(struct phylink *pl)if (pl->link_gpio)
drivers/net/phy/phylink.c void phylink_destroy(struct phylink *pl) · line 2148
Quoted 10 lines
drivers/net/phy/phylink.c +5
-0gpiod_put (pl->link_gpio); +if (pl->pcs) { +rcu_assign_pointer (pl->pcs->phylink,NULL ); +synchronize_rcu (); + } +cancel_work_sync (&pl->resolve);kfree (pl); }
[Severity: Medium]
Can phylink_destroy() still free pl while phylink_pcs_change() is using it if a prior phylink_major_config() switched pl->pcs to NULL?
When mac_select_pcs() returns NULL for a new interface mode (for example, when switching from SGMII to RGMII), phylink_major_config() clears pl->pcs->phylink and sets pl->pcs = NULL without waiting for an RCU grace period. If phylink_destroy() then runs, it skips synchronize_rcu() because pl->pcs is already NULL:
CPU0 CPU1
phylink_pcs_change(old_pcs, ...)
rcu_read_lock()
pl = rcu_dereference(old_pcs->phylink)phylink_major_config(pl, ...)
rcu_assign_pointer(old_pcs->phylink, NULL)
pl->pcs = NULL
...phylink_destroy(pl)
if (pl->pcs) -> false
cancel_work_sync(&pl->resolve)
kfree(pl)
phylink_link_changed(pl, ...)
pl->link_failed = true
phylink_run_resolve(pl)
<-- use-after-freeCalling synchronize_rcu() unconditionally in phylink_destroy(), or waiting for a grace period when unpublishing pl->pcs->phylink, would close this window.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v22 2/15 net: phylink: keep and use MAC supported_interfaces in phylink struct
In reply to Christian MarangiAdd 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 | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/phy/phylink.c +16 -8
@@ -63,6 +63,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;
@@ -633,7 +638,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,
@@ -1788,7 +1793,7 @@ static int phylink_create_sfp_cage_port(struct phylink *pl) port->is_mii = true; port->active = true; -phy_interface_and (port->interfaces, pl->config->supported_interfaces, +phy_interface_and (port->interfaces, pl->supported_interfaces, phylink_sfp_interfaces);phy_port_update_supported (port);
@@ -2091,6 +2096,9 @@ struct phylink *phylink_create(struct phylink_config *config,__set_bit (PHYLINK_DISABLE_STOPPED , &pl->phylink_disable_state);timer_setup (&pl->link_poll, phylink_fixed_poll,0 ); +phy_interface_copy (pl->supported_interfaces, + pl->config->supported_interfaces); +linkmode_fill (pl->supported);linkmode_copy (pl->link_config.advertising, pl->supported);phylink_validate (pl, pl->supported, &pl->link_config);
@@ -2218,7 +2226,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" );
@@ -3037,12 +3045,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 ; }
@@ -3921,14 +3929,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" );
@@ -4166,7 +4174,7 @@ static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)/* Set the PHY's host supported interfaces */ phy_interface_and (phy->host_interfaces, phylink_sfp_interfaces, - pl->config->supported_interfaces); + pl->supported_interfaces);phy_set_upstream_port (phy, pl->sfp_cage_port);
Signature · 1 line
2.55.0
PATCH v22 3/15 net: phylink: introduce internal phylink PCS handling
In reply to Christian MarangiIntroduce 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 | 201 ++++++++++++++++++++++++++++++++++---- include/linux/phylink.h | 16 +++ 2 files changed, 196 insertions(+), 21 deletions(-)
drivers/net/phy/phylink.c +180 -21
@@ -63,6 +63,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. */
@@ -522,6 +525,22 @@ 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)
@@ -535,6 +554,21 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl, pcs = pl->mac_ops->mac_select_pcs (pl->config, state->interface);if (IS_ERR (pcs))return PTR_ERR (pcs); +/* +* Find a PCS in available PCS list for the requested interface. +* +* Skip searching if the MAC doesn't require a dedicated PCS for +* the requested interface. +*/ + }else if (test_bit (state->interface, pl->config->pcs_interfaces)) { +struct phylink_pcs *tmp; + +list_for_each_entry (tmp, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (tmp, state->interface)) { + pcs = tmp; +break ; + } + } }if (pcs) {
@@ -549,13 +583,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 ; }
@@ -968,12 +999,22 @@ static void phylink_pcs_enable_eee(struct phylink_pcs *pcs)static unsigned int phylink_inband_caps (struct phylink *pl,phy_interface_t interface) { -struct phylink_pcs *pcs; +struct phylink_pcs *pcs =NULL ; -if (!pl->mac_ops->mac_select_pcs) -return 0 ; +if (pl->mac_ops->mac_select_pcs) { + pcs = pl->mac_ops->mac_select_pcs (pl->config, + interface); + }else if (test_bit (interface, pl->config->pcs_interfaces)) { +struct phylink_pcs *tmp; + +list_for_each_entry (tmp, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (tmp, interface)) { + pcs = tmp; +break ; + } + } + } - pcs = pl->mac_ops->mac_select_pcs (pl->config, interface);if (IS_ERR_OR_NULL (pcs))return 0 ;
@@ -1270,10 +1311,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)) { +struct phylink_pcs *tmp; + +list_for_each_entry (tmp, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (tmp, + state->interface)) { + pcs = tmp; +break ; + } + } + +if (!pcs) { +phylink_err (pl, +"couldn't find a PCS for %s\n" , +phy_modes (state->interface)); - pcs_changed = pl->pcs != pcs; + pl->major_config_failed = true; +return ; + } } + pcs_changed = pl->pcs != pcs; +phylink_pcs_neg_mode (pl, pcs, state->interface, state->advertising); phylink_dbg(pl, "major config, active %s/%s/%s\n",
@@ -1300,13 +1367,15 @@ static void phylink_major_config(struct phylink *pl, bool restart,if (pcs_changed) {phylink_pcs_disable (pl->pcs); -if (pl->pcs) -rcu_assign_pointer (pl->pcs->phylink,NULL ); +if (pl->mac_ops->mac_select_pcs) { +if (pl->pcs) +rcu_assign_pointer (pl->pcs->phylink,NULL ); -if (pcs) -rcu_assign_pointer (pcs->phylink, pl); +if (pcs) +rcu_assign_pointer (pcs->phylink, pl); + } - pl->pcs = pcs; +WRITE_ONCE (pl->pcs, pcs); }if (pl->pcs)
@@ -1901,6 +1970,49 @@ 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 ; + +if (WARN (rcu_access_pointer (pcs->phylink), +"%s: PCS already attached to a phylink instance\n" , + __func__)) +continue ; + +list_add_tail (&pcs->list, &pl->pcs_list); + } + +out: +kfree (pcss); + +return ret; +} +/** * phylink_update_pause_state() - Update the phylink pause frame configuration * @pl: a pointer to a &struct phylink instance
@@ -2042,9 +2154,20 @@ struct phylink *phylink_create(struct phylink_config *config,phy_interface_t iface, const struct phylink_mac_ops *mac_ops) { + struct phylink_pcs *tmp, *pcs;struct phylink *pl;int ret; +/* +* Make sure either PCS internal validation or .mac_select_pcs +* is used. Return error if both are defined. +*/ +if (config->num_possible_pcs && 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 ); + } +/* Validate the supplied configuration */ if (phy_interface_empty (config->supported_interfaces)) { dev_err(config->dev,
@@ -2059,6 +2182,7 @@ struct phylink *phylink_create(struct phylink_config *config,mutex_init (&pl->phydev_mutex);mutex_init (&pl->state_mutex);INIT_WORK (&pl->resolve, phylink_resolve); +INIT_LIST_HEAD (&pl->pcs_list); pl->config = config;if (config->type ==PHYLINK_NETDEV ) {
@@ -2096,16 +2220,31 @@ struct phylink *phylink_create(struct phylink_config *config,__set_bit (PHYLINK_DISABLE_STOPPED , &pl->phylink_disable_state);timer_setup (&pl->link_poll, phylink_fixed_poll,0 ); +/* Fill the PCS list with available PCS from phylink config */ + ret =phylink_fill_available_pcs (pl, config); +if (ret <0 ) + goto free_pl; + +/* Link available PCS to phylink */ +list_for_each_entry (pcs, &pl->pcs_list, list) +rcu_assign_pointer (pcs->phylink, pl); +phy_interface_copy (pl->supported_interfaces, pl->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); +linkmode_fill (pl->supported);linkmode_copy (pl->link_config.advertising, pl->supported);phylink_validate (pl, pl->supported, &pl->link_config); ret =phylink_parse_mode (pl, fwnode);if (ret <0 ) - goto free_pl; + goto unlink_pcs_list;if (pl->cfg_link_an_mode ==MLO_AN_FIXED ) { ret =phylink_parse_fixedlink (pl, fwnode);
@@ -2124,6 +2263,14 @@ struct phylink *phylink_create(struct phylink_config *config, release_link_gpio:if (pl->link_gpio)gpiod_put (pl->link_gpio); +unlink_pcs_list: +list_for_each_entry (pcs, &pl->pcs_list, list) +rcu_assign_pointer (pcs->phylink,NULL ); + +synchronize_rcu (); + +list_for_each_entry_safe (pcs, tmp, &pl->pcs_list, list) +list_del (&pcs->list); free_pl:kfree (pl);return ERR_PTR (ret);
@@ -2141,17 +2288,29 @@ EXPORT_SYMBOL_GPL(phylink_create);*/ void phylink_destroy (struct phylink *pl) { +struct phylink_pcs *pcs, *tmp; +sfp_bus_del_upstream (pl->sfp_bus);phylink_destroy_sfp_cage_port (pl);if (pl->link_gpio)gpiod_put (pl->link_gpio); -if (pl->pcs) { +/* Remove phylink instance from every PCS */ +list_for_each_entry (pcs, &pl->pcs_list, list) +rcu_assign_pointer (pcs->phylink,NULL ); + +if (pl->pcs)rcu_assign_pointer (pl->pcs->phylink,NULL ); + +if (pl->pcs || !list_empty (&pl->pcs_list))synchronize_rcu (); - }cancel_work_sync (&pl->resolve); + +/* Remove every PCS from phylink PCS list */ +list_for_each_entry_safe (pcs, tmp, &pl->pcs_list, list) +list_del (&pcs->list); +kfree (pl); }EXPORT_SYMBOL_GPL (phylink_destroy);
@@ -2620,7 +2779,7 @@ void phylink_pcs_change(struct phylink_pcs *pcs, bool up)rcu_read_lock (); pl =rcu_dereference (pcs->phylink); -if (pl) +if (pl &&READ_ONCE (pl->pcs) == pcs)phylink_link_changed (pl, up,"pcs" );rcu_read_unlock ();
include/linux/phylink.h +16 -0
@@ -12,6 +12,7 @@ struct ethtool_cmd;struct fwnode_handle ;struct net_device ;struct phylink ; +struct phylink_pcs ;enum {MLO_PAUSE_NONE ,
@@ -151,6 +152,8 @@ enum phylink_op_type {* if MAC link is at %MLO_AN_FIXED mode. * @supported_interfaces: bitmap describing which PHY_INTERFACE_MODE_xxx * are supported by the MAC/PCS. +* @pcs_interfaces: bitmap describing for which PHY_INTERFACE_MODE_xxx a +* dedicated PCS is required. * @lpi_interfaces: bitmap describing which PHY interface modes can support * LPI signalling. * @mac_capabilities: MAC pause/speed/duplex capabilities.
@@ -160,6 +163,10 @@ enum phylink_op_type {* @wol_phy_legacy: Use Wake-on-Lan with PHY even if phy_can_wakeup() is false * @wol_phy_speed_ctrl: Use phy speed control on suspend/resume * @wol_mac_support: Bitmask of MAC supported %WAKE_* options +* @num_possible_pcs: num of possible phylink_pcs PCS +* @fill_available_pcs: callback to fill the available PCS in the passed +* array struct of phylink_pcs PCS available_pcs up to +* num_possible_pcs. */ struct phylink_config {struct device *dev;
@@ -172,6 +179,7 @@ struct phylink_config {void (*get_fixed_state)(struct phylink_config *config,struct phylink_link_state *state);DECLARE_PHY_INTERFACE_MASK (supported_interfaces); +DECLARE_PHY_INTERFACE_MASK (pcs_interfaces);DECLARE_PHY_INTERFACE_MASK (lpi_interfaces);unsigned long mac_capabilities;unsigned long lpi_capabilities;
@@ -182,6 +190,11 @@ struct phylink_config {bool wol_phy_legacy;bool wol_phy_speed_ctrl;u32 wol_mac_support; + +unsigned int num_possible_pcs; +int (*fill_available_pcs)(struct phylink_config *config, +struct phylink_pcs **available_pcs, +unsigned int num_possible_pcs); };void phylink_limit_mac_speed (struct phylink_config *config,u32 max_speed);
@@ -497,6 +510,9 @@ struct phylink_pcs {struct phylink __rcu *phylink;bool poll;bool rxc_always_on; + +/* private: */ +struct list_head list; };/**
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH net-next v22 03/15] net: phylink: introduce internal phylink PCS handling
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] net: phylink: missing error return in phylink_validate_mac_and_pcs() - [Medium] net: phylink…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] net: phylink: missing error return in phylink_validate_mac_and_pcs()
- [Medium] net: phylink: broken PCS interface masking in phylink_create()
- [Low] net: phylink: duplicated PCS lookup logic across three functions --
commit 4ee2fa0fe02a77175d57df4608f9c44befd95369
Author: Christian Marangi <[email protected]>
net: phylink: introduce internal phylink PCS handling
This commit introduces an internal PCS list in phylink, populated via .fill_available_pcs in struct phylink_config, as an alternative to .mac_select_pcs for selecting and validating PCS instances based on their supported interfaces.
Quoted 4 lines
drivers/net/phy/phylink.c +0
-0
[ ... ]
Quoted 10 lines
drivers/net/phy/phylink.c +6
-0@@ -535,6 +554,21 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl, pcs = pl->mac_ops->mac_select_pcs (pl->config, state->interface);if (IS_ERR (pcs))return PTR_ERR (pcs); +/* +* Find a PCS in available PCS list for the requested interface. +* +* Skip searching if the MAC doesn't require a dedicated PCS for +* the requested interface. +*/
drivers/net/phy/phylink.c static int phylink_validate_mac_and_pcs(struct phylink *pl, · line 572
Quoted 10 lines
drivers/net/phy/phylink.c +9
-0+ }else if (test_bit (state->interface, pl->config->pcs_interfaces)) { +struct phylink_pcs *tmp; + +list_for_each_entry (tmp, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (tmp, state->interface)) { + pcs = tmp; +break ; + } + } }
[Severity: Medium]
Should phylink_validate_mac_and_pcs() return -EINVAL when state->interface is set in pl->config->pcs_interfaces, but no matching PCS is found in pl->pcs_list?
When pcs remains NULL, phylink_validate_mac_and_pcs() skips the if (pcs) block and validates state->interface against MAC capabilities alone via phylink_validate_mask_caps(). If a MAC lists modes in config->pcs_interfaces that the attached PCS does not support (for example, 10GBASER and USXGMII when paired with a PCS that only supports up to 2500BASEX), those modes pass validation during phylink_parse_mode(), PHY attachment, and SFP module selection, only for phylink_major_config() to fail at runtime with pl->major_config_failed = true when it sees !pcs.
Quoted 49 lines
drivers/net/phy/phylink.c +32
-4@@ -968,12 +999,22 @@ static void phylink_pcs_enable_eee(struct phylink_pcs *pcs)static unsigned int phylink_inband_caps (struct phylink *pl,phy_interface_t interface) { -struct phylink_pcs *pcs; +struct phylink_pcs *pcs =NULL ; -if (!pl->mac_ops->mac_select_pcs) -return 0 ; +if (pl->mac_ops->mac_select_pcs) { + pcs = pl->mac_ops->mac_select_pcs (pl->config, + interface); + }else if (test_bit (interface, pl->config->pcs_interfaces)) { +struct phylink_pcs *tmp; + +list_for_each_entry (tmp, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (tmp, interface)) { + pcs = tmp; +break ; + } + } + } - pcs = pl->mac_ops->mac_select_pcs (pl->config, interface);if (IS_ERR_OR_NULL (pcs))return 0 ;@@ -1270,10 +1311,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)) { +struct phylink_pcs *tmp; + +list_for_each_entry (tmp, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (tmp, + state->interface)) { + pcs = tmp; +break ; + } + } +
drivers/net/phy/phylink.c static void phylink_major_config(struct phylink *pl, bool restart, · line 1340
Quoted 10 lines
drivers/net/phy/phylink.c +7
-1+if (!pcs) { +phylink_err (pl, +"couldn't find a PCS for %s\n" , +phy_modes (state->interface)); - pcs_changed = pl->pcs != pcs; + pl->major_config_failed = true; +return ; + } }
[Severity: Low]
This isn't a bug by itself, but the PCS selection logic checking pl->mac_ops->mac_select_pcs and walking pl->pcs_list is duplicated across phylink_validate_mac_and_pcs(), phylink_inband_caps(), and phylink_major_config(). Factoring this lookup into a helper that returns a struct phylink_pcs pointer (or an ERR_PTR when a required PCS is not found) would avoid the triplicated loop and keep missing-PCS handling consistent across all three callers.
[ ... ]
Quoted 11 lines
drivers/net/phy/phylink.c +7
-0@@ -2096,16 +2220,31 @@ struct phylink *phylink_create(struct phylink_config *config,__set_bit (PHYLINK_DISABLE_STOPPED , &pl->phylink_disable_state);timer_setup (&pl->link_poll, phylink_fixed_poll,0 ); +/* Fill the PCS list with available PCS from phylink config */ + ret =phylink_fill_available_pcs (pl, config); +if (ret <0 ) + goto free_pl; + +/* Link available PCS to phylink */ +list_for_each_entry (pcs, &pl->pcs_list, list)
drivers/net/phy/phylink.c struct phylink *phylink_create(struct phylink_config *config, · line 2239
Quoted 10 lines
drivers/net/phy/phylink.c +7
-0+rcu_assign_pointer (pcs->phylink, pl); +phy_interface_copy (pl->supported_interfaces, pl->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);
[Severity: Medium]
As reported by Maxime Chevallier on v7 [1], does ORing pcs->supported_interfaces into pl->supported_interfaces leave unsupported interface modes enabled in pl->supported_interfaces?
Because pl->config->supported_interfaces already contains the modes in pl->config->pcs_interfaces, bitwise-ORing pcs->supported_interfaces never clears modes in config->pcs_interfaces that no PCS in pl->pcs_list supports. Conversely, if a generic PCS sets modes in pcs->supported_interfaces that the MAC itself does not support, phy_interface_or() adds them to pl->supported_interfaces, and because those modes are not in pl->config->pcs_interfaces, phylink treats them as native MAC modes requiring no PCS. Callers inspecting pl->supported_interfaces (such as SFP interface selection and PHY validation) will then consider unsupported modes valid until phylink_major_config() fails.
Should pl->supported_interfaces instead start with the non-PCS MAC interfaces (config->supported_interfaces & ~config->pcs_interfaces) and OR in (pcs->supported_interfaces & config->pcs_interfaces) for each available PCS (falling back to config->pcs_interfaces when pcs->supported_interfaces is empty)?
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v22 4/15 net: pcs: implement Firmware node support for PCS driver
In reply to Christian MarangiImplement 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 .fwnode_xlate 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_xlate() is provided.
For an advanced implementation a custom xlate function is required.
On removal the PCS driver must delete itself from the provider list using fwnode_pcs_del_provider().
A devm variant devm_fwnode_pcs_add() is provided to automate the release procedure.
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 | 245 +++++++++++++++++++++++++++++++ include/linux/pcs/pcs-provider.h | 70 +++++++++ include/linux/pcs/pcs.h | 74 ++++++++++ 5 files changed, 396 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
@@ -5,6 +5,12 @@ menu "PCS device drivers" +config FWNODE_PCS + bool "PCS Firmware Node" + depends on ACPI || OF || COMPILE_TEST + help + Firmware node PCS accessors + config PCS_XPCS tristate "Synopsys DesignWare Ethernet XPCS" select PHYLINK
drivers/net/pcs/Makefile +1 -0
@@ -3,6 +3,7 @@ pcs_xpcs-$(CONFIG_PCS_XPCS) := pcs-xpcs.o pcs-xpcs-nxp.o pcs-xpcs-wx.o +obj-$(CONFIG_FWNODE_PCS) += pcs.o obj-$(CONFIG_PCS_XPCS) += pcs_xpcs.o obj-$(CONFIG_PCS_XPCS_PLATFORM) += pcs-xpcs-plat.o obj-$(CONFIG_PCS_XPCS_ROCKCHIP) += pcs-xpcs-rk.o
drivers/net/pcs/pcs.c new file +245 -0
245 lines not on this page · raw message
include/linux/pcs/pcs-provider.h new file +70 -0
70 lines not on this page · raw message
include/linux/pcs/pcs.h new file +74 -0
74 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH net-next v22 04/15] net: pcs: implement Firmware node support for PCS driver
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net: pcs: fwnode_pcs_get_from_pcsspec() clobbers fwnode_xlate() error codes --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net: pcs: fwnode_pcs_get_from_pcsspec() clobbers fwnode_xlate() error codes
--
commit e6f4f8b873c7eec5313ef7cfbc2796106104d93a
Author: Christian Marangi <[email protected]>
net: pcs: implement Firmware node support for PCS driver
This commit adds a firmware node PCS provider registry and consumer helpers to look up, count, and parse phylink PCS instances from firmware nodes.
Quoted 5 lines
drivers/net/pcs/pcs.c new file +0
-0
[ ... ]
Quoted 19 lines
drivers/net/pcs/pcs.c new file +19
-0+static struct phylink_pcs * +__fwnode_pcs_get_from_pcsspec_provider (struct fwnode_reference_args *pcsspec, +struct fwnode_pcs_provider *provider) +{ +if (provider->fwnode != pcsspec->fwnode) +return ERR_PTR (-EINVAL ); + +return provider->fwnode_xlate (pcsspec, provider->data); +} + +static struct phylink_pcs * +fwnode_pcs_get_from_pcsspec (struct fwnode_reference_args *pcsspec) +{ +struct fwnode_pcs_provider *provider; +struct phylink_pcs *pcs =NULL ; + +if (!pcsspec) +return ERR_PTR (-EINVAL ); +
drivers/net/pcs/pcs.c line 161
Quoted 10 lines
drivers/net/pcs/pcs.c new file +10
-0+mutex_lock (&fwnode_pcs_mutex); +list_for_each_entry (provider, &fwnode_pcs_providers, link) { + pcs =__fwnode_pcs_get_from_pcsspec_provider (pcsspec, provider); +if (!IS_ERR (pcs)) +break ; + } +mutex_unlock (&fwnode_pcs_mutex); + +return !IS_ERR_OR_NULL (pcs) ? pcs :ERR_PTR (-ENODEV ); +}
[Severity: Medium]
Does fwnode_pcs_get_from_pcsspec() overwrite error pointers returned by provider->fwnode_xlate() with ERR_PTR(-ENODEV)?
When the matching provider's fwnode_xlate() callback returns an error pointer such as ERR_PTR(-EINVAL) for invalid #pcs-cells arguments, !IS_ERR(pcs) is false, so the loop continues and the return statement replaces any ERR_PTR() with ERR_PTR(-ENODEV). Conversely, if fwnode_xlate() returns NULL, !IS_ERR(pcs) is true and stops the search early.
Because fwnode_phylink_pcs_parse() treats -ENODEV as an unprobed PCS provider and skips it via continue, fatal fwnode_xlate() errors are silently ignored instead of aborting parsing and returning PTR_ERR(pcs). Checking provider->fwnode == pcsspec->fwnode in the loop to invoke provider->fwnode_xlate() once on the matching provider and returning its ERR_PTR() directly (defaulting pcs to ERR_PTR(-ENODEV) when no provider matches) would preserve the error code.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v22 5/15 net: phylink: save phylink instance fwnode on phylink_create
In reply to Christian MarangiThe firmware node associated with the phylink instance might differ from the device firmware node passed in phylink_config as such device can reference the parent switch or other platform peripherals.
Some function might need to reference this firmware node after phylink_create so add a dedicated fwnode entry in phylink internal struct and save it on phylink_create().
--- drivers/net/phy/phylink.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/net/phy/phylink.c +5 -0
@@ -47,6 +47,7 @@ struct phylink {const struct phylink_mac_ops *mac_ops;struct phylink_config *config;struct phylink_pcs *pcs; +struct fwnode_handle *fwnode;struct device *dev; unsigned int old_link_state:1;
@@ -2185,6 +2186,7 @@ struct phylink *phylink_create(struct phylink_config *config,INIT_LIST_HEAD (&pl->pcs_list); pl->config = config; + pl->fwnode =fwnode_handle_get ((struct fwnode_handle *)fwnode);if (config->type ==PHYLINK_NETDEV ) { pl->netdev =to_net_dev (config->dev);netif_carrier_off (pl->netdev);
@@ -2272,6 +2274,7 @@ struct phylink *phylink_create(struct phylink_config *config,list_for_each_entry_safe (pcs, tmp, &pl->pcs_list, list)list_del (&pcs->list); free_pl: +fwnode_handle_put (pl->fwnode);kfree (pl);return ERR_PTR (ret); }
@@ -2311,6 +2314,8 @@ void phylink_destroy(struct phylink *pl)list_for_each_entry_safe (pcs, tmp, &pl->pcs_list, list)list_del (&pcs->list); +fwnode_handle_put (pl->fwnode); +kfree (pl); }EXPORT_SYMBOL_GPL (phylink_destroy);
Signature · 1 line
2.55.0
PATCH v22 6/15 net: phylink: make phylink_validate state locked
In reply to Christian MarangiIn preparation for support for PCS release/late attach, rework the phylink_validate functions to be locked under the state mutex.
With the introduction of a notifier that can be called concurrently while a phylink_create is called or an interface mode validation is being run, on recalculating the supported interface and capabilities, an inconsistency can be introduced with 2 validation run at the same time.
To handle this, make the phylink_validate lock under the phylink state mutex, add lockless variant for phylink_validate_mask and phylink_validate and add lockdep check to make sure the mutex is held in the inner validate functions.
Function that already run phylink_validate under state mutex, are updated to use the lockless variant.
Special care was needed for phy_get_rate_matching that required a lockless variant. This is introduced by adding an internal phy header.
--- drivers/net/phy/phy-internal.h | 22 ++++++++++++ drivers/net/phy/phy.c | 11 +++--- drivers/net/phy/phylink.c | 62 ++++++++++++++++++++++++++++------ 3 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 drivers/net/phy/phy-internal.h
drivers/net/phy/phy-internal.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef __PHY_INTERNAL_H +#define __PHY_INTERNAL_H + +#include <linux/lockdep.h> +#include <linux/phy.h> + +static inline int __phy_get_rate_matching (struct phy_device *phydev, +phy_interface_t iface) +{ +int ret =RATE_MATCH_NONE ; + +lockdep_assert_held (&phydev->lock); + +if (phydev->drv->get_rate_matching) +return phydev->drv->get_rate_matching (phydev, iface); + +return ret; +} + +#endif /* __PHY_INTERNAL_H */
drivers/net/phy/phy.c +5 -6
@@ -38,6 +38,7 @@#include "phylib-internal.h" #include "phy-caps.h" +#include "phy-internal.h" #define PHY_STATE_TIME HZ
@@ -146,13 +147,11 @@ EXPORT_SYMBOL(phy_print_status);int phy_get_rate_matching (struct phy_device *phydev,phy_interface_t iface) { -int ret =RATE_MATCH_NONE ; +int ret; -if (phydev->drv->get_rate_matching) { -mutex_lock (&phydev->lock); - ret = phydev->drv->get_rate_matching (phydev, iface); -mutex_unlock (&phydev->lock); - } +mutex_lock (&phydev->lock); + ret =__phy_get_rate_matching (phydev, iface); +mutex_unlock (&phydev->lock);return ret; }
drivers/net/phy/phylink.c +52 -10
125 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v22 7/15 net: phylink: add capabilities helpers and track modified advertising
In reply to Christian MarangiIn preparation for PCS release/late attach, it's needed to introduce a helpers to calculate the capabilities after a PCS is removed/added and it's needed to track the advertising in link_config changed by the user compared to the originally one calculated in phylink_create().
To archieve this, introduce dedicated advertising variables in phylink struct and update it when the new phylink_update_capabilities() is called or advertise is updated in link_config.
In phylink struct the new advertising will contain the full advertising bits right after a phylink_validate and the new user_disabled_advertising will be the mask of modes that were disabled from an ethtool ksettings (with the parsing done in phylink_update_user_disabled_advertising()).
This double reference is needed to account case when a PCS gets removed and attached again to preserve user-defined advertising rules.
phylink_update_capabilities() will calculate the new supported modes and the new capabilities, update the full advertising in phylink and restore the user-disabled modes.
phylink_create() is also updated to init the phylink advertising and link_config advertising.
phylink_update_mac_pause_capabilities() is updated to use the new phylink_update_capabilities() to account for user-disabled advertising modes and update the full advertising in phylink.
Some rework were needed in the context of fixed-link, In-Band, PHY and SFP to permit detaching the supported and link_config advertising parsing outside the original config.
SFP logic is changed to not restrict the sfp_support mask in place but instead propagate the restricted mask to phylink_sfp_set_config() similar to how it's done for the PHY codepath.
This is needed to always have an unmodified source to reparse the capabilities.
PHY advertising is changed to save the original advertising before restricting it.
--- drivers/net/phy/phylink.c | 607 +++++++++++++++++++++++++------------- 1 file changed, 397 insertions(+), 210 deletions(-)
drivers/net/phy/phylink.c +397 -210
@@ -62,6 +62,8 @@ struct phylink {u8 link_port;/* The current non-phy ethtool port */ __ETHTOOL_DECLARE_LINK_MODE_MASK (supported);__ETHTOOL_DECLARE_LINK_MODE_MASK (supported_lpi); +__ETHTOOL_DECLARE_LINK_MODE_MASK (advertising); +__ETHTOOL_DECLARE_LINK_MODE_MASK (user_disabled_advertising);/* The link configuration settings */ struct phylink_link_state link_config;
@@ -85,6 +87,8 @@ struct phylink {/* Serialize updates to pl->phydev with phylink_resolve() */ struct mutex phydev_mutex;struct phylink_link_state phy_state; +/* PHY advertisement before restricting it to MAC/PCS capabilities */ +__ETHTOOL_DECLARE_LINK_MODE_MASK (phy_advertising);unsigned int phy_ib_mode;struct work_struct resolve;unsigned int pcs_neg_mode;
@@ -724,6 +728,79 @@ static int phylink_validate(struct phylink *pl, unsigned long *supported,return ret; } +static int __phylink_validate_phy (struct phylink *pl,struct phy_device *phy, +unsigned long *supported, +struct phylink_link_state *state) +{ +DECLARE_PHY_INTERFACE_MASK (interfaces); + +/* If the PHY provides a bitmap of the interfaces it will be using +* depending on the negotiated media speeds, use this to validate +* which ethtool link modes can be used. +*/ +if (!phy_interface_empty (phy->possible_interfaces)) { +/* We only care about the union of the PHY's interfaces and +* those which the host supports. +*/ +phy_interface_and (interfaces, phy->possible_interfaces, + pl->supported_interfaces); + +if (phy_interface_empty (interfaces)) { +phylink_err (pl,"PHY has no common interfaces\n" ); +return -EINVAL ; + } + +if (phy_on_sfp (phy)) { +/* If the PHY is on a SFP, limit the interfaces to +* those that can be used with a SFP module. +*/ +phy_interface_and (interfaces, interfaces, + phylink_sfp_interfaces); + +if (phy_interface_empty (interfaces)) { +phylink_err (pl,"SFP PHY's possible interfaces becomes empty\n" ); +return -EINVAL ; + } + } + +phylink_dbg (pl,"PHY %s uses interfaces %*pbl, validating %*pbl\n" , +phydev_name (phy), + (int )PHY_INTERFACE_MODE_MAX , + phy->possible_interfaces, + (int )PHY_INTERFACE_MODE_MAX , interfaces); + +return __phylink_validate_mask (pl, phy, supported, state, + interfaces); + } + +phylink_dbg (pl,"PHY %s doesn't supply possible interfaces\n" , +phydev_name (phy)); + +/* Check whether we would use rate matching for the proposed interface +* mode. +*/ + state->rate_matching =__phy_get_rate_matching (phy, state->interface); + +/* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R, +* 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching. +* For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching +* their Serdes is either unnecessary or not reasonable. +* +* For these which switch interface modes, we really need to know which +* interface modes the PHY supports to properly work out which ethtool +* linkmodes can be supported. For now, as a work-around, we validate +* against all interface modes, which may lead to more ethtool link +* modes being advertised than are actually supported. +*/ +if (phy->is_c45 && state->rate_matching ==RATE_MATCH_NONE && + state->interface !=PHY_INTERFACE_MODE_RXAUI && + state->interface !=PHY_INTERFACE_MODE_XAUI && + state->interface !=PHY_INTERFACE_MODE_USXGMII ) + state->interface =PHY_INTERFACE_MODE_NA ; + +return __phylink_validate (pl, supported, state); +} +static void phylink_fill_fixedlink_supported (unsigned long *supported) {linkmode_set_bit (ETHTOOL_LINK_MODE_Pause_BIT, supported);
@@ -740,12 +817,51 @@ static void phylink_fill_fixedlink_supported(unsigned long *supported)linkmode_set_bit (ETHTOOL_LINK_MODE_10000baseT_Full_BIT, supported); } -static int phylink_parse_fixedlink (struct phylink *pl, -const struct fwnode_handle *fwnode) +static int phylink_parse_fixedlink_capabilities (struct phylink *pl, +struct phylink_link_state *config) {__ETHTOOL_DECLARE_LINK_MODE_MASK (match) = {0 , };__ETHTOOL_DECLARE_LINK_MODE_MASK (mask) = {0 , };const struct link_capabilities *c; + +lockdep_assert_held (&pl->state_mutex); + +linkmode_zero (pl->supported); +phylink_fill_fixedlink_supported (pl->supported); + +linkmode_copy (config->advertising, pl->supported); +__phylink_validate (pl, pl->supported, config); + +linkmode_copy (pl->advertising, config->advertising); + + c =phy_caps_lookup (config->speed, config->duplex, + pl->supported, true); +if (c) +linkmode_and (match, pl->supported, c->linkmodes); + +linkmode_set_bit (ETHTOOL_LINK_MODE_Pause_BIT, mask); +linkmode_set_bit (ETHTOOL_LINK_MODE_Asym_Pause_BIT, mask); +linkmode_set_bit (ETHTOOL_LINK_MODE_Autoneg_BIT, mask); +linkmode_and (pl->supported, pl->supported, mask); + +phylink_set (pl->supported,MII ); + +if (c) { +linkmode_or (pl->supported, pl->supported, match); +linkmode_or (config->lp_advertising, + config->lp_advertising, match); + } + +linkmode_and (config->advertising, config->advertising, + pl->supported); +linkmode_copy (pl->advertising, config->advertising); + +return c ?0 : -EINVAL ; +} + +static int phylink_parse_fixedlink (struct phylink *pl, +const struct fwnode_handle *fwnode) +{struct fwnode_handle *fixed_node;struct gpio_desc *desc;u32 speed;
@@ -817,39 +933,77 @@ static int phylink_parse_fixedlink(struct phylink *pl,phylink_warn (pl,"fixed link specifies half duplex for %dMbps link?\n" , pl->link_config.speed); -linkmode_zero (pl->supported); -phylink_fill_fixedlink_supported (pl->supported); +mutex_lock (&pl->state_mutex); + ret =phylink_parse_fixedlink_capabilities (pl, &pl->link_config); +mutex_unlock (&pl->state_mutex); +if (ret) +phylink_warn (pl,"fixed link %s duplex %dMbps not recognised\n" , + pl->link_config.duplex ==DUPLEX_FULL ?"full" :"half" , + pl->link_config.speed); -linkmode_copy (pl->link_config.advertising, pl->supported); -phylink_validate (pl, pl->supported, &pl->link_config); + pl->link_config.link =1 ; + pl->link_config.an_complete =1 ; - c =phy_caps_lookup (pl->link_config.speed, pl->link_config.duplex, - pl->supported, true); -if (c) -linkmode_and (match, pl->supported, c->linkmodes); +return 0 ; +} -linkmode_set_bit (ETHTOOL_LINK_MODE_Pause_BIT, mask); -linkmode_set_bit (ETHTOOL_LINK_MODE_Asym_Pause_BIT, mask); -linkmode_set_bit (ETHTOOL_LINK_MODE_Autoneg_BIT, mask); -linkmode_and (pl->supported, pl->supported, mask); +static int phylink_parse_inband_capabilities (struct phylink *pl, +struct phylink_link_state *config) +{ +unsigned long caps; + +lockdep_assert_held (&pl->state_mutex); +linkmode_zero (pl->supported);phylink_set (pl->supported,MII ); +phylink_set (pl->supported, Autoneg); +phylink_set (pl->supported, Asym_Pause); +phylink_set (pl->supported, Pause); -if (c) { -linkmode_or (pl->supported, pl->supported, match); -linkmode_or (pl->link_config.lp_advertising, - pl->link_config.lp_advertising, match); - }else { -phylink_warn (pl,"fixed link %s duplex %dMbps not recognised\n" , - pl->link_config.duplex ==DUPLEX_FULL ?"full" :"half" , - pl->link_config.speed); +switch (config->interface) { +case PHY_INTERFACE_MODE_SGMII : +case PHY_INTERFACE_MODE_PSGMII : +case PHY_INTERFACE_MODE_QSGMII : +case PHY_INTERFACE_MODE_QUSGMII : +case PHY_INTERFACE_MODE_RGMII : +case PHY_INTERFACE_MODE_RGMII_ID : +case PHY_INTERFACE_MODE_RGMII_RXID : +case PHY_INTERFACE_MODE_RGMII_TXID : +case PHY_INTERFACE_MODE_RTBI : +case PHY_INTERFACE_MODE_1000BASEX : +case PHY_INTERFACE_MODE_2500BASEX : +case PHY_INTERFACE_MODE_5GBASER : +case PHY_INTERFACE_MODE_25GBASER : +case PHY_INTERFACE_MODE_USXGMII : +case PHY_INTERFACE_MODE_10G_QXGMII : +case PHY_INTERFACE_MODE_10GKR : +case PHY_INTERFACE_MODE_10GBASER : +case PHY_INTERFACE_MODE_XLGMII : +case PHY_INTERFACE_MODE_50GBASER : +case PHY_INTERFACE_MODE_LAUI : +case PHY_INTERFACE_MODE_100GBASEP : + caps = ~(MAC_SYM_PAUSE |MAC_ASYM_PAUSE ); + caps =phylink_get_capabilities (config->interface, caps, +RATE_MATCH_NONE ); +phylink_caps_to_linkmodes (pl->supported, caps); +break ; + +default : +phylink_err (pl, +"incorrect link mode %s for in-band status\n" , +phy_modes (config->interface)); +return -EINVAL ; } -linkmode_and (pl->link_config.advertising, pl->link_config.advertising, - pl->supported); +linkmode_copy (config->advertising, pl->supported); - pl->link_config.link =1 ; - pl->link_config.an_complete =1 ; +if (__phylink_validate (pl, pl->supported, config)) { +phylink_err (pl, +"failed to validate link configuration for in-band status\n" ); +return -EINVAL ; + } + +linkmode_copy (pl->advertising, config->advertising);return 0 ; }
@@ -859,7 +1013,7 @@ static int phylink_parse_mode(struct phylink *pl, {struct fwnode_handle *dn;const char *managed; -unsigned long caps; +int ret;if (pl->config->default_an_inband) pl->cfg_link_an_mode =MLO_AN_INBAND ;
@@ -881,54 +1035,11 @@ static int phylink_parse_mode(struct phylink *pl, }if (pl->cfg_link_an_mode ==MLO_AN_INBAND ) { -linkmode_zero (pl->supported); -phylink_set (pl->supported,MII ); -phylink_set (pl->supported, Autoneg); -phylink_set (pl->supported, Asym_Pause); -phylink_set (pl->supported, Pause); - -switch (pl->link_config.interface) { -case PHY_INTERFACE_MODE_SGMII : -case PHY_INTERFACE_MODE_PSGMII : -case PHY_INTERFACE_MODE_QSGMII : -case PHY_INTERFACE_MODE_QUSGMII : -case PHY_INTERFACE_MODE_RGMII : -case PHY_INTERFACE_MODE_RGMII_ID : -case PHY_INTERFACE_MODE_RGMII_RXID : -case PHY_INTERFACE_MODE_RGMII_TXID : -case PHY_INTERFACE_MODE_RTBI : -case PHY_INTERFACE_MODE_1000BASEX : -case PHY_INTERFACE_MODE_2500BASEX : -case PHY_INTERFACE_MODE_5GBASER : -case PHY_INTERFACE_MODE_25GBASER : -case PHY_INTERFACE_MODE_USXGMII : -case PHY_INTERFACE_MODE_10G_QXGMII : -case PHY_INTERFACE_MODE_10GKR : -case PHY_INTERFACE_MODE_10GBASER : -case PHY_INTERFACE_MODE_XLGMII : -case PHY_INTERFACE_MODE_50GBASER : -case PHY_INTERFACE_MODE_LAUI : -case PHY_INTERFACE_MODE_100GBASEP : - caps = ~(MAC_SYM_PAUSE |MAC_ASYM_PAUSE ); - caps =phylink_get_capabilities (pl->link_config.interface, caps, -RATE_MATCH_NONE ); -phylink_caps_to_linkmodes (pl->supported, caps); -break ; - -default : -phylink_err (pl, -"incorrect link mode %s for in-band status\n" , -phy_modes (pl->link_config.interface)); -return -EINVAL ; - } - -linkmode_copy (pl->link_config.advertising, pl->supported); - -if (phylink_validate (pl, pl->supported, &pl->link_config)) { -phylink_err (pl, -"failed to validate link configuration for in-band status\n" ); -return -EINVAL ; - } +mutex_lock (&pl->state_mutex); + ret =phylink_parse_inband_capabilities (pl, &pl->link_config); +mutex_unlock (&pl->state_mutex); +if (ret) +return ret; }return 0 ;
@@ -2087,6 +2198,8 @@ static void phylink_update_pause_state(struct phylink *pl, int pause_state)*/ linkmode_set_pause (config->advertising, tx_pause, rx_pause); +if (pl->phydev) +linkmode_set_pause (pl->phy_advertising, tx_pause, rx_pause); manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN || (!(pause_state & MLO_PAUSE_AN) &&
@@ -2120,6 +2233,142 @@ static void phylink_update_pause_state(struct phylink *pl, int pause_state) } } +static void phylink_update_user_disabled_advertising (struct phylink *pl, +const unsigned long *advertising) +{ +__ETHTOOL_DECLARE_LINK_MODE_MASK (disabled); + +/* Preserve restrictions for modes absent from the current full mask. */ +linkmode_andnot (disabled, pl->user_disabled_advertising, + pl->advertising); + +/* Update restrictions for modes in the current full advertisement. */ +linkmode_andnot (pl->user_disabled_advertising, pl->advertising, + advertising); + +/* Combine the updated restrictions with those preserved above. */ +linkmode_or (pl->user_disabled_advertising, + pl->user_disabled_advertising, disabled); +} + +static void phylink_sfp_update_capabilities (struct phylink *pl, +unsigned long *supported, +struct phylink_link_state *config, +phy_interface_t interface) +{ +enum inband_type inband_type; + + inband_type =phylink_get_inband_type (interface); +if (inband_type ==INBAND_NONE ) { +/* If this is the sole interface, and there is no inband +* support, clear the advertising mask and Autoneg bit in +* the support mask. Otherwise, just clear the Autoneg bit +* in the advertising mask. +*/ +if (phy_interface_weight (pl->sfp_interfaces) ==1 ) { +linkmode_clear_bit (ETHTOOL_LINK_MODE_Autoneg_BIT, + supported); +linkmode_zero (config->advertising); + }else { +linkmode_clear_bit (ETHTOOL_LINK_MODE_Autoneg_BIT, + config->advertising); + } + } +} + +static int phylink_phy_update_capabilities (struct phylink *pl,struct phy_device *phy, +phy_interface_t interface) +{ +__ETHTOOL_DECLARE_LINK_MODE_MASK (supported); +struct phylink_link_state config; +int ret; + +lockdep_assert_held (&pl->phydev_mutex); +lockdep_assert_held (&phy->lock); +lockdep_assert_held (&pl->state_mutex); + +memset (&config,0 ,sizeof (config)); +linkmode_copy (supported, phy->supported); +linkmode_copy (config.advertising, pl->phy_advertising); + config.interface = interface; + + ret =__phylink_validate_phy (pl, phy, supported, &config); +if (ret) { +phylink_warn (pl,"validation of %s with support %*pb and advertisement %*pb failed: %pe\n" , +phy_modes (config.interface), + __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported, + __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising, +ERR_PTR (ret)); +return ret; + } + +linkmode_copy (pl->supported, supported); +linkmode_copy (pl->link_config.advertising, config.advertising); +linkmode_copy (pl->advertising, pl->link_config.advertising); + +/* Restrict the phy advertisement according to the MAC support. */ +linkmode_copy (phy->advertising, config.advertising); + +return 0 ; +} + +static void phylink_update_capabilities (struct phylink *pl, +struct phylink_link_state *config) +{ +if (pl->phydev) { +lockdep_assert_held (&pl->phydev_mutex); +lockdep_assert_held (&pl->phydev->lock); + } +lockdep_assert_held (&pl->state_mutex); + +if (pl->phydev) { +phylink_phy_update_capabilities (pl, pl->phydev, + pl->phy_state.interface); +return ; + } + +if (pl->cfg_link_an_mode ==MLO_AN_FIXED ) { +phylink_parse_fixedlink_capabilities (pl, config); + goto exit; + } + +if (pl->cfg_link_an_mode ==MLO_AN_INBAND ) { +phylink_parse_inband_capabilities (pl, config); + goto exit; + } + +/* Revalidate the SFP module modes against every available interface. */ +if (pl->sfp_bus && !pl->phydev && config->interface && + !phy_interface_empty (pl->sfp_interfaces)) { +__ETHTOOL_DECLARE_LINK_MODE_MASK (supported); + +linkmode_copy (supported, pl->sfp_support); +linkmode_copy (config->advertising, pl->sfp_support); +if (!__phylink_validate_mask (pl,NULL , supported, config, + pl->sfp_interfaces)) { +phylink_sfp_update_capabilities (pl, supported, config, + config->interface); +linkmode_copy (pl->supported, supported); +linkmode_copy (pl->advertising, config->advertising); + } + + goto exit; + } + +/* No media is attached yet. Recalculate the generic capabilities. */ +linkmode_fill (pl->supported); +linkmode_copy (config->advertising, pl->supported); +__phylink_validate (pl, pl->supported, config); + +/* Save the newly calculated full advertisement */ +linkmode_copy (pl->advertising, config->advertising); + +exit: +/* Keep user-disabled modes out of the active advertisement. */ +linkmode_andnot (config->advertising, + pl->advertising, pl->user_disabled_advertising); +} +/** * phylink_update_mac_pause_capabilities() - Dynamically update MAC pause * @pl: a pointer to a &struct phylink returned from phylink_create()
@@ -2132,6 +2381,7 @@ static void phylink_update_pause_state(struct phylink *pl, int pause_state)void phylink_update_mac_pause_capabilities (struct phylink *pl,unsigned long mac_pause) {struct phylink_link_state *config = &pl->link_config; +struct phy_device *phydev;unsigned long old_pause;int pause_state;
@@ -2146,20 +2396,16 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long macif (old_pause == mac_pause)return ; +mutex_lock (&pl->phydev_mutex); + phydev = pl->phydev; +if (phydev) +mutex_lock (&phydev->lock);mutex_lock (&pl->state_mutex); pl->config->mac_capabilities &= ~(MAC_SYM_PAUSE |MAC_ASYM_PAUSE ); pl->config->mac_capabilities |= mac_pause; -phylink_set (pl->supported, Pause); -phylink_set (pl->supported, Asym_Pause); - -if (pl->phydev) -linkmode_and (pl->supported, pl->supported, pl->phydev->supported); -else if (pl->sfp_bus) -linkmode_and (pl->supported, pl->supported, pl->sfp_support); - -__phylink_validate (pl, pl->supported, config); +phylink_update_capabilities (pl, config); pause_state = config->pause;
@@ -2171,6 +2417,9 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac }mutex_unlock (&pl->state_mutex); +if (phydev) +mutex_unlock (&phydev->lock); +mutex_unlock (&pl->phydev_mutex);phylink_update_pause_state (pl, pause_state); }
@@ -2259,6 +2508,8 @@ struct phylink *phylink_create(struct phylink_config *config, pl->link_config.pause =MLO_PAUSE_AN ; pl->link_config.speed =SPEED_UNKNOWN ; pl->link_config.duplex =DUPLEX_UNKNOWN ; +linkmode_fill (pl->advertising); +linkmode_fill (pl->link_config.advertising); pl->pcs_state =PCS_STATE_DOWN ; pl->mac_ops = mac_ops;__set_bit (PHYLINK_DISABLE_STOPPED , &pl->phylink_disable_state);
@@ -2282,9 +2533,9 @@ struct phylink *phylink_create(struct phylink_config *config, pl->supported_interfaces, pcs->supported_interfaces); -linkmode_fill (pl->supported); -linkmode_copy (pl->link_config.advertising, pl->supported); -phylink_validate (pl, pl->supported, &pl->link_config); +mutex_lock (&pl->state_mutex); +phylink_update_capabilities (pl, &pl->link_config); +mutex_unlock (&pl->state_mutex); ret =phylink_parse_mode (pl, fwnode);if (ret <0 )
@@ -2417,84 +2668,9 @@ static void phylink_phy_change(struct phy_device *phydev, bool up) phydev->enable_tx_lpi ? "" : "no"); } -static int phylink_validate_phy (struct phylink *pl,struct phy_device *phy, -unsigned long *supported, -struct phylink_link_state *state) -{ -DECLARE_PHY_INTERFACE_MASK (interfaces); - -/* If the PHY provides a bitmap of the interfaces it will be using -* depending on the negotiated media speeds, use this to validate -* which ethtool link modes can be used. -*/ -if (!phy_interface_empty (phy->possible_interfaces)) { -/* We only care about the union of the PHY's interfaces and -* those which the host supports. -*/ -phy_interface_and (interfaces, phy->possible_interfaces, - pl->supported_interfaces); - -if (phy_interface_empty (interfaces)) { -phylink_err (pl,"PHY has no common interfaces\n" ); -return -EINVAL ; - } - -if (phy_on_sfp (phy)) { -/* If the PHY is on a SFP, limit the interfaces to -* those that can be used with a SFP module. -*/ -phy_interface_and (interfaces, interfaces, - phylink_sfp_interfaces); - -if (phy_interface_empty (interfaces)) { -phylink_err (pl,"SFP PHY's possible interfaces becomes empty\n" ); -return -EINVAL ; - } - } - -phylink_dbg (pl,"PHY %s uses interfaces %*pbl, validating %*pbl\n" , -phydev_name (phy), - (int )PHY_INTERFACE_MODE_MAX , - phy->possible_interfaces, - (int )PHY_INTERFACE_MODE_MAX , interfaces); - -return phylink_validate_mask (pl, phy, supported, state, - interfaces); - } - -phylink_dbg (pl,"PHY %s doesn't supply possible interfaces\n" , -phydev_name (phy)); - -/* Check whether we would use rate matching for the proposed interface -* mode. -*/ - state->rate_matching =phy_get_rate_matching (phy, state->interface); - -/* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R, -* 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching. -* For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching -* their Serdes is either unnecessary or not reasonable. -* -* For these which switch interface modes, we really need to know which -* interface modes the PHY supports to properly work out which ethtool -* linkmodes can be supported. For now, as a work-around, we validate -* against all interface modes, which may lead to more ethtool link -* modes being advertised than are actually supported. -*/ -if (phy->is_c45 && state->rate_matching ==RATE_MATCH_NONE && - state->interface !=PHY_INTERFACE_MODE_RXAUI && - state->interface !=PHY_INTERFACE_MODE_XAUI && - state->interface !=PHY_INTERFACE_MODE_USXGMII ) - state->interface =PHY_INTERFACE_MODE_NA ; - -return phylink_validate (pl, supported, state); -} -static int phylink_bringup_phy (struct phylink *pl,struct phy_device *phy,phy_interface_t interface) { -struct phylink_link_state config; -__ETHTOOL_DECLARE_LINK_MODE_MASK (supported);char *irq_str;int ret;
@@ -2507,18 +2683,17 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,*/ phy_support_asym_pause (phy); -memset (&config,0 ,sizeof (config)); -linkmode_copy (supported, phy->supported); -linkmode_copy (config.advertising, phy->advertising); - config.interface = interface; +mutex_lock (&pl->phydev_mutex); +mutex_lock (&phy->lock); +mutex_lock (&pl->state_mutex); + +linkmode_copy (pl->phy_advertising, phy->advertising); - ret =phylink_validate_phy (pl, phy, supported, &config); + ret =phylink_phy_update_capabilities (pl, phy, interface);if (ret) { -phylink_warn (pl,"validation of %s with support %*pb and advertisement %*pb failed: %pe\n" , -phy_modes (config.interface), - __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported, - __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising, -ERR_PTR (ret)); +mutex_unlock (&pl->state_mutex); +mutex_unlock (&phy->lock); +mutex_unlock (&pl->phydev_mutex);return ret; }
@@ -2531,19 +2706,11 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy, dev_name(&phy->mdio.dev), phy->drv->name, irq_str);kfree (irq_str); -mutex_lock (&pl->phydev_mutex); -mutex_lock (&phy->lock); -mutex_lock (&pl->state_mutex); pl->phy_state.interface = interface; pl->phy_state.pause =MLO_PAUSE_NONE ; pl->phy_state.speed =SPEED_UNKNOWN ; pl->phy_state.duplex =DUPLEX_UNKNOWN ; pl->phy_state.rate_matching =RATE_MATCH_NONE ; -linkmode_copy (pl->supported, supported); -linkmode_copy (pl->link_config.advertising, config.advertising); - -/* Restrict the phy advertisement according to the MAC support. */ -linkmode_copy (phy->advertising, config.advertising);/* If the MAC supports phylink managed EEE, restrict the EEE * advertisement according to the MAC's LPI capabilities.
@@ -3403,6 +3570,7 @@ static bool phylink_validate_pcs_inband_autoneg(struct phylink *pl,int phylink_ethtool_ksettings_set (struct phylink *pl,const struct ethtool_link_ksettings *kset) { +__ETHTOOL_DECLARE_LINK_MODE_MASK (requested_advertising);__ETHTOOL_DECLARE_LINK_MODE_MASK (support);const struct link_capabilities *c;struct phylink_link_state config;
@@ -3411,6 +3579,7 @@ int phylink_ethtool_ksettings_set(struct phylink *pl,if (pl->phydev) {struct ethtool_link_ksettings phy_kset = *kset; +int ret; linkmode_and(phy_kset.link_modes.advertising, phy_kset.link_modes.advertising,
@@ -3434,7 +3603,15 @@ int phylink_ethtool_ksettings_set(struct phylink *pl,* the presence of a PHY, this should not be changed as that * should be determined from the media side advertisement. */ -return phy_ethtool_ksettings_set (pl->phydev, &phy_kset); + ret =phy_ethtool_ksettings_set (pl->phydev, &phy_kset); +if (!ret) { +mutex_lock (&pl->state_mutex); +linkmode_copy (pl->phy_advertising, + phy_kset.link_modes.advertising); +mutex_unlock (&pl->state_mutex); + } + +return ret; } config = pl->link_config;
@@ -3492,6 +3669,7 @@ int phylink_ethtool_ksettings_set(struct phylink *pl,*/ linkmode_mod_bit (ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising, kset->base.autoneg ==AUTONEG_ENABLE ); +linkmode_copy (requested_advertising, config.advertising);/* If this link is with an SFP, ensure that changes to advertised modes * also cause the associated interface to be selected such that the
@@ -3539,6 +3717,7 @@ int phylink_ethtool_ksettings_set(struct phylink *pl,return -EINVAL ;mutex_lock (&pl->state_mutex); +phylink_update_user_disabled_advertising (pl, requested_advertising); pl->link_config.speed = config.speed; pl->link_config.duplex = config.duplex;
@@ -4022,19 +4201,34 @@ static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus) }static phy_interface_t phylink_choose_sfp_interface (struct phylink *pl, -const unsigned long *intf) +const unsigned long *intf, +const unsigned long *supported, +const struct phylink_link_state *state) { +__ETHTOOL_DECLARE_LINK_MODE_MASK (tmp_supported); +struct phylink_link_state tmp_state;phy_interface_t interface;size_t i; - interface =PHY_INTERFACE_MODE_NA ; -for (i =0 ; i <ARRAY_SIZE (phylink_sfp_interface_preference); i++) -if (test_bit (phylink_sfp_interface_preference[i], intf)) { - interface = phylink_sfp_interface_preference[i]; -break ; - } +for (i =0 ; i <ARRAY_SIZE (phylink_sfp_interface_preference); i++) { + interface = phylink_sfp_interface_preference[i]; +if (!test_bit (interface, intf)) +continue ; -return interface; + tmp_state = *state; + tmp_state.interface = interface; +linkmode_copy (tmp_supported, supported); +if (phylink_validate (pl, tmp_supported, &tmp_state)) +continue ; + +/* Skip this preferred interface if user-disabled in advertising */ +linkmode_andnot (tmp_state.advertising, tmp_state.advertising, + pl->user_disabled_advertising); +if (!phylink_is_empty_linkmode (tmp_state.advertising)) +return interface; + } + +return PHY_INTERFACE_MODE_NA ; } static void phylink_sfp_set_config(struct phylink *pl, unsigned long *supported,
@@ -4052,6 +4246,11 @@ static void phylink_sfp_set_config(struct phylink *pl, unsigned long *supported, changed = true; } +linkmode_copy (pl->advertising, state->advertising); +if (!pl->phydev) +linkmode_andnot (state->advertising, pl->advertising, + pl->user_disabled_advertising); +if (!linkmode_equal (pl->link_config.advertising, state->advertising)) {linkmode_copy (pl->link_config.advertising, state->advertising); changed = true;
@@ -4128,8 +4327,8 @@ static int phylink_sfp_config_phy(struct phylink *pl, struct phy_device *phy)static int phylink_sfp_config_optical (struct phylink *pl) {__ETHTOOL_DECLARE_LINK_MODE_MASK (support); +__ETHTOOL_DECLARE_LINK_MODE_MASK (sfp_supported);struct phylink_link_state config; -enum inband_type inband_type;phy_interface_t interface;int ret;
@@ -4151,15 +4350,16 @@ static int phylink_sfp_config_optical(struct phylink *pl)memset (&config,0 ,sizeof (config));linkmode_copy (support, pl->sfp_support); +linkmode_copy (sfp_supported, support);linkmode_copy (config.advertising, pl->sfp_support); config.speed =SPEED_UNKNOWN ; config.duplex =DUPLEX_UNKNOWN ; config.pause =MLO_PAUSE_AN ; -/* For all the interfaces that are supported, reduce the sfp_support +/* For all the interfaces that are supported, reduce the temporary * mask to only those link modes that can be supported. */ - ret =phylink_validate_mask (pl,NULL , pl->sfp_support, &config, + ret =phylink_validate_mask (pl,NULL , sfp_supported, &config, pl->sfp_interfaces);if (ret) { phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
@@ -4167,7 +4367,8 @@ static int phylink_sfp_config_optical(struct phylink *pl)return ret; } - interface =phylink_choose_sfp_interface (pl, pl->sfp_interfaces); + interface =phylink_choose_sfp_interface (pl, pl->sfp_interfaces, + sfp_supported, &config);if (interface ==PHY_INTERFACE_MODE_NA ) {phylink_err (pl,"failed to select SFP interface\n" );return -EINVAL ;
@@ -4176,22 +4377,8 @@ static int phylink_sfp_config_optical(struct phylink *pl)phylink_dbg (pl,"optical SFP: chosen %s interface\n" ,phy_modes (interface)); - inband_type =phylink_get_inband_type (interface); -if (inband_type ==INBAND_NONE ) { -/* If this is the sole interface, and there is no inband -* support, clear the advertising mask and Autoneg bit in -* the support mask. Otherwise, just clear the Autoneg bit -* in the advertising mask. -*/ -if (phy_interface_weight (pl->sfp_interfaces) ==1 ) { -linkmode_clear_bit (ETHTOOL_LINK_MODE_Autoneg_BIT, - pl->sfp_support); -linkmode_zero (config.advertising); - }else { -linkmode_clear_bit (ETHTOOL_LINK_MODE_Autoneg_BIT, - config.advertising); - } - } +phylink_sfp_update_capabilities (pl, sfp_supported, &config, + interface);if (!phylink_validate_pcs_inband_autoneg (pl, interface, config.advertising)) {
@@ -4212,7 +4399,7 @@ static int phylink_sfp_config_optical(struct phylink *pl) pl->link_port = pl->sfp_port; -phylink_sfp_set_config (pl, pl->sfp_support, &config, false); +phylink_sfp_set_config (pl, sfp_supported, &config, false);return 0 ; }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH net-next v22 07/15] net: phylink: add capabilities helpers and track modified advertising
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] net: phylink: in-band check bypasses SFP in phylink_update_capabilities() - [High] net: phylink…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] net: phylink: in-band check bypasses SFP in phylink_update_capabilities()
- [High] net: phylink: broken pause update in phylink_update_mac_pause_capabilities()
- [High] net: phylink: unchecked validation failure in phylink_update_capabilities()
- [Medium] net: phylink: stale Autoneg bit saved in phylink_ethtool_ksettings_set()
- [Medium] net: phylink: untracked advertising in phylink_update_pause_state()
- [Medium] net: phylink: broken multi-interface SFP tracking in pl->advertising
- [Medium] net: phylink: phylink_choose_sfp_interface() fails on disabled modes
--
commit 8b66b984e920c4acb0054fea1ab1a279ce12371a
Author: Christian Marangi <[email protected]>
net: phylink: add capabilities helpers and track modified advertising
This commit adds helpers to recalculate phylink capabilities and tracks full and user-disabled advertising masks across fixed-link, in-band, PHY, and SFP modes in preparation for dynamic PCS attach and release.
Quoted 4 lines
drivers/net/phy/phylink.c +0
-0
[ ... ]
drivers/net/phy/phylink.c static void phylink_update_pause_state(struct phylink *pl, int pause_state) · line 2205
Quoted 9 lines
drivers/net/phy/phylink.c +2
-0@@ -2087,6 +2198,8 @@ static void phylink_update_pause_state(struct phylink *pl, int pause_state)*/ linkmode_set_pause (config->advertising, tx_pause, rx_pause); +if (pl->phydev) +linkmode_set_pause (pl->phy_advertising, tx_pause, rx_pause); manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN || (!(pause_state & MLO_PAUSE_AN) &&
[Severity: Medium]
When pl->phydev is NULL, does phylink_update_pause_state() leave pl->user_disabled_advertising and pl->advertising unupdated?
On in-band or optical SFP links, pause changes made via ethtool -A only update config->advertising here. Any later call to phylink_update_capabilities() or phylink_sfp_set_config() recalculates config->advertising from pl->advertising and pl->user_disabled_advertising, overwriting the user's pause settings. Similarly, phylink_set_fixed_link() updates pl->link_config.advertising without updating pl->advertising.
Quoted 11 lines
drivers/net/phy/phylink.c +7
-0@@ -2120,6 +2233,142 @@ static void phylink_update_pause_state(struct phylink *pl, int pause_state) } } +static void phylink_update_user_disabled_advertising (struct phylink *pl, +const unsigned long *advertising) +{ +__ETHTOOL_DECLARE_LINK_MODE_MASK (disabled); + +/* Preserve restrictions for modes absent from the current full mask. */ + linkmode_andnot(disabled, pl->user_disabled_advertising,
drivers/net/phy/phylink.c static void phylink_update_pause_state(struct phylink *pl, int pause_state) · line 2252
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+ pl->advertising); + +/* Update restrictions for modes in the current full advertisement. */ +linkmode_andnot (pl->user_disabled_advertising, pl->advertising, + advertising); + +/* Combine the updated restrictions with those preserved above. */ +linkmode_or (pl->user_disabled_advertising, + pl->user_disabled_advertising, disabled); +}
[Severity: Medium]
Can modes outside pl->advertising ever be re-enabled once added to pl->user_disabled_advertising?
In phylink_sfp_config_optical(), phylink_validate() narrows config.advertising to the single selected interface before phylink_sfp_set_config() copies it into pl->advertising, and phylink_ethtool_ksettings_set() never updates pl->advertising when switching SFP interfaces. Because disabled here is not masked with ~advertising, any mode not in the current interface's pl->advertising remains permanently stuck in pl->user_disabled_advertising even when explicitly requested in advertising, and switching SFP interfaces via ethtool marks the previous interface's modes as disabled.
[ ... ]
Quoted 32 lines
drivers/net/phy/phylink.c +32
-0+static void phylink_update_capabilities (struct phylink *pl, +struct phylink_link_state *config) +{ +if (pl->phydev) { +lockdep_assert_held (&pl->phydev_mutex); +lockdep_assert_held (&pl->phydev->lock); + } +lockdep_assert_held (&pl->state_mutex); + +if (pl->phydev) { +phylink_phy_update_capabilities (pl, pl->phydev, + pl->phy_state.interface); +return ; + } + +if (pl->cfg_link_an_mode ==MLO_AN_FIXED ) { +phylink_parse_fixedlink_capabilities (pl, config); + goto exit; + } + +if (pl->cfg_link_an_mode ==MLO_AN_INBAND ) { +phylink_parse_inband_capabilities (pl, config); + goto exit; + } + +/* Revalidate the SFP module modes against every available interface. */ +if (pl->sfp_bus && !pl->phydev && config->interface && + !phy_interface_empty (pl->sfp_interfaces)) { +__ETHTOOL_DECLARE_LINK_MODE_MASK (supported); + +linkmode_copy (supported, pl->sfp_support); +linkmode_copy (config->advertising, pl->sfp_support);
drivers/net/phy/phylink.c static void phylink_update_pause_state(struct phylink *pl, int pause_state) · line 2356
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+if (!__phylink_validate_mask (pl,NULL , supported, config, + pl->sfp_interfaces)) { +phylink_sfp_update_capabilities (pl, supported, config, + config->interface); +linkmode_copy (pl->supported, supported); +linkmode_copy (pl->advertising, config->advertising); + } + + goto exit; + }
[Severity: High]
Does checking pl->cfg_link_an_mode == MLO_AN_INBAND before pl->sfp_bus bypass SFP capability recalculation on in-band SFP ports?
Direct MAC-to-SFP links specify managed = "in-band-status" in DT (or set config->default_an_inband = true), so pl->cfg_link_an_mode is MLO_AN_INBAND. Calling phylink_update_capabilities() (for example from phylink_update_mac_pause_capabilities()) runs phylink_parse_inband_capabilities() and jumps to exit, overwriting the SFP module's pl->supported and advertising masks with generic MII capabilities (and failing with -EINVAL on 100BASE-X SFP modules). In addition, when the SFP block is reached, it validates config->advertising across all pl->sfp_interfaces via __phylink_validate_mask() without restricting config->advertising to the active config->interface as phylink_sfp_config_optical() does.
Quoted 4 lines
drivers/net/phy/phylink.c +4
-0+ +/* No media is attached yet. Recalculate the generic capabilities. */ +linkmode_fill (pl->supported); +linkmode_copy (config->advertising, pl->supported);
drivers/net/phy/phylink.c static void phylink_update_pause_state(struct phylink *pl, int pause_state) · line 2370
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+__phylink_validate (pl, pl->supported, config); + +/* Save the newly calculated full advertisement */ +linkmode_copy (pl->advertising, config->advertising); + +exit: +/* Keep user-disabled modes out of the active advertisement. */ +linkmode_andnot (config->advertising, + pl->advertising, pl->user_disabled_advertising); +}
[Severity: High]
What happens to pl->supported, pl->advertising, and config->advertising when __phylink_validate() fails because config->interface is not in pl->supported_interfaces?
Before a late-attached PCS is registered in phylink_create() (or after a PCS is removed), __phylink_validate() returns -EINVAL immediately on the !test_bit(state->interface, interfaces) check without modifying pl->supported or config->advertising. Because the return value is ignored here after linkmode_fill(pl->supported), all three masks remain filled with all ones. Similarly, on validation failure, phylink_phy_update_capabilities() returns early leaving stale capabilities from a removed PCS in pl->supported and phy->advertising, phylink_parse_inband_capabilities() leaves pl->supported unvalidated and causes phylink_parse_mode() to fail phylink_create() when an in-band link uses a late-attached PCS, and phylink_parse_fixedlink_capabilities() ignores the __phylink_validate() failure.
Quoted 27 lines
drivers/net/phy/phylink.c +5
-9@@ -2146,20 +2396,16 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long macif (old_pause == mac_pause)return ; +mutex_lock (&pl->phydev_mutex); + phydev = pl->phydev; +if (phydev) +mutex_lock (&phydev->lock);mutex_lock (&pl->state_mutex); pl->config->mac_capabilities &= ~(MAC_SYM_PAUSE |MAC_ASYM_PAUSE ); pl->config->mac_capabilities |= mac_pause; -phylink_set (pl->supported, Pause); -phylink_set (pl->supported, Asym_Pause); - -if (pl->phydev) -linkmode_and (pl->supported, pl->supported, pl->phydev->supported); -else if (pl->sfp_bus) -linkmode_and (pl->supported, pl->supported, pl->sfp_support); - -__phylink_validate (pl, pl->supported, config); +phylink_update_capabilities (pl, config); pause_state = config->pause;
drivers/net/phy/phylink.c void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac · line 2425
Quoted 9 lines
drivers/net/phy/phylink.c +3
-0@@ -2171,6 +2417,9 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac }mutex_unlock (&pl->state_mutex); +if (phydev) +mutex_unlock (&phydev->lock); +mutex_unlock (&pl->phydev_mutex);phylink_update_pause_state (pl, pause_state); }
[Severity: High]
When a PHY is attached, does calling phylink_update_capabilities() followed by phylink_update_pause_state() break PHY pause renegotiation and permanently clear pause in pl->phy_advertising?
First, phylink_phy_update_capabilities() already updates phy->advertising with the new pause bits, so when phylink_update_pause_state() subsequently calls phy_set_asym_pause(), phy->advertising matches oldadv and phy_start_aneg() is skipped. Second, phylink_update_pause_state() writes the stripped pause_state (or the initial MLO_PAUSE_AN where MLO_PAUSE_RX and MLO_PAUSE_TX are both 0) into pl->phy_advertising via linkmode_set_pause(), permanently losing the PHY's pause advertisement when MAC pause capabilities are later re-enabled.
[ ... ]
Quoted 5 lines
drivers/net/phy/phylink.c +0
-1@@ -3434,7 +3603,15 @@ int phylink_ethtool_ksettings_set(struct phylink *pl,* the presence of a PHY, this should not be changed as that * should be determined from the media side advertisement. */ -return phy_ethtool_ksettings_set (pl->phydev, &phy_kset);
drivers/net/phy/phylink.c int phylink_ethtool_ksettings_set(struct phylink *pl, · line 3615
Quoted 10 lines
drivers/net/phy/phylink.c +9
-0+ ret =phy_ethtool_ksettings_set (pl->phydev, &phy_kset); +if (!ret) { +mutex_lock (&pl->state_mutex); +linkmode_copy (pl->phy_advertising, + phy_kset.link_modes.advertising); +mutex_unlock (&pl->state_mutex); + } + +return ret; }
[Severity: Medium]
Does copying phy_kset.link_modes.advertising into pl->phy_advertising save a stale ETHTOOL_LINK_MODE_Autoneg_BIT and a MAC-restricted mask?
phy_ethtool_ksettings_set() takes a const struct ethtool_link_ksettings * and updates ETHTOOL_LINK_MODE_Autoneg_BIT in phydev->advertising from cmd->base.autoneg without modifying phy_kset.link_modes.advertising, which was also already masked by pl->supported above. When phylink_phy_update_capabilities() later copies pl->phy_advertising back to phy->advertising, it overwrites the PHY's Autoneg bit and loses modes that were only temporarily unsupported by the MAC/PCS.
[ ... ]
Quoted 31 lines
drivers/net/phy/phylink.c +14
-8@@ -4022,19 +4201,34 @@ static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus) }static phy_interface_t phylink_choose_sfp_interface (struct phylink *pl, -const unsigned long *intf) +const unsigned long *intf, +const unsigned long *supported, +const struct phylink_link_state *state) { +__ETHTOOL_DECLARE_LINK_MODE_MASK (tmp_supported); +struct phylink_link_state tmp_state;phy_interface_t interface;size_t i; - interface =PHY_INTERFACE_MODE_NA ; -for (i =0 ; i <ARRAY_SIZE (phylink_sfp_interface_preference); i++) -if (test_bit (phylink_sfp_interface_preference[i], intf)) { - interface = phylink_sfp_interface_preference[i]; -break ; - } +for (i =0 ; i <ARRAY_SIZE (phylink_sfp_interface_preference); i++) { + interface = phylink_sfp_interface_preference[i]; +if (!test_bit (interface, intf)) +continue ; -return interface; + tmp_state = *state; + tmp_state.interface = interface; +linkmode_copy (tmp_supported, supported); +if (phylink_validate (pl, tmp_supported, &tmp_state)) +continue ;
drivers/net/phy/phylink.c static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus) · line 4232
Quoted 10 lines
drivers/net/phy/phylink.c +9
-0+ +/* Skip this preferred interface if user-disabled in advertising */ +linkmode_andnot (tmp_state.advertising, tmp_state.advertising, + pl->user_disabled_advertising); +if (!phylink_is_empty_linkmode (tmp_state.advertising)) +return interface; + } + +return PHY_INTERFACE_MODE_NA ; }
[Severity: Medium]
What happens if all link modes supported by a newly inserted optical SFP module are currently disabled in pl->user_disabled_advertising?
phylink_choose_sfp_interface() skips every interface and returns PHY_INTERFACE_MODE_NA without a fallback, causing phylink_sfp_config_optical() to fail module insertion with -EINVAL before phylink_sfp_set_config() updates pl->supported. The SFP state machine enters SFP_MOD_ERROR and the user cannot re-enable the module's modes via ethtool.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v22 8/15 net: phylink: support PCS provider release
In reply to Christian MarangiAdd support for release of fwnode PCS from a PCS provider. 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_DEL every time a new PCS provider is released.
phylink will then react to this event and will call the new function fwnode_pcs_matches_provider() on each PCS of the phylink instance and will check if the PCS is provided by the PCS provider that is getting released.
If a related PCS is found, then such PCS is removed from the phylink instance PCS list and the supported_interfaces value is updated.
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.
Finally a phylink resolve is triggered to handle the PCS removal.
Protect every access of phylink pcs_list with state_mutex to handle async PCS provider release and capabilities recalculation.
--- drivers/net/pcs/pcs.c | 47 +++++++++ drivers/net/phy/phylink.c | 203 ++++++++++++++++++++++++++++++++------ include/linux/pcs/pcs.h | 69 +++++++++++++ 3 files changed, 289 insertions(+), 30 deletions(-)
drivers/net/pcs/pcs.c +47 -0
@@ -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_xlate (struct fwnode_reference_args *pcsspec,void *data)
@@ -74,6 +87,11 @@ void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp)mutex_unlock (&fwnode_pcs_mutex); +/* Signal phylink to release any PCS from this provider */ +blocking_notifier_call_chain (&fwnode_pcs_notify_list, +FWNODE_PCS_PROVIDER_DEL , + pp); +fwnode_handle_put (pp->fwnode);kfree (pp); }
@@ -183,6 +201,35 @@ struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode, unsigned }EXPORT_SYMBOL_GPL (fwnode_pcs_get); +bool fwnode_pcs_matches_provider (struct fwnode_pcs_provider *provider, +const struct fwnode_handle *fwnode, +struct phylink_pcs *pl_pcs) +{ +struct fwnode_reference_args pcsspec; +int index =0 ; +int ret; + +while (true) { +struct phylink_pcs *pcs; + + ret =fwnode_parse_pcsspec (fwnode, index,NULL , &pcsspec); +if (ret) +return false; + + pcs =__fwnode_pcs_get_from_pcsspec_provider (&pcsspec, provider); +if (!IS_ERR (pcs) && pcs == pl_pcs) { +fwnode_handle_put (pcsspec.fwnode); +return true; + } + +fwnode_handle_put (pcsspec.fwnode); + index++; + } + +return false; +} +EXPORT_SYMBOL_GPL (fwnode_pcs_matches_provider); +unsigned int fwnode_phylink_pcs_count (struct fwnode_handle *fwnode) {struct fwnode_reference_args out_args;
drivers/net/phy/phylink.c +173 -30
325 lines not on this page · raw message
include/linux/pcs/pcs.h +69 -0
94 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH net-next v22 08/15] net: phylink: support PCS provider release
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] net: phylink: broken capability recalculation in phylink_update_capabilities() - [Medium] net…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] net: phylink: broken capability recalculation in phylink_update_capabilities()
- [Medium] net: phylink: mishandled force_major_config with phylink_disable_state
- [Medium] net: phylink: race between pcs_provider_notify() and phylink_create()
- [Medium] net: phylink: missing hardware advertisement update in pcs_provider_notify()
--
commit 43bb91371f247055f063dc4ec828173d12f85adf
Author: Christian Marangi <[email protected]>
net: phylink: support PCS provider release
This commit adds a blocking notifier chain for fwnode PCS provider removal and updates phylink to detach removed PCS instances, recalculate supported interfaces and capabilities, and trigger link resolution.
Quoted 4 lines
drivers/net/phy/phylink.c +0
-0
[ ... ]
drivers/net/phy/phylink.c static void phylink_update_capabilities(struct phylink *pl, · line 2381
Quoted 12 lines
drivers/net/phy/phylink.c +5
-0@@ -2350,6 +2371,11 @@ static void phylink_update_capabilities(struct phylink *pl, config->interface);linkmode_copy (pl->supported, supported);linkmode_copy (pl->advertising, config->advertising); + }else { +/* No SFP modes remain usable with the current interfaces. */ +linkmode_zero (pl->supported); +linkmode_zero (pl->advertising); +linkmode_zero (config->advertising); } goto exit;
[Severity: Medium]
Can this else branch ever be reached when a PCS is removed, and what happens to the non-SFP branches in phylink_update_capabilities() when validation fails?
First, phylink_update_capabilities() passes unmasked pl->sfp_interfaces to __phylink_validate_mask(), and in phylink_validate_mac_and_pcs() a missing PCS in pl->pcs_list leaves pcs as NULL without returning -EINVAL:
drivers/net/phy/phylink.c:phylink_validate_mac_and_pcs() {
...
} else if (test_bit(state->interface, pl->config->pcs_interfaces)) {
struct phylink_pcs *tmp;
list_for_each_entry(tmp, &pl->pcs_list, list) {
if (!phylink_validate_pcs_interface(tmp, state->interface)) {
pcs = tmp;
break;
}
}
}
if (pcs) {
...
}Because pcs is NULL, phylink_validate_mac_and_pcs() skips PCS validation and succeeds against the MAC capabilities alone, so __phylink_validate_mask() still returns 0 after the PCS is removed.
Second, when validation does return -EINVAL (for example, when state->interface is no longer in pl->supported_interfaces), none of the other branches in phylink_update_capabilities() clear the capability masks:
the generic fallback presets pl->supported and config->advertising with linkmode_fill(), ignores the -EINVAL return from __phylink_validate(), and leaves all linkmode bits set in pl->supported and pl->advertising; similarly, the pl->phydev, MLO_AN_FIXED, and MLO_AN_INBAND paths leave stale or unvalidated linkmodes in pl->supported and pl->advertising.
Quoted 28 lines
drivers/net/phy/phylink.c +24
-0@@ -2425,6 +2451,133 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac }EXPORT_SYMBOL_GPL (phylink_update_mac_pause_capabilities); +static void phylink_del_pcs (struct phylink *pl,struct phylink_pcs *pcs) +{ +ASSERT_RTNL (); + +lockdep_assert_held (&pl->state_mutex); + +rcu_assign_pointer (pcs->phylink,NULL ); +synchronize_rcu (); +list_del (&pcs->list); + +/* +* Check if we are removing the PCS currently +* in use by this phylink instance. If this is the case, +* tear down the link, force phylink resolve to reconfigure the +* interface mode, disable the current PCS and set the +* phylink PCS to NULL. +*/ +if (pl->pcs == pcs) { +if (pl->old_link_state) { +phylink_link_down (pl); + pl->old_link_state = false; + } +if (pl->cfg_link_an_mode ==MLO_AN_INBAND ) +timer_delete_sync (&pl->link_poll);
drivers/net/phy/phylink.c void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac · line 2487
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+ +if (pl->pcs_state !=PCS_STATE_DOWN ) +phylink_pcs_disable (pl->pcs); + +if (!pl->phylink_disable_state) + pl->force_major_config = true; + +WRITE_ONCE (pl->pcs,NULL ); + } +}
[Severity: Medium]
Does setting pl->force_major_config based on !pl->phylink_disable_state interact safely with phylink_stop() and PHYLINK_DISABLE_LINK?
If pl->phylink_disable_state is 0 when phylink_del_pcs() sets pl->force_major_config = true, and phylink_stop() runs after pcs_provider_notify() drops rtnl_lock() but before pl->resolve executes, phylink_stop() calls timer_delete_sync(&pl->link_poll) and then flushes pl->resolve via phylink_run_resolve_and_disable(). In phylink_resolve(), pl->force_major_config triggers phylink_major_config() even when pl->phylink_disable_state is non-zero, reconfiguring stopped hardware and potentially re-arming pl->link_poll via phylink_pcs_poll_start() after phylink_stop() already deleted it.
Conversely, if the active PCS is removed while the instance is started (pl->pcs_state == PCS_STATE_STARTED) but PHYLINK_DISABLE_LINK is set (such as when SFP LOS is asserted), !pl->phylink_disable_state is false, so pl->pcs is set to NULL without setting pl->force_major_config. When PHYLINK_DISABLE_LINK is later cleared on the same interface, phylink_resolve() skips phylink_major_config() and calls phylink_link_up() with pl->pcs == NULL.
Quoted 50 lines
drivers/net/phy/phylink.c +50
-0+ +static int pcs_provider_notify (struct notifier_block *self, +unsigned long val,void *data) +{ + struct phylink *pl = container_of(self, struct phylink, fwnode_pcs_nb); +struct fwnode_pcs_provider *pp = data; +struct phylink_pcs *pcs, *tmp; +struct phy_device *phydev; +bool resolve = false; + +rtnl_lock (); +mutex_lock (&pl->phydev_mutex); + phydev = pl->phydev; +if (phydev) +mutex_lock (&phydev->lock); +mutex_lock (&pl->state_mutex); + +/* phylink instance is being destroyed, ignore any event */ +if (pl->pcs_state ==PCS_STATE_DETACHING ) + goto exit; + +/* +* Loop all the PCS for phylink instance and check if +* this notification is relevant for some of them. +*/ +list_for_each_entry_safe (pcs, tmp, &pl->pcs_list, list) { +if (!fwnode_pcs_matches_provider (pp, pl->fwnode, pcs)) +continue ; + +phylink_del_pcs (pl, pcs); + resolve = true; + } + +/* Exit early if nothing has changed */ +if (!resolve) + goto exit; + +/* 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); + +/* Recalculate capabilities */ +phylink_update_capabilities (pl, &pl->link_config); + +exit: +mutex_unlock (&pl->state_mutex);
drivers/net/phy/phylink.c void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac · line 2547
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+if (phydev) +mutex_unlock (&phydev->lock); +mutex_unlock (&pl->phydev_mutex); +rtnl_unlock (); + +if (resolve) +phylink_run_resolve (pl); + +return resolve ?NOTIFY_OK :NOTIFY_DONE ; +}
[Severity: Medium]
When phylink_update_capabilities() restricts phydev->advertising or pl->link_config.advertising after a PCS is removed, what updates the hardware advertisement?
phylink_phy_update_capabilities() copies the restricted mask into phy->advertising in memory, but once the PHY state machine has moved past PHY_UP to PHY_NOLINK or PHY_RUNNING, phylib will not reprogram the PHY autonegotiation registers unless _phy_start_aneg() is called. Similarly, if an unused PCS is removed in in-band mode without setting pl->force_major_config, phylink_resolve() does not call phylink_change_inband_advert(). An attached PHY can then continue advertising and negotiating link modes that require the removed PCS, causing phylink_major_config() to fail when the link comes up.
[ ... ]
Quoted 23 lines
drivers/net/phy/phylink.c +9
-1@@ -2515,10 +2668,18 @@ struct phylink *phylink_create(struct phylink_config *config,__set_bit (PHYLINK_DISABLE_STOPPED , &pl->phylink_disable_state);timer_setup (&pl->link_poll, phylink_fixed_poll,0 ); +/* First register notifier for hotplug PCS events */ +if (!phy_interface_empty (config->pcs_interfaces)) { + pl->fwnode_pcs_nb.notifier_call = pcs_provider_notify; +register_fwnode_pcs_notifier (&pl->fwnode_pcs_nb); + } +/* Fill the PCS list with available PCS from phylink config */ ret =phylink_fill_available_pcs (pl, config);if (ret <0 ) - goto free_pl; + goto unregister_pcs_notify; + +mutex_lock (&pl->state_mutex);/* Link available PCS to phylink */ list_for_each_entry (pcs, &pl->pcs_list, list)@@ -2533,13 +2694,13 @@ struct phylink *phylink_create(struct phylink_config *config, pl->supported_interfaces, pcs->supported_interfaces);
drivers/net/phy/phylink.c struct phylink *phylink_create(struct phylink_config *config, · line 2703
Quoted 10 lines
drivers/net/phy/phylink.c +2
-2-mutex_lock (&pl->state_mutex);phylink_update_capabilities (pl, &pl->link_config); +mutex_unlock (&pl->state_mutex); ret =phylink_parse_mode (pl, fwnode);if (ret <0 ) - goto unlink_pcs_list; + goto unregister_pcs_notify;
[Severity: Medium]
Can pcs_provider_notify() race with phylink_create() before pl initialization is finished?
phylink_create() is called without rtnl_lock() held and registers pl->fwnode_pcs_nb before pl->pcs_list, pl->supported_interfaces, pl->cfg_link_an_mode, and pl->link_config are initialized, dropping pl->state_mutex between steps:
CPU0 CPU1phylink_create()
register_fwnode_pcs_notifier()
phylink_fill_available_pcs()
mutex_lock(&pl->state_mutex)
...
mutex_unlock(&pl->state_mutex)
mutex_lock(&pl->state_mutex)
...
mutex_unlock(&pl->state_mutex)
phylink_parse_mode()
pl->cfg_link_an_mode = ...
fwnode_pcs_del_provider()
pcs_provider_notify()
mutex_lock(&pl->state_mutex)
phylink_update_capabilities() phylink_parse_fixedlink()
pl->link_config.speed = ...
__set_bit(..., lp_advertising) <-- concurrent access to link_config
If pcs_provider_notify() runs after phylink_fill_available_pcs() unlocks pl->state_mutex, it can also observe entries in pl->pcs_list before their pcs->phylink pointer or pl->supported_interfaces is initialized. Registering the notifier after pl is initialized (or keeping initialization under pl->state_mutex before publishing the notifier) would avoid these races.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
PATCH v22 9/15 net: phylink: support late PCS provider attach
In reply to Christian MarangiAdd support for late PCS provider attachment to a phylink instance. Similar to FWNODE_PCS_PROVIDER_DEL, FWNODE_PCS_PROVIDER_ADD is added to address the case where a PCS provider is added after a phylink instance is created and started.
The PCS notifier will emit the event FWNODE_PCS_PROVIDER_ADD every time a new PCS provider is added.
If a related PCS is found, then such PCS is added to the phylink instance PCS list.
Such PCS is then linked to the phylink instance and the supported interfaces of the phylink instance get refreshed.
If we are in a major_config_failed scenario an interface reconfiguration is triggered in the next phylink resolve with a new phylink resolve right after with the new AN mode reevaluated if a new PCS is selected.
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 | 22 ++++++++ drivers/net/phy/phylink.c | 110 ++++++++++++++++++++++++++++++-------- include/linux/pcs/pcs.h | 31 +++++++++++ 3 files changed, 141 insertions(+), 22 deletions(-)
drivers/net/pcs/pcs.c +22 -0
@@ -69,6 +69,10 @@ fwnode_pcs_add_provider(struct fwnode_handle *fwnode,mutex_unlock (&fwnode_pcs_mutex); +blocking_notifier_call_chain (&fwnode_pcs_notify_list, +FWNODE_PCS_PROVIDER_ADD , + pp); +pr_debug ("Added pcs provider from %pfwf\n" , fwnode);return pp;
@@ -201,6 +205,24 @@ struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnode, unsigned }EXPORT_SYMBOL_GPL (fwnode_pcs_get); +struct phylink_pcs *fwnode_pcs_get_from_provider (struct fwnode_pcs_provider *provider, +const struct fwnode_handle *fwnode, +int index) +{ +struct fwnode_reference_args pcsspec; +struct phylink_pcs *pcs; +int ret; + + ret =fwnode_parse_pcsspec (fwnode, index,NULL , &pcsspec); +if (ret) +return ERR_PTR (ret); + + pcs =__fwnode_pcs_get_from_pcsspec_provider (&pcsspec, provider); +fwnode_handle_put (pcsspec.fwnode); +return pcs; +} +EXPORT_SYMBOL_GPL (fwnode_pcs_get_from_provider); +bool fwnode_pcs_matches_provider (struct fwnode_pcs_provider *provider,const struct fwnode_handle *fwnode,struct phylink_pcs *pl_pcs)
drivers/net/phy/phylink.c +88 -22
180 lines not on this page · raw message
include/linux/pcs/pcs.h +31 -0
@@ -5,6 +5,7 @@#include <linux/phylink.h> enum fwnode_pcs_notify_event { +FWNODE_PCS_PROVIDER_ADD ,FWNODE_PCS_PROVIDER_DEL , };
@@ -50,6 +51,28 @@ int unregister_fwnode_pcs_notifier(struct notifier_block *nb);struct phylink_pcs *fwnode_pcs_get (const struct fwnode_handle *fwnode,unsigned int index); +/** +* fwnode_pcs_get_from_provider() - Retrieve a PCS from a specific provider +* @provider: PCS provider to use +* @fwnode: firmware node +* @index: index fwnode PCS handle in firmware node +* +* Get a PCS from the firmware node at index specifically provided by +* passed PCS provider. +* +* Unlike fwnode_pcs_get(), this function does not search the global list of +* PCS providers. The caller must provide the provider responsible for the +* referenced PCS. +* +* Returns: a pointer to the phylink_pcs, NULL or a negative error pointer. Can +* return -EINVAL if the PCS provider firmware node doesn't match the referenced +* firmware node or a negative error pointer (or NULL) from the PCS provider +* xlate function for the relevant firmware node and PCS handle index. +*/ +struct phylink_pcs *fwnode_pcs_get_from_provider (struct fwnode_pcs_provider *provider, +const struct fwnode_handle *fwnode, +int index); +/** * fwnode_pcs_matches_provider() - Check whether a PCS belongs to a provider * @provider: PCS provider to check
@@ -120,6 +143,14 @@ static inline struct phylink_pcs *fwnode_pcs_get(const struct fwnode_handle *fwnreturn ERR_PTR (-ENOENT ); } +static inline struct phylink_pcs * +fwnode_pcs_get_from_provider (struct fwnode_pcs_provider *provider, +const struct fwnode_handle *fwnode, +int index) +{ +return ERR_PTR (-ENOENT ); +} +static inline bool fwnode_pcs_matches_provider (struct fwnode_pcs_provider *provider,const struct fwnode_handle *fwnode,struct phylink_pcs *pl_pcs)
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH net-next v22 09/15] net: phylink: support late PCS provider attach
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] net: phylink: pcs_provider_notify() misses PCS reconfig on late attach - [High] net: phylink…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] net: phylink: pcs_provider_notify() misses PCS reconfig on late attach
- [High] net: phylink: unreachable SFP revalidation in phylink_update_capabilities()
- [High] net: phylink: broken supported_interfaces refresh in pcs_provider_notify()
- [Medium] net: phylink: phylink_resolve() passes wrong state to phylink_major_config()
- [Medium] net: phylink: missing SFP interface selection in phylink_update_capabilities()
- [Medium] net: phylink: missing PHY aneg reconfig in pcs_provider_notify()
- [Low] net: phylink: unlocked mode and fixed-link parse in phylink_create()
--
commit df5385a2562b3f337c319241b34f5895e1000a4c
Author: Christian Marangi <[email protected]>
net: phylink: support late PCS provider attach
This commit adds FWNODE_PCS_PROVIDER_ADD notifier handling and SFP interface tracking so phylink can attach PCS providers registered after phylink_create() and revalidate capabilities.
Quoted 4 lines
drivers/net/phy/phylink.c +0
-0
[ ... ]
drivers/net/phy/phylink.c static void phylink_update_capabilities(struct phylink *pl, · line 2391
Quoted 12 lines
drivers/net/phy/phylink.c +5
-2@@ -2358,11 +2383,24 @@ static void phylink_update_capabilities(struct phylink *pl, goto exit; } -/* Revalidate the SFP module modes against every available interface. */ +/* Revalidate SFP modes against interfaces currently supported by the +* MAC and PCS. Keep the module's interface list intact so a later PCS +* addition can make an interface usable again. +*/ if (pl->sfp_bus && !pl->phydev && config->interface && - !phy_interface_empty (pl->sfp_interfaces)) { + !phy_interface_empty (pl->sfp_module_interfaces)) {
[Severity: High]
Is this SFP revalidation block reachable on standard SFP ports configured with managed = "in-band-status" or default_an_inband?
Just above this hunk, phylink_update_capabilities() checks pl->cfg_link_an_mode == MLO_AN_INBAND and jumps to exit:
drivers/net/phy/phylink.c:phylink_update_capabilities() {
...
if (pl->cfg_link_an_mode == MLO_AN_INBAND) {
phylink_parse_inband_capabilities(pl, config);
goto exit;
}
...
}When an SFP module is inserted on an in-band port, pl->cfg_link_an_mode remains MLO_AN_INBAND. As a result, pcs_provider_notify() runs phylink_parse_inband_capabilities() instead of this SFP block, leaving pl->sfp_interfaces stale and overwriting pl->supported and pl->advertising with generic in-band MII link modes instead of filtering against pl->sfp_module_interfaces and pl->sfp_support.
Quoted 5 lines
drivers/net/phy/phylink.c +3
-0__ETHTOOL_DECLARE_LINK_MODE_MASK (supported); +phy_interface_and (pl->sfp_interfaces, + pl->sfp_module_interfaces, + pl->supported_interfaces);
drivers/net/phy/phylink.c static void phylink_update_capabilities(struct phylink *pl, · line 2406
Quoted 10 lines
drivers/net/phy/phylink.c +7
-0+if (phy_interface_empty (pl->sfp_interfaces)) { +linkmode_zero (pl->supported); +linkmode_zero (pl->advertising); +linkmode_zero (config->advertising); + goto exit; + } +linkmode_copy (supported, pl->sfp_support);linkmode_copy (config->advertising, pl->sfp_support); if (!__phylink_validate_mask(pl, NULL, supported, config,
[Severity: Medium]
When pl->sfp_interfaces changes here, does phylink_update_capabilities() also need to re-select config->interface via phylink_choose_sfp_interface() and restrict config->advertising to that single interface with __phylink_validate()?
Without re-selecting config->interface and validating against it (as phylink_sfp_config_optical() does), __phylink_validate_mask() leaves link modes from all interfaces in pl->sfp_interfaces advertised in pl->advertising, and if a PCS removal drops the active config->interface while another interface in pl->sfp_interfaces remains usable, config->interface stays set to the removed mode.
In addition, if an optical SFP module is inserted before its PCS provider probes and pl->supported_interfaces has no overlap with pl->sfp_module_interfaces yet, phylink_sfp_config_optical() returns -EINVAL before setting pl->link_config.interface (putting the SFP state machine into SFP_MOD_ERROR with config->interface == PHY_INTERFACE_MODE_NA), so this config->interface check will skip the SFP module when the PCS provider later attaches.
Quoted 32 lines
drivers/net/phy/phylink.c +14
-7@@ -2494,6 +2532,7 @@ static int pcs_provider_notify(struct notifier_block *self,struct phylink_pcs *pcs, *tmp;struct phy_device *phydev;bool resolve = false; +int count, i;rtnl_lock ();mutex_lock (&pl->phydev_mutex);@@ -2506,16 +2545,44 @@ static int pcs_provider_notify(struct notifier_block *self,if (pl->pcs_state ==PCS_STATE_DETACHING ) goto exit; -/* -* Loop all the PCS for phylink instance and check if -* this notification is relevant for some of them. -*/ -list_for_each_entry_safe (pcs, tmp, &pl->pcs_list, list) { -if (!fwnode_pcs_matches_provider (pp, pl->fwnode, pcs)) -continue ; +switch (val) { +case FWNODE_PCS_PROVIDER_ADD : + count =fwnode_phylink_pcs_count (pl->fwnode); +for (i =0 ; i < count; i++) { + pcs =fwnode_pcs_get_from_provider (pp, pl->fwnode, i); +if (IS_ERR_OR_NULL (pcs)) +continue ; + +/* Trigger a resolve only if a PCS is actually added */ +if (phylink_add_pcs (pl, pcs)) + resolve = true; + } +
drivers/net/phy/phylink.c static int pcs_provider_notify(struct notifier_block *self, · line 2570
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+/* +* Force an interface reconfig if major config failed. +* Configure link_failed to retrigger a resolve a +* second time to reevaluate AN mode with the new +* PCS if selected. +*/ +if (resolve && pl->major_config_failed) { + pl->force_major_config = true; + pl->link_failed = true; + }
[Severity: High]
Can gating pl->force_major_config and pl->link_failed solely on pl->major_config_failed miss reattaching the PCS after a provider removal and re-addition?
When an active PCS is removed, phylink_del_pcs() sets pl->pcs = NULL, but pl->major_config_failed is only set later when the asynchronous pl->resolve work item runs phylink_major_config(). If pl->phylink_disable_state is non-zero when FWNODE_PCS_PROVIDER_DEL fires (for example, PHYLINK_DISABLE_LINK on an SFP cage with no signal), phylink_del_pcs() does not set pl->force_major_config and phylink_run_resolve() does not run pl->resolve, so pl->major_config_failed remains false; when the PCS provider is re-added and optical signal later returns, phylink_resolve() never runs phylink_major_config() and pl->pcs stays NULL. Likewise, if FWNODE_PCS_PROVIDER_ADD runs before the queued pl->resolve work from FWNODE_PCS_PROVIDER_DEL has executed, pl->major_config_failed is still false, so pl->link_failed is not set and phylink_resolve() does not retrigger a second pass after attaching pl->pcs in phylink_major_config().
[Severity: Medium]
When pl->major_config_failed is true in MLO_AN_PHY or MLO_AN_FIXED mode, does setting both pl->force_major_config = true and pl->link_failed = true cause phylink_resolve() to run phylink_major_config() with the wrong link_state?
In phylink_resolve(), the pl->link_failed branch runs before the MLO_AN_FIXED and MLO_AN_PHY branches and copies link_state from pl->link_config:
drivers/net/phy/phylink.c:phylink_resolve() {
...
} else if (pl->link_failed) {
link_state = pl->link_config;
link_state.link = false;
retrigger = true;
} else if (pl->act_link_an_mode == MLO_AN_FIXED) {
phylink_get_fixed_state(pl, &link_state);
mac_config = link_state.link;
} else if (pl->act_link_an_mode == MLO_AN_PHY) {
link_state = pl->phy_state;
mac_config = link_state.link;
}
...
if ((mac_config && link_state.interface != pl->link_config.interface) ||
pl->force_major_config) {
...
phylink_major_config(pl, false, &link_state);
pl->link_config.interface = link_state.interface;
pl->force_major_config = false;
}
...
}In MLO_AN_PHY mode, the PHY's rate_matching, pause, and current interface live in pl->phy_state, whereas pl->link_config has rate_matching = RATE_MATCH_NONE. Because the first resolve pass consumes pl->force_major_config while link_state holds pl->link_config, the retriggered second pass will not re-run phylink_major_config() if the interface matches or the PHY link is still down.
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+ +break ; +case FWNODE_PCS_PROVIDER_DEL : +/* +* Loop all the PCS for phylink instance and check if +* this notification is relevant for some of them. +*/ +list_for_each_entry_safe (pcs, tmp, &pl->pcs_list, list) { +if (!fwnode_pcs_matches_provider (pp, pl->fwnode, pcs)) +continue ;
drivers/net/phy/phylink.c static int pcs_provider_notify(struct notifier_block *self, · line 2588
Quoted 10 lines
drivers/net/phy/phylink.c +4
-2-phylink_del_pcs (pl, pcs); - resolve = true; +phylink_del_pcs (pl, pcs); + resolve = true; + } +break ; }/* Exit early if nothing has changed */
[Severity: High]
right below this hunk, pcs_provider_notify() refreshes pl->supported_interfaces and revalidates capabilities:
drivers/net/phy/phylink.c:pcs_provider_notify() {
...
/* 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);
/* Recalculate capabilities */
phylink_update_capabilities(pl, &pl->link_config);
...
}How is pl->supported_interfaces expected to change when a PCS is added or removed if pl->config->supported_interfaces is copied unconditionally without stripping unavailable pl->config->pcs_interfaces?
When a MAC driver includes PCS-backed modes in config->supported_interfaces (which phylink_create() requires to be non-empty even when all MAC modes need a PCS, as in airoha_setup_phylink() and Documentation/networking/pcs.rst), pl->supported_interfaces retains every mode in config->supported_interfaces even when pl->pcs_list is empty, and phylink_validate_mac_and_pcs() does not fail when no matching PCS is present in pl->pcs_list. Conversely, if a MAC driver omits config->pcs_interfaces from config->supported_interfaces until a PCS attaches, phylink_parse_mode() in phylink_create() and phylink_bringup_phy() fail validation with -EINVAL whenever the PCS provider probes late.
[Severity: Medium]
Also in pcs_provider_notify(), when pl->phydev is attached, phylink_update_capabilities() -> phylink_phy_update_capabilities() updates phy->advertising in memory, but nothing calls _phy_start_aneg() or phy_config_aneg(). Won't the PHY hardware keep advertising the stale link modes after a PCS provider is added or removed?
drivers/net/phy/phylink.c struct phylink *phylink_create(struct phylink_config *config, · line 2752
Quoted 10 lines
drivers/net/phy/phylink.c +0
-4@@ -2681,10 +2748,6 @@ struct phylink *phylink_create(struct phylink_config *config,mutex_lock (&pl->state_mutex); -/* Link available PCS to phylink */ -list_for_each_entry (pcs, &pl->pcs_list, list) -rcu_assign_pointer (pcs->phylink, pl); -phy_interface_copy (pl->supported_interfaces, pl->config->supported_interfaces);
[Severity: Low]
In phylink_create(), register_fwnode_pcs_notifier() is called before pl->state_mutex is released and before phylink_parse_mode() and phylink_parse_fixedlink() run unlocked:
drivers/net/phy/phylink.c:phylink_create() {
...
phylink_update_capabilities(pl, &pl->link_config);
mutex_unlock(&pl->state_mutex);
ret = phylink_parse_mode(pl, fwnode);
if (ret < 0)
goto unregister_pcs_notify;
if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
ret = phylink_parse_fixedlink(pl, fwnode);
...
}With FWNODE_PCS_PROVIDER_ADD now firing pcs_provider_notify() when a PCS provider registers on another CPU, can pcs_provider_notify() race with phylink_parse_mode() and phylink_parse_fixedlink()?
CPU0 CPU1phylink_create()
register_fwnode_pcs_notifier()
...
phylink_parse_mode()
pl->cfg_link_an_mode = MLO_AN_FIXED;
phylink_parse_fixedlink()
pl->link_config.duplex = DUPLEX_HALF;
fwnode_pcs_add_provider()
pcs_provider_notify()
mutex_lock(&pl->state_mutex);
phylink_update_capabilities()
phylink_parse_fixedlink_capabilities()
/* reads DUPLEX_HALF and
* modifies lp_advertising */
if (full-duplex)
pl->link_config.duplex = DUPLEX_FULL;
__set_bit(..., lp_advertising); <-- data raceHolding pl->state_mutex across phylink_parse_mode() and phylink_parse_fixedlink(), or registering the notifier after initial parsing completes, would avoid this race.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v22 10/15 net: Document PCS subsystem
In reply to Christian MarangiAdd extensive documentation of the new PCS subsystem and the fwnode implementation with producer/consumer API.
Also update the sfp-phylink migration guide.
--- Documentation/networking/index.rst | 1 + Documentation/networking/pcs.rst | 231 +++++++++++++++++++++++ Documentation/networking/sfp-phylink.rst | 35 ++-- 3 files changed, 243 insertions(+), 24 deletions(-) create mode 100644 Documentation/networking/pcs.rst
Documentation/networking/index.rst +1 -0
@@ -28,6 +28,7 @@ Contents: net_failover page_pool phy + pcs sfp-phylink alias bridge
Documentation/networking/pcs.rst new file +231 -0
231 lines not on this page · raw message
Documentation/networking/sfp-phylink.rst +11 -24
@@ -311,33 +311,20 @@ this documentation. priv->pcs = lynx_pcs_create_mdiodev(bus, 0); - Some PCS can be recovered based on firmware information: + PCS that should be recovered based on firmware information should implement + the external PCS as a :ref:`PCS provider <pcs_producer>` and reference it + with the use of PCS fwnode helper :ref:`PCS fwnode helper <pcs_fwnode>`. - .. code-block:: c - - priv->pcs = lynx_pcs_create_fwnode(of_fwnode_handle(node)); - -12. Populate the :c:func:`mac_select_pcs` callback and add it to your - :c:type:`struct phylink_mac_ops <phylink_mac_ops>` set of ops. This function - must return a pointer to the relevant :c:type:`struct phylink_pcs <phylink_pcs>` - that will be used for the requested link configuration: - - .. code-block:: c - - static struct phylink_pcs *foo_select_pcs(struct phylink_config *config, - phy_interface_t interface) - { - struct foo_priv *priv = container_of(config, struct foo_priv, - phylink_config); - - if ( /* 'interface' needs a PCS to function */ ) - return priv->pcs; +12. Populate the :c:var:`num_possible_pcs` value and + :c:func:`fill_available_pcs` callback and add it to your + :c:type:`struct phylink_config <phylink_config>`. - return NULL; - } + Refer to :ref:`Documentation/networking/pcs.rst <pcs_consumer>` for + further details and examples. - See :c:func:`mvpp2_select_pcs` for an example of a driver that has multiple - internal PCS. + Notice that the previous implementation of populating + :c:func:`mac_select_pcs` is considered deprecated in favor of the new + described above. 13. Fill-in all the :c:type:`phy_interface_t <phy_interface_t>` (i.e. all MAC to PHY link modes) that your MAC can output. The following example shows a
Signature · 1 line
2.55.0
PATCH v22 11/15 MAINTAINERS: add myself as PCS subsystem maintainer
In reply to Christian MarangiList all the files of the Ethernet PCS subsystem and add myself as maintainer.
--- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
@@ -10034,6 +10034,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.55.0
PATCH v22 12/15 net: phylink: add .pcs_link_down PCS OP
In reply to Christian MarangiPermit 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 | 12 ++++++++++++ 2 files changed, 21 insertions(+)
drivers/net/phy/phylink.c +9 -0
@@ -1142,6 +1142,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)
@@ -1854,6 +1860,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 +12 -0
@@ -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);
@@ -690,6 +692,16 @@ void pcs_an_restart(struct phylink_pcs *pcs);void pcs_link_up (struct phylink_pcs *pcs,unsigned int neg_mode,phy_interface_t interface,int speed,int duplex); +/** +* pcs_link_down() - tear down link between MAC and PCS +* @pcs: a pointer to a &struct phylink_pcs. +* +* This call will be made just after mac_link_down() to inform the PCS the +* link has gone down. PCS should be configured to stop processing packets +* for transmission and reception. +*/ +void pcs_link_down (struct phylink_pcs *pcs); +/** * pcs_disable_eee() - Disable EEE at the PCS * @pcs: a pointer to a &struct phylink_pcs
Signature · 1 line
2.55.0
PATCH v22 13/15 dt-bindings: net: pcs: Document support for Airoha Ethernet PCS
In reply to Christian MarangiDocument 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
@@ -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.55.0
[email protected]
AI reviewRe: [PATCH net-next v22 13/15] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] dt-bindings: net: pcs: airoha,scu conflicts with SCU generic PHY binding - [Low] dt-bindings: net…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] dt-bindings: net: pcs: airoha,scu conflicts with SCU generic PHY binding
- [Low] dt-bindings: net: pcs: unwieldy per-compatible conditionals in airoha,pcs.yaml
--
commit a852142e10dc6efd0e13f0e36a05eb4c25e78d50
Author: Christian Marangi <[email protected]>
dt-bindings: net: pcs: Document support for Airoha Ethernet PCS
This commit adds the Device Tree binding schema for the Airoha AN7581 Ethernet, PON, PCIe, and USB PCS and SerDes blocks.
Quoted 5 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +0
-0
[ ... ]
Quoted 30 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +30
-0+properties: + compatible: + enum: + - airoha,an7581-pcs-eth + - airoha,an7581-pcs-pon + - airoha,an7581-pcs-pcie + - airoha,an7581-pcs-usb + + reg: + minItems: 6 + maxItems: 15 + + 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
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml line 56
Quoted 10 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +10
-0+ - "#pcs-cells" + +allOf: + - if: + properties: + compatible: + contains: + enum: + - airoha,an7581-pcs-eth + - airoha,an7581-pcs-pon
[Severity: Low]
Should this binding be split into separate schema documents instead of combining all four compatibles here?
Because the eth/pon, pcie, and usb variants do not share a common register layout (requiring 8, 15, or 6 reg entries respectively), #pcs-cells count, or required properties (airoha,scu vs phys), the top-level properties block only sets loose bounds while three large per-compatible if-then blocks redefine almost every property in the file.
[ ... ]
Quoted 6 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +6
-0+examples: + - | + #include <dt-bindings/phy/phy.h> + + pcs@1fa08000 { + compatible = "airoha,an7581-pcs-pon";
[ ... ]
Quoted 3 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +3
-0+ airoha,scu = <&scuclk>; + #pcs-cells = <0>; + };
[ ... ]
Quoted 3 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +3
-0+ pcs@1fa04000 { + compatible = "airoha,an7581-pcs-pcie"; + reg = <0x1fa04000 0x1000>,
[ ... ]
Quoted 4 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +4
-0+ <0x1fa5a000 0x1000>, + <0x1fa5b000 0x1000>, + <0x1fa5c000 0x1000>; + reg-names = "pcs_mac0", "hsgmii_an0", "hsgmii_pcs0",
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml line 245
Quoted 10 lines
Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml new file +10
-0+ "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>; + };
[Severity: Medium]
Does the airoha,scu property duplicate and conflict with the existing generic PHY provider on the AN7581 SCU node?
In Documentation/devicetree/bindings/clock/airoha,en7523-scu.yaml and drivers/clk/clk-en7523.c (en7581_serdes_phy_set_mode()), the scuclk node (airoha,en7581-scu) is not a syscon and already exposes SerDes interface mode selection via #phy-cells = <1>. Requiring airoha,scu = <&scuclk> while setting phys: false on the eth, pon, and pcie variants bypasses the existing SCU PHY provider and causes syscon_regmap_lookup_by_phandle() on airoha,scu to fail with -EINVAL when given &scuclk. If Chip SCU registers are needed instead, existing Airoha bindings such as Documentation/devicetree/bindings/thermal/airoha,en7581-thermal.yaml use airoha,chip-scu pointing to &chip_scu.
In addition, pcs_ana (0x1fa5a000), pcs_pma0 (0x1fa5b000), and pcs_pma1 (0x1fa5c000) in the airoha,an7581-pcs-pcie example claim the same MMIO ranges as csr-2l, pma0, and pma1 of phy@1fa5a000 (airoha,en7581-pcie-phy) in arch/arm64/boot/dts/airoha/en7581.dtsi.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
PATCH v22 14/15 net: pcs: airoha: add PCS driver for Airoha AN7581 SoC
In reply to Christian MarangiAdd 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 | 17 + drivers/net/pcs/airoha/Makefile | 7 + drivers/net/pcs/airoha/pcs-airoha-common.c | 1309 ++++++++++++ drivers/net/pcs/airoha/pcs-airoha.h | 1311 ++++++++++++ drivers/net/pcs/airoha/pcs-an7581.c | 2100 ++++++++++++++++++++ 7 files changed, 4748 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
@@ -64,4 +64,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
@@ -10,3 +10,5 @@ obj-$(CONFIG_PCS_XPCS_ROCKCHIP) += pcs-xpcs-rk.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 +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config PCS_AIROHA + tristate + select PHYLINK + select FWNODE_PCS + select MFD_SYSCON + select REGMAP_MMIO + select GENERIC_PHY + +config PCS_AIROHA_AN7581 + tristate "Airoha AN7581 PCS driver" + depends on ARCH_AIROHA || COMPILE_TEST + 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
@@ -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 +1309 -0
1,309 lines not on this page · raw message
drivers/net/pcs/airoha/pcs-airoha.h new file +1311 -0
1,311 lines not on this page · raw message
drivers/net/pcs/airoha/pcs-an7581.c new file +2100 -0
345 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.55.0
Part of the diff is not shown here. View the raw message.
[email protected]
AI reviewRe: [PATCH net-next v22 14/15] net: pcs: airoha: add PCS driver for Airoha AN7581 SoC
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] net: pcs: airoha: broken syscon lookup and RMW race on airoha,scu - [High] net: pcs: airoha: missing…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] net: pcs: airoha: broken syscon lookup and RMW race on airoha,scu
- [High] net: pcs: airoha: missing 1000BASE-X setup in airoha_pcs_config() and link_up()
- [High] net: pcs: airoha: invalid in-band AN capability reported for 2500BASE-X
- [High] net: pcs: airoha: unsynchronized shared PLLs and resets on dual-port PCIe PCS
- [Medium] net: pcs: airoha: stale forced register bits across interface mode changes
- [Medium] net: pcs: airoha: link left up in airoha_pcs_get_state_10gbaser() default case
- [Medium] net: pcs: airoha: missing rxlock_workaround() call in airoha_pcs_get_state_10gbaser()
--
commit 6861815bdffccd11682b61e1e2c5599025467a68
Author: Christian Marangi <[email protected]>
net: pcs: airoha: add PCS driver for Airoha AN7581 SoC
This commit adds a phylink PCS provider driver for the Ethernet, PON, PCIe, and USB SerDes controllers on the Airoha AN7581 SoC.
Quoted 5 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +0
-0
[ ... ]
Quoted 14 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +14
-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 =0 , lpa =0 ; + +/* 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 392
Quoted 7 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +7
-0+static void airoha_pcs_get_state_10gbaser (struct airoha_pcs_priv *priv,int index, +struct phylink_link_state *state) +{ +struct airoha_pcs_maps *maps = &priv->maps[index]; +u32 status =0 , curr_mode =0 ; + +/* Toggle AN Status */
[Severity: Medium]
Should airoha_pcs_get_state_10gbaser() also call data->rxlock_workaround() before reading the link status, just like airoha_pcs_get_state_usxgmii() does?
10GBASE-R uses the same PMA, CDR, and usxgmii_pcs hardware blocks as USXGMII, and an7581_pcs_rxlock_workaround() checks AIROHA_PCS_USXGMII_PCS_STUS_1 against the PMA RX SigDet module to reset the CDR when RX fails to lock. Without that call here, a 10GBASE-R link that hits this hardware condition will never trigger the CDR reset recovery and will stay down.
Quoted 15 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +15
-0+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_BASE_R_10GB_T_PCS_STUS_1 , + &status); +regmap_read (maps->usxgmii_pcs,AIROHA_PCS_USXGMII_PCS_AN_STATS_0 , &curr_mode); + + state->link = !!(status &AIROHA_PCS_USXGMII_RX_LINK_STUS ); + +switch (curr_mode &AIROHA_PCS_USXGMII_CUR_USXGMII_MODE ) { +case AIROHA_PCS_USXGMII_CUR_USXGMII_MODE_10G : + state->speed =SPEED_10000 ; +break ;
drivers/net/pcs/airoha/pcs-airoha-common.c line 417
Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10
-0+case AIROHA_PCS_USXGMII_CUR_USXGMII_MODE_5G : + state->speed =SPEED_5000 ; +break ; +case AIROHA_PCS_USXGMII_CUR_USXGMII_MODE_2_5G : + state->speed =SPEED_2500 ; +break ; +default : + state->speed =SPEED_UNKNOWN ; +return ; + }
[Severity: Medium]
Does returning in the default case leave state->link set to true when AIROHA_PCS_USXGMII_RX_LINK_STUS is set?
When phylink_resolve() sees state->link true with SPEED_UNKNOWN, it calls phylink_link_up(), where airoha_pcs_link_up() hits its own default case on SPEED_UNKNOWN and returns early before clearing AIROHA_PCS_XFI_RXMPI_STOP | AIROHA_PCS_XFI_RXMBI_STOP | AIROHA_PCS_XFI_TXMPI_STOP | AIROHA_PCS_XFI_TXMBI_STOP in AIROHA_PCS_XFI_MAC_XFI_GIB_CFG. phylink then reports the carrier as up while the PCS datapath remains stopped.
[ ... ]
Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10
-0+/* Setup SGMII AN and advertisement in DEV_ABILITY */ +if (interface ==PHY_INTERFACE_MODE_SGMII ) { +if (neg_mode ==PHYLINK_PCS_NEG_INBAND_ENABLED ) { +int advertise =phylink_mii_c22_pcs_encode_advertisement (interface, + advertising); +if (advertise <0 ) +return advertise; + + regmap_update_bits(maps->hsgmii_an, AIROHA_PCS_HSGMII_AN_SGMII_REG_AN_4, + AIROHA_PCS_HSGMII_AN_SGMII_DEV_ABILITY,
drivers/net/pcs/airoha/pcs-airoha-common.c line 581
Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10
-0+FIELD_PREP (AIROHA_PCS_HSGMII_AN_SGMII_DEV_ABILITY , + advertise)); + +regmap_set_bits (maps->hsgmii_an,AIROHA_PCS_HSGMII_AN_SGMII_REG_AN_0 , +AIROHA_PCS_HSGMII_AN_SGMII_RA_ENABLE ); + }else { +regmap_clear_bits (maps->hsgmii_an,AIROHA_PCS_HSGMII_AN_SGMII_REG_AN_0 , +AIROHA_PCS_HSGMII_AN_SGMII_RA_ENABLE ); + } + }
[Severity: High]
Should this block also run for PHY_INTERFACE_MODE_1000BASEX?
phylink_mii_c22_pcs_encode_advertisement() encodes the Clause 37 1000BASE-X full-duplex and pause advertisement bits, but because this check only matches PHY_INTERFACE_MODE_SGMII, 1000BASE-X never programs AIROHA_PCS_HSGMII_AN_SGMII_REG_AN_4 or toggles AIROHA_PCS_HSGMII_AN_SGMII_RA_ENABLE in AIROHA_PCS_HSGMII_AN_SGMII_REG_AN_0.
In addition, airoha_pcs_config() clears AIROHA_PCS_LINK_MODE_P0 and AIROHA_PCS_FORCE_SPD_MODE_P0 in AIROHA_PCS_MULTI_SGMII_SGMII_STS_CTRL_0 for SGMII, 1000BASE-X, and 2500BASE-X, but the non-inband branch of airoha_pcs_link_up() only sets them back for PHY_INTERFACE_MODE_SGMII and PHY_INTERFACE_MODE_2500BASEX, leaving forced 1000BASE-X without its link speed configured in multi_sgmii.
[ ... ]
drivers/net/pcs/airoha/pcs-airoha-common.c line 930
Quoted 5 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +5
-0+static unsigned int airoha_pcs_inband_caps (struct phylink_pcs *pcs, +phy_interface_t interface) +{ +return LINK_INBAND_ENABLE |LINK_INBAND_DISABLE ; +}
[Severity: High]
Does advertising LINK_INBAND_ENABLE for PHY_INTERFACE_MODE_2500BASEX break 2500BASE-X link bring-up when in-band negotiation is requested?
When phylink_pcs_neg_mode() sees LINK_INBAND_ENABLE for 2500BASE-X, it can select PHYLINK_PCS_NEG_INBAND_ENABLED. However, airoha_pcs_config() unconditionally clears AIROHA_PCS_HSGMII_AN_SGMII_RA_ENABLE for PHY_INTERFACE_MODE_2500BASEX, and airoha_pcs_link_up() only handles PHY_INTERFACE_MODE_SGMII when neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED, skipping the else branch that programs AIROHA_PCS_LINK_MODE_P0_2_5G | AIROHA_PCS_FORCE_SPD_MODE_P0 in AIROHA_PCS_MULTI_SGMII_SGMII_STS_CTRL_0. Returning LINK_INBAND_DISABLE for PHY_INTERFACE_MODE_2500BASEX (and PHY_INTERFACE_MODE_10GBASER) would match what the hardware configuration supports.
[ ... ]
drivers/net/pcs/airoha/pcs-airoha-common.c line 1196
Quoted 6 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +6
-0+/* SCU is used to toggle XFI or HSGMII in global SoC registers */ +if (!priv->phy) { + priv->scu =syscon_regmap_lookup_by_phandle (dev->of_node,"airoha,scu" ); +if (IS_ERR (priv->scu)) +return PTR_ERR (priv->scu); + }
[Severity: High]
Does syscon_regmap_lookup_by_phandle(dev->of_node, "airoha,scu") always return -EPROBE_DEFER on AN7581?
The "airoha,scu" phandle points to the clock-controller@1fb00000 node, whose compatible is "airoha,en7581-scu" without "syscon", and en7581_clk_hw_init() in drivers/clk/clk-en7523.c maps that region with devm_platform_ioremap_resource() without registering a syscon regmap. syscon_regmap_lookup_by_phandle() therefore never finds a registered syscon and permanently defers probe for the Ethernet, PON, and PCIe PCS devices.
Furthermore, en7581_serdes_phy_set_mode() in drivers/clk/clk-en7523.c already modifies the same SCU registers (REG_NP_SCU_SSR3 at 0x94 and REG_NP_SCU_SSTR at 0x9c) using raw readl()/writel() under en_clk_priv::lock, which would race with regmap_update_bits(priv->scu, ...) in airoha_pcs_setup_scu_eth(), airoha_pcs_setup_scu_pon(), and airoha_pcs_setup_scu_pcie().
Quoted 5 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +0
-0
[ ... ]
drivers/net/pcs/airoha/pcs-an7581.c line 1137
Quoted 5 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +5
-0+/* Set RX rate ctrl */ +if (interface ==PHY_INTERFACE_MODE_2500BASEX ) +regmap_update_bits (pcs_pma,AIROHA_PCS_PMA_RX_FLL_2 , +AIROHA_PCS_PMA_CK_RATE , +AIROHA_PCS_PMA_CK_RATE_10 );
[Severity: Medium]
If a port switches interface modes at runtime, do several mode-specific register fields retain stale values from the previous mode?
Here in an7581_pcs_rx_bringup(), AIROHA_PCS_PMA_CK_RATE is set for 2500BASE-X but never restored for other modes, and AIROHA_PCS_PMA_FORCE_SEL_DA_FE_VOS in AIROHA_PCS_PMA_PXP_RX_FE_VOS is set for non-10G modes but never cleared when switching to USXGMII or 10GBASE-R. Similarly, in airoha_pcs_config(), AIROHA_PCS_HSGMII_PCS_MODE2_EN, AIROHA_PCS_HSGMII_PCS_MAC_MODE, and AIROHA_PCS_HSGMII_PCS_FORCE_RATEADAPT in AIROHA_PCS_HSGMII_PCS_CTROL_6, AIROHA_PCS_SGMII_SEND_AN_ERR_EN in AIROHA_PCS_HSGMII_PCS_CTROL_1, and AIROHA_PCS_HSGMII_AN_FORCE_AN_DONE in AIROHA_PCS_HSGMII_AN_SGMII_REG_AN_FORCE_CL37 are set for SGMII/1000BASE-X but not cleared for 2500BASE-X; and in airoha_pcs_link_up(), AIROHA_PCS_HSGMII_RATE_ADPT_FORCE_RATE_ADAPT_MODE_EN in AIROHA_PCS_HSGMII_RATE_ADAPT_CTRL_11 and AIROHA_PCS_HSGMII_PCS_FORCE_CUR_SGMII_MODE_SEL in AIROHA_PCS_HSGMII_PCS_AN_SGMII_MODE_FORCE are set for 10GBASE-R and forced SGMII respectively without being cleared in other modes.
[ ... ]
Quoted 6 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +6
-0+int an7581_pcs_bringup (struct airoha_pcs_priv *priv,int index, +phy_interface_t interface) +{ +struct regmap_field **pcs_ana_fields = priv->pcs_ana_fields[index]; + +/* Enable Analog Common Lane */
drivers/net/pcs/airoha/pcs-an7581.c line 1943
Quoted 10 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +10
-0+regmap_field_write (pcs_ana_fields[AN7581_PCS_CMN_EN ],0x1 ); + +/* Setup PLL */ +an7581_pcs_pll_bringup (priv, index, interface); + +msleep (100 ); + +/* Setup PHYA */ +return an7581_pcs_phya_bringup (priv, index, interface); +}
[Severity: High]
On the dual-port PCIe PCS ("airoha,an7581-pcs-pcie", num_port = 2), can configuring or bringing up one port disrupt or race with the other port?
Each port registers its own phylink_pcs instance attached to a separate phylink state lock, and struct airoha_pcs_priv has no lock serializing operations across the two ports. When either port runs airoha_pcs_pre_config() -> airoha_pcs_setup_scu(), it asserts and deasserts the shared device-wide priv->rsts ("mac" and "phy") reset lines. Then, in airoha_pcs_config() -> an7581_pcs_bringup(), both an7581_pcs_jcpll_bringup() and an7581_pcs_txpll_bringup() operate on priv->pcs_pma[0] and the shared JCPLL and TXPLL registers in priv->pcs_ana, force-disabling both PLLs and reprogramming interface-dependent parameters while the sibling port may be passing traffic or calibrating:
CPU0 (port 0) CPU1 (port 1)airoha_pcs_config()
an7581_pcs_bringup()
an7581_pcs_pll_bringup()
/* enables JCPLL & TXPLL */
msleep(100)
an7581_pcs_phya_bringup()
an7581_pcs_rx_prcal()
airoha_pcs_pre_config()
airoha_pcs_setup_scu()
reset_control_bulk_assert(priv->rsts)
airoha_pcs_config()
an7581_pcs_bringup()
an7581_pcs_txpll_bringup()
/* force-disables shared TXPLL */
an7581_pcs_apply_cdr_pr_idac()
/* calibrates with PLL disabled */ <-- calibration / link failureSignature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
PATCH v22 15/15 net: airoha: add phylink support
In reply to Christian MarangiAdd phylink support for each GDM port. For GDM1 add the internal interface mode as the only supported mode. For GDM2/3/4 add the required configuration of the PCS to make the external PHY or attached SFP cage work.
These needs to be defined in the GDM port node using the pcs-handle property.
Update and provide a .get/set_link_ksettings function that use phylink for ethtool OPs now that we fully support phylink.
--- drivers/net/ethernet/airoha/Kconfig | 2 + drivers/net/ethernet/airoha/airoha_eth.c | 197 +++++++++++++++++++++- drivers/net/ethernet/airoha/airoha_eth.h | 5 + drivers/net/ethernet/airoha/airoha_regs.h | 12 ++ 4 files changed, 213 insertions(+), 3 deletions(-)
drivers/net/ethernet/airoha/Kconfig +2 -0
@@ -20,6 +20,8 @@ config NET_AIROHA depends on NET_DSA || !NET_DSA select NET_AIROHA_NPU select PAGE_POOL + select FWNODE_PCS + select PHYLINK help This driver supports the gigabit ethernet MACs in the Airoha SoC family.
drivers/net/ethernet/airoha/airoha_eth.c +194 -3
272 lines not on this page · raw message
drivers/net/ethernet/airoha/airoha_eth.h +5 -0
@@ -612,6 +612,9 @@ struct airoha_gdm_dev {* QDMA migration. */ spinlock_t txq_lock[AIROHA_NUM_NETDEV_TX_RINGS ]; + +struct phylink *phylink; +struct phylink_config phylink_config; };struct airoha_gdm_port {
@@ -621,6 +624,8 @@ struct airoha_gdm_port {/* protect concurrent hw_stats accesses */ spinlock_t stats_lock; +/* protect concurrent GDM4 register access */ +struct mutex link_lock;struct metadata_dst *dsa_meta[AIROHA_MAX_DSA_PORTS ]; };
drivers/net/ethernet/airoha/airoha_regs.h +12 -0
@@ -371,6 +371,18 @@#define IP_FRAGMENT_PORT_MASK GENMASK(8, 5)#define IP_FRAGMENT_NBQ_MASK GENMASK(4, 0) +#define REG_FE_GDM4_TMBI_FRAG 0x2028 +#define GDM4_SGMII1_TX_WEIGHT_MASK GENMASK(31, 26) +#define GDM4_SGMII1_TX_FRAG_SIZE_MASK GENMASK(25, 16) +#define GDM4_SGMII0_TX_WEIGHT_MASK GENMASK(15, 10) +#define GDM4_SGMII0_TX_FRAG_SIZE_MASK GENMASK(9, 0) + +#define REG_FE_GDM4_RMBI_FRAG 0x202c +#define GDM4_SGMII1_RX_WEIGHT_MASK GENMASK(31, 26) +#define GDM4_SGMII1_RX_FRAG_SIZE_MASK GENMASK(25, 16) +#define GDM4_SGMII0_RX_WEIGHT_MASK GENMASK(15, 10) +#define GDM4_SGMII0_RX_FRAG_SIZE_MASK GENMASK(9, 0) +#define REG_MC_VLAN_EN 0x2100#define MC_VLAN_EN_MASK BIT(0)
Signature · 1 line
2.55.0
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Quoted 13 lines · On Tue, Nov 17, 2026 at 12:43:36PM +0000, [email protected] wrote:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Quoted 9 lines
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
The RCU """bug""" was found while evaluating the code. It's hardly possible to have such bug on real world scenario since PCS and MAC are currently tightly coupled. Nonetheless with VERY BAD PROGRAMMING it's still possible to trigger it.
It's both a fix and a prerequisite for having standalone PCS support.
(hope I don't have to repeat this message for the next gozillion revision of this series...)
Signature · 1 line
Ansuel