Skip to contentSkip to conversation

r8152: refactor and extend RTL8157/8159 support

PATCH In review Chih Kai Hsu 4 participants 26 messages Nov 30 – Dec 1

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v10 cover r8152: refactor and extend RTL8157/8159 support

RTL8157 and RTL8159 have different init, enable, up/down, hw_phy_cfg, and PHY access sequences from RTL8156. This series first fixes the up/down, unload, change_mtu and flow control settings that these chips shared with RTL8156 but need to be different, then separates the shared callbacks by chip generation and adds the missing RTL8157/RTL8159
functionality: TGPHY register access via phy_read/phy_write pointers in struct rtl_ops, and UPS support via r8157_ups_en().

v10:
- Patch #8:
- Reword the commit message: an unknown package or a failed read of the package register yields RTL_VER_UNKNOWN, which rtl8152_probe() and the configuration selector reject with -ENODEV, so a chip whose package cannot be identified no longer binds to r8152. Explain why skipping only the firmware is not enough, and that the package register is read independently by the configuration selector, rtl8152_probe() and r8153_ecm_probe().
- Log an unknown package with dev_err() instead of dev_info(), as the device is rejected.

- Patch #10: fix the indentation of EN_ALDPS_PLLOFF.

- Patch #11:
- Document in the commit message that a TGPHY access returns -ENODEV only when the device is marked inaccessible, so a failure that does not mark it (-ENOMEM, an unplug not yet reported by the USB core) is not detected, as in the OCP accessors.
- Document that a TGPHY write only waits for the busy bit before it posts the command, so success means the command was issued.
- Document the effect of read_mii_word() returning 0 on a failed read on mii_nway_restart(), rtl_ethtool_set_eee() and mii_ethtool_get_link_ksettings(), and that -ENODEV is returned only when the device is already inaccessible on entry.
- Document the control-flow changes on the older chips: the 0xd068 read in r8156_hw_phy_cfg(), the BMCR read in rtl8152_set_pauseparam(), and the errno of rtl_pre_ram_code() and the PATCH_REQUEST release in rtl_phy_patch_request().
- In rtl8152_set_speed(), keep waiting for BMCR_RESET to clear when a read fails, as r8153_phy_status() does, and stop early only when the device is inaccessible.
- set_carrier() sets RTL8152_LINK_CHG again and retries after 100 ms when the BMSR read fails, so that a link change is not lost. The retry is skipped when the device is inaccessible.
- Remove inline from r8152_mdio_write().

- Patch #12:
- Explain in the commit message why USB_FW_USB_VER is cleared in the r8157_ups_en() disable path: a power cut clears the PLA firmware version but not the USB one, while the USB patch is lost.
- Fix the indentation of USB_FW_USB_VER.

v9:
- Patch #4 (was v8 #4):
- Explain in the commit message why SLOT_EN in PLA_RCR is cleared.
- With no rate bit set, i.e. link down or a rate the driver does not advertise (500 or 1250 Mbit/s), disable the pause limit quietly with dev_dbg() instead of logging an error.
- Update the commit message.

- Patch #6 (was v8 #6): explain in the commit message that RMT_WAKE_EN is managed by the firmware, and that the driver only clears a stale value.

- Patch #7 (new): accept the PLA and USB firmware blocks for RTL8157 and RTL8159 in rtl8152_is_fw_mac_ok(), which rejected the whole image for these chips. Add a Fixes tag.

- Patch #8 (was v8 #7):
- Select rtl8159-2.fw for QFN100 and keep rtl8159-1.fw for QFN68.
- A failed read of the package register is logged and yields RTL_VER_UNKNOWN, since guessing a package could load the wrong firmware. __rtl_get_pkg_det() returns an errno.
- Split the VER_17 cases added by patch #7 into QFN68 and QFN100.

- Patch #11 (was v8 #10):
- rtl8152_set_speed() updates the cached speed/duplex, the UPS speed/duplex and force_media only once the final BMCR write succeeds, and keeps a pending PHY reset if that write fails. A failed read while waiting for the PHY reset to finish is not reported.
- set_carrier() keeps the current link state when the BMSR read fails.
- rtl8153_in_nway() reports the PHY as negotiating when the read fails, so that runtime suspend is delayed.
- Update the commit message.

- Patch #12 (was v8 #11): explain in the commit message that the backup-restore wait in r8157_ups_en() also applies to RTL8157.

v8:
- Move the fixes to the front of the series, so that they can be backported without the refactoring, and add Fixes tags:
- Patch #1 (was v7 #5): fix up and down register settings, now applied to the shared rtl8156_up()/rtl8156_down() before they are split.
- Patch #2 and #3 (were v7 #7): split rtl8157_unload() and rtl8157_change_mtu() into separate patches. Reword the commit messages.
- Patch #4 (was v7 #9): fix flow control for RTL8156/RTL8156B/RTL8157/RTL8159. It is now applied to the shared rtl8156_init() and rtl8156_enable(), and goes ahead of the refactoring. Rework the commit message: describe what was missing on each chip, and note that the 500 and 1250 Mbit/s rates the hardware can report disarm the pause limit.

- Patch #5 (was v7 #1): the RTL8156B-only part of r8156_init() is now the AUTO_SPEEDUP clear, as the flow control setup moved to patch #4. Update the commit message.

- Patch #6 (was v7 #2): RTL_VER_12 no longer gets
r8156b_u2phy_backup().

- Patch #7 (was v7 #3): rebase on the new order, with the VER_17 cases in rtl_fc_pause_pkt_en(), rtl8156_up() and rtl8156_down() split into QFN68 and QFN100.

- Patch #8 (was v7 #4): rebase on the new order. The register fixes and rtl8157_change_mtu() now come from patches #1 to #3, so this patch is a pure split with no functional change intended. Add rtl8156b_enable() for RTL8156B (VER_12/13/15), so that rtl8156_enable() serves RTL8156 only and neither needs version checks.

- Patch #9 (was v7 #6): rebase on the new order.

- Patch #10 (was v7 #8):
- Log a TGPHY busy timeout ratelimited, so that a stuck busy bit does not flood the log.
- Propagate the PHY access failure also in

    rtl8152_get_link_ksettings(), r8153_get_eee() and
    r8152_mdio_read(), and stop on a failed read in rtl8153_in_nway(),
    r8156_mdio_force_mode(), r8156_eee_en() and rtl8152_fw_phy_fixup().

- rtl8152_get_pauseparam() reports no flow control, and r8153_phy_status() keeps polling, when a read fails.
- rtl8152_set_speed() leaves the cached advertising and a pending PHY reset untouched when a write fails.
- read_mii_word() reports a failed read as 0, as the MII library takes the return value for register content.
- rtl_phy_patch_request() returns the errno of a failed write and releases PATCH_REQUEST when a later step fails.
- Rewrite the commit message and list what is left to a follow-up series.

- Patch #11 (was v7 #10): in r8157_ups_en(), stop waiting for the backup restore, and return, as soon as the device is marked RTL8152_INACCESSIBLE.

v7:
- Patch #1: split r8156_init() by chip generation rather than per chip. RTL8156 and RTL8156B share r8156_init(), and RTL8157 and RTL8159 share r8157_init(), since their sequences are near-identical. The few differences are selected with switch (tp->version). r8156b_init() and r8159_init() are gone.

- Patch #2: apply the new init writes to the merged r8156_init() and r8157_init().

- Patch #3:
- The RTL8159 QFN68/QFN100 differences become cases of the version switches in r8157_init(), and the QFN68-only u2phy backup restore write falls through to the common u3phy backup write.
- Report the number of retries (i - 1) in the "Needed %d retries to read package" warning.
- Update the commit message.

- Patch #4:
- Share the exit-OOB and enter-OOB sequences of RTL8156, RTL8156B,

    RTL8157 and RTL8159 through r8156_exit_oob() and r8156_enter_oob(),
    as RTL8152B and RTL8153 do.

- Serve RTL8156 and RTL8156B (VER_10/11/12/13/15) with a single rtl8156_enable() instead of adding rtl8156b_enable(). The r8156_fc_parameter() call stays behind a version check, so it is no longer dropped.
- Retitle and update the commit message.

- Patch #5: adapt to the shared r8156_exit_oob() and r8156_enter_oob(). Update the commit message.

- Patch #7: r8156_exit_oob() now calls tp->rtl_ops.change_mtu() instead of rtl8156_change_mtu(), so that rtl8157_up() gets rtl8157_change_mtu(). Update the commit message.

- Patch #9: apply rtl_fc_pause_pkt_en() in the merged r8156_init(). AUTO_SPEEDUP is still cleared there for RTL8156B only. Update the commit message.

- Patch #10: replace r8156_ups_en() with r8157_ups_en() in the merged r8157_init() only. Update the commit message.

v6:
- Patch #1: make it a pure split of r8156_init() per chip, plus the wait_autoload_done() helper, with no functional change. The new init writes it used to carry move to patch #2. Also convert r8152b_init() to wait_autoload_done().

- Patch #2 (new): add the new init writes for RTL8156B/8157/8159, taken out of patch #1.

- Patch #3:
- Fix the "Needed %d retries" warning in __rtl_get_pkg_det() to

    trigger only when a retry was actually needed (i != 1).

- Reword the commit message.

- Patch #6:
- r8159_hw_phy_cfg() ends with rtl_eee_enable(tp, tp->eee_en) so that the configured state is applied in both directions.
- Reword the commit message.

- Patch #8:
- A TGPHY busy timeout no longer sets RTL8152_INACCESSIBLE. wait_tgphy_cmd_ready() logs and returns the poll error, as the ADV command channel does, and returns -ENODEV only if the device is already marked inaccessible.
- Drop the RTL8152_INACCESSIBLE exceptions from the LAN_ON WARN_ON_ONCE() checks in hw_phy_cfg, which are no longer needed.
- Narrow the commit message to what the patch does and list what is left to a follow-up series: the remaining callers, the MII library callbacks, and the recovery from a TGPHY timeout.

- Patch #9:
- Explain the symptom, that RTL8157/RTL8159 could keep sending pause frames indefinitely, and add Fixes tags for RTL8156, RTL8157 and RTL8159.
- Keep the FC patch in r8156b_init() split from the AUTO_SPEEDUP clearing, and call rtl_fc_pause_pkt_en() right before it.

- Patch #10: note in the commit message that a backup-restore timeout in r8157_ups_en() follows what r8159_init() does for the same wait (mark the device inaccessible and log, without queuing a reset), and that recovery is left to a follow-up series.

v5:
- Patch #1: in r8157_init()/r8159_init(), call r8156b_wait_loading_flash() before wait_autoload_done(). Document in the commit message that the "Bulk In End" bit is cleared in r8157_init(), and drop the claim about the FC-timer block.

- Patch #2: accept the package read only if the full word was returned, warn when a retry was needed, and print an unknown package as 0x%04x. Say "firmware" instead of "PHY tuning" in the commit message.

- Patch #3: make it a pure split with no functional change. Its register fixes move to the new patch #4.

- Patch #4 (new): fix up and down register settings for RTL8156/8156B/8157/8159.

- Patch #5: document the new RTL8159 PHY tuning parameters in the commit message.

- Patch #6: add Fixes tags.

- Patch #7:
- A TGPHY busy timeout now sets RTL8152_INACCESSIBLE and returns -ENODEV.
- sram_write() returns the data-write result.
- The PHY firmware version checks, the BMCR reset poll, rtl8152_set_speed(), the pause parameters and the PHY patch handshake handle PHY access failures.
- SIOCGMIIREG returns 0 with val_out set on success, or the error.
- Commit message rewritten.

- Patch #8: drop the "no link" case, since the caller checks the link. An unknown speed now disarms the pause limit. Commit message rewritten.

- Patch #9: wait for the backup-restore flag with a timeout in r8157_ups_en(). Rename USB_FW_USE_VER to USB_FW_USB_VER.

v4:
- Patch #1: make wait_autoload_done() bail out immediately on RTL8152_INACCESSIBLE instead of waiting out the full 10 second timeout.
- Patch #6: propagate PHY access failures instead of ignoring them. ocp_reg_read()/ocp_reg_write() and every helper built on top of them (sram_read/write, sram2_read/write, the ocp_reg_*_bits and MDIO bit helpers) now return int and stop silently operating on stale data when a TGPHY access times out.

v3:
Rebase on top of latest net-next to resolve merge conflicts.

v2:
- For patch #1, use read_poll_timeout() to replace the manual for-loop polling.
- For patch #2 and #3, rewrite the commit message.

Chih Kai Hsu (12):
r8152: fix RTL8156/RTL8156B/RTL8157/RTL8159 up and down settings
r8152: add rtl8157_unload() for RTL8157 and RTL8159
r8152: add rtl8157_change_mtu() for RTL8157 and RTL8159
r8152: fix flow control for RTL8156/RTL8156B/RTL8157/RTL8159
r8152: split r8156_init by generation and factor out
wait_autoload_done
r8152: add new init writes for RTL8156/8156B/8157/8159
r8152: accept PLA and USB firmware blocks for RTL8157 and RTL8159
r8152: split RTL_VER_17 into QFN68 and QFN100 package variants
r8152: split rtl8156_enable/up/down into per-generation functions
r8152: split r8157_hw_phy_cfg into RTL8157 and RTL8159 variants
r8152: add TGPHY register access for RTL8157 and RTL8159
r8152: enable UPS for RTL8157 and RTL8159 QFN68

 drivers/net/usb/r8152.c | 1841 +++++++++++++++++++++++++++++----------
 1 file changed, 1391 insertions(+), 450 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v10 1/12 r8152: fix RTL8156/RTL8156B/RTL8157/RTL8159 up and down settings

Several settings the datasheet requires for these chips were either missing or gated on the wrong version range in the shared rtl8156_up()/rtl8156_down():

- PLA_RCR1 BIT(3) must be cleared on up for all of them. It was cleared only for RTL_VER_16 and later, so RTL8156/RTL8156B never reached it.
- USB_SPEED_OPTION's RG_PWRDN_EN and ALL_SPEED_OFF must be cleared on up for RTL8157/RTL8159 as well. The "version < RTL_VER_16" check excluded precisely the chips that need it.
- Clear_SDR must be enabled on up: USB 0xd3ca BIT(15) on RTL8156B (VER_13/15), and USB 0xd378 BIT(7) set plus USB 0xcd06 BIT(15) cleared on RTL8157/RTL8159.
- Entering OOB needs PLA_BDC_CR's ALDPS_PROXY_MODE and PLA_OOB_CTRL's DIS_MCU_CLROOB. Only r8152b_enter_oob() and r8153_enter_oob() set them; the RTL8156 family down path set NOW_IS_OOB alone.
- RTL8157/RTL8159 OOB RX settings are PLA_RMS 1526 and PLA_MTPS 10K, not the 1522 and 6K they inherited.
- PLA_MCU_SPDWN_EN is not used by RTL8157/RTL8159, so stop managing it on up/down for them.

Fixes 195aae321c82 ("r8152: support new chips")
Fixes fd3c7d080df5 ("r8152: Add support for the RTL8157 hardware")
Fixes ad2a55b63b41 ("r8152: Add support for the RTL8159 chip")
---
 drivers/net/usb/r8152.c | 52 ++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 11 deletions(-)
drivers/net/usb/r8152.c +41 -11
index 4815235e8e730..d00cc911ae135 100644
@@ -6820,8 +6820,7 @@ static void rtl8156_up(struct r8152 *tp)
 
 	ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB);
 
-	if (tp->version >= RTL_VER_16)
-		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR1, BIT(3));
+	ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR1, BIT(3));
 
 	ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, MCU_BORW_EN);
 
@@ -6843,11 +6842,12 @@ static void rtl8156_up(struct r8152 *tp)
 	ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, RXFIFO_FULL_MASK,
 		      0x08);
 
-	ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, PLA_MCU_SPDWN_EN);
-
 	if (tp->version < RTL_VER_16)
-		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_SPEED_OPTION,
-				  RG_PWRDN_EN | ALL_SPEED_OFF);
+		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
+				  PLA_MCU_SPDWN_EN);
+
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_SPEED_OPTION,
+			  RG_PWRDN_EN | ALL_SPEED_OFF);
 
 	ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400);
 
@@ -6860,6 +6860,23 @@ static void rtl8156_up(struct r8152 *tp)
 	if (tp->version < RTL_VER_16)
 		r8153_u2p3en(tp, true);
 
+	switch (tp->version) {
+	case RTL_VER_13:
+	case RTL_VER_15:
+		/* Enable Clear_SDR */
+		ocp_word_set_bits(tp, MCU_TYPE_USB, 0xd3ca, BIT(15));
+		break;
+	case RTL_VER_16:
+	case RTL_VER_17:
+		/* Enable Clear_SDR */
+		ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xd378, BIT(7));
+		/* Clear SDR bit */
+		ocp_word_clr_bits(tp, MCU_TYPE_USB, 0xcd06, BIT(15));
+		break;
+	default:
+		break;
+	}
+
 	if (tp->version < RTL_VER_16 && tp->udev->speed >= USB_SPEED_SUPER)
 		r8153b_u1u2en(tp, true);
 }
@@ -6871,8 +6888,9 @@ static void rtl8156_down(struct r8152 *tp)
 		return;
 	}
 
-	ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
-			  PLA_MCU_SPDWN_EN);
+	if (tp->version < RTL_VER_16)
+		ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
+				  PLA_MCU_SPDWN_EN);
 
 	r8153b_u1u2en(tp, false);
 	if (tp->version < RTL_VER_16) {
@@ -6891,8 +6909,17 @@ static void rtl8156_down(struct r8152 *tp)
 	rtl_disable(tp);
 	rtl_reset_bmu(tp);
 
-	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522);
-	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT);
+	switch (tp->version) {
+	case RTL_VER_16:
+	case RTL_VER_17:
+		ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1526);
+		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64);
+		break;
+	default:
+		ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522);
+		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT);
+		break;
+	}
 
 	/* Clear teredo wake event. bit[15:8] is the teredo wakeup
 	 * type. Set it to zero. bits[7:0] are the W1C bits about
@@ -6900,7 +6927,10 @@ static void rtl8156_down(struct r8152 *tp)
 	 */
 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
 
-	ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB);
+	ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_BDC_CR, ALDPS_PROXY_MODE);
+
+	ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL,
+			  NOW_IS_OOB | DIS_MCU_CLROOB);
 
 	ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, MCU_BORW_EN);
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH net-next v10 01/12] r8152: fix RTL8156/RTL8156B/RTL8157/RTL8159 up and down settings

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message of "r8152: fix RTL8156/RTL8156B/RTL8157/RTL8159 up and down settings" lists register…
- [Low] One patch bundles independent register fixes for different chip generations, tied to three Fixes: commits years apart, plus a cleanup.

--

r8152: fix RTL8156/RTL8156B/RTL8157/RTL8159 up and down settings

This patch changes the shared rtl8156_up() and rtl8156_down() paths to apply several register settings from the datasheet to RTL8156, RTL8156B, RTL8157 and RTL8159. It changes the version gating for PLA_RCR1, USB_SPEED_OPTION and PLA_MCU_SPDWN_EN, and adds Clear_SDR setup. It also sets ALDPS_PROXY_MODE and DIS_MCU_CLROOB on OOB entry and uses different PLA_RMS/PLA_MTPS values for RTL8157/RTL8159.

Quoted 7 lines

Several settings the datasheet requires for these chips were either missing or gated on the wrong version range in the shared rtl8156_up()/rtl8156_down():

- PLA_RCR1 BIT(3) must be cleared on up for all of them. It was cleared only for RTL_VER_16 and later, so RTL8156/RTL8156B never reached it.

[ ... ]

Quoted 3 lines

- Entering OOB needs PLA_BDC_CR's ALDPS_PROXY_MODE and PLA_OOB_CTRL's DIS_MCU_CLROOB. Only r8152b_enter_oob() and r8153_enter_oob() set them; the RTL8156 family down path set NOW_IS_OOB alone.

[ ... ]

Quoted 3 lines
Fixes 195aae321c82 ("r8152: support new chips")
Fixes fd3c7d080df5 ("r8152: Add support for the RTL8157 hardware")
Fixes ad2a55b63b41 ("r8152: Add support for the RTL8159 chip")

[Severity: Low]
This isn't a bug, but could the commit message say what users see when these settings are missing?

Each bullet names a register bit or value that the datasheet requires. None of them describes a failure, for example:

- RX not working
- WoL or OOB wake failing
- the wrong link or USB speed after ifdown/ifup or suspend
- a power-state problem

Because of the Fixes: 195aae321c82 tag, this patch will go to stable trees back to 2021. It also changes behaviour on RTL8156/RTL8156B, which have shipped for years:

rtl8156_up() now clears PLA_RCR1 BIT(3) for RTL_VER_10 through RTL_VER_15.

rtl8156_down() now sets ALDPS_PROXY_MODE and DIS_MCU_CLROOB on close and on system suspend, for every chip that uses this path.

Without a description of the symptom, how can maintainers and stable backporters judge how urgent this is, or check that the fix works?

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
index 4815235e8e730..d00cc911ae135 100644

[ ... ]

Quoted 10 lines
drivers/net/usb/r8152.c +6 -0
@@ -6860,6 +6860,23 @@ static void rtl8156_up(struct r8152 *tp)
 	if (tp->version < RTL_VER_16)
 		r8153_u2p3en(tp, true);
 
+	switch (tp->version) {
+	case RTL_VER_13:
+	case RTL_VER_15:
+		/* Enable Clear_SDR */
+		ocp_word_set_bits(tp, MCU_TYPE_USB, 0xd3ca, BIT(15));
+		break;

drivers/net/usb/r8152.c static void rtl8156_up(struct r8152 *tp) · line 6878

Quoted 10 lines
drivers/net/usb/r8152.c +10 -0
+	case RTL_VER_16:
+	case RTL_VER_17:
+		/* Enable Clear_SDR */
+		ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xd378, BIT(7));
+		/* Clear SDR bit */
+		ocp_word_clr_bits(tp, MCU_TYPE_USB, 0xcd06, BIT(15));
+		break;
+	default:
+		break;
+	}

[Severity: Low]
Would it make sense to split this into one patch per chip generation?

This switch puts two fixes side by side:

- the RTL8156B Clear_SDR setting (RTL_VER_13/RTL_VER_15), which goes

    back to 195aae321c82 from 2021
  - the RTL8157/RTL8159 setting (RTL_VER_16/RTL_VER_17), which goes back
    to fd3c7d080df5 and ad2a55b63b41 from 2026

RTL_VER_16 and RTL_VER_17 do not exist at 195aae321c82. The Fixes:
195aae321c82 tag sends this patch to exactly those older stable trees, where it would not apply or build as is.

rtl8156_down() has the same mix. There, the new PLA_RMS 1526 and PLA_MTPS 10K values apply only to RTL_VER_16/RTL_VER_17.

The PLA_MCU_SPDWN_EN change in rtl8156_up() and rtl8156_down() has only this justification:

"PLA_MCU_SPDWN_EN is not used by RTL8157/RTL8159, so stop managing it on up/down for them."

No harm is stated, so it reads as a cleanup inside a fix tagged for stable.

With one RTL8156/RTL8156B fix (Fixes: 195aae321c82) and one RTL8157/RTL8159 fix (Fixes: fd3c7d080df5 and ad2a55b63b41), each could go to the right trees and be bisected or reverted on its own.

A split would not remove all backport work. The baseline also uses the ocp_word_set_bits()/ocp_word_clr_bits() style helpers from d16133f177fea6, so an RTL8156-only fix would still need some manual backporting. The conflicts would be smaller, though.

These Fixes-tagged changes also sit in a series with net-next feature work such as UPS enablement and function splits.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1-nic_swsd%40realtek.com
#3 lore raw

PATCH v10 2/12 r8152: add rtl8157_unload() for RTL8157 and RTL8159

In reply to Chih Kai Hsu

RTL8157 and RTL8159 used rtl8153_unload(), which disables power cut with r8153_power_cut_en(). Per the datasheet they need r8157_power_cut_en(), which r8156_init() already calls for them.

Add rtl8157_unload() for them, and move the Interrupt Mitigation disable that rtl8153_unload() gated on "version >= RTL_VER_16" into it unconditionally.

Fixes fd3c7d080df5 ("r8152: Add support for the RTL8157 hardware")
Fixes ad2a55b63b41 ("r8152: Add support for the RTL8159 chip")
---
 drivers/net/usb/r8152.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/net/usb/r8152.c +12 -7
index d00cc911ae135..bb4106f238a3a 100644
@@ -9696,11 +9696,6 @@ static void rtl8153_unload(struct r8152 *tp)
 		return;
 
 	r8153_power_cut_en(tp, false);
-
-	if (tp->version >= RTL_VER_16) {
-		/* Disable Interrupt Mitigation */
-		ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xcf04, BIT(0) | BIT(1) | BIT(2) | BIT(7));
-	}
 }
 
 static void rtl8153b_unload(struct r8152 *tp)
@@ -9711,6 +9706,16 @@ static void rtl8153b_unload(struct r8152 *tp)
 	r8153b_power_cut_en(tp, false);
 }
 
+static void rtl8157_unload(struct r8152 *tp)
+{
+	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+		return;
+
+	r8157_power_cut_en(tp, false);
+	ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xcf04,
+			  BIT(0) | BIT(1) | BIT(2) | BIT(7));
+}
+
 static int r8152_desc_init(struct r8152 *tp)
 {
 	tp->rx_desc.size = sizeof(struct rx_desc);
@@ -9889,7 +9894,7 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->disable		= rtl8153_disable;
 		ops->up			= rtl8156_up;
 		ops->down		= rtl8156_down;
-		ops->unload		= rtl8153_unload;
+		ops->unload		= rtl8157_unload;
 		ops->eee_get		= r8153_get_eee;
 		ops->eee_set		= r8152_set_eee;
 		ops->in_nway		= rtl8153_in_nway;
@@ -9911,7 +9916,7 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->disable		= rtl8153_disable;
 		ops->up			= rtl8156_up;
 		ops->down		= rtl8156_down;
-		ops->unload		= rtl8153_unload;
+		ops->unload		= rtl8157_unload;
 		ops->eee_get		= r8153_get_eee;
 		ops->eee_set		= r8152_set_eee;
 		ops->in_nway		= rtl8153_in_nway;
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH net-next v10 02/12] r8152: add rtl8157_unload() for RTL8157 and RTL8159

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message for "r8152: add rtl8157_unload() for RTL8157 and RTL8159" carries two Fixes: tags but never says what users see…

--

r8152: add rtl8157_unload() for RTL8157 and RTL8159

This commit adds rtl8157_unload() for RTL8157 and RTL8159. On unload it now disables power cut with r8157_power_cut_en() instead of r8153_power_cut_en(). It also moves the Interrupt Mitigation disable out of rtl8153_unload() and runs it unconditionally in the new function.

Quoted 3 lines

RTL8157 and RTL8159 used rtl8153_unload(), which disables power cut with r8153_power_cut_en(). Per the datasheet they need r8157_power_cut_en(), which r8156_init() already calls for them.

[ ... ]

Quoted 2 lines
Fixes fd3c7d080df5 ("r8152: Add support for the RTL8157 hardware")
Fixes ad2a55b63b41 ("r8152: Add support for the RTL8159 chip")

[Severity: Low]
What do users actually see when these chips go through r8153_power_cut_en(tp, false) at unload? Could the commit message say?

The patch has two Fixes: tags, but the only reason given is "Per the datasheet". That makes it hard for stable backporters to tell which symptom is being fixed, for example misbehaviour after unbind/rebind or re-plug.

The message also doesn't mention that the register writes at unload change. The old path cleared PHASE2_EN:

drivers/net/usb/r8152.c:r8153_power_cut_en() {
	...
	else
		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT,
				  PWR_EN | PHASE2_EN);

	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS);
}

The new path only clears PWR_EN in USB_POWER_CUT. It also now clears USB_MISC_2 BIT(1):

drivers/net/usb/r8152.c:r8157_power_cut_en() {
	...
	} else {
		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, PWR_EN);
		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS);
		ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2, BIT(1));
	}
}

At this commit, nothing in the driver seems to set PHASE2_EN on these chips. Neither r8156_ups_en(true) nor r8157_power_cut_en(true) is called, and r8156_init() already clears USB_MISC_2 BIT(1). So the change only has an effect if hardware or firmware left those bits set.

Later in the series, the RTL8157/RTL8159 runtime enable path calls r8157_ups_en(tp, true). That sets UPS_EN | USP_PREWAKE | PHASE2_EN on RTL_VER_16 and RTL_VER_17_QFN68, and r8157_ups_en(false) doesn't clear PHASE2_EN. With this patch applied, PHASE2_EN would then stay set across unload.

Is it intended that PHASE2_EN stays set at unload? Could the commit message mention this, along with the user-visible symptom being fixed?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1-nic_swsd%40realtek.com
#5 lore raw

PATCH v10 3/12 r8152: add rtl8157_change_mtu() for RTL8157 and RTL8159

In reply to Chih Kai Hsu

RTL8157 and RTL8159 used rtl8156_change_mtu(), which programs a fixed MTPS_JUMBO. Per the datasheet they need an MTPS scaled from the configured MTU instead: max_pkt_size / 64, at least 10K and capped at 0xff.

Add rtl8157_change_mtu() for them. Since rtl8156_up() is shared with these chips, have it call tp->rtl_ops.change_mtu() instead of rtl8156_change_mtu() directly so each chip gets its own helper.

Fixes fd3c7d080df5 ("r8152: Add support for the RTL8157 hardware")
Fixes ad2a55b63b41 ("r8152: Add support for the RTL8159 chip")
---
 drivers/net/usb/r8152.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
drivers/net/usb/r8152.c +25 -3
index bb4106f238a3a..1517753f843ef 100644
@@ -6800,6 +6800,28 @@ static void rtl8156_change_mtu(struct r8152 *tp)
 		       ALIGN(rx_max_size + tp->tx_desc.size, 1024) / 16);
 }
 
+static void rtl8157_change_mtu(struct r8152 *tp)
+{
+	u32 max_pkt_size = mtu_to_size(tp->netdev->mtu);
+	u32 ocp_data;
+
+	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, max_pkt_size);
+
+	/* Use at least 10K for MTPS */
+	ocp_data = max_t(u32, max_pkt_size, 10 * 1024) / 64;
+
+	/* 16 * 1024 / 64 = 0x100, so the max is 0xff for 8 bits data */
+	ocp_data = min_t(u32, ocp_data, 0xff);
+
+	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, ocp_data);
+	r8156_fc_parameter(tp);
+
+	/* TX share fifo free credit full threshold */
+	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
+	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL,
+		       ALIGN(max_pkt_size + tp->tx_desc.size, 1024) / 16);
+}
+
 static void rtl8156_up(struct r8152 *tp)
 {
 	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
@@ -6826,7 +6848,7 @@ static void rtl8156_up(struct r8152 *tp)
 
 	rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
 
-	rtl8156_change_mtu(tp);
+	tp->rtl_ops.change_mtu(tp);
 
 	switch (tp->version) {
 	case RTL_TEST_01:
@@ -9900,7 +9922,7 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->in_nway		= rtl8153_in_nway;
 		ops->hw_phy_cfg		= r8157_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8157_runtime_enable;
-		ops->change_mtu		= rtl8156_change_mtu;
+		ops->change_mtu		= rtl8157_change_mtu;
 		tp->rx_buf_sz		= 32 * 1024;
 		tp->support_2500full	= 1;
 		tp->support_5000full	= 1;
@@ -9922,7 +9944,7 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->in_nway		= rtl8153_in_nway;
 		ops->hw_phy_cfg		= r8157_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8157_runtime_enable;
-		ops->change_mtu		= rtl8156_change_mtu;
+		ops->change_mtu		= rtl8157_change_mtu;
 		tp->rx_buf_sz		= 48 * 1024;
 		tp->support_2500full	= 1;
 		tp->support_5000full	= 1;
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH net-next v10 03/12] r8152: add rtl8157_change_mtu() for RTL8157 and RTL8159

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit is tagged with two Fixes: tags (fd3c7d080df5 for RTL8157 and ad2a55b63b41 for RTL8159), so it will be picked up for…

--

r8152: add rtl8157_change_mtu() for RTL8157 and RTL8159

This commit adds rtl8157_change_mtu() for RTL8157 and RTL8159. The new helper programs PLA_MTPS from the configured MTU: max_pkt_size / 64, with a 10K minimum and a cap of 0xff. Before this, these chips used the fixed MTPS_JUMBO from rtl8156_change_mtu(). rtl8156_up() now calls tp->rtl_ops.change_mtu(), so each chip uses its own helper.

Quoted 4 lines

RTL8157 and RTL8159 used rtl8156_change_mtu(), which programs a fixed MTPS_JUMBO. Per the datasheet they need an MTPS scaled from the configured MTU instead: max_pkt_size / 64, at least 10K and capped at 0xff.

[Severity: Low]
This patch has two Fixes: tags, so it will likely be picked up for stable. Could the commit message say what actually goes wrong on RTL8157 and RTL8159 with the old fixed MTPS_JUMBO (12 * 1024 / 64 = 0xc0)?

The only reason given is the datasheet. The message names no symptom and no affected MTU range. It also doesn't say whether the problem was seen on hardware or found by reading the datasheet.

It may also be worth stating that this changes the value for normal MTUs, not only for jumbo frames. In rtl8157_change_mtu(), the default MTU of 1500 gives:

    max_pkt_size = mtu_to_size(1500) = 1500 + 18 + 4 = 1522
    ocp_data = max_t(u32, 1522, 10 * 1024) / 64 = 0xa0

So at the default MTU, PLA_MTPS drops from 12K to 10K. The new value stays below the old 0xc0 until the MTU reaches roughly 12266. Above that it is larger than before, and it hits the 0xff cap for MTUs near max_mtu.

Without the user-visible impact, a stable backporter can't easily tell two cases apart. Does the bug only affect frames above 12K? Or is lowering MTPS for standard MTUs also intended and safe to backport?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1-nic_swsd%40realtek.com
#7 lore raw

PATCH v10 4/12 r8152: fix flow control for RTL8156/RTL8156B/RTL8157/RTL8159

In reply to Chih Kai Hsu

The flow-control setup that limits pause frames was incomplete or missing on several chips:

- RTL8156A (VER_10/11): r8156_init() skipped them, so the FC timer and FLOW_CTRL_PATCH_OPT setup that the FC_PATCH_TASK toggle in rtl8156_enable() depends on was never done.
- RTL8157/RTL8159 (VER_16/17): no limit on the number of pause packets was programmed, so they could keep sending pause frames indefinitely.

Collect the per-family sequence into rtl_fc_pause_pkt_en() and call it for all of them.

- VER_10/11: the FC timer + FLOW_CTRL_PATCH_OPT sequence that RTL8153B (VER_09) already runs from its pre_fw/post_fw hooks, applied directly instead.

- VER_12/13/15: the sequence r8156_init() already ran, moved here unchanged.

- VER_16/17: limit pause packets with the new PLA_PAUSE_LIMIT register instead of the FW patch. The datasheet gives a fixed count per link speed, reproduced here by a log2-ratio computation. Because the count follows the negotiated speed, this family is called from rtl8156_enable() rather than from init.

SLOT_EN in PLA_RCR is cleared as well, which these chips did not do before. With SLOT_EN clear, a pause frame is sent each time the pause timer expires, which is what the pause limit relies on. The clear is idempotent, so running it on every enable is harmless.

The speed decode covers every rate the driver advertises. With no rate bit set, i.e. link down or a rate the driver does not advertise (500 or 1250 Mbit/s), pause limiting is disabled rather than programmed from a guessed speed.

In r8156_init(), split AUTO_SPEEDUP clearing from the flow-control FW patch; it still applies to RTL8156B only.

Fixes 195aae321c82 ("r8152: support new chips")
Fixes fd3c7d080df5 ("r8152: Add support for the RTL8157 hardware")
Fixes ad2a55b63b41 ("r8152: Add support for the RTL8159 chip")
---
 drivers/net/usb/r8152.c | 128 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 113 insertions(+), 15 deletions(-)
drivers/net/usb/r8152.c +113 -15
index 1517753f843ef..e58b010c734ad 100644
@@ -87,6 +87,7 @@
 #define PLA_MTPS		0xe615
 #define PLA_TXFIFO_CTRL		0xe618
 #define PLA_TXFIFO_FULL		0xe61a
+#define PLA_PAUSE_LIMIT		0xe61e
 #define PLA_RSTTALLY		0xe800
 #define PLA_CR			0xe813
 #define PLA_CRWECR		0xe81c
@@ -293,6 +294,10 @@
 #define MTPS_JUMBO		(12 * 1024 / 64)
 #define MTPS_DEFAULT		(6 * 1024 / 64)
 
+/* PLA_PAUSE_LIMIT */
+#define PAUSE_LIMIT_EN		BIT(3)
+#define PAUSE_LIMIT_MASK	0xf0
+
 /* PLA_RSTTALLY */
 #define TALLY_RESET		0x0001
 
@@ -5961,6 +5966,105 @@ static void r8152b_enter_oob(struct r8152 *tp)
 			   RCR_APM | RCR_AM | RCR_AB);
 }
 
+static void rtl_fc_pause_pkt_en(struct r8152 *tp, u16 speed)
+{
+	int log2_ratio, ratio;
+	u16 num_pause_pkts;
+	u32 ocp_data;
+
+	switch (tp->version) {
+	case RTL_VER_10:
+	case RTL_VER_11:
+		ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
+			       CTRL_TIMER_EN | (1000 / 8));
+
+		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_CTRL,
+				  FLOW_CTRL_PATCH_OPT);
+
+		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
+		break;
+	case RTL_VER_12:
+	case RTL_VER_13:
+	case RTL_VER_15:
+		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, SLOT_EN);
+
+		ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_CPCR, FLOW_CTRL_EN);
+
+		/* enable fc timer and set timer to 600 ms. */
+		ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
+			       CTRL_TIMER_EN | (600 / 8));
+
+		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL);
+		if (!(ocp_data & DACK_DET_EN))
+			ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_CTRL,
+					  FLOW_CTRL_PATCH_2);
+
+		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
+		break;
+	case RTL_VER_16:
+	case RTL_VER_17:
+		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, SLOT_EN);
+
+		num_pause_pkts = 0xa;
+		ratio = 10000;
+
+		if (speed & _10bps) {
+			ratio /= 10;
+		} else if (speed & _100bps) {
+			ratio /= 100;
+		} else if (speed & _1000bps) {
+			ratio /= 1000;
+		} else if (speed & _2500bps) {
+			ratio /= 2500;
+		} else if (speed & _5000bps) {
+			ratio /= 5000;
+		} else if (speed & _10000bps) {
+			ratio /= 10000;
+		} else {
+			/* Link down, or a rate the driver does not advertise
+			 * (500 or 1250 Mbit/s). Disable the limit; the next
+			 * link up programs it again.
+			 */
+			dev_dbg(&tp->intf->dev,
+				"No known link speed (0x%04x), pause limit disabled\n",
+				speed);
+			ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_PAUSE_LIMIT,
+					  PAUSE_LIMIT_EN);
+			break;
+		}
+
+		log2_ratio = ilog2(ratio);
+		num_pause_pkts -= log2_ratio;
+
+		/* Round ratio up to the next power of two if it is
+		 * more than halfway there. Floating-point is avoided
+		 * by rewriting ratio > 1.5 * 2^log2_ratio as
+		 * 2 * ratio > 3 * 2^log2_ratio.
+		 */
+		if (2 * ratio > 3 * (1 << log2_ratio))
+			num_pause_pkts--;
+
+		/* At most 2^num_pause_pkts pause packets would be sent.
+		 *
+		 *  10 Mbit/s -> 2^0
+		 * 100 Mbit/s -> 2^3
+		 *   1 Gbit/s -> 2^7
+		 * 2.5 Gbit/s -> 2^8
+		 *   5 Gbit/s -> 2^9
+		 *  10 Gbit/s -> 2^10
+		 */
+		ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_PAUSE_LIMIT,
+			      PAUSE_LIMIT_MASK | PAUSE_LIMIT_EN,
+			      num_pause_pkts << 4);
+
+		ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_PAUSE_LIMIT,
+				  PAUSE_LIMIT_EN);
+		break;
+	default:
+		break;
+	}
+}
+
 static int r8153_pre_firmware_1(struct r8152 *tp)
 {
 	int i;
@@ -6404,6 +6508,10 @@ static int rtl8156_enable(struct r8152 *tp)
 	r8153_set_rx_early_size(tp);
 
 	speed = rtl8152_get_speed(tp);
+
+	if (tp->version >= RTL_VER_16)
+		rtl_fc_pause_pkt_en(tp, speed);
+
 	rtl_set_ifg(tp, speed);
 
 	if (tp->version >= RTL_VER_16)
@@ -8490,22 +8598,12 @@ static void r8156_init(struct r8152 *tp)
 
 	usb_enable_lpm(tp->udev);
 
-	if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_15) {
-		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, SLOT_EN);
-
-		ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_CPCR, FLOW_CTRL_EN);
-
-		/* enable fc timer and set timer to 600 ms. */
-		ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, CTRL_TIMER_EN | (600 / 8));
-
-		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
-		if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN))
-			ocp_data |= FLOW_CTRL_PATCH_2;
-		ocp_data &= ~AUTO_SPEEDUP;
-		ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
+	if (tp->version < RTL_VER_16)
+		rtl_fc_pause_pkt_en(tp, 0);
 
-		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
-	}
+	if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_15)
+		/* Disable Auto Speed up */
+		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_FW_CTRL, AUTO_SPEEDUP);
 
 	r8156_mac_clk_spd(tp, true);
 
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH net-next v10 04/12] r8152: fix flow control for RTL8156/RTL8156B/RTL8157/RTL8159

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] Three fixes with different stable targets are packed into one patch and one helper, rtl_fc_pause_pkt_en() in drivers/net/usb/r8152.c.

--

r8152: fix flow control for RTL8156/RTL8156B/RTL8157/RTL8159

This patch moves the flow-control setup for RTL8156, RTL8156B, RTL8157 and RTL8159 into a new helper, rtl_fc_pause_pkt_en(). It adds the missing FC timer and FLOW_CTRL_PATCH_OPT setup for RTL8156A. It also programs a new PLA_PAUSE_LIMIT register on RTL8157/RTL8159 from rtl8156_enable(), based on the link speed.

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
index 1517753f843ef..e58b010c734ad 100644

[ ... ]

Quoted 25 lines
drivers/net/usb/r8152.c +21 -0
@@ -5961,6 +5966,105 @@ static void r8152b_enter_oob(struct r8152 *tp)
 			   RCR_APM | RCR_AM | RCR_AB);
 }
 
+static void rtl_fc_pause_pkt_en(struct r8152 *tp, u16 speed)
+{
+	int log2_ratio, ratio;
+	u16 num_pause_pkts;
+	u32 ocp_data;
+
+	switch (tp->version) {
+	case RTL_VER_10:
+	case RTL_VER_11:
+		ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
+			       CTRL_TIMER_EN | (1000 / 8));
+
+		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_CTRL,
+				  FLOW_CTRL_PATCH_OPT);
+
+		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
+		break;
+	case RTL_VER_12:
+	case RTL_VER_13:
+	case RTL_VER_15:
+		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, SLOT_EN);

[ ... ]

Quoted 3 lines
drivers/net/usb/r8152.c +3 -0
+	case RTL_VER_16:
+	case RTL_VER_17:
+		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, SLOT_EN);

[ ... ]

Quoted 11 lines
drivers/net/usb/r8152.c +4 -0
@@ -6404,6 +6508,10 @@ static int rtl8156_enable(struct r8152 *tp)
 	r8153_set_rx_early_size(tp);
 
 	speed = rtl8152_get_speed(tp);
+
+	if (tp->version >= RTL_VER_16)
+		rtl_fc_pause_pkt_en(tp, speed);
+
 	rtl_set_ifg(tp, speed);
 
 	if (tp->version >= RTL_VER_16)

[ ... ]

Quoted 5 lines
drivers/net/usb/r8152.c +0 -1
@@ -8490,22 +8598,12 @@ static void r8156_init(struct r8152 *tp)
 
 	usb_enable_lpm(tp->udev);
 
-	if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_15) {

[ ... ]

drivers/net/usb/r8152.c static void r8156_init(struct r8152 *tp) · line 8602

Quoted 2 lines
drivers/net/usb/r8152.c +2 -0
+	if (tp->version < RTL_VER_16)
+		rtl_fc_pause_pkt_en(tp, 0);

[Severity: Low]

This isn't a runtime bug, but would stable have an easier time if this patch were split up?

rtl_fc_pause_pkt_en() combines three separate changes:

- the RTL8156A (RTL_VER_10/11) FC timer and FLOW_CTRL_PATCH_OPT setup.

    r8156_init() has lacked it since 195aae321c82 ("r8152: support new
    chips").

- the new PLA_PAUSE_LIMIT and SLOT_EN setup for RTL8157/RTL8159

    (RTL_VER_16/17). These chips only exist after fd3c7d080df5 and
    ad2a55b63b41.

- an unchanged move of the RTL8156B (RTL_VER_12/13/15) sequence.

Stable trees picked by Fixes: 195aae321c82 don't have RTL_VER_16/17, PLA_PAUSE_LIMIT, or the RTL_VER_16 handling in rtl8156_enable() that the new call sits next to. The patch would not apply there. Someone would have to split out the RTL8156A part by hand.

The RTL8156A fix is just the VER_10/11 case plus the new rtl_fc_pause_pkt_en(tp, 0) call in r8156_init(). Could that go in its own patch with Fixes: 195aae321c82? The VER_16/17 pause limit would then be a separate patch with Fixes: fd3c7d080df5 / ad2a55b63b41. The refactor into a common helper could come first, or be dropped.

Later patches in this series, such as "r8152: split r8156_init by generation and factor out wait_autoload_done", keep the combined helper. So this doesn't seem to be resolved later in the series.

Also, the subject says:

r8152: fix flow control for RTL8156/RTL8156B/RTL8157/RTL8159

The commit message says the VER_12/13/15 sequence is "moved here unchanged", so no RTL8156B behavior seems to change. Should RTL8156B be removed from the subject?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1-nic_swsd%40realtek.com
#9 lore raw

PATCH v10 5/12 r8152: split r8156_init by generation and factor out wait_autoload_done

In reply to Chih Kai Hsu

RTL8156, RTL8156B, RTL8157 and RTL8159 shared a single r8156_init(), gated by a dense set of tp->version range checks. That made the init sequence hard to follow.

Split r8156_init() into two functions by chip generation:
- r8156_init (VER_10/11 and VER_12/13/15, RTL8156 and RTL8156B)
- r8157_init (VER_16/17, RTL8157 and RTL8159)

The members of each generation have near-identical sequences, so they share a function and branch on tp->version only for the few real differences, such as the RTL8156B-only AUTO_SPEEDUP clear or the RTL8159-only backup-restore wait.

Also factor the repeated "poll PLA_BOOT_CTRL for AUTOLOAD_DONE" loop into wait_autoload_done(), reusing read_poll_timeout() instead of the 500 * msleep(20) loops. r8153_init(), r8153b_init(), r8153c_init(), r8153b_ups_en() and r8153c_ups_en() are converted to use it too.

wait_autoload_done() only reports failure (nonzero) when RTL8152_INACCESSIBLE is set; a plain AUTOLOAD_DONE timeout is logged but otherwise treated as success, same as every one of the loops it replaces, which only gave up on RTL8152_INACCESSIBLE and otherwise fell through to the rest of the init sequence (r8153_init() and r8153b_init() ignore even that, as before). The two
failure modes are not equivalent: once RTL8152_INACCESSIBLE is set, generic_ocp_read()/generic_ocp_write() short-circuit to -ENODEV without touching the device, so returning early costs nothing. A plain timeout means the device is still reachable, so skipping the rest of the bring-up would leave real hardware state half-configured.

No functional change intended.

---
 drivers/net/usb/r8152.c | 310 +++++++++++++++++++++++-----------------
 1 file changed, 180 insertions(+), 130 deletions(-)
drivers/net/usb/r8152.c +180 -130
index e58b010c734ad..a1e7a33b2f50e 100644
@@ -4177,6 +4177,23 @@ static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
 	return data;
 }
 
+static int wait_autoload_done(struct r8152 *tp)
+{
+	u16 ocp_data;
+	int ret;
+
+	ret = read_poll_timeout(ocp_read_word, ocp_data,
+				test_bit(RTL8152_INACCESSIBLE, &tp->flags) ||
+				(ocp_data & AUTOLOAD_DONE),
+				20000, 10 * USEC_PER_SEC, false, tp,
+				MCU_TYPE_PLA, PLA_BOOT_CTRL);
+
+	if (ret)
+		dev_err(&tp->intf->dev, "autoload done timeout\n");
+
+	return test_bit(RTL8152_INACCESSIBLE, &tp->flags) ? -ENODEV : 0;
+}
+
 static void r8153b_ups_en(struct r8152 *tp, bool enable)
 {
 	if (enable) {
@@ -4195,16 +4212,8 @@ static void r8153b_ups_en(struct r8152 *tp, bool enable)
 				  UPS_FORCE_PWR_DOWN);
 
 		if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
-			int i;
-
-			for (i = 0; i < 500; i++) {
-				if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
-					return;
-				if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
-				    AUTOLOAD_DONE)
-					break;
-				msleep(20);
-			}
+			if (wait_autoload_done(tp))
+				return;
 
 			tp->rtl_ops.hw_phy_cfg(tp);
 
@@ -4232,16 +4241,8 @@ static void r8153c_ups_en(struct r8152 *tp, bool enable)
 				  UPS_FORCE_PWR_DOWN);
 
 		if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
-			int i;
-
-			for (i = 0; i < 500; i++) {
-				if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
-					return;
-				if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
-				    AUTOLOAD_DONE)
-					break;
-				msleep(20);
-			}
+			if (wait_autoload_done(tp))
+				return;
 
 			tp->rtl_ops.hw_phy_cfg(tp);
 
@@ -7388,22 +7389,13 @@ static void r8152b_init(struct r8152 *tp)
 static void r8153_init(struct r8152 *tp)
 {
 	u32 ocp_data;
-	int i;
 
 	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
 		return;
 
 	r8153_u1u2en(tp, false);
 
-	for (i = 0; i < 500; i++) {
-		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
-		    AUTOLOAD_DONE)
-			break;
-
-		msleep(20);
-		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
-			break;
-	}
+	wait_autoload_done(tp);
 
 	r8153_phy_status(tp, 0);
 
@@ -7504,22 +7496,12 @@ static void r8153_init(struct r8152 *tp)
 
 static void r8153b_init(struct r8152 *tp)
 {
-	int i;
-
 	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
 		return;
 
 	r8153b_u1u2en(tp, false);
 
-	for (i = 0; i < 500; i++) {
-		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
-		    AUTOLOAD_DONE)
-			break;
-
-		msleep(20);
-		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
-			break;
-	}
+	wait_autoload_done(tp);
 
 	r8153_phy_status(tp, 0);
 
@@ -7574,8 +7556,6 @@ static void r8153b_init(struct r8152 *tp)
 
 static void r8153c_init(struct r8152 *tp)
 {
-	int i;
-
 	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
 		return;
 
@@ -7588,15 +7568,8 @@ static void r8153c_init(struct r8152 *tp)
 
 	ocp_word_set_bits(tp, MCU_TYPE_USB, 0xcbf0, BIT(1));
 
-	for (i = 0; i < 500; i++) {
-		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
-		    AUTOLOAD_DONE)
-			break;
-
-		msleep(20);
-		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
-			return;
-	}
+	if (wait_autoload_done(tp))
+		return;
 
 	r8153_phy_status(tp, 0);
 
@@ -8493,90 +8466,63 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 	set_bit(PHY_RESET, &tp->flags);
 }
 
-static int r8159_wait_backup_restore(struct r8152 *tp)
-{
-	u32 ocp_data;
-
-	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
-	if (!(ocp_data & PCUT_STATUS))
-		return 0;
-
-	return poll_timeout_us(ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL),
-			       ocp_data & BACKUP_RESTRORE, 200, 20000, false);
-}
-
 static void r8156_init(struct r8152 *tp)
 {
-	u32 ocp_data;
 	u16 data;
-	int i;
 
 	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
 		return;
 
-	if (tp->version == RTL_VER_16 || tp->version == RTL_VER_17) {
-		ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xcffe, BIT(3));
-		ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xd3ca, BIT(0));
-	}
-
 	ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_ECM_OP, EN_ALL_SPEED);
 
-	if (tp->version < RTL_VER_16)
-		ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
+	ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
 
 	ocp_word_set_bits(tp, MCU_TYPE_USB, USB_ECM_OPTION, BYPASS_MAC_RESET);
 
-	if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_15)
+	switch (tp->version) {
+	case RTL_VER_12:
+	case RTL_VER_13:
+	case RTL_VER_15:
 		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_U2P3_CTRL, RX_DETECT8);
+		break;
+	default:
+		break;
+	}
 
 	r8153b_u1u2en(tp, false);
 
 	switch (tp->version) {
 	case RTL_VER_13:
 	case RTL_VER_15:
-	case RTL_VER_16:
-	case RTL_VER_17:
 		r8156b_wait_loading_flash(tp);
 		break;
 	default:
 		break;
 	}
 
-	for (i = 0; i < 500; i++) {
-		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
-		    AUTOLOAD_DONE)
-			break;
-
-		msleep(20);
-		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
-			return;
-	}
-
-	if (tp->version == RTL_VER_17 && r8159_wait_backup_restore(tp)) {
-		rtl_set_inaccessible(tp);
-		dev_err(&tp->intf->dev, "init failed, backup-restore timed out\n");
+	if (wait_autoload_done(tp))
 		return;
-	}
 
 	data = r8153_phy_status(tp, 0);
 	if (data == PHY_STAT_EXT_INIT) {
 		ocp_reg_clr_bits(tp, 0xa468, BIT(3) | BIT(1));
-		if (tp->version >= RTL_VER_12)
+
+		switch (tp->version) {
+		case RTL_VER_12:
+		case RTL_VER_13:
+		case RTL_VER_15:
 			ocp_reg_clr_bits(tp, 0xa466, BIT(0));
+			break;
+		default:
+			break;
+		}
 	}
 
-	data = r8152_mdio_read(tp, MII_BMCR);
-	if (data & BMCR_PDOWN) {
-		data &= ~BMCR_PDOWN;
-		r8152_mdio_write(tp, MII_BMCR, data);
-	}
+	r8152_mdio_test_and_clr_bit(tp, MII_BMCR, BMCR_PDOWN);
 
 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
 
-	if (tp->version >= RTL_VER_16)
-		r8157_u2p3en(tp, false);
-	else
-		r8153_u2p3en(tp, false);
+	r8153_u2p3en(tp, false);
 
 	/* MSC timer = 0xfff * 8ms = 32760 ms */
 	ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
@@ -8584,11 +8530,7 @@ static void r8156_init(struct r8152 *tp)
 	/* U1/U2/L1 idle timer. 500 us */
 	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
 
-	if (tp->version >= RTL_VER_16)
-		r8157_power_cut_en(tp, false);
-	else
-		r8153b_power_cut_en(tp, false);
-
+	r8153b_power_cut_en(tp, false);
 	r8156_ups_en(tp, false);
 	r8153_queue_wake(tp, false);
 	rtl_runtime_suspend_enable(tp, false);
@@ -8598,48 +8540,156 @@ static void r8156_init(struct r8152 *tp)
 
 	usb_enable_lpm(tp->udev);
 
-	if (tp->version < RTL_VER_16)
-		rtl_fc_pause_pkt_en(tp, 0);
+	rtl_fc_pause_pkt_en(tp, 0);
 
-	if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_15)
+	switch (tp->version) {
+	case RTL_VER_12:
+	case RTL_VER_13:
+	case RTL_VER_15:
 		/* Disable Auto Speed up */
 		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_FW_CTRL, AUTO_SPEEDUP);
+		break;
+	default:
+		break;
+	}
 
 	r8156_mac_clk_spd(tp, true);
 
-	if (tp->version < RTL_VER_16)
-		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, PLA_MCU_SPDWN_EN);
+	ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, PLA_MCU_SPDWN_EN);
 
-	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
 	if (rtl8152_get_speed(tp) & LINK_STATUS)
-		ocp_data |= CUR_LINK_OK;
+		ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS,
+				  CUR_LINK_OK | POLL_LINK_CHG);
 	else
-		ocp_data &= ~CUR_LINK_OK;
-	ocp_data |= POLL_LINK_CHG;
-	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
+		ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, CUR_LINK_OK,
+			      POLL_LINK_CHG);
+
+	set_bit(GREEN_ETHERNET, &tp->flags);
+
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL,
+			  RX_AGG_DISABLE | RX_ZERO_EN);
+
+	switch (tp->version) {
+	case RTL_TEST_01:
+	case RTL_VER_10:
+	case RTL_VER_11:
+		ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ACT_ODMA);
+		break;
+	default:
+		break;
+	}
+
+	r8156_mdio_force_mode(tp);
+	rtl_tally_reset(tp);
+
+	tp->coalesce = 15000;	/* 15 us */
+}
+
+static int r8159_wait_backup_restore(struct r8152 *tp)
+{
+	u32 ocp_data;
+
+	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
+	if (!(ocp_data & PCUT_STATUS))
+		return 0;
+
+	return poll_timeout_us(ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL),
+			       ocp_data & BACKUP_RESTRORE, 200, 20000, false);
+}
+
+static void r8157_init(struct r8152 *tp)
+{
+	u16 data;
+
+	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+		return;
+
+	/* Enable SW reset */
+	ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xcffe, BIT(3));
+	ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xd3ca, BIT(0));
+
+	ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_ECM_OP, EN_ALL_SPEED);
+
+	ocp_word_set_bits(tp, MCU_TYPE_USB, USB_ECM_OPTION, BYPASS_MAC_RESET);
+
+	r8153b_u1u2en(tp, false);
+
+	r8156b_wait_loading_flash(tp);
+
+	if (wait_autoload_done(tp))
+		return;
+
+	switch (tp->version) {
+	case RTL_VER_17:
+		if (r8159_wait_backup_restore(tp)) {
+			rtl_set_inaccessible(tp);
+			dev_err(&tp->intf->dev,
+				"init failed, backup-restore timed out\n");
+			return;
+		}
+		break;
+	default:
+		break;
+	}
+
+	data = r8153_phy_status(tp, 0);
+	if (data == PHY_STAT_EXT_INIT) {
+		ocp_reg_clr_bits(tp, 0xa468, BIT(3) | BIT(1));
+		ocp_reg_clr_bits(tp, 0xa466, BIT(0));
+	}
+
+	r8152_mdio_test_and_clr_bit(tp, MII_BMCR, BMCR_PDOWN);
+
+	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
+
+	r8157_u2p3en(tp, false);
+
+	/* MSC timer = 0xfff * 8ms = 32760 ms */
+	ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
+
+	/* U1/U2/L1 idle timer. 500 us */
+	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
+
+	r8157_power_cut_en(tp, false);
+	r8156_ups_en(tp, false);
+	r8153_queue_wake(tp, false);
+	rtl_runtime_suspend_enable(tp, false);
+
+	if (tp->udev->speed >= USB_SPEED_SUPER)
+		r8153b_u1u2en(tp, true);
+
+	usb_enable_lpm(tp->udev);
+
+	r8156_mac_clk_spd(tp, true);
+
+	if (rtl8152_get_speed(tp) & LINK_STATUS)
+		ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS,
+				  CUR_LINK_OK | POLL_LINK_CHG);
+	else
+		ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, CUR_LINK_OK,
+			      POLL_LINK_CHG);
 
 	set_bit(GREEN_ETHERNET, &tp->flags);
 
 	/* RX aggregation / 16 bytes RX descriptor
 	 * BIT(11) is specific to RTL8159, with unknown meaning
 	 */
-	if (tp->version == RTL_VER_17)
+	switch (tp->version) {
+	case RTL_VER_17:
 		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL,
 				  RX_AGG_DISABLE | RX_DESC_16B | BIT(11));
-	else if (tp->version == RTL_VER_16)
-		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, RX_AGG_DISABLE | RX_DESC_16B);
-	else
-		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, RX_AGG_DISABLE | RX_ZERO_EN);
+		break;
+	default:
+		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL,
+				  RX_AGG_DISABLE | RX_DESC_16B);
+		break;
+	}
 
-	if (tp->version < RTL_VER_12)
-		ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ACT_ODMA);
+	/* Disable Rx Zero Len */
+	rtl_bmu_clr_bits(tp, 0x2300, BIT(3));
 
-	if (tp->version >= RTL_VER_16) {
-		/* Disable Rx Zero Len */
-		rtl_bmu_clr_bits(tp, 0x2300, BIT(3));
-		/* TX descriptor Signature */
-		ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xd4ae, BIT(1));
-	}
+	/* TX descriptor Signature */
+	ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xd4ae, BIT(1));
 
 	r8156_mdio_force_mode(tp);
 	rtl_tally_reset(tp);
@@ -10009,7 +10059,7 @@ static int rtl_ops_init(struct r8152 *tp)
 		tp->eee_en		= true;
 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
 		tp->eee_adv2		= MDIO_EEE_2_5GT | MDIO_EEE_5GT;
-		ops->init		= r8156_init;
+		ops->init		= r8157_init;
 		ops->enable		= rtl8156_enable;
 		ops->disable		= rtl8153_disable;
 		ops->up			= rtl8156_up;
@@ -10031,7 +10081,7 @@ static int rtl_ops_init(struct r8152 *tp)
 		tp->eee_en		= true;
 		tp->eee_adv		= MDIO_EEE_100TX | MDIO_EEE_1000T | MDIO_EEE_10GT;
 		tp->eee_adv2		= MDIO_EEE_2_5GT | MDIO_EEE_5GT;
-		ops->init		= r8156_init;
+		ops->init		= r8157_init;
 		ops->enable		= rtl8156_enable;
 		ops->disable		= rtl8153_disable;
 		ops->up			= rtl8156_up;
Signature · 1 line
2.34.1
#10 lore raw

PATCH v10 6/12 r8152: add new init writes for RTL8156/8156B/8157/8159

In reply to Chih Kai Hsu

Bring the per-generation init functions in line with the vendor init sequence by adding writes the shared r8156_init() never performed.

- Clear PREBOOT_OPTION and RMT_WAKE_EN in r8156_init() and r8157_init() to drop stale values. RMT_WAKE_EN is the device's USB remote wakeup enable. The firmware sets it on the host's SET_FEATURE(FUNCTION_SUSPEND) or SET_FEATURE(DEVICE_REMOTE_WAKEUP) and clears it itself, so the driver never sets it.

- Add r8156b_u2phy_backup() for RTL8156B (VER_13/15).

- Clear AUTO_SPEEDUP in r8157_init().

- Disable interrupt mitigation (0xcf04) in r8157_init().

- Clear the Warm RST / Bus RST event flag (0xcd06 BIT(11)) in r8157_init().

- Clear the FW u1u2 patch option (0xb9a6 BIT(0)) and enable the u3phy patch backup (0xb9a2) for RTL8159 (VER_17) only.

- Clear RX_END_TRANSFER_EN (0xd406 BIT(11)) for RTL8157 (VER_16), too. While it is set, incoming packets are buffered in the RX FIFO and Bulk-in transfers are suspended. The bit is zero by default and isn't used now. Clear it unconditionally for safety.

---
 drivers/net/usb/r8152.c | 76 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 63 insertions(+), 13 deletions(-)
drivers/net/usb/r8152.c +63 -13
index a1e7a33b2f50e..7e553652f97ec 100644
@@ -78,6 +78,7 @@
 #define PLA_EEE_TXTWSYS_2P5G	0xe058
 #define PLA_EEEP_CR		0xe080
 #define PLA_MAC_PWR_CTRL	0xe0c0
+#define PLA_RMT_WAKE		0xe0c8
 #define PLA_MAC_PWR_CTRL2	0xe0ca
 #define PLA_MAC_PWR_CTRL3	0xe0cc
 #define PLA_MAC_PWR_CTRL4	0xe0ce
@@ -376,6 +377,9 @@
 #define MCU_CLK_RATIO_MASK	0x0f0f0f0f
 #define ALDPS_SPDWN_RATIO	0x0f87
 
+/* PLA_RMT_WAKE */
+#define RMT_WAKE_EN		BIT(0)
+
 /* PLA_MAC_PWR_CTRL2 */
 #define EEE_SPDWN_RATIO		0x8007
 #define MAC_CLK_SPDWN_EN	BIT(15)
@@ -422,6 +426,7 @@
 
 /* PLA_INDICATE_FALG */
 #define UPCOMING_RUNTIME_D3	BIT(0)
+#define PREBOOT_OPTION		BIT(1)
 
 /* PLA_MACDBG_PRE and PLA_MACDBG_POST */
 #define DEBUG_OE		BIT(0)
@@ -547,6 +552,7 @@
 #define RX_AGG_DISABLE		0x0010
 #define RX_ZERO_EN		0x0080
 #define RX_DESC_16B		0x0400
+#define RX_END_TRANSFER_EN	BIT(11)
 
 /* USB_U2P3_CTRL */
 #define U2P3_ENABLE		0x0001
@@ -8466,6 +8472,15 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 	set_bit(PHY_RESET, &tp->flags);
 }
 
+static void r8156b_u2phy_backup(struct r8152 *tp)
+{
+	ocp_write_word(tp, MCU_TYPE_USB, 0xd3ce, 0x181b);
+	ocp_write_dword(tp, MCU_TYPE_USB, 0xd3d0, 0x616ccd99);
+	ocp_write_dword(tp, MCU_TYPE_USB, 0xd3d4, 0x08fc8101);
+	ocp_write_dword(tp, MCU_TYPE_USB, 0xd3d8, 0x159b1100);
+	ocp_write_word(tp, MCU_TYPE_USB, 0xd3dc, 0x0a00);
+}
+
 static void r8156_init(struct r8152 *tp)
 {
 	u16 data;
@@ -8530,11 +8545,24 @@ static void r8156_init(struct r8152 *tp)
 	/* U1/U2/L1 idle timer. 500 us */
 	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
 
+	switch (tp->version) {
+	case RTL_VER_13:
+	case RTL_VER_15:
+		r8156b_u2phy_backup(tp);
+		break;
+	default:
+		break;
+	}
+
 	r8153b_power_cut_en(tp, false);
 	r8156_ups_en(tp, false);
 	r8153_queue_wake(tp, false);
 	rtl_runtime_suspend_enable(tp, false);
 
+	ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, PREBOOT_OPTION);
+
+	ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_RMT_WAKE, RMT_WAKE_EN);
+
 	if (tp->udev->speed >= USB_SPEED_SUPER)
 		r8153b_u1u2en(tp, true);
 
@@ -8644,6 +8672,10 @@ static void r8157_init(struct r8152 *tp)
 
 	r8157_u2p3en(tp, false);
 
+	/* Disable Interrupt Mitigation */
+	ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xcf04,
+			  BIT(0) | BIT(1) | BIT(2) | BIT(7));
+
 	/* MSC timer = 0xfff * 8ms = 32760 ms */
 	ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
 
@@ -8655,11 +8687,30 @@ static void r8157_init(struct r8152 *tp)
 	r8153_queue_wake(tp, false);
 	rtl_runtime_suspend_enable(tp, false);
 
+	ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, PREBOOT_OPTION);
+
+	ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_RMT_WAKE, RMT_WAKE_EN);
+
+	/* Clear Warm RST / Bus RST event flag */
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, 0xcd06, BIT(11));
+
+	switch (tp->version) {
+	case RTL_VER_17:
+		/* Disable FW u1u2 patch option */
+		ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xb9a6, BIT(0));
+		break;
+	default:
+		break;
+	}
+
 	if (tp->udev->speed >= USB_SPEED_SUPER)
 		r8153b_u1u2en(tp, true);
 
 	usb_enable_lpm(tp->udev);
 
+	/* Disable Auto Speed up */
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_FW_CTRL, AUTO_SPEEDUP);
+
 	r8156_mac_clk_spd(tp, true);
 
 	if (rtl8152_get_speed(tp) & LINK_STATUS)
@@ -8671,19 +8722,9 @@ static void r8157_init(struct r8152 *tp)
 
 	set_bit(GREEN_ETHERNET, &tp->flags);
 
-	/* RX aggregation / 16 bytes RX descriptor
-	 * BIT(11) is specific to RTL8159, with unknown meaning
-	 */
-	switch (tp->version) {
-	case RTL_VER_17:
-		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL,
-				  RX_AGG_DISABLE | RX_DESC_16B | BIT(11));
-		break;
-	default:
-		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL,
-				  RX_AGG_DISABLE | RX_DESC_16B);
-		break;
-	}
+	/* RX aggregation / 16 bytes RX descriptor / Bulk In End transfer */
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL,
+			  RX_AGG_DISABLE | RX_DESC_16B | RX_END_TRANSFER_EN);
 
 	/* Disable Rx Zero Len */
 	rtl_bmu_clr_bits(tp, 0x2300, BIT(3));
@@ -8691,6 +8732,15 @@ static void r8157_init(struct r8152 *tp)
 	/* TX descriptor Signature */
 	ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xd4ae, BIT(1));
 
+	switch (tp->version) {
+	case RTL_VER_17:
+		/* Enable u3phy patch backup */
+		ocp_write_word(tp, MCU_TYPE_USB, 0xb9a2, 0x0448);
+		break;
+	default:
+		break;
+	}
+
 	r8156_mdio_force_mode(tp);
 	rtl_tally_reset(tp);
 
Signature · 1 line
2.34.1
#11 lore raw

PATCH v10 7/12 r8152: accept PLA and USB firmware blocks for RTL8157 and RTL8159

In reply to Chih Kai Hsu

rtl8152_is_fw_mac_ok() has no RTL_VER_16 or RTL_VER_17 case, so any RTL_FW_PLA or RTL_FW_USB block in the firmware for these chips fails the check and the whole image is rejected with "check PLA firmware failed" or "check USB firmware failed".

Add both versions with the same layout as RTL_VER_14: 16 break points and USB_BP2_EN as the enable register.

Fixes a51e171e176c ("r8152: Add firmware upload capability for RTL8157/RTL8159")
---
 drivers/net/usb/r8152.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/net/usb/r8152.c +4 -0
index 7e553652f97ec..3e16d0f832bc3 100644
@@ -4904,6 +4904,8 @@ static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
 			max_bp = 8;
 			break;
 		case RTL_VER_14:
+		case RTL_VER_16:
+		case RTL_VER_17:
 			fw_reg = 0xf800;
 			bp_ba_addr = PLA_BP_BA;
 			bp_en_addr = USB_BP2_EN;
@@ -4932,6 +4934,8 @@ static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
 		case RTL_VER_13:
 		case RTL_VER_14:
 		case RTL_VER_15:
+		case RTL_VER_16:
+		case RTL_VER_17:
 			fw_reg = 0xe600;
 			bp_ba_addr = USB_BP_BA;
 			bp_en_addr = USB_BP2_EN;
Signature · 1 line
2.34.1
#12 lore raw

PATCH v10 8/12 r8152: split RTL_VER_17 into QFN68 and QFN100 package variants

In reply to Chih Kai Hsu

RTL8159 is sold in QFN68 and QFN100 packages, and only QFN68 needs the u2phy backup-restore patch and enables UPS (in a later patch). There was previously no way to tell the two apart at runtime, since both used the same RTL_VER_17.

Split RTL_VER_17 into RTL_VER_17_QFN68 and RTL_VER_17_QFN100 and update every switch/case on RTL_VER_17 in the driver to handle both. Detect the package by reading the new PLA_PKG_DET register (0xdc48, masked with PKG_MASK = 0x1e) via a new __rtl_get_pkg_det() helper, called from __rtl_get_hw_ver() when the chip ID is 0x2020:
pkg_det value 0x1e/0x1c selects QFN68 and 0x18/0x1a selects QFN100. A genuine RTL8159 reports one of these, so any other value is a part the driver does not know and logs "Unknown package". A failed read of the register is logged separately. Both yield RTL_VER_UNKNOWN, which rtl8152_probe() and the configuration selector reject with -ENODEV. Before this patch every 0x2020 chip became RTL_VER_17, so a chip whose package cannot be identified no longer binds to r8152.

Skipping only the firmware would not be enough, as the package also selects the QFN68-only u2phy backup-restore write in r8157_init() and, in a later patch, whether UPS is enabled. Guessing could apply them to a part that does not support them.

The package register is read independently by the configuration selector, rtl8152_probe() and r8153_ecm_probe(). If the selector's read succeeds and the one in rtl8152_probe() fails after its retries, the device is left in vendor mode without a driver. A genuine RTL8159 can only be treated as unknown through such a read failure, which means the device is in trouble anyway.

QFN100 uses a different firmware image from QFN68, so rtl_fw_init() selects rtl8159-2.fw for it and keeps rtl8159-1.fw for QFN68.

r8157_init() gains a QFN68-only write ("Enable u2phy backup restore patch", 0xb99c BIT(0)) that does not apply to QFN100.

---
 drivers/net/usb/r8152.c | 122 ++++++++++++++++++++++++++++++++--------
 1 file changed, 100 insertions(+), 22 deletions(-)
drivers/net/usb/r8152.c +100 -22
index 3e16d0f832bc3..103d8430da424 100644
@@ -64,6 +64,7 @@
 #define PLA_MACDBG_POST		0xd38e	/* RTL_VER_04 only */
 #define PLA_EXTRA_STATUS	0xd398
 #define PLA_GPHY_CTRL		0xd3ae
+#define PLA_PKG_DET		0xdc48
 #define PLA_POL_GPIO_CTRL	0xdc6a
 #define PLA_EFUSE_DATA		0xdd00
 #define PLA_EFUSE_CMD		0xdd02
@@ -291,6 +292,9 @@
 #define IFG_144NS		BIT(9)
 #define IFG_96NS		(BIT(9) | BIT(8))
 
+/* PLA_PKG_DET */
+#define PKG_MASK		0x1e
+
 /* PLA_MTPS */
 #define MTPS_JUMBO		(12 * 1024 / 64)
 #define MTPS_DEFAULT		(6 * 1024 / 64)
@@ -1258,7 +1262,8 @@ enum rtl_version {
 	RTL_VER_14,
 	RTL_VER_15,
 	RTL_VER_16,
-	RTL_VER_17,
+	RTL_VER_17_QFN68,
+	RTL_VER_17_QFN100,
 
 	RTL_VER_MAX
 };
@@ -3423,7 +3428,8 @@ static void rtl8152_nic_reset(struct r8152 *tp)
 		break;
 
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_CR, CR_RE | CR_TE);
 		break;
 
@@ -3463,7 +3469,7 @@ static void rtl_eee_plus_en(struct r8152 *tp, bool enable)
 
 static void rtl_set_eee_plus(struct r8152 *tp)
 {
-	if (tp->version == RTL_VER_17)
+	if (tp->version == RTL_VER_17_QFN68 || tp->version == RTL_VER_17_QFN100)
 		return rtl_eee_plus_en(tp, false);
 
 	if (rtl8152_get_speed(tp) & _10bps)
@@ -3651,7 +3657,8 @@ static void r8153_set_rx_early_timeout(struct r8152 *tp)
 	case RTL_VER_13:
 	case RTL_VER_15:
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
 			       640 / 8);
 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
@@ -3696,7 +3703,8 @@ static void r8153_set_rx_early_size(struct r8152 *tp)
 			       ocp_data / 8);
 		break;
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
 			       ocp_data / 16);
 		break;
@@ -3812,6 +3820,8 @@ static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
 	case RTL_VER_13:
 	case RTL_VER_15:
 	case RTL_VER_16:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 	default:
 		if (enable)
 			ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_RCR1,
@@ -4492,6 +4502,8 @@ static void r8153_teredo_off(struct r8152 *tp)
 	case RTL_VER_14:
 	case RTL_VER_15:
 	case RTL_VER_16:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 	default:
 		/* The bit 0 ~ 7 are relative with teredo settings. They are
 		 * W1C (write 1 to clear), so set all 1 to disable it.
@@ -4546,7 +4558,8 @@ static void rtl_clear_bp(struct r8152 *tp, u16 type)
 		break;
 	case RTL_VER_14:
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 	default:
 		ocp_write_word(tp, type, USB_BP2_EN, 0);
 		bp_num = 16;
@@ -4658,7 +4671,8 @@ static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_
 	case RTL_VER_13:
 	case RTL_VER_15:
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 	default:
 		break;
 	}
@@ -4905,7 +4919,8 @@ static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
 			break;
 		case RTL_VER_14:
 		case RTL_VER_16:
-		case RTL_VER_17:
+		case RTL_VER_17_QFN68:
+		case RTL_VER_17_QFN100:
 			fw_reg = 0xf800;
 			bp_ba_addr = PLA_BP_BA;
 			bp_en_addr = USB_BP2_EN;
@@ -4935,7 +4950,8 @@ static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
 		case RTL_VER_14:
 		case RTL_VER_15:
 		case RTL_VER_16:
-		case RTL_VER_17:
+		case RTL_VER_17_QFN68:
+		case RTL_VER_17_QFN100:
 			fw_reg = 0xe600;
 			bp_ba_addr = USB_BP_BA;
 			bp_en_addr = USB_BP2_EN;
@@ -5822,7 +5838,8 @@ static void rtl_eee_enable(struct r8152 *tp, bool enable)
 	case RTL_VER_13:
 	case RTL_VER_15:
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		if (enable) {
 			r8156_eee_en(tp, true);
 			ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
@@ -6013,7 +6030,8 @@ static void rtl_fc_pause_pkt_en(struct r8152 *tp, u16 speed)
 		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
 		break;
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, SLOT_EN);
 
 		num_pause_pkts = 0xa;
@@ -6512,7 +6530,7 @@ static int rtl8156_enable(struct r8152 *tp)
 	set_tx_qlen(tp);
 	rtl_set_eee_plus(tp);
 
-	if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_17)
+	if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_17_QFN100)
 		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, RX_AGGR_NUM_MASK);
 
 	r8153_set_rx_early_timeout(tp);
@@ -7008,7 +7026,8 @@ static void rtl8156_up(struct r8152 *tp)
 		ocp_word_set_bits(tp, MCU_TYPE_USB, 0xd3ca, BIT(15));
 		break;
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		/* Enable Clear_SDR */
 		ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xd378, BIT(7));
 		/* Clear SDR bit */
@@ -7052,7 +7071,8 @@ static void rtl8156_down(struct r8152 *tp)
 
 	switch (tp->version) {
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1526);
 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64);
 		break;
@@ -8258,7 +8278,8 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 		sram2_write_w0w1(tp, 0x809d, 0xff00, 0x5000);
 		break;
 
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		/* Disable bypass turn off clk in ALDPS */
 		ocp_byte_clr_bits(tp, MCU_TYPE_PLA, 0xd3c8, BIT(0));
 
@@ -8652,7 +8673,8 @@ static void r8157_init(struct r8152 *tp)
 		return;
 
 	switch (tp->version) {
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		if (r8159_wait_backup_restore(tp)) {
 			rtl_set_inaccessible(tp);
 			dev_err(&tp->intf->dev,
@@ -8699,7 +8721,8 @@ static void r8157_init(struct r8152 *tp)
 	ocp_word_clr_bits(tp, MCU_TYPE_USB, 0xcd06, BIT(11));
 
 	switch (tp->version) {
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		/* Disable FW u1u2 patch option */
 		ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xb9a6, BIT(0));
 		break;
@@ -8737,7 +8760,11 @@ static void r8157_init(struct r8152 *tp)
 	ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xd4ae, BIT(1));
 
 	switch (tp->version) {
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+		/* Enable u2phy backup restore patch */
+		ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xb99c, BIT(0));
+		fallthrough;
+	case RTL_VER_17_QFN100:
 		/* Enable u3phy patch backup */
 		ocp_write_word(tp, MCU_TYPE_USB, 0xb9a2, 0x0448);
 		break;
@@ -10131,7 +10158,8 @@ static int rtl_ops_init(struct r8152 *tp)
 		r8157_desc_init(tp);
 		break;
 
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		tp->eee_en		= true;
 		tp->eee_adv		= MDIO_EEE_100TX | MDIO_EEE_1000T | MDIO_EEE_10GT;
 		tp->eee_adv2		= MDIO_EEE_2_5GT | MDIO_EEE_5GT;
@@ -10172,6 +10200,7 @@ static int rtl_ops_init(struct r8152 *tp)
 #define FIRMWARE_8156B_2	"rtl_nic/rtl8156b-2.fw"
 #define FIRMWARE_8157_1		"rtl_nic/rtl8157-1.fw"
 #define FIRMWARE_8159_1		"rtl_nic/rtl8159-1.fw"
+#define FIRMWARE_8159_2		"rtl_nic/rtl8159-2.fw"
 
 MODULE_FIRMWARE(FIRMWARE_8153A_2);
 MODULE_FIRMWARE(FIRMWARE_8153A_3);
@@ -10182,6 +10211,7 @@ MODULE_FIRMWARE(FIRMWARE_8156A_2);
 MODULE_FIRMWARE(FIRMWARE_8156B_2);
 MODULE_FIRMWARE(FIRMWARE_8157_1);
 MODULE_FIRMWARE(FIRMWARE_8159_1);
+MODULE_FIRMWARE(FIRMWARE_8159_2);
 
 static int rtl_fw_init(struct r8152 *tp)
 {
@@ -10223,9 +10253,12 @@ static int rtl_fw_init(struct r8152 *tp)
 	case RTL_VER_16:
 		rtl_fw->fw_name		= FIRMWARE_8157_1;
 		break;
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
 		rtl_fw->fw_name		= FIRMWARE_8159_1;
 		break;
+	case RTL_VER_17_QFN100:
+		rtl_fw->fw_name		= FIRMWARE_8159_2;
+		break;
 	default:
 		break;
 	}
@@ -10233,9 +10266,40 @@ static int rtl_fw_init(struct r8152 *tp)
 	return 0;
 }
 
+static int __rtl_get_pkg_det(struct usb_device *udev, u32 *pkg_det)
+{
+	__le32 *tmp;
+	int ret, i;
+
+	tmp = kmalloc_obj(*tmp);
+	if (!tmp)
+		return -ENOMEM;
+
+	for (i = 0, ret = 0; i < 3 && ret != 4; i++)
+		ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
+				      RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
+				      PLA_PKG_DET, MCU_TYPE_PLA, tmp,
+				      sizeof(*tmp), USB_CTRL_GET_TIMEOUT);
+
+	if (ret == sizeof(*tmp)) {
+		if (i != 1)
+			dev_warn(&udev->dev,
+				 "Needed %d retries to read package\n", i - 1);
+
+		*pkg_det = __le32_to_cpu(*tmp) & PKG_MASK;
+		ret = 0;
+	} else if (ret >= 0) {
+		ret = -EIO;
+	}
+
+	kfree(tmp);
+	return ret;
+}
+
 static u8 __rtl_get_hw_ver(struct usb_device *udev)
 {
 	u32 ocp_data = 0;
+	u32 pkg_det = 0;
 	__le32 *tmp;
 	u8 version;
 	int ret;
@@ -10318,7 +10382,20 @@ static u8 __rtl_get_hw_ver(struct usb_device *udev)
 		version = RTL_VER_16;
 		break;
 	case 0x2020:
-		version = RTL_VER_17;
+		ret = __rtl_get_pkg_det(udev, &pkg_det);
+		if (ret < 0) {
+			version = RTL_VER_UNKNOWN;
+			dev_err(&udev->dev, "Failed to read package: %d\n",
+				ret);
+		} else if (pkg_det == 0x1e || pkg_det == 0x1c) {
+			version = RTL_VER_17_QFN68;
+		} else if (pkg_det == 0x18 || pkg_det == 0x1a) {
+			version = RTL_VER_17_QFN100;
+		} else {
+			version = RTL_VER_UNKNOWN;
+			dev_err(&udev->dev, "Unknown package 0x%04x\n",
+				pkg_det);
+		}
 		break;
 	default:
 		version = RTL_VER_UNKNOWN;
@@ -10477,7 +10554,8 @@ static int rtl8152_probe_once(struct usb_interface *intf,
 	case RTL_VER_13:
 	case RTL_VER_15:
 	case RTL_VER_16:
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
+	case RTL_VER_17_QFN100:
 		netdev->max_mtu = size_to_mtu(16 * 1024);
 		break;
 	case RTL_VER_01:
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH net-next v10 08/12] r8152: split RTL_VER_17 into QFN68 and QFN100 package variants

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] This patch silently changes which firmware every existing QFN100 RTL8159 user loads, and the commit message does not explain the…

--

r8152: split RTL_VER_17 into QFN68 and QFN100 package variants

This splits RTL_VER_17 into RTL_VER_17_QFN68 and RTL_VER_17_QFN100. The package is detected by reading PLA_PKG_DET in __rtl_get_hw_ver(), and parts that cannot be identified are rejected. It also selects a new rtl8159-2.fw image for QFN100 and adds a QFN68-only u2phy backup-restore write in r8157_init().

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
index 3e16d0f832bc3..103d8430da424 100644

[ ... ]

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
@@ -10223,9 +10253,12 @@ static int rtl_fw_init(struct r8152 *tp)
 	case RTL_VER_16:
 		rtl_fw->fw_name		= FIRMWARE_8157_1;
 		break;

drivers/net/usb/r8152.c static int rtl_fw_init(struct r8152 *tp) · line 10264

Quoted 10 lines
drivers/net/usb/r8152.c +4 -1
-	case RTL_VER_17:
+	case RTL_VER_17_QFN68:
 		rtl_fw->fw_name		= FIRMWARE_8159_1;
 		break;
+	case RTL_VER_17_QFN100:
+		rtl_fw->fw_name		= FIRMWARE_8159_2;
+		break;
 	default:
 		break;
 	}

[Severity: Medium]
This changes the firmware that existing QFN100 RTL8159 users load. Before this patch every 0x2020 chip became RTL_VER_17, so QFN100 parts loaded rtl8159-1.fw. After it, QFN100 requests rtl8159-2.fw, and nothing falls back to rtl8159-1.fw.

The commit message only says:

QFN100 uses a different firmware image from QFN68, so rtl_fw_init() selects rtl8159-2.fw for it and keeps rtl8159-1.fw for QFN68.

RTL8159 firmware loading has been in the tree since commit a51e171e176c ("r8152: Add firmware upload capability for RTL8157/RTL8159"). Released kernels have therefore been applying rtl8159-1.fw to QFN100 parts.

Was that harmful on QFN100? If so, this patch fixes a user-visible bug. Should the commit message then describe the symptom and carry a Fixes:
tag?

This also adds a dependency on a new linux-firmware blob. If the blob is missing, rtl8152_request_firmware() only warns:

	if (rc) {
		rtl_fw->fw = ERR_PTR(rc);

		dev_warn(&tp->intf->dev,
			 "unable to load firmware patch %s (%ld)\n",
			 rtl_fw->fw_name, rc);
	}

Commit a51e171e176c says the RTL8159 needs its PHY firmware to work at speeds above 5GBit. A QFN100 user who upgrades the kernel without rtl8159-2.fw installed goes from having a PHY firmware patch to having none, and only sees a warning in the log. Can that drop links above 5G on QFN100?

Could the commit message mention the new rtl8159-2.fw requirement and whether it is already in linux-firmware? If rtl8159-1.fw was not harmful on QFN100, would a fallback to it be worth adding when rtl8159-2.fw cannot be loaded?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1-nic_swsd%40realtek.com
#14 lore raw

RE: [PATCH net-next v10 08/12] r8152: split RTL_VER_17 into QFN68 and QFN100 package variants

Quoted 81 lines

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] This patch silently changes which firmware every existing QFN100 RTL8159 user loads, and the commit message does not explain the…

--

r8152: split RTL_VER_17 into QFN68 and QFN100 package variants

This splits RTL_VER_17 into RTL_VER_17_QFN68 and RTL_VER_17_QFN100. The package is detected by reading PLA_PKG_DET in __rtl_get_hw_ver(), and parts that cannot be identified are rejected. It also selects a new rtl8159-2.fw image for QFN100 and adds a QFN68-only u2phy backup-restore write in r8157_init().

Quoted 4 lines
drivers/net/usb/r8152.c index renamed from drivers/net/usb/r8152.c +0 -0

3e16d0f832bc3..103d8430da424 100644

drivers/net/usb/r8152.c +0 -0

[ ... ]

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
@@ -10223,9 +10253,12 @@ static int rtl_fw_init(struct r8152 *tp)
      case RTL_VER_16:
              rtl_fw->fw_name         = FIRMWARE_8157_1;
              break;

drivers/net/usb/r8152.c static int rtl_fw_init(struct r8152 *tp) · line 10264

Quoted 10 lines
drivers/net/usb/r8152.c +4 -1
-     case RTL_VER_17:
+     case RTL_VER_17_QFN68:
              rtl_fw->fw_name         = FIRMWARE_8159_1;
              break;
+     case RTL_VER_17_QFN100:
+             rtl_fw->fw_name         = FIRMWARE_8159_2;
+             break;
      default:
              break;
      }

[Severity: Medium]
This changes the firmware that existing QFN100 RTL8159 users load. Before this patch every 0x2020 chip became RTL_VER_17, so QFN100 parts loaded rtl8159-1.fw. After it, QFN100 requests rtl8159-2.fw, and nothing falls back to rtl8159-1.fw.

The commit message only says:

QFN100 uses a different firmware image from QFN68, so rtl_fw_init() selects rtl8159-2.fw for it and keeps rtl8159-1.fw for QFN68.

RTL8159 firmware loading has been in the tree since commit a51e171e176c ("r8152: Add firmware upload capability for RTL8157/RTL8159"). Released kernels have therefore been applying rtl8159-1.fw to QFN100 parts.

Was that harmful on QFN100? If so, this patch fixes a user-visible bug. Should the commit message then describe the symptom and carry a Fixes:
tag?

This also adds a dependency on a new linux-firmware blob. If the blob is missing, rtl8152_request_firmware() only warns:

        if (rc) {
                rtl_fw->fw = ERR_PTR(rc);

                dev_warn(&tp->intf->dev,
                         "unable to load firmware patch %s (%ld)\n",
                         rtl_fw->fw_name, rc);
        }

Commit a51e171e176c says the RTL8159 needs its PHY firmware to work at speeds above 5GBit. A QFN100 user who upgrades the kernel without rtl8159-2.fw installed goes from having a PHY firmware patch to having none, and only sees a warning in the log. Can that drop links above 5G on QFN100?

Quoted 4 lines

Could the commit message mention the new rtl8159-2.fw requirement and whether it is already in linux-firmware? If rtl8159-1.fw was not harmful on QFN100, would a fallback to it be worth adding when rtl8159-2.fw cannot be loaded?

The QFN68 image has not been validated on QFN100. There is no fallback to it. If rtl8159-2.fw cannot be loaded, the driver warns and continues without PHY firmware, as for any other missing image.

Quoted 7 lines

[ ... ]

--
Sashiko AI review ·
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1-
nic_swsd%40realtek.com

Best Regards,
Chih-Kai

#15 lore raw

PATCH v10 9/12 r8152: split rtl8156_enable/up/down into per-generation functions

In reply to Chih Kai Hsu

rtl8156_enable(), rtl8156_up() and rtl8156_down() had accumulated tp->version range checks to cover RTL8156, RTL8156B, and RTL8157/RTL8159, including an early bail-out in rtl8156_enable() that skipped most of the function body for RTL8157/RTL8159. The result hides which settings belong to which chip, so chip-specific register differences cannot be reviewed, and cannot be added without touching the other families.

Give each generation its own enable callback: rtl8156_enable() for RTL8156 (VER_10/11), rtl8156b_enable() for RTL8156B (VER_12/13/15), and rtl8157_enable() for RTL8157/RTL8159 (VER_16/17). RTL8157/RTL8159 also get their own rtl8157_up()/rtl8157_down(); RTL8156 and RTL8156B still share rtl8156_up()/rtl8156_down().

The four chips share most of the up/down sequence, so move it into r8156_exit_oob() and r8156_enter_oob(), as RTL8152B/8153 do. Likewise, rtl8156_enable() and rtl8156b_enable() share the idle spin-down and L1 NYET steps, so move those into small helpers. The FC patch restart is also done by rtl8153_enable(), so its helper, rtl_fc_patch_restart(), is shared with it.

Each extracted function reproduces what its chips already did, with the guards that were true for them inlined and the ones that were false dropped as unreachable.

No functional change intended.

---
 drivers/net/usb/r8152.c | 290 +++++++++++++++++++++++++---------------
 1 file changed, 185 insertions(+), 105 deletions(-)
drivers/net/usb/r8152.c +185 -105
index 103d8430da424..a16e7d0257f2a 100644
@@ -3714,6 +3714,13 @@ static void r8153_set_rx_early_size(struct r8152 *tp)
 	}
 }
 
+static void rtl_fc_patch_restart(struct r8152 *tp)
+{
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
+	usleep_range(1000, 2000);
+	ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
+}
+
 static int rtl8153_enable(struct r8152 *tp)
 {
 	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
@@ -3729,9 +3736,7 @@ static int rtl8153_enable(struct r8152 *tp)
 	switch (tp->version) {
 	case RTL_VER_09:
 	case RTL_VER_14:
-		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
-		usleep_range(1000, 2000);
-		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
+		rtl_fc_patch_restart(tp);
 		break;
 	default:
 		break;
@@ -6517,6 +6522,28 @@ static void r8156_fc_parameter(struct r8152 *tp)
 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16);
 }
 
+static void r8156_idle_spdwn_en(struct r8152 *tp, u16 speed)
+{
+	if (speed & _2500bps)
+		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
+				  IDLE_SPDWN_EN);
+	else
+		ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
+				  IDLE_SPDWN_EN);
+}
+
+static void r8156_l1_nyet_hird(struct r8152 *tp, u16 speed)
+{
+	if (tp->udev->speed != USB_SPEED_HIGH)
+		return;
+
+	/* USB 0xb45e[3:0] l1_nyet_hird */
+	if (is_flow_control(speed))
+		ocp_word_w0w1(tp, MCU_TYPE_USB, USB_L1_CTRL, 0xf, 0xf);
+	else
+		ocp_word_w0w1(tp, MCU_TYPE_USB, USB_L1_CTRL, 0xf, 0x1);
+}
+
 static int rtl8156_enable(struct r8152 *tp)
 {
 	u16 speed;
@@ -6524,53 +6551,50 @@ static int rtl8156_enable(struct r8152 *tp)
 	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
 		return -ENODEV;
 
-	if (tp->version < RTL_VER_12)
-		r8156_fc_parameter(tp);
+	r8156_fc_parameter(tp);
 
 	set_tx_qlen(tp);
 	rtl_set_eee_plus(tp);
-
-	if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_17_QFN100)
-		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, RX_AGGR_NUM_MASK);
-
 	r8153_set_rx_early_timeout(tp);
 	r8153_set_rx_early_size(tp);
 
 	speed = rtl8152_get_speed(tp);
+	rtl_set_ifg(tp, speed);
 
-	if (tp->version >= RTL_VER_16)
-		rtl_fc_pause_pkt_en(tp, speed);
+	r8156_idle_spdwn_en(tp, speed);
 
-	rtl_set_ifg(tp, speed);
+	if (speed & _1000bps)
+		ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11);
+	else if (speed & _500bps)
+		ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d);
 
-	if (tp->version >= RTL_VER_16)
-		return rtl_enable(tp);
+	r8156_l1_nyet_hird(tp, speed);
+	rtl_fc_patch_restart(tp);
 
-	if (speed & _2500bps)
-		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
-				  IDLE_SPDWN_EN);
-	else
-		ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
-				  IDLE_SPDWN_EN);
+	return rtl_enable(tp);
+}
 
-	if (tp->version < RTL_VER_12) {
-		if (speed & _1000bps)
-			ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11);
-		else if (speed & _500bps)
-			ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d);
-	}
+static int rtl8156b_enable(struct r8152 *tp)
+{
+	u16 speed;
 
-	if (tp->udev->speed == USB_SPEED_HIGH) {
-		/* USB 0xb45e[3:0] l1_nyet_hird */
-		if (is_flow_control(speed))
-			ocp_word_w0w1(tp, MCU_TYPE_USB, USB_L1_CTRL, 0xf, 0xf);
-		else
-			ocp_word_w0w1(tp, MCU_TYPE_USB, USB_L1_CTRL, 0xf, 0x1);
-	}
+	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+		return -ENODEV;
 
-	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
-	usleep_range(1000, 2000);
-	ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK);
+	set_tx_qlen(tp);
+	rtl_set_eee_plus(tp);
+
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, RX_AGGR_NUM_MASK);
+
+	r8153_set_rx_early_timeout(tp);
+	r8153_set_rx_early_size(tp);
+
+	speed = rtl8152_get_speed(tp);
+	rtl_set_ifg(tp, speed);
+
+	r8156_idle_spdwn_en(tp, speed);
+	r8156_l1_nyet_hird(tp, speed);
+	rtl_fc_patch_restart(tp);
 
 	return rtl_enable(tp);
 }
@@ -6583,6 +6607,29 @@ static void rtl8156_disable(struct r8152 *tp)
 	rtl8153_disable(tp);
 }
 
+static int rtl8157_enable(struct r8152 *tp)
+{
+	u16 speed;
+
+	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+		return -ENODEV;
+
+	set_tx_qlen(tp);
+	rtl_set_eee_plus(tp);
+
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, RX_AGGR_NUM_MASK);
+
+	r8153_set_rx_early_timeout(tp);
+	r8153_set_rx_early_size(tp);
+
+	speed = rtl8152_get_speed(tp);
+	rtl_fc_pause_pkt_en(tp, speed);
+
+	rtl_set_ifg(tp, speed);
+
+	return rtl_enable(tp);
+}
+
 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
 			     u32 advertising)
 {
@@ -6937,38 +6984,8 @@ static void rtl8156_change_mtu(struct r8152 *tp)
 		       ALIGN(rx_max_size + tp->tx_desc.size, 1024) / 16);
 }
 
-static void rtl8157_change_mtu(struct r8152 *tp)
+static void r8156_exit_oob(struct r8152 *tp)
 {
-	u32 max_pkt_size = mtu_to_size(tp->netdev->mtu);
-	u32 ocp_data;
-
-	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, max_pkt_size);
-
-	/* Use at least 10K for MTPS */
-	ocp_data = max_t(u32, max_pkt_size, 10 * 1024) / 64;
-
-	/* 16 * 1024 / 64 = 0x100, so the max is 0xff for 8 bits data */
-	ocp_data = min_t(u32, ocp_data, 0xff);
-
-	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, ocp_data);
-	r8156_fc_parameter(tp);
-
-	/* TX share fifo free credit full threshold */
-	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
-	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL,
-		       ALIGN(max_pkt_size + tp->tx_desc.size, 1024) / 16);
-}
-
-static void rtl8156_up(struct r8152 *tp)
-{
-	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
-		return;
-
-	r8153b_u1u2en(tp, false);
-	if (tp->version < RTL_VER_16)
-		r8153_u2p3en(tp, false);
-	r8153_aldps_en(tp, false);
-
 	rxdy_gated_en(tp, true);
 	r8153_teredo_off(tp);
 
@@ -7000,10 +7017,20 @@ static void rtl8156_up(struct r8152 *tp)
 	/* share FIFO settings */
 	ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, RXFIFO_FULL_MASK,
 		      0x08);
+}
 
-	if (tp->version < RTL_VER_16)
-		ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
-				  PLA_MCU_SPDWN_EN);
+static void rtl8156_up(struct r8152 *tp)
+{
+	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+		return;
+
+	r8153b_u1u2en(tp, false);
+	r8153_u2p3en(tp, false);
+	r8153_aldps_en(tp, false);
+
+	r8156_exit_oob(tp);
+
+	ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, PLA_MCU_SPDWN_EN);
 
 	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_SPEED_OPTION,
 			  RG_PWRDN_EN | ALL_SPEED_OFF);
@@ -7016,8 +7043,7 @@ static void rtl8156_up(struct r8152 *tp)
 	}
 
 	r8153_aldps_en(tp, true);
-	if (tp->version < RTL_VER_16)
-		r8153_u2p3en(tp, true);
+	r8153_u2p3en(tp, true);
 
 	switch (tp->version) {
 	case RTL_VER_13:
@@ -7025,40 +7051,16 @@ static void rtl8156_up(struct r8152 *tp)
 		/* Enable Clear_SDR */
 		ocp_word_set_bits(tp, MCU_TYPE_USB, 0xd3ca, BIT(15));
 		break;
-	case RTL_VER_16:
-	case RTL_VER_17_QFN68:
-	case RTL_VER_17_QFN100:
-		/* Enable Clear_SDR */
-		ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xd378, BIT(7));
-		/* Clear SDR bit */
-		ocp_word_clr_bits(tp, MCU_TYPE_USB, 0xcd06, BIT(15));
-		break;
 	default:
 		break;
 	}
 
-	if (tp->version < RTL_VER_16 && tp->udev->speed >= USB_SPEED_SUPER)
+	if (tp->udev->speed >= USB_SPEED_SUPER)
 		r8153b_u1u2en(tp, true);
 }
 
-static void rtl8156_down(struct r8152 *tp)
+static void r8156_enter_oob(struct r8152 *tp)
 {
-	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
-		rtl_drop_queued_tx(tp);
-		return;
-	}
-
-	if (tp->version < RTL_VER_16)
-		ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
-				  PLA_MCU_SPDWN_EN);
-
-	r8153b_u1u2en(tp, false);
-	if (tp->version < RTL_VER_16) {
-		r8153_u2p3en(tp, false);
-		r8153b_power_cut_en(tp, false);
-	}
-	r8153_aldps_en(tp, false);
-
 	ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB);
 
 	/* RX FIFO settings for OOB */
@@ -7100,7 +7102,85 @@ static void rtl8156_down(struct r8152 *tp)
 
 	ocp_dword_set_bits(tp, MCU_TYPE_PLA, PLA_RCR,
 			   RCR_APM | RCR_AM | RCR_AB);
+}
+
+static void rtl8156_down(struct r8152 *tp)
+{
+	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
+		rtl_drop_queued_tx(tp);
+		return;
+	}
 
+	ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
+			  PLA_MCU_SPDWN_EN);
+
+	r8153b_u1u2en(tp, false);
+	r8153_u2p3en(tp, false);
+	r8153b_power_cut_en(tp, false);
+	r8153_aldps_en(tp, false);
+	r8156_enter_oob(tp);
+	r8153_aldps_en(tp, true);
+}
+
+static void rtl8157_change_mtu(struct r8152 *tp)
+{
+	u32 max_pkt_size = mtu_to_size(tp->netdev->mtu);
+	u32 ocp_data;
+
+	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, max_pkt_size);
+
+	/* Use at least 10K for MTPS */
+	ocp_data = max_t(u32, max_pkt_size, 10 * 1024) / 64;
+
+	/* 16 * 1024 / 64 = 0x100, so the max is 0xff for 8 bits data */
+	ocp_data = min_t(u32, ocp_data, 0xff);
+
+	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, ocp_data);
+	r8156_fc_parameter(tp);
+
+	/* TX share fifo free credit full threshold */
+	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
+	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL,
+		       ALIGN(max_pkt_size + tp->tx_desc.size, 1024) / 16);
+}
+
+static void rtl8157_up(struct r8152 *tp)
+{
+	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+		return;
+
+	r8153b_u1u2en(tp, false);
+	r8153_aldps_en(tp, false);
+	r8156_exit_oob(tp);
+
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_SPEED_OPTION,
+			  RG_PWRDN_EN | ALL_SPEED_OFF);
+
+	ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400);
+
+	if (tp->saved_wolopts != __rtl_get_wol(tp)) {
+		netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n");
+		__rtl_set_wol(tp, tp->saved_wolopts);
+	}
+
+	r8153_aldps_en(tp, true);
+
+	/* Enable Clear_SDR */
+	ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xd378, BIT(7));
+	/* Clear SDR bit */
+	ocp_word_clr_bits(tp, MCU_TYPE_USB, 0xcd06, BIT(15));
+}
+
+static void rtl8157_down(struct r8152 *tp)
+{
+	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
+		rtl_drop_queued_tx(tp);
+		return;
+	}
+
+	r8153b_u1u2en(tp, false);
+	r8153_aldps_en(tp, false);
+	r8156_enter_oob(tp);
 	r8153_aldps_en(tp, true);
 }
 
@@ -10102,7 +10182,7 @@ static int rtl_ops_init(struct r8152 *tp)
 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
 		tp->eee_adv2		= MDIO_EEE_2_5GT;
 		ops->init		= r8156_init;
-		ops->enable		= rtl8156_enable;
+		ops->enable		= rtl8156b_enable;
 		ops->disable		= rtl8153_disable;
 		ops->up			= rtl8156_up;
 		ops->down		= rtl8156_down;
@@ -10141,10 +10221,10 @@ static int rtl_ops_init(struct r8152 *tp)
 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
 		tp->eee_adv2		= MDIO_EEE_2_5GT | MDIO_EEE_5GT;
 		ops->init		= r8157_init;
-		ops->enable		= rtl8156_enable;
+		ops->enable		= rtl8157_enable;
 		ops->disable		= rtl8153_disable;
-		ops->up			= rtl8156_up;
-		ops->down		= rtl8156_down;
+		ops->up			= rtl8157_up;
+		ops->down		= rtl8157_down;
 		ops->unload		= rtl8157_unload;
 		ops->eee_get		= r8153_get_eee;
 		ops->eee_set		= r8152_set_eee;
@@ -10164,10 +10244,10 @@ static int rtl_ops_init(struct r8152 *tp)
 		tp->eee_adv		= MDIO_EEE_100TX | MDIO_EEE_1000T | MDIO_EEE_10GT;
 		tp->eee_adv2		= MDIO_EEE_2_5GT | MDIO_EEE_5GT;
 		ops->init		= r8157_init;
-		ops->enable		= rtl8156_enable;
+		ops->enable		= rtl8157_enable;
 		ops->disable		= rtl8153_disable;
-		ops->up			= rtl8156_up;
-		ops->down		= rtl8156_down;
+		ops->up			= rtl8157_up;
+		ops->down		= rtl8157_down;
 		ops->unload		= rtl8157_unload;
 		ops->eee_get		= r8153_get_eee;
 		ops->eee_set		= r8152_set_eee;
Signature · 1 line
2.34.1
#16 lore raw

PATCH v10 10/12 r8152: split r8157_hw_phy_cfg into RTL8157 and RTL8159 variants

In reply to Chih Kai Hsu

r8157_hw_phy_cfg() served both RTL8157 (VER_16) and RTL8159 (VER_17_QFN68/QFN100) via a tp->version switch inside a single function, but per the datasheet RTL8159's PHY tuning differs from RTL8157's in more than the per-version blocks already captured.

Split it into r8157_hw_phy_cfg() (VER_16 only) and a new r8159_hw_phy_cfg() (VER_17_QFN68/QFN100), each with its own copy of the shared prologue/epilogue. rtl_ops_init() assigns each one to its own chips only. Per the datasheet, RTL8159 diverges from RTL8157 in this split in several ways:

- RTL8159 doesn't need EEE disabled before reprogramming PHY parameters, so r8159_hw_phy_cfg() drops the rtl_eee_enable(tp, false) call that r8157_hw_phy_cfg() keeps.
- Since EEE isn't forced off up front, r8159_hw_phy_cfg() ends with rtl_eee_enable(tp, tp->eee_en) instead of only enabling EEE when eee_en is set, so the configured state is applied in both directions.
- RTL8159's EEE clock-divider and 0xa5ea encoding differ from RTL8157's: r8159_hw_phy_cfg() clears EN_10M_CLKDIV (ups_info. _10m_ckdiv = false) instead of setting it, and clears an extra bit (BIT(2)) of 0xa5ea.
- "Disable bypass_turn_off_clk_in_aldps" (0xd3c8 BIT(0)) is needed by both chips, so it moves from the VER_17-only switch case into the shared prologue and now also runs for RTL8157.

Newly added PHY tuning parameters for RTL8159:
- shorten CLKS latency
- CMP_Timer on MP_Timer=333
- spdchg_pga1_lpf_cap
- ENET PLL jitter improvement

Moreover, change sram2 0x8ffe[15:0] = 0xf450 to 0xf4ff.

Along the way, add sram2_set_bits()/sram2_clr_bits() helpers mirroring the existing sram_set_bits()/sram_clr_bits(), and use the named OCP_POWER_CFG/EN_10M_PLLOFF/EN_ALDPS_PLLOFF macros in place of the raw "ocp_reg_set_bits(tp, 0xa430, BIT(0) | BIT(1))".

---
 drivers/net/usb/r8152.c | 168 ++++++++++++++++++++++++++++++++--------
 1 file changed, 137 insertions(+), 31 deletions(-)
drivers/net/usb/r8152.c +137 -31
index a16e7d0257f2a..392b7015f4925 100644
@@ -660,6 +660,7 @@ enum spd_duplex {
 /* OCP_POWER_CFG */
 #define EEE_CLKDIV_EN		0x8000
 #define EN_ALDPS		0x0004
+#define EN_ALDPS_PLLOFF		0x0002
 #define EN_10M_PLLOFF		0x0001
 
 /* OCP_EEE_CONFIG1 */
@@ -1969,6 +1970,16 @@ static void sram2_write_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set)
 	ocp_reg_write(tp, OCP_SRAM2_DATA, data);
 }
 
+static void sram2_set_bits(struct r8152 *tp, u16 addr, u16 set)
+{
+	sram2_write_w0w1(tp, addr, 0, set);
+}
+
+static void sram2_clr_bits(struct r8152 *tp, u16 addr, u16 clear)
+{
+	sram2_write_w0w1(tp, addr, clear, 0);
+}
+
 static void r8152_mdio_clr_bit(struct r8152 *tp, u16 addr, u16 clear)
 {
 	int data;
@@ -8231,11 +8242,14 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 	ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_PHY_PWR, PFM_PWM_SWITCH);
 
 	/* Advanced Power Saving parameter */
-	ocp_reg_set_bits(tp, 0xa430, BIT(0) | BIT(1));
+	ocp_reg_set_bits(tp, OCP_POWER_CFG, EN_10M_PLLOFF | EN_ALDPS_PLLOFF);
 
 	/* Disable ALDPS force mode */
 	ocp_reg_clr_bits(tp, 0xa44a, BIT(2));
 
+	/* Disable bypass_turn_off_clk_in_aldps */
+	ocp_byte_clr_bits(tp, MCU_TYPE_PLA, 0xd3c8, BIT(0));
+
 	switch (tp->version) {
 	case RTL_VER_16:
 		/* XG_INRX parameter */
@@ -8251,7 +8265,7 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 		sram2_write_w0w1(tp, 0x8078, 0xff00, 0x3000);
 
 		/* green mode */
-		sram2_write_w0w1(tp, 0x89e9, 0xff00, 0);
+		sram2_clr_bits(tp, 0x89e9, 0xff00);
 		sram2_write_w0w1(tp, 0x8ffd, 0xff00, 0x0100);
 		sram2_write_w0w1(tp, 0x8ffe, 0xff00, 0x0200);
 		sram2_write_w0w1(tp, 0x8fff, 0xff00, 0x0400);
@@ -8357,12 +8371,85 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 		sram2_write_w0w1(tp, 0x807c, 0xff00, 0x5000);
 		sram2_write_w0w1(tp, 0x809d, 0xff00, 0x5000);
 		break;
+	default:
+		break;
+	}
+
+	if (rtl_phy_patch_request(tp, true, true))
+		return;
+
+	ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, EEE_SPDWN_EN);
+
+	ocp_reg_w0w1(tp, OCP_DOWN_SPEED, EN_EEE_100 | EN_EEE_1000, EN_10M_CLKDIV);
+
+	tp->ups_info._10m_ckdiv = true;
+	tp->ups_info.eee_plloff_100 = false;
+	tp->ups_info.eee_plloff_giga = false;
+
+	ocp_reg_set_bits(tp, OCP_POWER_CFG, EEE_CLKDIV_EN);
+	tp->ups_info.eee_ckdiv = true;
+
+	rtl_phy_patch_request(tp, false, true);
+
+	rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
+
+	ocp_reg_clr_bits(tp, 0xa428, BIT(9));
+	ocp_reg_clr_bits(tp, 0xa5ea, BIT(0) | BIT(1));
+	tp->ups_info.lite_mode = 0;
+
+	if (tp->eee_en)
+		rtl_eee_enable(tp, true);
+
+	r8153_aldps_en(tp, true);
+	r8152b_enable_fc(tp);
+
+	set_bit(PHY_RESET, &tp->flags);
+}
+
+static void r8159_hw_phy_cfg(struct r8152 *tp)
+{
+	u16 data;
+
+	r8156b_wait_loading_flash(tp);
+
+	ocp_word_test_and_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS);
+
+	data = r8153_phy_status(tp, 0);
+	switch (data) {
+	case PHY_STAT_EXT_INIT:
+		rtl8152_apply_firmware(tp, true);
+		ocp_reg_clr_bits(tp, 0xa466, BIT(0));
+		ocp_reg_clr_bits(tp, 0xa468, BIT(3) | BIT(1));
+		break;
+	case PHY_STAT_LAN_ON:
+	case PHY_STAT_PWRDN:
+	default:
+		rtl8152_apply_firmware(tp, false);
+		break;
+	}
+
+	r8152_mdio_test_and_clr_bit(tp, MII_BMCR, BMCR_PDOWN);
+
+	r8153_aldps_en(tp, false);
+
+	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
+	WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
 
+	/* PFM mode */
+	ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_PHY_PWR, PFM_PWM_SWITCH);
+
+	/* Advanced Power Saving parameter */
+	ocp_reg_set_bits(tp, OCP_POWER_CFG, EN_10M_PLLOFF | EN_ALDPS_PLLOFF);
+
+	/* Disable ALDPS force mode */
+	ocp_reg_clr_bits(tp, 0xa44a, BIT(2));
+
+	/* Disable bypass_turn_off_clk_in_aldps */
+	ocp_byte_clr_bits(tp, MCU_TYPE_PLA, 0xd3c8, BIT(0));
+
+	switch (tp->version) {
 	case RTL_VER_17_QFN68:
 	case RTL_VER_17_QFN100:
-		/* Disable bypass turn off clk in ALDPS */
-		ocp_byte_clr_bits(tp, MCU_TYPE_PLA, 0xd3c8, BIT(0));
-
 		/* Power level tuning
 		 * test mode power level
 		 */
@@ -8372,22 +8459,35 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 		sram_write_w0w1(tp, 0x81ae, 0xff00, 0x0f00);
 		sram_write_w0w1(tp, 0x81b9, 0xff00, 0xb900);
 		/* normal link TX filter */
-		sram2_write_w0w1(tp, 0x83b0, 0x0e00, 0);
-		sram2_write_w0w1(tp, 0x83c5, 0x0e00, 0);
-		sram2_write_w0w1(tp, 0x83da, 0x0e00, 0);
-		sram2_write_w0w1(tp, 0x83ef, 0x0e00, 0);
+		sram2_clr_bits(tp, 0x83b0, 0x0e00);
+		sram2_clr_bits(tp, 0x83c5, 0x0e00);
+		sram2_clr_bits(tp, 0x83da, 0x0e00);
+		sram2_clr_bits(tp, 0x83ef, 0x0e00);
+
+		ocp_reg_w0w1(tp, 0xbf38, 0x01f0, 0x0160);
+		ocp_reg_w0w1(tp, 0xbf3a, 0x001f, 0x0014);
+		/* shorten CLKS latency */
+		ocp_reg_clr_bits(tp, 0xbf28, BIT(14) | BIT(13));
+		ocp_reg_clr_bits(tp, 0xbf2c, BIT(15) | BIT(14));
+		/* CMP_Timer on MP_Timer=333
+		 * GPHY OCP 0xbf28 bit[0] = 0x1
+		 * GPHY OCP 0xbf28 bit[6:1] = 0x3
+		 * GPHY OCP 0xbf28 bit[12:7] = 0x3
+		 */
+		ocp_reg_w0w1(tp, 0xbf28, 0x1fff, 0x0187);
+		ocp_reg_w0w1(tp, 0xbf2a, 0x3f, 0x03);
 
 		/* AFE power saving for 2.5G & 5G */
 		sram_write(tp, 0x8173, 0x8620);
 		sram_write(tp, 0x8175, 0x8671);
 
-		sram_write_w0w1(tp, 0x817c, 0, BIT(13));
-		sram_write_w0w1(tp, 0x8187, 0, BIT(13));
-		sram_write_w0w1(tp, 0x8192, 0, BIT(13));
-		sram_write_w0w1(tp, 0x819d, 0, BIT(13));
-		sram_write_w0w1(tp, 0x81a8, BIT(13), 0);
-		sram_write_w0w1(tp, 0x81b3, BIT(13), 0);
-		sram_write_w0w1(tp, 0x81be, 0, BIT(13));
+		sram_set_bits(tp, 0x817c, BIT(13));
+		sram_set_bits(tp, 0x8187, BIT(13));
+		sram_set_bits(tp, 0x8192, BIT(13));
+		sram_set_bits(tp, 0x819d, BIT(13));
+		sram_clr_bits(tp, 0x81a8, BIT(13));
+		sram_clr_bits(tp, 0x81b3, BIT(13));
+		sram_set_bits(tp, 0x81be, BIT(13));
 
 		sram_write_w0w1(tp, 0x817d, 0xff00, 0xa600);
 		sram_write_w0w1(tp, 0x8188, 0xff00, 0xa600);
@@ -8451,10 +8551,10 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 		sram2_write_w0w1(tp, 0x84b2, 0xff00, 0x6000);
 		/* Training AAGC PAR (with uc2 patch) */
 		sram2_write(tp, 0x8ffc, 0x6008);
-		sram2_write(tp, 0x8ffe, 0xf450);
+		sram2_write(tp, 0x8ffe, 0xf4ff);
 		/* DAC BGK */
-		sram2_write_w0w1(tp, 0x8015, 0, BIT(9));
-		sram2_write_w0w1(tp, 0x8016, 0, BIT(11));
+		sram2_set_bits(tp, 0x8015, BIT(9));
+		sram2_set_bits(tp, 0x8016, BIT(11));
 		sram2_write_w0w1(tp, 0x8fe6, 0xff00, 0x0800);
 		sram2_write(tp, 0x8fe4, 0x2114);
 		/* 10G PBO table */
@@ -8463,14 +8563,14 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 		sram2_write_w0w1(tp, 0x864b, 0xff00, 0xdc00);
 		/* 2.5G ado power window size */
 		sram2_write_w0w1(tp, 0x8154, 0xc000, 0x4000);
-		sram2_write_w0w1(tp, 0x8158, 0xc000, 0);
+		sram2_clr_bits(tp, 0x8158, 0xc000);
 		/* 10G lock far */
 		sram2_write(tp, 0x826c, 0xffff);
 		sram2_write(tp, 0x826e, 0xffff);
 		/* XG INRX parameter */
 		sram2_write_w0w1(tp, 0x8872, 0xff00, 0x0e00);
-		sram_write_w0w1(tp, 0x8012, 0, BIT(11));
-		sram_write_w0w1(tp, 0x8012, 0, BIT(14));
+		sram_set_bits(tp, 0x8012, BIT(11));
+		sram_set_bits(tp, 0x8012, BIT(14));
 		ocp_reg_set_bits(tp, 0xb576, BIT(0));
 		sram_write_w0w1(tp, 0x834a, 0xff00, 0x0700);
 		sram2_write_w0w1(tp, 0x8217, 0x3f00, 0x2a00);
@@ -8481,7 +8581,7 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 		/* improve UBE */
 		ocp_reg_set_bits(tp, 0xbf0c, 0x7 << 11);
 		/* close Sparse NEC, improve connect 5EUU cable performance */
-		sram2_write_w0w1(tp, 0x88de, 0xff00, 0);
+		sram2_clr_bits(tp, 0x88de, 0xff00);
 		/* 5G slave compatibility issue */
 		sram2_write(tp, 0x80b4, 0x5195);
 
@@ -8540,8 +8640,15 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 		sram2_write(tp, 0x8ff8, 0xaa5a);
 
 		sram2_write_w0w1(tp, 0x88d5, 0xff00, 0x0200);
-		break;
 
+		/* spdchg_pga1_lpf_cap */
+		sram_write_w0w1(tp, 0x84bb, 0xff00, 0x0a00);
+		sram_write_w0w1(tp, 0x84c0, 0xff00, 0x1600);
+
+		/* ENET PLL jitter improvement */
+		ocp_reg_w0w1(tp, 0xbf8a, 0xfc00, 0x2000);
+		ocp_reg_set_bits(tp, 0xbf88, BIT(2));
+		break;
 	default:
 		break;
 	}
@@ -8551,9 +8658,9 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 
 	ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, EEE_SPDWN_EN);
 
-	ocp_reg_w0w1(tp, OCP_DOWN_SPEED, EN_EEE_100 | EN_EEE_1000, EN_10M_CLKDIV);
-
-	tp->ups_info._10m_ckdiv = true;
+	ocp_reg_clr_bits(tp, OCP_DOWN_SPEED,
+			 EN_EEE_100 | EN_EEE_1000 | EN_10M_CLKDIV);
+	tp->ups_info._10m_ckdiv = false;
 	tp->ups_info.eee_plloff_100 = false;
 	tp->ups_info.eee_plloff_giga = false;
 
@@ -8565,11 +8672,10 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 	rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
 
 	ocp_reg_clr_bits(tp, 0xa428, BIT(9));
-	ocp_reg_clr_bits(tp, 0xa5ea, BIT(0) | BIT(1));
+	ocp_reg_clr_bits(tp, 0xa5ea, BIT(0) | BIT(1) | BIT(2));
 	tp->ups_info.lite_mode = 0;
 
-	if (tp->eee_en)
-		rtl_eee_enable(tp, true);
+	rtl_eee_enable(tp, tp->eee_en);
 
 	r8153_aldps_en(tp, true);
 	r8152b_enable_fc(tp);
@@ -10252,7 +10358,7 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->eee_get		= r8153_get_eee;
 		ops->eee_set		= r8152_set_eee;
 		ops->in_nway		= rtl8153_in_nway;
-		ops->hw_phy_cfg		= r8157_hw_phy_cfg;
+		ops->hw_phy_cfg		= r8159_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8157_runtime_enable;
 		ops->change_mtu		= rtl8157_change_mtu;
 		tp->rx_buf_sz		= 48 * 1024;
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH net-next v10 10/12] r8152: split r8157_hw_phy_cfg into RTL8157 and RTL8159 variants

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] r8159_hw_phy_cfg() (drivers/net/usb/r8152.c, RTL_VER_17_QFN68/QFN100 case) adds two PHY register writes that have…

--

r8152: split r8157_hw_phy_cfg into RTL8157 and RTL8159 variants

This commit splits r8157_hw_phy_cfg() into r8157_hw_phy_cfg() for RTL_VER_16 and a new r8159_hw_phy_cfg() for RTL_VER_17_QFN68/QFN100. Each function gets its own copy of the shared prologue and epilogue. It also changes the EEE handling for RTL8159, adds new RTL8159 PHY tuning parameters, and adds sram2_set_bits()/sram2_clr_bits() helpers.

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
index a16e7d0257f2a..392b7015f4925 100644

[ ... ]

Quoted 5 lines
drivers/net/usb/r8152.c +0 -1
@@ -8372,22 +8459,35 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)
 		sram_write_w0w1(tp, 0x81ae, 0xff00, 0x0f00);
 		sram_write_w0w1(tp, 0x81b9, 0xff00, 0xb900);
 		/* normal link TX filter */
-		sram2_write_w0w1(tp, 0x83b0, 0x0e00, 0);

drivers/net/usb/r8152.c static void r8157_hw_phy_cfg(struct r8152 *tp) · line 8468

Quoted 10 lines
drivers/net/usb/r8152.c +7 -3
-		sram2_write_w0w1(tp, 0x83c5, 0x0e00, 0);
-		sram2_write_w0w1(tp, 0x83da, 0x0e00, 0);
-		sram2_write_w0w1(tp, 0x83ef, 0x0e00, 0);
+		sram2_clr_bits(tp, 0x83b0, 0x0e00);
+		sram2_clr_bits(tp, 0x83c5, 0x0e00);
+		sram2_clr_bits(tp, 0x83da, 0x0e00);
+		sram2_clr_bits(tp, 0x83ef, 0x0e00);
+
+		ocp_reg_w0w1(tp, 0xbf38, 0x01f0, 0x0160);
+		ocp_reg_w0w1(tp, 0xbf3a, 0x001f, 0x0014);

[Severity: Low]
What are these two writes to 0xbf38 and 0xbf3a in r8159_hw_phy_cfg() for? They have no comment, and the driver doesn't touch either register before this patch, so this isn't code moved from somewhere else.

The commit message lists the new RTL8159 parameters as:

Newly added PHY tuning parameters for RTL8159:
- shorten CLKS latency
- CMP_Timer on MP_Timer=333
- spdchg_pga1_lpf_cap
- ENET PLL jitter improvement

Neither write fits any item on that list. Because they follow the "normal link TX filter" sram2 block with no comment of their own, it is hard to tell which feature they belong to.

Could a comment be added here, and could the commit message mention them?

drivers/net/usb/r8152.c static void r8157_hw_phy_cfg(struct r8152 *tp) · line 8478

Quoted 10 lines
drivers/net/usb/r8152.c +10 -0
+		/* shorten CLKS latency */
+		ocp_reg_clr_bits(tp, 0xbf28, BIT(14) | BIT(13));
+		ocp_reg_clr_bits(tp, 0xbf2c, BIT(15) | BIT(14));
+		/* CMP_Timer on MP_Timer=333
+		 * GPHY OCP 0xbf28 bit[0] = 0x1
+		 * GPHY OCP 0xbf28 bit[6:1] = 0x3
+		 * GPHY OCP 0xbf28 bit[12:7] = 0x3
+		 */
+		ocp_reg_w0w1(tp, 0xbf28, 0x1fff, 0x0187);
+		ocp_reg_w0w1(tp, 0xbf2a, 0x3f, 0x03);

[Severity: Low]
A related question about the 0xbf2a write: the "CMP_Timer on MP_Timer=333" comment above it only describes the bitfields of 0xbf28. Is the 0xbf2a write part of the CMP_Timer setting, or something else? If it is part of it, could the comment also describe 0xbf2a bit[5:0]?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1-nic_swsd%40realtek.com
#18 lore raw

PATCH v10 11/12 r8152: add TGPHY register access for RTL8157 and RTL8159

In reply to Chih Kai Hsu

RTL8157 and RTL8159 have a TGPHY command channel (USB_TGPHY_ADDR/DATA/CMD) for PHY registers that needs no OCP base switching. Use it for these chips; older chips keep the OCP path.

Route ocp_reg_read()/ocp_reg_write() through new rtl_ops.phy_read and phy_write hooks, set per chip.

Unlike the OCP path, a TGPHY access can fail: the busy bit can time out, or the device can be gone. The access is then abandoned and returns an errno, -ENODEV once the device is marked inaccessible. A transfer that fails without marking it (-ENOMEM, or an unplug the USB core has not reported yet) is not detected, as in the OCP accessors. A timeout is logged ratelimited, so a stuck busy bit does not flood the log, and does not mark the device inaccessible, as for the ADV command channel.

As with the ADV channel, a write only waits for the busy bit before it posts the command, so success means the command was issued, not that the PHY has completed it. A write that never completes is reported by the next TGPHY access.

The PHY accessors and the helpers built on them therefore return int, and the callers where a failure would be taken for register content or for success now act on it:

- rtl8152_set_pauseparam(), rtl8152_set_speed(), SIOCGMIIREG/ SIOCSMIIREG, r8153_get_eee() and r8152_mdio_read() propagate the error, as does rtl8152_get_link_ksettings() for the 10GBT registers. rtl8152_get_pauseparam() reports no flow control when a read fails.
- read_mii_word() reports a failed read as 0, because the MII library takes the return value for register content. A TGPHY timeout returned as an errno would look like a BMCR with autonegotiation enabled, and mii_nway_restart() would write it back; with 0 it returns without writing. -ENODEV is returned only when the device is already inaccessible on entry, as the writes that follow are blocked then.
Other callers of the MII library see the 0 as register content too:
mii_nway_restart() fails with -EINVAL, which ethtool -r and rtl_ethtool_set_eee() report in place of the timeout, and mii_ethtool_get_link_ksettings() reports autonegotiation off, 10 Mb/s half duplex and an unknown speed, so rtl8152_get_link_ksettings() returns 0 with those settings unless the 10GBT reads fail as well.
- rtl8152_set_speed() updates the cached speed/duplex state only once the final BMCR write succeeds, and keeps a pending PHY reset if that write fails. PHY registers already written before a failure are not rolled back, as that would need further accesses that can fail too, so the PHY may keep a partly updated advertisement until the next successful set_speed(). While waiting for the PHY reset to finish, a failed read keeps the wait going, as r8153_phy_status() does, and only an inaccessible device ends it early. The result is not reported, as the BMCR write has been issued and cached.
- set_carrier() keeps the current link state when the BMSR read fails. An errno would otherwise be masked with BMSR_LSTATUS: -ENODEV would look like link up and -ETIMEDOUT like link down. The work function has already consumed RTL8152_LINK_CHG, so a link change seen by the interrupt would be lost, leaving the carrier and the TX queue out of step with the link. Set the flag again and retry after 100 ms, unless the device is inaccessible, in which case the work function would return at once.
- The PHY firmware version checks skip the patch when the version cannot be read.
- rtl_phy_patch_request() returns the errno of a failed write, and releases PATCH_REQUEST when a later step fails, so a failed access does not leave it asserted.
- r8153_phy_status() treats a failed read as "not ready" and keeps polling, so one failure does not end the wait early.
- rtl8153_in_nway() reports the PHY as negotiating when the read fails, so runtime suspend is delayed rather than risking a missed link-up wake event.
- r8156_mdio_force_mode(), r8156_eee_en() and rtl8152_fw_phy_fixup() stop on a failed read instead of using stale data.

The hw_phy_cfg and init sequences, the ocp_reg_*_bits() and sram_*() callers that write PHY configuration and r8152_mmd_read() still ignore the status, as does recovery from a TGPHY timeout. These are left to a follow-up series.

Older chips are unaffected in the PHY access itself, as r8152_phy_read() always succeeds. The error handling changes a few paths there as well, without any other register access: the 0xd068 read in r8156_hw_phy_cfg() now runs before the MID_REVERSE test, rtl8152_set_pauseparam() reads BMCR also when autonegotiation is not requested, and rtl_pre_ram_code() returns the errno of rtl_phy_patch_request(), which now also releases PATCH_REQUEST when the device turns inaccessible while waiting.

---
 drivers/net/usb/r8152.c | 609 ++++++++++++++++++++++++++++++----------
 1 file changed, 459 insertions(+), 150 deletions(-)
drivers/net/usb/r8152.c +459 -150
index 392b7015f4925..b9586c30c04dd 100644
@@ -163,6 +163,9 @@
 #define USB_ADV_ADDR		0xd5d6
 #define USB_ADV_DATA		0xd5d8
 #define USB_ADV_CMD		0xd5dc
+#define USB_TGPHY_ADDR		0xd630
+#define USB_TGPHY_DATA		0xd632
+#define USB_TGPHY_CMD		0xd634
 #define USB_UPS_CTRL		0xd800
 #define USB_POWER_CUT		0xd80a
 #define USB_MISC_0		0xd81a
@@ -516,6 +519,10 @@
 #define ADV_CMD_WR		BIT(1)
 #define ADV_CMD_IP		BIT(2)
 
+/* USB_TGPHY_CMD */
+#define TGPHY_CMD_BUSY		BIT(0)
+#define TGPHY_CMD_WR		BIT(1)
+
 /* USB_UPS_CTRL */
 #define POWER_CUT		0x0100
 
@@ -964,6 +971,8 @@ struct r8152 {
 		void (*hw_phy_cfg)(struct r8152 *tp);
 		void (*autosuspend_en)(struct r8152 *tp, bool enable);
 		void (*change_mtu)(struct r8152 *tp);
+		int (*phy_read)(struct r8152 *tp, u16 addr, u16 *data);
+		int (*phy_write)(struct r8152 *tp, u16 addr, u16 data);
 	} rtl_ops;
 
 	struct ups_info {
@@ -1643,7 +1652,7 @@ static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
 	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
 }
 
-static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
+static int r8152_phy_read(struct r8152 *tp, u16 addr, u16 *data)
 {
 	u16 ocp_base, ocp_index;
 
@@ -1654,10 +1663,12 @@ static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
 	}
 
 	ocp_index = (addr & 0x0fff) | 0xb000;
-	return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
+	*data = ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
+
+	return 0;
 }
 
-static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
+static int r8152_phy_write(struct r8152 *tp, u16 addr, u16 data)
 {
 	u16 ocp_base, ocp_index;
 
@@ -1669,16 +1680,33 @@ static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
 
 	ocp_index = (addr & 0x0fff) | 0xb000;
 	ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
+
+	return 0;
+}
+
+static int ocp_reg_read(struct r8152 *tp, u16 addr, u16 *data)
+{
+	return tp->rtl_ops.phy_read(tp, addr, data);
 }
 
-static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
+static int ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
 {
-	ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
+	return tp->rtl_ops.phy_write(tp, addr, data);
 }
 
-static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
+static int r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
 {
-	return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
+	return ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
+}
+
+static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
+{
+	u16 data;
+	int ret;
+
+	ret = ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2, &data);
+
+	return ret < 0 ? ret : data;
 }
 
 static int wait_cmd_ready(struct r8152 *tp, u16 cmd)
@@ -1797,16 +1825,32 @@ static int rtl_ip_set_bits(struct r8152 *tp, u16 addr, u32 set)
 	return rtl_ip_w0w1(tp, addr, 0, set);
 }
 
-static void sram_write(struct r8152 *tp, u16 addr, u16 data)
+static int sram_write(struct r8152 *tp, u16 addr, u16 data)
 {
-	ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
-	ocp_reg_write(tp, OCP_SRAM_DATA, data);
+	int ret;
+
+	ret = ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
+	if (ret < 0)
+		goto out;
+
+	ret = ocp_reg_write(tp, OCP_SRAM_DATA, data);
+
+out:
+	return ret;
 }
 
-static u16 sram_read(struct r8152 *tp, u16 addr)
+static int sram_read(struct r8152 *tp, u16 addr, u16 *data)
 {
-	ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
-	return ocp_reg_read(tp, OCP_SRAM_DATA);
+	int ret;
+
+	ret = ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
+	if (ret < 0)
+		goto out;
+
+	ret = ocp_reg_read(tp, OCP_SRAM_DATA, data);
+
+out:
+	return ret;
 }
 
 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
@@ -1822,7 +1866,10 @@ static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
 
 	ret = r8152_mdio_read(tp, reg);
 
-	return ret;
+	/* The MII library takes the return value for register content,
+	 * so a failed read must not be passed on as an errno.
+	 */
+	return ret < 0 ? 0 : ret;
 }
 
 static
@@ -1911,100 +1958,221 @@ static void ocp_byte_set_bits(struct r8152 *tp, u16 type, u16 index, u8 set)
 	ocp_byte_w0w1(tp, type, index, 0, set);
 }
 
-static void ocp_reg_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set)
+static int ocp_reg_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set)
 {
 	u16 data;
+	int ret;
+
+	ret = ocp_reg_read(tp, addr, &data);
+	if (ret < 0)
+		goto out;
 
-	data = ocp_reg_read(tp, addr);
 	data = (data & ~clear) | set;
-	ocp_reg_write(tp, addr, data);
+	ret = ocp_reg_write(tp, addr, data);
+
+out:
+	return ret;
 }
 
-static void ocp_reg_clr_bits(struct r8152 *tp, u16 addr, u16 clear)
+static int ocp_reg_clr_bits(struct r8152 *tp, u16 addr, u16 clear)
 {
-	ocp_reg_w0w1(tp, addr, clear, 0);
+	return ocp_reg_w0w1(tp, addr, clear, 0);
 }
 
-static void ocp_reg_set_bits(struct r8152 *tp, u16 addr, u16 set)
+static int ocp_reg_set_bits(struct r8152 *tp, u16 addr, u16 set)
 {
-	ocp_reg_w0w1(tp, addr, 0, set);
+	return ocp_reg_w0w1(tp, addr, 0, set);
 }
 
-static void sram_write_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set)
+static int sram_write_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set)
 {
 	u16 data;
+	int ret;
+
+	ret = sram_read(tp, addr, &data);
+	if (ret < 0)
+		goto out;
 
-	data = sram_read(tp, addr);
 	data = (data & ~clear) | set;
-	ocp_reg_write(tp, OCP_SRAM_DATA, data);
+	ret = ocp_reg_write(tp, OCP_SRAM_DATA, data);
+
+out:
+	return ret;
 }
 
-static void sram_clr_bits(struct r8152 *tp, u16 addr, u16 clear)
+static int sram_clr_bits(struct r8152 *tp, u16 addr, u16 clear)
 {
-	sram_write_w0w1(tp, addr, clear, 0);
+	return sram_write_w0w1(tp, addr, clear, 0);
 }
 
-static void sram_set_bits(struct r8152 *tp, u16 addr, u16 set)
+static int sram_set_bits(struct r8152 *tp, u16 addr, u16 set)
 {
-	sram_write_w0w1(tp, addr, 0, set);
+	return sram_write_w0w1(tp, addr, 0, set);
 }
 
-static void sram2_write(struct r8152 *tp, u16 addr, u16 data)
+static int sram2_write(struct r8152 *tp, u16 addr, u16 data)
 {
-	ocp_reg_write(tp, OCP_SRAM2_ADDR, addr);
-	ocp_reg_write(tp, OCP_SRAM2_DATA, data);
+	int ret;
+
+	ret = ocp_reg_write(tp, OCP_SRAM2_ADDR, addr);
+	if (ret < 0)
+		goto out;
+
+	ret = ocp_reg_write(tp, OCP_SRAM2_DATA, data);
+
+out:
+	return ret;
 }
 
-static u16 sram2_read(struct r8152 *tp, u16 addr)
+static int sram2_read(struct r8152 *tp, u16 addr, u16 *data)
 {
-	ocp_reg_write(tp, OCP_SRAM2_ADDR, addr);
-	return ocp_reg_read(tp, OCP_SRAM2_DATA);
+	int ret;
+
+	ret = ocp_reg_write(tp, OCP_SRAM2_ADDR, addr);
+	if (ret < 0)
+		goto out;
+
+	ret = ocp_reg_read(tp, OCP_SRAM2_DATA, data);
+
+out:
+	return ret;
 }
 
-static void sram2_write_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set)
+static int sram2_write_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set)
 {
 	u16 data;
+	int ret;
+
+	ret = sram2_read(tp, addr, &data);
+	if (ret < 0)
+		goto out;
 
-	data = sram2_read(tp, addr);
 	data = (data & ~clear) | set;
-	ocp_reg_write(tp, OCP_SRAM2_DATA, data);
+	ret = ocp_reg_write(tp, OCP_SRAM2_DATA, data);
+
+out:
+	return ret;
 }
 
-static void sram2_set_bits(struct r8152 *tp, u16 addr, u16 set)
+static int sram2_set_bits(struct r8152 *tp, u16 addr, u16 set)
 {
-	sram2_write_w0w1(tp, addr, 0, set);
+	return sram2_write_w0w1(tp, addr, 0, set);
 }
 
-static void sram2_clr_bits(struct r8152 *tp, u16 addr, u16 clear)
+static int sram2_clr_bits(struct r8152 *tp, u16 addr, u16 clear)
 {
-	sram2_write_w0w1(tp, addr, clear, 0);
+	return sram2_write_w0w1(tp, addr, clear, 0);
 }
 
-static void r8152_mdio_clr_bit(struct r8152 *tp, u16 addr, u16 clear)
+static int r8152_mdio_clr_bit(struct r8152 *tp, u16 addr, u16 clear)
 {
 	int data;
 
 	data = r8152_mdio_read(tp, addr);
-	r8152_mdio_write(tp, addr, data & ~clear);
+	if (data < 0)
+		goto out;
+
+	data = r8152_mdio_write(tp, addr, data & ~clear);
+
+out:
+	return data;
 }
 
-static void r8152_mdio_set_bit(struct r8152 *tp, u16 addr, u16 set)
+static int r8152_mdio_set_bit(struct r8152 *tp, u16 addr, u16 set)
 {
 	int data;
 
 	data = r8152_mdio_read(tp, addr);
-	r8152_mdio_write(tp, addr, data | set);
+	if (data < 0)
+		goto out;
+
+	data = r8152_mdio_write(tp, addr, data | set);
+
+out:
+	return data;
 }
 
 static int r8152_mdio_test_and_clr_bit(struct r8152 *tp, u16 addr, u16 clear)
 {
-	int data;
+	int data, ret;
 
-	data = r8152_mdio_read(tp, addr);
-	if (data & clear)
-		r8152_mdio_write(tp, addr, data & ~clear);
+	ret = r8152_mdio_read(tp, addr);
+	if (ret < 0)
+		goto out;
+
+	data = ret;
+	if (data & clear) {
+		ret = r8152_mdio_write(tp, addr, data & ~clear);
+		if (ret < 0)
+			goto out;
+	}
 
-	return data & clear;
+	ret = !!(data & clear);
+
+out:
+	return ret;
+}
+
+static int wait_tgphy_cmd_ready(struct r8152 *tp)
+{
+	u16 ocp_data;
+	int ret;
+
+	ret = read_poll_timeout(ocp_read_word, ocp_data,
+				test_bit(RTL8152_INACCESSIBLE, &tp->flags) ||
+				!(ocp_data & TGPHY_CMD_BUSY),
+				2000, 20000, false, tp,
+				MCU_TYPE_USB, USB_TGPHY_CMD);
+
+	if (ret)
+		dev_err_ratelimited(&tp->intf->dev, "TGPHY cmd busy timeout\n");
+
+	return test_bit(RTL8152_INACCESSIBLE, &tp->flags) ? -ENODEV : ret;
+}
+
+static int rtl_tgphy_access(struct r8152 *tp, u16 addr, u16 *data, bool write)
+{
+	u16 cmd = 0;
+	int ret;
+
+	ret = wait_tgphy_cmd_ready(tp);
+	if (ret < 0)
+		goto out;
+
+	if (write) {
+		cmd |= TGPHY_CMD_WR;
+		ocp_write_word(tp, MCU_TYPE_USB, USB_TGPHY_DATA, *data);
+	}
+
+	ocp_write_word(tp, MCU_TYPE_USB, USB_TGPHY_ADDR, addr);
+
+	cmd |= TGPHY_CMD_BUSY;
+	ocp_write_word(tp, MCU_TYPE_USB, USB_TGPHY_CMD, cmd);
+
+	if (!write) {
+		ret = wait_tgphy_cmd_ready(tp);
+		if (ret < 0)
+			goto out;
+
+		*data = ocp_read_word(tp, MCU_TYPE_USB, USB_TGPHY_DATA);
+	}
+
+out:
+	/* Only failures that latch RTL8152_INACCESSIBLE are detected */
+	if (!ret && test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+		ret = -ENODEV;
+
+	return ret;
+}
+
+static int r8157_phy_read(struct r8152 *tp, u16 addr, u16 *data)
+{
+	return rtl_tgphy_access(tp, addr, data, false);
+}
+
+static int r8157_phy_write(struct r8152 *tp, u16 addr, u16 data)
+{
+	return rtl_tgphy_access(tp, addr, &data, true);
 }
 
 static int
@@ -4187,18 +4355,20 @@ static void r8153b_green_en(struct r8152 *tp, bool enable)
 
 static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
 {
-	u16 data;
+	u16 data = 0, val;
 	int i;
 
 	for (i = 0; i < 500; i++) {
-		data = ocp_reg_read(tp, OCP_PHY_STATUS);
-		data &= PHY_STAT_MASK;
-		if (desired) {
-			if (data == desired)
+		if (!ocp_reg_read(tp, OCP_PHY_STATUS, &val)) {
+			data = val & PHY_STAT_MASK;
+			if (desired) {
+				if (data == desired)
+					break;
+			} else if (data == PHY_STAT_LAN_ON ||
+				   data == PHY_STAT_PWRDN ||
+				   data == PHY_STAT_EXT_INIT) {
 				break;
-		} else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
-			   data == PHY_STAT_EXT_INIT) {
-			break;
+			}
 		}
 
 		msleep(20);
@@ -4597,36 +4767,52 @@ static inline void rtl_reset_ocp_base(struct r8152 *tp)
 static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait)
 {
 	u16 check;
-	int i;
+	u16 ocp_data;
+	int i, ret;
 
 	if (request) {
-		ocp_reg_set_bits(tp, OCP_PHY_PATCH_CMD, PATCH_REQUEST);
+		ret = ocp_reg_set_bits(tp, OCP_PHY_PATCH_CMD, PATCH_REQUEST);
 		check = 0;
 	} else {
-		ocp_reg_clr_bits(tp, OCP_PHY_PATCH_CMD, PATCH_REQUEST);
+		ret = ocp_reg_clr_bits(tp, OCP_PHY_PATCH_CMD, PATCH_REQUEST);
 		check = PATCH_READY;
 	}
 
-	for (i = 0; wait && i < 5000; i++) {
-		u32 ocp_data;
+	if (ret < 0)
+		return ret;
 
-		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
-			return -ENODEV;
+	for (i = 0; wait && i < 5000; i++) {
+		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
+			ret = -ENODEV;
+			goto err;
+		}
 
 		usleep_range(1000, 2000);
-		ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT);
-		if ((ocp_data & PATCH_READY) ^ check)
+		ret = ocp_reg_read(tp, OCP_PHY_PATCH_STAT, &ocp_data);
+		if (ret < 0)
+			goto err;
+		else if ((ocp_data & PATCH_READY) ^ check)
 			break;
 	}
 
-	if (request && wait &&
-	    !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
-		dev_err(&tp->intf->dev, "PHY patch request fail\n");
-		rtl_phy_patch_request(tp, false, false);
-		return -ETIME;
-	} else {
-		return 0;
+	if (request && wait) {
+		ret = ocp_reg_read(tp, OCP_PHY_PATCH_STAT, &ocp_data);
+		if (ret < 0) {
+			goto err;
+		} else if (!(ocp_data & PATCH_READY)) {
+			dev_err(&tp->intf->dev, "PHY patch request fail\n");
+			ret = -ETIME;
+			goto err;
+		}
 	}
+
+	return 0;
+
+err:
+	if (request)
+		rtl_phy_patch_request(tp, false, false);
+
+	return ret;
 }
 
 static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key)
@@ -4646,8 +4832,11 @@ static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key)
 static int
 rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait)
 {
-	if (rtl_phy_patch_request(tp, true, wait))
-		return -ETIME;
+	int ret;
+
+	ret = rtl_phy_patch_request(tp, true, wait);
+	if (ret)
+		return ret;
 
 	rtl_patch_key_set(tp, key_addr, patch_key);
 
@@ -5347,10 +5536,14 @@ static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy,
 {
 	u32 len;
 	u8 *data;
+	u16 ver;
 
 	rtl_reset_ocp_base(tp);
 
-	if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) {
+	if (sram_read(tp, SRAM_GPHY_FW_VER, &ver) < 0)
+		return;
+
+	if (ver >= __le16_to_cpu(phy->version)) {
 		dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
 		return;
 	}
@@ -5397,7 +5590,8 @@ static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy,
 
 	rtl_phy_patch_request(tp, false, wait);
 
-	if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version))
+	if (sram_read(tp, SRAM_GPHY_FW_VER, &ver) >= 0 &&
+	    ver == __le16_to_cpu(phy->version))
 		dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
 	else
 		dev_err(&tp->intf->dev, "ram code speedup mode fail\n");
@@ -5405,14 +5599,17 @@ static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy,
 
 static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver)
 {
-	u16 ver_addr, ver;
+	u16 ver_addr, ver, cur_ver;
 
 	ver_addr = __le16_to_cpu(phy_ver->ver.addr);
 	ver = __le16_to_cpu(phy_ver->ver.data);
 
 	rtl_reset_ocp_base(tp);
 
-	if (sram_read(tp, ver_addr) >= ver) {
+	if (sram_read(tp, ver_addr, &cur_ver) < 0)
+		return 0;
+
+	if (cur_ver >= ver) {
 		dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
 		return 0;
 	}
@@ -5431,7 +5628,8 @@ static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix)
 	rtl_reset_ocp_base(tp);
 
 	addr = __le16_to_cpu(fix->setting.addr);
-	data = ocp_reg_read(tp, addr);
+	if (ocp_reg_read(tp, addr, &data) < 0)
+		return;
 
 	switch (__le16_to_cpu(fix->bit_cmd)) {
 	case FW_FIXUP_AND:
@@ -5735,10 +5933,10 @@ static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
 
 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
 {
-	u16 data;
+	u16 data = 0;
 
 	r8152_mmd_indirect(tp, dev, reg);
-	data = ocp_reg_read(tp, OCP_EEE_DATA);
+	ocp_reg_read(tp, OCP_EEE_DATA, &data);
 	ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
 
 	return data;
@@ -5803,7 +6001,8 @@ static void r8156_eee_en(struct r8152 *tp, bool enable)
 
 	r8153_eee_en(tp, enable);
 
-	config = ocp_reg_read(tp, OCP_EEE_ADV2);
+	if (ocp_reg_read(tp, OCP_EEE_ADV2, &config) < 0)
+		return;
 
 	if (enable && (tp->eee_adv2 & MDIO_EEE_2_5GT))
 		config |= MDIO_EEE_2_5GT;
@@ -6359,8 +6558,8 @@ static void r8153b_hw_phy_cfg(struct r8152 *tp)
 	 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
 	 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
 	 */
-	ocp_data = ocp_reg_read(tp, 0xc426);
-	ocp_data &= 0x3fff;
+	ocp_reg_read(tp, 0xc426, &data);
+	ocp_data = data & 0x3fff;
 	if (ocp_data) {
 		u32 swr_cnt_1ms_ini;
 
@@ -6645,6 +6844,9 @@ static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
 			     u32 advertising)
 {
 	u16 bmcr;
+	u32 speed_duplex = tp->ups_info.speed_duplex;
+	bool full_duplex = tp->mii.full_duplex;
+	bool force_media;
 	int ret = 0;
 
 	if (autoneg == AUTONEG_DISABLE) {
@@ -6656,24 +6858,24 @@ static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
 			bmcr = BMCR_SPEED10;
 			if (duplex == DUPLEX_FULL) {
 				bmcr |= BMCR_FULLDPLX;
-				tp->ups_info.speed_duplex = FORCE_10M_FULL;
+				speed_duplex = FORCE_10M_FULL;
 			} else {
-				tp->ups_info.speed_duplex = FORCE_10M_HALF;
+				speed_duplex = FORCE_10M_HALF;
 			}
 			break;
 		case SPEED_100:
 			bmcr = BMCR_SPEED100;
 			if (duplex == DUPLEX_FULL) {
 				bmcr |= BMCR_FULLDPLX;
-				tp->ups_info.speed_duplex = FORCE_100M_FULL;
+				speed_duplex = FORCE_100M_FULL;
 			} else {
-				tp->ups_info.speed_duplex = FORCE_100M_HALF;
+				speed_duplex = FORCE_100M_HALF;
 			}
 			break;
 		case SPEED_1000:
 			if (tp->mii.supports_gmii) {
 				bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
-				tp->ups_info.speed_duplex = NWAY_1000M_FULL;
+				speed_duplex = NWAY_1000M_FULL;
 				break;
 			}
 			fallthrough;
@@ -6682,12 +6884,8 @@ static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
 			goto out;
 		}
 
-		if (duplex == DUPLEX_FULL)
-			tp->mii.full_duplex = 1;
-		else
-			tp->mii.full_duplex = 0;
-
-		tp->mii.force_media = 1;
+		full_duplex = duplex == DUPLEX_FULL;
+		force_media = true;
 	} else {
 		u16 orig, new1;
 		u32 support;
@@ -6712,92 +6910,129 @@ static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
 		if (!advertising)
 			return -EINVAL;
 
-		orig = r8152_mdio_read(tp, MII_ADVERTISE);
+		ret = r8152_mdio_read(tp, MII_ADVERTISE);
+		if (ret < 0)
+			goto out;
+
+		orig = ret;
 		new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
 				ADVERTISE_100HALF | ADVERTISE_100FULL);
 		if (advertising & RTL_ADVERTISED_10_HALF) {
 			new1 |= ADVERTISE_10HALF;
-			tp->ups_info.speed_duplex = NWAY_10M_HALF;
+			speed_duplex = NWAY_10M_HALF;
 		}
 		if (advertising & RTL_ADVERTISED_10_FULL) {
 			new1 |= ADVERTISE_10FULL;
-			tp->ups_info.speed_duplex = NWAY_10M_FULL;
+			speed_duplex = NWAY_10M_FULL;
 		}
 
 		if (advertising & RTL_ADVERTISED_100_HALF) {
 			new1 |= ADVERTISE_100HALF;
-			tp->ups_info.speed_duplex = NWAY_100M_HALF;
+			speed_duplex = NWAY_100M_HALF;
 		}
 		if (advertising & RTL_ADVERTISED_100_FULL) {
 			new1 |= ADVERTISE_100FULL;
-			tp->ups_info.speed_duplex = NWAY_100M_FULL;
+			speed_duplex = NWAY_100M_FULL;
 		}
 
 		if (orig != new1) {
-			r8152_mdio_write(tp, MII_ADVERTISE, new1);
+			ret = r8152_mdio_write(tp, MII_ADVERTISE, new1);
+			if (ret < 0)
+				goto out;
+
 			tp->mii.advertising = new1;
 		}
 
 		if (tp->mii.supports_gmii) {
-			orig = r8152_mdio_read(tp, MII_CTRL1000);
+			ret = r8152_mdio_read(tp, MII_CTRL1000);
+			if (ret < 0)
+				goto out;
+
+			orig = ret;
 			new1 = orig & ~(ADVERTISE_1000FULL |
 					ADVERTISE_1000HALF);
 
 			if (advertising & RTL_ADVERTISED_1000_FULL) {
 				new1 |= ADVERTISE_1000FULL;
-				tp->ups_info.speed_duplex = NWAY_1000M_FULL;
+				speed_duplex = NWAY_1000M_FULL;
 			}
 
-			if (orig != new1)
-				r8152_mdio_write(tp, MII_CTRL1000, new1);
+			if (orig != new1) {
+				ret = r8152_mdio_write(tp, MII_CTRL1000, new1);
+				if (ret < 0)
+					goto out;
+			}
 		}
 
-		if (tp->support_2500full || tp->support_5000full || tp->support_10000full) {
-			orig = ocp_reg_read(tp, OCP_10GBT_CTRL);
-			new1 = orig & ~(MDIO_AN_10GBT_CTRL_ADV2_5G | MDIO_AN_10GBT_CTRL_ADV5G
-					| MDIO_AN_10GBT_CTRL_ADV10G);
+		if (tp->support_2500full || tp->support_5000full ||
+		    tp->support_10000full) {
+			ret = ocp_reg_read(tp, OCP_10GBT_CTRL, &orig);
+			if (ret < 0)
+				goto out;
+
+			new1 = orig & ~(MDIO_AN_10GBT_CTRL_ADV2_5G |
+					MDIO_AN_10GBT_CTRL_ADV5G |
+					MDIO_AN_10GBT_CTRL_ADV10G);
 
 			if (advertising & RTL_ADVERTISED_2500_FULL) {
 				new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G;
-				tp->ups_info.speed_duplex = NWAY_2500M_FULL;
+				speed_duplex = NWAY_2500M_FULL;
 			}
 
 			if (advertising & RTL_ADVERTISED_5000_FULL) {
 				new1 |= MDIO_AN_10GBT_CTRL_ADV5G;
-				tp->ups_info.speed_duplex = NWAY_5000M_FULL;
+				speed_duplex = NWAY_5000M_FULL;
 			}
 
 			if (advertising & RTL_ADVERTISED_10000_FULL) {
 				new1 |= MDIO_AN_10GBT_CTRL_ADV10G;
-				tp->ups_info.speed_duplex = NWAY_10000M_FULL;
+				speed_duplex = NWAY_10000M_FULL;
 			}
 
-			if (orig != new1)
-				ocp_reg_write(tp, OCP_10GBT_CTRL, new1);
+			if (orig != new1) {
+				ret = ocp_reg_write(tp, OCP_10GBT_CTRL, new1);
+				if (ret < 0)
+					goto out;
+			}
 		}
 
 		bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
 
-		tp->mii.force_media = 0;
+		force_media = false;
 	}
 
 	if (test_and_clear_bit(PHY_RESET, &tp->flags))
 		bmcr |= BMCR_RESET;
 
-	r8152_mdio_write(tp, MII_BMCR, bmcr);
+	ret = r8152_mdio_write(tp, MII_BMCR, bmcr);
+	if (ret < 0) {
+		if (bmcr & BMCR_RESET)
+			set_bit(PHY_RESET, &tp->flags);
+		goto out;
+	}
+
+	tp->ups_info.speed_duplex = speed_duplex;
+	tp->mii.full_duplex = full_duplex;
+	tp->mii.force_media = force_media;
 
 	if (bmcr & BMCR_RESET) {
 		int i;
 
 		for (i = 0; i < 50; i++) {
 			msleep(20);
-			if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
+			ret = r8152_mdio_read(tp, MII_BMCR);
+			if (ret >= 0 && !(ret & BMCR_RESET))
+				break;
+			if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
 				break;
 		}
+
+		/* The BMCR write was issued; ignore the wait result. */
+		ret = 0;
 	}
 
 out:
-	return ret;
+	return ret < 0 ? ret : 0;
 }
 
 static void rtl8152_up(struct r8152 *tp)
@@ -7213,7 +7448,12 @@ static bool rtl8152_in_nway(struct r8152 *tp)
 
 static bool rtl8153_in_nway(struct r8152 *tp)
 {
-	u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
+	u16 phy_state;
+
+	if (ocp_reg_read(tp, OCP_PHY_STATE, &phy_state) < 0)
+		return true;
+
+	phy_state &= 0xff;
 
 	if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
 		return false;
@@ -7229,7 +7469,9 @@ static void r8156_mdio_force_mode(struct r8152 *tp)
 	 * 0: MDIO force mode
 	 * 1: MMD force mode
 	 */
-	data = ocp_reg_read(tp, 0xa5b4);
+	if (ocp_reg_read(tp, 0xa5b4, &data) < 0)
+		return;
+
 	if (data & BIT(15)) {
 		data &= ~BIT(15);
 		ocp_reg_write(tp, 0xa5b4, data);
@@ -7240,8 +7482,18 @@ static void set_carrier(struct r8152 *tp)
 {
 	struct net_device *netdev = tp->netdev;
 	struct napi_struct *napi = &tp->napi;
+	int bmsr;
+
+	bmsr = r8152_mdio_read(tp, MII_BMSR);
+	if (bmsr < 0) {
+		if (bmsr != -ENODEV) {
+			set_bit(RTL8152_LINK_CHG, &tp->flags);
+			schedule_delayed_work(&tp->schedule, HZ / 10);
+		}
+		return;
+	}
 
-	if (r8152_mdio_read(tp, MII_BMSR) & BMSR_LSTATUS) {
+	if (bmsr & BMSR_LSTATUS) {
 		if (!netif_carrier_ok(netdev)) {
 			tp->rtl_ops.enable(tp);
 			netif_stop_queue(netdev);
@@ -7869,19 +8121,20 @@ static void r8156_hw_phy_cfg(struct r8152 *tp)
 		ocp_reg_clr_bits(tp, 0xa86a, BIT(0));
 
 		/* MDI SWAP */
+		ocp_reg_read(tp, 0xd068, &data);
 		if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) &&
-		    (ocp_reg_read(tp, 0xd068) & BIT(1))) {
+		    (data & BIT(1))) {
 			u16 swap_a, swap_b;
 
-			data = ocp_reg_read(tp, 0xd068);
+			ocp_reg_read(tp, 0xd068, &data);
 			data &= ~0x1f;
 			data |= 0x1; /* p0 */
 			ocp_reg_write(tp, 0xd068, data);
-			swap_a = ocp_reg_read(tp, 0xd06a);
+			ocp_reg_read(tp, 0xd06a, &swap_a);
 			data &= ~0x18;
 			data |= 0x18; /* p3 */
 			ocp_reg_write(tp, 0xd068, data);
-			swap_b = ocp_reg_read(tp, 0xd06a);
+			ocp_reg_read(tp, 0xd06a, &swap_b);
 			data &= ~0x18; /* p0 */
 			ocp_reg_write(tp, 0xd068, data);
 			ocp_reg_write(tp, 0xd06a,
@@ -7893,11 +8146,11 @@ static void r8156_hw_phy_cfg(struct r8152 *tp)
 			data &= ~0x18;
 			data |= 0x08; /* p1 */
 			ocp_reg_write(tp, 0xd068, data);
-			swap_a = ocp_reg_read(tp, 0xd06a);
+			ocp_reg_read(tp, 0xd06a, &swap_a);
 			data &= ~0x18;
 			data |= 0x10; /* p2 */
 			ocp_reg_write(tp, 0xd068, data);
-			swap_b = ocp_reg_read(tp, 0xd06a);
+			ocp_reg_read(tp, 0xd06a, &swap_b);
 			data &= ~0x18;
 			data |= 0x08; /* p1 */
 			ocp_reg_write(tp, 0xd068, data);
@@ -7908,16 +8161,16 @@ static void r8156_hw_phy_cfg(struct r8152 *tp)
 			ocp_reg_write(tp, 0xd068, data);
 			ocp_reg_write(tp, 0xd06a,
 				      (swap_b & ~0x7ff) | (swap_a & 0x7ff));
-			swap_a = ocp_reg_read(tp, 0xbd5a);
-			swap_b = ocp_reg_read(tp, 0xbd5c);
+			ocp_reg_read(tp, 0xbd5a, &swap_a);
+			ocp_reg_read(tp, 0xbd5c, &swap_b);
 			ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) |
 				      ((swap_b & 0x1f) << 8) |
 				      ((swap_b >> 8) & 0x1f));
 			ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) |
 				      ((swap_a & 0x1f) << 8) |
 				      ((swap_a >> 8) & 0x1f));
-			swap_a = ocp_reg_read(tp, 0xbc18);
-			swap_b = ocp_reg_read(tp, 0xbc1a);
+			ocp_reg_read(tp, 0xbc18, &swap_a);
+			ocp_reg_read(tp, 0xbc1a, &swap_b);
 			ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) |
 				      ((swap_b & 0x1f) << 8) |
 				      ((swap_b >> 8) & 0x1f));
@@ -9396,8 +9649,15 @@ int rtl8152_get_link_ksettings(struct net_device *netdev,
 			 cmd->link_modes.supported, tp->support_10000full);
 
 	if (tp->support_2500full || tp->support_5000full || tp->support_10000full) {
-		u16 ocp_10gbt_ctrl = ocp_reg_read(tp, OCP_10GBT_CTRL);
-		u16 ocp_10gbt_stat = ocp_reg_read(tp, OCP_10GBT_STAT);
+		u16 ocp_10gbt_ctrl, ocp_10gbt_stat;
+
+		ret = ocp_reg_read(tp, OCP_10GBT_CTRL, &ocp_10gbt_ctrl);
+		if (ret < 0)
+			goto out_unlock;
+
+		ret = ocp_reg_read(tp, OCP_10GBT_STAT, &ocp_10gbt_stat);
+		if (ret < 0)
+			goto out_unlock;
 
 		if (tp->support_2500full) {
 			linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
@@ -9439,6 +9699,7 @@ int rtl8152_get_link_ksettings(struct net_device *netdev,
 		}
 	}
 
+out_unlock:
 	mutex_unlock(&tp->control);
 
 	usb_autopm_put_interface(tp->intf);
@@ -9625,21 +9886,37 @@ static int r8153_get_eee(struct r8152 *tp, struct ethtool_keee *eee)
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common) = {};
 	u16 speed = rtl8152_get_speed(tp);
 	u16 val;
+	int ret;
+
+	ret = ocp_reg_read(tp, OCP_EEE_ABLE, &val);
+	if (ret < 0)
+		goto out;
 
-	val = ocp_reg_read(tp, OCP_EEE_ABLE);
 	mii_eee_cap1_mod_linkmode_t(eee->supported, val);
 
-	val = ocp_reg_read(tp, OCP_EEE_ADV);
+	ret = ocp_reg_read(tp, OCP_EEE_ADV, &val);
+	if (ret < 0)
+		goto out;
+
 	mii_eee_cap1_mod_linkmode_t(eee->advertised, val);
 
-	val = ocp_reg_read(tp, OCP_EEE_LPABLE);
+	ret = ocp_reg_read(tp, OCP_EEE_LPABLE, &val);
+	if (ret < 0)
+		goto out;
+
 	mii_eee_cap1_mod_linkmode_t(eee->lp_advertised, val);
 
 	if (tp->support_2500full || tp->support_5000full) {
-		val = ocp_reg_read(tp, OCP_EEE_ADV2);
+		ret = ocp_reg_read(tp, OCP_EEE_ADV2, &val);
+		if (ret < 0)
+			goto out;
+
 		mii_eee_cap2_mod_linkmode_adv_t(eee->advertised, val);
 
-		val = ocp_reg_read(tp, OCP_EEE_LPABLE2);
+		ret = ocp_reg_read(tp, OCP_EEE_LPABLE2, &val);
+		if (ret < 0)
+			goto out;
+
 		mii_eee_cap2_mod_linkmode_adv_t(eee->lp_advertised, val);
 	}
 
@@ -9675,7 +9952,8 @@ static int r8153_get_eee(struct r8152 *tp, struct ethtool_keee *eee)
 	linkmode_and(common, common, eee->lp_advertised);
 	eee->eee_active = !linkmode_empty(common);
 
-	return 0;
+out:
+	return ret < 0 ? ret : 0;
 }
 
 static int
@@ -9915,7 +10193,7 @@ static int rtl8152_set_ringparam(struct net_device *netdev,
 static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
 {
 	struct r8152 *tp = netdev_priv(netdev);
-	u16 bmcr, lcladv, rmtadv;
+	int bmcr, lcladv, rmtadv;
 	u8 cap;
 
 	if (usb_autopm_get_interface(tp->intf) < 0)
@@ -9931,7 +10209,10 @@ static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pau
 
 	usb_autopm_put_interface(tp->intf);
 
-	if (!(bmcr & BMCR_ANENABLE)) {
+	/* Without a readable PHY, or with autoneg off, there is no flow
+	 * control to report.
+	 */
+	if (bmcr < 0 || lcladv < 0 || rmtadv < 0 || !(bmcr & BMCR_ANENABLE)) {
 		pause->autoneg = 0;
 		pause->rx_pause = 0;
 		pause->tx_pause = 0;
@@ -9962,7 +10243,11 @@ static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_paus
 
 	mutex_lock(&tp->control);
 
-	if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) {
+	ret = r8152_mdio_read(tp, MII_BMCR);
+	if (ret < 0)
+		goto out;
+
+	if (pause->autoneg && !(ret & BMCR_ANENABLE)) {
 		ret = -EINVAL;
 		goto out;
 	}
@@ -9973,16 +10258,20 @@ static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_paus
 	if (pause->tx_pause)
 		cap |= FLOW_CTRL_TX;
 
-	old = r8152_mdio_read(tp, MII_ADVERTISE);
+	ret = r8152_mdio_read(tp, MII_ADVERTISE);
+	if (ret < 0)
+		goto out;
+
+	old = ret;
 	new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap);
 	if (old != new1)
-		r8152_mdio_write(tp, MII_ADVERTISE, new1);
+		ret = r8152_mdio_write(tp, MII_ADVERTISE, new1);
 
 out:
 	mutex_unlock(&tp->control);
 	usb_autopm_put_interface(tp->intf);
 
-	return ret;
+	return ret < 0 ? ret : 0;
 }
 
 static const struct ethtool_ops ops = {
@@ -10016,7 +10305,7 @@ static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
 {
 	struct r8152 *tp = netdev_priv(netdev);
 	struct mii_ioctl_data *data = if_mii(rq);
-	int res;
+	int res, val;
 
 	if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
 		return -ENODEV;
@@ -10032,7 +10321,11 @@ static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
 
 	case SIOCGMIIREG:
 		mutex_lock(&tp->control);
-		data->val_out = r8152_mdio_read(tp, data->reg_num);
+		val = r8152_mdio_read(tp, data->reg_num);
+		if (val < 0)
+			res = val;
+		else
+			data->val_out = val;
 		mutex_unlock(&tp->control);
 		break;
 
@@ -10042,7 +10335,7 @@ static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
 			break;
 		}
 		mutex_lock(&tp->control);
-		r8152_mdio_write(tp, data->reg_num, data->val_in);
+		res = r8152_mdio_write(tp, data->reg_num, data->val_in);
 		mutex_unlock(&tp->control);
 		break;
 
@@ -10205,6 +10498,8 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->in_nway		= rtl8152_in_nway;
 		ops->hw_phy_cfg		= r8152b_hw_phy_cfg;
 		ops->autosuspend_en	= rtl_runtime_suspend_enable;
+		ops->phy_read		= r8152_phy_read;
+		ops->phy_write		= r8152_phy_write;
 		tp->rx_buf_sz		= 16 * 1024;
 		tp->eee_en		= true;
 		tp->eee_adv		= MDIO_EEE_100TX;
@@ -10227,6 +10522,8 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->hw_phy_cfg		= r8153_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8153_runtime_enable;
 		ops->change_mtu		= rtl8153_change_mtu;
+		ops->phy_read		= r8152_phy_read;
+		ops->phy_write		= r8152_phy_write;
 		if (tp->udev->speed < USB_SPEED_SUPER)
 			tp->rx_buf_sz	= 16 * 1024;
 		else
@@ -10250,6 +10547,8 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->hw_phy_cfg		= r8153b_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8153b_runtime_enable;
 		ops->change_mtu		= rtl8153_change_mtu;
+		ops->phy_read		= r8152_phy_read;
+		ops->phy_write		= r8152_phy_write;
 		tp->rx_buf_sz		= 32 * 1024;
 		tp->eee_en		= true;
 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
@@ -10274,6 +10573,8 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->hw_phy_cfg		= r8156_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8156_runtime_enable;
 		ops->change_mtu		= rtl8156_change_mtu;
+		ops->phy_read		= r8152_phy_read;
+		ops->phy_write		= r8152_phy_write;
 		tp->rx_buf_sz		= 48 * 1024;
 		tp->support_2500full	= 1;
 		r8152_desc_init(tp);
@@ -10299,6 +10600,8 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->hw_phy_cfg		= r8156b_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8156_runtime_enable;
 		ops->change_mtu		= rtl8156_change_mtu;
+		ops->phy_read		= r8152_phy_read;
+		ops->phy_write		= r8152_phy_write;
 		tp->rx_buf_sz		= 48 * 1024;
 		r8152_desc_init(tp);
 		break;
@@ -10316,6 +10619,8 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->hw_phy_cfg		= r8153c_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8153c_runtime_enable;
 		ops->change_mtu		= rtl8153c_change_mtu;
+		ops->phy_read		= r8152_phy_read;
+		ops->phy_write		= r8152_phy_write;
 		tp->rx_buf_sz		= 32 * 1024;
 		tp->eee_en		= true;
 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
@@ -10338,6 +10643,8 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->hw_phy_cfg		= r8157_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8157_runtime_enable;
 		ops->change_mtu		= rtl8157_change_mtu;
+		ops->phy_read		= r8157_phy_read;
+		ops->phy_write		= r8157_phy_write;
 		tp->rx_buf_sz		= 32 * 1024;
 		tp->support_2500full	= 1;
 		tp->support_5000full	= 1;
@@ -10361,6 +10668,8 @@ static int rtl_ops_init(struct r8152 *tp)
 		ops->hw_phy_cfg		= r8159_hw_phy_cfg;
 		ops->autosuspend_en	= rtl8157_runtime_enable;
 		ops->change_mtu		= rtl8157_change_mtu;
+		ops->phy_read		= r8157_phy_read;
+		ops->phy_write		= r8157_phy_write;
 		tp->rx_buf_sz		= 48 * 1024;
 		tp->support_2500full	= 1;
 		tp->support_5000full	= 1;
Signature · 1 line
2.34.1
#19 lore raw

Re: [PATCH net-next v10 11/12] r8152: add TGPHY register access for RTL8157 and RTL8159

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] If the TGPHY busy bit stays set, set_carrier() retries every 100 ms with no limit and no backoff, for as long as the interface is…
- [Medium] On RTL8157/8159 a single PHY write can now be dropped while later writes still go through.
- [Low] The commit message says rtl_phy_patch_request() 'releases PATCH_REQUEST when a later step fails, so a failed access does not…

--

r8152: add TGPHY register access for RTL8157 and RTL8159

This patch adds a TGPHY command channel for PHY register access on RTL8157 and RTL8159. ocp_reg_read() and ocp_reg_write() now go through per-chip rtl_ops.phy_read/phy_write hooks. The PHY accessors and the helpers built on them return int, and selected callers act on failures.

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
index 392b7015f4925..b9586c30c04dd 100644

[ ... ]

Quoted 1 line
drivers/net/usb/r8152.c +0 -0
@@ -1911,100 +1958,221 @@ static void ocp_byte_set_bits(struct r8152 *tp, u16 type, u16 index, u8 set)

[ ... ]

Quoted 15 lines
drivers/net/usb/r8152.c +15 -0
+static int wait_tgphy_cmd_ready(struct r8152 *tp)
+{
+	u16 ocp_data;
+	int ret;
+
+	ret = read_poll_timeout(ocp_read_word, ocp_data,
+				test_bit(RTL8152_INACCESSIBLE, &tp->flags) ||
+				!(ocp_data & TGPHY_CMD_BUSY),
+				2000, 20000, false, tp,
+				MCU_TYPE_USB, USB_TGPHY_CMD);
+
+	if (ret)
+		dev_err_ratelimited(&tp->intf->dev, "TGPHY cmd busy timeout\n");
+
+	return test_bit(RTL8152_INACCESSIBLE, &tp->flags) ? -ENODEV : ret;

drivers/net/usb/r8152.c static void ocp_byte_set_bits(struct r8152 *tp, u16 type, u16 index, u8 set) · line 2140

Quoted 10 lines
drivers/net/usb/r8152.c +10 -0
+}
+
+static int rtl_tgphy_access(struct r8152 *tp, u16 addr, u16 *data, bool write)
+{
+	u16 cmd = 0;
+	int ret;
+
+	ret = wait_tgphy_cmd_ready(tp);
+	if (ret < 0)
+		goto out;

[Severity: Medium]
On RTL8157/8159, can a single dropped PHY write now corrupt the PHY patch RAM without any error being reported?

Before this patch, these chips used the OCP window. There, a failed control transfer goes through r8152_control_msg() and rtl_set_inaccessible(), which blocks every later write. A register sequence therefore either completed or stopped.

Here a busy timeout skips this one access and returns -ETIMEDOUT. The device stays accessible, so if the busy bit clears later, the next write goes through.

The PHY loaders write OCP_SRAM_ADDR once and then stream data words, relying on the hardware to auto-increment the address. They ignore every return value:

drivers/net/usb/r8152.c:rtl8152_fw_phy_nc_apply() {
    ...
	ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
	for (i = 0; i < num; i++)
		ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
    ...
}

The same pattern appears in rtl8152_fw_phy_union_apply(). It also appears in open-coded SRAM/SRAM2 bursts in r8157_hw_phy_cfg() (0x8f9c, 0x8566, 0x82c8, 0x82a0) and r8159_hw_phy_cfg() (0x81c4).

A dropped DATA write would shift every later word down one SRAM address. A dropped ADDR write would send the whole block to wherever the address pointer was left.

Nothing reads the data back. rtl8152_apply_firmware() also ignores the errno that rtl_pre_ram_code() now returns, and it still records rtl_fw->version and logs success.

The commit message says the sram_*() and hw_phy_cfg callers still ignore the status. Could it also mention, or the code prevent, this partial-stream case? It was not possible with the OCP path.

[ ... ]

Quoted 3 lines
drivers/net/usb/r8152.c +0 -0
@@ -4597,36 +4767,52 @@ static inline void rtl_reset_ocp_base(struct r8152 *tp)
 static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait)
 {

[ ... ]

Quoted 23 lines
drivers/net/usb/r8152.c +11 -6
-	for (i = 0; wait && i < 5000; i++) {
-		u32 ocp_data;
+	if (ret < 0)
+		return ret;
 
-		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
-			return -ENODEV;
+	for (i = 0; wait && i < 5000; i++) {
+		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
+			ret = -ENODEV;
+			goto err;
+		}
 
 		usleep_range(1000, 2000);
-		ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT);
-		if ((ocp_data & PATCH_READY) ^ check)
+		ret = ocp_reg_read(tp, OCP_PHY_PATCH_STAT, &ocp_data);
+		if (ret < 0)
+			goto err;
+		else if ((ocp_data & PATCH_READY) ^ check)
 			break;
 	}

[ ... ]

drivers/net/usb/r8152.c static inline void rtl_reset_ocp_base(struct r8152 *tp) · line 4816

Quoted 8 lines
drivers/net/usb/r8152.c +7 -0
+	return 0;
+
+err:
+	if (request)
+		rtl_phy_patch_request(tp, false, false);
+
+	return ret;
 }

[Severity: Low]
The commit message says:

rtl_phy_patch_request() returns the errno of a failed write, and releases PATCH_REQUEST when a later step fails, so a failed access does not leave it asserted.

Is that guaranteed on this err path?

The release is an ocp_reg_clr_bits() read-modify-write of OCP_PHY_PATCH_CMD. It goes through the same TGPHY channel that just failed, and its return value is discarded.

Suppose the original failure was a busy timeout, for example on the OCP_PHY_PATCH_STAT read in the wait loop. The release's own wait_tgphy_cmd_ready() would then most likely time out too. PATCH_REQUEST would stay set, and the caller would only see the original errno.

The success-path release is also unchecked. This applies to rtl_post_ram_code() and to the r8153b, r8156, r8156b, r8157 and r8159 hw_phy_cfg functions:

	rtl_phy_patch_request(tp, false, true);

A dropped clear there would leave the PHY MCU in patch-request mode until the next full hw_phy_cfg.

Could the commit message be reworded, or could the result of the release be checked?

[ ... ]

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
@@ -7240,8 +7482,18 @@ static void set_carrier(struct r8152 *tp)
 {
 	struct net_device *netdev = tp->netdev;
 	struct napi_struct *napi = &tp->napi;

drivers/net/usb/r8152.c static void set_carrier(struct r8152 *tp) · line 7494

Quoted 10 lines
drivers/net/usb/r8152.c +10 -0
+	int bmsr;
+
+	bmsr = r8152_mdio_read(tp, MII_BMSR);
+	if (bmsr < 0) {
+		if (bmsr != -ENODEV) {
+			set_bit(RTL8152_LINK_CHG, &tp->flags);
+			schedule_delayed_work(&tp->schedule, HZ / 10);
+		}
+		return;
+	}

[Severity: Medium]
If the TGPHY busy bit stays set on RTL8157/8159, does this retry forever?

wait_tgphy_cmd_ready() returns -ETIMEDOUT without setting RTL8152_INACCESSIBLE, so bmsr is never -ENODEV here. The following chain then repeats every 100 ms with no retry limit or backoff:

rtl_work_func_t()
set_carrier()

    r8152_mdio_read(tp, MII_BMSR)
      rtl_tgphy_access()
        wait_tgphy_cmd_ready()   -> -ETIMEDOUT
    set_bit(RTL8152_LINK_CHG, &tp->flags)
    schedule_delayed_work(&tp->schedule, HZ / 10)

It only stops when the device is marked inaccessible or the interface goes down.

While it runs, each pass does a usb_autopm get/put. Each pass also holds tp->control for 20 to 40 ms out of every 100 ms. The ethtool handlers, the MII ioctls and hw_phy_work all wait on that mutex.

Each pass also reaches dev_err_ratelimited(). The default ratelimit of 10 messages per 5 seconds still lets about 2 lines per second through, with no end. Does that match this statement in the commit message?

A timeout is logged ratelimited, so a stuck busy bit does not flood the log

In addition, delay_autosuspend() returns true whenever the delayed work is pending:

	if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
		return true;

rtl8153_in_nway() also returns true on a failed read. Runtime autosuspend would therefore be blocked for as long as the fault lasts.

The commit message leaves recovery from a TGPHY timeout to a follow-up series. Should this retry be bounded in the meantime?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1-nic_swsd%40realtek.com
#20 lore raw

PATCH v10 12/12 r8152: enable UPS for RTL8157 and RTL8159 QFN68

In reply to Chih Kai Hsu

RTL8157 and RTL8159 QFN68 support UPS, but rtl8157_runtime_enable() never toggled it on runtime suspend/resume, and r8156_ups_flags() used the older per-chip EEE/lite_mode encoding instead of the per-speed EEE flags their UPS firmware expects. RTL8159 QFN100 does not support UPS.

Add r8157_ups_en(), mirroring r8156_ups_en(), and call it from rtl8157_runtime_enable() for VER_16/QFN68 only; QFN100 keeps its existing no-UPS behaviour there. Use r8157_ups_en(tp, false) in r8157_init() in place of r8156_ups_en(tp, false).

Extend r8156_ups_flags() with per-speed EEE flags (UPS_FLAGS_EN_100M_EEE/1000M_EEE/2500M_EEE/5000M_EEE/10G_EEE) and skip the lite_mode bits[6:5] encoding for VER_16/QFN68. Add ups_flags_speed() entries 10 and 11 for NWAY_5000M_FULL and NWAY_10000M_FULL, previously unmapped.

Add the USB_FW_USB_VER register (0xcfd7), used by the new r8157_ups_en() disable path. A power cut clears the PLA firmware version register along with the patch, but the USB one survives while the USB patch is lost. rtl8152_fw_mac_apply() skips a block whose version register is already current, so the disable path clears the USB version before hw_phy_cfg() applies the firmware again.

The disable path waits for the backup-restore to finish after a power cut on RTL8157 as well as RTL8159, before reconfiguring the PHY. If that wait in r8157_ups_en() times out, it marks the device inaccessible and logs, without queuing a reset, as r8157_init() does for RTL8159. During runtime resume the interface then stays unusable until a replug or an unrelated reset, since r8157_ups_en() returns void and the error is not passed back. Recovery for these timeouts is left to a follow-up series.

---
 drivers/net/usb/r8152.c | 131 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 120 insertions(+), 11 deletions(-)
drivers/net/usb/r8152.c +120 -11
index b9586c30c04dd..0fb6dfe4abf56 100644
@@ -132,6 +132,7 @@
 #define USB_BURST_SIZE		0xcfc0
 #define USB_FW_FIX_EN0		0xcfca
 #define USB_FW_FIX_EN1		0xcfcc
+#define USB_FW_USB_VER		0xcfd7
 #define USB_LPM_CONFIG		0xcfd8
 #define USB_ECM_OPTION		0xcfee
 #define USB_CSTMR		0xcfef	/* RTL8153A */
@@ -619,6 +620,11 @@
 #define UPS_FLAGS_250M_CKDIV		BIT(2)
 #define UPS_FLAGS_EN_ALDPS		BIT(3)
 #define UPS_FLAGS_CTAP_SHORT_DIS	BIT(4)
+#define UPS_FLAGS_EN_100M_EEE		BIT(9)
+#define UPS_FLAGS_EN_1000M_EEE		BIT(10)
+#define UPS_FLAGS_EN_2500M_EEE		BIT(11)
+#define UPS_FLAGS_EN_5000M_EEE		BIT(12)
+#define UPS_FLAGS_EN_10G_EEE		BIT(13)
 #define UPS_FLAGS_SPEED_MASK		(0xf << 16)
 #define ups_flags_speed(x)		((x) << 16)
 #define UPS_FLAGS_EN_EEE		BIT(20)
@@ -4256,8 +4262,27 @@ static void r8156_ups_flags(struct r8152 *tp)
 	if (tp->ups_info.aldps)
 		ups_flags |= UPS_FLAGS_EN_ALDPS;
 
-	if (tp->ups_info.eee)
-		ups_flags |= UPS_FLAGS_EN_EEE;
+	if (tp->ups_info.eee) {
+		switch (tp->version) {
+		case RTL_VER_17_QFN68:
+			if (tp->eee_adv & MDIO_EEE_10GT)
+				ups_flags |= UPS_FLAGS_EN_10G_EEE;
+			fallthrough;
+		case RTL_VER_16:
+			if (tp->eee_adv & MDIO_EEE_100TX)
+				ups_flags |= UPS_FLAGS_EN_100M_EEE;
+			if (tp->eee_adv & MDIO_EEE_1000T)
+				ups_flags |= UPS_FLAGS_EN_1000M_EEE;
+			if (tp->eee_adv2 & MDIO_EEE_2_5GT)
+				ups_flags |= UPS_FLAGS_EN_2500M_EEE;
+			if (tp->eee_adv2 & MDIO_EEE_5GT)
+				ups_flags |= UPS_FLAGS_EN_5000M_EEE;
+			break;
+		default:
+			ups_flags |= UPS_FLAGS_EN_EEE;
+			break;
+		}
+	}
 
 	if (tp->ups_info.flow_control)
 		ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
@@ -4308,20 +4333,33 @@ static void r8156_ups_flags(struct r8152 *tp)
 	case NWAY_2500M_FULL:
 		ups_flags |= ups_flags_speed(9);
 		break;
+	case NWAY_5000M_FULL:
+		ups_flags |= ups_flags_speed(10);
+		break;
+	case NWAY_10000M_FULL:
+		ups_flags |= ups_flags_speed(11);
+		break;
 	default:
 		break;
 	}
 
-	switch (tp->ups_info.lite_mode) {
-	case 1:
-		ups_flags |= 0 << 5;
-		break;
-	case 2:
-		ups_flags |= 2 << 5;
+	switch (tp->version) {
+	case RTL_VER_16:
+	case RTL_VER_17_QFN68:
 		break;
-	case 0:
 	default:
-		ups_flags |= 1 << 5;
+		switch (tp->ups_info.lite_mode) {
+		case 1:
+			ups_flags |= 0 << 5;
+			break;
+		case 2:
+			ups_flags |= 2 << 5;
+			break;
+		case 0:
+		default:
+			ups_flags |= 1 << 5;
+			break;
+		}
 		break;
 	}
 
@@ -4496,6 +4534,58 @@ static void r8156_ups_en(struct r8152 *tp, bool enable)
 	}
 }
 
+static void r8157_ups_en(struct r8152 *tp, bool enable)
+{
+	u32 ocp_data;
+	int ret;
+
+	if (enable) {
+		r8156_ups_flags(tp);
+
+		ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_POWER_CUT,
+				  UPS_EN | USP_PREWAKE | PHASE2_EN);
+
+		ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_MISC_2,
+				  UPS_FORCE_PWR_DOWN);
+	} else {
+		ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT,
+				  UPS_EN | USP_PREWAKE);
+
+		ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2,
+				  UPS_FORCE_PWR_DOWN);
+
+		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
+		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+			return;
+
+		if (ocp_data & PCUT_STATUS) {
+			ret = read_poll_timeout(ocp_read_word, ocp_data,
+						test_bit(RTL8152_INACCESSIBLE,
+							 &tp->flags) ||
+						(ocp_data & BACKUP_RESTRORE),
+						200, 20000, false, tp,
+						MCU_TYPE_USB, USB_GPHY_CTRL);
+			if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+				return;
+
+			if (ret) {
+				rtl_set_inaccessible(tp);
+				dev_err(&tp->intf->dev,
+					"backup-restore timed out\n");
+				return;
+			}
+
+			/* clear USB fw_ver_reg */
+			ocp_write_byte(tp, MCU_TYPE_USB, USB_FW_USB_VER, 0);
+
+			tp->rtl_ops.hw_phy_cfg(tp);
+
+			rtl8152_set_speed(tp, tp->autoneg, tp->speed,
+					  tp->duplex, tp->advertising);
+		}
+	}
+}
+
 static void r8153_power_cut_en(struct r8152 *tp, bool enable)
 {
 	if (enable)
@@ -4654,9 +4744,28 @@ static void rtl8157_runtime_enable(struct r8152 *tp, bool enable)
 		r8153b_u1u2en(tp, false);
 		r8157_u2p3en(tp, false);
 		rtl_runtime_suspend_enable(tp, true);
+
+		switch (tp->version) {
+		case RTL_VER_16:
+		case RTL_VER_17_QFN68:
+			r8157_ups_en(tp, true);
+			break;
+		default:
+			break;
+		}
 	} else {
 		r8153_queue_wake(tp, false);
 		rtl_runtime_suspend_enable(tp, false);
+
+		switch (tp->version) {
+		case RTL_VER_16:
+		case RTL_VER_17_QFN68:
+			r8157_ups_en(tp, false);
+			break;
+		default:
+			break;
+		}
+
 		r8157_u2p3en(tp, true);
 		if (tp->udev->speed >= USB_SPEED_SUPER)
 			r8153b_u1u2en(tp, true);
@@ -9148,7 +9257,7 @@ static void r8157_init(struct r8152 *tp)
 	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
 
 	r8157_power_cut_en(tp, false);
-	r8156_ups_en(tp, false);
+	r8157_ups_en(tp, false);
 	r8153_queue_wake(tp, false);
 	rtl_runtime_suspend_enable(tp, false);
 
Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH net-next v10 12/12] r8152: enable UPS for RTL8157 and RTL8159 QFN68

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] r8152: PCUT_STATUS cleared before r8157_ups_en(tp, false) in r8157_init() - [Medium] r8152: PLA…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] r8152: PCUT_STATUS cleared before r8157_ups_en(tp, false) in r8157_init()
- [Medium] r8152: PLA registers accessed before r8157_ups_en() on runtime resume
- [Medium] r8152: unrecoverable RTL8152_INACCESSIBLE latch in r8157_ups_en()
--

commit c10e0bfa00a1dd7f7d30bc58798ee583928fdb60
Author: Chih Kai Hsu <[email protected]>

r8152: enable UPS for RTL8157 and RTL8159 QFN68

This commit adds r8157_ups_en(), updates r8156_ups_flags() with per-speed EEE and speed flags, and enables UPS on runtime suspend and resume for RTL8157 and RTL8159 QFN68.

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
index b9586c30c04dd..0fb6dfe4abf56 100644

[ ... ]

Quoted 34 lines
drivers/net/usb/r8152.c +30 -0
@@ -4496,6 +4534,58 @@ static void r8156_ups_en(struct r8152 *tp, bool enable)
 	}
 }
 
+static void r8157_ups_en(struct r8152 *tp, bool enable)
+{
+	u32 ocp_data;
+	int ret;
+
+	if (enable) {
+		r8156_ups_flags(tp);
+
+		ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_POWER_CUT,
+				  UPS_EN | USP_PREWAKE | PHASE2_EN);
+
+		ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_MISC_2,
+				  UPS_FORCE_PWR_DOWN);
+	} else {
+		ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT,
+				  UPS_EN | USP_PREWAKE);
+
+		ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2,
+				  UPS_FORCE_PWR_DOWN);
+
+		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
+		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+			return;
+
+		if (ocp_data & PCUT_STATUS) {
+			ret = read_poll_timeout(ocp_read_word, ocp_data,
+						test_bit(RTL8152_INACCESSIBLE,
+							 &tp->flags) ||
+						(ocp_data & BACKUP_RESTRORE),
+						200, 20000, false, tp,

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable) · line 4576

Quoted 10 lines
drivers/net/usb/r8152.c +10 -0
+						MCU_TYPE_USB, USB_GPHY_CTRL);
+			if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+				return;
+
+			if (ret) {
+				rtl_set_inaccessible(tp);
+				dev_err(&tp->intf->dev,
+					"backup-restore timed out\n");
+				return;
+			}

[Severity: Medium]
As reported by Jakub Kicinski on v7 [1], can latching RTL8152_INACCESSIBLE here leave the interface permanently unusable after a runtime resume?

When read_poll_timeout() times out during rtl8152_runtime_resume() -> rtl8157_runtime_enable(tp, false) -> r8157_ups_en(tp, false), the error is not returned to the PM core and no USB reset is queued, while rtl8152_resume() skips its usb_reset_device() fallback when runtime_resume is true:

drivers/net/usb/r8152.c:rtl8152_resume() {
    ...
	if (!runtime_resume && test_bit(RTL8152_INACCESSIBLE, &tp->flags))
		usb_reset_device(tp->udev);
    ...
}

Every subsequent r8152_control_msg() then returns -ENODEV before reaching usb_queue_reset_device(), so nothing clears RTL8152_INACCESSIBLE until the device is replugged or reset externally.

Quoted 30 lines
drivers/net/usb/r8152.c +21 -0
+
+			/* clear USB fw_ver_reg */
+			ocp_write_byte(tp, MCU_TYPE_USB, USB_FW_USB_VER, 0);
+
+			tp->rtl_ops.hw_phy_cfg(tp);
+
+			rtl8152_set_speed(tp, tp->autoneg, tp->speed,
+					  tp->duplex, tp->advertising);
+		}
+	}
+}
+
 static void r8153_power_cut_en(struct r8152 *tp, bool enable)
 {
 	if (enable)
@@ -4654,9 +4744,28 @@ static void rtl8157_runtime_enable(struct r8152 *tp, bool enable)
 		r8153b_u1u2en(tp, false);
 		r8157_u2p3en(tp, false);
 		rtl_runtime_suspend_enable(tp, true);
+
+		switch (tp->version) {
+		case RTL_VER_16:
+		case RTL_VER_17_QFN68:
+			r8157_ups_en(tp, true);
+			break;
+		default:
+			break;
+		}
 	} else {
 		r8153_queue_wake(tp, false);

drivers/net/usb/r8152.c static void rtl8157_runtime_enable(struct r8152 *tp, bool enable) · line 4767

Quoted 10 lines
drivers/net/usb/r8152.c +9 -0
 		rtl_runtime_suspend_enable(tp, false);
+
+		switch (tp->version) {
+		case RTL_VER_16:
+		case RTL_VER_17_QFN68:
+			r8157_ups_en(tp, false);
+			break;
+		default:
+			break;
+		}

[Severity: Medium]
Should r8157_ups_en(tp, false) run before r8153_queue_wake(tp, false) and rtl_runtime_suspend_enable(tp, false), matching rtl8153b_runtime_enable(), rtl8153c_runtime_enable(), and r8157_init()?

Calling r8153_queue_wake(tp, false) and rtl_runtime_suspend_enable(tp, false) first accesses MCU_TYPE_PLA registers while UPS is still active and before r8157_ups_en(tp, false) waits for BACKUP_RESTRORE after a power cut, so the writes clearing UPCOMING_RUNTIME_D3 and LINK_OFF_WAKE_EN and restoring tp->saved_wolopts can be ignored or overwritten by the restored suspend-time snapshot.

drivers/net/usb/r8152.c static void r8157_init(struct r8152 *tp) · line 9262

Quoted 12 lines
drivers/net/usb/r8152.c +2 -1
+
 		r8157_u2p3en(tp, true);
 		if (tp->udev->speed >= USB_SPEED_SUPER)
 			r8153b_u1u2en(tp, true);
@@ -9148,7 +9257,7 @@ static void r8157_init(struct r8152 *tp)
 	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
 
 	r8157_power_cut_en(tp, false);
-	r8156_ups_en(tp, false);
+	r8157_ups_en(tp, false);
 	r8153_queue_wake(tp, false);
 	rtl_runtime_suspend_enable(tp, false);

[Severity: Medium]
Does r8157_power_cut_en(tp, false) clear PCUT_STATUS before r8157_ups_en(tp, false) can check it?

drivers/net/usb/r8152.c:r8157_power_cut_en() {
    ...
	} else {
		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, PWR_EN);
		ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS);
		ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2, BIT(1));
	}
}

Because PCUT_STATUS in USB_MISC_0 is cleared on the preceding line, the if (ocp_data & PCUT_STATUS) block in r8157_ups_en(tp, false) is never entered from r8157_init(). When re-initializing via r8157_init() after a UPS power cut (such as on rtl8152_reset_resume()), RTL_VER_16 skips waiting for BACKUP_RESTRORE, and neither RTL_VER_16 nor RTL_VER_17_QFN68 clears USB_FW_USB_VER, causing rtl8152_fw_mac_apply() to see the surviving version register and skip reloading the lost USB firmware patch.

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#22 lore raw

Re: [PATCH net-next v10 12/12] r8152: enable UPS for RTL8157 and RTL8159 QFN68

In reply to Chih Kai Hsu
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 5 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The new power-cut recovery in r8157_ups_en(false) can never run from r8157_init(), so a reset-resume after a UPS power cut does…
- [Medium] The driver treats RTL8157 (RTL_VER_16) backup-restore two different ways.
- [Medium] Runtime resume has a new failure path with no recovery.
- [Medium] After a UPS power cut, r8157_ups_en(false) starts the fixed 20 ms BACKUP_RESTRORE poll and then calls hw_phy_cfg() without first…
- [Medium] r8152: PLA registers accessed before r8157_ups_en() on runtime resume
- [Low] r8157_ups_en(false) forces the USB MAC firmware to be re-applied by writing 0 to a hardcoded address, USB_FW_USB_VER (0xcfd7, line…

--

r8152: enable UPS for RTL8157 and RTL8159 QFN68

This patch adds r8157_ups_en() and calls it from rtl8157_runtime_enable() for RTL_VER_16 and RTL_VER_17_QFN68, so UPS is toggled across runtime suspend and resume. It also extends r8156_ups_flags() with per-speed EEE flags and two new speed encodings. On the disable path, after a power cut, it waits for backup-restore and clears USB_FW_USB_VER before the firmware is applied again.

Quoted 4 lines
drivers/net/usb/r8152.c +0 -0
index b9586c30c04dd..0fb6dfe4abf56 100644

[ ... ]

Quoted 10 lines
drivers/net/usb/r8152.c +6 -0
@@ -4496,6 +4534,58 @@ static void r8156_ups_en(struct r8152 *tp, bool enable)
 	}
 }
 
+static void r8157_ups_en(struct r8152 *tp, bool enable)
+{
+	u32 ocp_data;
+	int ret;
+
+	if (enable) {

[ ... ]

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable) · line 4561

Quoted 12 lines
drivers/net/usb/r8152.c +12 -0
+	} else {
+		ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT,
+				  UPS_EN | USP_PREWAKE);
+
+		ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2,
+				  UPS_FORCE_PWR_DOWN);
+
+		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
+		if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+			return;
+
+		if (ocp_data & PCUT_STATUS) {

[Severity: Medium]
On resume, the PLA registers are written before UPS is disabled. Is that ordering intended?

rtl8157_runtime_enable(false) first runs r8153_queue_wake(tp, false) and rtl_runtime_suspend_enable(tp, false), and only then calls r8157_ups_en(tp, false). The following are all written while UPS_EN, USP_PREWAKE and UPS_FORCE_PWR_DOWN are still set:

PLA_INDICATE_FALG, PLA_SUSPEND_FLAG, PLA_EXTRA_STATUS, PLA_CRWECR, PLA_CONFIG34, and PLA_CONFIG5 and PLA_CFG_WOL via __rtl_set_wol()

Those writes also happen before the BACKUP_RESTRORE poll has confirmed that the device has recovered from a power cut.

The existing runtime paths that toggle UPS disable it first. rtl8153b_runtime_enable() has:

	} else {
		r8153b_ups_en(tp, false);
		r8153_queue_wake(tp, false);
		rtl_runtime_suspend_enable(tp, false);

rtl8153c_runtime_enable() uses the same order. So do r8156_init() and r8157_init(). The enable side of rtl8157_runtime_enable() already arms UPS last, so only the resume side is inverted.

If PCUT_STATUS is set on resume, could these writes be lost? They clear UPCOMING_RUNTIME_D3 and LINK_OFF_WAKE_EN and restore tp->saved_wolopts. Could the backup-restore then overwrite them with the suspend-time state, which had WAKE_ANY and LINK_OFF_WAKE_EN set? The device could then come back from runtime resume still configured to wake on any event and on link-off, rather than with the wolopts the user asked for.

There is a second ordering issue. r8153_queue_wake(tp, false) clears LINK_CHG_EVENT and LINK_CHANGE_FLAG before r8157_ups_en() reprograms the PHY through hw_phy_cfg() and rtl8152_set_speed(). Any link-change state raised by that PHY reset is therefore not cleared.

Should r8157_ups_en(tp, false) move to the top of the else branch, to match rtl8153b_runtime_enable()?

[Severity: Medium]
After a UPS power cut, this starts the 20 ms BACKUP_RESTRORE poll and then calls hw_phy_cfg() without waiting for MAC autoload first. Is that safe?

The other post-power-cut paths in the driver do wait. r8153b_ups_en() has:

		if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
			if (wait_autoload_done(tp))
				return;

			tp->rtl_ops.hw_phy_cfg(tp);

r8157_init() runs r8156b_wait_loading_flash(), then wait_autoload_done(), then r8159_wait_backup_restore().

This path never calls wait_autoload_done(). r8156b_wait_loading_flash() only runs inside hw_phy_cfg(), which is after the poll.

If autoload or flash loading is still running when runtime resume starts, could the 20 ms poll time out and mark the device inaccessible? Or could the PHY be reprogrammed before autoload has finished?

r8156_ups_en(false), which this function mirrors, also skips the autoload wait. But r8156_ups_en(true) is never called anywhere, so that path has never been exercised.

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable) · line 4567

Quoted 6 lines
drivers/net/usb/r8152.c +6 -0
+			ret = read_poll_timeout(ocp_read_word, ocp_data,
+						test_bit(RTL8152_INACCESSIBLE,
+							 &tp->flags) ||
+						(ocp_data & BACKUP_RESTRORE),
+						200, 20000, false, tp,
+						MCU_TYPE_USB, USB_GPHY_CTRL);

[Severity: Medium]
The runtime path and the init path treat RTL8157 (RTL_VER_16) backup-restore differently. Which one matches the hardware?

rtl8157_runtime_enable(false) sends RTL_VER_16 into this poll, and a timeout marks the device inaccessible. r8157_init(), however, only waits for backup-restore on RTL8159:

	switch (tp->version) {
	case RTL_VER_17_QFN68:
	case RTL_VER_17_QFN100:
		if (r8159_wait_backup_restore(tp)) {

The backup-restore enables later in r8157_init() are also programmed only for the RTL8159 variants: 0xb99c BIT(0) for QFN68, and 0xb9a2 for QFN68 and QFN100.

The commit message says RTL8157 needs this wait after a power cut. If so, r8157_init() touches the PHY on RTL_VER_16 without waiting.

If RTL8157 never asserts BACKUP_RESTRORE after a UPS power cut, then every runtime resume after a power cut on RTL_VER_16 would time out here and leave the interface unusable until replug.

Also, the only place in the driver that sets BACKUP_RESTRORE is rtl_ram_code_speed_up():

		ocp_word_set_bits(tp, MCU_TYPE_USB, USB_GPHY_CTRL,
				  GPHY_PATCH_DONE | BACKUP_RESTRORE);

Suppose no PHY speed-up firmware was loaded, for example because the firmware file is missing, and the hardware doesn't set the bit itself. Can this poll ever succeed?

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable) · line 4576

Quoted 9 lines
drivers/net/usb/r8152.c +9 -0
+			if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+				return;
+
+			if (ret) {
+				rtl_set_inaccessible(tp);
+				dev_err(&tp->intf->dev,
+					"backup-restore timed out\n");
+				return;
+			}

[Severity: Medium]
If this times out during runtime resume, does anything recover the device?

r8157_ups_en() returns void, so rtl8157_runtime_enable() carries on with r8157_u2p3en() and r8153b_u1u2en(). rtl8152_runtime_resume() then returns 0. rtl8152_resume() only resets the device on system resume:

	if (!runtime_resume && test_bit(RTL8152_INACCESSIBLE, &tp->flags))
		usb_reset_device(tp->udev);

Once RTL8152_INACCESSIBLE is set, the OCP accessors short-circuit, so they never reach the reg_access_reset_count and usb_queue_reset_device() recovery either. The interface stays up but unusable until replug or an unrelated reset.

The commit message acknowledges this and leaves recovery to a follow-up series. Could this call usb_queue_reset_device(tp->intf) instead, as the driver already does on TX timeout?

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable) · line 4579

Quoted 3 lines
drivers/net/usb/r8152.c +3 -0
+
+			/* clear USB fw_ver_reg */
+			ocp_write_byte(tp, MCU_TYPE_USB, USB_FW_USB_VER, 0);

[Severity: Low]
Is it guaranteed that the loaded firmware's USB block uses 0xcfd7 as its version register?

rtl8152_fw_mac_apply() decides whether to skip a block using the address from the firmware file:

	fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
	if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) {

rtl8152_is_fw_mac_ok() doesn't check fw_ver_reg against any expected value for RTL_VER_16/17.

Suppose an rtl8157/rtl8159 firmware image declares a different fw_ver_reg for its RTL_FW_USB block. This write then has no effect, and after a power cut the USB block is skipped as already newest. It would also zero whatever register is at 0xcfd7.

Could this clear the fw_ver_reg from the loaded RTL_FW_USB block instead, or bypass the version check after a power cut?

Quoted 8 lines
drivers/net/usb/r8152.c +8 -0
+
+			tp->rtl_ops.hw_phy_cfg(tp);
+
+			rtl8152_set_speed(tp, tp->autoneg, tp->speed,
+					  tp->duplex, tp->advertising);
+		}
+	}
+}

[ ... ]

drivers/net/usb/r8152.c static void r8157_init(struct r8152 *tp) · line 9260

Quoted 6 lines
drivers/net/usb/r8152.c +1 -1
@@ -9148,7 +9257,7 @@ static void r8157_init(struct r8152 *tp)
 	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
 
 	r8157_power_cut_en(tp, false);
-	r8156_ups_en(tp, false);
+	r8157_ups_en(tp, false);

[Severity: Medium]
Can the power-cut branch of r8157_ups_en(tp, false) ever run from r8157_init()?

The line before it clears PCUT_STATUS:

r8157_power_cut_en()

    ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS);

So r8157_ups_en() always reads PCUT_STATUS as 0 here. It skips the backup-restore poll, the USB_FW_USB_VER clear, and the hw_phy_cfg() and rtl8152_set_speed() reprogramming.

rtl8157_runtime_enable(true) now arms UPS for RTL_VER_16 and RTL_VER_17_QFN68. If the USB core reset-resumes the device after a UPS power cut, for example after a failed resume, rtl8152_reset_resume() never reaches rtl8157_runtime_enable(false):

rtl8152_reset_resume()

    clear_bit(SELECTIVE_SUSPEND, &tp->flags);
    rtl_reset_ocp_base(tp);
    tp->rtl_ops.init(tp);
        r8157_power_cut_en(tp, false)   <- clears PCUT_STATUS
        r8157_ups_en(tp, false)         <- skips recovery
    queue_delayed_work(..., &tp->hw_phy_work, 0);
        hw_phy_cfg()
            rtl8152_fw_mac_apply()      <- sees stale USB version

The commit message says the USB version register survives the power cut while the USB patch is lost. Wouldn't rtl8152_fw_mac_apply() then skip the USB block and leave the USB MCU running without its patch?

On RTL_VER_16 this init path also has no backup-restore wait, because r8157_init() only calls r8159_wait_backup_restore() for the QFN68 and QFN100 variants.

Should PCUT_STATUS be checked before r8157_power_cut_en(tp, false) clears it? As written, replacing r8156_ups_en() with r8157_ups_en() here doesn't change the power-cut handling.

Quoted 2 lines
drivers/net/usb/r8152.c +0 -0
 	r8153_queue_wake(tp, false);
 	rtl_runtime_suspend_enable(tp, false);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1-nic_swsd%40realtek.com
#23 lore raw

RE: [PATCH net-next v10 12/12] r8152: enable UPS for RTL8157 and RTL8159 QFN68

Quoted 63 lines

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 5 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The new power-cut recovery in r8157_ups_en(false) can never run from r8157_init(), so a reset-resume after a UPS power cut does…
- [Medium] The driver treats RTL8157 (RTL_VER_16) backup-restore two different ways.
- [Medium] Runtime resume has a new failure path with no recovery.
- [Medium] After a UPS power cut, r8157_ups_en(false) starts the fixed 20 ms BACKUP_RESTRORE poll and then calls hw_phy_cfg() without first…
- [Medium] r8152: PLA registers accessed before r8157_ups_en() on runtime resume
- [Low] r8157_ups_en(false) forces the USB MAC firmware to be re-applied by writing 0 to a hardcoded address, USB_FW_USB_VER (0xcfd7, line…

--

r8152: enable UPS for RTL8157 and RTL8159 QFN68

This patch adds r8157_ups_en() and calls it from rtl8157_runtime_enable() for RTL_VER_16 and RTL_VER_17_QFN68, so UPS is toggled across runtime suspend and resume. It also extends r8156_ups_flags() with per-speed EEE flags and two new speed encodings. On the disable path, after a power cut, it waits for backup-restore and clears USB_FW_USB_VER before the firmware is applied again.

Quoted 4 lines
drivers/net/usb/r8152.c index renamed from drivers/net/usb/r8152.c +0 -0

b9586c30c04dd..0fb6dfe4abf56 100644

drivers/net/usb/r8152.c +0 -0

[ ... ]

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool

Quoted 1 line
drivers/net/usb/r8152.c +0 -0
@@ -4496,6 +4534,58 @@ static void r8156_ups_en(struct r8152 *tp, bool

enable)

Quoted 8 lines
drivers/net/usb/r8152.c +5 -0
      }
 }
 
+static void r8157_ups_en(struct r8152 *tp, bool enable) {
+     u32 ocp_data;
+     int ret;
+
+     if (enable) {

[ ... ]

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable)

Quoted 8 lines
drivers/net/usb/r8152.c +8 -0
+     } else {
+             ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT,
+                               UPS_EN | USP_PREWAKE);
+
+             ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2,
+                               UPS_FORCE_PWR_DOWN);
+
+             ocp_data = ocp_read_word(tp, MCU_TYPE_USB,

USB_MISC_0);

Quoted 8 lines

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable) · line 4561

Quoted 4 lines
drivers/net/usb/r8152.c +4 -0
+             if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+                     return;
+
+             if (ocp_data & PCUT_STATUS) {

[Severity: Medium]
On resume, the PLA registers are written before UPS is disabled. Is that ordering intended?

Yes, that is intended.

Quoted 38 lines

rtl8157_runtime_enable(false) first runs r8153_queue_wake(tp, false) and rtl_runtime_suspend_enable(tp, false), and only then calls r8157_ups_en(tp, false). The following are all written while UPS_EN, USP_PREWAKE and UPS_FORCE_PWR_DOWN are still set:

PLA_INDICATE_FALG, PLA_SUSPEND_FLAG, PLA_EXTRA_STATUS,
PLA_CRWECR,
PLA_CONFIG34, and PLA_CONFIG5 and PLA_CFG_WOL via __rtl_set_wol()

Those writes also happen before the BACKUP_RESTRORE poll has confirmed that the device has recovered from a power cut.

The existing runtime paths that toggle UPS disable it first. rtl8153b_runtime_enable() has:

        } else {
                r8153b_ups_en(tp, false);
                r8153_queue_wake(tp, false);
                rtl_runtime_suspend_enable(tp, false);

rtl8153c_runtime_enable() uses the same order. So do r8156_init() and r8157_init(). The enable side of rtl8157_runtime_enable() already arms UPS last, so only the resume side is inverted.

If PCUT_STATUS is set on resume, could these writes be lost? They clear UPCOMING_RUNTIME_D3 and LINK_OFF_WAKE_EN and restore tp->saved_wolopts. Could the backup-restore then overwrite them with the suspend-time state, which had WAKE_ANY and LINK_OFF_WAKE_EN set? The device could then come back from runtime resume still configured to wake on any event and on link-off, rather than with the wolopts the user asked for.

There is a second ordering issue. r8153_queue_wake(tp, false) clears LINK_CHG_EVENT and LINK_CHANGE_FLAG before r8157_ups_en() reprograms the PHY through hw_phy_cfg() and rtl8152_set_speed(). Any link-change state raised by that PHY reset is therefore not cleared.

Quoted 6 lines

Should r8157_ups_en(tp, false) move to the top of the else branch, to match rtl8153b_runtime_enable()?

[Severity: Medium]
After a UPS power cut, this starts the 20 ms BACKUP_RESTRORE poll and then calls hw_phy_cfg() without waiting for MAC autoload first. Is that safe?

The two chips shouldn't wait for autoload.

Quoted 32 lines

The other post-power-cut paths in the driver do wait. r8153b_ups_en() has:

                if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) &
PCUT_STATUS) {
                        if (wait_autoload_done(tp))
                                return;

                        tp->rtl_ops.hw_phy_cfg(tp);

r8157_init() runs r8156b_wait_loading_flash(), then wait_autoload_done(), then r8159_wait_backup_restore().

This path never calls wait_autoload_done(). r8156b_wait_loading_flash() only runs inside hw_phy_cfg(), which is after the poll.

If autoload or flash loading is still running when runtime resume starts, could the 20 ms poll time out and mark the device inaccessible?
Or could the PHY be reprogrammed before autoload has finished?

r8156_ups_en(false), which this function mirrors, also skips the autoload wait. But r8156_ups_en(true) is never called anywhere, so that path has never been exercised.

Quoted 1 line

+ ret = read_poll_timeout(ocp_read_word,

ocp_data,

Quoted 1 line

+

test_bit(RTL8152_INACCESSIBLE,

Quoted 1 line

+ &tp->flags)

||
> +                                             (ocp_data &
Quoted 8 lines

BACKUP_RESTRORE),

drivers/net/usb/r8152.c

Quoted 3 lines
drivers/net/usb/r8152.c +3 -0
+                                             200, 20000, false, tp,
+                                             MCU_TYPE_USB,
+ USB_GPHY_CTRL);

[Severity: Medium]
The runtime path and the init path treat RTL8157 (RTL_VER_16) backup-restore differently. Which one matches the hardware?

Waiting backup-restore is intended for RTL8157 when resuming from UPS.

Quoted 42 lines

rtl8157_runtime_enable(false) sends RTL_VER_16 into this poll, and a timeout marks the device inaccessible. r8157_init(), however, only waits for backup-restore on RTL8159:

        switch (tp->version) {
        case RTL_VER_17_QFN68:
        case RTL_VER_17_QFN100:
                if (r8159_wait_backup_restore(tp)) {

The backup-restore enables later in r8157_init() are also programmed only for the RTL8159 variants: 0xb99c BIT(0) for QFN68, and 0xb9a2 for QFN68 and QFN100.

The commit message says RTL8157 needs this wait after a power cut. If so, r8157_init() touches the PHY on RTL_VER_16 without waiting.

If RTL8157 never asserts BACKUP_RESTRORE after a UPS power cut, then every runtime resume after a power cut on RTL_VER_16 would time out here and leave the interface unusable until replug.

Also, the only place in the driver that sets BACKUP_RESTRORE is rtl_ram_code_speed_up():

ocp_word_set_bits(tp, MCU_TYPE_USB, USB_GPHY_CTRL,
GPHY_PATCH_DONE |
BACKUP_RESTRORE);

Suppose no PHY speed-up firmware was loaded, for example because the firmware file is missing, and the hardware doesn't set the bit itself. Can this poll ever succeed?

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable) · line 4576

Quoted 9 lines
drivers/net/usb/r8152.c +9 -0
+                     if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
+                             return;
+
+                     if (ret) {
+                             rtl_set_inaccessible(tp);
+                             dev_err(&tp->intf->dev,
+                                     "backup-restore timed out\n");
+                             return;
+                     }
Quoted 2 lines

[Severity: Medium]
If this times out during runtime resume, does anything recover the device?

As I said in the previous version, this matches how the existing timeout in r8157_init() behaves.

Recovery from the timeout requires more than just queuing a reset. The reset has to survive a re-autosuspend and rerun init() in rtl8152_post_reset(), and the runtime resume path has to report the failure.

Quoted 60 lines

r8157_ups_en() returns void, so rtl8157_runtime_enable() carries on with r8157_u2p3en() and r8153b_u1u2en(). rtl8152_runtime_resume() then returns 0. rtl8152_resume() only resets the device on system resume:

        if (!runtime_resume && test_bit(RTL8152_INACCESSIBLE,
&tp->flags))
                usb_reset_device(tp->udev);

Once RTL8152_INACCESSIBLE is set, the OCP accessors short-circuit, so they never reach the reg_access_reset_count and usb_queue_reset_device() recovery either. The interface stays up but unusable until replug or an unrelated reset.

The commit message acknowledges this and leaves recovery to a follow-up series. Could this call usb_queue_reset_device(tp->intf) instead, as the driver already does on TX timeout?

drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable)

Quoted 3 lines
drivers/net/usb/r8152.c +3 -0
+
+                     /* clear USB fw_ver_reg */
+                     ocp_write_byte(tp, MCU_TYPE_USB,

USB_FW_USB_VER,

Quoted 1 line

+ 0);

[Severity: Low]
Is it guaranteed that the loaded firmware's USB block uses 0xcfd7 as its version register?

rtl8152_fw_mac_apply() decides whether to skip a block using the address from the firmware file:

        fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
        if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >=

mac->fw_ver_data) {

rtl8152_is_fw_mac_ok() doesn't check fw_ver_reg against any expected value for RTL_VER_16/17.

Suppose an rtl8157/rtl8159 firmware image declares a different fw_ver_reg for its RTL_FW_USB block. This write then has no effect, and after a power cut the USB block is skipped as already newest. It would also zero whatever register is at 0xcfd7.

Could this clear the fw_ver_reg from the loaded RTL_FW_USB block instead, or bypass the version check after a power cut?

Quoted 8 lines
drivers/net/usb/r8152.c +8 -0
+
+                     tp->rtl_ops.hw_phy_cfg(tp);
+
+                     rtl8152_set_speed(tp, tp->autoneg, tp->speed,
+                                       tp->duplex, tp->advertising);
+             }
+     }
+}

[ ... ]

Quoted 3 lines
drivers/net/usb/r8152.c +0 -0
@@ -9148,7 +9257,7 @@ static void r8157_init(struct r8152 *tp)
      ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
 
Quoted 7 lines

drivers/net/usb/r8152.c static void r8157_init(struct r8152 *tp) · line 9260

Quoted 3 lines
drivers/net/usb/r8152.c +1 -1
      r8157_power_cut_en(tp, false);
-     r8156_ups_en(tp, false);
+     r8157_ups_en(tp, false);

[Severity: Medium]
Can the power-cut branch of r8157_ups_en(tp, false) ever run from r8157_init()?

Yes, if the device entered UPS, the PCUT_STATUS would be set to 1.

Quoted 37 lines

The line before it clears PCUT_STATUS:

r8157_power_cut_en()

    ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS);

So r8157_ups_en() always reads PCUT_STATUS as 0 here. It skips the backup-restore poll, the USB_FW_USB_VER clear, and the hw_phy_cfg() and rtl8152_set_speed() reprogramming.

rtl8157_runtime_enable(true) now arms UPS for RTL_VER_16 and RTL_VER_17_QFN68. If the USB core reset-resumes the device after a UPS power cut, for example after a failed resume, rtl8152_reset_resume() never reaches rtl8157_runtime_enable(false):

rtl8152_reset_resume()

    clear_bit(SELECTIVE_SUSPEND, &tp->flags);
    rtl_reset_ocp_base(tp);
    tp->rtl_ops.init(tp);
        r8157_power_cut_en(tp, false)   <- clears PCUT_STATUS
        r8157_ups_en(tp, false)         <- skips recovery
    queue_delayed_work(..., &tp->hw_phy_work, 0);
        hw_phy_cfg()
            rtl8152_fw_mac_apply()      <- sees stale USB version

The commit message says the USB version register survives the power cut while the USB patch is lost. Wouldn't rtl8152_fw_mac_apply() then skip the USB block and leave the USB MCU running without its patch?

On RTL_VER_16 this init path also has no backup-restore wait, because r8157_init() only calls r8159_wait_backup_restore() for the QFN68 and QFN100 variants.

Should PCUT_STATUS be checked before r8157_power_cut_en(tp, false) clears it? As written, replacing r8156_ups_en() with r8157_ups_en() here doesn't change the power-cut handling.

Quoted 7 lines

drivers/net/usb/r8152.c static void r8157_init(struct r8152 *tp) · line 9262

Quoted 2 lines
drivers/net/usb/r8152.c +0 -0
      r8153_queue_wake(tp, false);
      rtl_runtime_suspend_enable(tp, false);

--
Sashiko AI review ·
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130063123.2408-1
-nic_swsd%40realtek.com

Best Regards,
Chih-Kai

#24 lore raw
In reply to Chih Kai Hsu
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#25 lore raw
In reply to Chih Kai Hsu
Sashiko has finished reviewing this 12-patch series:

Sashiko has finished reviewing this 12-patch series:

- Patches 1-11: no issues found.
- Patch 12 ("r8152: enable UPS for RTL8157 and RTL8159 QFN68"): 3 potential issues (3 Medium)

A detailed review has been sent as a separate reply to patch 12.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]