[PATCH net-next v3 01/11] net: phy: phy_port: Correctly recompute the port's linkmodes

Maxime Chevallier posted 11 patches 6 days, 22 hours ago
There is a newer version of this series
[PATCH net-next v3 01/11] net: phy: phy_port: Correctly recompute the port's linkmodes
Posted by Maxime Chevallier 6 days, 22 hours ago
a PHY-driven phy_port contains a 'supported' field containing the
linkmodes available on this port. This is populated based on :
 - The PHY's reported features
 - The DT representation of the connector
 - The PHY's attach_mdi() callback

As these different attrbution methods work in conjunction, the helper
phy_port_update_supported() recomputes the final 'supported' value based
on the populated mediums, linkmodes and pairs.

However this recompute wasn't correctly implemented, and added more
modes than necessary by or'ing the medium-specific modes to the existing
support. Let's fix this and properly filter the modes.

Fixes: 589e934d2735 ("net: phy: Introduce PHY ports representation")
Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phy_port.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/drivers/net/phy/phy_port.c b/drivers/net/phy/phy_port.c
index ec93c8ca051e..6c1d3c487b13 100644
--- a/drivers/net/phy/phy_port.c
+++ b/drivers/net/phy/phy_port.c
@@ -117,12 +117,6 @@ void phy_port_update_supported(struct phy_port *port)
 	unsigned long mode;
 	int i;
 
-	for_each_set_bit(i, &port->mediums, __ETHTOOL_LINK_MEDIUM_LAST) {
-		linkmode_zero(supported);
-		phy_caps_medium_get_supported(supported, i, port->pairs);
-		linkmode_or(port->supported, port->supported, supported);
-	}
-
 	/* If there's no pairs specified, we grab the default number of
 	 * pairs as the max of the default pairs for each linkmode
 	 */
@@ -132,6 +126,19 @@ void phy_port_update_supported(struct phy_port *port)
 			port->pairs = max_t(int, port->pairs,
 					    ethtool_linkmode_n_pairs(mode));
 
+	if (linkmode_empty(port->supported)) {
+		for_each_set_bit(i, &port->mediums,
+				 __ETHTOOL_LINK_MEDIUM_LAST) {
+			__ETHTOOL_DECLARE_LINK_MODE_MASK(med_supported) = {0};
+
+			phy_caps_medium_get_supported(med_supported, i,
+						      port->pairs);
+			linkmode_or(supported, supported, med_supported);
+		}
+
+		linkmode_and(port->supported, port->supported, supported);
+	}
+
 	/* Serdes ports supported through SFP may not have any medium set,
 	 * as they will output PHY_INTERFACE_MODE_XXX modes. In that case, derive
 	 * the supported list based on these interfaces
-- 
2.49.0
Re: [net-next,v3,01/11] net: phy: phy_port: Correctly recompute the port's linkmodes
Posted by Simon Horman 4 days, 19 hours ago
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.

For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
net: phy: phy_port: Correctly recompute the port's linkmodes

This commit fixes phy_port_update_supported() to properly filter linkmodes
instead of incorrectly or'ing medium-specific modes to the existing support.
It moves the medium iteration inside a check for empty port->supported.

Fixes: 589e934d2735 ("net: phy: Introduce PHY ports representation")

> diff --git a/drivers/net/phy/phy_port.c b/drivers/net/phy/phy_port.c
> --- a/drivers/net/phy/phy_port.c
> +++ b/drivers/net/phy/phy_port.c

[ ... ]

> @@ -132,6 +126,19 @@ void phy_port_update_supported(struct phy_port *port)
>  			port->pairs = max_t(int, port->pairs,
>  					    ethtool_linkmode_n_pairs(mode));
>
> +	if (linkmode_empty(port->supported)) {
> +		for_each_set_bit(i, &port->mediums,
> +				 __ETHTOOL_LINK_MEDIUM_LAST) {
> +			__ETHTOOL_DECLARE_LINK_MODE_MASK(med_supported) = {0};
> +
> +			phy_caps_medium_get_supported(med_supported, i,
> +						      port->pairs);
> +			linkmode_or(supported, supported, med_supported);
> +		}
> +
> +		linkmode_and(port->supported, port->supported, supported);
> +	}

Jakub Kicinski raised a concern in v2 about the AND operation when
port->supported starts empty. The v3 fix adds the
'if (linkmode_empty(port->supported))' check, but then still performs:

    linkmode_and(port->supported, port->supported, supported);

inside that block. Since we just confirmed port->supported is empty,
ANDing it with anything will always produce an empty result.

Should this be linkmode_copy() or linkmode_or() instead of linkmode_and()
when port->supported starts empty?

Reference: https://lore.kernel.org/all/20260130040009.707764-1-kuba@kernel.org/
Re: [net-next,v3,01/11] net: phy: phy_port: Correctly recompute the port's linkmodes
Posted by Maxime Chevallier 4 days, 19 hours ago
Hi Simon

On 03/02/2026 18:58, Simon Horman wrote:
> This is an AI-generated review of your patch. The human sending this
> email has considered the AI review valid, or at least plausible.
> 
> For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
> ---
> net: phy: phy_port: Correctly recompute the port's linkmodes
> 
> This commit fixes phy_port_update_supported() to properly filter linkmodes
> instead of incorrectly or'ing medium-specific modes to the existing support.
> It moves the medium iteration inside a check for empty port->supported.
> 
> Fixes: 589e934d2735 ("net: phy: Introduce PHY ports representation")
> 
>> diff --git a/drivers/net/phy/phy_port.c b/drivers/net/phy/phy_port.c
>> --- a/drivers/net/phy/phy_port.c
>> +++ b/drivers/net/phy/phy_port.c
> 
> [ ... ]
> 
>> @@ -132,6 +126,19 @@ void phy_port_update_supported(struct phy_port *port)
>>  			port->pairs = max_t(int, port->pairs,
>>  					    ethtool_linkmode_n_pairs(mode));
>>
>> +	if (linkmode_empty(port->supported)) {
>> +		for_each_set_bit(i, &port->mediums,
>> +				 __ETHTOOL_LINK_MEDIUM_LAST) {
>> +			__ETHTOOL_DECLARE_LINK_MODE_MASK(med_supported) = {0};
>> +
>> +			phy_caps_medium_get_supported(med_supported, i,
>> +						      port->pairs);
>> +			linkmode_or(supported, supported, med_supported);
>> +		}
>> +
>> +		linkmode_and(port->supported, port->supported, supported);

Yeah :(

>> +	}
> 
> Jakub Kicinski raised a concern in v2 about the AND operation when
> port->supported starts empty. The v3 fix adds the
> 'if (linkmode_empty(port->supported))' check, but then still performs:
> 
>     linkmode_and(port->supported, port->supported, supported);
> 
> inside that block. Since we just confirmed port->supported is empty,
> ANDing it with anything will always produce an empty result.
> 
> Should this be linkmode_copy() or linkmode_or() instead of linkmode_and()
> when port->supported starts empty?
> 
> Reference: https://lore.kernel.org/all/20260130040009.707764-1-kuba@kernel.org/

True, that's already addressed in V4 :)

Maxime