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

From: Chih Kai Hsu

Date: Fri Nov 27 2026 - 03:39:26 EST


> Thank you for your contribution! Sashiko AI review found 6 potential
> issue(s) to consider.
>
> Critical: 0 · High: 0 · Medium: 1 · Low: 5
>
> 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] set_carrier() now returns early when the BMSR read fails, and
> the link-change event is lost.
> - [Low] rtl_tgphy_access() reports a write as successful once the command
> has been posted.
> - [Low] Two statements in the commit message are not quite right.
> - [Low] read_mii_word() now returns 0 for a failed PHY read, and the MII
> library callers in drivers/net/mii.c take that 0 as real register…
> - [Low] rtl_tgphy_access() relies on the comment 'A failed control
> transfer latches RTL8152_INACCESSIBLE' and ignores the return values…
> - [Low] After writing BMCR_RESET, rtl8152_set_speed() polls BMCR for up
> to 50 x 20 ms, but the new loop stops on the first failed read: `if…
>
> --
>
> r8152: add TGPHY register access for RTL8157 and RTL8159
>
> This patch adds a TGPHY command channel for PHY register access on
> RTL8157/RTL8159 and routes ocp_reg_read()/ocp_reg_write() through new
> per-chip rtl_ops.phy_read/phy_write hooks. The PHY accessors and helpers now
> return int, and a number of callers are changed to act on a failed access.
>
> > - 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 for an inaccessible device is
> > still returned, as the writes that follow are blocked then.
>
> [ ... ]
>
> > Older chips are unaffected, as r8152_phy_read() always succeeds.
>
> [Severity: Low]
> Is this last statement accurate? A few paths also change on the OCP-path chips.
>
> In r8156_hw_phy_cfg() for RTL_VER_11, the 0xd068 PHY read now always runs,
> and it runs before the MID_REVERSE test. Before, it only ran when
> MID_REVERSE was set:
>
> ocp_reg_read(tp, 0xd068, &data);
> if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE)
> &&
> (data & BIT(1))) {
>
> rtl8152_set_pauseparam() now reads BMCR even when pause->autoneg is 0.
>
> rtl_pre_ram_code() now returns the errno from rtl_phy_patch_request() instead
> of always returning -ETIME. rtl_phy_patch_request() now also tries a release on
> the -ENODEV path.
>
> None of these look harmful. Should the commit message mention them?
>
> In the read_mii_word() bullet, -ENODEV is only still returned by the
> test_bit(RTL8152_INACCESSIBLE) check at entry. If the device becomes
> inaccessible during the access, r8152_mdio_read() returns -ENODEV and
> read_mii_word() turns it into 0:
>
> return ret < 0 ? 0 : ret;
>
> Could the wording be changed to match the code?
>
> > diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c index
> > 2f3d97e2400c3..e6a5d35403a28 100644
> > --- a/drivers/net/usb/r8152.c
> > +++ b/drivers/net/usb/r8152.c
>
> [ ... ]
>
> > @@ -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;
> > }
>
> [Severity: Low]
> With a failed read reported as 0, the MII library callers in drivers/net/mii.c treat
> it as real register content.
>
> mii_nway_restart() sees BMCR_ANENABLE clear and returns -EINVAL. On
> RTL8157/RTL8159, a TGPHY timeout on that BMCR read therefore reaches
> userspace as -EINVAL from rtl8152_nway_reset() (ethtool -r). The same happens
> in rtl_ethtool_set_eee(), after eee_set() has already written the EEE registers.
>
> mii_ethtool_get_link_ksettings() returns void. With BMCR=0 it reports
> AUTONEG_DISABLE with SPEED_10/DUPLEX_HALF, and BMSR=0 gives
> SPEED_UNKNOWN.
> It also overwrites mii->full_duplex.
>
> In rtl8152_get_link_ksettings(), the new OCP_10GBT_CTRL/STAT reads
> propagate the error. If only the MII library reads fail, though, the function
> returns 0 with made-up link settings. Is this split intended?
>
> The commit message explains the 0 mapping for mii_nway_restart(). It does not
> mention the misleading -EINVAL or the get_link_ksettings output.
>
> [ ... ]
>
> > +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);
> > + }
> >
> > - return data & clear;
> > + 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);
> > + }
>
> [Severity: Low]
> For a write, the only busy wait happens before the command is issued. Once
> DATA/ADDR/CMD have been posted, ret is still 0. If the busy bit never clears for
> that write, the function still returns 0. The -ETIMEDOUT is then reported by
> whichever TGPHY access comes next.
>
> The commit message says rtl8152_set_speed() updates the cached
> speed/duplex state only once the final BMCR write succeeds. When PHY_RESET
> is not pending, nothing reads the PHY after that write. So
> tp->ups_info.speed_duplex, tp->mii.full_duplex and tp->mii.force_media
> are committed even if the PHY never took the write. SIOCSMIIREG in
> rtl8152_ioctl() now also returns this status to userspace.
>
> ocp_adv_write() uses the same posted-write pattern, so this may be the
> intended hardware protocol.
>
> Should the write path wait for TGPHY_CMD_BUSY to clear after posting the
> command? If not, should it be documented that write success only means the
> command was issued?
>
> > +
> > +out:
> > + /* A failed control transfer latches RTL8152_INACCESSIBLE */
> > + if (!ret && test_bit(RTL8152_INACCESSIBLE, &tp->flags))
> > + ret = -ENODEV;
> > +
> > + return ret;
> > +}
>
> [Severity: Low]
> Is this comment true for every failed transfer? r8152_control_msg() returns
> -ENODEV early, without setting the flag:
>
> if (ret == -ENODEV)
> return ret;
>
> generic_ocp_read() and generic_ocp_write() then call rtl_set_unplug(). That
> function only latches the flag once the USB core has marked the device
> detached:
>
> static void rtl_set_unplug(struct r8152 *tp)
> {
> if (tp->udev->state == USB_STATE_NOTATTACHED)
> rtl_set_inaccessible(tp);
> }
>
> get_registers()/set_registers() can also return -ENOMEM on an allocation failure
> without touching the flag.
>
> In these cases a DATA/ADDR/CMD write can be dropped, or the final
> USB_TGPHY_DATA read can return 0xffff. For -ENOMEM it can return an
> uninitialised value from the local tmp in ocp_read_word(). Either way,
> rtl_tgphy_access() still returns 0.
>
> This is no worse than the OCP path. Still, do the comment and the commit
> message ("returns an errno (-ENODEV if the device is inaccessible)") overstate
> what is detected here?
>
> [ ... ]
>
> > 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;
> > }
> > +
> > + /* The new settings are already applied and cached. */
> > + ret = 0;
> > }
>
> [Severity: Low]
> Does a single failed read here end the reset wait early? On RTL8157/RTL8159, a
> TGPHY -ETIMEDOUT breaks out of the loop as if BMCR_RESET had cleared, and
> ret is then forced to 0. Callers such as
> rtl_hw_phy_work_func_t() then continue PHY setup while the PHY may still be
> in reset.
>
> r8153_phy_status() in this patch treats a failed read as not ready and keeps
> polling. Would it be more consistent to stop here only on -ENODEV, and keep
> polling on a timeout?
>
> The commit message says a failed read is not reported. It does not say that the
> read also ends the wait.
>
> [ ... ]
>
> > @@ -7240,8 +7480,13 @@ static void set_carrier(struct r8152 *tp) {
> > struct net_device *netdev = tp->netdev;
> > struct napi_struct *napi = &tp->napi;
> > + int bmsr;
> >
> > - if (r8152_mdio_read(tp, MII_BMSR) & BMSR_LSTATUS) {
> > + bmsr = r8152_mdio_read(tp, MII_BMSR);
> > + if (bmsr < 0)
> > + return;
> > +
>
> [Severity: Medium]
> Can this lose the link change event? rtl_work_func_t() has already
> consumed the flag before it calls set_carrier():
>
> if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
> set_carrier(tp);
>
> On a link down, intr_callback() has also already stopped the queue:
>
> if (netif_carrier_ok(tp->netdev)) {
> netif_stop_queue(tp->netdev);
> set_bit(RTL8152_LINK_CHG, &tp->flags);
> schedule_delayed_work(&tp->schedule, 0);
> }
>
> Suppose the BMSR read then times out on RTL8157/RTL8159 in
> rtl_tgphy_access(). Neither the netif_carrier_off() path nor
> netif_wake_queue() runs, and RTL8152_LINK_CHG is not set again.
>
> The netdev then keeps the carrier on with the TX queue stopped. This lasts
> until another interrupt shows a mismatch, or until the TX watchdog calls
> rtl8152_tx_timeout(). A link up can be missed the same way.
>
> Could this error path set RTL8152_LINK_CHG again and reschedule
> tp->schedule? The LINK_STATUS branch at the end of set_carrier() already
> does that.

Yes, I will set the flag again and reschedule the work.

>
> > if (bmsr & BMSR_LSTATUS) {
> > if (!netif_carrier_ok(netdev)) {
> > tp->rtl_ops.enable(tp);
> > netif_stop_queue(netdev);
>
> [ ... ]
>
> --
> Sashiko AI review ·
> https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126033211.51850-1
> -nic_swsd%40realtek.com

Best Regards,
Chih-Kai