From nobody Sat Oct 4 17:31:18 2025 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 63AA6306D3A; Thu, 14 Aug 2025 13:58:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755179938; cv=none; b=ttucoXH2y927or75WEKEu2S2R/SQGhQpzkkMTD+fCE6V0dU2dxXRmDDOXto/ID3Q+5PrAxxPdgkXZnj1HL2xuO8QHwfSA6ojC7ngisTlqgFpy+Hj+cQgfZwLwXiiu6OVnO7Hof2PNxw+ZgrOG2fHDzxGpnHk3nR7S+Hzt7fgZsY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755179938; c=relaxed/simple; bh=C3Xu2T3hAnuILljVeu+3jEON/CDYpdPFSeeokvo7p1Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=liJevQsZ4NtoIu/m7CDn0b9bw3mZrDloSQWyixofB5oGUFj554BmWt0txmVEl/r3nHIhfdPtODfwyzGg3Q6O2iLXNK82jbTqY8Cc87MpiL+9lSkks2jpkYLdqXa+8CGgWD6tAXkleivKb7InoJa+OrRgivqMj/q71fZzrk8d3h4= 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=QBbSl6gO; arc=none smtp.client-ip=217.70.183.201 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="QBbSl6gO" Received: by mail.gandi.net (Postfix) with ESMTPSA id 5055644410; Thu, 14 Aug 2025 13:58:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1755179933; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=udI4hLqAZTng/Qv86v8J1mpmbFlLW6aKEGi5U1ckKMY=; b=QBbSl6gOlOJe/Rm8HDwPwXn9H0eR38xt+aIUzpZWT6zOmuK/Y8LJsZNkQ7z5VkvkKlgPua 2a/fJR6wN+LPDWP9h71OsDDE8zbuJ73FRTmF6vJl0QP9KC4Rw+h7TX9OThxol4tZ7vpQRz Hh3xx3xXtuddcE+sjvgk1A/CUZl1XaFD1uEuN0r2oRCN4PSg/79O83TutBWI8EiTLe6jua Q/+inPlqEPivxP2AVXFvZMbb/7C1GLKVe2/Zh0uzDyQyrjT2vDFLiVLRbu/Fl+mgny9OM/ jj6r9u6pMz4XppFU7yU6xCFi29cLm8Z6ywAO7CNnU8tyXO2CC3e0WV8L1bxC/A== From: Maxime Chevallier To: davem@davemloft.net Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, thomas.petazzoni@bootlin.com, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , linux-arm-kernel@lists.infradead.org, Christophe Leroy , Herve Codina , Florian Fainelli , Heiner Kallweit , 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, Antoine Tenart , devicetree@vger.kernel.org, Conor Dooley , Krzysztof Kozlowski , Rob Herring , Romain Gantois , Daniel Golle , Dimitri Fedrau Subject: [PATCH net-next v11 08/16] net: phy: Introduce generic SFP handling for PHY drivers Date: Thu, 14 Aug 2025 15:58:23 +0200 Message-ID: <20250814135832.174911-9-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250814135832.174911-1-maxime.chevallier@bootlin.com> References: <20250814135832.174911-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-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgddugeduvdeiucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeforgigihhmvgcuvehhvghvrghllhhivghruceomhgrgihimhgvrdgthhgvvhgrlhhlihgvrhessghoohhtlhhinhdrtghomheqnecuggftrfgrthhtvghrnhepveegtdffleffleevueellefgjeefvedvjefhheegfefgffdvfeetgeevudetffdtnecukfhppedvrgdtudemtggsudelmeekugegheemgeeltddtmeeiheeikeemvdelsgdumeelvghfheemvgektgejnecuvehluhhsthgvrhfuihiivgepgeenucfrrghrrghmpehinhgvthepvdgrtddumegtsgduleemkegugeehmeegledttdemieehieekmedvlegsudemlegvfhehmegvkegtjedphhgvlhhopehfvgguohhrrgdrhhhomhgvpdhmrghilhhfrhhomhepmhgrgihimhgvrdgthhgvvhgrlhhlihgvrhessghoohhtlhhinhdrtghomhdpnhgspghrtghpthhtohepfedupdhrtghpthhtohepuggrvhgvmhesuggrvhgvmhhlohhfthdrnhgvthdprhgtphhtthhopehmrgigihhmvgdrtghhvghvrghllhhivghrsegsohhothhlihhnrdgtohhmpdhrtghpthhtohepnhgvthguvghvsehvghgvrhdrk hgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqrghrmhdqmhhsmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopegrnhgurhgvfieslhhunhhnrdgthhdprhgtphhtthhopehkuhgsrgeskhgvrhhnvghlrdhorhhg X-GND-Sasl: maxime.chevallier@bootlin.com Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Maxime Chevallier --- 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); =20 +static int phy_sfp_module_insert(void *upstream, const struct sfp_eeprom_i= d *id) +{ + struct phy_device *phydev =3D 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 =3D phy_get_sfp_port(phydev); + if (!port) + return -EINVAL; + + sfp_parse_support(phydev->sfp_bus, id, sfp_support, interfaces); + + if (phydev->n_ports =3D=3D 1) + phydev->port =3D 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 req= uested 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 =3D upstream; + struct phy_port *port =3D 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 =3D=3D 1) + phydev->port =3D PORT_NONE; +} + +static void phy_sfp_link_up(void *upstream) +{ + struct phy_device *phydev =3D upstream; + struct phy_port *port =3D 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 =3D upstream; + struct phy_port *port =3D 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 =3D { + .attach =3D phy_sfp_attach, + .detach =3D phy_sfp_detach, + .module_insert =3D phy_sfp_module_insert, + .module_remove =3D phy_sfp_module_remove, + .link_up =3D phy_sfp_link_up, + .link_down =3D phy_sfp_link_down, + .connect_phy =3D phy_sfp_connect_phy, + .disconnect_phy =3D phy_sfp_disconnect_phy, +}; + static int phy_add_port(struct phy_device *phydev, struct phy_port *port) { int ret =3D 0; @@ -1646,6 +1727,7 @@ static int phy_setup_sfp_port(struct phy_device *phyd= ev) * is a MII port. */ port->is_mii =3D true; + port->is_sfp =3D true; =20 phy_add_port(phydev, port); =20 @@ -3479,6 +3561,13 @@ static int phy_setup_ports(struct phy_device *phydev) if (ret) return ret; =20 + /* Use generic SFP probing only if the driver didn't do so already */ + if (!phydev->sfp_bus) { + ret =3D phy_sfp_probe(phydev, &sfp_phydev_ops); + if (ret) + goto out; + } + if (phydev->n_ports < phydev->max_n_ports) { ret =3D phy_default_setup_single_port(phydev); if (ret) @@ -3514,6 +3603,25 @@ static int phy_setup_ports(struct phy_device *phydev) return ret; } =20 +/** + * 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 no= ne + * 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); =20 +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; =20 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; }; =20 struct phy_port *phy_port_alloc(void); --=20 2.49.0