From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B493606A68F for ; Fri, 4 Dec 2026 11:07:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=XdKUp2rN; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=T0pZ9o/k DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=X2cGz0hTr8Dg/J34mOYNM2EpUUvq44ABpcEcCjunb3U=; b=XdKUp2rNNnFWNK ydz9yNXGA0n9PvtE+d9DNGhbbfNVF5HHKUTungVGmXUuCpUv+vlt2q+jhpZjzTJt446N9oeyl5nn/ f4aoH/Q4CpAsQKNJ5HtrtQGBJmEV8MftF2OXlE5fLsYw1ul7mtfxNW3iA3RmN27yJFcUWpdBkMxNB xkRcaWzaTKmOxKd2dPbPvhLkJ06FsPg9BneZbGvRmMA0Z56lJcNYmAcE2yfQRkFUfMOVwk2Ttp2LH SaKvZ4DIlSUqa4342ThiZgxArD1mM7A5Sbywn5nhrB5tjpj90z40Udf9iTGrxZkB4s53xvUM2x77H Xl3yHbqAa/B4ApegtWqg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZR8l-000000004O8-0apw; Fri, 04 Dec 2026 11:07:47 +0000 Received: from smtpout-04.galae.net ([185.171.202.116]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZR8J-000000004Er-0F6J for linux-arm-kernel@lists.infradead.org; Fri, 04 Dec 2026 11:07:21 +0000 Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id 46AE9C026D8; Fri, 4 Dec 2026 11:07:29 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 72A9E603C4; Fri, 4 Dec 2026 11:07:17 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 6412711EA18F3; Fri, 04 Dec 2026 12:07:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1796382433; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=X2cGz0hTr8Dg/J34mOYNM2EpUUvq44ABpcEcCjunb3U=; b=T0pZ9o/kObWQqK7LxgSkjkWtI1cJvvZGAyA6ev53Hhmcq7y4n7F5KPgoM5aQbw9IO9Sha7 sjK6N+2YCCQJFM8LjV6C2q6sRg+KJL/T2XgDmC5p/dPNuHaXAd1bd9Eu1zCmBwu5fPeZWj 4V6XqUf/rI9tq+NRlUvnY02rOZuxNbly2P3LNEXK2jLyt+Fr4teHL0n5+Sy4u1A8tESVxq uXwALoXU3PLmHFrcGAUNUt5ZgM3YpfTm/a+PvdbyJIDKGYrJCTxWtuEhDldvLttM96CXSt kHQw/cKIIaiLTJCC0KZVWwHkScATv/b+Q8n1oWiBUwZ1bNf9ZWnyM7gjT8h5dA== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Willem de Bruijn , Paolo Abeni , Russell King , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Sebastian Hesselbarth , Gregory Clement , Heiner Kallweit Cc: Maxime Chevallier , 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 , Krzysztof Kozlowski , Rob Herring , Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , marcin.s.wojtas@gmail.com, Daniel Golle , Dimitri Fedrau , Frank Wunderlich , Pietro Ameruoso , Aleksei Sviridkin 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 Message-ID: <20261204110622.1106492-5-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204110622.1106492-1-maxime.chevallier@bootlin.com> References: <20261204110622.1106492-1-maxime.chevallier@bootlin.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261204_030719_601831_CBBCC8E3 X-CRM114-Status: GOOD ( 24.42 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-19.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 --- 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 #include #include +#include #include #include #include +#include #include #include @@ -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