[PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device
Maxime Chevallier
maxime.chevallier at bootlin.com
Fri Dec 4 03:06:13 PST 2026
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 at 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
More information about the linux-arm-kernel
mailing list