[PATCH net-next 08/13] net: phylink: Represent PHY-less SFP modules with phy_port

Maxime Chevallier posted 13 patches 1 week, 5 days ago
There is a newer version of this series
[PATCH net-next 08/13] net: phylink: Represent PHY-less SFP modules with phy_port
Posted by Maxime Chevallier 1 week, 5 days ago
Let phylink handle the phy_port for PHY-less modules, and register it to
the topology.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phylink.c | 68 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 66 insertions(+), 2 deletions(-)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 310af33d49a0..6c62604b00a6 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -96,6 +96,7 @@ struct phylink {
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
 	u8 sfp_port;
 	struct phy_port *sfp_bus_port;
+	struct phy_port *mod_port;
 
 	struct eee_config eee_cfg;
 
@@ -1786,13 +1787,31 @@ static int phylink_create_sfp_port(struct phylink *pl)
 	else
 		pl->sfp_bus_port = port;
 
+	if (pl->mod_port) {
+		ret = phy_link_topo_add_port(pl->netdev, pl->mod_port);
+		if (ret)
+			goto out_bus_port;
+	}
+
+	return 0;
+out_bus_port:
+	phy_link_topo_del_port(pl->netdev, port);
+	phy_port_destroy(port);
 	return ret;
 }
 
 static void phylink_destroy_sfp_port(struct phylink *pl)
 {
-	if (pl->netdev && pl->sfp_bus_port)
-		phy_link_topo_del_port(pl->netdev, pl->sfp_bus_port);
+	if (pl->netdev) {
+		if (pl->sfp_bus_port)
+			phy_link_topo_del_port(pl->netdev, pl->sfp_bus_port);
+
+		/* Only remove it from the topology, it will be destroyed at
+		 * module removal.
+		 */
+		if (pl->mod_port)
+			phy_link_topo_del_port(pl->netdev, pl->mod_port);
+	}
 
 	if (pl->sfp_bus_port)
 		phy_port_destroy(pl->sfp_bus_port);
@@ -3998,6 +4017,49 @@ static void phylink_sfp_disconnect_phy(void *upstream,
 	phylink_disconnect_phy(upstream);
 }
 
+static int phylink_sfp_connect_nophy(void *upstream)
+{
+	const struct sfp_module_caps *caps;
+	struct phylink *pl = upstream;
+	struct phy_port *port;
+	int ret = 0;
+
+	/* Create mod port */
+	port = phy_port_alloc();
+	if (!port)
+		return -ENOMEM;
+
+	port->active = true;
+
+	caps = sfp_get_module_caps(pl->sfp_bus);
+
+	phy_caps_linkmode_filter_ifaces(port->supported, caps->link_modes,
+					pl->sfp_bus_port->interfaces);
+
+	if (pl->netdev) {
+		ret = phy_link_topo_add_port(pl->netdev, port);
+		if (ret) {
+			phy_port_destroy(port);
+			return ret;
+		}
+	}
+
+	pl->mod_port = port;
+
+	return 0;
+}
+
+static void phylink_sfp_disconnect_nophy(void *upstream)
+{
+	struct phylink *pl = upstream;
+
+	if (pl->netdev)
+		phy_link_topo_del_port(pl->netdev, pl->mod_port);
+
+	phy_port_destroy(pl->mod_port);
+	pl->mod_port = NULL;
+}
+
 static const struct sfp_upstream_ops sfp_phylink_ops = {
 	.attach = phylink_sfp_attach,
 	.detach = phylink_sfp_detach,
@@ -4009,6 +4071,8 @@ static const struct sfp_upstream_ops sfp_phylink_ops = {
 	.link_down = phylink_sfp_link_down,
 	.connect_phy = phylink_sfp_connect_phy,
 	.disconnect_phy = phylink_sfp_disconnect_phy,
+	.connect_nophy = phylink_sfp_connect_nophy,
+	.disconnect_nophy = phylink_sfp_disconnect_nophy,
 };
 
 /* Helpers for MAC drivers */
-- 
2.49.0
Re: [PATCH net-next 08/13] net: phylink: Represent PHY-less SFP modules with phy_port
Posted by Romain Gantois 1 week, 4 days ago
On Tuesday, 27 January 2026 14:41:56 CET Maxime Chevallier wrote:
...
> @@ -1786,13 +1787,31 @@ static int phylink_create_sfp_port(struct phylink
> *pl) else
> 
>  		pl->sfp_bus_port = port;
> 
> +	if (pl->mod_port) {
> +		ret = phy_link_topo_add_port(pl->netdev, pl->mod_port);
> +		if (ret)
> +			goto out_bus_port;
> +	}
> +
> +	return 0;
> +out_bus_port:
> +	phy_link_topo_del_port(pl->netdev, port);

This seems strange to me. Why clean up after phy_link_topo_add_port() if it 
returned an error code? Presumably phy_link_topo_add_port() cleans up after 
itself if it encounters an error doesn't it?

> +	phy_port_destroy(port);
> 
>  	return ret;
>  
>  }
>  
>  static void phylink_destroy_sfp_port(struct phylink *pl)
>  {
> 
> -	if (pl->netdev && pl->sfp_bus_port)
> -		phy_link_topo_del_port(pl->netdev, pl->sfp_bus_port);
> +	if (pl->netdev) {
> +		if (pl->sfp_bus_port)
> +			phy_link_topo_del_port(pl->netdev, pl->sfp_bus_port);
> +
> +		/* Only remove it from the topology, it will be destroyed at
> +		 * module removal.
> +		 */
> +		if (pl->mod_port)
> +			phy_link_topo_del_port(pl->netdev, pl->mod_port);
> +	}
> 
>  	if (pl->sfp_bus_port)
>  	
>  		phy_port_destroy(pl->sfp_bus_port);
> 
> @@ -3998,6 +4017,49 @@ static void phylink_sfp_disconnect_phy(void
> *upstream, phylink_disconnect_phy(upstream);
> 
>  }
> 
> +static int phylink_sfp_connect_nophy(void *upstream)

I'd name this "phylink_sfp_connect_no_phy" just to keep the name formatting 
consistent.

Thanks,

-- 
Romain Gantois, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Re: [PATCH net-next 08/13] net: phylink: Represent PHY-less SFP modules with phy_port
Posted by Maxime Chevallier 1 week, 2 days ago

On 28/01/2026 17:01, Romain Gantois wrote:
> On Tuesday, 27 January 2026 14:41:56 CET Maxime Chevallier wrote:
> ...
>> @@ -1786,13 +1787,31 @@ static int phylink_create_sfp_port(struct phylink
>> *pl) else
>>
>>  		pl->sfp_bus_port = port;
>>
>> +	if (pl->mod_port) {
>> +		ret = phy_link_topo_add_port(pl->netdev, pl->mod_port);
>> +		if (ret)
>> +			goto out_bus_port;
>> +	}
>> +
>> +	return 0;
>> +out_bus_port:
>> +	phy_link_topo_del_port(pl->netdev, port);
> 
> This seems strange to me. Why clean up after phy_link_topo_add_port() if it 
> returned an error code? Presumably phy_link_topo_add_port() cleans up after 
> itself if it encounters an error doesn't it?

Because we're not cleaning up the same port, notice the 'port' vs
'pl->mod_port' params :)

> 
>> +	phy_port_destroy(port);
>>
>>  	return ret;
>>  
>>  }
>>  
>>  static void phylink_destroy_sfp_port(struct phylink *pl)
>>  {
>>
>> -	if (pl->netdev && pl->sfp_bus_port)
>> -		phy_link_topo_del_port(pl->netdev, pl->sfp_bus_port);
>> +	if (pl->netdev) {
>> +		if (pl->sfp_bus_port)
>> +			phy_link_topo_del_port(pl->netdev, pl->sfp_bus_port);
>> +
>> +		/* Only remove it from the topology, it will be destroyed at
>> +		 * module removal.
>> +		 */
>> +		if (pl->mod_port)
>> +			phy_link_topo_del_port(pl->netdev, pl->mod_port);
>> +	}
>>
>>  	if (pl->sfp_bus_port)
>>  	
>>  		phy_port_destroy(pl->sfp_bus_port);
>>
>> @@ -3998,6 +4017,49 @@ static void phylink_sfp_disconnect_phy(void
>> *upstream, phylink_disconnect_phy(upstream);
>>
>>  }
>>
>> +static int phylink_sfp_connect_nophy(void *upstream)
> 
> I'd name this "phylink_sfp_connect_no_phy" just to keep the name formatting 
> consistent.

having 'nophy' as a single word made it clearer that this was "phy" vs
"nophy" IMO, rather than potentially interpreting "phy" as a suffix. but
I see your point, I think I'll rename it :)

Maxime

> 
> Thanks,
>