From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.246.84.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382446; b=bPnWvlyRITz1Cyl1n+ElIcxfmVHYUnAQWsLZHPul5S7/cz0Q9OY/qpA4e0LURkei4HF3 jesoTsYOKpVMgv+Zo0iN5N5e9sBikZimVf8C3+7hzxw7cc2OjW8K4hzZbuzfdMN3jAm9l BhU3JM3GVH1mWGJl2Tb51PMbr4oqPTnayCB35SsNo2uqdY0rwIocHQqW3w93ukmXPFvCY S8MG9W2AjKnFiDpUevelQqRNdX+QycP5mcujxkeLusXlnQI9ClXOgmBfDK4sDvjEfKvHc u6ym3DcCec5YN8k0/BJt50gpQStWKHQZfHdlNoPcXLurNo3iIygQXkCJ6p3WF2Bn/Ig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382446; h=DMARC-Filter:Received:Received:Received:DKIM-Signature:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Last-TLS-Session-Version; bh=nC8EOp5uJkLf4DZDBkITEJ7omvhj/1PRQsjmNya4Cko=; b=UpCiCqzpDZI0jnXiTuE0O7yiBpl/o8y3kBjHJWXtxfksVqdXiZ8lvRhg+gRfsaxmrqVx pOh/LFL284tD7oWVLFmX8s/ov3iJnd/iaD6JNr4H0p+jhUfT04NoYGT9Z1C/pWamY7EGO 7W1nu0XTCizuvytZG5F4rJqVAD25qXffLK69pjQo6MRZCDe1Jb2ugD7PMP/eQyF0FZ3gY 76arA60acU8nNRjs327tjzSmOfpqkgCSD+MDUa5HDONcJLh8bGJkMT+bTzHJIuqr5chK7 fy+FoV5p3ysfuBzNy97/X8aiZjiFxaNaVMIoEGNlttv+rFw+8HBb8lbQ9Mk3CKPns5g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=Q8x9kPzf; arc=none smtp.remote-ip=185.246.84.56 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (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 E4ADE606A333; Fri, 4 Dec 2026 11:07:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E4ADE606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=Q8x9kPzf Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id 9186D1A1947; Fri, 4 Dec 2026 11:07:23 +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-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 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