Netdev List
 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 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device
Date: Fri,  4 Dec 2026 12:06:13 +0100	[thread overview]
Message-ID: <20261204110622.1106492-5-maxime.chevallier@bootlin.com> (raw)
In-Reply-To: <20261204110622.1106492-1-maxime.chevallier@bootlin.com>

Like any other MII link, muxed MII's need all the involved components
(MAC, PCS, PHY, etc) to be involved for everything to work smoothly.

When switching from one port to another, we need to re-construct the
link and set all parameters, as both ports may be using different kinds
of technologies (think SGMII PHY on one side, 1000BaseX SFP on the
other).

Setting-up a mux therefore requires attaching the mux to its parent
(i.e. the MAC), and let its parent register callbacks to be notified
when switching occurs:

      /--- parent_ops
      |
      v     +-----+
+-----+     |     |------ PHY
| MAC | ----| MUX |
+-----+     |     |------ SFP
            +-----+

In practice, these parent_ops will be implemented by phylink.

The mux needs the netdev reference too as it's required when adding the
devlink dependency.

All PHYs in the mux are attached to the netdev, meaning we end-up with
more than one PHY attached to the netdev. We override the link
notification callback from a mux provided one.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phy_mux.c | 196 +++++++++++++++++++++++++++++++++++++-
 include/linux/phy_mux.h   |  39 ++++++++
 2 files changed, 232 insertions(+), 3 deletions(-)

diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
index e259eb4e271a..691bca09e0af 100644
--- a/drivers/net/phy/phy_mux.c
+++ b/drivers/net/phy/phy_mux.c
@@ -3,9 +3,11 @@
 #include <linux/device.h>
 #include <linux/list.h>
 #include <linux/mutex.h>
+#include <linux/netdevice.h>
 #include <linux/phy.h>
 #include <linux/phy_mux.h>
 #include <linux/property.h>
+#include <linux/rtnetlink.h>
 #include <linux/slab.h>
 #include <linux/xarray.h>
 
@@ -16,12 +18,196 @@ struct phy_mux {
 	struct list_head node;
 	struct device *dev;
 
+	struct net_device *netdev;
+	struct device_link *devlink;
+	const struct phy_mux_parent_ops *pops;
+	void *parent_priv;
+
 	struct xarray ports;
 	int n_ports;
 	int max_n_ports;
-
 };
 
+static void phy_mux_phy_link_change(struct phy_device *phydev, bool up)
+{
+}
+
+static int phy_mux_port_bind_phy(struct phy_mux_port *mux_port)
+{
+	struct phy_device *phydev;
+
+	phydev = fwnode_phy_find_device(mux_port->fwnode);
+	if (!phydev)
+		return -EPROBE_DEFER;
+
+	if (phydev->mux_port) {
+		put_device(&phydev->mdio.dev);
+		return -EBUSY;
+	}
+
+	mux_port->phydev = phydev;
+	phydev->mux_port = mux_port;
+
+	return 0;
+}
+
+static void phy_mux_port_unbind_phy(struct phy_mux_port *mux_port)
+{
+	struct phy_device *phydev = mux_port->phydev;
+
+	if (!phydev)
+		return;
+
+	phydev->mux_port = NULL;
+	mux_port->phydev = NULL;
+
+	put_device(&phydev->mdio.dev);
+}
+
+static void phy_mux_detach_phys(struct phy_mux *mux)
+{
+	struct phy_mux_port *port;
+	unsigned long id;
+
+	xa_for_each(&mux->ports, id, port) {
+		if (!port->attached)
+			continue;
+
+		phy_detach(port->phydev);
+		phy_mux_port_unbind_phy(port);
+		port->attached = false;
+	}
+}
+
+/**
+ * phy_mux_attach() - Attach the mux to its parent
+ * @mux: The mux
+ * @dev: The net_device the mux leads to
+ * @phy_flags: PHY device flags, for the PHYs sitting behind the mux
+ * @pops: The parent's callbacks
+ * @priv: Passed back to @pops
+ *
+ * All the PHYs behind the mux are looked up and attached to @dev, and stay
+ * attached until phy_mux_detach(). The parent only gets to use the one that
+ * is selected.
+ *
+ * Returns: 0 on success, -EBUSY if already attached, -EPROBE_DEFER if a PHY
+ * isn't there yet, or a negative error.
+ */
+static int phy_mux_attach(struct phy_mux *mux, struct net_device *dev,
+			  u32 phy_flags,
+			  const struct phy_mux_parent_ops *pops, void *priv)
+{
+	struct phy_mux_port *port;
+	bool has_phys = false;
+	unsigned long id;
+	int ret;
+
+	if (mux->netdev)
+		return -EBUSY;
+
+	xa_for_each(&mux->ports, id, port) {
+		if (port->type != PHY_MUX_PORT_PHY)
+			continue;
+
+		ret = phy_mux_port_bind_phy(port);
+		if (ret)
+			goto err_detach;
+
+		ret = phy_attach_direct(dev, port->phydev, phy_flags,
+					port->interface);
+		if (ret) {
+			phy_mux_port_unbind_phy(port);
+			goto err_detach;
+		}
+
+		port->phydev->phy_link_change = phy_mux_phy_link_change;
+		port->attached = true;
+		has_phys = true;
+	}
+
+	if (has_phys)
+		dev->phydev = NULL;
+
+	if (dev->dev.parent)
+		mux->devlink = device_link_add(dev->dev.parent, mux->dev,
+					       DL_FLAG_STATELESS);
+
+	mux->pops = pops;
+	mux->parent_priv = priv;
+	mux->netdev = dev;
+
+	if (pops && pops->attach)
+		pops->attach(mux, priv);
+
+	return 0;
+
+err_detach:
+	phy_mux_detach_phys(mux);
+
+	return ret;
+}
+
+/**
+ * fwnode_phy_mux_attach() - Find a mux from its firmware node and attach it
+ * @fwnode: The mux device's fwnode
+ * @dev: The net_device the mux leads to
+ * @phy_flags: PHY device flags, for the PHYs sitting behind the mux
+ * @pops: The parent's callbacks
+ * @priv: Passed back to @pops
+ *
+ * The parent gets the mux through its attach callback, called before the
+ * mux can be unregistered.
+ *
+ * Returns: 0 on success, -EPROBE_DEFER if no mux is registered yet for that
+ * node, or a negative error.
+ */
+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)
+{
+	int ret = -EPROBE_DEFER;
+	struct phy_mux *mux;
+
+	mutex_lock(&phy_mux_mutex);
+	list_for_each_entry(mux, &phy_muxes, node) {
+		if (dev_fwnode(mux->dev) != fwnode)
+			continue;
+
+		ret = phy_mux_attach(mux, dev, phy_flags, pops, priv);
+		break;
+	}
+	mutex_unlock(&phy_mux_mutex);
+
+	return ret;
+}
+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.
+ */
+void phy_mux_detach(struct phy_mux *mux)
+{
+	if (!mux->netdev)
+		return;
+
+	phy_mux_detach_phys(mux);
+
+	if (mux->devlink) {
+		device_link_del(mux->devlink);
+		mux->devlink = NULL;
+	}
+
+	mux->pops = NULL;
+	mux->parent_priv = NULL;
+	mux->netdev = NULL;
+}
+EXPORT_SYMBOL_GPL(phy_mux_detach);
+
 /**
  * phy_mux_alloc() - Allocate a mux
  * @dev: The mux device
@@ -52,6 +238,7 @@ static void phy_mux_port_destroy(struct phy_mux_port *mux_port)
 {
 	switch (mux_port->type) {
 	case PHY_MUX_PORT_PHY:
+		phy_mux_port_unbind_phy(mux_port);
 		fwnode_handle_put(mux_port->fwnode);
 		break;
 	}
@@ -160,14 +347,17 @@ EXPORT_SYMBOL_GPL(phy_mux_register);
 /**
  * phy_mux_unregister() - Make the mux unavailable to its parent
  * @mux: The mux, registered
- *
- * The mux still has to be freed with phy_mux_free().
  */
 void phy_mux_unregister(struct phy_mux *mux)
 {
 	mutex_lock(&phy_mux_mutex);
 	list_del(&mux->node);
 	mutex_unlock(&phy_mux_mutex);
+
+	rtnl_lock();
+	if (mux->netdev && mux->pops && mux->pops->detach)
+		mux->pops->detach(mux, mux->parent_priv);
+	rtnl_unlock();
 }
 EXPORT_SYMBOL_GPL(phy_mux_unregister);
 
diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
index 3dde6dd57faf..cdc58b56a3b4 100644
--- a/include/linux/phy_mux.h
+++ b/include/linux/phy_mux.h
@@ -7,6 +7,7 @@
 
 struct device;
 struct fwnode_handle;
+struct net_device;
 struct phy_mux;
 struct phy_mux_port;
 
@@ -23,6 +24,7 @@ enum phy_mux_port_type {
  *	    mux is attached
  * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY
  * @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
  */
 struct phy_mux_port {
 	u32 id;
@@ -32,8 +34,24 @@ struct phy_mux_port {
 	struct fwnode_handle *fwnode;
 
 	phy_interface_t interface;
+
+	bool attached;
+};
+
+/**
+ * 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.
+ * @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);
+	void (*detach)(struct phy_mux *mux, void *priv);
 };
 
+#if IS_ENABLED(CONFIG_PHY_MUX)
+
 struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports);
 int phy_mux_fwnode_add_ports(struct phy_mux *mux,
 			     const struct fwnode_handle *fwnode);
@@ -41,4 +59,25 @@ int phy_mux_register(struct phy_mux *mux);
 void phy_mux_unregister(struct phy_mux *mux);
 void phy_mux_free(struct phy_mux *mux);
 
+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);
+
+#else
+
+static inline 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)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void phy_mux_detach(struct phy_mux *mux)
+{
+}
+
+#endif /* CONFIG_PHY_MUX */
+
 #endif /* __PHY_MUX_H */
-- 
2.56.1


  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 ` Maxime Chevallier [this message]
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 ` [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-5-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