Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Maxime Chevallier <maxime.chevallier@bootlin.com>
To: davem@davemloft.net, "Andrew Lunn" <andrew+netdev@lunn.ch>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Willem de Bruijn" <willemb@google.com>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Russell King" <linux@armlinux.org.uk>,
	"Marek Behún" <kabel@kernel.org>,
	"Sebastian Hesselbarth" <sebastian.hesselbarth@gmail.com>,
	"Gregory Clement" <gregory.clement@bootlin.com>,
	"Heiner Kallweit" <hkallweit1@gmail.com>
Cc: "Maxime Chevallier" <maxime.chevallier@bootlin.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	thomas.petazzoni@bootlin.com, devicetree@vger.kernel.org,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Christophe Leroy" <christophe.leroy@csgroup.eu>,
	"Herve Codina" <herve.codina@bootlin.com>,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Vladimir Oltean" <vladimir.oltean@nxp.com>,
	"Köry Maincent" <kory.maincent@bootlin.com>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>,
	"Nicolò Veronese" <nicveronese@gmail.com>,
	"Simon Horman" <horms@kernel.org>,
	marcin.s.wojtas@gmail.com, "Daniel Golle" <daniel@makrotopia.org>,
	"Dimitri Fedrau" <dimitri.fedrau@liebherr.com>,
	"Frank Wunderlich" <frank.wunderlich@linux.dev>,
	"Pietro Ameruoso" <p.ameruoso@live.it>,
	"Aleksei Sviridkin" <f@lex.la>
Subject: [PATCH net-next 10/12] net: phylink: Support an SFP cage behind a phy_mux
Date: Fri,  4 Dec 2026 12:06:19 +0100	[thread overview]
Message-ID: <20261204110622.1106492-11-maxime.chevallier@bootlin.com> (raw)
In-Reply-To: <20261204110622.1106492-1-maxime.chevallier@bootlin.com>

With a mux, the SFP cage isn't described in the MAC's node but in one of
the mux ports. phylink is still the upstream of that cage, so let's look
for it there as well.

From there, modules are handled the same way as with any other
MAC-driven cage. What phylink needs to do on top of that is tell the mux
when a module is there, with phy_mux_port_set_available(), so that the
mux can select the SFP port, and go back to the PHY port when the module
is removed.

There's one limitation for now, we only report the modules that phylink
accepted. On the Turris Omnia the mux switches for any module though, so
with an unsupported module we're left with the PHY port selected while
the MAC is wired to the cage. Handling that needs a new SFP upstream
callback to follow the raw module presence, this is left for later.

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

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 702614adcb94..6d62bd37f95a 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -93,12 +93,17 @@ struct phylink {
 
 	struct sfp_bus *sfp_bus;
 	bool sfp_may_have_phy;
+	bool sfp_mod_present;
+	bool sfp_behind_mux;
+	bool sfp_selected;
+	bool sfp_link_down;
 	DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
 	u8 sfp_port;
 	struct phy_port *sfp_cage_port;
 	struct phy_port *mod_port;
 	struct phy_mux *mux;
+	struct phy_mux_port *mux_sfp_port;
 
 	struct eee_config eee_cfg;
 
@@ -1876,6 +1881,30 @@ static int phylink_register_sfp(struct phylink *pl,
 	return ret;
 }
 
+static int phylink_register_mux_sfp(struct phylink *pl,
+				    const struct fwnode_handle *fwnode)
+{
+	struct fwnode_handle *mux_fwnode, *sfp_fwnode;
+	int ret;
+
+	mux_fwnode = fwnode_find_reference(fwnode, "mii-mux-handle", 0);
+	if (IS_ERR(mux_fwnode))
+		return phylink_register_sfp(pl, fwnode);
+
+	sfp_fwnode = fwnode_phy_mux_find_sfp_port(mux_fwnode);
+	fwnode_handle_put(mux_fwnode);
+	if (!sfp_fwnode)
+		return phylink_register_sfp(pl, fwnode);
+
+	pl->sfp_behind_mux = true;
+	pl->sfp_link_down = true;
+
+	ret = phylink_register_sfp(pl, sfp_fwnode);
+	fwnode_handle_put(sfp_fwnode);
+
+	return ret;
+}
+
 /**
  * phylink_set_fixed_link() - set the fixed link
  * @pl: a pointer to a &struct phylink returned from phylink_create()
@@ -2128,7 +2157,7 @@ struct phylink *phylink_create(struct phylink_config *config,
 
 	pl->req_link_an_mode = pl->cfg_link_an_mode;
 
-	ret = phylink_register_sfp(pl, fwnode);
+	ret = phylink_register_mux_sfp(pl, fwnode);
 	if (ret < 0)
 		goto release_link_gpio;
 
@@ -2150,6 +2179,7 @@ static void phylink_detach_mux(struct phylink *pl)
 
 	phy_mux_detach(pl->mux);
 	pl->mux = NULL;
+	pl->mux_sfp_port = NULL;
 }
 
 /**
@@ -2766,6 +2796,9 @@ void phylink_start(struct phylink *pl)
 		phy_start(pl->phydev);
 	if (pl->sfp_bus)
 		sfp_upstream_start(pl->sfp_bus);
+	if (pl->mux_sfp_port)
+		phy_mux_port_set_available(pl->mux_sfp_port,
+					   pl->sfp_mod_present);
 	if (pl->mux)
 		phy_mux_start(pl->mux);
 }
@@ -3842,7 +3875,8 @@ int phylink_speed_down(struct phylink *pl, bool sync)
 
 	ASSERT_RTNL();
 
-	if (!pl->sfp_bus && pl->phydev)
+	if (pl->phydev &&
+	    (!pl->sfp_bus || (pl->sfp_behind_mux && !pl->sfp_selected)))
 		ret = phy_speed_down(pl->phydev, sync);
 
 	return ret;
@@ -3865,7 +3899,8 @@ int phylink_speed_up(struct phylink *pl)
 
 	ASSERT_RTNL();
 
-	if (!pl->sfp_bus && pl->phydev)
+	if (pl->phydev &&
+	    (!pl->sfp_bus || (pl->sfp_behind_mux && !pl->sfp_selected)))
 		ret = phy_speed_up(pl->phydev);
 
 	return ret;
@@ -3884,6 +3919,10 @@ static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
 	struct phylink *pl = upstream;
 
 	pl->netdev->sfp_bus = NULL;
+
+	pl->sfp_mod_present = false;
+	if (pl->mux_sfp_port)
+		phy_mux_port_set_available(pl->mux_sfp_port, false);
 }
 
 static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
@@ -4082,11 +4121,19 @@ static int phylink_sfp_config_optical(struct phylink *pl)
 	return 0;
 }
 
+static void phylink_sfp_set_present(struct phylink *pl, bool present)
+{
+	pl->sfp_mod_present = present;
+	if (pl->mux_sfp_port)
+		phy_mux_port_set_available(pl->mux_sfp_port, present);
+}
+
 static int phylink_sfp_module_insert(void *upstream,
 				     const struct sfp_eeprom_id *id)
 {
 	const struct sfp_module_caps *caps;
 	struct phylink *pl = upstream;
+	int ret;
 
 	ASSERT_RTNL();
 
@@ -4096,18 +4143,27 @@ static int phylink_sfp_module_insert(void *upstream,
 	pl->sfp_may_have_phy = caps->may_have_phy;
 	pl->sfp_port = caps->port;
 
+	phylink_sfp_set_present(pl, true);
+
 	/* If this module may have a PHY connecting later, defer until later */
 	if (pl->sfp_may_have_phy)
 		return 0;
 
-	return phylink_sfp_config_optical(pl);
+	ret = phylink_sfp_config_optical(pl);
+	if (ret)
+		phylink_sfp_set_present(pl, false);
+
+	return ret;
 }
 
 static void phylink_sfp_module_remove(void *upstream)
 {
 	struct phylink *pl = upstream;
 
+	ASSERT_RTNL();
+
 	phy_interface_zero(pl->sfp_interfaces);
+	phylink_sfp_set_present(pl, false);
 }
 
 static int phylink_add_sfp_mod_port(struct phylink *pl)
@@ -4211,6 +4267,10 @@ static void phylink_sfp_link_down(void *upstream)
 
 	ASSERT_RTNL();
 
+	pl->sfp_link_down = true;
+	if (pl->sfp_behind_mux && !pl->sfp_selected)
+		return;
+
 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
 }
 
@@ -4220,6 +4280,10 @@ static void phylink_sfp_link_up(void *upstream)
 
 	ASSERT_RTNL();
 
+	pl->sfp_link_down = false;
+	if (pl->sfp_behind_mux && !pl->sfp_selected)
+		return;
+
 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
 }
 
@@ -4268,6 +4332,7 @@ static void phylink_mux_attach(struct phy_mux *mux, void *priv)
 		phy_mux_set_mac_managed_pm(mux);
 
 	pl->mux = mux;
+	pl->mux_sfp_port = phy_mux_get_sfp_port(mux);
 }
 
 static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
@@ -4277,6 +4342,19 @@ static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
 	struct phy_device *phy;
 	int ret;
 
+	if (port->type == PHY_MUX_PORT_SFP) {
+		if (!pl->sfp_bus)
+			return -ENODEV;
+
+		pl->sfp_selected = true;
+		if (pl->sfp_link_down)
+			phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
+		else
+			phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
+
+		return 0;
+	}
+
 	if (port->type != PHY_MUX_PORT_PHY)
 		return -EOPNOTSUPP;
 
@@ -4291,7 +4369,10 @@ static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
 		return ret;
 	}
 
+	clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
+
 	pl->link_config.interface = port->interface;
+	pl->req_link_an_mode = pl->cfg_link_an_mode;
 	phylink_mac_initial_config(pl, false);
 	phy_start(phy);
 
@@ -4304,7 +4385,10 @@ static void phylink_mux_deselect(struct phy_mux *mux,
 	struct phylink *pl = priv;
 	struct phy_device *phy;
 
-	if (pl->phydev != port->phydev)
+	if (port->type == PHY_MUX_PORT_SFP)
+		pl->sfp_selected = false;
+
+	if (port->type != PHY_MUX_PORT_PHY || pl->phydev != port->phydev)
 		return;
 
 	phy = port->phydev;
-- 
2.56.1



  parent reply	other threads:[~2026-12-04 11:08 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 01/12] dt-bindings: net: Add Ethernet " Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 07/12] net: phylink: Factor out phylink_clear_phydev() Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 08/12] net: phy: phy_mux: Add phy_mux_set_mac_managed_pm() Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux Maxime Chevallier
2026-12-04 11:06 ` Maxime Chevallier [this message]
2026-12-04 11:06 ` [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer Maxime Chevallier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204110622.1106492-11-maxime.chevallier@bootlin.com \
    --to=maxime.chevallier@bootlin.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=christophe.leroy@csgroup.eu \
    --cc=conor+dt@kernel.org \
    --cc=daniel@makrotopia.org \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=dimitri.fedrau@liebherr.com \
    --cc=edumazet@kernel.org \
    --cc=f.fainelli@gmail.com \
    --cc=f@lex.la \
    --cc=frank.wunderlich@linux.dev \
    --cc=gregory.clement@bootlin.com \
    --cc=herve.codina@bootlin.com \
    --cc=hkallweit1@gmail.com \
    --cc=horms@kernel.org \
    --cc=kabel@kernel.org \
    --cc=kory.maincent@bootlin.com \
    --cc=krzk+dt@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=marcin.s.wojtas@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=nicveronese@gmail.com \
    --cc=o.rempel@pengutronix.de \
    --cc=p.ameruoso@live.it \
    --cc=pabeni@redhat.com \
    --cc=robh@kernel.org \
    --cc=sebastian.hesselbarth@gmail.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=vladimir.oltean@nxp.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox