net: pcs: Introduce support for fwnode PCS
Subsystems (9)
v8 cover net: pcs: Introduce support for fwnode PCS
This series introduce a most awaited feature that is correctly provide PCS with fwnode without having to use specific export symbol and additional handling of PCS in phylink.
At times there were 2 different implementation (this and the one from Sean) but Sean agreed that this can be picked and used in favor of his implementation as long as his case with race condition is correctly handled.
---
First the PCS fwnode:
The concept is to implement a producer-consumer API similar to other subsystem like clock or PHY.
That seems to be the best solution to the problem as PCS driver needs to be detached from phylink and implement a simple way to provide a PCS while maintaining support for probe defer or driver removal.
To keep the implementation simple, the PCS driver devs needs some collaboration to correctly implement this. This is O.K. as helper to correctly implement this are provided hence it's really a matter of following a pattern to correct follow removal of a PCS driver.
A PCS provider have to implement and call fwnode_pcs_add_provider() in probe function and define an xlate function to define how the PCS should be provided based on the requested interface and phandle spec defined in fwnode (based on the #pcs-cells)
fwnode_pcs_get() is provided to provide a specific PCS declared in fwnode at index.
A simple xlate function is provided for simple single PCS implementation, fwnode_pcs_simple_get.
A PCS provider on driver removal should first call fwnode_pcs_del_provider() to delete itself as a provider and then release the PCS from phylink with phylink_release_pcs() under rtnl lock.
---
Second PCS handling in phylink:
We have the PCS problem for the only reason that in initial implementation, we permitted way too much flexibility to MAC driver and things started to deviate. At times we couldn't think SoC would start to put PCS outside the MAC hence it was OK to assume they would live in the same driver. With the introduction of 10g in more consumer devices, we are observing a rapid growth of this pattern with multiple PCS external to MAC.
To put a stop on this, the only solution is to give back to phylink control on PCS handling and enforce more robust supported interface definition from both MAC and PCS side.
It's suggested to read patch 0003 of this series for more info, here a brief explaination of the idea:
This series introduce handling of PCS in phylink and try to deprecate .mac_select_pcs.
Phylink now might contain a linked list of available PCS and those will be used for PCS selection on phylink_major_config.
MAC driver needs to define pcs_interfaces mask in phylink_config for every interface that needs a dedicated PCS.
These PCS needs to be provided to phylink at phylink_create time by setting the .fill_available_pcs and .num_possible_pcs in phylink_config. Helpers to parse PCS from fwnode are provided fwnode_phylink_pcs_count() that will return the count of PCS entries described in the firmware node and fwnode_phylink_pcs_parse() that will fill a preallocated array of PCS pointer with the actual available PCS (ignoring the one that still needs to be probed).
phylink_create() will fill the internal PCS list with the passed array of PCS. phylink_major_config and other user of .mac_select_pcs are adapted to make use of this new PCS list.
The supported interface value is also moved internally to phylink struct. This is to handle late removal and addition of PCS. (the bonus effect to this is giving phylink a clear idea of what is actually supported by the MAC and his constraint with PCS)
The supported interface mask in phylink is done by OR the supported_interfaces in phylink_config with every PCS in PCS list.
PCS removal is supported by forcing a mac_config, refresh the supported interfaces and run a phy_resolve().
PCS late addition is supported by introducing a global notifier for PCS provider. If a phylink have the pcs_interfaces mask not zero, it's registered to this notifier.
PCS provider will emit a global PCS add event to signal any interface that a new PCS might be available.
The function will then check if the PCS is related to the MAC fwnode and add it accordingly.
A user for this new implementation is provided as an Airoha PCS driver. This was also tested downstream with the IPQ95xx QCOM SoC and with the help of Daniel also on the various Mediatek MT7988 SoC with both SFP cage implementation and DSA attached.
Lots of tests were done with driver unbind/bind and with interface up/down also by adding print to make sure major_config_fail gets correctly triggered and reset once the PCS comes back.
The dedicated commits have longer description on the implementation so it's suggested to also check there for additional info.
It's worth to mention that OpenWrt is currently using this on Mediatek SoC and QCOM ipq807x/ipq60xx/ipq50xx and Airoha are already ported in staging tree for testing.
---
Changes 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 (12):
net: phylink: keep and use MAC supported_interfaces in phylink struct
net: phylink: introduce internal phylink PCS handling
net: phylink: add phylink_release_pcs() to externally release a PCS
net: pcs: implement Firmware node support for PCS driver
net: phylink: support late PCS provider attach
net: Document PCS subsystem
MAINTAINERS: add myself as PCS subsystem maintainer
of: property: fw_devlink: Add support for "pcs-handle"
net: phylink: add .pcs_link_down PCS OP
dt-bindings: net: pcs: Document support for Airoha Ethernet PCS
net: pcs: airoha: add PCS driver for Airoha AN7581 SoC
net: airoha: add phylink support
.../bindings/net/pcs/airoha,pcs.yaml | 261 ++ Documentation/networking/index.rst | 1 + Documentation/networking/pcs.rst | 229 ++ MAINTAINERS | 9 + drivers/net/ethernet/airoha/Kconfig | 1 + drivers/net/ethernet/airoha/airoha_eth.c | 193 +- drivers/net/ethernet/airoha/airoha_eth.h | 7 +- drivers/net/ethernet/airoha/airoha_regs.h | 12 + drivers/net/pcs/Kconfig | 8 + drivers/net/pcs/Makefile | 3 + drivers/net/pcs/airoha/Kconfig | 12 + drivers/net/pcs/airoha/Makefile | 7 + drivers/net/pcs/airoha/pcs-airoha-common.c | 1324 +++++++++++ drivers/net/pcs/airoha/pcs-airoha.h | 1311 +++++++++++ drivers/net/pcs/airoha/pcs-an7581.c | 2093 +++++++++++++++++ drivers/net/pcs/pcs.c | 261 ++ drivers/net/phy/phylink.c | 367 ++- drivers/of/property.c | 2 + include/linux/pcs/pcs-provider.h | 41 + include/linux/pcs/pcs.h | 140 ++ include/linux/phylink.h | 30 + 21 files changed, 6264 insertions(+), 48 deletions(-) create mode 100644 Documentation/devicetree/bindings/net/pcs/airoha,pcs.yaml create mode 100644 Documentation/networking/pcs.rst create mode 100644 drivers/net/pcs/airoha/Kconfig create mode 100644 drivers/net/pcs/airoha/Makefile create mode 100644 drivers/net/pcs/airoha/pcs-airoha-common.c create mode 100644 drivers/net/pcs/airoha/pcs-airoha.h create mode 100644 drivers/net/pcs/airoha/pcs-an7581.c create mode 100644 drivers/net/pcs/pcs.c create mode 100644 include/linux/pcs/pcs-provider.h create mode 100644 include/linux/pcs/pcs.h
Signature · 1 line
2.53.0
RFC PATCH v8 1/12 net: phylink: keep and use MAC supported_interfaces in phylink struct
Add in phylink struct a copy of supported_interfaces from phylink_config and make use of that instead of relying on phylink_config value.
This in preparation for support of PCS handling internally to phylink where a PCS can be removed or added after the phylink is created and we need both a reference of the supported_interfaces value from phylink_config and an internal value that can be updated with the new PCS info.
--- drivers/net/phy/phylink.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/net/phy/phylink.c +15 -7
@@ -60,6 +60,11 @@ struct phylink {/* The link configuration settings */ struct phylink_link_state link_config; +/* What interface are supported by the current link. +* Can change on removal or addition of new PCS. +*/ +DECLARE_PHY_INTERFACE_MASK (supported_interfaces); +/* The current settings */ phy_interface_t cur_interface;
@@ -629,7 +634,7 @@ static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy,static int phylink_validate (struct phylink *pl,unsigned long *supported,struct phylink_link_state *state) { -const unsigned long *interfaces = pl->config->supported_interfaces; +const unsigned long *interfaces = pl->supported_interfaces;if (state->interface ==PHY_INTERFACE_MODE_NA ) return phylink_validate_mask(pl, NULL, supported, state,
@@ -1868,6 +1873,9 @@ struct phylink *phylink_create(struct phylink_config *config,mutex_init (&pl->state_mutex);INIT_WORK (&pl->resolve, phylink_resolve); +phy_interface_copy (pl->supported_interfaces, + config->supported_interfaces); + pl->config = config;if (config->type ==PHYLINK_NETDEV ) { pl->netdev =to_net_dev (config->dev);
@@ -2026,7 +2034,7 @@ static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,* those which the host supports. */ phy_interface_and (interfaces, phy->possible_interfaces, - pl->config->supported_interfaces); + pl->supported_interfaces);if (phy_interface_empty (interfaces)) {phylink_err (pl,"PHY has no common interfaces\n" );
@@ -2828,12 +2836,12 @@ static phy_interface_t phylink_sfp_select_interface(struct phylink *pl,return interface; } -if (!test_bit (interface, pl->config->supported_interfaces)) { +if (!test_bit (interface, pl->supported_interfaces)) {phylink_err (pl,"selection of interface failed, SFP selected %s (%u) but MAC supports %*pbl\n" ,phy_modes (interface), interface, (int )PHY_INTERFACE_MODE_MAX , - pl->config->supported_interfaces); + pl->supported_interfaces);return PHY_INTERFACE_MODE_NA ; }
@@ -3761,14 +3769,14 @@ static int phylink_sfp_config_optical(struct phylink *pl)phylink_dbg (pl,"optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n" , (int )PHY_INTERFACE_MODE_MAX , - pl->config->supported_interfaces, + pl->supported_interfaces, (int )PHY_INTERFACE_MODE_MAX , pl->sfp_interfaces);/* Find the union of the supported interfaces by the PCS/MAC and * the SFP module. */ -phy_interface_and (pl->sfp_interfaces, pl->config->supported_interfaces, +phy_interface_and (pl->sfp_interfaces, pl->supported_interfaces, pl->sfp_interfaces);if (phy_interface_empty (pl->sfp_interfaces)) {phylink_err (pl,"unsupported SFP module: no common interface modes\n" );
@@ -3939,7 +3947,7 @@ static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)/* Set the PHY's host supported interfaces */ phy_interface_and (phy->host_interfaces, phylink_sfp_interfaces, - pl->config->supported_interfaces); + pl->supported_interfaces);/* Do the initial configuration */ return phylink_sfp_config_phy (pl, phy);
Signature · 1 line
2.53.0
RFC PATCH v8 2/12 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 | 224 ++++++++++++++++++++++++++++++++------ include/linux/phylink.h | 16 +++ 2 files changed, 205 insertions(+), 35 deletions(-)
drivers/net/phy/phylink.c +189 -35
@@ -60,6 +60,9 @@ struct phylink {/* The link configuration settings */ struct phylink_link_state link_config; +/* List of available PCS */ +struct list_head pcs_list; +/* What interface are supported by the current link. * Can change on removal or addition of new PCS. */
@@ -154,6 +157,8 @@ static const phy_interface_t phylink_sfp_interface_preference[] = {static DECLARE_PHY_INTERFACE_MASK (phylink_sfp_interfaces); +static void phylink_run_resolve (struct phylink *pl); +/** * phylink_set_port_modes() - set the port type modes in the ethtool mask * @mask: ethtool link mode mask
@@ -518,12 +523,29 @@ static void phylink_validate_mask_caps(unsigned long *supported,linkmode_and (state->advertising, state->advertising, mask); } +static int phylink_validate_pcs_interface (struct phylink_pcs *pcs, +phy_interface_t interface) +{ +/* If PCS define an empty supported_interfaces value, assume +* all interface are supported. +*/ +if (phy_interface_empty (pcs->supported_interfaces)) +return 0 ; + +/* Ensure that this PCS supports the interface mode */ +if (!test_bit (interface, pcs->supported_interfaces)) +return -EINVAL ; + +return 0 ; +} +static int phylink_validate_mac_and_pcs (struct phylink *pl,unsigned long *supported,struct phylink_link_state *state) { -struct phylink_pcs *pcs =NULL ;unsigned long capabilities; +struct phylink_pcs *pcs; +bool pcs_found = false;int ret;/* Get the PCS for this interface mode */
@@ -531,9 +553,24 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl, pcs = pl->mac_ops->mac_select_pcs (pl->config, state->interface);if (IS_ERR (pcs))return PTR_ERR (pcs); + + pcs_found = !!pcs; +/* +* Find a PCS in available PCS list for the requested interface. +* +* Skip searching if the MAC doesn't require a dedicated PCS for +* the requested interface. +*/ + }else if (test_bit (state->interface, pl->config->pcs_interfaces)) { +list_for_each_entry (pcs, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (pcs, state->interface)) { + pcs_found = true; +break ; + } + } } -if (pcs) { +if (pcs_found) {/* The PCS, if present, must be setup before phylink_create() * has been called. If the ops is not initialised, print an * error and backtrace rather than oopsing the kernel.
@@ -545,13 +582,10 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl,return -EINVAL ; } -/* Ensure that this PCS supports the interface which the MAC -* returned it for. It is an error for the MAC to return a PCS -* that does not support the interface mode. -*/ -if (!phy_interface_empty (pcs->supported_interfaces) && - !test_bit (state->interface, pcs->supported_interfaces)) { -phylink_err (pl,"MAC returned PCS which does not support %s\n" , +/* Recheck PCS to handle legacy way for .mac_select_pcs */ + ret =phylink_validate_pcs_interface (pcs, state->interface); +if (ret) { +phylink_err (pl,"selected PCS does not support %s\n" ,phy_modes (state->interface));return -EINVAL ; }
@@ -965,12 +999,22 @@ static unsigned int phylink_inband_caps(struct phylink *pl, phy_interface_t interface) {struct phylink_pcs *pcs; +bool pcs_found = false; -if (!pl->mac_ops->mac_select_pcs) -return 0 ; +if (pl->mac_ops->mac_select_pcs) { + pcs = pl->mac_ops->mac_select_pcs (pl->config, + interface); + pcs_found = !!pcs; + }else if (test_bit (interface, pl->config->pcs_interfaces)) { +list_for_each_entry (pcs, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (pcs, interface)) { + pcs_found = true; +break ; + } + } + } - pcs = pl->mac_ops->mac_select_pcs (pl->config, interface); -if (!pcs) +if (!pcs_found)return 0 ;return phylink_pcs_inband_caps (pcs, interface);
@@ -1265,10 +1309,36 @@ static void phylink_major_config(struct phylink *pl, bool restart, pl->major_config_failed = true;return ; } +/* Find a PCS in available PCS list for the requested interface. +* This doesn't overwrite the previous .mac_select_pcs as either +* .mac_select_pcs or PCS list implementation are permitted. +* +* Skip searching if the MAC doesn't require a dedicated PCS for +* the requested interface. +*/ + }else if (test_bit (state->interface, pl->config->pcs_interfaces)) { +bool pcs_found = false; + +list_for_each_entry (pcs, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (pcs, + state->interface)) { + pcs_found = true; +break ; + } + } + +if (!pcs_found) { +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",
@@ -1295,13 +1365,15 @@ static void phylink_major_config(struct phylink *pl, bool restart,if (pcs_changed) {phylink_pcs_disable (pl->pcs); -if (pl->pcs) - pl->pcs->phylink =NULL ; +if (pl->mac_ops->mac_select_pcs) { +if (pl->pcs) + pl->pcs->phylink =NULL ; -if (pcs) - pcs->phylink = pl; +if (pcs) + pcs->phylink = pl; + } - pl->pcs = pcs; +WRITE_ONCE (pl->pcs, pcs); }if (pl->pcs)
@@ -1834,6 +1906,44 @@ int phylink_set_fixed_link(struct phylink *pl, }EXPORT_SYMBOL_GPL (phylink_set_fixed_link); +static int phylink_fill_available_pcs (struct phylink *pl, +struct phylink_config *config) +{ +struct phylink_pcs **pcss; +int i, ret; + +if (!config->num_possible_pcs) +return 0 ; + +if (!config->fill_available_pcs) { +dev_err (config->dev, +"phylink: error: num_possible_pcs defined but no fill_available_pcs\n" ); +return -EINVAL ; + } + + pcss =kzalloc_objs (*pcss, config->num_possible_pcs); +if (!pcss) +return -ENOMEM ; + + ret = config->fill_available_pcs (config, pcss, config->num_possible_pcs); +if (ret <0 ) + goto out; + +for (i =0 ; i < config->num_possible_pcs; i++) { +struct phylink_pcs *pcs = pcss[i]; + +if (!pcs) +continue ; + +list_add (&pcs->list, &pl->pcs_list); + } + +out: +kfree (pcss); + +return ret; +} +/** * phylink_create() - create a phylink instance * @config: a pointer to the target &struct phylink_config
@@ -1855,6 +1965,7 @@ struct phylink *phylink_create(struct phylink_config *config,phy_interface_t iface, const struct phylink_mac_ops *mac_ops) { + struct phylink_pcs *pcs;struct phylink *pl;int ret;
@@ -1865,6 +1976,16 @@ struct phylink *phylink_create(struct phylink_config *config,return ERR_PTR (-EINVAL ); } +/* +* Make sure either PCS internal validation or .mac_select_pcs +* is used. Return error if both are defined. +*/ +if (config->num_possible_pcs && mac_ops->mac_select_pcs) { +dev_err (config->dev, +"phylink: error: either phylink_config .num_possible_pcs or .mac_select_pcs must be used\n" ); +return ERR_PTR (-EINVAL ); + } + pl =kzalloc_obj (*pl);if (!pl)return ERR_PTR (-ENOMEM );
@@ -1872,10 +1993,26 @@ struct phylink *phylink_create(struct phylink_config *config,mutex_init (&pl->phydev_mutex);mutex_init (&pl->state_mutex);INIT_WORK (&pl->resolve, phylink_resolve); +INIT_LIST_HEAD (&pl->pcs_list); + +/* Fill the PCS list with available PCS from phylink config */ + ret =phylink_fill_available_pcs (pl, config); +if (ret <0 ) + goto free_pl; + +/* Link available PCS to phylink */ +list_for_each_entry (pcs, &pl->pcs_list, list) + pcs->phylink = pl;phy_interface_copy (pl->supported_interfaces, config->supported_interfaces); +/* Update supported interfaces */ +list_for_each_entry (pcs, &pl->pcs_list, list) +phy_interface_or (pl->supported_interfaces, + pl->supported_interfaces, + pcs->supported_interfaces); + pl->config = config;if (config->type ==PHYLINK_NETDEV ) { pl->netdev =to_net_dev (config->dev);
@@ -1883,8 +2020,7 @@ struct phylink *phylink_create(struct phylink_config *config, }else if (config->type ==PHYLINK_DEV ) { pl->dev = config->dev; }else { -kfree (pl); -return ERR_PTR (-EINVAL ); + goto unlink_pcs_list; } pl->mac_supports_eee_ops =phylink_mac_implements_lpi (mac_ops);
@@ -1917,28 +2053,29 @@ struct phylink *phylink_create(struct phylink_config *config,phylink_validate (pl, pl->supported, &pl->link_config); ret =phylink_parse_mode (pl, fwnode); -if (ret <0 ) { -kfree (pl); -return ERR_PTR (ret); - } +if (ret <0 ) + goto unlink_pcs_list;if (pl->cfg_link_an_mode ==MLO_AN_FIXED ) { ret =phylink_parse_fixedlink (pl, fwnode); -if (ret <0 ) { -kfree (pl); -return ERR_PTR (ret); - } +if (ret <0 ) + goto unlink_pcs_list; } pl->req_link_an_mode = pl->cfg_link_an_mode; ret =phylink_register_sfp (pl, fwnode); -if (ret <0 ) { -kfree (pl); -return ERR_PTR (ret); - } +if (ret <0 ) + goto unlink_pcs_list;return pl; + +unlink_pcs_list: +list_for_each_entry (pcs, &pl->pcs_list, list) + pcs->phylink =NULL ; +free_pl: +kfree (pl); +return ERR_PTR (ret); }EXPORT_SYMBOL_GPL (phylink_create);
@@ -1953,11 +2090,21 @@ EXPORT_SYMBOL_GPL(phylink_create);*/ void phylink_destroy (struct phylink *pl) { +struct phylink_pcs *pcs, *tmp; +sfp_bus_del_upstream (pl->sfp_bus);if (pl->link_gpio)gpiod_put (pl->link_gpio);cancel_work_sync (&pl->resolve); + +/* Drop link between PCS and phylink */ +/* Remove every PCS from phylink PCS list */ +list_for_each_entry_safe (pcs, tmp, &pl->pcs_list, list) { + pcs->phylink =NULL ; +list_del (&pcs->list); + } +kfree (pl); }EXPORT_SYMBOL_GPL (phylink_destroy);
@@ -2413,8 +2560,15 @@ void phylink_pcs_change(struct phylink_pcs *pcs, bool up) {struct phylink *pl = pcs->phylink; -if (pl) -phylink_link_changed (pl, up,"pcs" ); +/* +* Ignore PCS link state change if the PCS is not +* attached to a phylink instance or the phylink +* instance is not currently using this PCS. +*/ +if (!pl ||READ_ONCE (pl->pcs) != pcs) +return ; + +phylink_link_changed (pl, up,"pcs" ); }EXPORT_SYMBOL_GPL (phylink_pcs_change);
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 *phylink;bool poll;bool rxc_always_on; + +/* private: */ +struct list_head list; };/**
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH net-next v8 02/12] net: phylink: introduce internal phylink PCS handling
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Missing `list_del(&pcs->list)` in the error path of `phylink_create()` leaves dangling pointers to…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Missing list_del(&pcs->list) in the error path of phylink_create() leaves dangling pointers to freed memory.
- [High] phylink_validate_mac_and_pcs() fails to return an error when a required PCS is not found.
- [High] phy_interface_or() mishandles the 'empty means all' semantic for pcs->supported_interfaces.
- [High] Race condition causing a Use-After-Free in phylink_pcs_change() during phylink_destroy().
- [Medium] phylink_fill_available_pcs() reverses the priority order of the provided PCS array.
--
commit b5576005f95ddfaef269adf2938d4dda4a006f6b
Author: Christian Marangi <[email protected]>
net: phylink: introduce internal phylink PCS handling
This commit introduces an internal linked list implementation for handling PCS devices within phylink, providing an alternative to the .mac_select_pcs callback. It allows MAC drivers to provide an array of available PCS devices which are automatically mapped and selected based on the interface mode.
Quoted 4 lines
drivers/net/phy/phylink.c +0
-0
[ ... ]
Quoted 44 lines
drivers/net/phy/phylink.c +27
-1@@ -518,12 +523,29 @@ static void phylink_validate_mask_caps(unsigned long *supported,linkmode_and (state->advertising, state->advertising, mask); } +static int phylink_validate_pcs_interface (struct phylink_pcs *pcs, +phy_interface_t interface) +{ +/* If PCS define an empty supported_interfaces value, assume +* all interface are supported. +*/ +if (phy_interface_empty (pcs->supported_interfaces)) +return 0 ; + +/* Ensure that this PCS supports the interface mode */ +if (!test_bit (interface, pcs->supported_interfaces)) +return -EINVAL ; + +return 0 ; +} +static int phylink_validate_mac_and_pcs (struct phylink *pl,unsigned long *supported,struct phylink_link_state *state) { -struct phylink_pcs *pcs =NULL ;unsigned long capabilities; +struct phylink_pcs *pcs; +bool pcs_found = false;int ret;/* Get the PCS for this interface mode */ @@ -531,9 +553,24 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl, pcs = pl->mac_ops->mac_select_pcs (pl->config, state->interface);if (IS_ERR (pcs))return PTR_ERR (pcs); + + pcs_found = !!pcs; +/* +* Find a PCS in available PCS list for the requested interface. +* +* Skip searching if the MAC doesn't require a dedicated PCS for +* the requested interface. +*/ + }else if (test_bit (state->interface, pl->config->pcs_interfaces)) {
drivers/net/phy/phylink.c static int phylink_validate_mac_and_pcs(struct phylink *pl, · line 573
Quoted 10 lines
drivers/net/phy/phylink.c +7
-1+list_for_each_entry (pcs, &pl->pcs_list, list) { +if (!phylink_validate_pcs_interface (pcs, state->interface)) { + pcs_found = true; +break ; + } + } } -if (pcs) { +if (pcs_found) {
[Severity: High]
Does this code incorrectly fall through and return success when a required PCS is not found?
If test_bit(state->interface, pl->config->pcs_interfaces) requires a dedicated PCS, but none in pl->pcs_list matches, pcs_found remains false. The function then skips the pcs_found validation block, proceeds to validate MAC capabilities, and successfully returns 0.
Could this falsely advertise the interface as supported, leading to failures later in phylink_major_config() where the missing PCS causes an abort? Should there be an explicit error return when a required PCS is not found?
[ ... ]
Quoted 25 lines
drivers/net/phy/phylink.c +21
-0@@ -1834,6 +1906,44 @@ int phylink_set_fixed_link(struct phylink *pl, }EXPORT_SYMBOL_GPL (phylink_set_fixed_link); +static int phylink_fill_available_pcs (struct phylink *pl, +struct phylink_config *config) +{ +struct phylink_pcs **pcss; +int i, ret; + +if (!config->num_possible_pcs) +return 0 ; + +if (!config->fill_available_pcs) { +dev_err (config->dev, +"phylink: error: num_possible_pcs defined but no fill_available_pcs\n" ); +return -EINVAL ; + } + + pcss =kzalloc_objs (*pcss, config->num_possible_pcs); +if (!pcss) +return -ENOMEM ; + + ret = config->fill_available_pcs (config, pcss, config->num_possible_pcs); +if (ret <0 )
drivers/net/phy/phylink.c int phylink_set_fixed_link(struct phylink *pl, · line 1939
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+ goto out; + +for (i =0 ; i < config->num_possible_pcs; i++) { +struct phylink_pcs *pcs = pcss[i]; + +if (!pcs) +continue ; + +list_add (&pcs->list, &pl->pcs_list); + }
[Severity: Medium]
Does this loop reverse the intended priority order of the provided PCS array?
The function iterates sequentially through the pcss array and uses list_add() to insert each PCS at the head of the list. When evaluating the list from head to tail later (e.g., in phylink_validate_mac_and_pcs()), the MAC driver's lowest-priority PCS (last in the array) will be checked before higher-priority ones.
Should this use list_add_tail() instead to preserve the array order?
[ ... ]
Quoted 13 lines
drivers/net/phy/phylink.c +9
-0@@ -1872,10 +1993,26 @@ struct phylink *phylink_create(struct phylink_config *config,mutex_init (&pl->phydev_mutex);mutex_init (&pl->state_mutex);INIT_WORK (&pl->resolve, phylink_resolve); +INIT_LIST_HEAD (&pl->pcs_list); + +/* Fill the PCS list with available PCS from phylink config */ + ret =phylink_fill_available_pcs (pl, config); +if (ret <0 ) + 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 2014
Quoted 10 lines
drivers/net/phy/phylink.c +6
-0+ pcs->phylink = pl;phy_interface_copy (pl->supported_interfaces, config->supported_interfaces); +/* Update supported interfaces */ +list_for_each_entry (pcs, &pl->pcs_list, list) +phy_interface_or (pl->supported_interfaces, + pl->supported_interfaces, + pcs->supported_interfaces);
[Severity: High]
Does this loop mishandle the empty means all semantic for supported interfaces?
The subsystem defines that an empty pcs->supported_interfaces mask means the PCS supports all interfaces (as handled in phylink_validate_pcs_interface()). However, OR-ing an empty bitmask into pl->supported_interfaces contributes no interfaces at all.
Could this silently drop capabilities for generic PCSs that leave their supported_interfaces empty?
[ ... ]
Quoted 30 lines
drivers/net/phy/phylink.c +6
-12@@ -1917,28 +2053,29 @@ struct phylink *phylink_create(struct phylink_config *config,phylink_validate (pl, pl->supported, &pl->link_config); ret =phylink_parse_mode (pl, fwnode); -if (ret <0 ) { -kfree (pl); -return ERR_PTR (ret); - } +if (ret <0 ) + goto unlink_pcs_list;if (pl->cfg_link_an_mode ==MLO_AN_FIXED ) { ret =phylink_parse_fixedlink (pl, fwnode); -if (ret <0 ) { -kfree (pl); -return ERR_PTR (ret); - } +if (ret <0 ) + goto unlink_pcs_list; } pl->req_link_an_mode = pl->cfg_link_an_mode; ret =phylink_register_sfp (pl, fwnode); -if (ret <0 ) { -kfree (pl); -return ERR_PTR (ret); - } +if (ret <0 ) + goto unlink_pcs_list;
drivers/net/phy/phylink.c struct phylink *phylink_create(struct phylink_config *config, · line 2079
Quoted 10 lines
drivers/net/phy/phylink.c +7
-0return pl; + +unlink_pcs_list: +list_for_each_entry (pcs, &pl->pcs_list, list) + pcs->phylink =NULL ; +free_pl: +kfree (pl); +return ERR_PTR (ret); }
[Severity: High]
Does this error path leave dangling pointers to freed memory?
The loop clears pcs->phylink but fails to call list_del(&pcs->list) before freeing pl. Because the phylink_pcs structures are long-lived and owned by the MAC driver or provider, their list nodes are left pointing to the freed pl->pcs_list memory.
If a driver retries initialization and provides the same PCS objects, could this list corruption lead to a use-after-free crash during the list_add() step?
[ ... ]
Quoted 6 lines
drivers/net/phy/phylink.c +0
-2@@ -2413,8 +2560,15 @@ void phylink_pcs_change(struct phylink_pcs *pcs, bool up) {struct phylink *pl = pcs->phylink; -if (pl) -phylink_link_changed (pl, up,"pcs" );
drivers/net/phy/phylink.c void phylink_pcs_change(struct phylink_pcs *pcs, bool up) · line 2572
Quoted 10 lines
drivers/net/phy/phylink.c +9
-0+/* +* Ignore PCS link state change if the PCS is not +* attached to a phylink instance or the phylink +* instance is not currently using this PCS. +*/ +if (!pl ||READ_ONCE (pl->pcs) != pcs) +return ; + +phylink_link_changed (pl, up,"pcs" ); }
[Severity: High]
Is there a race condition here during phylink_destroy() that can cause a use-after-free?
If an external PCS driver triggers an interrupt and calls phylink_pcs_change() concurrently with phylink_destroy(), pl might be read as non-NULL before phylink_destroy() unlinks the PCS and frees pl.
Without proper synchronization (like RCU or a lock), could the interrupt handler be preempted, and subsequently dereference pl via READ_ONCE(pl->pcs) after it has been freed?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RFC PATCH v8 3/12 net: phylink: add phylink_release_pcs() to externally release a PCS
In reply to Christian MarangiAdd phylink_release_pcs() to externally release a PCS from a phylink instance. This can be used to handle case when a single PCS needs to be removed and the phylink instance needs to be refreshed.
On calling phylink_release_pcs(), the PCS will be removed from the phylink internal PCS list and the phylink supported_interfaces value is reparsed with the remaining PCS interfaces.
Also a phylink resolve is triggered to handle the PCS removal.
The flag force_major_config is set to make phylink resolve reconfigure the interface (even if it didn't change).
This is needed to handle the special case when the current PCS used by phylink is removed and a major_config is needed to propagae the configuration change. With this option enabled we also force mac_config even if the PHY link is not up for the in-band case.
--- drivers/net/phy/phylink.c | 56 +++++++++++++++++++++++++++++++++++++++ include/linux/phylink.h | 2 ++ 2 files changed, 58 insertions(+)
drivers/net/phy/phylink.c +56 -0
@@ -158,6 +158,8 @@ static const phy_interface_t phylink_sfp_interface_preference[] = {static DECLARE_PHY_INTERFACE_MASK (phylink_sfp_interfaces);static void phylink_run_resolve (struct phylink *pl); +static void phylink_link_down (struct phylink *pl); +static void phylink_pcs_disable (struct phylink_pcs *pcs);/** * phylink_set_port_modes() - set the port type modes in the ethtool mask
@@ -918,6 +920,60 @@ static void phylink_resolve_an_pause(struct phylink_link_state *state) } } +/** +* phylink_release_pcs - Removes a PCS from the phylink PCS available list +* @pcs: a pointer to the phylink_pcs struct to be released +* +* This function release a PCS from the phylink PCS available list if +* actually in use. It also refreshes the supported interfaces of the +* phylink instance by copying the supported interfaces from the phylink +* conf and merging the supported interfaces of the remaining available PCS +* in the list and trigger a resolve. +*/ +void phylink_release_pcs (struct phylink_pcs *pcs) +{ +struct phylink *pl; + +ASSERT_RTNL (); + + pl = pcs->phylink; +if (!pl) +return ; + +mutex_lock (&pl->state_mutex); + +list_del (&pcs->list); + pcs->phylink =NULL ; + +/* +* Check if we are removing the PCS currently +* in use by phylink. 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) { +phylink_link_down (pl); +phylink_pcs_disable (pl->pcs); + + pl->force_major_config = true; + pl->pcs =NULL ; + } + +mutex_unlock (&pl->state_mutex); + +/* Refresh supported interfaces */ +phy_interface_copy (pl->supported_interfaces, + pl->config->supported_interfaces); +list_for_each_entry (pcs, &pl->pcs_list, list) +phy_interface_or (pl->supported_interfaces, + pl->supported_interfaces, + pcs->supported_interfaces); + +phylink_run_resolve (pl); +} +EXPORT_SYMBOL_GPL (phylink_release_pcs); +static unsigned int phylink_pcs_inband_caps (struct phylink_pcs *pcs,phy_interface_t interface) {
include/linux/phylink.h +2 -0
@@ -751,6 +751,8 @@ void phylink_disconnect_phy(struct phylink *);int phylink_set_fixed_link (struct phylink *,const struct phylink_link_state *); +void phylink_release_pcs (struct phylink_pcs *pcs); +void phylink_mac_change (struct phylink *,bool up);void phylink_pcs_change (struct phylink_pcs *,bool up);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH net-next v8 03/12] net: phylink: add phylink_release_pcs() to externally release a PCS
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Unconditional and unbalanced `phylink_link_down()` in `phylink_release_pcs()` causes duplicate…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unconditional and unbalanced phylink_link_down() in phylink_release_pcs() causes duplicate mac_link_down() callbacks and corrupts the internal phylink state machine. --
commit 2b29c68aa2ea3d8d4cc5979668a733bbdab7236c
Author: Christian Marangi <[email protected]>
net: phylink: add phylink_release_pcs() to externally release a PCS
This commit introduces phylink_release_pcs() to allow removing a PCS from a phylink instance. When the released PCS is the active one, the link is brought down and a major config is forced to trigger a reconfiguration.
Quoted 4 lines
drivers/net/phy/phylink.c +0
-0
[ ... ]
Quoted 1 line
drivers/net/phy/phylink.c +0
-0@@ -918,6 +920,60 @@ static void phylink_resolve_an_pause(struct phylink_link_state *state)
[ ... ]
drivers/net/phy/phylink.c static void phylink_resolve_an_pause(struct phylink_link_state *state) · line 959
Quoted 12 lines
drivers/net/phy/phylink.c +12
-0+/* +* Check if we are removing the PCS currently +* in use by phylink. 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) { +phylink_link_down (pl); +phylink_pcs_disable (pl->pcs); + + pl->force_major_config = true;
[Severity: High]
Does this unconditional call to phylink_link_down() cause unbalanced mac_link_down() callbacks if the link is already down?
Also, if the link was up, does pl->old_link_state need to be cleared here?
For PHYLINK_DEV instances that lack a netdev, phylink_link_is_up(pl) relies entirely on pl->old_link_state. Because pl->old_link_state remains true, the scheduled phylink_resolve() evaluates cur_link_state as true.
Inside phylink_resolve():
1. The force_major_config block sees cur_link_state == true and calls phylink_link_down() a second time, then sets cur_link_state = false.
2. Later, the state update block compares link_state.link != cur_link_state (false != false), evaluating to false. As a result, pl->old_link_state is NOT updated and remains true permanently.
3. On the next phylink_resolve() run, cur_link_state again evaluates to true. The state update block compares link_state.link != cur_link_state (false != true), evaluating to true, and calls phylink_link_down(pl) a third time.
Can this state machine corruption result in multiple unbalanced mac_link_down() calls and cause clock underflows, regulator warnings, or hardware lockups in MAC drivers?
Quoted 4 lines
drivers/net/phy/phylink.c +4
-0+ pl->pcs =NULL ; + } + +mutex_unlock (&pl->state_mutex);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [RFC PATCH net-next v8 03/12] net: phylink: add phylink_release_pcs() to externally release a PCS
In reply to Christian MarangiHello Christian,
Quoted 79 lines · Christian Marangi
Add
phylink_release_pcs()to externally release a PCS from a phylink instance. This can be used to handle case when a single PCS needs to be removed and the phylink instance needs to be refreshed.On calling
phylink_release_pcs(), the PCS will be removed from the phylink internal PCS list and the phylinksupported_interfacesvalue is reparsed with the remaining PCS interfaces.Also a phylink resolve is triggered to handle the PCS removal.
The flag
force_major_configis 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 amajor_configis needed to propagae the configuration change. With this option enabled we also forcemac_configeven if the PHY link is not up for the in-band case.Signed-off-by Christian Marangi <[email protected]>--- drivers/net/phy/phylink.c | 56 +++++++++++++++++++++++++++++++++++++++ include/linux/phylink.h | 2 ++ 2 files changed, 58 insertions(+)drivers/net/phy/phylink.c +41
-0@@ -158,6 +158,8 @@ static const phy_interface_t phylink_sfp_interface_preference[] = {static DECLARE_PHY_INTERFACE_MASK (phylink_sfp_interfaces);static void phylink_run_resolve (struct phylink *pl); +static void phylink_link_down (struct phylink *pl); +static void phylink_pcs_disable (struct phylink_pcs *pcs);/** * phylink_set_port_modes() - set the port type modes in the ethtool mask @@ -918,6 +920,60 @@ static void phylink_resolve_an_pause(struct phylink_link_state *state) } } +/** +* phylink_release_pcs - Removes a PCS from the phylink PCS available list +* @pcs: a pointer to the phylink_pcs struct to be released +* +* This function release a PCS from the phylink PCS available list if +* actually in use. It also refreshes the supported interfaces of the +* phylink instance by copying the supported interfaces from the phylink +* conf and merging the supported interfaces of the remaining available PCS +* in the list and trigger a resolve. +*/ +void phylink_release_pcs (struct phylink_pcs *pcs) +{ +struct phylink *pl; + +ASSERT_RTNL (); + + pl = pcs->phylink; +if (!pl) +return ; + +mutex_lock (&pl->state_mutex); + +list_del (&pcs->list); + pcs->phylink =NULL ; + +/* +* Check if we are removing the PCS currently +* in use by phylink. 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) { +phylink_link_down (pl); +phylink_pcs_disable (pl->pcs); + + pl->force_major_config = true; + pl->pcs =NULL ; + }
drivers/net/phy/phylink.c static void phylink_resolve_an_pause(struct phylink_link_state *state) · line 971
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+ +mutex_unlock (&pl->state_mutex); + +/* Refresh supported interfaces */ +phy_interface_copy (pl->supported_interfaces, + pl->config->supported_interfaces); +list_for_each_entry (pcs, &pl->pcs_list, list) +phy_interface_or (pl->supported_interfaces, + pl->supported_interfaces, + pcs->supported_interfaces);
I've given more thought to that 'supported_interfaces' thing. This patchset redefines the meaning of
pl->config->supported_interfaces
Currently, it's filled by the MAC driver and means "Every interface we can support, including the ones provided by PCSs that we can use with this MAC".
It now becomes "Every interface we support without needing a PCS", at least the way I understand that.
It's not an error in your code, but I think this is worth documenting somewhere as this changes one the things that's already fairly error-prone in new drivers.
I don't know to what extent people use that, be we have a porting guide that explains how to use phylink in a MAC driver, maybe an update in there would be nice as well :
Maxime
Re: [RFC PATCH net-next v8 03/12] net: phylink: add phylink_release_pcs() to externally release a PCS
Quoted 96 lines · Maxime Chevallier
Hello Christian,
Quoted 79 lines · Christian Marangi
Add
phylink_release_pcs()to externally release a PCS from a phylink instance. This can be used to handle case when a single PCS needs to be removed and the phylink instance needs to be refreshed.On calling
phylink_release_pcs(), the PCS will be removed from the phylink internal PCS list and the phylinksupported_interfacesvalue is reparsed with the remaining PCS interfaces.Also a phylink resolve is triggered to handle the PCS removal.
The flag
force_major_configis 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 amajor_configis needed to propagae the configuration change. With this option enabled we also forcemac_configeven if the PHY link is not up for the in-band case.Signed-off-by Christian Marangi <[email protected]>--- drivers/net/phy/phylink.c | 56 +++++++++++++++++++++++++++++++++++++++ include/linux/phylink.h | 2 ++ 2 files changed, 58 insertions(+)drivers/net/phy/phylink.c +41
-0@@ -158,6 +158,8 @@ static const phy_interface_t phylink_sfp_interface_preference[] = {static DECLARE_PHY_INTERFACE_MASK (phylink_sfp_interfaces);static void phylink_run_resolve (struct phylink *pl); +static void phylink_link_down (struct phylink *pl); +static void phylink_pcs_disable (struct phylink_pcs *pcs);/** * phylink_set_port_modes() - set the port type modes in the ethtool mask @@ -918,6 +920,60 @@ static void phylink_resolve_an_pause(struct phylink_link_state *state) } } +/** +* phylink_release_pcs - Removes a PCS from the phylink PCS available list +* @pcs: a pointer to the phylink_pcs struct to be released +* +* This function release a PCS from the phylink PCS available list if +* actually in use. It also refreshes the supported interfaces of the +* phylink instance by copying the supported interfaces from the phylink +* conf and merging the supported interfaces of the remaining available PCS +* in the list and trigger a resolve. +*/ +void phylink_release_pcs (struct phylink_pcs *pcs) +{ +struct phylink *pl; + +ASSERT_RTNL (); + + pl = pcs->phylink; +if (!pl) +return ; + +mutex_lock (&pl->state_mutex); + +list_del (&pcs->list); + pcs->phylink =NULL ; + +/* +* Check if we are removing the PCS currently +* in use by phylink. 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) { +phylink_link_down (pl); +phylink_pcs_disable (pl->pcs); + + pl->force_major_config = true; + pl->pcs =NULL ; + }drivers/net/phy/phylink.c static void phylink_resolve_an_pause(struct phylink_link_state *state) · line 971
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+ +mutex_unlock (&pl->state_mutex); + +/* Refresh supported interfaces */ +phy_interface_copy (pl->supported_interfaces, + pl->config->supported_interfaces); +list_for_each_entry (pcs, &pl->pcs_list, list) +phy_interface_or (pl->supported_interfaces, + pl->supported_interfaces, + pcs->supported_interfaces);I've given more thought to that '
supported_interfaces' thing. This patchset redefines the meaning of
Quoted 9 lines
pl->config->supported_interfacesCurrently, it's filled by the MAC driver and means "Every interface we can support, including the ones provided by PCSs that we can use with this MAC".
It now becomes "Every interface we support without needing a PCS", at least the way I understand that.
Wait but with the current code using the OR logic, it still follows "Every interface we can support...". The modes that needs a PCS are specificed with the pcs_interfaces mask in phylink_config.
The late add and release operates on the phylink supported_interfaces ONLY when the MAC didn't specify support for it (by removing it as only the PCS will declare support for it)
The confusion is present because everything is validated later on major_config so those supported_interfaces are just an HINT that are later verified with get_caps and with the pcs_validate OPs.
Adding the supported_interfaces to phylink is really to keep an original reference of the value. This is to address a pattern I have notice where the MAC driver always OR the interfaces with the one supported by the PCS. (I remember it was pointed out by Russell)
But I'm more than open to discussion as this is something marginal to the whole implementation, I'm also questioning if this OR is actually useful to anything on the nth tought on this.
One thing that I notice is that parsing this early with AND might be problematic at phylink_create, but I still have to evaluate that.
My take is that would be good to have some review also on the other logic as I think I reached a point where Sashiko starts to comments on more or less unreal problem.
Quoted 13 lines
It's not an error in your code, but I think this is worth documenting somewhere as this changes one the things that's already fairly error-prone in new drivers.
I don't know to what extent people use that, be we have a porting guide that explains how to use phylink in a MAC driver, maybe an update in there would be nice as well :
Maxime
Signature · 1 line
Ansuel
Re: [RFC PATCH net-next v8 03/12] net: phylink: add phylink_release_pcs() to externally release a PCS
Hi Christian,
Quoted 103 lines · Christian Marangi
Quoted 102 lines · Maxime Chevallier
Hello Christian,
Quoted 79 lines · Christian Marangi
Add
phylink_release_pcs()to externally release a PCS from a phylink instance. This can be used to handle case when a single PCS needs to be removed and the phylink instance needs to be refreshed.On calling
phylink_release_pcs(), the PCS will be removed from the phylink internal PCS list and the phylinksupported_interfacesvalue is reparsed with the remaining PCS interfaces.Also a phylink resolve is triggered to handle the PCS removal.
The flag
force_major_configis 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 amajor_configis needed to propagae the configuration change. With this option enabled we also forcemac_configeven if the PHY link is not up for the in-band case.Signed-off-by Christian Marangi <[email protected]>--- drivers/net/phy/phylink.c | 56 +++++++++++++++++++++++++++++++++++++++ include/linux/phylink.h | 2 ++ 2 files changed, 58 insertions(+)drivers/net/phy/phylink.c +41
-0@@ -158,6 +158,8 @@ static const phy_interface_t phylink_sfp_interface_preference[] = {static DECLARE_PHY_INTERFACE_MASK (phylink_sfp_interfaces);static void phylink_run_resolve (struct phylink *pl); +static void phylink_link_down (struct phylink *pl); +static void phylink_pcs_disable (struct phylink_pcs *pcs);/** * phylink_set_port_modes() - set the port type modes in the ethtool mask @@ -918,6 +920,60 @@ static void phylink_resolve_an_pause(struct phylink_link_state *state) } } +/** +* phylink_release_pcs - Removes a PCS from the phylink PCS available list +* @pcs: a pointer to the phylink_pcs struct to be released +* +* This function release a PCS from the phylink PCS available list if +* actually in use. It also refreshes the supported interfaces of the +* phylink instance by copying the supported interfaces from the phylink +* conf and merging the supported interfaces of the remaining available PCS +* in the list and trigger a resolve. +*/ +void phylink_release_pcs (struct phylink_pcs *pcs) +{ +struct phylink *pl; + +ASSERT_RTNL (); + + pl = pcs->phylink; +if (!pl) +return ; + +mutex_lock (&pl->state_mutex); + +list_del (&pcs->list); + pcs->phylink =NULL ; + +/* +* Check if we are removing the PCS currently +* in use by phylink. 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) { +phylink_link_down (pl); +phylink_pcs_disable (pl->pcs); + + pl->force_major_config = true; + pl->pcs =NULL ; + }drivers/net/phy/phylink.c static void phylink_resolve_an_pause(struct phylink_link_state *state) · line 971
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+ +mutex_unlock (&pl->state_mutex); + +/* Refresh supported interfaces */ +phy_interface_copy (pl->supported_interfaces, + pl->config->supported_interfaces); +list_for_each_entry (pcs, &pl->pcs_list, list) +phy_interface_or (pl->supported_interfaces, + pl->supported_interfaces, + pcs->supported_interfaces);I've given more thought to that '
supported_interfaces' thing. This patchset redefines the meaning of
pl->config->supported_interfacesCurrently, it's filled by the MAC driver and means "Every interface we can support, including the ones provided by PCSs that we can use with this MAC".
Quoted 7 lines
Quoted 3 lines
It now becomes "Every interface we support without needing a PCS", at least the way I understand that.
Wait but with the current code using the OR logic, it still follows "Every interface we can support...". The modes that needs a PCS are specificed with the
pcs_interfacesmask inphylink_config.
you current code is correct, I was mostly concerned about the doc that goes along with it :)
So in the end, we'd have something like (simplified):
pl->config.supported_interfaces = RGMII_xx | SGMII | 1000BaseX pl->config.pcs_interfaces = SGMII | 1000BaseX
pcs->supported_interface = SGMII| 1000BaseX
correct ?
Quoted 18 lines
The late add and release operates on the phylink
supported_interfacesONLY when the MAC didn't specify support for it (by removing it as only the PCS will declare support for it)The confusion is present because everything is validated later on
major_configso thosesupported_interfacesare just an HINT that are later verified withget_capsand with thepcs_validateOPs.Adding the
supported_interfacesto phylink is really to keep an original reference of the value. This is to address a pattern I have notice where the MAC driver always OR the interfaces with the one supported by the PCS. (I remember it was pointed out by Russell)But I'm more than open to discussion as this is something marginal to the whole implementation, I'm also questioning if this OR is actually useful to anything on the nth tought on this.
Quoted 6 lines
One thing that I notice is that parsing this early with AND might be problematic at
phylink_create, but I still have to evaluate that.My take is that would be good to have some review also on the other logic as I think I reached a point where Sashiko starts to comments on more or less unreal problem.
True, TBH all the fwnode part is something I'm a bit less familiar with though so maybe someone else can browse through that.
FWIW, I've tested that whole series on a board that has "legacy" PCS board that has mvpp2 and 2 possible PCSs, and it seems to work fine so no regressions there :)
A side note with the "legacy" naming, I'd rather have it called "built-in" or something like that, I don't see a clear path to porting the existing code to fwnode without breaking DT compat, as it's likely we'll have to remove the PCS register ranges out of the MAC's range.
Thanks for this work anyway, this is great !
Maxime
Re: [RFC PATCH net-next v8 03/12] net: phylink: add phylink_release_pcs() to externally release a PCS
Quoted 110 lines · Maxime Chevallier
Hi Christian,
Quoted 107 lines · Christian Marangi
Quoted 105 lines · Maxime Chevallier
Hello Christian,
Quoted 79 lines · Christian Marangi
Add
phylink_release_pcs()to externally release a PCS from a phylink instance. This can be used to handle case when a single PCS needs to be removed and the phylink instance needs to be refreshed.On calling
phylink_release_pcs(), the PCS will be removed from the phylink internal PCS list and the phylinksupported_interfacesvalue is reparsed with the remaining PCS interfaces.Also a phylink resolve is triggered to handle the PCS removal.
The flag
force_major_configis 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 amajor_configis needed to propagae the configuration change. With this option enabled we also forcemac_configeven if the PHY link is not up for the in-band case.Signed-off-by Christian Marangi <[email protected]>--- drivers/net/phy/phylink.c | 56 +++++++++++++++++++++++++++++++++++++++ include/linux/phylink.h | 2 ++ 2 files changed, 58 insertions(+)drivers/net/phy/phylink.c +41
-0@@ -158,6 +158,8 @@ static const phy_interface_t phylink_sfp_interface_preference[] = {static DECLARE_PHY_INTERFACE_MASK (phylink_sfp_interfaces);static void phylink_run_resolve (struct phylink *pl); +static void phylink_link_down (struct phylink *pl); +static void phylink_pcs_disable (struct phylink_pcs *pcs);/** * phylink_set_port_modes() - set the port type modes in the ethtool mask @@ -918,6 +920,60 @@ static void phylink_resolve_an_pause(struct phylink_link_state *state) } } +/** +* phylink_release_pcs - Removes a PCS from the phylink PCS available list +* @pcs: a pointer to the phylink_pcs struct to be released +* +* This function release a PCS from the phylink PCS available list if +* actually in use. It also refreshes the supported interfaces of the +* phylink instance by copying the supported interfaces from the phylink +* conf and merging the supported interfaces of the remaining available PCS +* in the list and trigger a resolve. +*/ +void phylink_release_pcs (struct phylink_pcs *pcs) +{ +struct phylink *pl; + +ASSERT_RTNL (); + + pl = pcs->phylink; +if (!pl) +return ; + +mutex_lock (&pl->state_mutex); + +list_del (&pcs->list); + pcs->phylink =NULL ; + +/* +* Check if we are removing the PCS currently +* in use by phylink. 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) { +phylink_link_down (pl); +phylink_pcs_disable (pl->pcs); + + pl->force_major_config = true; + pl->pcs =NULL ; + }drivers/net/phy/phylink.c static void phylink_resolve_an_pause(struct phylink_link_state *state) · line 971
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+ +mutex_unlock (&pl->state_mutex); + +/* Refresh supported interfaces */ +phy_interface_copy (pl->supported_interfaces, + pl->config->supported_interfaces); +list_for_each_entry (pcs, &pl->pcs_list, list) +phy_interface_or (pl->supported_interfaces, + pl->supported_interfaces, + pcs->supported_interfaces);I've given more thought to that '
supported_interfaces' thing. This patchset redefines the meaning of
pl->config->supported_interfacesCurrently, it's filled by the MAC driver and means "Every interface we can support, including the ones provided by PCSs that we can use with this MAC".
It now becomes "Every interface we support without needing a PCS", at least the way I understand that.
Quoted 6 lines
Quoted 3 lines
Wait but with the current code using the OR logic, it still follows "Every interface we can support...". The modes that needs a PCS are specificed with the
pcs_interfacesmask inphylink_config.you current code is correct, I was mostly concerned about the doc that goes along with it :)
Oh yep thanks, I will update also that entry. Also maybe check the .rst introduced and tell me if something is confusing or badly described.
Quoted 10 lines
So in the end, we'd have something like (simplified):
pl->config.supported_interfaces=RGMII_xx| SGMII | 1000BaseXpl->config.pcs_interfaces= SGMII | 1000BaseX
pcs->supported_interface= SGMII| 1000BaseXcorrect ?
Correct. phylink_config describing that for SGMII and 1000BaseX a PCS is required and the related PCS declaring support for those modes.
Code will skip searching for a PCS for RGMII.
Quoted 28 lines
Quoted 18 lines
The late add and release operates on the phylink
supported_interfacesONLY when the MAC didn't specify support for it (by removing it as only the PCS will declare support for it)The confusion is present because everything is validated later on
major_configso thosesupported_interfacesare just an HINT that are later verified withget_capsand with thepcs_validateOPs.Adding the
supported_interfacesto phylink is really to keep an original reference of the value. This is to address a pattern I have notice where the MAC driver always OR the interfaces with the one supported by the PCS. (I remember it was pointed out by Russell)But I'm more than open to discussion as this is something marginal to the whole implementation, I'm also questioning if this OR is actually useful to anything on the nth tought on this.
Quoted 6 lines
One thing that I notice is that parsing this early with AND might be problematic at
phylink_create, but I still have to evaluate that.My take is that would be good to have some review also on the other logic as I think I reached a point where Sashiko starts to comments on more or less unreal problem.
True, TBH all the fwnode part is something I'm a bit less familiar with though so maybe someone else can browse through that.
Quoted 8 lines
FWIW, I've tested that whole series on a board that has "legacy" PCS board that has mvpp2 and 2 possible PCSs, and it seems to work fine so no regressions there :)
A side note with the "legacy" naming, I'd rather have it called "built-in" or something like that, I don't see a clear path to porting the existing code to fwnode without breaking DT compat, as it's likely we'll have to remove the PCS register ranges out of the MAC's range.
I think also built-in might be confusing as a SoC might have a PCS built-in but just as a separate device on a different register map.
A better description might be "externally-managed" but that is very long...
Also the use of fwnode and DT is just to reference them and use the normal helper but the implementation is liberal on custom implementation for the current code.
Everything is around the pcs_interface mask, num_possible_pcs and the fill_available_pcs.
Nobody stops from implementing custom parser in fill_available_pcs and return a PCS pointer created directly by the MAC. (In such case late and release won't be needed as everything is present when phylink_create is called)
On OpenWrt we migrated every SoC that used ""exotic"" pcs reference (Qcom, Realtek, MTK, Airoha) but I don't have a board that used something like the 'pcs' property.
Quoted 4 lines
Thanks for this work anyway, this is great !
Maxime
Signature · 1 line
Ansuel
Re: [RFC PATCH net-next v8 03/12] net: phylink: add phylink_release_pcs() to externally release a PCS
Quoted 4 lines · Christian Marangi
Nobody stops from implementing custom parser in
fill_available_pcsand return a PCS pointer created directly by the MAC. (In such case late and release won't be needed as everything is present whenphylink_createis called)
Ah alright :)
So my only request then would be to update the phylink porting guide to at least point to the PCS documentation you're adding to say that the select_pcs() op is deprecated :
Thanks,
Maxime
Quoted 1 line
RFC PATCH v8 4/12 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 .get function.
PCS driver will have to call fwnode_pcs_add_provider() and pass the firmware node pointer and a xlate function to return the correct PCS for the passed #pcs-cells.
This will register the PCS in a global list of providers so that consumer can access it.
The consumer will then use fwnode_pcs_get() to get the actual PCS by passing the firmware node pointer and the index for #pcs-cells.
For a simple implementation where #pcs-cells is 0 and the PCS driver expose a single PCS, the xlate function fwnode_pcs_simple_get() is provided.
For an advanced implementation a custom xlate function is required.
On removal the PCS driver should first delete itself from the provider list using fwnode_pcs_del_provider() and then call phylink_release_pcs() on every PCS the driver provides.
Generic functions fwnode_phylink_pcs_count() and fwnode_phylink_pcs_parse() are provided for MAC driver that will declare PCS in DT (or ACPI).
Function fwnode_phylink_pcs_count() will parse "pcs-handle" property and will return the number of PCS entries described in the passed firmware node.
Function fwnode_phylink_pcs_parse() will parse "pcs-handle" property and fill the passed available_pcs array with the available PCS found up to passed num_pcs value. It's worth to mention that this function will ignore PCS that still needs to be probed (returning -ENODEV) and such PCS won't be added to the available_pcs array.
--- drivers/net/pcs/Kconfig | 6 + drivers/net/pcs/Makefile | 1 + drivers/net/pcs/pcs.c | 212 +++++++++++++++++++++++++++++++ include/linux/pcs/pcs-provider.h | 41 ++++++ include/linux/pcs/pcs.h | 75 +++++++++++ 5 files changed, 335 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) + help + Firmware node PCS accessors + config PCS_XPCS tristate "Synopsys DesignWare Ethernet XPCS" select PHYLINK
drivers/net/pcs/Makefile +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 # Makefile for Linux PCS drivers +obj-$(CONFIG_FWNODE_PCS) += pcs.o pcs_xpcs-$(CONFIG_PCS_XPCS) := pcs-xpcs.o pcs-xpcs-plat.o \ pcs-xpcs-nxp.o pcs-xpcs-wx.o
drivers/net/pcs/pcs.c new file +212 -0
212 lines not on this page · raw message
include/linux/pcs/pcs-provider.h new file +41 -0
41 lines not on this page · raw message
include/linux/pcs/pcs.h new file +75 -0
75 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH net-next v8 04/12] net: pcs: implement Firmware node support for PCS driver
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The PCS provider framework lacks module reference counting, exposing consumers to Use-After-Free…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The PCS provider framework lacks module reference counting, exposing consumers to Use-After-Free crashes during concurrent driver removal.
- [Medium] fwnode_phylink_pcs_count() silently truncates the PCS count on Device Tree errors instead of propagating them.
- [Low] Dead code and illogical condition in fwnode_parse_pcsspec.
--
commit 31d56c9668ba31e1d161e00a93f5481143830f95
Author: Christian Marangi <[email protected]>
net: pcs: implement Firmware node support for PCS driver
Implement the foundation of Firmware node support for PCS driver. This implements a simple Provider API where a PCS driver can expose multiple PCS with an xlate .get function.
Quoted 5 lines
drivers/net/pcs/pcs.c new file +0
-0
[ ... ]
Quoted 14 lines
drivers/net/pcs/pcs.c new file +14
-0+static int fwnode_parse_pcsspec (const struct fwnode_handle *fwnode, +int index,const char *name, +struct fwnode_reference_args *out_args) +{ +int ret; + +if (!fwnode) +return -EINVAL ; + +if (name) { + index =fwnode_property_match_string (fwnode,"pcs-names" , + name); +if (index <0 ) +return index;
drivers/net/pcs/pcs.c line 106
Quoted 10 lines
drivers/net/pcs/pcs.c new file +10
-0+ } + + ret =fwnode_property_get_reference_args (fwnode,"pcs-handle" , +"#pcs-cells" , +-1 , index, out_args); +if (ret || (name && index <0 )) +return ret; + +return 0 ; +}
[Severity: Low]
Is the condition (name && index < 0) needed here?
The earlier check:
if (name) {
index = fwnode_property_match_string(fwnode, "pcs-names",
name);
if (index < 0)
return index;
}already returns early if name is present and index is less than zero, so index will always be >= 0 when we reach this final check.
[ ... ]
Quoted 11 lines
drivers/net/pcs/pcs.c new file +11
-0+static struct phylink_pcs * +fwnode_pcs_get_from_pcsspec (struct fwnode_reference_args *pcsspec) +{ +struct fwnode_pcs_provider *provider; +struct phylink_pcs *pcs =ERR_PTR (-ENODEV ); + +if (!pcsspec) +return ERR_PTR (-EINVAL ); + +mutex_lock (&fwnode_pcs_mutex); +list_for_each_entry (provider, &fwnode_pcs_providers, link) {
drivers/net/pcs/pcs.c line 128
Quoted 10 lines
drivers/net/pcs/pcs.c new file +10
-0+if (provider->fwnode == pcsspec->fwnode) { + pcs = provider->get (pcsspec, provider->data); +if (!IS_ERR (pcs)) +break ; + } + } +mutex_unlock (&fwnode_pcs_mutex); + +return pcs; +}
[Severity: High]
Is there a way to prevent the provider module from being unloaded while the consumer is actively using the PCS?
It looks like this framework doesn't capture the module owner (e.g. by passing THIS_MODULE to fwnode_pcs_add_provider) or use try_module_get() when handing the pointer to the consumer in fwnode_pcs_get_from_pcsspec().
If a consumer calls fwnode_pcs_get() but hasn't yet linked the PCS to its pl->pcs_list (meaning pcs->phylink is still NULL), and the provider unbinds during this window, phylink_release_pcs() might act as a no-op. The provider could free the PCS memory while the consumer still holds the pointer, leading to a use-after-free crash.
[ ... ]
Quoted 10 lines
drivers/net/pcs/pcs.c new file +10
-0+unsigned int fwnode_phylink_pcs_count (struct fwnode_handle *fwnode) +{ +struct fwnode_reference_args out_args; +int index =0 ; +int ret; + +while (true) { + ret =fwnode_property_get_reference_args (fwnode,"pcs-handle" , +"#pcs-cells" , +-1 , index, &out_args);
drivers/net/pcs/pcs.c line 172
Quoted 10 lines
drivers/net/pcs/pcs.c new file +10
-0+/* We expect to reach an -ENOENT error while counting */ +if (ret) +break ; + +fwnode_handle_put (out_args.fwnode); + index++; + } + +return index; +}
[Severity: Medium]
Does this function silently truncate the count on malformed device tree nodes?
If fwnode_property_get_reference_args() returns a critical error like -EINVAL, the loop breaks and returns the current index. Because the function returns an unsigned int, negative error codes aren't propagated to the caller.
Could this mask actual configuration errors and cause the MAC driver to proceed with an incomplete PCS configuration instead of failing the probe?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
RFC PATCH v8 5/12 net: phylink: support late PCS provider attach
In reply to Christian MarangiAdd support for late PCS provider attachment to a phylink instance. This works by creating a global notifier for the PCS provider and making each phylink instance that makes use of fwnode subscribe to this notifier.
The PCS notifier will emit the event FWNODE_PCS_PROVIDER_ADD every time a new PCS provider is added.
phylink will then react to this event and will call the new function fwnode_phylink_pcs_get_from_fwnode() that will check if the PCS fwnode provided by the event is present in the pcs-handle property of the phylink instance.
If a related PCS is found, then such PCS is added to the phylink instance PCS list.
Then we link the PCS to the phylink instance and we refresh the supported interfaces of the phylink instance.
Finally we check if we are in a major_config_failed scenario and trigger an interface reconfiguration in the next phylink resolve.
In the example scenario where the link was previously torn down due to removal of PCS, the link will be established again as the PCS came back and is now available to phylink.
--- drivers/net/pcs/pcs.c | 49 +++++++++++++++++++++++++++++ drivers/net/phy/phylink.c | 60 +++++++++++++++++++++++++++++++++++- include/linux/pcs/pcs.h | 65 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+), 1 deletion(-)
drivers/net/pcs/pcs.c +49 -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_get (struct fwnode_reference_args *pcsspec,void *data)
@@ -55,6 +68,10 @@ int fwnode_pcs_add_provider(struct fwnode_handle *fwnode,fwnode_dev_initialized (fwnode, true); +blocking_notifier_call_chain (&fwnode_pcs_notify_list, +FWNODE_PCS_PROVIDER_ADD , + fwnode); +return 0 ; }EXPORT_SYMBOL_GPL (fwnode_pcs_add_provider);
@@ -150,6 +167,38 @@ struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, unsigned int in }EXPORT_SYMBOL_GPL (fwnode_pcs_get); +struct phylink_pcs * +fwnode_phylink_pcs_get_from_fwnode (struct fwnode_handle *fwnode, +struct fwnode_handle *pcs_fwnode) +{ +struct fwnode_reference_args pcsspec; +int index =0 ; +int ret; + +/* Loop until we find a matching PCS node or +* fwnode_parse_pcsspec() returns error +* if we don't have any other PCS reference to check. +*/ +while (true) { + ret =fwnode_parse_pcsspec (fwnode, index,NULL , &pcsspec); +if (ret) +return ERR_PTR (ret); + +/* Exit loop if we found the matching PCS node */ +if (pcsspec.fwnode == pcs_fwnode) { +fwnode_handle_put (pcsspec.fwnode); +break ; + } + +/* Check the next PCS reference */ +fwnode_handle_put (pcsspec.fwnode); + index++; + } + +return fwnode_pcs_get (fwnode, index); +} +EXPORT_SYMBOL_GPL (fwnode_phylink_pcs_get_from_fwnode); +unsigned int fwnode_phylink_pcs_count (struct fwnode_handle *fwnode) {struct fwnode_reference_args out_args;
drivers/net/phy/phylink.c +59 -1
93 lines not on this page · raw message
include/linux/pcs/pcs.h +65 -0
90 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH net-next v8 05/12] net: phylink: support late PCS provider attach
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [Critical] Unconditional `list_add()` in `pcs_provider_notify()` leads to list corruption for shared PCS…
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [Critical] Unconditional list_add() in pcs_provider_notify() leads to list corruption for shared PCS providers.
- [Critical] Missing PCS removal handling leads to Use-After-Free in pl->pcs_list.
- [High] TOCTOU race in phylink_create() allows a late PCS provider to be permanently missed.
- [High] Incorrect fwnode used for pcs-handle lookup silently breaks late attach on multi-port devices (e.g. DSA).
- [High] Late attach updates supported interfaces but leaves ethtool linkmodes stale.
- [High] pcs_provider_notify() ignores subsequent PCS instances if a MAC requires multiple PCSs from the same provider fwnode.
- [High] Self-deadlock due to unconditional rtnl_lock() acquisition in the PCS notifier callback.
--
commit 101d55ca1c5df8e03923c9d69f42065ce0d1aab9
Author: Christian Marangi <[email protected]>
net: phylink: support late PCS provider attach
This commit adds support for late PCS provider attachment in phylink. It creates a global notifier chain for PCS providers and allows phylink instances to subscribe and dynamically add PCS providers when they become available.
Quoted 4 lines
drivers/net/pcs/pcs.c +0
-0
[ ... ]
Quoted 16 lines
drivers/net/pcs/pcs.c +12
-0@@ -150,6 +167,38 @@ struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, unsigned int in }EXPORT_SYMBOL_GPL (fwnode_pcs_get); +struct phylink_pcs * +fwnode_phylink_pcs_get_from_fwnode (struct fwnode_handle *fwnode, +struct fwnode_handle *pcs_fwnode) +{ +struct fwnode_reference_args pcsspec; +int index =0 ; +int ret; + +/* Loop until we find a matching PCS node or +* fwnode_parse_pcsspec() returns error +* if we don't have any other PCS reference to check. +*/
drivers/net/pcs/pcs.c struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, unsigned int in · line 191
Quoted 10 lines
drivers/net/pcs/pcs.c +10
-0+while (true) { + ret =fwnode_parse_pcsspec (fwnode, index,NULL , &pcsspec); +if (ret) +return ERR_PTR (ret); + +/* Exit loop if we found the matching PCS node */ +if (pcsspec.fwnode == pcs_fwnode) { +fwnode_handle_put (pcsspec.fwnode); +break ; + }
[Severity: High]
Does breaking out of the loop on the first match ignore subsequent PCS instances if a MAC requires multiple PCSs from the same provider?
If a phylink instance is configured to use multiple PCSs from the same provider, it seems the subsequent indices will never be discovered since the notifier only fires once per provider fwnode.
Quoted 17 lines
drivers/net/pcs/pcs.c +10
-0+ +/* Check the next PCS reference */ +fwnode_handle_put (pcsspec.fwnode); + index++; + } + +return fwnode_pcs_get (fwnode, index); +} +EXPORT_SYMBOL_GPL (fwnode_phylink_pcs_get_from_fwnode); +unsigned int fwnode_phylink_pcs_count (struct fwnode_handle *fwnode) {struct fwnode_reference_args out_args;drivers/net/phy/phylink.c +0
-0
[ ... ]
drivers/net/phy/phylink.c static int phylink_fill_available_pcs(struct phylink *pl, · line 2012
Quoted 12 lines
drivers/net/phy/phylink.c +8
-0@@ -2000,6 +2002,53 @@ static int phylink_fill_available_pcs(struct phylink *pl,return ret; } +static int pcs_provider_notify (struct notifier_block *self, +unsigned long val,void *data) +{ + struct phylink *pl = container_of(self, struct phylink, fwnode_pcs_nb); +struct fwnode_handle *pcs_fwnode = data; +struct phylink_pcs *pcs; + +rtnl_lock ();
[Severity: High]
Can this unconditional lock acquisition cause a self-deadlock?
Because the provider addition is emitted via a synchronous blocking notifier, if a network driver or subsystem calls fwnode_pcs_add_provider() from a context that already holds rtnl_lock(), wouldn't this attempt to acquire the lock a second time?
drivers/net/phy/phylink.c static int phylink_fill_available_pcs(struct phylink *pl, · line 2018
Quoted 6 lines
drivers/net/phy/phylink.c +6
-0+ +/* Check if the just added PCS provider is +* in the phylink instance pcs-handle property. +*/ + pcs =fwnode_phylink_pcs_get_from_fwnode (dev_fwnode (pl->config->dev), + pcs_fwnode);
[Severity: High]
Does this use the correct fwnode for multi-port devices like DSA switches?
It looks like pl->config->dev points to the parent switch's device, not the individual port. If the port-specific fwnode passed to phylink_create() isn't used here, will this search the switch's root device node instead and silently fail to find the pcs-handle?
drivers/net/phy/phylink.c static int phylink_fill_available_pcs(struct phylink *pl, · line 2028
Quoted 10 lines
drivers/net/phy/phylink.c +10
-0+if (IS_ERR (pcs)) { +rtnl_unlock (); +return NOTIFY_DONE ; + } + +/* Add the PCS */ +mutex_lock (&pl->state_mutex); + +/* Link PCS with phylink */ +list_add (&pcs->list, &pl->pcs_list);
[Severity: Critical]
Could this unconditional list addition corrupt the list if a single PCS provider is shared by multiple phylink instances?
If a single PCS provider is referenced by multiple ports on a switch, pcs_provider_notify() will trigger for each phylink instance. Since the list head inside struct phylink_pcs can only be part of one list at a time, won't the second list_add() corrupt the first phylink instance's pcs_list?
drivers/net/phy/phylink.c static int phylink_fill_available_pcs(struct phylink *pl, · line 2037
Quoted 9 lines
drivers/net/phy/phylink.c +9
-0+ pcs->phylink = pl; + +/* Refresh supported interfaces */ +phy_interface_copy (pl->supported_interfaces, + pl->config->supported_interfaces); +list_for_each_entry (pcs, &pl->pcs_list, list) +phy_interface_or (pl->supported_interfaces, + pl->supported_interfaces, + pcs->supported_interfaces);
[Severity: High]
Is a call to phylink_validate() needed here to update the ethtool linkmodes?
This updates the supported interfaces mask with the PCS capabilities, but without recalculating pl->supported and pl->link_config.advertising, will the device fail to advertise or negotiate the new speeds and duplexes enabled by the late-attached PCS?
Quoted 13 lines
drivers/net/phy/phylink.c +13
-0+ +/* Force an interface reconfig if major config fail */ +if (pl->major_config_failed) + pl->force_major_config = true; + +mutex_unlock (&pl->state_mutex); + +rtnl_unlock (); + +phylink_run_resolve (pl); + +return NOTIFY_OK ; +}
[ ... ]
drivers/net/phy/phylink.c struct phylink *phylink_create(struct phylink_config *config, · line 2180
Quoted 9 lines
drivers/net/phy/phylink.c +5
-0@@ -2124,6 +2173,12 @@ struct phylink *phylink_create(struct phylink_config *config,if (ret <0 ) goto unlink_pcs_list; +/* Register notifier for late PCS attach */ +if (!phy_interface_empty (config->pcs_interfaces)) { + pl->fwnode_pcs_nb.notifier_call = pcs_provider_notify; +register_fwnode_pcs_notifier (&pl->fwnode_pcs_nb); + }
[Severity: High]
Is there a race condition between the initial scan for available PCS providers and the registration of this late-attach notifier?
In phylink_create(), the initial scan happens here:
ret = phylink_fill_available_pcs(pl, config); if (ret < 0) goto free_pl;
If a PCS provider driver probes and triggers the provider add notification after phylink_fill_available_pcs() but before register_fwnode_pcs_notifier() is called, won't the notification be permanently missed?
Quoted 9 lines
drivers/net/phy/phylink.c +1
-0+return pl; unlink_pcs_list:include/linux/pcs/pcs.h +0
-0
include/linux/pcs/pcs.h line 9
Quoted 6 lines
include/linux/pcs/pcs.h +3
-0@@ -4,7 +4,36 @@#include <linux/phylink.h> +enum fwnode_pcs_notify_event { +FWNODE_PCS_PROVIDER_ADD , +};
[Severity: Critical]
Is there a missing mechanism to handle PCS provider removal?
Without a corresponding remove event, when a PCS provider driver is unbound and frees the struct phylink_pcs, phylink is not notified. Since phylink retains the freed PCS pointer in pl->pcs_list, won't subsequent operations like phylink_resolve() iterate over the list and trigger a use-after-free?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
RFC PATCH v8 6/12 net: Document PCS subsystem
In reply to Christian MarangiAdd extensive documentation of the new PCS subsystem and the fwnode implementation with producer/consumer API.
--- Documentation/networking/index.rst | 1 + Documentation/networking/pcs.rst | 229 +++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 Documentation/networking/pcs.rst
Documentation/networking/index.rst +1 -0
@@ -28,6 +28,7 @@ Contents: net_failover page_pool phy + pcs sfp-phylink alias bridge
Documentation/networking/pcs.rst new file +229 -0
229 lines not on this page · raw message
Signature · 1 line
2.53.0
RFC PATCH v8 7/12 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
@@ -9593,6 +9593,15 @@ F: include/uapi/linux/if_bridge.h F: include/linux/netfilter_bridge/ F: net/bridge/ +ETHERNET PCS SUBSYSTEM +M: Christian Marangi <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/networking/pcs.rst +F: drivers/net/pcs/pcs.c +F: include/linux/pcs/pcs-provider.h +F: include/linux/pcs/pcs.h + ETHERNET PHY LIBRARY M: Andrew Lunn <[email protected]> M: Heiner Kallweit <[email protected]>
Signature · 1 line
2.53.0
RFC PATCH v8 8/12 of: property: fw_devlink: Add support for "pcs-handle"
In reply to Christian MarangiAdd support for parsing PCS binding so that fw_devlink can enforce the dependency with Ethernet port.
--- drivers/of/property.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/of/property.c +2 -0
@@ -1392,6 +1392,7 @@ DEFINE_SIMPLE_PROP(access_controllers, "access-controllers", "#access-controller DEFINE_SIMPLE_PROP(pses, "pses", "#pse-cells") DEFINE_SIMPLE_PROP(power_supplies, "power-supplies", NULL) DEFINE_SIMPLE_PROP(mmc_pwrseq, "mmc-pwrseq", NULL) +DEFINE_SIMPLE_PROP(pcs_handle, "pcs-handle", "#pcs-cells") DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)DEFINE_SUFFIX_PROP (gpio,"-gpio" ,"#gpio-cells" )
@@ -1548,6 +1549,7 @@ static const struct supplier_bindings of_supplier_bindings[] = { { .parse_prop = parse_interrupts, }, { .parse_prop = parse_interrupt_map, }, { .parse_prop = parse_access_controllers, }, + { .parse_prop = parse_pcs_handle, }, { .parse_prop = parse_regulators, }, { .parse_prop = parse_gpio, }, { .parse_prop = parse_gpios, },
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH net-next v8 08/12] of: property: fw_devlink: Add support for "pcs-handle"
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] fwnode_property_get_reference_args() usage in newly added pcs.c causes -EINVAL on existing DTs…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] fwnode_property_get_reference_args() usage in newly added pcs.c causes -EINVAL on existing DTs due to lacking a cell count fallback. --
commit 91acc7e182a01f5abd0ae44dd757caf24a80257a
Author: Christian Marangi <[email protected]>
of: property: fw_devlink: Add support for "pcs-handle"
Add support for parsing PCS binding so that fw_devlink can enforce the dependency with Ethernet port.
drivers/of/property.c DEFINE_SIMPLE_PROP(access_controllers, "access-controllers", "#access-controller · line 1395
Quoted 9 lines
drivers/of/property.c +1
-0@@ -1392,6 +1392,7 @@ DEFINE_SIMPLE_PROP(access_controllers, "access-controllers", "#access-controller DEFINE_SIMPLE_PROP(pses, "pses", "#pse-cells") DEFINE_SIMPLE_PROP(power_supplies, "power-supplies", NULL) DEFINE_SIMPLE_PROP(mmc_pwrseq, "mmc-pwrseq", NULL) +DEFINE_SIMPLE_PROP (pcs_handle,"pcs-handle" ,"#pcs-cells" )
[Severity: Medium]
While this fw_devlink integration correctly falls back to 0 cells (via parse_prop_cells()) when #pcs-cells is missing, the newly added API in drivers/net/pcs/pcs.c does not.
In drivers/net/pcs/pcs.c:fwnode_parse_pcsspec(), the fallback cell count is hardcoded to -1:
ret = fwnode_property_get_reference_args(fwnode, "pcs-handle", "#pcs-cells", -1, index, out_args);
Similarly, in drivers/net/pcs/pcs.c:fwnode_phylink_pcs_count():
ret = fwnode_property_get_reference_args(fwnode, "pcs-handle", "#pcs-cells", -1, index, &out_args);
Passing -1 as the fallback cell count causes parsing to fail with -EINVAL on existing devicetrees where the pcs-handle property points to a node without #pcs-cells.
Could this create a regression for the pcs-handle binding, causing existing MAC drivers that migrate to this new API to fail discovery of their PCS?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
RFC PATCH v8 9/12 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
@@ -1038,6 +1038,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)
@@ -1739,6 +1745,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_up() - 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.53.0
[email protected]
AI reviewRe: [RFC PATCH net-next v8 09/12] net: phylink: add .pcs_link_down PCS OP
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Copy-paste error in the kernel-doc comment for the newly added `pcs_link_down` function incorrectly…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Copy-paste error in the kernel-doc comment for the newly added pcs_link_down function incorrectly names it pcs_link_up. --
commit 2d1df5800af742cd81f1378ff1f3f3bc3801c928
Author: Christian Marangi <[email protected]>
net: phylink: add .pcs_link_down PCS OP
Permits a PCS driver to define a specific operation to tear down the link between the MAC and the PCS. Adds a new .pcs_link_down callback which is called after .mac_link_down during phylink_link_down().
Quoted 4 lines
include/linux/phylink.h +0
-0
[ ... ]
include/linux/phylink.h void pcs_an_restart(struct phylink_pcs *pcs); · line 696
Quoted 6 lines
include/linux/phylink.h +2
-0@@ -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_up() - tear down link between MAC and PCS
[Severity: Low]
This isn't a bug, but should this kernel-doc block name the function pcs_link_down() instead of pcs_link_up()? It appears the function name was carried over from above, which might trigger kernel-doc warnings during documentation builds.
Quoted 11 lines
include/linux/phylink.h +8
-0+* @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
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
RFC PATCH v8 10/12 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.53.0
RFC PATCH v8 11/12 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 | 12 + drivers/net/pcs/airoha/Makefile | 7 + drivers/net/pcs/airoha/pcs-airoha-common.c | 1324 +++++++++++++ drivers/net/pcs/airoha/pcs-airoha.h | 1311 ++++++++++++ drivers/net/pcs/airoha/pcs-an7581.c | 2093 ++++++++++++++++++++ 7 files changed, 4751 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
@@ -41,4 +41,6 @@ config PCS_RZN1_MIIC Renesas RZ/N1, RZ/N2H, and RZ/T2H SoCs. This PCS converts MII to RMII/RGMII, or can be set in pass-through mode for MII. +source "drivers/net/pcs/airoha/Kconfig" + endmenu
drivers/net/pcs/Makefile +2 -0
@@ -9,3 +9,5 @@ obj-$(CONFIG_PCS_XPCS) += pcs_xpcs.o obj-$(CONFIG_PCS_LYNX) += pcs-lynx.o obj-$(CONFIG_PCS_MTK_LYNXI) += pcs-mtk-lynxi.o obj-$(CONFIG_PCS_RZN1_MIIC) += pcs-rzn1-miic.o + +obj-$(CONFIG_PCS_AIROHA) += airoha/
drivers/net/pcs/airoha/Kconfig new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config PCS_AIROHA + tristate + select FWNODE_PCS + +config PCS_AIROHA_AN7581 + tristate "Airoha AN7581 PCS driver" + select PCS_AIROHA + help + This module provides helper to phylink for managing the Airoha + AN7581 PCS for SoC Ethernet and PON SERDES.
drivers/net/pcs/airoha/Makefile new file +7 -0
@@ -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 +1324 -0
1,324 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 +2093 -0
335 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.53.0
Part of the diff is not shown here. View the raw message.
Re: [RFC PATCH net-next v8 11/12] net: pcs: airoha: add PCS driver for Airoha AN7581 SoC
Hi.
Quoted 17 lines · Christian Marangi
Add PCS driver for Airoha AN7581 SoC for Ethernet/PON/PCIe/USB SERDES and permit usage of external PHY or connected SFP cage. Supported modes are USXGMII, 10G-BASER, 2500BASE-X, 1000BASE-X and SGMII.
The driver probe and register the various needed registers and register as a PCS provider for fwnode usage.
Signed-off-by Christian Marangi <[email protected]>--- drivers/net/pcs/Kconfig | 2 + drivers/net/pcs/Makefile | 2 + drivers/net/pcs/airoha/Kconfig | 12 + drivers/net/pcs/airoha/Makefile | 7 + drivers/net/pcs/airoha/pcs-airoha-common.c | 1324 +++++++++++++ drivers/net/pcs/airoha/pcs-airoha.h | 1311 ++++++++++++ drivers/net/pcs/airoha/pcs-an7581.c | 2093 ++++++++++++++++++++ 7 files changed, 4751 insertions(+)
Quoted 5 lines
create mode 100644
drivers/net/pcs/airoha/Kconfigcreate mode 100644drivers/net/pcs/airoha/Makefilecreate mode 100644drivers/net/pcs/airoha/pcs-airoha-common.ccreate mode 100644drivers/net/pcs/airoha/pcs-airoha.hcreate mode 100644drivers/net/pcs/airoha/pcs-an7581.c
My comment that the files should be renamed now instead of later when support for other airoha platforms are added still stands. The common code is not common among other platforms (EN7523 as example).
MvH
Benjamin Larsson
[email protected]
AI reviewRe: [RFC PATCH net-next v8 11/12] net: pcs: airoha: add PCS driver for Airoha AN7581 SoC
In reply to Christian MarangiThank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider: - [High] Bringing up a second port on multi-port devices (e.g., PCIe) inadvertently resets the shared…
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
- [High] Bringing up a second port on multi-port devices (e.g., PCIe) inadvertently resets the shared global reset lines and analog PLLs, breaking the active link on the first port.
- [High] Sticky AN configuration bits break subsequent inband auto-negotiation configurations.
- [High] The driver acquires a generic PHY for USB ports but never initializes or powers it on.
- [High] Multiple regmap_read() calls fail to check for error codes, leading to uninitialized stack memory being consumed.
- [Medium] The RX signal detection loop polls the hardware status register 6 times back-to-back without any delay.
- [Medium] The return value of airoha_pcs_setup_scu() is ignored, which can lead to writing to hardware registers while the device is still held in reset.
- [Medium] Missing delay between reset assertion and deassertion.
- [Medium] Using read-modify-write on Write-1-to-Clear (W1C) interrupt status registers will unintentionally clear unhandled interrupts.
--
commit 1a7ffd4436659e2a919b7ea23223e2dc899f6f4c
Author: Christian Marangi <[email protected]>
Subject: net: pcs: airoha: add PCS driver for Airoha AN7581 SoC
This commit adds a new PCS driver for the Airoha AN7581 SoC, supporting Ethernet, PON, PCIe, and USB SERDES. It registers as a PCS provider for fwnode usage and supports various modes including USXGMII, 10G-BASER, and SGMII.
Quoted 7 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +0
-0@@ ... @@
drivers/net/pcs/airoha/pcs-airoha-common.c new file +3
-0+static int airoha_pcs_setup_scu (struct airoha_pcs_priv *priv, +int index,phy_interface_t interface) +{
[ ... ]
drivers/net/pcs/airoha/pcs-airoha-common.c line 143
Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10
-0+/* TODO better handle reset from MAC */ + ret =reset_control_bulk_assert (ARRAY_SIZE (priv->rsts), + priv->rsts); +if (ret) +return ret; + + ret =reset_control_bulk_deassert (ARRAY_SIZE (priv->rsts), + priv->rsts); +if (ret) +return ret;
[Severity: Medium]
Will the hardware reliably detect this reset? The reset is asserted and immediately deasserted without any delay (such as usleep_range) between them, which might result in a pulse too short for the hardware to sample correctly.
[Severity: High]
Does this unconditionally reset the global device-wide mac and phy reset lines on every port's pre_config? In multi-port scenarios like PCIe, bringing up a second port seems like it would inadvertently reset these shared lines and destroy the active configuration of the already-running first port.
Quoted 7 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +0
-0@@ ... @@
drivers/net/pcs/airoha/pcs-an7581.c new file +3
-0+static void an7581_pcs_jcpll_bringup (struct airoha_pcs_priv *priv, +int index,phy_interface_t interface) +{
[ ... ]
drivers/net/pcs/airoha/pcs-an7581.c line 702
Quoted 8 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +8
-0+/* This comment only apply to Serdes PCIe that expose +* 2 PCS. +* +* The Serdes PCIe expose 2 PCS but always require +* the PMA for the first PCS to be configured +* for correct functionality for JCPLL. +*/ + pcs_pma = priv->pcs_pma[0 ];
[Severity: High]
Could this hardcoded access to port 0's PMA registers break an active link? By toggling the shared analog PLLs and resets via priv->pcs_pma[0] when configuring port 1, it appears this might drop the link of port 0.
Quoted 8 lines
@@ ... @@
drivers/net/pcs/airoha/pcs-an7581.c new file +7
-0+static bool an7581_pcs_have_rx_signal (struct airoha_pcs_priv *priv,int index) +{ +struct regmap *pcs_pma = priv->pcs_pma[index]; +unsigned int count =0 ; +u32 val; +int i; +
drivers/net/pcs/airoha/pcs-an7581.c line 2064
Quoted 10 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +10
-0+regmap_write (pcs_pma,AIROHA_PCS_PMA_DIG_RESERVE_0 , +AIROHA_PCS_TRIGGER_RX_SIDGET_SCAN ); + +/* Scan 6 times for RX sigdet module to detect RX signal */ +for (i =0 ; i <AIROHA_PCS_MAX_RX_SIGDET_TRY ; i++) { +regmap_read (pcs_pma,AIROHA_PCS_PMA_DIG_RO_RESERVE_2 , + &val); +if (val &AIROHA_PCS_RX_SIGDET ) + count++; + }
[Severity: Medium]
Can the analog circuitry complete the scan in time? The loop polls the status register 6 times back-to-back without any temporal spacing, meaning the CPU might instantaneously sample the exact same state in a few nanoseconds before the hardware has time to vary its output.
Quoted 9 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +0
-0@@ ... @@
drivers/net/pcs/airoha/pcs-airoha-common.c new file +5
-0+static int airoha_pcs_config (struct phylink_pcs *pcs,unsigned int neg_mode, +phy_interface_t interface, +const unsigned long *advertising, +bool permit_pause_to_mac) +{
[ ... ]
drivers/net/pcs/airoha/pcs-airoha-common.c line 641
Quoted 8 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +8
-0+if (interface ==PHY_INTERFACE_MODE_1000BASEX && + neg_mode !=PHYLINK_PCS_NEG_INBAND_ENABLED ) { +regmap_set_bits (maps->hsgmii_pcs,AIROHA_PCS_HSGMII_PCS_CTROL_1 , +AIROHA_PCS_SGMII_SEND_AN_ERR_EN ); + +regmap_set_bits (maps->hsgmii_pcs,AIROHA_PCS_HSGMII_AN_SGMII_REG_AN_FORCE_CL37 , +AIROHA_PCS_HSGMII_AN_FORCE_AN_DONE ); + }
[Severity: High]
Are these sticky AN configuration bits ever cleared if the port is later reconfigured for a mode that requires inband auto-negotiation? Since the USB port does not issue a global reset during bringup, these force AN done bits might permanently leak and break inband AN until a hard reboot.
Quoted 3 lines
@@ ... @@
drivers/net/pcs/airoha/pcs-airoha-common.c new file +2
-0+static int airoha_pcs_probe (struct platform_device *pdev) +{
[ ... ]
Quoted 14 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +9
-0+if (data->port_type ==AIROHA_PCS_USB ) { +struct phy *phy; + + phy =devm_phy_get (dev,NULL ); +if (IS_ERR (phy)) +return dev_err_probe (dev,PTR_ERR (phy),"failed to get phy\n" ); + + priv->phy = phy; + }drivers/net/pcs/airoha/pcs-an7581.c new file +0
-0@@ ... @@
drivers/net/pcs/airoha/pcs-an7581.c line 1945
Quoted 8 lines
drivers/net/pcs/airoha/pcs-an7581.c new file +8
-0+int an7581_pcs_usb_bringup (struct airoha_pcs_priv *priv, +int index,phy_interface_t interface) +{ +int ret; + + ret =phy_set_mode_ext (priv->phy,PHY_MODE_ETHERNET , interface); +if (ret) +return ret;
[Severity: High]
Does this code ever initialize or power on the generic PHY? The PHY is acquired during probe and its mode is set during bringup, but there are no calls to phy_init() or phy_power_on(). Failing to do so leaves the PHY unclocked or powered down, rendering the USB PCS non-functional.
Quoted 8 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +0
-0@@ ... @@
drivers/net/pcs/airoha/pcs-airoha-common.c new file +4
-0+static void airoha_pcs_pre_config (struct phylink_pcs *pcs, +phy_interface_t interface) +{ +struct airoha_pcs_port *port =to_airoha_pcs_port (pcs);
drivers/net/pcs/airoha/pcs-airoha-common.c line 877
Quoted 10 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +10
-0+struct airoha_pcs_priv *priv = port->priv; +struct airoha_pcs_maps *maps; + + maps = &priv->maps[port->index]; + +/* Select HSGMII or USXGMII in SCU regs */ +airoha_pcs_setup_scu (priv, port->index, interface); + +/* MPI MBI disable */ + regmap_set_bits(maps->pcs_mac, AIROHA_PCS_XFI_MAC_XFI_GIB_CFG,
[Severity: Medium]
Is it safe to ignore the return value of airoha_pcs_setup_scu() here? If the reset toggling fails, the hardware might remain held in reset, which could cause subsequent MMIO writes to stall the bus or fail silently.
drivers/net/pcs/airoha/pcs-airoha-common.c line 326
Quoted 12 lines
@@ ... @@
drivers/net/pcs/airoha/pcs-airoha-common.c new file +11
-0+static void airoha_pcs_get_state_sgmii (struct airoha_pcs_priv *priv, +unsigned int neg_mode,int index, +struct phylink_link_state *state) +{ +struct airoha_pcs_maps *maps = &priv->maps[index]; +u32 bmsr, lpa; + +regmap_read (maps->hsgmii_an,AIROHA_PCS_HSGMII_AN_SGMII_REG_AN_1 , + &bmsr); +regmap_read (maps->hsgmii_an,AIROHA_PCS_HSGMII_AN_SGMII_REG_AN_5 , + &lpa);
[Severity: High]
Does this code risk using uninitialized stack variables? The return values of regmap_read() are not checked. If the MMIO read fails due to power management or hardware faults, random stack data in bmsr and lpa will be used to set network state parameters, leaking garbage into the networking stack.
Quoted 4 lines
@@ ... @@
drivers/net/pcs/airoha/pcs-airoha-common.c new file +3
-0+static void airoha_pcs_interrupt_init_usxgmii (struct airoha_pcs_priv *priv, +int index) +{
[ ... ]
drivers/net/pcs/airoha/pcs-airoha-common.c line 282
Quoted 6 lines
drivers/net/pcs/airoha/pcs-airoha-common.c new file +6
-0+/* Clear any pending interrupt */ +regmap_set_bits (maps->usxgmii_pcs,AIROHA_PCS_USXGMII_PCS_INT_STA_2 , +AIROHA_PCS_USXGMII_RPCS_FSM_DEC_ERR_INT | +AIROHA_PCS_USXGMII_R_TYPE_E_INT | +AIROHA_PCS_USXGMII_R_TYPE_T_INT | +AIROHA_PCS_USXGMII_R_TYPE_D_INT );
[Severity: Medium]
Will this unintentionally clear unhandled interrupts? Using regmap_set_bits() performs a read-modify-write operation. If these status registers are Write-1-to-Clear (W1C), it will read all active interrupts and write them back as 1, silently clearing all pending interrupts instead of just the ones specified in the mask.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
RFC PATCH v8 12/12 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 | 1 + drivers/net/ethernet/airoha/airoha_eth.c | 193 +++++++++++++++++++++- drivers/net/ethernet/airoha/airoha_eth.h | 7 +- drivers/net/ethernet/airoha/airoha_regs.h | 12 ++ 4 files changed, 206 insertions(+), 7 deletions(-)
drivers/net/ethernet/airoha/Kconfig +1 -0
@@ -20,6 +20,7 @@ config NET_AIROHA depends on NET_DSA || !NET_DSA select NET_AIROHA_NPU select PAGE_POOL + select PHYLINK help This driver supports the gigabit ethernet MACs in the Airoha SoC family.
drivers/net/ethernet/airoha/airoha_eth.c +188 -5
265 lines not on this page · raw message
drivers/net/ethernet/airoha/airoha_eth.h +5 -2
@@ -554,6 +554,9 @@ struct airoha_gdm_dev {int nbq;struct airoha_hw_stats stats; + +struct phylink *phylink; +struct phylink_config phylink_config; };struct airoha_gdm_port {
@@ -561,8 +564,8 @@ struct airoha_gdm_port {int id;int users; -/* protect concurrent hw_stats accesses */ -spinlock_t stats_lock; +/* protect concurrent hw_stats and frag register accesses */ +spinlock_t lock;struct metadata_dst *dsa_meta[AIROHA_MAX_DSA_PORTS ]; };
drivers/net/ethernet/airoha/airoha_regs.h +12 -0
@@ -358,6 +358,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.53.0
Lorenzo Bianconi
maintainerRe: [RFC PATCH net-next v8 12/12] net: airoha: add phylink support
[...]
Quoted 26 lines
drivers/net/ethernet/airoha/airoha_eth.c +26
-0+static void airoha_mac_config (struct phylink_config *config,unsigned int mode, +const struct phylink_link_state *state) +{ +} + +static void airoha_mac_link_up (struct phylink_config *config,struct phy_device *phy, +unsigned int mode,phy_interface_t interface, +int speed,int duplex,bool tx_pause,bool rx_pause) +{ + struct airoha_gdm_dev *dev = container_of(config, struct airoha_gdm_dev, + phylink_config); +struct airoha_gdm_port *port = dev->port; +struct airoha_eth *eth = dev->eth; +u32 frag_size_tx, frag_size_rx; +u32 mask, val; + +/* TX/RX frag is configured only for GDM4 */ +if (port->id !=AIROHA_GDM4_IDX ) +return ; + +switch (speed) { +case SPEED_10000 : +case SPEED_5000 : + frag_size_tx =8 ; + frag_size_rx =8 ; +break ;
drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3238
Quoted 10 lines
drivers/net/ethernet/airoha/airoha_eth.c +10
-0+case SPEED_2500 : + frag_size_tx =2 ; + frag_size_rx =1 ; +break ; +default : + frag_size_tx =1 ; + frag_size_rx =0 ; + } + +spin_lock (&port->lock);
Using a spin_lock seems overkill here to me since airoha_mac_link_up() run in the process context and the spin_lock you are acquiring here was just used for stats. Since mac_link_up is running in some cases acquiring RTNL, I guess you can even do something like:
bool rtnl = rtnl_is_locked(); if (!rtnl) rtnl_lock(); ... if (!rtnl) rtnl_unlock();
Regards,
Lorenzo
Quoted 201 lines
drivers/net/ethernet/airoha/airoha_eth.c +117
-2+ +/* Configure TX/RX frag based on speed */ +if (dev->nbq ==1 ) { + mask =GDM4_SGMII1_TX_FRAG_SIZE_MASK ; + val =FIELD_PREP (GDM4_SGMII1_TX_FRAG_SIZE_MASK , + frag_size_tx); + }else { + mask =GDM4_SGMII0_TX_FRAG_SIZE_MASK ; + val =FIELD_PREP (GDM4_SGMII0_TX_FRAG_SIZE_MASK , + frag_size_tx); + } +airoha_fe_rmw (eth,REG_FE_GDM4_TMBI_FRAG , mask, val); + +if (dev->nbq ==1 ) { + mask =GDM4_SGMII1_RX_FRAG_SIZE_MASK ; + val =FIELD_PREP (GDM4_SGMII1_RX_FRAG_SIZE_MASK , + frag_size_rx); + }else { + mask =GDM4_SGMII0_RX_FRAG_SIZE_MASK ; + val =FIELD_PREP (GDM4_SGMII0_RX_FRAG_SIZE_MASK , + frag_size_rx); + } +airoha_fe_rmw (eth,REG_FE_GDM4_RMBI_FRAG , mask, val); + +spin_unlock (&port->lock); +} + +/* Nothing to do in MAC, everything is handled in PCS */ +static void airoha_mac_link_down (struct phylink_config *config,unsigned int mode, +phy_interface_t interface) +{ +} + +static const struct phylink_mac_ops airoha_phylink_ops = { + .mac_config = airoha_mac_config, + .mac_link_up = airoha_mac_link_up, + .mac_link_down = airoha_mac_link_down, +}; + +static int airoha_fill_available_pcs (struct phylink_config *config, +struct phylink_pcs **available_pcs, +unsigned int num_possible_pcs) +{ +struct device *dev = config->dev; + +return fwnode_phylink_pcs_parse (dev_fwnode (dev), available_pcs, + num_possible_pcs); +} + +static int airoha_setup_phylink (struct net_device *netdev) +{ +struct airoha_gdm_dev *dev =netdev_priv (netdev); +struct device_node *np = netdev->dev.of_node; +struct airoha_gdm_port *port = dev->port; +struct phylink_config *config; +phy_interface_t phy_mode; +struct phylink *phylink; +int err; + + err =of_get_phy_mode (np, &phy_mode); +if (err) { +dev_err (&netdev->dev,"incorrect phy-mode\n" ); +return err; + } + + config = &dev->phylink_config; + config->dev = &netdev->dev; + config->type =PHYLINK_NETDEV ; + +/* +* GDM1 only supports internal for Embedded Switch +* and doesn't require a PCS. +*/ +if (port->id ==AIROHA_GDM1_IDX ) { + config->mac_capabilities =MAC_ASYM_PAUSE |MAC_SYM_PAUSE | +MAC_10000FD ; + +__set_bit (PHY_INTERFACE_MODE_INTERNAL , + config->supported_interfaces); + }else { + config->mac_capabilities =MAC_ASYM_PAUSE |MAC_SYM_PAUSE | +MAC_10 |MAC_100 |MAC_1000 | +MAC_2500FD |MAC_5000FD |MAC_10000FD ; + + config->num_possible_pcs =fwnode_phylink_pcs_count (dev_fwnode (config->dev)); + config->fill_available_pcs = airoha_fill_available_pcs; + +__set_bit (PHY_INTERFACE_MODE_SGMII , + config->supported_interfaces); +__set_bit (PHY_INTERFACE_MODE_1000BASEX , + config->supported_interfaces); +__set_bit (PHY_INTERFACE_MODE_2500BASEX , + config->supported_interfaces); +__set_bit (PHY_INTERFACE_MODE_10GBASER , + config->supported_interfaces); +__set_bit (PHY_INTERFACE_MODE_USXGMII , + config->supported_interfaces); + +phy_interface_copy (config->pcs_interfaces, + config->supported_interfaces); + } + + phylink =phylink_create (config,of_fwnode_handle (np), + phy_mode, &airoha_phylink_ops); +if (IS_ERR (phylink)) +return PTR_ERR (phylink); + + dev->phylink = phylink; + +return 0 ; +} +static int airoha_alloc_gdm_device (struct airoha_eth *eth,struct airoha_gdm_port *port,int nbq,struct device_node *np)@@ -3231,7 +3411,7 @@ static int airoha_alloc_gdm_device(struct airoha_eth *eth, dev->nbq = nbq; port->devs[index] = dev; -return 0 ; +return airoha_setup_phylink (netdev); } static int airoha_alloc_gdm_port(struct airoha_eth *eth,@@ -3266,7 +3446,7 @@ static int airoha_alloc_gdm_port(struct airoha_eth *eth,return -ENOMEM ; port->id = id; -spin_lock_init (&port->stats_lock); +spin_lock_init (&port->lock); eth->ports[p] = port; err =airoha_metadata_dst_alloc (port);@@ -3457,6 +3637,8 @@ static int airoha_probe(struct platform_device *pdev) netdev =netdev_from_priv (dev);if (netdev->reg_state ==NETREG_REGISTERED )unregister_netdev (netdev); +if (dev->phylink) +phylink_destroy (dev->phylink);of_node_put (netdev->dev.of_node); }airoha_metadata_dst_free (port);@@ -3493,6 +3675,7 @@ static void airoha_remove(struct platform_device *pdev) netdev =netdev_from_priv (dev);unregister_netdev (netdev); +phylink_destroy (dev->phylink);of_node_put (netdev->dev.of_node); }airoha_metadata_dst_free (port);drivers/net/ethernet/airoha/airoha_eth.h +5
-2@@ -554,6 +554,9 @@ struct airoha_gdm_dev {int nbq;struct airoha_hw_stats stats; + +struct phylink *phylink; +struct phylink_config phylink_config; };struct airoha_gdm_port {@@ -561,8 +564,8 @@ struct airoha_gdm_port {int id;int users; -/* protect concurrent hw_stats accesses */ -spinlock_t stats_lock; +/* protect concurrent hw_stats and frag register accesses */ +spinlock_t lock;struct metadata_dst *dsa_meta[AIROHA_MAX_DSA_PORTS ]; };drivers/net/ethernet/airoha/airoha_regs.h +12
-0@@ -358,6 +358,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.53.0
[email protected]
AI reviewRe: [RFC PATCH net-next v8 12/12] net: airoha: add phylink support
In reply to Christian MarangiThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] PCS configuration fails because netdev->dev.fwnode is not initialized before calling fwnode_phylink_pcs_count() and fwnode_phylink_pcs_parse().
Pre-existing issues:
- [High] Spinlock deadlock due to acquiring port->lock without disabling softirqs. --
commit c3dd483dacef136d470df9049b76a9b65a5390bc
Author: Christian Marangi <[email protected]>
net: airoha: add phylink support
Add phylink support for each GDM port. For GDM1 add the internal interface mode as the only supported mode. For GDM2/3/4 add the required configuration of the PCS to make the external PHY or attached SFP cage work.
drivers/net/ethernet/airoha/airoha_eth.c static void airoha_update_hw_stats(struct airoha_gdm_dev *dev) · line 1792
Quoted 10 lines
drivers/net/ethernet/airoha/airoha_eth.c +1
-1@@ -1788,7 +1789,7 @@ static void airoha_update_hw_stats(struct airoha_gdm_dev *dev)struct airoha_gdm_port *port = dev->port;int i; -spin_lock (&port->stats_lock); +spin_lock (&port->lock);
[Severity: High]
This is a pre-existing issue, but can this cause a spinlock deadlock if a softirq interrupts a process-context thread holding this lock?
This function is reachable from ndo_get_stats64, which the networking core can invoke from softirq/BH context (e.g., via bonding or team drivers). If this lock is also acquired in process context without disabling softirqs, a deadlock can occur.
[ ... ]
Quoted 1 line
drivers/net/ethernet/airoha/airoha_eth.c +0
-0@@ -3168,6 +3199,155 @@ bool airoha_is_valid_gdm_dev(struct airoha_eth *eth,
[ ... ]
Quoted 4 lines
drivers/net/ethernet/airoha/airoha_eth.c +4
-0+static void airoha_mac_link_up (struct phylink_config *config,struct phy_device *phy, +unsigned int mode,phy_interface_t interface, +int speed,int duplex,bool tx_pause,bool rx_pause) +{
[ ... ]
drivers/net/ethernet/airoha/airoha_eth.c
Quoted 1 line
drivers/net/ethernet/airoha/airoha_eth.c +1
-0+spin_lock (&port->lock);
[Severity: High]
Does acquiring port->lock here expose the deadlock mentioned above?
airoha_mac_link_up() executes in process context. If a softirq interrupts it while holding port->lock, the softirq might call airoha_update_hw_stats() and attempt to acquire the exact same lock.
Should this and other process-context acquisitions of port->lock use spin_lock_bh() instead?
Quoted 1 line
drivers/net/ethernet/airoha/airoha_eth.c +1
-0+/* Configure TX/RX frag based on speed */
[ ... ]
Quoted 2 lines
drivers/net/ethernet/airoha/airoha_eth.c +2
-0+static int airoha_setup_phylink (struct net_device *netdev) +{
[ ... ]
drivers/net/ethernet/airoha/airoha_eth.c bool airoha_is_valid_gdm_dev(struct airoha_eth *eth, · line 3323
Quoted 6 lines
drivers/net/ethernet/airoha/airoha_eth.c +6
-0+ }else { + config->mac_capabilities =MAC_ASYM_PAUSE |MAC_SYM_PAUSE | +MAC_10 |MAC_100 |MAC_1000 | +MAC_2500FD |MAC_5000FD |MAC_10000FD ; + + config->num_possible_pcs =fwnode_phylink_pcs_count (dev_fwnode (config->dev));
[Severity: High]
Will dev_fwnode() return NULL here because netdev->dev.fwnode is not yet initialized?
In airoha_alloc_gdm_device(), the device tree node is assigned using netdev->dev.of_node = of_node_get(np), but the generic fwnode pointer is left uninitialized.
Since airoha_setup_phylink() runs before register_netdevice(), dev_fwnode() evaluates to NULL. This causes fwnode_phylink_pcs_count() to unconditionally return 0, and fwnode_phylink_pcs_parse() to fail later in airoha_fill_available_pcs().
Does this silently disable PCS discovery and break support for external PHYs and SFP cages?
Could this be resolved by using device_set_node(&netdev->dev, of_fwnode_handle(np)) in airoha_alloc_gdm_device(), or by passing of_fwnode_handle(np) directly?
Quoted 1 line
drivers/net/ethernet/airoha/airoha_eth.c +1
-0+ config->fill_available_pcs = airoha_fill_available_pcs;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12