From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E4761DF75A for ; Wed, 25 Mar 2026 08:20:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426803; cv=none; b=p6MYKLYw+23Yd8gIxD5zsdL5q4nRkC+V8u86WpddpHSc6/BN0arLj/ft4eJSyBzT+15qd6WExysHV2SB5bwKnp3rQFRBjbYTTF1UaxmnlSCmWF0/prPgRNo5j8y74tMTY/yNvmq7TqIcLtMAYovcID2kun284sJiM2qumu5K9tA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426803; c=relaxed/simple; bh=Wsz2AZSfO3CMZ9hi9RAex30t9IvMTQzL9FzkNmtSFG8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jPK57XsXGLNgvYyzxUQnghje589roWbRPP5l23w86mo9GnVYD5ipGsZkjmhyaC+cCtH1dk7EDhpHv+COyewOdECrabb2A2D62q2OqxFZZEV7wwpzPQGHOl5kVloNR4xCXnkved8533+EInum5NkLTzWr1lHRTeq9KDF2wCbUaGs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=1fo/NWv7; arc=none smtp.client-ip=185.171.202.116 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.b="1fo/NWv7" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id 2E6CBC580AB; Wed, 25 Mar 2026 08:20:27 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 8228A601A1; Wed, 25 Mar 2026 08:19:59 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 5BB901045037C; Wed, 25 Mar 2026 09:19:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426798; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=AR1POQwTIVmZIlP545WnmjBu2iS3sERk3rgCHJfHWpA=; b=1fo/NWv7fZDybaqhrOeVdcNO308c1+uawuo+CdfSkStUQ2kOXtf0mfBZ7tqAHP5fqTqbsc AFBX9n5VLv8FvcmHELXthMnkpEvpR1df1ns0s38LRoZdi6JDNd/EK6dcdl8C8X/KGyF1lQ m3uKsrzXMZL+VpYgHvibGf+gogKIPwT8YtH2JLqwh/IlybFV9RXqMs5WvG4snAebo8yyam wQVMK31H5rP1o56bE/JFSDoS58HA3okFtBT0+Xk/PNK9BOVdEbno4ZRI/cnsdqclEUoAH3 5QFj9J3x4LNH70MfM8QwA1Ilh5oRO+Pt81Qh5PW4qTTc2Ho0bCrLt+xa588A0Q== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 01/10] net: phy: phy_link_topology: Add a helper for opportunistic alloc Date: Wed, 25 Mar 2026 09:19:26 +0100 Message-ID: <20260325081937.571115-2-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" The phy_link_topology structure stores information about the PHY-related components connected to a net_device. It is opportunistically allocated, when we add the first item to the topology, as this is not relevant for all kinds of net_devices. In preparation for the addition of phy_port tracking in the topology, let's make a dedicated helper for that allocation sequence. Signed-off-by: Maxime Chevallier --- drivers/net/phy/phy_link_topology.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/drivers/net/phy/phy_link_topology.c b/drivers/net/phy/phy_link= _topology.c index 1f1eb5d59b38..fdfafd951905 100644 --- a/drivers/net/phy/phy_link_topology.c +++ b/drivers/net/phy/phy_link_topology.c @@ -27,21 +27,34 @@ static int netdev_alloc_phy_link_topology(struct net_de= vice *dev) return 0; } =20 +static struct phy_link_topology *phy_link_topo_get_or_alloc(struct net_dev= ice *dev) +{ + int ret; + + if (dev->link_topo) + return dev->link_topo; + + /* The topology is allocated the first time we add an object to it. + * It is freed alongside the netdev. + */ + ret =3D netdev_alloc_phy_link_topology(dev); + if (ret) + return ERR_PTR(ret); + + return dev->link_topo; +} + int phy_link_topo_add_phy(struct net_device *dev, struct phy_device *phy, enum phy_upstream upt, void *upstream) { - struct phy_link_topology *topo =3D dev->link_topo; + struct phy_link_topology *topo; struct phy_device_node *pdn; int ret; =20 - if (!topo) { - ret =3D netdev_alloc_phy_link_topology(dev); - if (ret) - return ret; - - topo =3D dev->link_topo; - } + topo =3D phy_link_topo_get_or_alloc(dev); + if (IS_ERR(topo)) + return PTR_ERR(topo); =20 pdn =3D kzalloc_obj(*pdn); if (!pdn) --=20 2.49.0 From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D71B39891F for ; Wed, 25 Mar 2026 08:20:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.85.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426807; cv=none; b=AXC5CC4t8x16W4PwkgDFsva2d1iLwShWCdJ4PpR8fdQ6DOtpl8Qj9IdqzzHr0ZkCxr3R8qcSrBZepLTEWyWAaRtLIqAneLcdpMNpCkDpYR7eXxJDMHRjWv9t6ASLpAEjm4VLb6jze+sB3XTol/HWLTTbjTmpLYDUOfr5hSXi+N0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426807; c=relaxed/simple; bh=Cpplk4Mnx3hfXmc4blGhcmVbB7Eo+SGFJoOIrBjBDFI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M+FTXkhYPigItiIqPgYIzEL/9TPWvseSPbBYsmLQmrbDt8vLznXa/fuLyNYvIcmRnWkK31QqlrK33Pm7/bRSjcCclWlveU5KLkl6CEWgrewcl3iFWC3Rn0yCzuD1bV5zJLVFhsUPfJJ0NAqskkVR6/i6fYPkuJgmeRrFWp40WDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=rV/jL973; arc=none smtp.client-ip=185.246.85.4 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.b="rV/jL973" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 662A04E427EC; Wed, 25 Mar 2026 08:20:03 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 1BA4D601A1; Wed, 25 Mar 2026 08:20:03 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 05D6610451367; Wed, 25 Mar 2026 09:19:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426801; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=53D6dSIeAVBcDkwAxX7FgjypCmx8gL0tNh0pf/go21s=; b=rV/jL9739LGKczz+3CTHJ/gaatacZOn4A8jbvKhjZNmduhntHjI8wutJV+P1A8DiNJpNk2 uP/qw0xzG7l/6TaoNTOnhUBWAMKUV7EkgnjqtSSbOoFsBn6mQqiJ7zTZZZwtWK5zKK5/bV eRfTwbuHoajefbEVRAKNm2IbPq4zhCs3YsB3ELXFfStdFky4uECyiwUp0a8qRptuwggJcF shlQW++YzJH7FT1qLLqPS14zzLwb8rZMsqf5Zwto9EtP6j7e4wzLbLkvPtjzSlhqgxL8/M 1d+EjVk9+csVRcGkFHwAxWKxXmFKQd5pejDysaXRNCopfnakQYypC5cPOupyuQ== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 02/10] net: phy: phy_link_topology: Track ports in phy_link_topology Date: Wed, 25 Mar 2026 09:19:27 +0100 Message-ID: <20260325081937.571115-3-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" phy_port is aimed at representing the various physical interfaces of a net_device. They can be controlled by various components in the link, such as the Ethernet PHY, the Ethernet MAC, and SFP module, etc. Let's therefore make so we keep track of all the ports connected to a netdev in phy_link_topology. The only ports added for now are phy-driven ports. Signed-off-by: Maxime Chevallier --- drivers/net/phy/phy_link_topology.c | 53 +++++++++++++++++++++++++++++ include/linux/phy_link_topology.h | 18 ++++++++++ include/linux/phy_port.h | 2 ++ net/core/dev.c | 1 + 4 files changed, 74 insertions(+) diff --git a/drivers/net/phy/phy_link_topology.c b/drivers/net/phy/phy_link= _topology.c index fdfafd951905..207128303ca2 100644 --- a/drivers/net/phy/phy_link_topology.c +++ b/drivers/net/phy/phy_link_topology.c @@ -7,6 +7,7 @@ */ =20 #include +#include #include #include #include @@ -22,6 +23,9 @@ static int netdev_alloc_phy_link_topology(struct net_devi= ce *dev) xa_init_flags(&topo->phys, XA_FLAGS_ALLOC1); topo->next_phy_index =3D 1; =20 + xa_init_flags(&topo->ports, XA_FLAGS_ALLOC1); + topo->next_port_index =3D 1; + dev->link_topo =3D topo; =20 return 0; @@ -44,12 +48,45 @@ static struct phy_link_topology *phy_link_topo_get_or_a= lloc(struct net_device *d return dev->link_topo; } =20 +int phy_link_topo_add_port(struct net_device *dev, struct phy_port *port) +{ + struct phy_link_topology *topo; + int ret; + + topo =3D phy_link_topo_get_or_alloc(dev); + if (IS_ERR(topo)) + return PTR_ERR(topo); + + /* Attempt to re-use a previously allocated port_id */ + if (port->id) + ret =3D xa_insert(&topo->ports, port->id, port, GFP_KERNEL); + else + ret =3D xa_alloc_cyclic(&topo->ports, &port->id, port, + xa_limit_32b, &topo->next_port_index, + GFP_KERNEL); + + return ret; +} +EXPORT_SYMBOL_GPL(phy_link_topo_add_port); + +void phy_link_topo_del_port(struct net_device *dev, struct phy_port *port) +{ + struct phy_link_topology *topo =3D dev->link_topo; + + if (!topo) + return; + + xa_erase(&topo->ports, port->id); +} +EXPORT_SYMBOL_GPL(phy_link_topo_del_port); + int phy_link_topo_add_phy(struct net_device *dev, struct phy_device *phy, enum phy_upstream upt, void *upstream) { struct phy_link_topology *topo; struct phy_device_node *pdn; + struct phy_port *port; int ret; =20 topo =3D phy_link_topo_get_or_alloc(dev); @@ -89,8 +126,20 @@ int phy_link_topo_add_phy(struct net_device *dev, if (ret < 0) goto err; =20 + /* Add all the PHY's ports to the topology */ + list_for_each_entry(port, &phy->ports, head) { + ret =3D phy_link_topo_add_port(dev, port); + if (ret) + goto del_ports; + } + return 0; =20 +del_ports: + list_for_each_entry_continue_reverse(port, &phy->ports, head) + phy_link_topo_del_port(dev, port); + + xa_erase(&topo->phys, phy->phyindex); err: kfree(pdn); return ret; @@ -102,10 +151,14 @@ void phy_link_topo_del_phy(struct net_device *dev, { struct phy_link_topology *topo =3D dev->link_topo; struct phy_device_node *pdn; + struct phy_port *port; =20 if (!topo) return; =20 + list_for_each_entry(port, &phy->ports, head) + phy_link_topo_del_port(dev, port); + pdn =3D xa_erase(&topo->phys, phy->phyindex); =20 /* We delete the PHY from the topology, however we don't re-set the diff --git a/include/linux/phy_link_topology.h b/include/linux/phy_link_top= ology.h index 68a59e25821c..66bceff72b19 100644 --- a/include/linux/phy_link_topology.h +++ b/include/linux/phy_link_topology.h @@ -16,11 +16,15 @@ =20 struct xarray; struct phy_device; +struct phy_port; struct sfp_bus; =20 struct phy_link_topology { struct xarray phys; u32 next_phy_index; + + struct xarray ports; + u32 next_port_index; }; =20 struct phy_device_node { @@ -43,6 +47,9 @@ int phy_link_topo_add_phy(struct net_device *dev, =20 void phy_link_topo_del_phy(struct net_device *dev, struct phy_device *phy); =20 +int phy_link_topo_add_port(struct net_device *dev, struct phy_port *port); +void phy_link_topo_del_port(struct net_device *dev, struct phy_port *port); + static inline struct phy_device * phy_link_topo_get_phy(struct net_device *dev, u32 phyindex) { @@ -72,6 +79,17 @@ static inline void phy_link_topo_del_phy(struct net_devi= ce *dev, { } =20 +static inline int phy_link_topo_add_port(struct net_device *dev, + struct phy_port *port) +{ + return 0; +} + +static inline void phy_link_topo_del_port(struct net_device *dev, + struct phy_port *port) +{ +} + static inline struct phy_device * phy_link_topo_get_phy(struct net_device *dev, u32 phyindex) { diff --git a/include/linux/phy_port.h b/include/linux/phy_port.h index 0ef0f5ce4709..4e2a3fdd2f2e 100644 --- a/include/linux/phy_port.h +++ b/include/linux/phy_port.h @@ -36,6 +36,7 @@ struct phy_port_ops { /** * struct phy_port - A representation of a network device physical interfa= ce * + * @id: Unique identifier for the port within the topology * @head: Used by the port's parent to list ports * @parent_type: The type of device this port is directly connected to * @phy: If the parent is PHY_PORT_PHYDEV, the PHY controlling that port @@ -52,6 +53,7 @@ struct phy_port_ops { * @is_sfp: Indicates if this port drives an SFP cage. */ struct phy_port { + u32 id; struct list_head head; enum phy_port_parent parent_type; union { diff --git a/net/core/dev.c b/net/core/dev.c index 200d44883fc1..fa3592e8866f 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -11311,6 +11311,7 @@ static void netdev_free_phy_link_topology(struct ne= t_device *dev) =20 if (IS_ENABLED(CONFIG_PHYLIB) && topo) { xa_destroy(&topo->phys); + xa_destroy(&topo->ports); kfree(topo); dev->link_topo =3D NULL; } --=20 2.49.0 From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D843E39A07B for ; Wed, 25 Mar 2026 08:20:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426810; cv=none; b=Xi2rzXma0nyemDgDuET5yctouSl3oT2V3GHDWG0LWEzjuUY6Vxne9nLoPjROHyhv+kzP2rASNyVTFYS/ghy61cHLLJht5JG+kpvQnsy6YDh+BYePIQqygxtKQ3Q/2O0Mp7pUnTWjTqvCfZVRtoeHzxd7g72kk9DIRDoR4tFuiOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426810; c=relaxed/simple; bh=K8Aex1iQUhW3plKJEs3vIm71bf9I6DqWySrZFbQHkdM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RxGGzFiJNVi/LKH4jAidlqV2L6B8vk1ebokVUoWRp1Qnqy2s3/rnXocXOfccKwphzEuskG4c/vDr7fOChnagwkVv8MfaAhUzta2/3XSzFvgar6CTb2ncLrfla0TUb/ez1nB/3kAboLCz/NFKAeHgN2BcQZdKI7EcOs7w/D7RXRE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=Kuv+lP1r; arc=none smtp.client-ip=185.171.202.116 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.b="Kuv+lP1r" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id 9D28DC580AD; Wed, 25 Mar 2026 08:20:33 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id F2852601A1; Wed, 25 Mar 2026 08:20:05 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 4EA371045037C; Wed, 25 Mar 2026 09:20:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426804; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=bigvl21tXAgDCjS7awQU1dDKzJ105Y4hf0eUUyEWy7E=; b=Kuv+lP1rqnYJi6Ia/pWSuZCsAN5tf3kyHwT0xHQG9IAat6BcIH++OzbmBRP/bI3/R0gliQ Dk67BuE7eS+ipvKK/nj8o+ZPbwuwfo7Xarn9NYmWaURKasiagtM/kEVhJRfgbx0laOObVC LRhESVtxl8wWikkl2iu+odUyK1FRIqH2htZZx9QG1PXqB96iXikBLsgjQ45+Ew6FIT/p7m +LZpenkAVFgJhVjVxlvU4ndbIHLDg1qQ6LPwWpE+P8tU+Ha6PcQViEtzLp9U0d2+jtic5G jVdD2R4yXS/8ZNYc9NSe165TZie2Pn3Fi/ITT5U+iEI8VSLHVSc0+In/sjeU6w== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 03/10] net: phylink: Register a phy_port for MAC-driven SFP busses Date: Wed, 25 Mar 2026 09:19:28 +0100 Message-ID: <20260325081937.571115-4-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" phy_port tracks the interfaces that a netdevice feeds into. SFP cages are such ports, but so far we are only tracking the ones that are driven by PHYs acting as media-converters. Let's populate a phy_port for MAC driver SFP cages, handled by phylink. This phy_port represents the SFP cage itself, and not the module that may be plugged into it. It's therefore not an MDI interface, so only the 'interfaces' field is relevant here. The phy_port is only populated for 'NETDEV' phylink instances, as otherwise we don't have any topology to attach the port to. Signed-off-by: Maxime Chevallier --- drivers/net/phy/phylink.c | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index f80cd13c8e32..7535fb117bf5 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include #include #include @@ -93,6 +95,7 @@ struct phylink { DECLARE_PHY_INTERFACE_MASK(sfp_interfaces); __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support); u8 sfp_port; + struct phy_port *sfp_bus_port; =20 struct eee_config eee_cfg; =20 @@ -1765,6 +1768,46 @@ static void phylink_fixed_poll(struct timer_list *t) =20 static const struct sfp_upstream_ops sfp_phylink_ops; =20 +static int phylink_create_sfp_port(struct phylink *pl) +{ + struct phy_port *port; + int ret =3D 0; + + if (!pl->netdev || !pl->sfp_bus) + return 0; + + port =3D phy_port_alloc(); + if (!port) + return -ENOMEM; + + port->is_sfp =3D true; + port->is_mii =3D true; + port->active =3D true; + + phy_interface_and(port->interfaces, pl->config->supported_interfaces, + phylink_sfp_interfaces); + phy_port_update_supported(port); + + ret =3D phy_link_topo_add_port(pl->netdev, port); + if (ret) + phy_port_destroy(port); + else + pl->sfp_bus_port =3D port; + + return ret; +} + +static void phylink_destroy_sfp_port(struct phylink *pl) +{ + if (pl->netdev && pl->sfp_bus_port) + phy_link_topo_del_port(pl->netdev, pl->sfp_bus_port); + + if (pl->sfp_bus_port) + phy_port_destroy(pl->sfp_bus_port); + + pl->sfp_bus_port =3D NULL; +} + static int phylink_register_sfp(struct phylink *pl, const struct fwnode_handle *fwnode) { @@ -1782,9 +1825,18 @@ static int phylink_register_sfp(struct phylink *pl, =20 pl->sfp_bus =3D bus; =20 + ret =3D phylink_create_sfp_port(pl); + if (ret) { + sfp_bus_put(bus); + return ret; + } + ret =3D sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops); sfp_bus_put(bus); =20 + if (ret) + phylink_destroy_sfp_port(pl); + return ret; } =20 @@ -1946,6 +1998,7 @@ EXPORT_SYMBOL_GPL(phylink_create); void phylink_destroy(struct phylink *pl) { sfp_bus_del_upstream(pl->sfp_bus); + phylink_destroy_sfp_port(pl); if (pl->link_gpio) gpiod_put(pl->link_gpio); =20 --=20 2.49.0 From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A88D339B495 for ; Wed, 25 Mar 2026 08:20:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426812; cv=none; b=ZUo5eCpogEaLD3mYRH5hLgRZopSltrRqI/PPCG/SbxTgriciICTdQMHbEfJKH5MAde8aFw6xXu5WylXgntl4Pf667MdK63fqWqfTeubJVnjD/JgUiB/Ww62rPgu7iOdt4BrODVTjb6FEMVOdW74XNoJDXxCsFVCCXXRD9U4vM4w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426812; c=relaxed/simple; bh=P89XoIX5xTpIgcPAy9Rqn11WzqpcB75XjYG9hlbzdZs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Yy6Df8lxHyTZOrGxkRKInOpycC12ExNHF5TmHNCeJ4wrXyPpToThIia0P/n6CCDroJ0+Vh3kWhJzz/4pwXvimKgvH5CX39y+jg7EIoIZUWQMI7YMW8NX0iC7ZMjjGSekr0JQs++feYs0bVoEncbpgnbh7VQXADteMpGGo1B9XtQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=Qza87Pjb; arc=none smtp.client-ip=185.171.202.116 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.b="Qza87Pjb" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id 07368C580AB; Wed, 25 Mar 2026 08:20:36 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 5CFC4601A1; Wed, 25 Mar 2026 08:20:08 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 4CC3410451368; Wed, 25 Mar 2026 09:20:05 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426807; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=Wnzb92n8n6jzst7oG6QZziN8KevfYfIOW5TWQ8o8SBg=; b=Qza87Pjb55QQGF9Sjsu3sV1oOTHvr4cUtuBr76KGiL00Uava9+LPKqvFznYITdtaJkfTFI q7rxu16lcZOTk2QGb2wlLD5GQI0mgR6xAx8UGqend+5JoH6ZjPrCHUmEl3t7v7P3bVe81a LuXxunGu8hADro75FYjEf3dlYxYr3hZ6OPSUjvsvCD+LJhW2IHADq4xvyuA4ZFGhnOTE0B Co5mZhXgvGt57i5ZU3KNCyCGvM1HYb2FNIxPyBdbw+DqlOxuImIsNqVrUMRdZW2ODch/QO 5+lIDGxzq0cayI5YgRZF9wpkw6fuEiF3UNESARNR3zS4yodFH8HALa5DpKoU+w== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 04/10] net: phy: Create SFP phy_port before registering upstream Date: Wed, 25 Mar 2026 09:19:29 +0100 Message-ID: <20260325081937.571115-5-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" When dealing with PHY-driven SFP, we create a phy_port representing the SFP bus when we know we have such a bus. We can move the port creation before registering the sfp upstream ops, as long as we know the SFP bus is there. This will allow passing the phy_port along with the upstream information to the SFP bus. Signed-off-by: Maxime Chevallier --- drivers/net/phy/phy_device.c | 42 ++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c index 0edff47478c2..a5400f01fe17 100644 --- a/drivers/net/phy/phy_device.c +++ b/drivers/net/phy/phy_device.c @@ -1665,13 +1665,13 @@ static void phy_del_port(struct phy_device *phydev,= struct phy_port *port) phydev->n_ports--; } =20 -static int phy_setup_sfp_port(struct phy_device *phydev) +static struct phy_port *phy_setup_sfp_port(struct phy_device *phydev) { struct phy_port *port =3D phy_port_alloc(); int ret; =20 if (!port) - return -ENOMEM; + return ERR_PTR(-ENOMEM); =20 port->parent_type =3D PHY_PORT_PHY; port->phy =3D phydev; @@ -1686,10 +1686,12 @@ static int phy_setup_sfp_port(struct phy_device *ph= ydev) * when attaching the port to the phydev. */ ret =3D phy_add_port(phydev, port); - if (ret) + if (ret) { phy_port_destroy(port); + return ERR_PTR(ret); + } =20 - return ret; + return port; } =20 /** @@ -1698,22 +1700,34 @@ static int phy_setup_sfp_port(struct phy_device *ph= ydev) */ static int phy_sfp_probe(struct phy_device *phydev) { + struct phy_port *port =3D NULL; struct sfp_bus *bus; - int ret =3D 0; + int ret; =20 - if (phydev->mdio.dev.fwnode) { - bus =3D sfp_bus_find_fwnode(phydev->mdio.dev.fwnode); - if (IS_ERR(bus)) - return PTR_ERR(bus); + if (!phydev->mdio.dev.fwnode) + return 0; + + bus =3D sfp_bus_find_fwnode(phydev->mdio.dev.fwnode); + if (IS_ERR(bus)) + return PTR_ERR(bus); =20 - phydev->sfp_bus =3D bus; + phydev->sfp_bus =3D bus; =20 - ret =3D sfp_bus_add_upstream(bus, phydev, &sfp_phydev_ops); - sfp_bus_put(bus); + if (bus) { + port =3D phy_setup_sfp_port(phydev); + if (IS_ERR(port)) { + sfp_bus_put(bus); + return PTR_ERR(port); + } } =20 - if (!ret && phydev->sfp_bus) - ret =3D phy_setup_sfp_port(phydev); + ret =3D sfp_bus_add_upstream(bus, phydev, &sfp_phydev_ops); + sfp_bus_put(bus); + + if (ret && port) { + phy_del_port(phydev, port); + phy_port_destroy(port); + } =20 return ret; } --=20 2.49.0 From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66A7539B971 for ; Wed, 25 Mar 2026 08:20:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426815; cv=none; b=ab/a5SNFjjZJKvmyxWo/UD7zbzMft1XZLwa9EMcTqwlHXkXfu+0zhV7GowdT0kgsWOjHaxsoOqdDEL2twLjPE9rJ6ENXhKCea86iHR4xu6Q1TYwzywdZsWxhE4e4+ExKSvQ9Vtt/iN1+HVb6QzcFV5zvdNjakjvFJh6V0MEuLaw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426815; c=relaxed/simple; bh=f1Lo98/ip8mQ3+o+s36f6xKHDGXFYEk9edKkG95GmHM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CvNFZGeKfsSO9Rd0vOAmhwA+kbumm5vyFcx1iRP8L7yKV68VFEGdUOISTZGX1/yrjiSRZaD8HZ3Gd8Dm+O7J2Dvy53vqfsUoc4vzdaZWHl3ilaPWtsV7maHbx71ZkuetJtja7yHqeVui+AICk8DicolRzoGocPdQqCHke4PawIk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=g6yTnzTy; arc=none smtp.client-ip=185.246.84.56 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.b="g6yTnzTy" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id F10A51A2FE0; Wed, 25 Mar 2026 08:20:10 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id C768F601A1; Wed, 25 Mar 2026 08:20:10 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 9BEC710451365; Wed, 25 Mar 2026 09:20:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426809; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=xip5Y7z1xZ8ySqNVk8Es9KjrEvRoDP4dPSU0UcThR8k=; b=g6yTnzTyvv30cZZ3pNB+E6ANiFEEfCnN6cq25ZuM0unMzqhKZfEFkil9TLR9IQO/LPZER9 DAh2oyHwj59v7yd6/RY1TWDzVYzQY0/ph+D3p5nfP69UJQDfJMEAAIPrgdSKW2alT5p1ol YCRyCK4iIM3nKbJ4MBu2YJsVZj0LNoCbet7J3o+eMccjk52weTtX4omZQUxy1I7orr3jlP Cdh0QJeDMotpzUAG8sWB1NQDPrkbMsvtHevNsRDUt4A+vwB663FP2TjqA05T0LrmpH+Ssb +XXhZ4Q4uvdgDo2+meLSeo7jh29UnhBxcLhWPBHNAkiSLSWk2JFoyf0XyqathA== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 05/10] net: phy: Represent PHY-less SFP modules with phy_port Date: Wed, 25 Mar 2026 09:19:30 +0100 Message-ID: <20260325081937.571115-6-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" Now that the SFP bus infrastructure notifies when PHY-less modules are connected, we can create a phy_port to represent it. Instead of letting the SFP subsystem handle that, the Bus' upstream is in charge of maintaining that phy_port and register it to the topology, as the upstream (in this case a phy device) is directly interacting with the underlying net_device. Add a phy_caps helper to get the achievable modes on this module based on what the phy_port representing the bus supports. Signed-off-by: Maxime Chevallier --- drivers/net/phy/phy-caps.h | 2 + drivers/net/phy/phy_caps.c | 26 ++++++++++ drivers/net/phy/phy_device.c | 97 +++++++++++++++++++++++++++++++++++- include/linux/phy.h | 6 +++ 4 files changed, 129 insertions(+), 2 deletions(-) diff --git a/drivers/net/phy/phy-caps.h b/drivers/net/phy/phy-caps.h index 421088e6f6e8..ec3d39a0ae06 100644 --- a/drivers/net/phy/phy-caps.h +++ b/drivers/net/phy/phy-caps.h @@ -66,5 +66,7 @@ void phy_caps_medium_get_supported(unsigned long *support= ed, enum ethtool_link_medium medium, int lanes); u32 phy_caps_mediums_from_linkmodes(unsigned long *linkmodes); +void phy_caps_linkmode_filter_ifaces(unsigned long *to, const unsigned lon= g *from, + const unsigned long *interfaces); =20 #endif /* __PHY_CAPS_H */ diff --git a/drivers/net/phy/phy_caps.c b/drivers/net/phy/phy_caps.c index 942d43191561..558e4df4d63c 100644 --- a/drivers/net/phy/phy_caps.c +++ b/drivers/net/phy/phy_caps.c @@ -445,3 +445,29 @@ u32 phy_caps_mediums_from_linkmodes(unsigned long *lin= kmodes) return mediums; } EXPORT_SYMBOL_GPL(phy_caps_mediums_from_linkmodes); + +/** + * phy_caps_linkmode_filter_ifaces() - Filter linkmodes with an interface = list + * @to: Stores the filtered linkmodes + * @from: Linkmodes to filter + * @interfaces: Bitfield of phy_interface_t that we use for filtering + * + * Filter the provided linkmodes, only to keep the ones we can possibly ac= hieve + * when using any of the provided MII interfaces. + */ +void phy_caps_linkmode_filter_ifaces(unsigned long *to, + const unsigned long *from, + const unsigned long *interfaces) +{ + __ETHTOOL_DECLARE_LINK_MODE_MASK(ifaces_supported) =3D {}; + unsigned int ifaces_caps =3D 0; + phy_interface_t interface; + + for_each_set_bit(interface, interfaces, PHY_INTERFACE_MODE_MAX) + ifaces_caps |=3D phy_caps_from_interface(interface); + + phy_caps_linkmodes(ifaces_caps, ifaces_supported); + + linkmode_and(to, from, ifaces_supported); +} +EXPORT_SYMBOL_GPL(phy_caps_linkmode_filter_ifaces); diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c index a5400f01fe17..2c0b0d275809 100644 --- a/drivers/net/phy/phy_device.c +++ b/drivers/net/phy/phy_device.c @@ -1482,9 +1482,17 @@ static int phy_sfp_connect_phy(void *upstream, struc= t phy_device *phy) { struct phy_device *phydev =3D upstream; struct net_device *dev =3D phydev->attached_dev; + int ret; =20 - if (dev) - return phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev); + /* If we aren't attached to a netdev, we can't add the SFP PHY to its + * topology. + */ + if (!dev) + return 0; + + ret =3D phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev); + if (!ret) + phydev->has_sfp_mod_phy =3D true; =20 return 0; } @@ -1504,6 +1512,8 @@ static void phy_sfp_disconnect_phy(void *upstream, st= ruct phy_device *phy) struct phy_device *phydev =3D upstream; struct net_device *dev =3D phydev->attached_dev; =20 + phydev->has_sfp_mod_phy =3D false; + if (dev) phy_link_topo_del_phy(dev, phy); } @@ -1609,6 +1619,75 @@ static void phy_sfp_link_down(void *upstream) port->ops->link_down(port); } =20 +static int phy_add_sfp_mod_port(struct phy_device *phydev) +{ + const struct sfp_module_caps *caps; + struct phy_port *port; + int ret =3D 0; + + /* Create mod port */ + port =3D phy_port_alloc(); + if (!port) + return -ENOMEM; + + port->active =3D true; + + caps =3D sfp_get_module_caps(phydev->sfp_bus); + + phy_caps_linkmode_filter_ifaces(port->supported, caps->link_modes, + phydev->sfp_bus_port->interfaces); + + if (phydev->attached_dev) { + ret =3D phy_link_topo_add_port(phydev->attached_dev, port); + if (ret) { + phy_port_destroy(port); + return ret; + } + } + + /* we don't use phy_add_port() here as the module port isn't a direct + * interface from the PHY, but rather an extension to the sfp-bus, that + * is already represented by its own phy_port + */ + phydev->mod_port =3D port; + + return 0; +} + +static void phy_del_sfp_mod_port(struct phy_device *phydev) +{ + if (!phydev->mod_port) + return; + + if (phydev->attached_dev) + phy_link_topo_del_port(phydev->attached_dev, phydev->mod_port); + + phy_port_destroy(phydev->mod_port); + phydev->mod_port =3D NULL; +} + +static int phy_sfp_module_start(void *upstream) +{ + struct phy_device *phydev =3D upstream; + + /* If there's a downstream SFP module, and it doesn't contain a PHY + * device, let's create a phy_port to represent that module. + */ + if (!phydev->has_sfp_mod_phy) + return phy_add_sfp_mod_port(phydev); + + return 0; +} + +static void phy_sfp_module_stop(void *upstream) +{ + struct phy_device *phydev =3D upstream; + + /* Called upon module removal or upstream removal */ + if (!phydev->has_sfp_mod_phy) + phy_del_sfp_mod_port(phydev); +} + static const struct sfp_upstream_ops sfp_phydev_ops =3D { .attach =3D phy_sfp_attach, .detach =3D phy_sfp_detach, @@ -1618,6 +1697,8 @@ static const struct sfp_upstream_ops sfp_phydev_ops = =3D { .link_down =3D phy_sfp_link_down, .connect_phy =3D phy_sfp_connect_phy, .disconnect_phy =3D phy_sfp_disconnect_phy, + .module_start =3D phy_sfp_module_start, + .module_stop =3D phy_sfp_module_stop, }; =20 static int phy_add_port(struct phy_device *phydev, struct phy_port *port) @@ -1727,8 +1808,11 @@ static int phy_sfp_probe(struct phy_device *phydev) if (ret && port) { phy_del_port(phydev, port); phy_port_destroy(port); + port =3D NULL; } =20 + phydev->sfp_bus_port =3D port; + return ret; } =20 @@ -1818,6 +1902,12 @@ int phy_attach_direct(struct net_device *dev, struct= phy_device *phydev, err =3D phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev); if (err) goto error; + + if (phydev->mod_port) { + err =3D phy_link_topo_add_port(dev, phydev->mod_port); + if (err) + goto error; + } } =20 /* Some Ethernet drivers try to connect to a PHY device before @@ -1951,6 +2041,8 @@ void phy_detach(struct phy_device *phydev) phydev->attached_dev->phydev =3D NULL; phydev->attached_dev =3D NULL; phy_link_topo_del_phy(dev, phydev); + if (phydev->mod_port) + phy_link_topo_del_port(dev, phydev->mod_port); } =20 phydev->phy_link_change =3D NULL; @@ -3792,6 +3884,7 @@ static int phy_remove(struct device *dev) =20 sfp_bus_del_upstream(phydev->sfp_bus); phydev->sfp_bus =3D NULL; + phydev->sfp_bus_port =3D NULL; =20 if (phydev->drv && phydev->drv->remove) phydev->drv->remove(phydev); diff --git a/include/linux/phy.h b/include/linux/phy.h index 5de4b172cd0b..c20f603e6010 100644 --- a/include/linux/phy.h +++ b/include/linux/phy.h @@ -582,6 +582,7 @@ struct phy_oatc14_sqi_capability { * @wol_enabled: Set to true if the PHY or the attached MAC have Wake-on-L= AN * enabled. * @is_genphy_driven: PHY is driven by one of the generic PHY drivers + * @has_sfp_mod_phy: Set true if downstream SFP bus's module contains a PHY * @state: State of the PHY for management purposes * @dev_flags: Device-specific flags used by the PHY driver. * @@ -594,6 +595,8 @@ struct phy_oatc14_sqi_capability { * @phylink: Pointer to phylink instance for this PHY * @sfp_bus_attached: Flag indicating whether the SFP bus has been attached * @sfp_bus: SFP bus attached to this PHY's fiber port + * @sfp_bus_port: The phy_port connected to the downstream SFP bus + * @mod_port: phy_port representing the SFP module, if it is phy-less * @attached_dev: The attached enet driver's device instance ptr * @adjust_link: Callback for the enet controller to respond to changes: i= n the * link state. @@ -704,6 +707,7 @@ struct phy_device { unsigned irq_rerun:1; =20 unsigned default_timestamp:1; + unsigned has_sfp_mod_phy:1; =20 int rate_matching; =20 @@ -782,6 +786,8 @@ struct phy_device { /* This may be modified under the rtnl lock */ bool sfp_bus_attached; struct sfp_bus *sfp_bus; + struct phy_port *sfp_bus_port; + struct phy_port *mod_port; struct phylink *phylink; struct net_device *attached_dev; struct mii_timestamper *mii_ts; --=20 2.49.0 From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B05FA396B98 for ; Wed, 25 Mar 2026 08:20:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426817; cv=none; b=WkUhszUo0eESGzE3zXEkpmbZmPiQ+C6nz65Yc32kkAKzA7FAvNH2zlhyQ9zmPETBQhATA3MSuluDVAaseBuGfM9eM4zCqmfK1MNyeNf64x2gI3QxuEMLbopSPfGpQPdHa30Q8H+werZ5XX4f03uZE5yAlTMEgG6OgcXJjo907zU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426817; c=relaxed/simple; bh=n+/eeFZ1KaBKkTVwQyOhrNeVZTsPVlh2kmn3CKOJrF8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nEbgH0IcFCYWlQo5QA2vpRRVuYsmHE1JPV5fgGnRTiKCShpcBDg3207IAcTQqkgIKHJ50DXgJC/2zB9ixoSqYZ6t9JXqMZcayspV+EJvddDC0h/Ys+fhx08oHrsyp9IK+3Jks7BB77RETfRKhSQj5mwo0BlCgw9TDsgH8uez5LY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=AdbUrSim; arc=none smtp.client-ip=185.171.202.116 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.b="AdbUrSim" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id D017EC580AB; Wed, 25 Mar 2026 08:20:40 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 320A3601A1; Wed, 25 Mar 2026 08:20:13 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id F37B410451369; Wed, 25 Mar 2026 09:20:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426812; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=ly+cldkPfJWshEyvYF+QzhXSu8lWYMwFRWFMwAjqCiU=; b=AdbUrSimY5T7N1ufrxf/X5u50iHAT+iZF8rRhIyN5aDJbuGLe3kSFRKB4laJSXrrKYSj+V QyRgx+VZWNX5cHNohI2/Z+N/NjieQpbu4W7uiUEmyds9LlKmVsylXgsnAAUOzBJZ1ioWtz DsQd/yIELZiKSw0FFXFIAhU6feUYUNMuWE5gWxR9SK4y+Z/GvYvqoVowQl9gN4M8wEVSRt gXynv3rfUKkXYcTh5BkFVKoCBgmhsTDtsWKR388tb023kxmesQlIHFl/fxTdcF3RFRhqP+ rXvFJgsH757f1L/0kjC7Cq+nyFz+7R1jw8bSzL5ueLHGD6PMdhywXp3UwDEoXA== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 06/10] net: phylink: Represent PHY-less SFP modules with phy_port Date: Wed, 25 Mar 2026 09:19:31 +0100 Message-ID: <20260325081937.571115-7-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" Let phylink handle the phy_port for PHY-less modules, and register it to the topology. Signed-off-by: Maxime Chevallier --- drivers/net/phy/phylink.c | 71 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index 7535fb117bf5..73d838b918d1 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -96,6 +96,7 @@ struct phylink { __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support); u8 sfp_port; struct phy_port *sfp_bus_port; + struct phy_port *mod_port; =20 struct eee_config eee_cfg; =20 @@ -1790,10 +1791,21 @@ static int phylink_create_sfp_port(struct phylink *= pl) =20 ret =3D phy_link_topo_add_port(pl->netdev, port); if (ret) - phy_port_destroy(port); - else - pl->sfp_bus_port =3D port; + goto out_destroy_port; + + pl->sfp_bus_port =3D port; + + if (pl->mod_port) { + ret =3D phy_link_topo_add_port(pl->netdev, pl->mod_port); + if (ret) + goto out_del_bus_port; + } =20 + return 0; +out_del_bus_port: + phy_link_topo_del_port(pl->netdev, port); +out_destroy_port: + phy_port_destroy(port); return ret; } =20 @@ -3926,14 +3938,65 @@ static void phylink_sfp_module_remove(void *upstrea= m) phy_interface_zero(pl->sfp_interfaces); } =20 +static int phylink_add_sfp_mod_port(struct phylink *pl) +{ + const struct sfp_module_caps *caps; + struct phy_port *port; + int ret =3D 0; + + if (!pl->sfp_bus_port) + return 0; + + /* Create mod port */ + port =3D phy_port_alloc(); + if (!port) + return -ENOMEM; + + port->active =3D true; + + caps =3D sfp_get_module_caps(pl->sfp_bus); + + phy_caps_linkmode_filter_ifaces(port->supported, caps->link_modes, + pl->sfp_bus_port->interfaces); + + if (pl->netdev) { + ret =3D phy_link_topo_add_port(pl->netdev, port); + if (ret) { + phy_port_destroy(port); + return ret; + } + } + + pl->mod_port =3D port; + + return 0; +} + +static void phylink_del_sfp_mod_port(struct phylink *pl) +{ + if (!pl->mod_port) + return; + + if (pl->netdev) + phy_link_topo_del_port(pl->netdev, pl->mod_port); + + phy_port_destroy(pl->mod_port); + pl->mod_port =3D NULL; +} + static int phylink_sfp_module_start(void *upstream) { struct phylink *pl =3D upstream; + int ret; =20 /* If this SFP module has a PHY, start the PHY now. */ if (pl->phydev) { phy_start(pl->phydev); return 0; + } else { + ret =3D phylink_add_sfp_mod_port(pl); + if (ret) + return ret; } =20 /* If the module may have a PHY but we didn't detect one we @@ -3952,6 +4015,8 @@ static void phylink_sfp_module_stop(void *upstream) /* If this SFP module has a PHY, stop it. */ if (pl->phydev) phy_stop(pl->phydev); + else + phylink_del_sfp_mod_port(pl); } =20 static void phylink_sfp_link_down(void *upstream) --=20 2.49.0 From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E553B39D6D2 for ; Wed, 25 Mar 2026 08:20:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426819; cv=none; b=RuXGmXOirwaOAhLtNVYnkawUeuzsCTTPB81Z65HMo8aKExkdMWS3NvJooFiSshjyscvSwt6oNfNvm6YMmrGCugZDlCiJ622ai/gHlXtiAcPRfXoWO7Lqq6jPvfOd8ULg0FG+HntNkJcaGgbSVEU8kXCaBT+XEGvg7w4nZt1UcWE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426819; c=relaxed/simple; bh=iDiJ/hD9RyZ/VBe/8q2iwHgY1byzT7ehQ7dGfaxCVmM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OQp2amEgHlJcI4gayk/LhjGbM/DneXbhg4eGDRHHjP53BlCyhhVMHmw3DuNXz3jlLL/zYFUGQHAVeSz3+qHUOpf6EvZQb4wTxopyPfkVqakC+5M2EtN1w1L44RiSbTBkUgLivQ3hueKXSMNvptEM/KfNvrL9WCf4/7Td9smgVQI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=wcgAwGmh; arc=none smtp.client-ip=185.171.202.116 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.b="wcgAwGmh" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id 4A08DC580AC; Wed, 25 Mar 2026 08:20:43 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 9F848601A1; Wed, 25 Mar 2026 08:20:15 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 853B61045136B; Wed, 25 Mar 2026 09:20:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426814; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=mHopyKwGp0vXHDq7EzOxlyO9Kyr64b+XQr9pdlp672E=; b=wcgAwGmh2xKyx7ZJ2WDoTUnsYjHB+uH9/6nVscC7ATgjqYOwe/po30qEHwzU7hVPQ1PvRW pDKB4ow37ovJYTfyrZMWRyWleVywlMF89zwA6WNmSUAROwhGZsa4XUJEDiftf+/YdTbCeo x8vv6wSHA/iqxhhJNyR1FrYUl38ExzQzrow1fu2f6YKuJweB+AnuWr+EB95Ymy52SrDTzg M+Bn7H/BlJqXrJNpwVJPfpvyUH28URXD4wuOc7kZaprztv8aVN8rVm6aBXesDeg39s5IpI EkObrj0bnLqNe1dj3id75g/VjxEIcLRFR0MjKuaADCxbGuTF827j6sIDRJ6tew== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 07/10] net: phy: phy_port: Store information about a MII port's vacant state Date: Wed, 25 Mar 2026 09:19:32 +0100 Message-ID: <20260325081937.571115-8-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" MII phy_ports are not meant to be connected directly to a link partner. They are meant to feed into some media converter devices, so far we only support SFP modules for that. We have information about what MII they can handle, however we don't store anything about whether they are currently connected to an SFP module or not. As phy_port aims at listing the front-facing ports, let's store an "vacant" bit to know whether or not a MII port is currently vacant (i.e. there's no module in the SFP cage), or not (i.e. there's an SFP module). Signed-off-by: Maxime Chevallier --- drivers/net/phy/phy_device.c | 17 +++++++++++++---- drivers/net/phy/phylink.c | 14 +++++++++++++- include/linux/phy_port.h | 4 ++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c index 2c0b0d275809..855db4431611 100644 --- a/drivers/net/phy/phy_device.c +++ b/drivers/net/phy/phy_device.c @@ -1556,6 +1556,7 @@ static int phy_sfp_module_insert(void *upstream, cons= t struct sfp_eeprom_id *id) struct phy_device *phydev =3D upstream; const struct sfp_module_caps *caps; struct phy_port *port; + int ret =3D 0; =20 phy_interface_t iface; =20 @@ -1584,9 +1585,12 @@ static int phy_sfp_module_insert(void *upstream, con= st struct sfp_eeprom_id *id) phydev->port =3D caps->port; =20 if (port->ops && port->ops->configure_mii) - return port->ops->configure_mii(port, true, iface); + ret =3D port->ops->configure_mii(port, true, iface); =20 - return 0; + if (!ret) + port->vacant =3D false; + + return ret; } =20 static void phy_sfp_module_remove(void *upstream) @@ -1594,8 +1598,12 @@ static void phy_sfp_module_remove(void *upstream) struct phy_device *phydev =3D upstream; struct phy_port *port =3D phy_get_sfp_port(phydev); =20 - if (port && port->ops && port->ops->configure_mii) - port->ops->configure_mii(port, false, PHY_INTERFACE_MODE_NA); + if (port) { + if (port->ops && port->ops->configure_mii) + port->ops->configure_mii(port, false, + PHY_INTERFACE_MODE_NA); + port->vacant =3D true; + } =20 if (phydev->n_ports =3D=3D 1) phydev->port =3D PORT_NONE; @@ -1762,6 +1770,7 @@ static struct phy_port *phy_setup_sfp_port(struct phy= _device *phydev) */ port->is_mii =3D true; port->is_sfp =3D true; + port->vacant =3D true; =20 /* The port->supported and port->interfaces list will be populated * when attaching the port to the phydev. diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index 73d838b918d1..8bf0e2f143df 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -1784,6 +1784,7 @@ static int phylink_create_sfp_port(struct phylink *pl) port->is_sfp =3D true; port->is_mii =3D true; port->active =3D true; + port->vacant =3D true; =20 phy_interface_and(port->interfaces, pl->config->supported_interfaces, phylink_sfp_interfaces); @@ -3915,6 +3916,7 @@ static int phylink_sfp_module_insert(void *upstream, { const struct sfp_module_caps *caps; struct phylink *pl =3D upstream; + int ret; =20 ASSERT_RTNL(); =20 @@ -3924,17 +3926,27 @@ static int phylink_sfp_module_insert(void *upstream, pl->sfp_may_have_phy =3D caps->may_have_phy; pl->sfp_port =3D caps->port; =20 + if (pl->sfp_bus_port) + pl->sfp_bus_port->vacant =3D false; + /* If this module may have a PHY connecting later, defer until later */ if (pl->sfp_may_have_phy) return 0; =20 - return phylink_sfp_config_optical(pl); + ret =3D phylink_sfp_config_optical(pl); + if (ret && pl->sfp_bus_port) + pl->sfp_bus_port->vacant =3D true; + + return ret; } =20 static void phylink_sfp_module_remove(void *upstream) { struct phylink *pl =3D upstream; =20 + if (pl->sfp_bus_port) + pl->sfp_bus_port->vacant =3D true; + phy_interface_zero(pl->sfp_interfaces); } =20 diff --git a/include/linux/phy_port.h b/include/linux/phy_port.h index 4e2a3fdd2f2e..b0b1fa6a67a9 100644 --- a/include/linux/phy_port.h +++ b/include/linux/phy_port.h @@ -51,6 +51,9 @@ struct phy_port_ops { * @is_mii: Indicates if this port is MII (Media Independent Interface), * or MDI (Media Dependent Interface). * @is_sfp: Indicates if this port drives an SFP cage. + * @vacant: For MII ports, indicates whether or not the port has a connect= ion to + * another device (e.g. for SFP ports, indicates the absence or presen= ce + * of an SFP module) */ struct phy_port { u32 id; @@ -71,6 +74,7 @@ struct phy_port { unsigned int active:1; unsigned int is_mii:1; unsigned int is_sfp:1; + unsigned int vacant:1; }; =20 struct phy_port *phy_port_alloc(void); --=20 2.49.0 From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70EA939F179 for ; Wed, 25 Mar 2026 08:20:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426820; cv=none; b=oK3EgEM0Ck2OL2xS9n8mhhlUmEL5dc1XVXrb7gLYW2qhVCs/8tNvaRjwnRMhKxj5pHA/N8ghG36C11FaLeCSZf0WI3DLHwQYDe3fAbNFKdsiF4NeMKtQAn6rOfL04HRgE0S68cERwv7qgY1+XWwzsXfiMmMBSw6RSYooN+WDp7Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426820; c=relaxed/simple; bh=f0/3/mrCPrJHqIVyCbu48EYwHwbuoZwmfW42AbHjWH0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ng8yZC0sKDnTunF0gwcOu886TEuBBYUCDpZ0MNabD3p0BvDMfO73JI2M2TVlJRl2F6uqvfjy5sUhyreY/NCmpdbd3cM5LCzmrzgqsumOjM8Z6wLqCUeSZ63yu+bOlgQY29fuGD5IfacZkh4qBbq5dvbdP8lO0JTPWDCtP16v2gA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=BbZgNQLG; arc=none smtp.client-ip=185.171.202.116 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.b="BbZgNQLG" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id C41ACC580AA; Wed, 25 Mar 2026 08:20:45 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 255AD601A1; Wed, 25 Mar 2026 08:20:18 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id DE01B1045136C; Wed, 25 Mar 2026 09:20:14 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426816; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=oE3YHgEOb7zMldz7W5UEO4lNk/cteuFmDnyVlbi0dJs=; b=BbZgNQLGtEWrU5A213esKDV/CSmekD18WSXWwUzsbO2qBGMG5f6U3SoOUmVmbqVBRdWxYQ l6i2+p6Cd+JWUg67+BoRO2Tt3R7ND0aGhJ3VwZn3fYqZ1hVzW+kN/T5o0bSKRXTqWBvLng HA/qzXsyoqTyyZCz2zDz2Yt97QZXulVZOk7TuDhhkeByLmlkyoxddQseDO787YgyokvLws b1mKYG08yNMNNqaIoOoG/zGXnum+XWQS11Ygelkn0cjW4BLF5/IHqNpAHctkp7ov43jMPW 78/oZgfXP9eIJjHRrB8MYP6GdpKy+wKWFHYw2QFfRRhdmlqJEtCMYiBAo314bQ== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 08/10] net: phy: phy_link_topology: Add a helper to retrieve ports Date: Wed, 25 Mar 2026 09:19:33 +0100 Message-ID: <20260325081937.571115-9-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" In order to allow netlink access to phy_ports, let's add a helper to retrieve them. When handling a port coming from phy_link_topology, the caller must hold rtnl until it's done with it. Signed-off-by: Maxime Chevallier --- include/linux/phy_link_topology.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/linux/phy_link_topology.h b/include/linux/phy_link_top= ology.h index 66bceff72b19..ac2f5c80f1a6 100644 --- a/include/linux/phy_link_topology.h +++ b/include/linux/phy_link_topology.h @@ -13,6 +13,7 @@ =20 #include #include +#include =20 struct xarray; struct phy_device; @@ -66,6 +67,20 @@ phy_link_topo_get_phy(struct net_device *dev, u32 phyind= ex) return NULL; } =20 +static inline struct phy_port * +phy_link_topo_get_port(struct net_device *dev, u32 port_id) +{ + struct phy_link_topology *topo =3D dev->link_topo; + + ASSERT_RTNL(); + + if (!topo) + return NULL; + + /* Caller must hold RTNL while handling the phy_port */ + return xa_load(&topo->ports, port_id); +} + #else static inline int phy_link_topo_add_phy(struct net_device *dev, struct phy_device *phy, @@ -95,6 +110,12 @@ phy_link_topo_get_phy(struct net_device *dev, u32 phyin= dex) { return NULL; } + +static inline struct phy_port * +phy_link_topo_get_port(struct net_device *dev, u32 port_id) +{ + return NULL; +} #endif =20 #endif /* __PHY_LINK_TOPOLOGY_H */ --=20 2.49.0 From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7D7F39D6D2 for ; Wed, 25 Mar 2026 08:20:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.85.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426824; cv=none; b=T+FaflICZn5Qjg/o6F6BjEUK/NzxZ7LjIgtSHQEnpyi/ljsYO/yQAYEx4UNEs50u5OOPYhgxRqrztydWNF4y8rm4Awy0haq9/seCqB5ReybNIEoR4CFCeThYrbrgntfX+hVWtAVaHVYAdH4BnDJ4vpRi45+4GDKaI2bCZ6F3vys= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426824; c=relaxed/simple; bh=UYe4RVSsWw7xZOwqfKLieTsZPxT6eV+QWjzm1aIqAoM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NTCNZIqKZeAQCSnqL3VLzckHw/GOHLmX4iFhA6yudT5iBPodsrUwG8VgNyo23P9x4grdjadGsGO7lIHleqz5ysr50CExP2bJ9X4SkA4kKrRgzoxKoSR/cIPaFT7oz0nUlTeEIn5ZvHHP8FE9+1xt9ds5f2P8gF8wkwaotc4XhWI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=Hfaz84Bk; arc=none smtp.client-ip=185.246.85.4 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.b="Hfaz84Bk" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 8B5664E427EC; Wed, 25 Mar 2026 08:20:21 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 5915F601A1; Wed, 25 Mar 2026 08:20:21 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 5FE391045136D; Wed, 25 Mar 2026 09:20:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426820; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=vIwC37ckfpx7X0B48piz4NlvspAcnVk1yE/7+O3nieo=; b=Hfaz84Bky8xJewfW8nYrf/tcERTe4/7cBL7eCJA6ndkPjURcTJFXPFO9c1kylP6U32I6av mUm0vuX5W6TcviKWhwA3vM+YxE8lUqmvvVeQjZE6S8xtZheZxsnTlY4BTP9KzINYOFX+Gm 7FiToo0mRFic6FKKj3dSKyaCgDYKtQmiT1D37ByyC6Qw3B9WlcX5o85caV3PBHQUFWz7pq MxXfk97SWw0sEEISmFKaxuNctzr+6R6VYMZb7kRdBeZEzW6T+Z04S+JUR2lfIi6a/vxsL9 bRCJmhmBBCkMZUcRZoqYujGRPvPMxT3hkjffBcqLQ8D8Wz/AUrF7B3mKaz440w== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 09/10] netlink: specs: Add ethernet port listing with ethtool Date: Wed, 25 Mar 2026 09:19:34 +0100 Message-ID: <20260325081937.571115-10-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" Ethernet network interfaces may have more than one front-facing port. The phy_port infrastructure was introduced to keep track of these ports, and allow userspace to know about the presence and capability of these ports. Add a ethnl netlink message to report this information. Signed-off-by: Maxime Chevallier --- Documentation/netlink/specs/ethtool.yaml | 50 +++++++++++++++++++ Documentation/networking/ethtool-netlink.rst | 35 +++++++++++++ .../uapi/linux/ethtool_netlink_generated.h | 19 +++++++ 3 files changed, 104 insertions(+) diff --git a/Documentation/netlink/specs/ethtool.yaml b/Documentation/netli= nk/specs/ethtool.yaml index 5dd4d1b5d94b..9b907f40aba0 100644 --- a/Documentation/netlink/specs/ethtool.yaml +++ b/Documentation/netlink/specs/ethtool.yaml @@ -210,6 +210,10 @@ definitions: - name: discard value: 31 + - + name: port-type + type: enum + entries: [mdi, sfp] =20 attribute-sets: - @@ -1905,6 +1909,32 @@ attribute-sets: name: link type: nest nested-attributes: mse-snapshot + - + name: port + attr-cnt-name: --ethtool-a-port-cnt + attributes: + - + name: header + type: nest + nested-attributes: header + - + name: id + type: u32 + - + name: supported-modes + type: nest + nested-attributes: bitset + - + name: supported-interfaces + type: nest + nested-attributes: bitset + - + name: type + type: u32 + enum: port-type + - + name: vacant + type: u32 =20 operations: enum-model: directional @@ -2859,6 +2889,26 @@ operations: - worst-channel - link dump: *mse-get-op + - + name: port-get + doc: Get ports attached to an interface + + attribute-set: port + + do: &port-get-op + request: + attributes: + - header + - id + reply: + attributes: + - header + - id + - supported-modes + - supported-interfaces + - type + - vacant + dump: *port-get-op =20 mcast-groups: list: diff --git a/Documentation/networking/ethtool-netlink.rst b/Documentation/n= etworking/ethtool-netlink.rst index e92abf45faf5..0a5dfba55870 100644 --- a/Documentation/networking/ethtool-netlink.rst +++ b/Documentation/networking/ethtool-netlink.rst @@ -2537,6 +2537,40 @@ Within each channel nest, only the metrics supported= by the PHY will be present. See ``struct phy_mse_snapshot`` kernel documentation in ``include/linux/phy.h``. =20 +PORT_GET +=3D=3D=3D=3D=3D=3D=3D=3D + +Retrieve information about the physical connection points of a network dev= ice, +referred to as "ports". User needs to specify a PORT_ID for the DO operati= on, +in which case the DO request returns information about that specific port. + +As there can be more than one port, the DUMP operation can be used to list= the +ports present on a given interface, by passing an interface index or name = in +the dump request. + +Request contents: + + =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + ``ETHTOOL_A_PORT_HEADER`` nested request header + ``ETHTOOL_A_PORT_ID`` u32 port id + =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + +Kernel response contents: + + =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D =3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + ``ETHTOOL_A_PORT_HEADER`` nested request header + ``ETHTOOL_A_PORT_ID`` u32 the port's unique identi= fier, + per netdevice. + ``ETHTOOL_A_PORT_SUPPORTED_MODES`` bitset bitset of supported link= modes + ``ETHTOOL_A_PORT_SUPPORTED_INTERFACES`` bitset bitset of supported MII + interfaces + ``ETHTOOL_A_PORT_TYPE`` u8 the port type + ``ETHTOOL_A_PORT_VACANT`` u8 for non-mdi ports, indic= ates + if the port is connected= to + another device that could + expose a MDI + =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D =3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + Request translation =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 @@ -2647,4 +2681,5 @@ are netlink only. n/a ``ETHTOOL_MSG_PHY_GET`` ``SIOCGHWTSTAMP`` ``ETHTOOL_MSG_TSCONFIG_GET`` ``SIOCSHWTSTAMP`` ``ETHTOOL_MSG_TSCONFIG_SET`` + n/a ``ETHTOOL_MSG_PORT_GET`` =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D diff --git a/include/uapi/linux/ethtool_netlink_generated.h b/include/uapi/= linux/ethtool_netlink_generated.h index 8134baf7860f..ee26d59453e6 100644 --- a/include/uapi/linux/ethtool_netlink_generated.h +++ b/include/uapi/linux/ethtool_netlink_generated.h @@ -78,6 +78,11 @@ enum ethtool_pse_event { ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR =3D 64, }; =20 +enum ethtool_port_type { + ETHTOOL_PORT_TYPE_MDI, + ETHTOOL_PORT_TYPE_SFP, +}; + enum { ETHTOOL_A_HEADER_UNSPEC, ETHTOOL_A_HEADER_DEV_INDEX, @@ -840,6 +845,18 @@ enum { ETHTOOL_A_MSE_MAX =3D (__ETHTOOL_A_MSE_CNT - 1) }; =20 +enum { + ETHTOOL_A_PORT_HEADER =3D 1, + ETHTOOL_A_PORT_ID, + ETHTOOL_A_PORT_SUPPORTED_MODES, + ETHTOOL_A_PORT_SUPPORTED_INTERFACES, + ETHTOOL_A_PORT_TYPE, + ETHTOOL_A_PORT_VACANT, + + __ETHTOOL_A_PORT_CNT, + ETHTOOL_A_PORT_MAX =3D (__ETHTOOL_A_PORT_CNT - 1) +}; + enum { ETHTOOL_MSG_USER_NONE =3D 0, ETHTOOL_MSG_STRSET_GET =3D 1, @@ -893,6 +910,7 @@ enum { ETHTOOL_MSG_RSS_CREATE_ACT, ETHTOOL_MSG_RSS_DELETE_ACT, ETHTOOL_MSG_MSE_GET, + ETHTOOL_MSG_PORT_GET, =20 __ETHTOOL_MSG_USER_CNT, ETHTOOL_MSG_USER_MAX =3D (__ETHTOOL_MSG_USER_CNT - 1) @@ -954,6 +972,7 @@ enum { ETHTOOL_MSG_RSS_CREATE_NTF, ETHTOOL_MSG_RSS_DELETE_NTF, ETHTOOL_MSG_MSE_GET_REPLY, + ETHTOOL_MSG_PORT_GET_REPLY, =20 __ETHTOOL_MSG_KERNEL_CNT, ETHTOOL_MSG_KERNEL_MAX =3D (__ETHTOOL_MSG_KERNEL_CNT - 1) --=20 2.49.0 From nobody Wed Apr 1 22:34:46 2026 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 314D23A254E for ; Wed, 25 Mar 2026 08:20:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426827; cv=none; b=r7yj9C0L11AQYYZxQiJDEZExTdrnV8oHA1lBikKAXvREL7HXi2y2ZXUQ9RJHnNiowrONPxXNwL39B3wxKO8uCMYTTmBfTXgFd1Wh3JsErbKjRfHM63vbPoRDMzMGIzOCuyjgemFUDB2IfPXIjuPjY9mGQ+fPBbZ/5uZ4o470OGw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774426827; c=relaxed/simple; bh=NihJt7iBdGDuI3mCuJh7WoqreTQ6eC/M0bLX5g/YOcM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tHmWOI26c8u/Yqu+3/dxzpZyHjNpYA3ahiOUM8nei74ZATMt15Sf5Czt2XwE6b1+W7LCVCfSmrP5Wvf/onn/ISPGrIcxCtJCWYwgyAgudU9x6ffVIwm21GPwRuiRRxgEIh/1wWH0/LP3IOcG2JtmH931Dxun9UocZtUolzyJ2k0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=j73dLK9G; arc=none smtp.client-ip=185.246.84.56 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.b="j73dLK9G" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id ED91F1A2FE0; Wed, 25 Mar 2026 08:20:23 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id BB99E601A1; Wed, 25 Mar 2026 08:20:23 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 6BD701045136F; Wed, 25 Mar 2026 09:20:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1774426822; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=Yn7FgoZNO+vO4ksqXWBcO7reXcYMLYat/OxPYZb8SpE=; b=j73dLK9Grl0CrwDQaih6X/KWh1wO50AbIlZ0ICN2DA6z9JM6oe/7PuPK6ZebdGKP/WUFw/ QapSdaxzlNAcCPZ7fyRxy9+u23a1BWtoN/AXfcfOTVE0AeqE6YawycFbNCDPkX70kNuRnq KDH0ZjNOpxiisOjmPJN3okoIjUC6AqBQSw+uMNkg+Mk7Xi4LwRBSiCviDzRb3y/xzAIkkl MU812fhfABOb18cJiqwpX9d81PMrqESTV8/Lf+v+NK8ZWXzHzxY2tM8ficg9vNRrVtthon XyPupzCxU2D9cY0DUsaFIQzFghu9NyEaoIARPEzsbaqgKJaFfFsK2Pd0SxZ6Uw== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , Heiner Kallweit Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , mwojtas@chromium.org, Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v8 10/10] net: ethtool: Introduce ethtool command to list ports Date: Wed, 25 Mar 2026 09:19:35 +0100 Message-ID: <20260325081937.571115-11-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260325081937.571115-1-maxime.chevallier@bootlin.com> References: <20260325081937.571115-1-maxime.chevallier@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" Expose the phy_port information to userspace, so that we can know how many ports are available on a given interface, as well as their capabilities. For MDI ports, we report the list of supported linkmodes based on what the PHY that drives this port says. For MII ports, i.e. empty SFP cages, we report the MII linkmodes that we can output on this port. Signed-off-by: Maxime Chevallier --- MAINTAINERS | 1 + net/ethtool/Makefile | 2 +- net/ethtool/netlink.c | 25 +++ net/ethtool/netlink.h | 8 + net/ethtool/port.c | 368 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 403 insertions(+), 1 deletion(-) create mode 100644 net/ethtool/port.c diff --git a/MAINTAINERS b/MAINTAINERS index a09bf30a057d..236747fb6307 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -18408,6 +18408,7 @@ F: Documentation/devicetree/bindings/net/ethernet-c= onnector.yaml F: Documentation/networking/phy-port.rst F: drivers/net/phy/phy_port.c F: include/linux/phy_port.h +F: net/ethtool/port.c K: struct\s+phy_port|phy_port_ =20 NETWORKING [GENERAL] diff --git a/net/ethtool/Makefile b/net/ethtool/Makefile index 629c10916670..9b5b09670008 100644 --- a/net/ethtool/Makefile +++ b/net/ethtool/Makefile @@ -9,4 +9,4 @@ ethtool_nl-y :=3D netlink.o bitset.o strset.o linkinfo.o li= nkmodes.o rss.o \ channels.o coalesce.o pause.o eee.o tsinfo.o cabletest.o \ tunnels.o fec.o eeprom.o stats.o phc_vclocks.o mm.o \ module.o cmis_fw_update.o cmis_cdb.o pse-pd.o plca.o \ - phy.o tsconfig.o mse.o + phy.o tsconfig.o mse.o port.o diff --git a/net/ethtool/netlink.c b/net/ethtool/netlink.c index 5046023a30b1..7d23aba7c30d 100644 --- a/net/ethtool/netlink.c +++ b/net/ethtool/netlink.c @@ -19,6 +19,8 @@ static u32 ethnl_bcast_seq; ETHTOOL_FLAG_OMIT_REPLY) #define ETHTOOL_FLAGS_STATS (ETHTOOL_FLAGS_BASIC | ETHTOOL_FLAG_STATS) =20 +char phy_interface_names[PHY_INTERFACE_MODE_MAX][ETH_GSTRING_LEN] __ro_aft= er_init; + const struct nla_policy ethnl_header_policy[] =3D { [ETHTOOL_A_HEADER_DEV_INDEX] =3D { .type =3D NLA_U32 }, [ETHTOOL_A_HEADER_DEV_NAME] =3D { .type =3D NLA_NUL_STRING, @@ -422,6 +424,7 @@ ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] =3D { [ETHTOOL_MSG_TSCONFIG_SET] =3D ðnl_tsconfig_request_ops, [ETHTOOL_MSG_PHY_GET] =3D ðnl_phy_request_ops, [ETHTOOL_MSG_MSE_GET] =3D ðnl_mse_request_ops, + [ETHTOOL_MSG_PORT_GET] =3D ðnl_port_request_ops, }; =20 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *= cb) @@ -1546,6 +1549,15 @@ static const struct genl_ops ethtool_genl_ops[] =3D { .policy =3D ethnl_mse_get_policy, .maxattr =3D ARRAY_SIZE(ethnl_mse_get_policy) - 1, }, + { + .cmd =3D ETHTOOL_MSG_PORT_GET, + .doit =3D ethnl_default_doit, + .start =3D ethnl_port_dump_start, + .dumpit =3D ethnl_port_dumpit, + .done =3D ethnl_port_dump_done, + .policy =3D ethnl_port_get_policy, + .maxattr =3D ARRAY_SIZE(ethnl_port_get_policy) - 1, + }, }; =20 static const struct genl_multicast_group ethtool_nl_mcgrps[] =3D { @@ -1568,10 +1580,23 @@ static struct genl_family ethtool_genl_family __ro_= after_init =3D { =20 /* module setup */ =20 +static void __init ethnl_phy_names_populate(void) +{ + const char *name; + int i; + + for (i =3D 0; i < PHY_INTERFACE_MODE_MAX; i++) { + name =3D phy_modes(i); + strscpy(phy_interface_names[i], name, ETH_GSTRING_LEN); + } +} + static int __init ethnl_init(void) { int ret; =20 + ethnl_phy_names_populate(); + ret =3D genl_register_family(ðtool_genl_family); if (WARN(ret < 0, "ethtool: genetlink family registration failed")) return ret; diff --git a/net/ethtool/netlink.h b/net/ethtool/netlink.h index aaf6f2468768..8b131e53fba4 100644 --- a/net/ethtool/netlink.h +++ b/net/ethtool/netlink.h @@ -5,11 +5,14 @@ =20 #include #include +#include #include #include =20 struct ethnl_req_info; =20 +extern char phy_interface_names[PHY_INTERFACE_MODE_MAX][ETH_GSTRING_LEN]; + u32 ethnl_bcast_seq_next(void); int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info, const struct nlattr *nest, struct net *net, @@ -444,6 +447,7 @@ extern const struct ethnl_request_ops ethnl_mm_request_= ops; extern const struct ethnl_request_ops ethnl_phy_request_ops; extern const struct ethnl_request_ops ethnl_tsconfig_request_ops; extern const struct ethnl_request_ops ethnl_mse_request_ops; +extern const struct ethnl_request_ops ethnl_port_request_ops; =20 extern const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_FLAGS = + 1]; extern const struct nla_policy ethnl_header_policy_stats[ETHTOOL_A_HEADER_= FLAGS + 1]; @@ -500,6 +504,7 @@ extern const struct nla_policy ethnl_phy_get_policy[ETH= TOOL_A_PHY_HEADER + 1]; extern const struct nla_policy ethnl_tsconfig_get_policy[ETHTOOL_A_TSCONFI= G_HEADER + 1]; extern const struct nla_policy ethnl_tsconfig_set_policy[ETHTOOL_A_TSCONFI= G_MAX + 1]; extern const struct nla_policy ethnl_mse_get_policy[ETHTOOL_A_MSE_HEADER += 1]; +extern const struct nla_policy ethnl_port_get_policy[ETHTOOL_A_PORT_ID + 1= ]; =20 int ethnl_set_features(struct sk_buff *skb, struct genl_info *info); int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info); @@ -515,6 +520,9 @@ int ethnl_tsinfo_dumpit(struct sk_buff *skb, struct net= link_callback *cb); int ethnl_tsinfo_done(struct netlink_callback *cb); int ethnl_rss_create_doit(struct sk_buff *skb, struct genl_info *info); int ethnl_rss_delete_doit(struct sk_buff *skb, struct genl_info *info); +int ethnl_port_dump_start(struct netlink_callback *cb); +int ethnl_port_dumpit(struct sk_buff *skb, struct netlink_callback *cb); +int ethnl_port_dump_done(struct netlink_callback *cb); =20 extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN]; extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_G= STRING_LEN]; diff --git a/net/ethtool/port.c b/net/ethtool/port.c new file mode 100644 index 000000000000..2b32c99e06f0 --- /dev/null +++ b/net/ethtool/port.c @@ -0,0 +1,368 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2026 Bootlin + */ +#include +#include +#include +#include + +#include "bitset.h" +#include "common.h" +#include "netlink.h" + +struct port_req_info { + struct ethnl_req_info base; + u32 port_id; +}; + +struct port_reply_data { + struct ethnl_reply_data base; + __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); + DECLARE_PHY_INTERFACE_MASK(interfaces); + u32 port_id; + bool mii; + bool sfp; + bool vacant; +}; + +#define PORT_REQINFO(__req_base) \ + container_of(__req_base, struct port_req_info, base) + +#define PORT_REPDATA(__reply_base) \ + container_of(__reply_base, struct port_reply_data, base) + +const struct nla_policy ethnl_port_get_policy[ETHTOOL_A_PORT_ID + 1] =3D { + [ETHTOOL_A_PORT_HEADER] =3D NLA_POLICY_NESTED(ethnl_header_policy), + [ETHTOOL_A_PORT_ID] =3D NLA_POLICY_MIN(NLA_U32, 1), +}; + +static int port_parse_request(struct ethnl_req_info *req_info, + const struct genl_info *info, + struct nlattr **tb, + struct netlink_ext_ack *extack) +{ + struct port_req_info *request =3D PORT_REQINFO(req_info); + + if (GENL_REQ_ATTR_CHECK(info, ETHTOOL_A_PORT_ID)) + return -EINVAL; + + request->port_id =3D nla_get_u32(tb[ETHTOOL_A_PORT_ID]); + + return 0; +} + +static int port_prepare_data(const struct ethnl_req_info *req_info, + struct ethnl_reply_data *reply_data, + const struct genl_info *info) +{ + struct port_reply_data *reply =3D PORT_REPDATA(reply_data); + struct port_req_info *request =3D PORT_REQINFO(req_info); + struct phy_port *port; + + /* RTNL must be held while holding a ref to the phy_port. Here, caller + * holds RTNL. + */ + port =3D phy_link_topo_get_port(req_info->dev, request->port_id); + if (!port) + return -ENODEV; + + linkmode_copy(reply->supported, port->supported); + phy_interface_copy(reply->interfaces, port->interfaces); + reply->port_id =3D port->id; + reply->mii =3D port->is_mii; + reply->sfp =3D port->is_sfp; + reply->vacant =3D port->vacant; + + return 0; +} + +static int port_reply_size(const struct ethnl_req_info *req_info, + const struct ethnl_reply_data *reply_data) +{ + bool compact =3D req_info->flags & ETHTOOL_FLAG_COMPACT_BITSETS; + struct port_reply_data *reply =3D PORT_REPDATA(reply_data); + size_t size =3D 0; + int ret; + + /* ETHTOOL_A_PORT_ID */ + size +=3D nla_total_size(sizeof(u32)); + + if (!reply->mii) { + /* ETHTOOL_A_PORT_SUPPORTED_MODES */ + ret =3D ethnl_bitset_size(reply->supported, NULL, + __ETHTOOL_LINK_MODE_MASK_NBITS, + link_mode_names, compact); + if (ret < 0) + return ret; + + size +=3D ret; + } else { + /* ETHTOOL_A_PORT_SUPPORTED_INTERFACES */ + ret =3D ethnl_bitset_size(reply->interfaces, NULL, + PHY_INTERFACE_MODE_MAX, + phy_interface_names, compact); + if (ret < 0) + return ret; + + size +=3D ret; + } + + /* ETHTOOL_A_PORT_TYPE */ + size +=3D nla_total_size(sizeof(u32)); + + /* ETHTOOL_A_PORT_VACANT */ + size +=3D nla_total_size(sizeof(u32)); + + return size; +} + +static int port_fill_reply(struct sk_buff *skb, + const struct ethnl_req_info *req_info, + const struct ethnl_reply_data *reply_data) +{ + bool compact =3D req_info->flags & ETHTOOL_FLAG_COMPACT_BITSETS; + struct port_reply_data *reply =3D PORT_REPDATA(reply_data); + int ret, port_type =3D ETHTOOL_PORT_TYPE_MDI; + + if (nla_put_u32(skb, ETHTOOL_A_PORT_ID, reply->port_id)) + return -EMSGSIZE; + + if (!reply->mii) { + ret =3D ethnl_put_bitset(skb, ETHTOOL_A_PORT_SUPPORTED_MODES, + reply->supported, NULL, + __ETHTOOL_LINK_MODE_MASK_NBITS, + link_mode_names, compact); + if (ret < 0) + return ret; + } else { + ret =3D ethnl_put_bitset(skb, ETHTOOL_A_PORT_SUPPORTED_INTERFACES, + reply->interfaces, NULL, + PHY_INTERFACE_MODE_MAX, + phy_interface_names, compact); + if (ret < 0) + return ret; + } + + if (reply->mii || reply->sfp) + port_type =3D ETHTOOL_PORT_TYPE_SFP; + + if (nla_put_u32(skb, ETHTOOL_A_PORT_TYPE, port_type) || + nla_put_u32(skb, ETHTOOL_A_PORT_VACANT, reply->vacant)) + return -EMSGSIZE; + + return 0; +} + +struct port_dump_ctx { + struct port_req_info *req_info; + struct port_reply_data *reply_data; + unsigned long ifindex; + unsigned long pos_portid; +}; + +static struct port_dump_ctx * +port_dump_ctx_get(struct netlink_callback *cb) +{ + return (struct port_dump_ctx *)cb->ctx; +} + +int ethnl_port_dump_start(struct netlink_callback *cb) +{ + const struct genl_dumpit_info *info =3D genl_dumpit_info(cb); + struct port_dump_ctx *ctx =3D port_dump_ctx_get(cb); + struct nlattr **tb =3D info->info.attrs; + struct port_reply_data *reply_data; + struct port_req_info *req_info; + int ret; + + BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); + + req_info =3D kzalloc_obj(*req_info); + if (!req_info) + return -ENOMEM; + + reply_data =3D kmalloc_obj(*reply_data); + if (!reply_data) { + ret =3D -ENOMEM; + goto free_req_info; + } + + ret =3D ethnl_parse_header_dev_get(&req_info->base, tb[ETHTOOL_A_PORT_HEA= DER], + genl_info_net(&info->info), + info->info.extack, false); + if (ret < 0) + goto free_rep_data; + + ctx->ifindex =3D 0; + + /* For filtered DUMP requests, let's just store the ifindex. We'll check + * again if the netdev is still there when looping over the netdev list + * in the DUMP loop. + */ + if (req_info->base.dev) { + ctx->ifindex =3D req_info->base.dev->ifindex; + netdev_put(req_info->base.dev, &req_info->base.dev_tracker); + req_info->base.dev =3D NULL; + } + + ctx->req_info =3D req_info; + ctx->reply_data =3D reply_data; + + return 0; + +free_rep_data: + kfree(reply_data); +free_req_info: + kfree(req_info); + + return ret; +} + +static int port_dump_one(struct sk_buff *skb, struct net_device *dev, + struct netlink_callback *cb) +{ + struct port_dump_ctx *ctx =3D port_dump_ctx_get(cb); + void *ehdr; + int ret; + + ehdr =3D ethnl_dump_put(skb, cb, ETHTOOL_MSG_PORT_GET_REPLY); + if (!ehdr) + return -EMSGSIZE; + + memset(ctx->reply_data, 0, sizeof(struct port_reply_data)); + ctx->reply_data->base.dev =3D dev; + + rtnl_lock(); + netdev_lock_ops(dev); + + ret =3D port_prepare_data(&ctx->req_info->base, &ctx->reply_data->base, + genl_info_dump(cb)); + + netdev_unlock_ops(dev); + rtnl_unlock(); + + if (ret < 0) + goto out; + + ret =3D ethnl_fill_reply_header(skb, dev, ETHTOOL_A_PORT_HEADER); + if (ret < 0) + goto out; + + ret =3D port_fill_reply(skb, &ctx->req_info->base, &ctx->reply_data->base= ); + +out: + ctx->reply_data->base.dev =3D NULL; + if (ret < 0) + genlmsg_cancel(skb, ehdr); + else + genlmsg_end(skb, ehdr); + + return ret; +} + +static int port_dump_one_dev(struct sk_buff *skb, struct netlink_callback = *cb) +{ + struct port_dump_ctx *ctx =3D port_dump_ctx_get(cb); + struct net_device *dev; + struct phy_port *port; + int ret; + + dev =3D ctx->req_info->base.dev; + + if (!dev->link_topo) + return 0; + + xa_for_each_start(&dev->link_topo->ports, ctx->pos_portid, port, + ctx->pos_portid) { + ctx->req_info->port_id =3D ctx->pos_portid; + + ret =3D port_dump_one(skb, dev, cb); + if (ret) + return ret; + } + + ctx->pos_portid =3D 0; + + return 0; +} + +static int port_dump_all_dev(struct sk_buff *skb, struct netlink_callback = *cb) +{ + struct port_dump_ctx *ctx =3D port_dump_ctx_get(cb); + struct net *net =3D sock_net(skb->sk); + netdevice_tracker dev_tracker; + struct net_device *dev; + int ret =3D 0; + + rcu_read_lock(); + for_each_netdev_dump(net, dev, ctx->ifindex) { + netdev_hold(dev, &dev_tracker, GFP_ATOMIC); + rcu_read_unlock(); + + ctx->req_info->base.dev =3D dev; + ret =3D port_dump_one_dev(skb, cb); + + rcu_read_lock(); + netdev_put(dev, &dev_tracker); + ctx->req_info->base.dev =3D NULL; + + if (ret) + break; + + ret =3D 0; + } + rcu_read_unlock(); + + return ret; +} + +int ethnl_port_dumpit(struct sk_buff *skb, struct netlink_callback *cb) +{ + const struct genl_dumpit_info *info =3D genl_dumpit_info(cb); + struct port_dump_ctx *ctx =3D port_dump_ctx_get(cb); + int ret =3D 0; + + if (ctx->ifindex) { + netdevice_tracker dev_tracker; + struct net_device *dev; + + dev =3D netdev_get_by_index(genl_info_net(&info->info), + ctx->ifindex, &dev_tracker, + GFP_KERNEL); + if (!dev) + return -ENODEV; + + ctx->req_info->base.dev =3D dev; + ret =3D port_dump_one_dev(skb, cb); + + netdev_put(dev, &dev_tracker); + } else { + ret =3D port_dump_all_dev(skb, cb); + } + + return ret; +} + +int ethnl_port_dump_done(struct netlink_callback *cb) +{ + struct port_dump_ctx *ctx =3D port_dump_ctx_get(cb); + + kfree(ctx->req_info); + kfree(ctx->reply_data); + + return 0; +} + +const struct ethnl_request_ops ethnl_port_request_ops =3D { + .request_cmd =3D ETHTOOL_MSG_PORT_GET, + .reply_cmd =3D ETHTOOL_MSG_PORT_GET_REPLY, + .hdr_attr =3D ETHTOOL_A_PORT_HEADER, + .req_info_size =3D sizeof(struct port_req_info), + .reply_data_size =3D sizeof(struct port_reply_data), + + .parse_request =3D port_parse_request, + .prepare_data =3D port_prepare_data, + .reply_size =3D port_reply_size, + .fill_reply =3D port_fill_reply, +}; --=20 2.49.0