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 05/12] net: phy: phy_mux: Add port selection logic
Date: Fri, 4 Dec 2026 12:06:14 +0100 [thread overview]
Message-ID: <20261204110622.1106492-6-maxime.chevallier@bootlin.com> (raw)
In-Reply-To: <20261204110622.1106492-1-maxime.chevallier@bootlin.com>
As Muxes drive multiple MII ports but only one can be used at a time, we
need logic to select which one to use, and notify the parent (i.e. the
netdev).
Selecting a port tells the MAC to start using what's at the end of the
MII line (a PHY, an SFP cage, etc.), so that it can start configuring
its MAC, PCS, Serdes and so on accordingly.
Deselecting it tells the parent to stop using it.
All the ports are deselected when the mux gets attached, and nothing is
selected until the parent starts the mux with phy_mux_start(), which it
is expected to do when the interface is brought up.
The selection policy is as simple as it gets for now, the first port is
selected.
For that, we introduce mux_port_ops:
/--- parent_ops
|
| / phy_mux_port_ops
v +-----+ v
+-----+ | |------ PHY
| MAC | ----| MUX |
+-----+ | |------ SFP
+-----+ ^
\ phy_mux_port_ops
They allow notifying what's at the end of a mux'd MII line (a PHY, an SFP)
that it has been selected or deselected. That way it can act accordingly
to stop trying to reach the MAC.
For now, only deselect is implemented, the rest will come later.
Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
drivers/net/phy/phy_mux.c | 134 +++++++++++++++++++++++++++++++++++++-
include/linux/phy_mux.h | 29 +++++++++
2 files changed, 162 insertions(+), 1 deletion(-)
diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
index 691bca09e0af..407269980edc 100644
--- a/drivers/net/phy/phy_mux.c
+++ b/drivers/net/phy/phy_mux.c
@@ -14,6 +14,11 @@
static LIST_HEAD(phy_muxes);
static DEFINE_MUTEX(phy_mux_mutex);
+enum phy_mux_state {
+ PHY_MUX_STOPPED,
+ PHY_MUX_STARTED,
+};
+
struct phy_mux {
struct list_head node;
struct device *dev;
@@ -26,12 +31,97 @@ struct phy_mux {
struct xarray ports;
int n_ports;
int max_n_ports;
+
+ struct phy_mux_port *current_port;
+ enum phy_mux_state state;
};
+static void phy_mux_port_deselect(struct phy_mux_port *port)
+{
+ if (port->ops && port->ops->deselect)
+ port->ops->deselect(port);
+}
+
static void phy_mux_phy_link_change(struct phy_device *phydev, bool up)
{
}
+static void phy_mux_port_set_parent_phy(struct phy_mux *mux,
+ struct phy_mux_port *port, bool used)
+{
+ if (port->type != PHY_MUX_PORT_PHY)
+ return;
+
+ if (!used)
+ port->phydev->phy_link_change = phy_mux_phy_link_change;
+
+ if (mux->netdev)
+ mux->netdev->phydev = used ? port->phydev : NULL;
+}
+
+static void __phy_mux_deselect_port(struct phy_mux *mux,
+ struct phy_mux_port *port)
+{
+ if (mux->pops && mux->pops->deselect) {
+ mux->pops->deselect(mux, port, mux->parent_priv);
+ phy_mux_port_set_parent_phy(mux, port, false);
+ }
+
+ phy_mux_port_deselect(port);
+
+ port->selected = false;
+}
+
+static int __phy_mux_select_port(struct phy_mux *mux,
+ struct phy_mux_port *port)
+{
+ int ret;
+
+ if (mux->pops && mux->pops->select) {
+ ret = mux->pops->select(mux, port, mux->parent_priv);
+ phy_mux_port_set_parent_phy(mux, port, !ret);
+ if (ret)
+ return ret;
+ }
+
+ port->selected = true;
+
+ return 0;
+}
+
+static int phy_mux_select(struct phy_mux *mux, struct phy_mux_port *port)
+{
+ int ret;
+
+ if (mux->current_port == port)
+ return 0;
+
+ if (mux->current_port) {
+ __phy_mux_deselect_port(mux, mux->current_port);
+ mux->current_port = NULL;
+ }
+
+ if (!port)
+ return 0;
+
+ ret = __phy_mux_select_port(mux, port);
+ if (ret)
+ return ret;
+
+ mux->current_port = port;
+
+ return 0;
+}
+
+static void phy_mux_phy_power_down(struct phy_mux_port *port)
+{
+ phy_suspend(port->phydev);
+}
+
+static const struct phy_mux_port_ops phy_mux_phy_power_ops = {
+ .deselect = phy_mux_phy_power_down,
+};
+
static int phy_mux_port_bind_phy(struct phy_mux_port *mux_port)
{
struct phy_device *phydev;
@@ -129,6 +219,9 @@ static int phy_mux_attach(struct phy_mux *mux, struct net_device *dev,
if (has_phys)
dev->phydev = NULL;
+ xa_for_each(&mux->ports, id, port)
+ phy_mux_port_deselect(port);
+
if (dev->dev.parent)
mux->devlink = device_link_add(dev->dev.parent, mux->dev,
DL_FLAG_STATELESS);
@@ -188,13 +281,15 @@ EXPORT_SYMBOL_GPL(fwnode_phy_mux_attach);
* phy_mux_detach() - Detach the mux from its parent
* @mux: The mux
*
- * The PHYs behind the mux are detached.
+ * The mux is stopped and its PHYs are detached. Caller must hold rtnl when
+ * the mux was started.
*/
void phy_mux_detach(struct phy_mux *mux)
{
if (!mux->netdev)
return;
+ phy_mux_stop(mux);
phy_mux_detach_phys(mux);
if (mux->devlink) {
@@ -208,6 +303,41 @@ void phy_mux_detach(struct phy_mux *mux)
}
EXPORT_SYMBOL_GPL(phy_mux_detach);
+/**
+ * phy_mux_start() - Start selecting ports
+ * @mux: The mux, attached
+ *
+ * To be called when the parent is ready to use the PHY of the selected
+ * port, typically when the interface is brought up. Caller must hold rtnl.
+ */
+void phy_mux_start(struct phy_mux *mux)
+{
+ struct phy_mux_port *port;
+ unsigned long id = 0;
+
+ mux->state = PHY_MUX_STARTED;
+
+ port = xa_find(&mux->ports, &id, ULONG_MAX, XA_PRESENT);
+ phy_mux_select(mux, port);
+}
+EXPORT_SYMBOL_GPL(phy_mux_start);
+
+/**
+ * phy_mux_stop() - Deselect the port in use and stop selecting ports
+ * @mux: The mux
+ *
+ * Does nothing when the mux isn't started. Caller must hold rtnl otherwise.
+ */
+void phy_mux_stop(struct phy_mux *mux)
+{
+ if (mux->state < PHY_MUX_STARTED)
+ return;
+
+ mux->state = PHY_MUX_STOPPED;
+ phy_mux_select(mux, NULL);
+}
+EXPORT_SYMBOL_GPL(phy_mux_stop);
+
/**
* phy_mux_alloc() - Allocate a mux
* @dev: The mux device
@@ -305,6 +435,7 @@ int phy_mux_fwnode_add_ports(struct phy_mux *mux,
port->type = PHY_MUX_PORT_PHY;
port->fwnode = phy_fwnode;
port->mux = mux;
+ port->ops = &phy_mux_phy_power_ops;
ret = fwnode_get_phy_mode(child);
if (ret < 0) {
@@ -357,6 +488,7 @@ void phy_mux_unregister(struct phy_mux *mux)
rtnl_lock();
if (mux->netdev && mux->pops && mux->pops->detach)
mux->pops->detach(mux, mux->parent_priv);
+ phy_mux_stop(mux);
rtnl_unlock();
}
EXPORT_SYMBOL_GPL(phy_mux_unregister);
diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
index cdc58b56a3b4..6a42ffff02b7 100644
--- a/include/linux/phy_mux.h
+++ b/include/linux/phy_mux.h
@@ -11,6 +11,14 @@ struct net_device;
struct phy_mux;
struct phy_mux_port;
+/**
+ * struct phy_mux_port_ops - ops for the multiplexed mii ports of the mux
+ * @deselect: Disconnect the port from the MII
+ */
+struct phy_mux_port_ops {
+ void (*deselect)(struct phy_mux_port *port);
+};
+
enum phy_mux_port_type {
PHY_MUX_PORT_PHY,
};
@@ -23,8 +31,10 @@ enum phy_mux_port_type {
* @phydev: For PHY_MUX_PORT_PHY, the PHY device behind the port, while the
* mux is attached
* @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY
+ * @ops: How to drive that port
* @interface: The MII mode the link runs in when this port is selected
* @attached: For PHY_MUX_PORT_PHY, the PHY is attached to the mux's netdev
+ * @selected: The port currently drives the MII
*/
struct phy_mux_port {
u32 id;
@@ -33,20 +43,29 @@ struct phy_mux_port {
struct phy_device *phydev;
struct fwnode_handle *fwnode;
+ const struct phy_mux_port_ops *ops;
phy_interface_t interface;
bool attached;
+ bool selected;
};
/**
* struct phy_mux_parent_ops - Ops for the device providing the single MII
* that's multiplexed, usually the MAC
* @attach: The mux is attached to the parent.
+ * @select: A port was selected, the parent must now use it (e.g. connect
+ * to its PHY)
+ * @deselect: The port is no longer in use
* @detach: The mux is going away, the parent must detach from it. Called with
* rtnl held.
*/
struct phy_mux_parent_ops {
void (*attach)(struct phy_mux *mux, void *priv);
+ int (*select)(struct phy_mux *mux, struct phy_mux_port *port,
+ void *priv);
+ void (*deselect)(struct phy_mux *mux, struct phy_mux_port *port,
+ void *priv);
void (*detach)(struct phy_mux *mux, void *priv);
};
@@ -63,6 +82,8 @@ int fwnode_phy_mux_attach(const struct fwnode_handle *fwnode,
struct net_device *dev, u32 phy_flags,
const struct phy_mux_parent_ops *pops, void *priv);
void phy_mux_detach(struct phy_mux *mux);
+void phy_mux_start(struct phy_mux *mux);
+void phy_mux_stop(struct phy_mux *mux);
#else
@@ -78,6 +99,14 @@ static inline void phy_mux_detach(struct phy_mux *mux)
{
}
+static inline void phy_mux_start(struct phy_mux *mux)
+{
+}
+
+static inline void phy_mux_stop(struct phy_mux *mux)
+{
+}
+
#endif /* CONFIG_PHY_MUX */
#endif /* __PHY_MUX_H */
--
2.56.1
next prev parent reply other threads:[~2026-12-04 11:07 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 ` Maxime Chevallier [this message]
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 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
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-6-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