From: Maxime Chevallier <maxime.chevallier@bootlin.com>
To: davem@davemloft.net
Cc: "Maxime Chevallier" <maxime.chevallier@bootlin.com>,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-arm-msm@vger.kernel.org, thomas.petazzoni@bootlin.com,
"Andrew Lunn" <andrew@lunn.ch>,
"Jakub Kicinski" <kuba@kernel.org>,
"Eric Dumazet" <edumazet@google.com>,
"Paolo Abeni" <pabeni@redhat.com>,
"Russell King" <linux@armlinux.org.uk>,
linux-arm-kernel@lists.infradead.org,
"Christophe Leroy" <christophe.leroy@csgroup.eu>,
"Herve Codina" <herve.codina@bootlin.com>,
"Florian Fainelli" <f.fainelli@gmail.com>,
"Heiner Kallweit" <hkallweit1@gmail.com>,
"Vladimir Oltean" <vladimir.oltean@nxp.com>,
"Köry Maincent" <kory.maincent@bootlin.com>,
"Marek Behún" <kabel@kernel.org>,
"Oleksij Rempel" <o.rempel@pengutronix.de>,
"Nicolò Veronese" <nicveronese@gmail.com>,
"Simon Horman" <horms@kernel.org>,
mwojtas@chromium.org, "Antoine Tenart" <atenart@kernel.org>,
devicetree@vger.kernel.org, "Conor Dooley" <conor+dt@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Romain Gantois" <romain.gantois@bootlin.com>,
"Daniel Golle" <daniel@makrotopia.org>,
"Dimitri Fedrau" <dimitri.fedrau@liebherr.com>
Subject: [PATCH net-next v15 07/15] net: phy: Introduce generic SFP handling for PHY drivers
Date: Thu, 6 Nov 2025 10:47:32 +0100 [thread overview]
Message-ID: <20251106094742.2104099-8-maxime.chevallier@bootlin.com> (raw)
In-Reply-To: <20251106094742.2104099-1-maxime.chevallier@bootlin.com>
There are currently 4 PHY drivers that can drive downstream SFPs:
marvell.c, marvell10g.c, at803x.c and marvell-88x2222.c. Most of the
logic is boilerplate, either calling into generic phylib helpers (for
SFP PHY attach, bus attach, etc.) or performing the same tasks with a
bit of validation :
- Getting the module's expected interface mode
- Making sure the PHY supports it
- Optionaly perform some configuration to make sure the PHY outputs
the right mode
This can be made more generic by leveraging the phy_port, and its
configure_mii() callback which allows setting a port's interfaces when
the port is a serdes.
Introduce a generic PHY SFP support. If a driver doesn't probe the SFP
bus itself, but an SFP phandle is found in devicetree/firmware, then the
generic PHY SFP support will be used, relying on port ops.
PHY driver need to :
- Register a .attach_port() callback
- When a serdes port is registered to the PHY, drivers must set
port->interfaces to the set of PHY_INTERFACE_MODE the port can output
- If the port has limitations regarding speed, duplex and aneg, the
port can also fine-tune the final linkmodes that can be supported
- The port may register a set of ops, including .configure_mii(), that
will be called at module_insert time to adjust the interface based on
the module detected.
Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
drivers/net/phy/phy_device.c | 107 +++++++++++++++++++++++++++++++++++
include/linux/phy.h | 2 +
include/linux/phy_port.h | 2 +
3 files changed, 111 insertions(+)
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 8e51de932e54..9e39424164c3 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1598,6 +1598,86 @@ void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
}
EXPORT_SYMBOL(phy_sfp_detach);
+static int phy_sfp_module_insert(void *upstream, const struct sfp_eeprom_id *id)
+{
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
+ struct phy_device *phydev = upstream;
+ const struct sfp_module_caps *caps;
+ struct phy_port *port;
+
+ phy_interface_t iface;
+
+ linkmode_zero(sfp_support);
+
+ port = phy_get_sfp_port(phydev);
+ if (!port)
+ return -EINVAL;
+
+ caps = sfp_get_module_caps(phydev->sfp_bus);
+
+ linkmode_and(sfp_support, port->supported, caps->link_modes);
+ if (linkmode_empty(sfp_support)) {
+ dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n");
+ return -EINVAL;
+ }
+
+ iface = sfp_select_interface(phydev->sfp_bus, sfp_support);
+ if (iface == PHY_INTERFACE_MODE_NA) {
+ dev_err(&phydev->mdio.dev, "PHY %s does not support the SFP module's requested MII interfaces\n",
+ phydev_name(phydev));
+ return -EINVAL;
+ }
+
+ if (phydev->n_ports == 1)
+ phydev->port = caps->port;
+
+ if (port->ops && port->ops->configure_mii)
+ return port->ops->configure_mii(port, true, iface);
+
+ return 0;
+}
+
+static void phy_sfp_module_remove(void *upstream)
+{
+ struct phy_device *phydev = upstream;
+ struct phy_port *port = phy_get_sfp_port(phydev);
+
+ if (port && port->ops && port->ops->configure_mii)
+ port->ops->configure_mii(port, false, PHY_INTERFACE_MODE_NA);
+
+ if (phydev->n_ports == 1)
+ phydev->port = PORT_NONE;
+}
+
+static void phy_sfp_link_up(void *upstream)
+{
+ struct phy_device *phydev = upstream;
+ struct phy_port *port = phy_get_sfp_port(phydev);
+
+ if (port && port->ops && port->ops->link_up)
+ port->ops->link_up(port);
+}
+
+static void phy_sfp_link_down(void *upstream)
+{
+ struct phy_device *phydev = upstream;
+ struct phy_port *port = phy_get_sfp_port(phydev);
+
+ if (port && port->ops && port->ops->link_down)
+ port->ops->link_down(port);
+}
+
+static const struct sfp_upstream_ops sfp_phydev_ops = {
+ .attach = phy_sfp_attach,
+ .detach = phy_sfp_detach,
+ .module_insert = phy_sfp_module_insert,
+ .module_remove = phy_sfp_module_remove,
+ .link_up = phy_sfp_link_up,
+ .link_down = phy_sfp_link_down,
+ .connect_phy = phy_sfp_connect_phy,
+ .disconnect_phy = phy_sfp_disconnect_phy,
+};
+
static int phy_add_port(struct phy_device *phydev, struct phy_port *port)
{
int ret = 0;
@@ -1657,6 +1737,7 @@ static int phy_setup_sfp_port(struct phy_device *phydev)
* is a MII port.
*/
port->is_mii = true;
+ port->is_sfp = true;
phy_add_port(phydev, port);
@@ -3497,6 +3578,13 @@ static int phy_setup_ports(struct phy_device *phydev)
if (ret)
return ret;
+ /* Use generic SFP probing only if the driver didn't do so already */
+ if (!phydev->sfp_bus) {
+ ret = phy_sfp_probe(phydev, &sfp_phydev_ops);
+ if (ret)
+ goto out;
+ }
+
if (phydev->n_ports < phydev->max_n_ports) {
ret = phy_default_setup_single_port(phydev);
if (ret)
@@ -3532,6 +3620,25 @@ static int phy_setup_ports(struct phy_device *phydev)
return ret;
}
+/**
+ * phy_get_sfp_port() - Returns the first valid SFP port of a PHY
+ * @phydev: pointer to the PHY device to get the SFP port from
+ *
+ * Returns: The first active SFP (serdes) port of a PHY device, NULL if none
+ * exist.
+ */
+struct phy_port *phy_get_sfp_port(struct phy_device *phydev)
+{
+ struct phy_port *port;
+
+ list_for_each_entry(port, &phydev->ports, head)
+ if (port->active && port->is_sfp)
+ return port;
+
+ return NULL;
+}
+EXPORT_SYMBOL_GPL(phy_get_sfp_port);
+
/**
* fwnode_mdio_find_device - Given a fwnode, find the mdio_device
* @fwnode: pointer to the mdio_device's fwnode
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 0d33669f3471..2a9763e923fd 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -2420,6 +2420,8 @@ int __phy_hwtstamp_set(struct phy_device *phydev,
struct kernel_hwtstamp_config *config,
struct netlink_ext_ack *extack);
+struct phy_port *phy_get_sfp_port(struct phy_device *phydev);
+
extern const struct bus_type mdio_bus_type;
extern const struct class mdio_bus_class;
diff --git a/include/linux/phy_port.h b/include/linux/phy_port.h
index ce0208fbccf7..550c3f4ab19f 100644
--- a/include/linux/phy_port.h
+++ b/include/linux/phy_port.h
@@ -49,6 +49,7 @@ struct phy_port_ops {
* @active: Indicates if the port is currently part of the active link.
* @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.
*/
struct phy_port {
struct list_head head;
@@ -67,6 +68,7 @@ struct phy_port {
unsigned int not_described:1;
unsigned int active:1;
unsigned int is_mii:1;
+ unsigned int is_sfp:1;
};
struct phy_port *phy_port_alloc(void);
--
2.49.0
next prev parent reply other threads:[~2025-11-06 9:48 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-06 9:47 [PATCH net-next v15 00/15] net: phy: Introduce PHY ports representation Maxime Chevallier
2025-11-06 9:47 ` [PATCH net-next v15 01/15] dt-bindings: net: Introduce the ethernet-connector description Maxime Chevallier
2025-11-11 3:34 ` Andrew Lunn
2025-11-12 8:16 ` Maxime Chevallier
2025-11-13 8:10 ` Maxime Chevallier
2025-11-12 12:43 ` Rob Herring
2025-11-12 13:25 ` Andrew Lunn
2025-11-12 13:37 ` Maxime Chevallier
2025-11-06 9:47 ` [PATCH net-next v15 02/15] net: ethtool: Introduce ETHTOOL_LINK_MEDIUM_* values Maxime Chevallier
2025-11-11 3:37 ` Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 03/15] net: phy: Introduce PHY ports representation Maxime Chevallier
2025-11-11 3:53 ` Andrew Lunn
2025-11-12 8:23 ` Maxime Chevallier
2025-11-06 9:47 ` [PATCH net-next v15 04/15] net: phy: dp83822: Add support for phy_port representation Maxime Chevallier
2025-11-11 3:54 ` Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 05/15] dt-bindings: net: dp83822: Deprecate ti,fiber-mode Maxime Chevallier
2025-11-06 9:47 ` [PATCH net-next v15 06/15] net: phy: Create a phy_port for PHY-driven SFPs Maxime Chevallier
2025-11-11 3:56 ` Andrew Lunn
2025-11-06 9:47 ` Maxime Chevallier [this message]
2025-11-11 3:58 ` [PATCH net-next v15 07/15] net: phy: Introduce generic SFP handling for PHY drivers Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 08/15] net: phy: marvell-88x2222: Support SFP through phy_port interface Maxime Chevallier
2025-11-11 4:00 ` Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 09/15] net: phy: marvell: " Maxime Chevallier
2025-11-11 4:01 ` Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 10/15] net: phy: marvell10g: Support SFP through phy_port Maxime Chevallier
2025-11-11 4:03 ` Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 11/15] net: phy: at803x: Support SFP through phy_port interface Maxime Chevallier
2025-11-11 4:05 ` Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 12/15] net: phy: qca807x: " Maxime Chevallier
2025-11-11 4:06 ` Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 13/15] net: phy: Only rely on phy_port for PHY-driven SFP Maxime Chevallier
2025-11-11 4:07 ` Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 14/15] net: phy: dp83822: Add SFP support through the phy_port interface Maxime Chevallier
2025-11-11 4:07 ` Andrew Lunn
2025-11-06 9:47 ` [PATCH net-next v15 15/15] Documentation: networking: Document the phy_port infrastructure Maxime Chevallier
2025-11-11 4:11 ` Andrew Lunn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251106094742.2104099-8-maxime.chevallier@bootlin.com \
--to=maxime.chevallier@bootlin.com \
--cc=andrew@lunn.ch \
--cc=atenart@kernel.org \
--cc=christophe.leroy@csgroup.eu \
--cc=conor+dt@kernel.org \
--cc=daniel@makrotopia.org \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=dimitri.fedrau@liebherr.com \
--cc=edumazet@google.com \
--cc=f.fainelli@gmail.com \
--cc=herve.codina@bootlin.com \
--cc=hkallweit1@gmail.com \
--cc=horms@kernel.org \
--cc=kabel@kernel.org \
--cc=kory.maincent@bootlin.com \
--cc=krzk+dt@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=mwojtas@chromium.org \
--cc=netdev@vger.kernel.org \
--cc=nicveronese@gmail.com \
--cc=o.rempel@pengutronix.de \
--cc=pabeni@redhat.com \
--cc=robh@kernel.org \
--cc=romain.gantois@bootlin.com \
--cc=thomas.petazzoni@bootlin.com \
--cc=vladimir.oltean@nxp.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).