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 ACB4F606A322 for ; Fri, 4 Dec 2026 11:08:11 +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=K/OKs1sa; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=ks0zYkH4 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=KGKJgqUlBm873GiLBYXxD17oQUTh2RcMCYYfu9+BjbU=; b=K/OKs1saoIlECb a7jkmLF9ZZU/6pVxITQYMxxnsjJDA2v5UTSKa6etrK2jF/1l/91it/MyoSMOhWPgiehuvjRPOtoyL tL3NoKFTr7qoTNEID56X6B2MbF3uusI/3OJrOILnD60o5efpc7oICyUJagz+Di38LlA3IXxR4ciVx V3yUOATAGXRn95MlLVSgxjldtDKgDEB4bkI3f2vIe2hJ8TcyhA7QRGByjaXP+rPbgkpAIMJH7fJQW kWYE4kxas6rrDULEJQezISOsahzISOOIm+24enWd3Rj/j1YnfUNHoayjDyDw9Fo4xQJLlc6P7gxo3 W2MJDlM8vEtyLTkYUgFw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZR8l-000000004Og-2EdQ; Fri, 04 Dec 2026 11:07:47 +0000 Received: from smtpout-03.galae.net ([185.246.85.4]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZR8W-000000004J1-46ug for linux-arm-kernel@lists.infradead.org; Fri, 04 Dec 2026 11:07:36 +0000 Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 4E5444E4185D; Fri, 4 Dec 2026 11:07:31 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 20BE3603C4; Fri, 4 Dec 2026 11:07:31 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 8527B11EA18F6; Fri, 04 Dec 2026 12:07:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1796382448; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=KGKJgqUlBm873GiLBYXxD17oQUTh2RcMCYYfu9+BjbU=; b=ks0zYkH4xtfWEgMMMElEfm8HMZDTSQSOrQuE1TfkHzDDTsFWXVNJoVLro/BoksgPSqiOkI d6wBXaolDIlR2JTqR1Cu0kypgDThDHOLKi2yqngDM2hZpZUmFf+C8n5eRpcDDdAg9vgvW/ ncCjeLEnFhBxtWJqcyWM2FLF/2YV61WIciRIN/KzsuG7VagRO/cIFMygxEQyOnkpGNu61m Z6kZ7dKGlNKMzArfwKoz0LANgaq1ETQNnLdU8UZNDH8EYOm8rGPNxPw7SW6XxPZ0UiagwG GQwNjBi08x5xLAuPZknlA3VwjaqOWiJ+2qZ5met7QhXJtKYJdxEyz42e29im9g== 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 06/12] net: phy: phy_mux: Support SFP cage ports Date: Fri, 4 Dec 2026 12:06:15 +0100 Message-ID: <20261204110622.1106492-7-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_030735_728932_4D402E61 X-CRM114-Status: GOOD ( 31.61 ) 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 It's quite common to have combo ports where one of the ports is an SFP cage, let's allow adding one on the mux. To keep things realistic, we only allow one SFP cage per combo port. When an SFP cage doesn't have any module inserted, it doesn't really make sense to allow using it as the active port, so we need to keep track of whether there's a module or not. There's a simple way to implement a combo port that we find on Turris Omnia for example, that is to wire the SFP cage's mod_def0 to the physical multiplexer. In this case, we can't select the port to use from the host, but we can report that the port is in use and shutdown the unused port. For that, we however need to indicate that the SFP cage always takes precedence, as represented in the auto-mux binding. So, add support for attaching SFP cages to muxes, and implement the automatic mux. Signed-off-by: Maxime Chevallier --- drivers/net/phy/phy_mux.c | 190 ++++++++++++++++++++++++++++++++++++-- include/linux/phy_mux.h | 34 ++++++- 2 files changed, 213 insertions(+), 11 deletions(-) diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c index 407269980edc..de3336970bd9 100644 --- a/drivers/net/phy/phy_mux.c +++ b/drivers/net/phy/phy_mux.c @@ -9,6 +9,7 @@ #include #include #include +#include #include static LIST_HEAD(phy_muxes); @@ -33,9 +34,15 @@ struct phy_mux { int max_n_ports; struct phy_mux_port *current_port; + struct work_struct state_work; enum phy_mux_state state; }; +static void phy_mux_trigger(struct phy_mux *mux) +{ + queue_work(system_freezable_power_efficient_wq, &mux->state_work); +} + static void phy_mux_port_deselect(struct phy_mux_port *port) { if (port->ops && port->ops->deselect) @@ -122,6 +129,109 @@ static const struct phy_mux_port_ops phy_mux_phy_power_ops = { .deselect = phy_mux_phy_power_down, }; +static struct phy_mux_port *phy_mux_choose_port(struct phy_mux *mux) +{ + struct phy_mux_port *port; + unsigned long id; + + xa_for_each(&mux->ports, id, port) + if (port->has_precedence && port->available) + return port; + + xa_for_each(&mux->ports, id, port) + if (port->available) + return port; + + return NULL; +} + +static void phy_mux_state_machine(struct work_struct *work) +{ + struct phy_mux *mux = container_of(work, struct phy_mux, state_work); + + rtnl_lock(); + + switch (mux->state) { + case PHY_MUX_STOPPED: + break; + + case PHY_MUX_STARTED: + phy_mux_select(mux, phy_mux_choose_port(mux)); + break; + } + + rtnl_unlock(); +} + +/** + * phy_mux_port_set_available() - Report whether a port has a medium to use + * @port: The mux port + * @available: true when there is something to link with on that port + * + * The parent of the mux calls this for the SFP cage port, when a module gets + * plugged or removed. Caller must hold rtnl. + */ +void phy_mux_port_set_available(struct phy_mux_port *port, bool available) +{ + struct phy_mux *mux = port->mux; + + ASSERT_RTNL(); + + if (port->available == available) + return; + + port->available = available; + + if (available && mux->state == PHY_MUX_STARTED) { + phy_mux_select(mux, phy_mux_choose_port(mux)); + return; + } + + phy_mux_trigger(mux); +} +EXPORT_SYMBOL_GPL(phy_mux_port_set_available); + +/** + * phy_mux_get_sfp_port() - Get the SFP cage port of a mux + * @mux: The mux + * + * A mux has at most one SFP cage port. + * + * Returns: the SFP cage port, or NULL when the mux has none. + */ +struct phy_mux_port *phy_mux_get_sfp_port(struct phy_mux *mux) +{ + struct phy_mux_port *port; + unsigned long id; + + xa_for_each(&mux->ports, id, port) + if (port->type == PHY_MUX_PORT_SFP) + return port; + + return NULL; +} +EXPORT_SYMBOL_GPL(phy_mux_get_sfp_port); + +/** + * fwnode_phy_mux_find_sfp_port() - Find the SFP cage port of a mux + * @fwnode: The mux device's fwnode + * + * Returns: the fwnode of the port carrying an sfp property, with a reference + * held, or NULL. + */ +struct fwnode_handle * +fwnode_phy_mux_find_sfp_port(const struct fwnode_handle *fwnode) +{ + struct fwnode_handle *child; + + fwnode_for_each_available_child_node(fwnode, child) + if (fwnode_property_present(child, "sfp")) + return child; + + return NULL; +} +EXPORT_SYMBOL_GPL(fwnode_phy_mux_find_sfp_port); + static int phy_mux_port_bind_phy(struct phy_mux_port *mux_port) { struct phy_device *phydev; @@ -312,13 +422,8 @@ EXPORT_SYMBOL_GPL(phy_mux_detach); */ 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); + phy_mux_trigger(mux); } EXPORT_SYMBOL_GPL(phy_mux_start); @@ -335,6 +440,8 @@ void phy_mux_stop(struct phy_mux *mux) mux->state = PHY_MUX_STOPPED; phy_mux_select(mux, NULL); + + cancel_work(&mux->state_work); } EXPORT_SYMBOL_GPL(phy_mux_stop); @@ -356,6 +463,7 @@ struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports) return ERR_PTR(-ENOMEM); xa_init_flags(&mux->ports, XA_FLAGS_ALLOC1); + INIT_WORK(&mux->state_work, phy_mux_state_machine); mux->dev = dev; mux->max_n_ports = max_n_ports; @@ -371,6 +479,9 @@ static void phy_mux_port_destroy(struct phy_mux_port *mux_port) phy_mux_port_unbind_phy(mux_port); fwnode_handle_put(mux_port->fwnode); break; + case PHY_MUX_PORT_SFP: + fwnode_handle_put(mux_port->fwnode); + break; } kfree(mux_port); @@ -396,29 +507,80 @@ static int phy_mux_register_port(struct phy_mux *mux, if (ret) return ret; + port->available = true; + mux->n_ports++; return 0; } +static int phy_mux_add_sfp_port(struct phy_mux *mux, + struct fwnode_handle *child, + int (*init_port)(struct phy_mux_port *port, + struct fwnode_handle *child)) +{ + struct phy_mux_port *port; + int ret; + + if (phy_mux_get_sfp_port(mux)) + return -EBUSY; + + port = kzalloc_obj(*port); + if (!port) + return -ENOMEM; + + port->type = PHY_MUX_PORT_SFP; + port->fwnode = fwnode_handle_get(child); + port->mux = mux; + + if (init_port) { + ret = init_port(port, child); + if (ret) { + phy_mux_port_destroy(port); + return ret; + } + } + + ret = phy_mux_register_port(mux, port); + if (ret) { + phy_mux_port_destroy(port); + return ret; + } + + port->available = false; + + return 0; +} + /** * phy_mux_fwnode_add_ports() - Create the ports described under a fwnode * @mux: The mux - * @fwnode: The mux device's fwnode, each child being a port with a - * phy-handle and a phy-mode + * @fwnode: The mux device's fwnode, each child being a port with either a + * phy-handle and a phy-mode, or an sfp property + * @init_port: Optional driver callback to parse the rest of a port's node * * The PHYs themselves are only looked up when the mux gets attached. * * Returns: 0 on success or a negative error. */ int phy_mux_fwnode_add_ports(struct phy_mux *mux, - const struct fwnode_handle *fwnode) + const struct fwnode_handle *fwnode, + int (*init_port)(struct phy_mux_port *port, + struct fwnode_handle *child)) { struct fwnode_handle *child, *phy_fwnode; struct phy_mux_port *port; int ret; fwnode_for_each_available_child_node(fwnode, child) { + if (fwnode_property_present(child, "sfp")) { + ret = phy_mux_add_sfp_port(mux, child, init_port); + if (ret) + goto err; + + continue; + } + phy_fwnode = fwnode_get_phy_node(child); if (IS_ERR(phy_fwnode)) { ret = PTR_ERR(phy_fwnode); @@ -444,6 +606,14 @@ int phy_mux_fwnode_add_ports(struct phy_mux *mux, } port->interface = ret; + if (init_port) { + ret = init_port(port, child); + if (ret) { + phy_mux_port_destroy(port); + goto err; + } + } + ret = phy_mux_register_port(mux, port); if (ret) { phy_mux_port_destroy(port); @@ -502,6 +672,8 @@ void phy_mux_free(struct phy_mux *mux) struct phy_mux_port *port; unsigned long id; + disable_work_sync(&mux->state_work); + xa_for_each(&mux->ports, id, port) phy_mux_port_destroy(port); diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h index 6a42ffff02b7..77cf389b8c77 100644 --- a/include/linux/phy_mux.h +++ b/include/linux/phy_mux.h @@ -21,6 +21,7 @@ struct phy_mux_port_ops { enum phy_mux_port_type { PHY_MUX_PORT_PHY, + PHY_MUX_PORT_SFP, }; /** @@ -30,9 +31,14 @@ enum phy_mux_port_type { * @type: What sits behind that port * @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 + * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY. For + * PHY_MUX_PORT_SFP, the port's own firmware node, carrying the sfp + * property. * @ops: How to drive that port * @interface: The MII mode the link runs in when this port is selected + * @available: The port can be used (e.g. an SFP module is plugged) + * @has_precedence: The port is selected as soon as it is available, whatever + * the other ports do. * @attached: For PHY_MUX_PORT_PHY, the PHY is attached to the mux's netdev * @selected: The port currently drives the MII */ @@ -46,6 +52,8 @@ struct phy_mux_port { const struct phy_mux_port_ops *ops; phy_interface_t interface; + bool available; + bool has_precedence; bool attached; bool selected; }; @@ -73,7 +81,9 @@ struct phy_mux_parent_ops { 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); + const struct fwnode_handle *fwnode, + int (*init_port)(struct phy_mux_port *port, + struct fwnode_handle *child)); int phy_mux_register(struct phy_mux *mux); void phy_mux_unregister(struct phy_mux *mux); void phy_mux_free(struct phy_mux *mux); @@ -84,6 +94,10 @@ int fwnode_phy_mux_attach(const struct fwnode_handle *fwnode, void phy_mux_detach(struct phy_mux *mux); void phy_mux_start(struct phy_mux *mux); void phy_mux_stop(struct phy_mux *mux); +struct fwnode_handle * +fwnode_phy_mux_find_sfp_port(const struct fwnode_handle *fwnode); +struct phy_mux_port *phy_mux_get_sfp_port(struct phy_mux *mux); +void phy_mux_port_set_available(struct phy_mux_port *port, bool available); #else @@ -107,6 +121,22 @@ static inline void phy_mux_stop(struct phy_mux *mux) { } +static inline struct fwnode_handle * +fwnode_phy_mux_find_sfp_port(const struct fwnode_handle *fwnode) +{ + return NULL; +} + +static inline struct phy_mux_port *phy_mux_get_sfp_port(struct phy_mux *mux) +{ + return NULL; +} + +static inline void phy_mux_port_set_available(struct phy_mux_port *port, + bool available) +{ +} + #endif /* CONFIG_PHY_MUX */ #endif /* __PHY_MUX_H */ -- 2.56.1