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

From: Chih Kai Hsu

Date: Tue Dec 01 2026 - 07:21:47 EST


> 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.
>
> > diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c index
> > b9586c30c04dd..0fb6dfe4abf56 100644
> > --- a/drivers/net/usb/r8152.c
> > +++ b/drivers/net/usb/r8152.c
>
> [ ... ]
>
> > @@ -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) {
>
> [ ... ]
>
> > + } 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?

Yes, that is 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 two chips shouldn't wait for autoload.

>
> 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.
>
> > + 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?

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

>
> 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?
>
> > + 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?

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.

>
> 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?
>
> > +
> > + /* 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?
>
> > +
> > + tp->rtl_ops.hw_phy_cfg(tp);
> > +
> > + rtl8152_set_speed(tp, tp->autoneg, tp->speed,
> > + tp->duplex, tp->advertising);
> > + }
> > + }
> > +}
>
> [ ... ]
>
> > @@ -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()?

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

>
> 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.
>
> > 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