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 BB2CE606A333 for ; Fri, 4 Dec 2026 11:07:53 +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=DrOmA+Lb; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=Q8x9kPzf 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=nC8EOp5uJkLf4DZDBkITEJ7omvhj/1PRQsjmNya4Cko=; b=DrOmA+Lby2NK58 pp70tKo0uSi8KMeq+k+q/I9Yai3yUr1+RSrXIwaSAWypUF4P6tYtUirrcX2jPZnjgYwtk19r2zK2E q1SLqxld14YQgQ1AXBiAalCMgfHJ4CrYlzoPM3DeYsxK/P4Y2hZD0t3TtJzrpdJxoF1u4aVLMYzsA 4d0XfcO7oCVhC2y+rh2yeFascvcu/3uNXupAWiV1+v3JqFRy6PYqNN2kch7nVHsE4o+hKKjgWdjyR +P8sn6PFNtymW50LDGFbc1dyXpfljYM6EtbKIIL21CLiqxFue15Yzgwhy7noqGozKp7SKy5kfhMWM 1XGQXXIRmz3/Va8OCnfg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZR8l-000000004OK-0zvU; 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 1xZR8P-000000004HF-0IQv for linux-arm-kernel@lists.infradead.org; Fri, 04 Dec 2026 11:07:31 +0000 Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id 399DCC026D7; Fri, 4 Dec 2026 11:07:35 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 65045603C4; Fri, 4 Dec 2026 11:07:23 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id A13FF11EA187F; Fri, 04 Dec 2026 12:07:14 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1796382440; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=nC8EOp5uJkLf4DZDBkITEJ7omvhj/1PRQsjmNya4Cko=; b=Q8x9kPzf1h2fHJh/9pb73awcy///Mg9UyU4tJxIZMSlXrqgU999cqb6ELehn6coPdbBo8p rXnWdAGmd0NpsWNPxy00Lq+I0iDUwG3MEXHaknPQHagljBB73OvnLDDIUcJiDUFR9Uah03 dd8QYVOY/ZhQqLUKEZomiMJg3OEkZjaza8vJzl4iFX6nljZWa3sL4G98Y90exKGlupZmUD ZAhnbTBRZZKYlorkmzhsYQV50N81tFqjgfBdl5x7vGJi0JV5u+IZqGy9Wv0QmW2iZdwJKa emvwbiq0G3uPdzJyIDt6kN9pULw8mX37R/eSutOk10sm56DPjXggc/U1OYOakg== 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 05/12] net: phy: phy_mux: Add port selection logic Date: Fri, 4 Dec 2026 12:06:14 +0100 Message-ID: <20261204110622.1106492-6-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_030729_893228_C92AA7AE X-CRM114-Status: GOOD ( 27.12 ) 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 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 --- 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