From patchwork Wed Sep 11 21:27:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Chevallier X-Patchwork-Id: 13801129 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EC695EE57D7 for ; Wed, 11 Sep 2024 21:29:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=8rqeH8IixeEEWajyVevr1Htj2CPvSE4kTjgluMfWNRM=; b=xNS00jUS6oQQhW2UArMB3tcfYf RP8Aa8ORRIRHBfSs3WUrWRLaQl+apTm7LGF8F1kvHDeUd2A5pWkr53Q/b9BiZ0q14CSRbv2KB/l3s 6lTTIT/L0g5PsN0yGYQVzjEy2FEdaN/iiJWLs0NdNqiZNxgegk38EYyDQeJU1Vs0J8hY+wJoCB1Nc sU1fKbXHxmTipuTUrbC7cJ7VABFJP/AZcmDBhVP5IyOsZ6PsC7FjwH3XhNHhjxN0lW5A4/l/5y1IA VnOVBOeZsT1lqKi+KfdsM9fAiT8vozhRytgnotBSWe0AawB69BHRqIUn3zCA2lk/U8s1E0a6ipZRx lUjS2Cww==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1soUu2-0000000B3i1-20N8; Wed, 11 Sep 2024 21:29:30 +0000 Received: from relay5-d.mail.gandi.net ([217.70.183.197]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1soUrw-0000000B3Bb-1CO4 for linux-arm-kernel@lists.infradead.org; Wed, 11 Sep 2024 21:27:22 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 279D81C0005; Wed, 11 Sep 2024 21:27:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1726090038; 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=8rqeH8IixeEEWajyVevr1Htj2CPvSE4kTjgluMfWNRM=; b=gaHkeb+X0hYdtP+OMqx1viofufMrfCd8Zbtxg2hs15VCuqSBbdVlcZUnJBFYEHHUSy6HhT 88zFlZlTAw12gpqc676LuBGIBYvyrSehYZ11M7cDOuq44xrS7vKkcbqgTtUG1KjqOqIsN0 EiUP5OxaDSmA40hH/HGcSjlbnACW8nwqM7sBqrGN0anoXIEJXVfE4VGQUDoACNAAWH1hOn FwCriRIENAxX7OHMqA8610qLX/ZL5cHFGZKp3EiOOF5vZXbmmPe09ft9JkN38WWkFqoF9j WA8+8Hb3XqnJPHrtrpLRqfBz/5rMmwoebV2lSiUQ86xpMZ7clEbcazqulxAMSA== From: Maxime Chevallier To: davem@davemloft.net Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@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?Marek_Beh=C3=BAn?= , =?utf-8?q?K=C3=B6ry_Maince?= =?utf-8?q?nt?= , Oleksij Rempel Subject: [PATCH net-next 1/7] net: phy: allow isolating PHY devices Date: Wed, 11 Sep 2024 23:27:05 +0200 Message-ID: <20240911212713.2178943-2-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240911212713.2178943-1-maxime.chevallier@bootlin.com> References: <20240911212713.2178943-1-maxime.chevallier@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: maxime.chevallier@bootlin.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240911_142720_876953_18ACD3B4 X-CRM114-Status: GOOD ( 20.46 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The 802.3 specifications describes the isolation mode as setting the PHY's MII interface in high-impedance mode, thus isolating the PHY from that bus. This effectively breaks the link between the MAC and the PHY, but without necessarily disrupting the link between the PHY and the LP. This mode can be useful for testing purposes, but also when there are multiple PHYs on the same MII bus (a case that the 802.3 specification refers to). In Isolation mode, the PHY will still continue to respond to MDIO commands. Introduce a helper to set the phy in an isolated mode. Signed-off-by: Maxime Chevallier --- drivers/net/phy/phy_device.c | 76 +++++++++++++++++++++++++++++++++--- include/linux/phy.h | 4 ++ 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c index 560e338b307a..c468e72bef4b 100644 --- a/drivers/net/phy/phy_device.c +++ b/drivers/net/phy/phy_device.c @@ -2127,6 +2127,38 @@ int phy_loopback(struct phy_device *phydev, bool enable) } EXPORT_SYMBOL(phy_loopback); +int phy_isolate(struct phy_device *phydev, bool enable) +{ + int ret = 0; + + if (!phydev->drv) + return -EIO; + + mutex_lock(&phydev->lock); + + if (enable && phydev->isolated) { + ret = -EBUSY; + goto out; + } + + if (!enable && !phydev->isolated) { + ret = -EINVAL; + goto out; + } + + ret = genphy_isolate(phydev, enable); + + if (ret) + goto out; + + phydev->isolated = enable; + +out: + mutex_unlock(&phydev->lock); + return ret; +} +EXPORT_SYMBOL(phy_isolate); + /** * phy_reset_after_clk_enable - perform a PHY reset if needed * @phydev: target phy_device struct @@ -2280,7 +2312,7 @@ int genphy_setup_forced(struct phy_device *phydev) ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); return phy_modify(phydev, MII_BMCR, - ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); + ~(BMCR_LOOPBACK | BMCR_PDOWN), ctl); } EXPORT_SYMBOL(genphy_setup_forced); @@ -2369,8 +2401,11 @@ EXPORT_SYMBOL(genphy_read_master_slave); */ int genphy_restart_aneg(struct phy_device *phydev) { - /* Don't isolate the PHY if we're negotiating */ - return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, + u16 mask = phydev->isolated ? 0 : BMCR_ISOLATE; + /* Don't isolate the PHY if we're negotiating, unless the PHY is + * explicitly isolated + */ + return phy_modify(phydev, MII_BMCR, mask, BMCR_ANENABLE | BMCR_ANRESTART); } EXPORT_SYMBOL(genphy_restart_aneg); @@ -2394,7 +2429,8 @@ int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart) if (ret < 0) return ret; - if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE)) + if (!(ret & BMCR_ANENABLE) || + ((ret & BMCR_ISOLATE) && !phydev->isolated)) restart = true; } @@ -2495,7 +2531,8 @@ int genphy_c37_config_aneg(struct phy_device *phydev) if (ctl < 0) return ctl; - if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) + if (!(ctl & BMCR_ANENABLE) || + ((ctl & BMCR_ISOLATE) && !phydev->isolated)) changed = 1; /* do restart aneg */ } @@ -2782,12 +2819,18 @@ EXPORT_SYMBOL(genphy_c37_read_status); int genphy_soft_reset(struct phy_device *phydev) { u16 res = BMCR_RESET; + u16 mask = 0; int ret; if (phydev->autoneg == AUTONEG_ENABLE) res |= BMCR_ANRESTART; - ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res); + if (phydev->isolated) + res |= BMCR_ISOLATE; + else + mask |= BMCR_ISOLATE; + + ret = phy_modify(phydev, MII_BMCR, mask, res); if (ret < 0) return ret; @@ -2912,6 +2955,12 @@ int genphy_loopback(struct phy_device *phydev, bool enable) u16 ctl = BMCR_LOOPBACK; int ret, val; + /* Isolating and looping-back the MII interface doesn't really + * make sense + */ + if (phydev->isolated) + return -EINVAL; + ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); phy_modify(phydev, MII_BMCR, ~0, ctl); @@ -2924,6 +2973,8 @@ int genphy_loopback(struct phy_device *phydev, bool enable) } else { phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0); + genphy_isolate(phydev, phydev->isolated); + phy_config_aneg(phydev); } @@ -2931,6 +2982,19 @@ int genphy_loopback(struct phy_device *phydev, bool enable) } EXPORT_SYMBOL(genphy_loopback); +int genphy_isolate(struct phy_device *phydev, bool enable) +{ + u16 val = 0; + + if (enable) + val = BMCR_ISOLATE; + + phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, val); + + return 0; +} +EXPORT_SYMBOL(genphy_isolate); + /** * phy_remove_link_mode - Remove a supported link mode * @phydev: phy_device structure to remove link mode from diff --git a/include/linux/phy.h b/include/linux/phy.h index a98bc91a0cde..ae33919aa0f5 100644 --- a/include/linux/phy.h +++ b/include/linux/phy.h @@ -573,6 +573,7 @@ struct macsec_ops; * @mac_managed_pm: Set true if MAC driver takes of suspending/resuming PHY * @wol_enabled: Set to true if the PHY or the attached MAC have Wake-on-LAN * enabled. + * @isolated: Set to true if the PHY's MII has been isolated. * @state: State of the PHY for management purposes * @dev_flags: Device-specific flags used by the PHY driver. * @@ -676,6 +677,7 @@ struct phy_device { unsigned is_on_sfp_module:1; unsigned mac_managed_pm:1; unsigned wol_enabled:1; + unsigned isolated:1; unsigned autoneg:1; /* The most recently read link state */ @@ -1781,6 +1783,7 @@ int phy_suspend(struct phy_device *phydev); int phy_resume(struct phy_device *phydev); int __phy_resume(struct phy_device *phydev); int phy_loopback(struct phy_device *phydev, bool enable); +int phy_isolate(struct phy_device *phydev, bool enable); int phy_sfp_connect_phy(void *upstream, struct phy_device *phy); void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy); void phy_sfp_attach(void *upstream, struct sfp_bus *bus); @@ -1894,6 +1897,7 @@ int genphy_read_master_slave(struct phy_device *phydev); int genphy_suspend(struct phy_device *phydev); int genphy_resume(struct phy_device *phydev); int genphy_loopback(struct phy_device *phydev, bool enable); +int genphy_isolate(struct phy_device *phydev, bool enable); int genphy_soft_reset(struct phy_device *phydev); irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev);