[PATCH net-next v11 08/16] net: phy: Introduce generic SFP handling for PHY drivers

Maxime Chevallier posted 16 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH net-next v11 08/16] net: phy: Introduce generic SFP handling for PHY drivers
Posted by Maxime Chevallier 1 month, 3 weeks ago
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 | 108 +++++++++++++++++++++++++++++++++++
 include/linux/phy.h          |   2 +
 include/linux/phy_port.h     |   1 +
 3 files changed, 111 insertions(+)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index d195e15bde11..038b38ad5c35 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1587,6 +1587,87 @@ 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)
+{
+	struct phy_device *phydev = upstream;
+	struct phy_port *port;
+
+	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
+	DECLARE_PHY_INTERFACE_MASK(interfaces);
+	phy_interface_t iface;
+
+	linkmode_zero(sfp_support);
+
+	port = phy_get_sfp_port(phydev);
+	if (!port)
+		return -EINVAL;
+
+	sfp_parse_support(phydev->sfp_bus, id, sfp_support, interfaces);
+
+	if (phydev->n_ports == 1)
+		phydev->port = sfp_parse_port(phydev->sfp_bus, id, sfp_support);
+
+	linkmode_and(sfp_support, port->supported, sfp_support);
+	linkmode_and(interfaces, interfaces, port->interfaces);
+
+	if (linkmode_empty(sfp_support)) {
+		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n");
+		return -EINVAL;
+	}
+
+	/* Check that this interface is supported */
+	if (!test_bit(iface, port->interfaces)) {
+		dev_err(&phydev->mdio.dev, "PHY %s does not support the SFP module's requested MII interfaces\n", phydev_name(phydev));
+		return -EINVAL;
+	}
+
+	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;
@@ -1646,6 +1727,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);
 
@@ -3479,6 +3561,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)
@@ -3514,6 +3603,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 89e959588036..5bfd542cbfc8 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -2146,6 +2146,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 f47ac5f5ef9e..697721a6239f 100644
--- a/include/linux/phy_port.h
+++ b/include/linux/phy_port.h
@@ -67,6 +67,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
Re: [PATCH net-next v11 08/16] net: phy: Introduce generic SFP handling for PHY drivers
Posted by Simon Horman 1 month, 1 week ago
On Thu, Aug 14, 2025 at 03:58:23PM +0200, Maxime Chevallier wrote:

...

> diff --git a/include/linux/phy_port.h b/include/linux/phy_port.h
> index f47ac5f5ef9e..697721a6239f 100644
> --- a/include/linux/phy_port.h
> +++ b/include/linux/phy_port.h
> @@ -67,6 +67,7 @@ struct phy_port {
>  	unsigned int not_described:1;
>  	unsigned int active:1;
>  	unsigned int is_mii:1;
> +	unsigned int is_sfp:1;

nit: Please also add is_spf to the Kernel doc for this structure.


>  };
>  
>  struct phy_port *phy_port_alloc(void);
> -- 
> 2.49.0
>
Re: [PATCH net-next v11 08/16] net: phy: Introduce generic SFP handling for PHY drivers
Posted by kernel test robot 1 month, 2 weeks ago
Hi Maxime,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Maxime-Chevallier/dt-bindings-net-Introduce-the-ethernet-connector-description/20250814-221559
base:   net-next/main
patch link:    https://lore.kernel.org/r/20250814135832.174911-9-maxime.chevallier%40bootlin.com
patch subject: [PATCH net-next v11 08/16] net: phy: Introduce generic SFP handling for PHY drivers
config: i386-randconfig-013-20250815 (https://download.01.org/0day-ci/archive/20250815/202508151058.jqJsn9VB-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250815/202508151058.jqJsn9VB-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202508151058.jqJsn9VB-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/net/phy/phy_device.c:1625:47: warning: variable 'iface' is uninitialized when used here [-Wuninitialized]
    1625 |                 return port->ops->configure_mii(port, true, iface);
         |                                                             ^~~~~
   drivers/net/phy/phy_device.c:1597:2: note: variable 'iface' is declared here
    1597 |         phy_interface_t iface;
         |         ^
   1 warning generated.


vim +/iface +1625 drivers/net/phy/phy_device.c

  1589	
  1590	static int phy_sfp_module_insert(void *upstream, const struct sfp_eeprom_id *id)
  1591	{
  1592		struct phy_device *phydev = upstream;
  1593		struct phy_port *port;
  1594	
  1595		__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
  1596		DECLARE_PHY_INTERFACE_MASK(interfaces);
  1597		phy_interface_t iface;
  1598	
  1599		linkmode_zero(sfp_support);
  1600	
  1601		port = phy_get_sfp_port(phydev);
  1602		if (!port)
  1603			return -EINVAL;
  1604	
  1605		sfp_parse_support(phydev->sfp_bus, id, sfp_support, interfaces);
  1606	
  1607		if (phydev->n_ports == 1)
  1608			phydev->port = sfp_parse_port(phydev->sfp_bus, id, sfp_support);
  1609	
  1610		linkmode_and(sfp_support, port->supported, sfp_support);
  1611		linkmode_and(interfaces, interfaces, port->interfaces);
  1612	
  1613		if (linkmode_empty(sfp_support)) {
  1614			dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n");
  1615			return -EINVAL;
  1616		}
  1617	
  1618		/* Check that this interface is supported */
  1619		if (!test_bit(iface, port->interfaces)) {
  1620			dev_err(&phydev->mdio.dev, "PHY %s does not support the SFP module's requested MII interfaces\n", phydev_name(phydev));
  1621			return -EINVAL;
  1622		}
  1623	
  1624		if (port->ops && port->ops->configure_mii)
> 1625			return port->ops->configure_mii(port, true, iface);
  1626	
  1627		return 0;
  1628	}
  1629	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH net-next v11 08/16] net: phy: Introduce generic SFP handling for PHY drivers
Posted by Maxime Chevallier 1 month, 1 week ago

On 15/08/2025 05:25, kernel test robot wrote:
> Hi Maxime,
>
> kernel test robot noticed the following build warnings:
>
> [auto build test WARNING on net-next/main]
>
> url:    https://github.com/intel-lab-lkp/linux/commits/Maxime-Chevallier/dt-bindings-net-Introduce-the-ethernet-connector-description/20250814-221559
> base:   net-next/main
> patch link:    https://lore.kernel.org/r/20250814135832.174911-9-maxime.chevallier%40bootlin.com
> patch subject: [PATCH net-next v11 08/16] net: phy: Introduce generic SFP handling for PHY drivers
> config: i386-randconfig-013-20250815 (https://download.01.org/0day-ci/archive/20250815/202508151058.jqJsn9VB-lkp@intel.com/config)
> compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250815/202508151058.jqJsn9VB-lkp@intel.com/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202508151058.jqJsn9VB-lkp@intel.com/
>
> All warnings (new ones prefixed by >>):
>
>>> drivers/net/phy/phy_device.c:1625:47: warning: variable 'iface' is uninitialized when used here [-Wuninitialized]
>      1625 |                 return port->ops->configure_mii(port, true, iface);
>           |                                                             ^~~~~
>     drivers/net/phy/phy_device.c:1597:2: note: variable 'iface' is declared here
>      1597 |         phy_interface_t iface;
>           |         ^
>     1 warning generated.
>
That's completly wrong indeed... I had an extra question to ask to Russell
wrt. that feature, then forgot about it and sent the series...

I'll address that then

Maxime