Netdev List
 help / color / mirror / Atom feed
From: Muhammed Sariyildiz <asiyee994@gmail.com>
To: netdev@vger.kernel.org
Cc: hkallweit1@gmail.com, nic_swsd@realtek.com,
	andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com,
	Muhammed Sariyildiz <asiyee994@gmail.com>
Subject: [PATCH net-next v2] r8169: handle PHY read errors in RTL8168H/RTL8117 setup
Date: Sun,  6 Dec 2026 17:03:01 +0300	[thread overview]
Message-ID: <20261206140301.13740-1-asiyee994@gmail.com> (raw)

phy_read_paged() returns a negative errno when the PHY register access
fails. A few chip setup paths use the return value as register data
without checking for an error first.

In rtl_hw_start_8168h_1() and rtl_hw_start_8117(), a failed SAW counter
read (-ETIMEDOUT) is masked with 0x3fff and yields a bogus non-zero
count, which is then used to program the 1ms timer in the MAC OCP
register. In rtl8168h_2_hw_phy_config(), a failed TX LPF read is
treated as a valid low-nibble value and used to program another PHY
register.

Treat a failed SAW counter read as a zero count so the existing check
skips the update, and skip the TX LPF adjustment when its read fails.
Both are optional tuning steps, so leaving the defaults in place is
safe.

The issue was found through manual source code inspection.
The error path has not been triggered at runtime, so this
remains a theoretical error-handling issue.

The change was build-tested, but has not been runtime-tested.
No hardware testing was performed. The system has a Realtek
RTL8168H/8111H (PCI ID 10ec:8168, XID 541), but the patched
driver was not run on it.

Signed-off-by: Muhammed Sariyildiz <asiyee994@gmail.com>
---

v2:
  - Target net-next as requested by Andrew Lunn.
  - Drop Fixes tags as requested.
  - Document discovery and testing status.

v1: https://lore.kernel.org/netdev/20261205175625.24461-1-asiyee994@gmail.com/

 drivers/net/ethernet/realtek/r8169_main.c       | 12 +++++++++---
 drivers/net/ethernet/realtek/r8169_phy_config.c | 17 ++++++++++-------
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index 1da64c6e39e2..b4600d2a6ac8 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -3905,11 +3905,14 @@ static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
 
 	rtl_pcie_state_l2l3_disable(tp);
 
-	rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff;
+	rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13);
+	if (rg_saw_cnt < 0)
+		rg_saw_cnt = 0;
+	rg_saw_cnt &= 0x3fff;
 	if (rg_saw_cnt > 0) {
 		u16 sw_cnt_1ms_ini;
 
-		sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
+		sw_cnt_1ms_ini = 16000000 / rg_saw_cnt;
 		sw_cnt_1ms_ini &= 0x0fff;
 		r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
 	}
@@ -4043,7 +4046,10 @@ static void rtl_hw_start_8117(struct rtl8169_private *tp)
 	rtl_pcie_state_l2l3_disable(tp);
 
 	if (tp->phydev) {
-		rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff;
+		rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13);
+		if (rg_saw_cnt < 0)
+			rg_saw_cnt = 0;
+		rg_saw_cnt &= 0x3fff;
 		if (rg_saw_cnt > 0) {
 			u16 sw_cnt_1ms_ini;
 
diff --git a/drivers/net/ethernet/realtek/r8169_phy_config.c b/drivers/net/ethernet/realtek/r8169_phy_config.c
index 4f1d447136ec..896773c24113 100644
--- a/drivers/net/ethernet/realtek/r8169_phy_config.c
+++ b/drivers/net/ethernet/realtek/r8169_phy_config.c
@@ -798,6 +798,7 @@ static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp,
 {
 	u16 ioffset, rlen;
 	u32 data;
+	int ret;
 
 	r8169_apply_firmware(tp);
 
@@ -815,13 +816,15 @@ static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp,
 		phy_write_paged(phydev, 0x0bcf, 0x16, ioffset);
 
 	/* Modify rlen (TX LPF corner frequency) level */
-	data = phy_read_paged(phydev, 0x0bcd, 0x16);
-	data &= 0x000f;
-	rlen = 0;
-	if (data > 3)
-		rlen = data - 3;
-	data = rlen | (rlen << 4) | (rlen << 8) | (rlen << 12);
-	phy_write_paged(phydev, 0x0bcd, 0x17, data);
+	ret = phy_read_paged(phydev, 0x0bcd, 0x16);
+	if (ret >= 0) {
+		data = ret & 0x000f;
+		rlen = 0;
+		if (data > 3)
+			rlen = data - 3;
+		data = rlen | (rlen << 4) | (rlen << 8) | (rlen << 12);
+		phy_write_paged(phydev, 0x0bcd, 0x17, data);
+	}
 
 	/* disable phy pfm mode */
 	phy_modify_paged(phydev, 0x0a44, 0x11, BIT(7), 0);
-- 
2.56.1


                 reply	other threads:[~2026-12-06 14:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206140301.13740-1-asiyee994@gmail.com \
    --to=asiyee994@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=hkallweit1@gmail.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=nic_swsd@realtek.com \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox