From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 8974026A0DB for ; Mon, 28 Apr 2025 13:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; cv=none; b=Fxbt7NAUaQG/3Oy/vK4CKGuFpd5cREHFAc+pM8+5tPgr2G1hBYRZeUGb65DPVJnqm1FesvpDm635gJZaAvL7cTiOIT1cpeoS9AO7JHfdm885rhGHK0UGYRKuZGnFOt1IjT0PSgMOyJSx4by2QDilgbK2b/77WUuOkx1Tx9DrWDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; c=relaxed/simple; bh=PfDu6Fp8KyBjovfjyOpuBNf1DbqZ7wUZxTHJtarXAtU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GU+aTvSdmDJUrtdYfJ+U6r1fP+RBhjy1UjU4mS6A4vphTQNzjl+GILrRqqljlHF17doxx4aRVojzJZjTcaVm0ib3rSYoboFF95cNvv1iyIPwmEipGVqKDeZfQLkA6aLCGty5V2WIBHmxyNOzDFvDBR2YcKUlcpU7urztN5Jjj44= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OB7-0000MP-L6; Mon, 28 Apr 2025 15:05:45 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FK-0V; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJ65-0D; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 01/12] net: usb: lan78xx: Improve error handling in PHY initialization Date: Mon, 28 Apr 2025 15:05:31 +0200 Message-Id: <20250428130542.3879769-2-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> 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-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Ensure that return values from `lan78xx_write_reg()`, `lan78xx_read_reg()`, and `phy_find_first()` are properly checked and propagated. Use `ERR_PTR(ret)` for error reporting in `lan7801_phy_init()` and replace `-EIO` with `-ENODEV` where appropriate to provide more accurate error codes. Signed-off-by: Oleksij Rempel --- changes v6: - confirmed with maintainers that fixed_phy_register() leak is acceptable as soon as it is fixed within the patch set changes v5: - make sure lan7801_phy_init() caller is testing against IS_ERR instead of NULL. changes v4: - split the patch and move part of it before PHYlink migration --- drivers/net/usb/lan78xx.c | 47 ++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index e4f1663b6204..19db18cf0504 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -2510,14 +2510,13 @@ static void lan78xx_remove_irq_domain(struct lan78x= x_net *dev) =20 static struct phy_device *lan7801_phy_init(struct lan78xx_net *dev) { - u32 buf; - int ret; struct fixed_phy_status fphy_status =3D { .link =3D 1, .speed =3D SPEED_1000, .duplex =3D DUPLEX_FULL, }; struct phy_device *phydev; + int ret; =20 phydev =3D phy_find_first(dev->mdiobus); if (!phydev) { @@ -2525,30 +2524,40 @@ static struct phy_device *lan7801_phy_init(struct l= an78xx_net *dev) phydev =3D fixed_phy_register(PHY_POLL, &fphy_status, NULL); if (IS_ERR(phydev)) { netdev_err(dev->net, "No PHY/fixed_PHY found\n"); - return NULL; + return ERR_PTR(-ENODEV); } netdev_dbg(dev->net, "Registered FIXED PHY\n"); dev->interface =3D PHY_INTERFACE_MODE_RGMII; ret =3D lan78xx_write_reg(dev, MAC_RGMII_ID, MAC_RGMII_ID_TXC_DELAY_EN_); + if (ret < 0) + return ERR_PTR(ret); + ret =3D lan78xx_write_reg(dev, RGMII_TX_BYP_DLL, 0x3D00); - ret =3D lan78xx_read_reg(dev, HW_CFG, &buf); - buf |=3D HW_CFG_CLK125_EN_; - buf |=3D HW_CFG_REFCLK25_EN_; - ret =3D lan78xx_write_reg(dev, HW_CFG, buf); + if (ret < 0) + return ERR_PTR(ret); + + ret =3D lan78xx_update_reg(dev, HW_CFG, HW_CFG_CLK125_EN_ | + HW_CFG_REFCLK25_EN_, + HW_CFG_CLK125_EN_ | HW_CFG_REFCLK25_EN_); + if (ret < 0) + return ERR_PTR(ret); } else { if (!phydev->drv) { netdev_err(dev->net, "no PHY driver found\n"); - return NULL; + return ERR_PTR(-EINVAL); } dev->interface =3D PHY_INTERFACE_MODE_RGMII_ID; /* The PHY driver is responsible to configure proper RGMII * interface delays. Disable RGMII delays on MAC side. */ - lan78xx_write_reg(dev, MAC_RGMII_ID, 0); + ret =3D lan78xx_write_reg(dev, MAC_RGMII_ID, 0); + if (ret < 0) + return ERR_PTR(ret); =20 phydev->is_internal =3D false; } + return phydev; } =20 @@ -2562,9 +2571,10 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) switch (dev->chipid) { case ID_REV_CHIP_ID_7801_: phydev =3D lan7801_phy_init(dev); - if (!phydev) { - netdev_err(dev->net, "lan7801: PHY Init Failed"); - return -EIO; + if (IS_ERR(phydev)) { + netdev_err(dev->net, "lan7801: failed to init PHY: %pe\n", + phydev); + return PTR_ERR(phydev); } break; =20 @@ -2573,7 +2583,7 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) phydev =3D phy_find_first(dev->mdiobus); if (!phydev) { netdev_err(dev->net, "no PHY found\n"); - return -EIO; + return -ENODEV; } phydev->is_internal =3D true; dev->interface =3D PHY_INTERFACE_MODE_GMII; @@ -2581,7 +2591,7 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) =20 default: netdev_err(dev->net, "Unknown CHIP ID found\n"); - return -EIO; + return -ENODEV; } =20 /* if phyirq is not set, use polling mode in phylib */ @@ -2633,7 +2643,10 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) sizeof(u32)); if (len >=3D 0) { /* Ensure the appropriate LEDs are enabled */ - lan78xx_read_reg(dev, HW_CFG, ®); + ret =3D lan78xx_read_reg(dev, HW_CFG, ®); + if (ret < 0) + return ret; + reg &=3D ~(HW_CFG_LED0_EN_ | HW_CFG_LED1_EN_ | HW_CFG_LED2_EN_ | @@ -2642,7 +2655,9 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) (len > 1) * HW_CFG_LED1_EN_ | (len > 2) * HW_CFG_LED2_EN_ | (len > 3) * HW_CFG_LED3_EN_; - lan78xx_write_reg(dev, HW_CFG, reg); + ret =3D lan78xx_write_reg(dev, HW_CFG, reg); + if (ret < 0) + return ret; } } =20 --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 F1835F9D9 for ; Mon, 28 Apr 2025 13:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; cv=none; b=tG2nfcKtGDF0ylHURPQzP/UKulsw8RPHBXvSuthW8f7NtX1Jq/jZC0/SQT1Yb/AuGN2Cj3KE6tnmPvOnb4Duovvm8WIJrdO48NBDZnMG9MhzLJn9WVVPXrrFu7ggzOkMOVQShqML2qZETWta1uUYNL1EA6rAUujmm9OAp6u4RvQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; c=relaxed/simple; bh=EKrdrdjRyLeoPTwBO7+5pjftSljEtQ9GICzVJzj1620=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pWz3frwL3fOm8iy4i5SuVql5IOPgtvMnVOc92nE/ed/qJ7/rxXb78GQcMFiLb7CQ+V4zedjXDtUHqPBo/U2T6oH7NV96CsPxeBG/J+OLyNmjv6L9ao9Khivg9tvprwTBDx9qWulg5N1zQbdjI5ixwLBuyG5mKzNF8cq+2bHg5W8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OB7-0000MR-L6; Mon, 28 Apr 2025 15:05:45 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FL-0V; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJ6R-0G; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 02/12] net: usb: lan78xx: remove explicit check for missing PHY driver Date: Mon, 28 Apr 2025 15:05:32 +0200 Message-Id: <20250428130542.3879769-3-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> 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-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" RGMII timing correctness relies on the PHY providing internal delays. This is typically ensured via PHY driver, strap pins, or PCB layout. Explicitly checking for a PHY driver here is unnecessary and non-standard. This logic applies to all MACs, not just LAN78xx, and should be left to phylib, phylink, or platform configuration. Drop the check and rely on standard subsystem behavior. Signed-off-by: Oleksij Rempel --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 19db18cf0504..9c0658227bde 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -2543,10 +2543,6 @@ static struct phy_device *lan7801_phy_init(struct la= n78xx_net *dev) if (ret < 0) return ERR_PTR(ret); } else { - if (!phydev->drv) { - netdev_err(dev->net, "no PHY driver found\n"); - return ERR_PTR(-EINVAL); - } dev->interface =3D PHY_INTERFACE_MODE_RGMII_ID; /* The PHY driver is responsible to configure proper RGMII * interface delays. Disable RGMII delays on MAC side. --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 97F3426A0F8 for ; Mon, 28 Apr 2025 13:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; cv=none; b=oXhshZ0h1S8lbOo7tKvOKErtIhPrAwwuoZVg60yWkik2EUtDjDwQfHFc1hfqWHLD8AoE9D8Ryr4/9UZbKkemFdecB8bijdGTgWqN+Q8t2D/sJJZX8G9WWaqSXrOxqdo/VIygaqY3D5JoAeRDcqTfP3FcKvYE6oB1UTPjDyu9kGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; c=relaxed/simple; bh=5URiGYR5zlAEfTrePIZ8K4h++6AoVHBDIfSU+U1wtuI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=BzfX8AH4BVvhoVkPYfmiXb1amOjZtVr+3ByzAXVR12BnFLmOtfhRXSPISwpwtjAcgbLNlkCzW68t55jZ2eeRcu3Ga2vf6h7tO1wH8kNa81fMVAIpF5xH7NxWf9mrdxJCFdQbvO7InmJY5khAqJznb3cbWntJh2UsWSiOZiDumUk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OB7-0000MQ-L7; Mon, 28 Apr 2025 15:05:45 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FM-0Y; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJ7J-0J; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 03/12] net: usb: lan78xx: refactor PHY init to separate detection and MAC configuration Date: Mon, 28 Apr 2025 15:05:33 +0200 Message-Id: <20250428130542.3879769-4-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Split out PHY detection into lan78xx_get_phy() and MAC-side setup into lan78xx_mac_prepare_for_phy(), making the main lan78xx_phy_init() cleaner and easier to follow. This improves separation of concerns and prepares the code for a future transition to phylink. Fixed PHY registration and interface selection are now handled in lan78xx_get_phy(), while MAC-side delay configuration is done in lan78xx_mac_prepare_for_phy(). The fixed PHY fallback is preserved for setups like EVB-KSZ9897-1, where LAN7801 connects directly to a KSZ switch without a standard PHY or device tree support. No functional changes intended. Signed-off-by: Oleksij Rempel --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 174 ++++++++++++++++++++++++++++---------- 1 file changed, 128 insertions(+), 46 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 9c0658227bde..7f1ecc415d53 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -2508,53 +2508,145 @@ static void lan78xx_remove_irq_domain(struct lan78= xx_net *dev) dev->domain_data.irqdomain =3D NULL; } =20 -static struct phy_device *lan7801_phy_init(struct lan78xx_net *dev) +/** + * lan78xx_register_fixed_phy() - Register a fallback fixed PHY + * @dev: LAN78xx device + * + * Registers a fixed PHY with 1 Gbps full duplex. This is used in special = cases + * like EVB-KSZ9897-1, where LAN7801 acts as a USB-to-Ethernet interface t= o a + * switch without a visible PHY. + * + * Return: pointer to the registered fixed PHY, or ERR_PTR() on error. + */ +static struct phy_device *lan78xx_register_fixed_phy(struct lan78xx_net *d= ev) { struct fixed_phy_status fphy_status =3D { .link =3D 1, .speed =3D SPEED_1000, .duplex =3D DUPLEX_FULL, }; + + netdev_info(dev->net, + "No PHY found on LAN7801 =E2=80=93 registering fixed PHY (e.g. EVB-K= SZ9897-1)\n"); + + return fixed_phy_register(PHY_POLL, &fphy_status, NULL); +} + +/** + * lan78xx_get_phy() - Probe or register PHY device and set interface mode + * @dev: LAN78xx device structure + * + * This function attempts to find a PHY on the MDIO bus. If no PHY is found + * and the chip is LAN7801, it registers a fixed PHY as fallback. It also + * sets dev->interface based on chip ID and detected PHY type. + * + * Return: a valid PHY device pointer, or ERR_PTR() on failure. + */ +static struct phy_device *lan78xx_get_phy(struct lan78xx_net *dev) +{ struct phy_device *phydev; - int ret; =20 + /* Attempt to locate a PHY on the MDIO bus */ phydev =3D phy_find_first(dev->mdiobus); - if (!phydev) { - netdev_dbg(dev->net, "PHY Not Found!! Registering Fixed PHY\n"); - phydev =3D fixed_phy_register(PHY_POLL, &fphy_status, NULL); - if (IS_ERR(phydev)) { - netdev_err(dev->net, "No PHY/fixed_PHY found\n"); - return ERR_PTR(-ENODEV); + + switch (dev->chipid) { + case ID_REV_CHIP_ID_7801_: + if (phydev) { + /* External RGMII PHY detected */ + dev->interface =3D PHY_INTERFACE_MODE_RGMII_ID; + phydev->is_internal =3D false; + + if (!phydev->drv) + netdev_warn(dev->net, + "PHY driver not found =E2=80=93 assuming RGMII delays are on PCB = or strapped for the PHY\n"); + + return phydev; } - netdev_dbg(dev->net, "Registered FIXED PHY\n"); + dev->interface =3D PHY_INTERFACE_MODE_RGMII; + /* No PHY found =E2=80=93 fallback to fixed PHY (e.g. KSZ switch board) = */ + return lan78xx_register_fixed_phy(dev); + + case ID_REV_CHIP_ID_7800_: + case ID_REV_CHIP_ID_7850_: + if (!phydev) + return ERR_PTR(-ENODEV); + + /* These use internal GMII-connected PHY */ + dev->interface =3D PHY_INTERFACE_MODE_GMII; + phydev->is_internal =3D true; + return phydev; + + default: + netdev_err(dev->net, "Unknown CHIP ID: 0x%08x\n", dev->chipid); + return ERR_PTR(-ENODEV); + } +} + +/** + * lan78xx_mac_prepare_for_phy() - Preconfigure MAC-side interface settings + * @dev: LAN78xx device + * + * Configure MAC-side registers according to dev->interface, which should = be + * set by lan78xx_get_phy(). + * + * - For PHY_INTERFACE_MODE_RGMII: + * Enable MAC-side TXC delay. This mode seems to be used in a special se= tup + * without a real PHY, likely on EVB-KSZ9897-1. In that design, LAN7801 = is + * connected to the KSZ9897 switch, and the link timing is expected to be + * hardwired (e.g. via strapping or board layout). No devicetree support= is + * assumed here. + * + * - For PHY_INTERFACE_MODE_RGMII_ID: + * Disable MAC-side delay and rely on the PHY driver to provide delay. + * + * - For GMII, no MAC-specific config is needed. + * + * Return: 0 on success or a negative error code. + */ +static int lan78xx_mac_prepare_for_phy(struct lan78xx_net *dev) +{ + int ret; + + switch (dev->interface) { + case PHY_INTERFACE_MODE_RGMII: + /* Enable MAC-side TX clock delay */ ret =3D lan78xx_write_reg(dev, MAC_RGMII_ID, MAC_RGMII_ID_TXC_DELAY_EN_); if (ret < 0) - return ERR_PTR(ret); + return ret; =20 ret =3D lan78xx_write_reg(dev, RGMII_TX_BYP_DLL, 0x3D00); if (ret < 0) - return ERR_PTR(ret); + return ret; =20 - ret =3D lan78xx_update_reg(dev, HW_CFG, HW_CFG_CLK125_EN_ | - HW_CFG_REFCLK25_EN_, + ret =3D lan78xx_update_reg(dev, HW_CFG, + HW_CFG_CLK125_EN_ | HW_CFG_REFCLK25_EN_, HW_CFG_CLK125_EN_ | HW_CFG_REFCLK25_EN_); if (ret < 0) - return ERR_PTR(ret); - } else { - dev->interface =3D PHY_INTERFACE_MODE_RGMII_ID; - /* The PHY driver is responsible to configure proper RGMII - * interface delays. Disable RGMII delays on MAC side. - */ + return ret; + + break; + + case PHY_INTERFACE_MODE_RGMII_ID: + /* Disable MAC-side TXC delay, PHY provides it */ ret =3D lan78xx_write_reg(dev, MAC_RGMII_ID, 0); if (ret < 0) - return ERR_PTR(ret); + return ret; =20 - phydev->is_internal =3D false; + break; + + case PHY_INTERFACE_MODE_GMII: + /* No MAC-specific configuration required */ + break; + + default: + netdev_warn(dev->net, "Unsupported interface mode: %d\n", + dev->interface); + break; } =20 - return phydev; + return 0; } =20 static int lan78xx_phy_init(struct lan78xx_net *dev) @@ -2564,31 +2656,13 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) u32 mii_adv; struct phy_device *phydev; =20 - switch (dev->chipid) { - case ID_REV_CHIP_ID_7801_: - phydev =3D lan7801_phy_init(dev); - if (IS_ERR(phydev)) { - netdev_err(dev->net, "lan7801: failed to init PHY: %pe\n", - phydev); - return PTR_ERR(phydev); - } - break; - - case ID_REV_CHIP_ID_7800_: - case ID_REV_CHIP_ID_7850_: - phydev =3D phy_find_first(dev->mdiobus); - if (!phydev) { - netdev_err(dev->net, "no PHY found\n"); - return -ENODEV; - } - phydev->is_internal =3D true; - dev->interface =3D PHY_INTERFACE_MODE_GMII; - break; + phydev =3D lan78xx_get_phy(dev); + if (IS_ERR(phydev)) + return PTR_ERR(phydev); =20 - default: - netdev_err(dev->net, "Unknown CHIP ID found\n"); - return -ENODEV; - } + ret =3D lan78xx_mac_prepare_for_phy(dev); + if (ret < 0) + goto free_phy; =20 /* if phyirq is not set, use polling mode in phylib */ if (dev->domain_data.phyirq > 0) @@ -2662,6 +2736,14 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) dev->fc_autoneg =3D phydev->autoneg; =20 return 0; + +free_phy: + if (phy_is_pseudo_fixed_link(phydev)) { + fixed_phy_unregister(phydev); + phy_device_free(phydev); + } + + return ret; } =20 static int lan78xx_set_rx_max_frame_length(struct lan78xx_net *dev, int si= ze) --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 4EA54269D18 for ; Mon, 28 Apr 2025 13:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845552; cv=none; b=kGLvxfQbGbhxHXmZE27f4t5BbOj38RpVA9G5WdZkkMsIdwF1PCuoZlwS2y9IQRUUPpT3upI2TccPkA0jCsDuRI5hzuft/OwBqJOK/GLw103zUiHTUU5ghlVvbu73wZyRb7LQZDEGN6nzsKdzklmIWfPv1bOGefAUyUXAYZk06kc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845552; c=relaxed/simple; bh=CywE26rgas3evbSjo68DaIxIX7NCR5wqWdWiofqIa3Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IVLbhWrZAS84dsez1MxOv5tmnORIM0yQpuRIRxNdLxNly6VVFT3JWnGjhNCs5OfRZZy7axWmH3Kc2XpyIzE2ftv1aEGbNWqIKfu+oxFdegMEzUbZ4Tqk5Mz9WzUZcswxbBxB+SqlEbPb02ktyjS+PaTxFworud+FAGY4rtlSFxI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OB7-0000MT-L6; Mon, 28 Apr 2025 15:05:45 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FN-0e; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJ7t-0N; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 04/12] net: usb: lan78xx: move LED DT configuration to helper Date: Mon, 28 Apr 2025 15:05:34 +0200 Message-Id: <20250428130542.3879769-5-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> 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-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Extract the LED enable logic based on the "microchip,led-modes" property into a new helper function lan78xx_configure_leds_from_dt(). This simplifies lan78xx_phy_init() and improves modularity. No functional changes intended. Signed-off-by: Oleksij Rempel --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 72 +++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 7f1ecc415d53..07530eef82cb 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -2649,6 +2649,49 @@ static int lan78xx_mac_prepare_for_phy(struct lan78x= x_net *dev) return 0; } =20 +/** + * lan78xx_configure_leds_from_dt() - Configure LED enables based on DT + * @dev: LAN78xx device + * @phydev: PHY device (must be valid) + * + * Reads "microchip,led-modes" property from the PHY's DT node and enables + * the corresponding number of LEDs by writing to HW_CFG. + * + * This helper preserves the original logic, enabling up to 4 LEDs. + * If the property is not present, this function does nothing. + * + * Return: 0 on success or a negative error code. + */ +static int lan78xx_configure_leds_from_dt(struct lan78xx_net *dev, + struct phy_device *phydev) +{ + struct device_node *np =3D phydev->mdio.dev.of_node; + u32 reg; + int len, ret; + + if (!np) + return 0; + + len =3D of_property_count_elems_of_size(np, "microchip,led-modes", + sizeof(u32)); + if (len < 0) + return 0; + + ret =3D lan78xx_read_reg(dev, HW_CFG, ®); + if (ret < 0) + return ret; + + reg &=3D ~(HW_CFG_LED0_EN_ | HW_CFG_LED1_EN_ | + HW_CFG_LED2_EN_ | HW_CFG_LED3_EN_); + + reg |=3D (len > 0) * HW_CFG_LED0_EN_ | + (len > 1) * HW_CFG_LED1_EN_ | + (len > 2) * HW_CFG_LED2_EN_ | + (len > 3) * HW_CFG_LED3_EN_; + + return lan78xx_write_reg(dev, HW_CFG, reg); +} + static int lan78xx_phy_init(struct lan78xx_net *dev) { __ETHTOOL_DECLARE_LINK_MODE_MASK(fc) =3D { 0, }; @@ -2704,32 +2747,9 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) =20 phy_support_eee(phydev); =20 - if (phydev->mdio.dev.of_node) { - u32 reg; - int len; - - len =3D of_property_count_elems_of_size(phydev->mdio.dev.of_node, - "microchip,led-modes", - sizeof(u32)); - if (len >=3D 0) { - /* Ensure the appropriate LEDs are enabled */ - ret =3D lan78xx_read_reg(dev, HW_CFG, ®); - if (ret < 0) - return ret; - - reg &=3D ~(HW_CFG_LED0_EN_ | - HW_CFG_LED1_EN_ | - HW_CFG_LED2_EN_ | - HW_CFG_LED3_EN_); - reg |=3D (len > 0) * HW_CFG_LED0_EN_ | - (len > 1) * HW_CFG_LED1_EN_ | - (len > 2) * HW_CFG_LED2_EN_ | - (len > 3) * HW_CFG_LED3_EN_; - ret =3D lan78xx_write_reg(dev, HW_CFG, reg); - if (ret < 0) - return ret; - } - } + ret =3D lan78xx_configure_leds_from_dt(dev, phydev); + if (ret) + goto free_phy; =20 genphy_config_aneg(phydev); =20 --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 9A78026A1A3 for ; Mon, 28 Apr 2025 13:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; cv=none; b=sUCQcXgMTrlGO9ubhcT+CvzeXXDWgx/h+PfWXro3/1cQ6Q9TO77CJQYLSpy614qxQvpbNIr20tMDH38Q1svBJrp8cBvELnAPLbaYxVn5Cc9PIkqRrkKqGXTdsviztSfu9LDFTRxzQCbShn2aAWt7b08fmzZoSiPwCefysKFJ0Cw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; c=relaxed/simple; bh=czQrwqSOCU4ro8+9cqoU25PAcGWoBynUwIf9q024yyM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YgbubdtvK4k20AJoLqHzWusW2HnGya8R6nt86ivWSp40ar8AUrJFz+miNMh7eL5VwDJ9kS1c/fH6U5v4MSIdBjLJytGIVBpAbjyuwASlnxuNGQdmqpGIxhalnJ2oMzFUlXoenWWM/jNqCRloMhdF8cRXJdrahXNdLUklnAn+xTs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OB7-0000MX-L6; Mon, 28 Apr 2025 15:05:45 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FO-19; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJ8e-0Q; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 05/12] net: usb: lan78xx: Extract PHY interrupt acknowledgment to helper Date: Mon, 28 Apr 2025 15:05:35 +0200 Message-Id: <20250428130542.3879769-6-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> 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-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the PHY interrupt acknowledgment logic from lan78xx_link_reset() to a new helper function lan78xx_phy_int_ack(). This simplifies the code and prepares for reusing the acknowledgment logic independently from the full link reset process, such as when using phylink. No functional change intended. Signed-off-by: Oleksij Rempel --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 07530eef82cb..de2b429e906e 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -1636,6 +1636,20 @@ static int lan78xx_mac_reset(struct lan78xx_net *dev) return ret; } =20 +/** + * lan78xx_phy_int_ack - Acknowledge PHY interrupt + * @dev: pointer to the LAN78xx device structure + * + * This function acknowledges the PHY interrupt by setting the + * INT_STS_PHY_INT_ bit in the interrupt status register (INT_STS). + * + * Return: 0 on success or a negative error code on failure. + */ +static int lan78xx_phy_int_ack(struct lan78xx_net *dev) +{ + return lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_); +} + static int lan78xx_link_reset(struct lan78xx_net *dev) { struct phy_device *phydev =3D dev->net->phydev; @@ -1644,7 +1658,7 @@ static int lan78xx_link_reset(struct lan78xx_net *dev) u32 buf; =20 /* clear LAN78xx interrupt status */ - ret =3D lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_); + ret =3D lan78xx_phy_int_ack(dev); if (unlikely(ret < 0)) return ret; =20 --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 4EAC026A0C5 for ; Mon, 28 Apr 2025 13:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; cv=none; b=M7GQc3lhFvOQEEbt7j5hVYoXg9xRBflMsn2kl3HfVpkiIqArUm6DcPsUSH6CNQqECja90Fohsdg3c6efUrMDqQnhPJ5g/VCA1kFKZ6x15fS4KT4eIpfYPpZIhW+3B9iymxk87pGfkR5GZK43DpG3m8pir1dlNddRRdQxM4kfXkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; c=relaxed/simple; bh=yDBPrVSL+UwTdx3j7DqwG9mfpeNf/ZqzrDQtnxyuIIs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BjlJKBwcR7q/28eWIukCUuP0teXIrV4ULv/Z0GCMuAl0nt04+pW1sQfMWJJp+kUFCfGhfZSimdZ+AYO87hzAMNXiE4tiqBZwoZHyRUXacm00sx+VW9rx9Tqt3OMnQjHQPWcxEi9l38NOr6neJg53+SDKfm0MBITwqXbi2a36hw8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OB7-0000MS-L7; Mon, 28 Apr 2025 15:05:45 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FQ-0n; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJ8z-0U; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 06/12] net: usb: lan78xx: Refactor USB link power configuration into helper Date: Mon, 28 Apr 2025 15:05:36 +0200 Message-Id: <20250428130542.3879769-7-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> 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-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the USB link power configuration logic from lan78xx_link_reset() to a new helper function lan78xx_configure_usb(). This simplifies the main link reset path and isolates USB-specific logic. The new function handles U1/U2 enablement based on Ethernet link speed, but only for SuperSpeed-capable devices (LAN7800 and LAN7801). LAN7850, a High-Speed-only device, is explicitly excluded. A warning is logged if SuperSpeed is reported unexpectedly for LAN7850. Add a forward declaration for lan78xx_configure_usb() as preparation for the upcoming phylink conversion, where it will also be used from the mac_link_up() callback. Open questions remain: - Why is the 1000 Mbps configuration split into two steps (U2 disable, then U1 enable), unlike the single-step config used for 10/100 Mbps? - U1/U2 behavior appears to depend on proper EEPROM configuration. There are known devices in the field without EEPROM. Should the driver enforce safe defaults in such cases? Due to lack of USB subsystem expertise, no changes were made to this logic beyond structural refactoring. Signed-off-by: Oleksij Rempel --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 90 +++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index de2b429e906e..bff53324c70a 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -1650,12 +1650,13 @@ static int lan78xx_phy_int_ack(struct lan78xx_net *= dev) return lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_); } =20 +static int lan78xx_configure_usb(struct lan78xx_net *dev, int speed); + static int lan78xx_link_reset(struct lan78xx_net *dev) { struct phy_device *phydev =3D dev->net->phydev; struct ethtool_link_ksettings ecmd; int ladv, radv, ret, link; - u32 buf; =20 /* clear LAN78xx interrupt status */ ret =3D lan78xx_phy_int_ack(dev); @@ -1681,36 +1682,9 @@ static int lan78xx_link_reset(struct lan78xx_net *de= v) =20 phy_ethtool_ksettings_get(phydev, &ecmd); =20 - if (dev->udev->speed =3D=3D USB_SPEED_SUPER) { - if (ecmd.base.speed =3D=3D 1000) { - /* disable U2 */ - ret =3D lan78xx_read_reg(dev, USB_CFG1, &buf); - if (ret < 0) - return ret; - buf &=3D ~USB_CFG1_DEV_U2_INIT_EN_; - ret =3D lan78xx_write_reg(dev, USB_CFG1, buf); - if (ret < 0) - return ret; - /* enable U1 */ - ret =3D lan78xx_read_reg(dev, USB_CFG1, &buf); - if (ret < 0) - return ret; - buf |=3D USB_CFG1_DEV_U1_INIT_EN_; - ret =3D lan78xx_write_reg(dev, USB_CFG1, buf); - if (ret < 0) - return ret; - } else { - /* enable U1 & U2 */ - ret =3D lan78xx_read_reg(dev, USB_CFG1, &buf); - if (ret < 0) - return ret; - buf |=3D USB_CFG1_DEV_U2_INIT_EN_; - buf |=3D USB_CFG1_DEV_U1_INIT_EN_; - ret =3D lan78xx_write_reg(dev, USB_CFG1, buf); - if (ret < 0) - return ret; - } - } + ret =3D lan78xx_configure_usb(dev, ecmd.base.speed); + if (ret < 0) + return ret; =20 ladv =3D phy_read(phydev, MII_ADVERTISE); if (ladv < 0) @@ -2522,6 +2496,60 @@ static void lan78xx_remove_irq_domain(struct lan78xx= _net *dev) dev->domain_data.irqdomain =3D NULL; } =20 +/** + * lan78xx_configure_usb - Configure USB link power settings + * @dev: pointer to the LAN78xx device structure + * @speed: negotiated Ethernet link speed (in Mbps) + * + * This function configures U1/U2 link power management for SuperSpeed + * USB devices based on the current Ethernet link speed. It uses the + * USB_CFG1 register to enable or disable U1 and U2 low-power states. + * + * Note: Only LAN7800 and LAN7801 support SuperSpeed (USB 3.x). + * LAN7850 is a High-Speed-only (USB 2.0) device and is skipped. + * + * Return: 0 on success or a negative error code on failure. + */ +static int lan78xx_configure_usb(struct lan78xx_net *dev, int speed) +{ + u32 mask, val; + int ret; + + /* Only configure USB settings for SuperSpeed devices */ + if (dev->udev->speed !=3D USB_SPEED_SUPER) + return 0; + + /* LAN7850 does not support USB 3.x */ + if (dev->chipid =3D=3D ID_REV_CHIP_ID_7850_) { + netdev_warn_once(dev->net, "Unexpected SuperSpeed for LAN7850 (USB 2.0 o= nly)\n"); + return 0; + } + + switch (speed) { + case SPEED_1000: + /* Disable U2, enable U1 */ + ret =3D lan78xx_update_reg(dev, USB_CFG1, + USB_CFG1_DEV_U2_INIT_EN_, 0); + if (ret < 0) + return ret; + + return lan78xx_update_reg(dev, USB_CFG1, + USB_CFG1_DEV_U1_INIT_EN_, + USB_CFG1_DEV_U1_INIT_EN_); + + case SPEED_100: + case SPEED_10: + /* Enable both U1 and U2 */ + mask =3D USB_CFG1_DEV_U1_INIT_EN_ | USB_CFG1_DEV_U2_INIT_EN_; + val =3D mask; + return lan78xx_update_reg(dev, USB_CFG1, mask, val); + + default: + netdev_warn(dev->net, "Unsupported link speed: %d\n", speed); + return -EINVAL; + } +} + /** * lan78xx_register_fixed_phy() - Register a fallback fixed PHY * @dev: LAN78xx device --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 E095C262FD8 for ; Mon, 28 Apr 2025 13:05:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845552; cv=none; b=rd52ymv69u69dB4v9aROWAV7nrFHmzDZmsJNgeORJ2GA+cOGz/MNfDdPCHKvYB0aVt+tLQ0DSzQnlSkFaV6f8VIT/8fH/eXuM6bvD5f/BK+oplnWOPHaA4YtM0uBkCegTAG+LywX5quYHOCEB3PVBeRI2ZS1iy+fv4RseXGcVCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845552; c=relaxed/simple; bh=Ym/VO1lsf4cxLe2zwu26BSqCE0/7Tphe1tRXLpCeV9Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=dcA8kt2VWUUyTVaX85FEEIqE5ZKuD70gMeI/9/4ZC1IxxXVr91qSUhGmryDCjUEWfXWPvSZRSqeJG0jTgpDS+pECgOxC0gdnqzDPOsxEfCpRAjXm83U/hZRsG64Bay7CSVLeZxFr0zK7M5SxaDSp31MgcaCs1WGSCqSGmPbs8Yg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OB7-0000MV-L7; Mon, 28 Apr 2025 15:05:45 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FR-19; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJ9Z-0X; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 07/12] net: usb: lan78xx: Extract flow control configuration to helper Date: Mon, 28 Apr 2025 15:05:37 +0200 Message-Id: <20250428130542.3879769-8-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Move flow control register configuration from lan78xx_update_flowcontrol() into a new helper function lan78xx_configure_flowcontrol(). This separates hardware-specific programming from policy logic and simplifies the upcoming phylink integration. The values used in this initial version of lan78xx_configure_flowcontrol() are taken over as-is from the original implementation to avoid functional changes. While they may not be optimal for all USB and link speed combinations, they are known to work reliably. Optimization of pause time and thresholds based on runtime conditions can be done in a separate follow-up patch. The forward declaration of lan78xx_configure_flowcontrol() will also be removed later during the phylink conversion. Signed-off-by: Oleksij Rempel --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 105 +++++++++++++++++++++++++++++++------- 1 file changed, 87 insertions(+), 18 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index bff53324c70a..58e3589e3b89 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -1554,10 +1554,12 @@ static void lan78xx_set_multicast(struct net_device= *netdev) schedule_work(&pdata->set_multicast); } =20 +static int lan78xx_configure_flowcontrol(struct lan78xx_net *dev, + bool tx_pause, bool rx_pause); + static int lan78xx_update_flowcontrol(struct lan78xx_net *dev, u8 duplex, u16 lcladv, u16 rmtadv) { - u32 flow =3D 0, fct_flow =3D 0; u8 cap; =20 if (dev->fc_autoneg) @@ -1565,27 +1567,13 @@ static int lan78xx_update_flowcontrol(struct lan78x= x_net *dev, u8 duplex, else cap =3D dev->fc_request_control; =20 - if (cap & FLOW_CTRL_TX) - flow |=3D (FLOW_CR_TX_FCEN_ | 0xFFFF); - - if (cap & FLOW_CTRL_RX) - flow |=3D FLOW_CR_RX_FCEN_; - - if (dev->udev->speed =3D=3D USB_SPEED_SUPER) - fct_flow =3D FLOW_CTRL_THRESHOLD(FLOW_ON_SS, FLOW_OFF_SS); - else if (dev->udev->speed =3D=3D USB_SPEED_HIGH) - fct_flow =3D FLOW_CTRL_THRESHOLD(FLOW_ON_HS, FLOW_OFF_HS); - netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s", (cap & FLOW_CTRL_RX ? "enabled" : "disabled"), (cap & FLOW_CTRL_TX ? "enabled" : "disabled")); =20 - lan78xx_write_reg(dev, FCT_FLOW, fct_flow); - - /* threshold value should be set before enabling flow */ - lan78xx_write_reg(dev, FLOW, flow); - - return 0; + return lan78xx_configure_flowcontrol(dev, + cap & FLOW_CTRL_TX, + cap & FLOW_CTRL_RX); } =20 static void lan78xx_rx_urb_submit_all(struct lan78xx_net *dev); @@ -2550,6 +2538,87 @@ static int lan78xx_configure_usb(struct lan78xx_net = *dev, int speed) } } =20 +/** + * lan78xx_configure_flowcontrol - Set MAC and FIFO flow control configura= tion + * @dev: pointer to the LAN78xx device structure + * @tx_pause: enable transmission of pause frames + * @rx_pause: enable reception of pause frames + * + * This function configures the LAN78xx flow control settings by writing + * to the FLOW and FCT_FLOW registers. The pause time is set to the + * maximum allowed value (65535 quanta). FIFO thresholds are selected + * based on USB speed. + * + * The Pause Time field is measured in units of 512-bit times (quanta): + * - At 1 Gbps: 1 quanta =3D 512 ns =E2=86=92 max ~33.6 ms pause + * - At 100 Mbps: 1 quanta =3D 5.12 =C2=B5s =E2=86=92 max ~335 ms pause + * - At 10 Mbps: 1 quanta =3D 51.2 =C2=B5s =E2=86=92 max ~3.3 s pause + * + * Flow control thresholds (FCT_FLOW) are used to trigger pause/resume: + * - RXUSED is the number of bytes used in the RX FIFO + * - Flow is turned ON when RXUSED =E2=89=A5 FLOW_ON threshold + * - Flow is turned OFF when RXUSED =E2=89=A4 FLOW_OFF threshold + * - Both thresholds are encoded in units of 512 bytes (rounded up) + * + * Thresholds differ by USB speed because available USB bandwidth + * affects how fast packets can be drained from the RX FIFO: + * - USB 3.x (SuperSpeed): + * FLOW_ON =3D 9216 bytes =E2=86=92 18 units + * FLOW_OFF =3D 4096 bytes =E2=86=92 8 units + * - USB 2.0 (High-Speed): + * FLOW_ON =3D 8704 bytes =E2=86=92 17 units + * FLOW_OFF =3D 1024 bytes =E2=86=92 2 units + * + * Note: The FCT_FLOW register must be configured before enabling TX pause + * (i.e., before setting FLOW_CR_TX_FCEN_), as required by the hardw= are. + * + * Return: 0 on success or a negative error code on failure. + */ +static int lan78xx_configure_flowcontrol(struct lan78xx_net *dev, + bool tx_pause, bool rx_pause) +{ + /* Use maximum pause time: 65535 quanta (512-bit times) */ + const u32 pause_time_quanta =3D 65535; + u32 fct_flow =3D 0; + u32 flow =3D 0; + int ret; + + /* Prepare MAC flow control bits */ + if (tx_pause) + flow |=3D FLOW_CR_TX_FCEN_ | pause_time_quanta; + + if (rx_pause) + flow |=3D FLOW_CR_RX_FCEN_; + + /* Select RX FIFO thresholds based on USB speed + * + * FCT_FLOW layout: + * bits [6:0] FLOW_ON threshold (RXUSED =E2=89=A5 ON =E2=86=92 assert= pause) + * bits [14:8] FLOW_OFF threshold (RXUSED =E2=89=A4 OFF =E2=86=92 deas= sert pause) + * thresholds are expressed in units of 512 bytes + */ + switch (dev->udev->speed) { + case USB_SPEED_SUPER: + fct_flow =3D FLOW_CTRL_THRESHOLD(FLOW_ON_SS, FLOW_OFF_SS); + break; + case USB_SPEED_HIGH: + fct_flow =3D FLOW_CTRL_THRESHOLD(FLOW_ON_HS, FLOW_OFF_HS); + break; + default: + netdev_warn(dev->net, "Unsupported USB speed: %d\n", + dev->udev->speed); + return -EINVAL; + } + + /* Step 1: Write FIFO thresholds before enabling pause frames */ + ret =3D lan78xx_write_reg(dev, FCT_FLOW, fct_flow); + if (ret < 0) + return ret; + + /* Step 2: Enable MAC pause functionality */ + return lan78xx_write_reg(dev, FLOW, flow); +} + /** * lan78xx_register_fixed_phy() - Register a fallback fixed PHY * @dev: LAN78xx device --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 16DA626981E for ; Mon, 28 Apr 2025 13:05:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; cv=none; b=Ee7brPUs4qrwT9Jhfp4w9OVR+M5v1/e5apCgjeXcM12ZsmYvrE1/3rG2W7v20I6et+zbNvveTJkn9cpa7hF+4xrSLT8sDunqRhjU4xKYfhZnqCnc9V/LbTC1xOdq59UWMWKxGMdizIPacb+35PL37E0HLRx1clH9mea5IWXeD0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845553; c=relaxed/simple; bh=tsk34V8fkBxAfHdz4MTKjubcqHkvDaXi1ZkPRd/Kg98=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=JXCQ7iEQfu6kTVGb80cR6dWizqDwZgakYzmUjfgfCu7PWPjpk/UfEzUzCz7B6VQt7ZgFgsFo/q2cdoZPyCaBruN7b9b3OfiC8uuUgD0AgrRUbkV14u/iVm0+b49RAjMMGRdNmr9yGuv3pSb3p1W3mrUpulRfHAoXyXS6b6Jy904= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OB7-0000MY-L6; Mon, 28 Apr 2025 15:05:45 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FU-1D; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJ9s-0b; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 08/12] net: usb: lan78xx: Convert to PHYLINK for improved PHY and MAC management Date: Mon, 28 Apr 2025 15:05:38 +0200 Message-Id: <20250428130542.3879769-9-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Convert the LAN78xx USB Ethernet driver to use the PHYLINK framework for managing PHY and MAC interactions. This improves consistency with other network drivers, simplifies pause frame handling, and enables cleaner suspend/resume support. Key changes: - Replace all PHYLIB-based logic with PHYLINK equivalents: - Replace phy_connect()/phy_disconnect() with phylink_connect_phy() - Replace phy_start()/phy_stop() with phylink_start()/phylink_stop() - Replace pauseparam handling with phylink_ethtool_get/set_pauseparam() - Introduce lan78xx_phylink_setup() to configure PHYLINK - Add phylink MAC operations: - lan78xx_mac_config() - lan78xx_mac_link_up() - lan78xx_mac_link_down() - Remove legacy link state handling: - lan78xx_link_status_change() - lan78xx_link_reset() - Handle fixed-link fallback for LAN7801 using phylink_set_fixed_link() - Replace deprecated flow control handling with phylink-managed logic Power management: - Switch suspend/resume paths to use phylink_suspend()/phylink_resume() - Ensure proper use of rtnl_lock() where required - Note: full runtime testing of power management is currently limited due to hardware setup constraints Note: Conversion of EEE (Energy Efficient Ethernet) handling to the PHYLINK-managed API will be done in a follow-up patch. For now, the legacy EEE enable logic is preserved in mac_link_up(). Signed-off-by: Oleksij Rempel --- changes v6: - move functional interface changes and refactoring to separate patches - switch suspend/resume to use phylink_suspend/resume with correct locking - avoid setting phydev->interface manually; rely on phylink negotiation - remove legacy pause/aneg setup and dead code (e.g. lan78xx_link_status_ch= ange) - EEE conversion postponed to follow-up patch - note that power management testing is limited due to HW constraints changes v5: - merge ethtool pause interface changes to this patch changes v4: - add PHYLINK dependency - remove PHYLIB and FIXED_PHY, both are replaced by PHYLINK changes v3: - lan78xx_phy_init: drop phy_suspend() - lan78xx_phylink_setup: use phy_interface_set_rgmii() changes v2: - lan78xx_mac_config: remove unused rgmii_id - lan78xx_mac_config: PHY_INTERFACE_MODE_RGMII* variants - lan78xx_mac_config: remove auto-speed and duplex configuration - lan78xx_phylink_setup: set link_interface to PHY_INTERFACE_MODE_RGMII_ID instead of PHY_INTERFACE_MODE_NA. - lan78xx_phy_init: use phylink_set_fixed_link() instead of allocating fixed PHY. - lan78xx_configure_usb: move function values to separate variables 20220427_lukas_polling_be_gone_on_lan95xx.cover --- drivers/net/usb/Kconfig | 3 +- drivers/net/usb/lan78xx.c | 551 ++++++++++++++++++-------------------- 2 files changed, 258 insertions(+), 296 deletions(-) diff --git a/drivers/net/usb/Kconfig b/drivers/net/usb/Kconfig index 3c360d4f0635..71168e47a9b1 100644 --- a/drivers/net/usb/Kconfig +++ b/drivers/net/usb/Kconfig @@ -115,9 +115,8 @@ config USB_RTL8152 config USB_LAN78XX tristate "Microchip LAN78XX Based USB Ethernet Adapters" select MII - select PHYLIB + select PHYLINK select MICROCHIP_PHY - select FIXED_PHY select CRC32 help This option adds support for Microchip LAN78XX based USB 2 diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 58e3589e3b89..bbc218def781 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -384,7 +385,7 @@ struct skb_data { /* skb->cb is one of these */ #define EVENT_RX_HALT 1 #define EVENT_RX_MEMORY 2 #define EVENT_STS_SPLIT 3 -#define EVENT_LINK_RESET 4 +#define EVENT_PHY_INT_ACK 4 #define EVENT_RX_PAUSED 5 #define EVENT_DEV_WAKING 6 #define EVENT_DEV_ASLEEP 7 @@ -455,7 +456,6 @@ struct lan78xx_net { =20 unsigned long data[5]; =20 - int link_on; u8 mdix_ctrl; =20 u32 chipid; @@ -463,13 +463,13 @@ struct lan78xx_net { struct mii_bus *mdiobus; phy_interface_t interface; =20 - int fc_autoneg; - u8 fc_request_control; - int delta; struct statstage stats; =20 struct irq_domain_data domain_data; + + struct phylink *phylink; + struct phylink_config phylink_config; }; =20 /* use ethtool to change the level for any given device */ @@ -1554,28 +1554,6 @@ static void lan78xx_set_multicast(struct net_device = *netdev) schedule_work(&pdata->set_multicast); } =20 -static int lan78xx_configure_flowcontrol(struct lan78xx_net *dev, - bool tx_pause, bool rx_pause); - -static int lan78xx_update_flowcontrol(struct lan78xx_net *dev, u8 duplex, - u16 lcladv, u16 rmtadv) -{ - u8 cap; - - if (dev->fc_autoneg) - cap =3D mii_resolve_flowctrl_fdx(lcladv, rmtadv); - else - cap =3D dev->fc_request_control; - - netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s", - (cap & FLOW_CTRL_RX ? "enabled" : "disabled"), - (cap & FLOW_CTRL_TX ? "enabled" : "disabled")); - - return lan78xx_configure_flowcontrol(dev, - cap & FLOW_CTRL_TX, - cap & FLOW_CTRL_RX); -} - static void lan78xx_rx_urb_submit_all(struct lan78xx_net *dev); =20 static int lan78xx_mac_reset(struct lan78xx_net *dev) @@ -1638,75 +1616,6 @@ static int lan78xx_phy_int_ack(struct lan78xx_net *d= ev) return lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_); } =20 -static int lan78xx_configure_usb(struct lan78xx_net *dev, int speed); - -static int lan78xx_link_reset(struct lan78xx_net *dev) -{ - struct phy_device *phydev =3D dev->net->phydev; - struct ethtool_link_ksettings ecmd; - int ladv, radv, ret, link; - - /* clear LAN78xx interrupt status */ - ret =3D lan78xx_phy_int_ack(dev); - if (unlikely(ret < 0)) - return ret; - - mutex_lock(&phydev->lock); - phy_read_status(phydev); - link =3D phydev->link; - mutex_unlock(&phydev->lock); - - if (!link && dev->link_on) { - dev->link_on =3D false; - - /* reset MAC */ - ret =3D lan78xx_mac_reset(dev); - if (ret < 0) - return ret; - - timer_delete(&dev->stat_monitor); - } else if (link && !dev->link_on) { - dev->link_on =3D true; - - phy_ethtool_ksettings_get(phydev, &ecmd); - - ret =3D lan78xx_configure_usb(dev, ecmd.base.speed); - if (ret < 0) - return ret; - - ladv =3D phy_read(phydev, MII_ADVERTISE); - if (ladv < 0) - return ladv; - - radv =3D phy_read(phydev, MII_LPA); - if (radv < 0) - return radv; - - netif_dbg(dev, link, dev->net, - "speed: %u duplex: %d anadv: 0x%04x anlpa: 0x%04x", - ecmd.base.speed, ecmd.base.duplex, ladv, radv); - - ret =3D lan78xx_update_flowcontrol(dev, ecmd.base.duplex, ladv, - radv); - if (ret < 0) - return ret; - - if (!timer_pending(&dev->stat_monitor)) { - dev->delta =3D 1; - mod_timer(&dev->stat_monitor, - jiffies + STAT_UPDATE_TIMER); - } - - lan78xx_rx_urb_submit_all(dev); - - local_bh_disable(); - napi_schedule(&dev->napi); - local_bh_enable(); - } - - return 0; -} - /* some work can't be done in tasklets, so we use keventd * * NOTE: annoying asymmetry: if it's active, schedule_work() fails, @@ -1733,7 +1642,7 @@ static void lan78xx_status(struct lan78xx_net *dev, s= truct urb *urb) =20 if (intdata & INT_ENP_PHY_INT) { netif_dbg(dev, link, dev->net, "PHY INTR: 0x%08x\n", intdata); - lan78xx_defer_kevent(dev, EVENT_LINK_RESET); + lan78xx_defer_kevent(dev, EVENT_PHY_INT_ACK); =20 if (dev->domain_data.phyirq > 0) generic_handle_irq_safe(dev->domain_data.phyirq); @@ -2015,63 +1924,16 @@ static void lan78xx_get_pause(struct net_device *ne= t, struct ethtool_pauseparam *pause) { struct lan78xx_net *dev =3D netdev_priv(net); - struct phy_device *phydev =3D net->phydev; - struct ethtool_link_ksettings ecmd; - - phy_ethtool_ksettings_get(phydev, &ecmd); - - pause->autoneg =3D dev->fc_autoneg; - - if (dev->fc_request_control & FLOW_CTRL_TX) - pause->tx_pause =3D 1; =20 - if (dev->fc_request_control & FLOW_CTRL_RX) - pause->rx_pause =3D 1; + phylink_ethtool_get_pauseparam(dev->phylink, pause); } =20 static int lan78xx_set_pause(struct net_device *net, struct ethtool_pauseparam *pause) { struct lan78xx_net *dev =3D netdev_priv(net); - struct phy_device *phydev =3D net->phydev; - struct ethtool_link_ksettings ecmd; - int ret; - - phy_ethtool_ksettings_get(phydev, &ecmd); - - if (pause->autoneg && !ecmd.base.autoneg) { - ret =3D -EINVAL; - goto exit; - } - - dev->fc_request_control =3D 0; - if (pause->rx_pause) - dev->fc_request_control |=3D FLOW_CTRL_RX; - - if (pause->tx_pause) - dev->fc_request_control |=3D FLOW_CTRL_TX; - - if (ecmd.base.autoneg) { - __ETHTOOL_DECLARE_LINK_MODE_MASK(fc) =3D { 0, }; - u32 mii_adv; - - linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, - ecmd.link_modes.advertising); - linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, - ecmd.link_modes.advertising); - mii_adv =3D (u32)mii_advertise_flowctrl(dev->fc_request_control); - mii_adv_to_linkmode_adv_t(fc, mii_adv); - linkmode_or(ecmd.link_modes.advertising, fc, - ecmd.link_modes.advertising); =20 - phy_ethtool_ksettings_set(phydev, &ecmd); - } - - dev->fc_autoneg =3D pause->autoneg; - - ret =3D 0; -exit: - return ret; + return phylink_ethtool_set_pauseparam(dev->phylink, pause); } =20 static int lan78xx_get_regs_len(struct net_device *netdev) @@ -2332,26 +2194,6 @@ static void lan78xx_remove_mdio(struct lan78xx_net *= dev) mdiobus_free(dev->mdiobus); } =20 -static void lan78xx_link_status_change(struct net_device *net) -{ - struct lan78xx_net *dev =3D netdev_priv(net); - struct phy_device *phydev =3D net->phydev; - u32 data; - int ret; - - ret =3D lan78xx_read_reg(dev, MAC_CR, &data); - if (ret < 0) - return; - - if (phydev->enable_tx_lpi) - data |=3D MAC_CR_EEE_EN_; - else - data &=3D ~MAC_CR_EEE_EN_; - lan78xx_write_reg(dev, MAC_CR, data); - - phy_print_status(phydev); -} - static int irq_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hwirq) { @@ -2484,6 +2326,75 @@ static void lan78xx_remove_irq_domain(struct lan78xx= _net *dev) dev->domain_data.irqdomain =3D NULL; } =20 +static void lan78xx_mac_config(struct phylink_config *config, unsigned int= mode, + const struct phylink_link_state *state) +{ + struct net_device *net =3D to_net_dev(config->dev); + struct lan78xx_net *dev =3D netdev_priv(net); + u32 mac_cr =3D 0; + int ret; + + /* Check if the mode is supported */ + if (mode !=3D MLO_AN_FIXED && mode !=3D MLO_AN_PHY) { + netdev_err(net, "Unsupported negotiation mode: %u\n", mode); + return; + } + + switch (state->interface) { + case PHY_INTERFACE_MODE_GMII: + mac_cr |=3D MAC_CR_GMII_EN_; + break; + case PHY_INTERFACE_MODE_RGMII: + case PHY_INTERFACE_MODE_RGMII_ID: + case PHY_INTERFACE_MODE_RGMII_TXID: + case PHY_INTERFACE_MODE_RGMII_RXID: + break; + default: + netdev_warn(net, "Unsupported interface mode: %d\n", + state->interface); + return; + } + + ret =3D lan78xx_update_reg(dev, MAC_CR, MAC_CR_GMII_EN_, mac_cr); + if (ret < 0) + netdev_err(net, "Failed to config MAC with error %pe\n", + ERR_PTR(ret)); +} + +static void lan78xx_mac_link_down(struct phylink_config *config, + unsigned int mode, phy_interface_t interface) +{ + struct net_device *net =3D to_net_dev(config->dev); + struct lan78xx_net *dev =3D netdev_priv(net); + int ret; + + /* MAC reset will not de-assert TXEN/RXEN, we need to stop them + * manually before reset. TX and RX should be disabled before running + * link_up sequence. + */ + ret =3D lan78xx_stop_tx_path(dev); + if (ret < 0) + goto link_down_fail; + + ret =3D lan78xx_stop_rx_path(dev); + if (ret < 0) + goto link_down_fail; + + /* MAC reset seems to not affect MAC configuration, no idea if it is + * really needed, but it was done in previous driver version. So, leave + * it here. + */ + ret =3D lan78xx_mac_reset(dev); + if (ret < 0) + goto link_down_fail; + + return; + +link_down_fail: + netdev_err(dev->net, "Failed to set MAC down with error %pe\n", + ERR_PTR(ret)); +} + /** * lan78xx_configure_usb - Configure USB link power settings * @dev: pointer to the LAN78xx device structure @@ -2619,28 +2530,100 @@ static int lan78xx_configure_flowcontrol(struct la= n78xx_net *dev, return lan78xx_write_reg(dev, FLOW, flow); } =20 +static void lan78xx_mac_link_up(struct phylink_config *config, + struct phy_device *phy, + unsigned int mode, phy_interface_t interface, + int speed, int duplex, + bool tx_pause, bool rx_pause) +{ + struct net_device *net =3D to_net_dev(config->dev); + struct lan78xx_net *dev =3D netdev_priv(net); + u32 mac_cr =3D 0; + int ret; + + switch (speed) { + case SPEED_1000: + mac_cr |=3D MAC_CR_SPEED_1000_; + break; + case SPEED_100: + mac_cr |=3D MAC_CR_SPEED_100_; + break; + case SPEED_10: + mac_cr |=3D MAC_CR_SPEED_10_; + break; + default: + netdev_err(dev->net, "Unsupported speed %d\n", speed); + return; + } + + if (duplex =3D=3D DUPLEX_FULL) + mac_cr |=3D MAC_CR_FULL_DUPLEX_; + + /* make sure TXEN and RXEN are disabled before reconfiguring MAC */ + ret =3D lan78xx_update_reg(dev, MAC_CR, MAC_CR_SPEED_MASK_ | + MAC_CR_FULL_DUPLEX_ | MAC_CR_EEE_EN_, mac_cr); + if (ret < 0) + goto link_up_fail; + + ret =3D lan78xx_configure_flowcontrol(dev, tx_pause, rx_pause); + if (ret < 0) + goto link_up_fail; + + ret =3D lan78xx_configure_usb(dev, speed); + if (ret < 0) + goto link_up_fail; + + lan78xx_rx_urb_submit_all(dev); + + ret =3D lan78xx_flush_rx_fifo(dev); + if (ret < 0) + goto link_up_fail; + + ret =3D lan78xx_flush_tx_fifo(dev); + if (ret < 0) + goto link_up_fail; + + ret =3D lan78xx_start_tx_path(dev); + if (ret < 0) + goto link_up_fail; + + ret =3D lan78xx_start_rx_path(dev); + if (ret < 0) + goto link_up_fail; + + return; +link_up_fail: + netdev_err(dev->net, "Failed to set MAC up with error %pe\n", + ERR_PTR(ret)); +} + +static const struct phylink_mac_ops lan78xx_phylink_mac_ops =3D { + .mac_config =3D lan78xx_mac_config, + .mac_link_down =3D lan78xx_mac_link_down, + .mac_link_up =3D lan78xx_mac_link_up, +}; + /** - * lan78xx_register_fixed_phy() - Register a fallback fixed PHY + * lan78xx_set_fixed_link() - Set fixed link configuration for LAN7801 * @dev: LAN78xx device * - * Registers a fixed PHY with 1 Gbps full duplex. This is used in special = cases - * like EVB-KSZ9897-1, where LAN7801 acts as a USB-to-Ethernet interface t= o a - * switch without a visible PHY. + * Use fixed link configuration with 1 Gbps full duplex. This is used in s= pecial + * cases like EVB-KSZ9897-1, where LAN7801 acts as a USB-to-Ethernet inter= face + * to a switch without a visible PHY. * * Return: pointer to the registered fixed PHY, or ERR_PTR() on error. */ -static struct phy_device *lan78xx_register_fixed_phy(struct lan78xx_net *d= ev) +static int lan78xx_set_fixed_link(struct lan78xx_net *dev) { - struct fixed_phy_status fphy_status =3D { - .link =3D 1, + struct phylink_link_state state =3D { .speed =3D SPEED_1000, .duplex =3D DUPLEX_FULL, }; =20 netdev_info(dev->net, - "No PHY found on LAN7801 =E2=80=93 registering fixed PHY (e.g. EVB-K= SZ9897-1)\n"); + "No PHY found on LAN7801 =E2=80=93 using fixed link instead (e.g. EV= B-KSZ9897-1)\n"); =20 - return fixed_phy_register(PHY_POLL, &fphy_status, NULL); + return phylink_set_fixed_link(dev->phylink, &state); } =20 /** @@ -2676,7 +2659,7 @@ static struct phy_device *lan78xx_get_phy(struct lan7= 8xx_net *dev) =20 dev->interface =3D PHY_INTERFACE_MODE_RGMII; /* No PHY found =E2=80=93 fallback to fixed PHY (e.g. KSZ switch board) = */ - return lan78xx_register_fixed_phy(dev); + return NULL; =20 case ID_REV_CHIP_ID_7800_: case ID_REV_CHIP_ID_7850_: @@ -2803,20 +2786,63 @@ static int lan78xx_configure_leds_from_dt(struct la= n78xx_net *dev, return lan78xx_write_reg(dev, HW_CFG, reg); } =20 +static int lan78xx_phylink_setup(struct lan78xx_net *dev) +{ + struct phylink_config *pc =3D &dev->phylink_config; + struct phylink *phylink; + + pc->dev =3D &dev->net->dev; + pc->type =3D PHYLINK_NETDEV; + pc->mac_capabilities =3D MAC_SYM_PAUSE | MAC_ASYM_PAUSE | MAC_10 | + MAC_100 | MAC_1000FD; + pc->mac_managed_pm =3D true; + + if (dev->chipid =3D=3D ID_REV_CHIP_ID_7801_) + phy_interface_set_rgmii(pc->supported_interfaces); + else + __set_bit(PHY_INTERFACE_MODE_GMII, pc->supported_interfaces); + + phylink =3D phylink_create(pc, dev->net->dev.fwnode, + dev->interface, &lan78xx_phylink_mac_ops); + if (IS_ERR(phylink)) + return PTR_ERR(phylink); + + dev->phylink =3D phylink; + + return 0; +} + static int lan78xx_phy_init(struct lan78xx_net *dev) { - __ETHTOOL_DECLARE_LINK_MODE_MASK(fc) =3D { 0, }; - int ret; - u32 mii_adv; struct phy_device *phydev; + int ret; =20 phydev =3D lan78xx_get_phy(dev); + /* phydev can be NULL if no PHY is found and the chip is LAN7801, + * which will use a fixed link later. + * If an error occurs, return the error code immediately. + */ if (IS_ERR(phydev)) return PTR_ERR(phydev); =20 + ret =3D lan78xx_phylink_setup(dev); + if (ret < 0) + return ret; + + /* If no PHY is found, set up a fixed link. It is very specific to + * the LAN7801 and is used in special cases like EVB-KSZ9897-1 where + * LAN7801 acts as a USB-to-Ethernet interface to a switch without + * a visible PHY. + */ + if (!phydev) { + ret =3D lan78xx_set_fixed_link(dev); + if (ret < 0) + return ret; + } + ret =3D lan78xx_mac_prepare_for_phy(dev); if (ret < 0) - goto free_phy; + return ret; =20 /* if phyirq is not set, use polling mode in phylib */ if (dev->domain_data.phyirq > 0) @@ -2825,56 +2851,25 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) phydev->irq =3D PHY_POLL; netdev_dbg(dev->net, "phydev->irq =3D %d\n", phydev->irq); =20 - /* set to AUTOMDIX */ - phydev->mdix =3D ETH_TP_MDI_AUTO; - - ret =3D phy_connect_direct(dev->net, phydev, - lan78xx_link_status_change, - dev->interface); + ret =3D phylink_connect_phy(dev->phylink, phydev); if (ret) { - netdev_err(dev->net, "can't attach PHY to %s\n", - dev->mdiobus->id); - if (dev->chipid =3D=3D ID_REV_CHIP_ID_7801_) { - if (phy_is_pseudo_fixed_link(phydev)) { - fixed_phy_unregister(phydev); - phy_device_free(phydev); - } - } - return -EIO; + netdev_err(dev->net, "can't attach PHY to %s, error %pe\n", + dev->mdiobus->id, ERR_PTR(ret)); + return ret; } =20 - /* MAC doesn't support 1000T Half */ - phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT); - - /* support both flow controls */ - dev->fc_request_control =3D (FLOW_CTRL_RX | FLOW_CTRL_TX); - linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, - phydev->advertising); - linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, - phydev->advertising); - mii_adv =3D (u32)mii_advertise_flowctrl(dev->fc_request_control); - mii_adv_to_linkmode_adv_t(fc, mii_adv); - linkmode_or(phydev->advertising, fc, phydev->advertising); - phy_support_eee(phydev); =20 - ret =3D lan78xx_configure_leds_from_dt(dev, phydev); - if (ret) - goto free_phy; - - genphy_config_aneg(phydev); - - dev->fc_autoneg =3D phydev->autoneg; - - return 0; + return lan78xx_configure_leds_from_dt(dev, phydev); +} =20 -free_phy: - if (phy_is_pseudo_fixed_link(phydev)) { - fixed_phy_unregister(phydev); - phy_device_free(phydev); +static void lan78xx_phy_uninit(struct lan78xx_net *dev) +{ + if (dev->phylink) { + phylink_disconnect_phy(dev->phylink); + phylink_destroy(dev->phylink); + dev->phylink =3D NULL; } - - return ret; } =20 static int lan78xx_set_rx_max_frame_length(struct lan78xx_net *dev, int si= ze) @@ -3213,7 +3208,6 @@ static int lan78xx_reset(struct lan78xx_net *dev) unsigned long timeout; int ret; u32 buf; - u8 sig; =20 ret =3D lan78xx_read_reg(dev, HW_CFG, &buf); if (ret < 0) @@ -3370,22 +3364,12 @@ static int lan78xx_reset(struct lan78xx_net *dev) if (ret < 0) return ret; =20 + buf &=3D ~(MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_); + /* LAN7801 only has RGMII mode */ - if (dev->chipid =3D=3D ID_REV_CHIP_ID_7801_) { + if (dev->chipid =3D=3D ID_REV_CHIP_ID_7801_) buf &=3D ~MAC_CR_GMII_EN_; - /* Enable Auto Duplex and Auto speed */ - buf |=3D MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_; - } =20 - if (dev->chipid =3D=3D ID_REV_CHIP_ID_7800_ || - dev->chipid =3D=3D ID_REV_CHIP_ID_7850_) { - ret =3D lan78xx_read_raw_eeprom(dev, 0, 1, &sig); - if (!ret && sig !=3D EEPROM_INDICATOR) { - /* Implies there is no external eeprom. Set mac speed */ - netdev_info(dev->net, "No External EEPROM. Setting MAC Speed\n"); - buf |=3D MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_; - } - } ret =3D lan78xx_write_reg(dev, MAC_CR, buf); if (ret < 0) return ret; @@ -3435,9 +3419,11 @@ static int lan78xx_open(struct net_device *net) =20 mutex_lock(&dev->dev_mutex); =20 - phy_start(net->phydev); + lan78xx_init_stats(dev); + + napi_enable(&dev->napi); =20 - netif_dbg(dev, ifup, dev->net, "phy initialised successfully"); + set_bit(EVENT_DEV_OPEN, &dev->flags); =20 /* for Link Check */ if (dev->urb_intr) { @@ -3449,31 +3435,9 @@ static int lan78xx_open(struct net_device *net) } } =20 - ret =3D lan78xx_flush_rx_fifo(dev); - if (ret < 0) - goto done; - ret =3D lan78xx_flush_tx_fifo(dev); - if (ret < 0) - goto done; - - ret =3D lan78xx_start_tx_path(dev); - if (ret < 0) - goto done; - ret =3D lan78xx_start_rx_path(dev); - if (ret < 0) - goto done; - - lan78xx_init_stats(dev); - - set_bit(EVENT_DEV_OPEN, &dev->flags); + phylink_start(dev->phylink); =20 netif_start_queue(net); - - dev->link_on =3D false; - - napi_enable(&dev->napi); - - lan78xx_defer_kevent(dev, EVENT_LINK_RESET); done: mutex_unlock(&dev->dev_mutex); =20 @@ -3541,12 +3505,7 @@ static int lan78xx_stop(struct net_device *net) net->stats.rx_packets, net->stats.tx_packets, net->stats.rx_errors, net->stats.tx_errors); =20 - /* ignore errors that occur stopping the Tx and Rx data paths */ - lan78xx_stop_tx_path(dev); - lan78xx_stop_rx_path(dev); - - if (net->phydev) - phy_stop(net->phydev); + phylink_stop(dev->phylink); =20 usb_kill_urb(dev->urb_intr); =20 @@ -3556,7 +3515,7 @@ static int lan78xx_stop(struct net_device *net) */ clear_bit(EVENT_TX_HALT, &dev->flags); clear_bit(EVENT_RX_HALT, &dev->flags); - clear_bit(EVENT_LINK_RESET, &dev->flags); + clear_bit(EVENT_PHY_INT_ACK, &dev->flags); clear_bit(EVENT_STAT_UPDATE, &dev->flags); =20 cancel_delayed_work_sync(&dev->wq); @@ -4480,14 +4439,14 @@ static void lan78xx_delayedwork(struct work_struct = *work) } } =20 - if (test_bit(EVENT_LINK_RESET, &dev->flags)) { + if (test_bit(EVENT_PHY_INT_ACK, &dev->flags)) { int ret =3D 0; =20 - clear_bit(EVENT_LINK_RESET, &dev->flags); - if (lan78xx_link_reset(dev) < 0) { - netdev_info(dev->net, "link reset failed (%d)\n", - ret); - } + clear_bit(EVENT_PHY_INT_ACK, &dev->flags); + ret =3D lan78xx_phy_int_ack(dev); + if (ret) + netdev_info(dev->net, "PHY INT ack failed (%pe)\n", + ERR_PTR(ret)); } =20 if (test_bit(EVENT_STAT_UPDATE, &dev->flags)) { @@ -4561,32 +4520,29 @@ static void lan78xx_disconnect(struct usb_interface= *intf) struct lan78xx_net *dev; struct usb_device *udev; struct net_device *net; - struct phy_device *phydev; =20 dev =3D usb_get_intfdata(intf); usb_set_intfdata(intf, NULL); if (!dev) return; =20 - netif_napi_del(&dev->napi); - udev =3D interface_to_usbdev(intf); net =3D dev->net; =20 + rtnl_lock(); + phylink_stop(dev->phylink); + phylink_disconnect_phy(dev->phylink); + rtnl_unlock(); + + netif_napi_del(&dev->napi); + unregister_netdev(net); =20 timer_shutdown_sync(&dev->stat_monitor); set_bit(EVENT_DEV_DISCONNECT, &dev->flags); cancel_delayed_work_sync(&dev->wq); =20 - phydev =3D net->phydev; - - phy_disconnect(net->phydev); - - if (phy_is_pseudo_fixed_link(phydev)) { - fixed_phy_unregister(phydev); - phy_device_free(phydev); - } + phylink_destroy(dev->phylink); =20 usb_scuttle_anchored_urbs(&dev->deferred); =20 @@ -4670,7 +4626,6 @@ static int lan78xx_probe(struct usb_interface *intf, goto out1; } =20 - /* netdev_printk() needs this */ SET_NETDEV_DEV(netdev, &intf->dev); =20 dev =3D netdev_priv(netdev); @@ -4784,12 +4739,12 @@ static int lan78xx_probe(struct usb_interface *intf, =20 ret =3D lan78xx_phy_init(dev); if (ret < 0) - goto free_urbs; + goto phy_uninit; =20 ret =3D register_netdev(netdev); if (ret !=3D 0) { netif_err(dev, probe, netdev, "couldn't register the device\n"); - goto out8; + goto phy_uninit; } =20 usb_set_intfdata(intf, dev); @@ -4804,8 +4759,8 @@ static int lan78xx_probe(struct usb_interface *intf, =20 return 0; =20 -out8: - phy_disconnect(netdev->phydev); +phy_uninit: + lan78xx_phy_uninit(dev); free_urbs: usb_free_urb(dev->urb_intr); out5: @@ -5140,6 +5095,10 @@ static int lan78xx_suspend(struct usb_interface *int= f, pm_message_t message) spin_unlock_irq(&dev->txq.lock); } =20 + rtnl_lock(); + phylink_suspend(dev->phylink, false); + rtnl_unlock(); + /* stop RX */ ret =3D lan78xx_stop_rx_path(dev); if (ret < 0) @@ -5367,11 +5326,15 @@ static int lan78xx_reset_resume(struct usb_interfac= e *intf) if (ret < 0) return ret; =20 - phy_start(dev->net->phydev); - ret =3D lan78xx_resume(intf); + if (ret < 0) + return ret; =20 - return ret; + rtnl_lock(); + phylink_resume(dev->phylink); + rtnl_unlock(); + + return 0; } =20 static const struct usb_device_id products[] =3D { --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 1B41426FA6C for ; Mon, 28 Apr 2025 13:05:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845558; cv=none; b=GDgIDqVv4WYxWXb71aCo276q2+VNJ7Knm97JxMRiZdleZ2SEbDd3X0dFMAPwVtdABwQNozNFaAAOVSTLCo3GaYriv3iDPpxwUYf3udXyxiYu5iwnD7jGcQwC2Da6653X0251eLkYBj2qFjc34tutFLy2IMj1iMHMGoUqC7rdfzg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845558; c=relaxed/simple; bh=4D/z67H6fuxxF+uknzRTrvKwWcaBfvedbwgiJKGi73k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bAiCqP8azj4+uMbQVXVX4nnLRODRq8yJoJ+iiNdGigNEpP/+VefdA6KAY+B5jCCDJFIlvk6UyzfcwAj/lKrvY9eSNvPojvpzAG6slWOZmquw9qng54Ox0uUXFGnzWAbTRqoxdyfrgYQDWLHhSbMSOPEI5OhyKVenxjwJsczV75g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OBB-0000Ma-Oo; Mon, 28 Apr 2025 15:05:49 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FX-1V; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJAT-0e; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , Maxime Chevallier , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Simon Horman Subject: [PATCH net-next v7 09/12] net: usb: lan78xx: Use ethtool_op_get_link to reflect current link status Date: Mon, 28 Apr 2025 15:05:39 +0200 Message-Id: <20250428130542.3879769-10-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> 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-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Replace the custom lan78xx_get_link implementation with the standard ethtool_op_get_link helper, which uses netif_carrier_ok to reflect the current link status accurately. Signed-off-by: Oleksij Rempel Reviewed-by: Maxime Chevallier --- drivers/net/usb/lan78xx.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index bbc218def781..f10925b2bcaa 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -1839,18 +1839,6 @@ static int lan78xx_set_eee(struct net_device *net, s= truct ethtool_keee *edata) return ret; } =20 -static u32 lan78xx_get_link(struct net_device *net) -{ - u32 link; - - mutex_lock(&net->phydev->lock); - phy_read_status(net->phydev); - link =3D net->phydev->link; - mutex_unlock(&net->phydev->lock); - - return link; -} - static void lan78xx_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info) { @@ -1970,7 +1958,7 @@ lan78xx_get_regs(struct net_device *netdev, struct et= htool_regs *regs, } =20 static const struct ethtool_ops lan78xx_ethtool_ops =3D { - .get_link =3D lan78xx_get_link, + .get_link =3D ethtool_op_get_link, .nway_reset =3D phy_ethtool_nway_reset, .get_drvinfo =3D lan78xx_get_drvinfo, .get_msglevel =3D lan78xx_get_msglevel, --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 68CA52550C9 for ; Mon, 28 Apr 2025 13:05:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845554; cv=none; b=faA9z7ZsED2zzDtcUqtscwnyiYFGQElwuNcI+DD7CUn3SM1kdXTGa0m2jKT7MAc6miXxVbAf0sQ1CVHjZZHG1Xsqchqz6x+NhnGr+m+Tfbc5qm/i2LQTKEJ5fMI0L34krN8OmcN8TtA6BkJKCmSEjADktR5/vf1IWgYzyR1oMt0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845554; c=relaxed/simple; bh=IEPdzM0ySBeqSfWvOtA1uHLO5erXtGheeDeYpWDl8IU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GuRfi2cO1cLH33UQQPptcw4dB4ybKCuhWWgHaP2bwEjjbAMyK+UUrxmNvyEP3xw4ti/w/JJxlkvmF1sCzWc/d4TQWhOcFfbVBbjgD4iaBMK+xeFlD4rcDstbehMnWRC4FKncEKaAvpDBSwyXiKJGHSlbbI6Y1Jha9777nFWNXps= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OB7-0000MU-L7; Mon, 28 Apr 2025 15:05:45 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006FZ-15; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJAu-0h; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , Maxime Chevallier , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Simon Horman Subject: [PATCH net-next v7 10/12] net: usb: lan78xx: port link settings to phylink API Date: Mon, 28 Apr 2025 15:05:40 +0200 Message-Id: <20250428130542.3879769-11-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> 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-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Refactor lan78xx_get_link_ksettings and lan78xx_set_link_ksettings to use the phylink API (phylink_ethtool_ksettings_get and phylink_ethtool_ksettings_set) instead of directly interfacing with the PHY. This change simplifies the code and ensures better integration with the phylink framework for link management. Additionally, the explicit calls to usb_autopm_get_interface() and usb_autopm_put_interface() have been removed. These were originally needed to manage USB power management during register accesses. However, lan78xx_mdiobus_read() and lan78xx_mdiobus_write() already handle USB auto power management internally, ensuring that the interface remains active when necessary. Since there are no other direct register accesses in these functions that require explicit power management handling, the extra calls have become redundant and are no longer needed. Signed-off-by: Oleksij Rempel Reviewed-by: Maxime Chevallier --- changes v4: - add explanation why we do not care about usb_autopm in this functions --- drivers/net/usb/lan78xx.c | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index f10925b2bcaa..45e9158655b7 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -1866,46 +1866,16 @@ static int lan78xx_get_link_ksettings(struct net_de= vice *net, struct ethtool_link_ksettings *cmd) { struct lan78xx_net *dev =3D netdev_priv(net); - struct phy_device *phydev =3D net->phydev; - int ret; - - ret =3D usb_autopm_get_interface(dev->intf); - if (ret < 0) - return ret; =20 - phy_ethtool_ksettings_get(phydev, cmd); - - usb_autopm_put_interface(dev->intf); - - return ret; + return phylink_ethtool_ksettings_get(dev->phylink, cmd); } =20 static int lan78xx_set_link_ksettings(struct net_device *net, const struct ethtool_link_ksettings *cmd) { struct lan78xx_net *dev =3D netdev_priv(net); - struct phy_device *phydev =3D net->phydev; - int ret =3D 0; - int temp; - - ret =3D usb_autopm_get_interface(dev->intf); - if (ret < 0) - return ret; - - /* change speed & duplex */ - ret =3D phy_ethtool_ksettings_set(phydev, cmd); =20 - if (!cmd->base.autoneg) { - /* force link down */ - temp =3D phy_read(phydev, MII_BMCR); - phy_write(phydev, MII_BMCR, temp | BMCR_LOOPBACK); - mdelay(1); - phy_write(phydev, MII_BMCR, temp); - } - - usb_autopm_put_interface(dev->intf); - - return ret; + return phylink_ethtool_ksettings_set(dev->phylink, cmd); } =20 static void lan78xx_get_pause(struct net_device *net, --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 F2C7326FA5E for ; Mon, 28 Apr 2025 13:05:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845558; cv=none; b=ETY5oekk++2Xo/YmlKqfpg56NDiPJiLZnmKBvJSdNnq5tYZzlCwsWkEEGmQFkohX0LvQrJU9VwRNtdKSc/F19F5Ll5dhKAY4BZ79F8oVTHGrGFSzKn4J6GlEU3bssSNp9bdNTbT9Y9L3LgJmOnYkMnQ0WWTsXVjF2S6o+dl74sc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845558; c=relaxed/simple; bh=m3CV5y+75OrkXs29P7nZ6ztqK94G8aYhLYyrBKdBEpg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=dqox+rWuWWPNUmHVJYp+IM6k08kLw8Ic1hVvZ4XF1Axn2EfKELDDtvqHvZOpDjbjX1b/C4I9dxjkugBVrkvFpfa37VrBZhReGbPfMkC2mmACBXzj/pWEaHXMIGmu3MAqMUPJ2cH+azEFdpGhBFrXgRZ1NSxHFbhfrgUwwbbDF9Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OBB-0000MW-NZ; Mon, 28 Apr 2025 15:05:49 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006Fd-1C; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJB8-0k; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 11/12] net: usb: lan78xx: Integrate EEE support with phylink LPI API Date: Mon, 28 Apr 2025 15:05:41 +0200 Message-Id: <20250428130542.3879769-12-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Refactor Energy-Efficient Ethernet (EEE) support in the LAN78xx driver to fully integrate with the phylink Low Power Idle (LPI) API. This includes: - Replacing direct calls to `phy_ethtool_get_eee` and `phy_ethtool_set_eee` with `phylink_ethtool_get_eee` and `phylink_ethtool_set_eee`. - Implementing `.mac_enable_tx_lpi` and `.mac_disable_tx_lpi` to control LPI transitions via phylink. - Configuring `lpi_timer_default` to align with recommended values from LAN7800 documentation. - ensure EEE is disabled on controller reset Signed-off-by: Oleksij Rempel --- changes v6: - clarify in lan78xx_mac_eee_enable() comment that MAC_CR_EEE_EN can be modified without disabling TX/RX. I can't recall where the requirement to disable TX/RX came from; may have confused it with nearby MAC_CR bits that require this kind of configuration changes v5: - remove redundant error prints changes v2: - use latest PHYlink TX_LPI API --- drivers/net/usb/lan78xx.c | 123 ++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 44 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 45e9158655b7..ba6a6cda779e 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -1789,54 +1789,15 @@ static int lan78xx_set_wol(struct net_device *netde= v, static int lan78xx_get_eee(struct net_device *net, struct ethtool_keee *ed= ata) { struct lan78xx_net *dev =3D netdev_priv(net); - struct phy_device *phydev =3D net->phydev; - int ret; - u32 buf; - - ret =3D usb_autopm_get_interface(dev->intf); - if (ret < 0) - return ret; - - ret =3D phy_ethtool_get_eee(phydev, edata); - if (ret < 0) - goto exit; =20 - ret =3D lan78xx_read_reg(dev, MAC_CR, &buf); - if (buf & MAC_CR_EEE_EN_) { - /* EEE_TX_LPI_REQ_DLY & tx_lpi_timer are same uSec unit */ - ret =3D lan78xx_read_reg(dev, EEE_TX_LPI_REQ_DLY, &buf); - edata->tx_lpi_timer =3D buf; - } else { - edata->tx_lpi_timer =3D 0; - } - - ret =3D 0; -exit: - usb_autopm_put_interface(dev->intf); - - return ret; + return phylink_ethtool_get_eee(dev->phylink, edata); } =20 static int lan78xx_set_eee(struct net_device *net, struct ethtool_keee *ed= ata) { struct lan78xx_net *dev =3D netdev_priv(net); - int ret; - u32 buf; - - ret =3D usb_autopm_get_interface(dev->intf); - if (ret < 0) - return ret; =20 - ret =3D phy_ethtool_set_eee(net->phydev, edata); - if (ret < 0) - goto out; - - buf =3D (u32)edata->tx_lpi_timer; - ret =3D lan78xx_write_reg(dev, EEE_TX_LPI_REQ_DLY, buf); -out: - usb_autopm_put_interface(dev->intf); - - return ret; + return phylink_ethtool_set_eee(dev->phylink, edata); } =20 static void lan78xx_get_drvinfo(struct net_device *net, @@ -2555,10 +2516,62 @@ static void lan78xx_mac_link_up(struct phylink_conf= ig *config, ERR_PTR(ret)); } =20 +/** + * lan78xx_mac_eee_enable - Enable or disable MAC-side EEE support + * @dev: LAN78xx device + * @enable: true to enable EEE, false to disable + * + * This function sets or clears the MAC_CR_EEE_EN_ bit to control Energy + * Efficient Ethernet (EEE) operation. According to current understanding + * of the LAN7800 documentation, this bit can be modified while TX and RX + * are enabled. No explicit requirement was found to disable data paths + * before changing this bit. + * + * Return: 0 on success or a negative error code + */ +static int lan78xx_mac_eee_enable(struct lan78xx_net *dev, bool enable) +{ + u32 mac_cr =3D 0; + + if (enable) + mac_cr |=3D MAC_CR_EEE_EN_; + + return lan78xx_update_reg(dev, MAC_CR, MAC_CR_EEE_EN_, mac_cr); +} + +static void lan78xx_mac_disable_tx_lpi(struct phylink_config *config) +{ + struct net_device *net =3D to_net_dev(config->dev); + struct lan78xx_net *dev =3D netdev_priv(net); + + lan78xx_mac_eee_enable(dev, false); +} + +static int lan78xx_mac_enable_tx_lpi(struct phylink_config *config, u32 ti= mer, + bool tx_clk_stop) +{ + struct net_device *net =3D to_net_dev(config->dev); + struct lan78xx_net *dev =3D netdev_priv(net); + int ret; + + /* Software should only change this field when Energy Efficient + * Ethernet Enable (EEEEN) is cleared. We ensure that by clearing + * EEEEN during probe, and phylink itself guarantees that + * mac_disable_tx_lpi() will have been previously called. + */ + ret =3D lan78xx_write_reg(dev, EEE_TX_LPI_REQ_DLY, timer); + if (ret < 0) + return ret; + + return lan78xx_mac_eee_enable(dev, true); +} + static const struct phylink_mac_ops lan78xx_phylink_mac_ops =3D { .mac_config =3D lan78xx_mac_config, .mac_link_down =3D lan78xx_mac_link_down, .mac_link_up =3D lan78xx_mac_link_up, + .mac_disable_tx_lpi =3D lan78xx_mac_disable_tx_lpi, + .mac_enable_tx_lpi =3D lan78xx_mac_enable_tx_lpi, }; =20 /** @@ -2754,12 +2767,36 @@ static int lan78xx_phylink_setup(struct lan78xx_net= *dev) pc->mac_capabilities =3D MAC_SYM_PAUSE | MAC_ASYM_PAUSE | MAC_10 | MAC_100 | MAC_1000FD; pc->mac_managed_pm =3D true; + pc->lpi_capabilities =3D MAC_100FD | MAC_1000FD; + /* + * Default TX LPI (Low Power Idle) request delay count is set to 50us. + * + * Source: LAN7800 Documentation, DS00001992H, Section 15.1.57, Page 204. + * + * Reasoning: + * According to the application note in the LAN7800 documentation, a + * zero delay may negatively impact the TX data path=E2=80=99s ability to + * support Gigabit operation. A value of 50us is recommended as a + * reasonable default when the part operates at Gigabit speeds, + * balancing stability and power efficiency in EEE mode. This delay can + * be increased based on performance testing, as EEE is designed for + * scenarios with mostly idle links and occasional bursts of full + * bandwidth transmission. The goal is to ensure reliable Gigabit + * performance without overly aggressive power optimization during + * inactive periods. + */ + pc->lpi_timer_default =3D 50; + pc->eee_enabled_default =3D true; =20 if (dev->chipid =3D=3D ID_REV_CHIP_ID_7801_) phy_interface_set_rgmii(pc->supported_interfaces); else __set_bit(PHY_INTERFACE_MODE_GMII, pc->supported_interfaces); =20 + memcpy(dev->phylink_config.lpi_interfaces, + dev->phylink_config.supported_interfaces, + sizeof(dev->phylink_config.lpi_interfaces)); + phylink =3D phylink_create(pc, dev->net->dev.fwnode, dev->interface, &lan78xx_phylink_mac_ops); if (IS_ERR(phylink)) @@ -2816,8 +2853,6 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) return ret; } =20 - phy_support_eee(phydev); - return lan78xx_configure_leds_from_dt(dev, phydev); } =20 @@ -3322,7 +3357,7 @@ static int lan78xx_reset(struct lan78xx_net *dev) if (ret < 0) return ret; =20 - buf &=3D ~(MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_); + buf &=3D ~(MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_ | MAC_CR_EEE_EN_); =20 /* LAN7801 only has RGMII mode */ if (dev->chipid =3D=3D ID_REV_CHIP_ID_7801_) --=20 2.39.5 From nobody Mon Feb 9 10:57:03 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 8890026F44C for ; Mon, 28 Apr 2025 13:05:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845557; cv=none; b=Jf+scKRirAFwEKeqvn/fpEtc/4dckaD3hbPj8sB9VRrwkcTv11gY8eMQPKSqHvy5WpMBGQB+VL+5qe5A4tuZPCEYbEqoREt1K+5S9tHmWZDAyKgDrTtTQVhNuVEgV/gK6W0eSO1225FgqT+rqMEIuF3w24DRTwC0LHJ6ffwXGmM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745845557; c=relaxed/simple; bh=p5K3m/2awqH/wsJhZKbELlqT5m3wW53cDzS4wBXBhoc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jRa2UGbKctt6kkhosK/xsuRMD3FA+j4N5fozcomERm0doX8Blxi5YlEH1s7kDWxkVRg6KCV0AN7VwYWoyL8VguJlteaChEF8QOiJdCUlsC539R6ppsgWXzKhIOo4Vp+IslEwQleYG7r43Th6C4FlDpKwm7ZKBpbTjroNJpMBWoA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1u9OBB-0000MZ-Oh; Mon, 28 Apr 2025 15:05:49 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9OB6-0006Fe-1S; Mon, 28 Apr 2025 15:05:44 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1u9OB6-00GJBg-0o; Mon, 28 Apr 2025 15:05:44 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v7 12/12] net: usb: lan78xx: remove unused struct members Date: Mon, 28 Apr 2025 15:05:42 +0200 Message-Id: <20250428130542.3879769-13-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428130542.3879769-1-o.rempel@pengutronix.de> References: <20250428130542.3879769-1-o.rempel@pengutronix.de> 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-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Remove unused members from struct lan78xx_net, including: driver_priv suspend_count mdix_ctrl These fields are no longer used in the driver and can be safely removed as part of a cleanup. Signed-off-by: Oleksij Rempel --- changes v6: - drop only those fields not already removed in previous patches - align patch structure with review feedback from Russell King --- drivers/net/usb/lan78xx.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index ba6a6cda779e..647ab9d66334 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -414,7 +414,6 @@ struct lan78xx_net { struct net_device *net; struct usb_device *udev; struct usb_interface *intf; - void *driver_priv; =20 unsigned int tx_pend_data_len; size_t n_tx_urbs; @@ -449,15 +448,12 @@ struct lan78xx_net { unsigned long flags; =20 wait_queue_head_t *wait; - unsigned char suspend_count; =20 unsigned int maxpacket; struct timer_list stat_monitor; =20 unsigned long data[5]; =20 - u8 mdix_ctrl; - u32 chipid; u32 chiprev; struct mii_bus *mdiobus; --=20 2.39.5