r8152: refactor and extend RTL8157/8159 support
Subsystems (4)
v2 cover r8152: refactor and extend RTL8157/8159 support
v2:
- For patch #1, use read_poll_timeout() to replace the manual for-loop polling.
- For patch #2 and #3, rewrite the commit message.
v1:
RTL8157 and RTL8159 have different init, enable, up/down, hw_phy_cfg, and PHY access sequences from RTL8156. This series splits the shared callbacks into per-chip variants and adds the missing RTL8157/8159
functionality: dedicated unload and change_mtu callbacks, TGPHY register access via phy_read/phy_write pointers in struct rtl_ops, flow control patch support through rtl_fc_pause_pkt_en(), and UPS support via r8157_ups_en().
Chih Kai Hsu (8):
r8152: refactor r8156_init
r8152: support RTL8159 for different packages
r8152: refactor rtl8156_enable, rtl8156_up, and rtl8156_down
r8152: refactor r8157_hw_phy_cfg
r8152: support rtl8157_unload and rtl8157_change_mtu
r8152: add TGPHY access support
r8152: support rtl_fc_pause_pkt_en()
r8152: support UPS for RTL8157 and RTL8159
drivers/net/usb/r8152.c | 1297 +++++++++++++++++++++++++++++++-------- 1 file changed, 1054 insertions(+), 243 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/8 r8152: refactor r8156_init
RTL8157 and RTL8159 have different init sequences from RTL8156. Split r8156_init into per-chip functions: r8156_init (VER_10/11), r8156b_init (VER_12/13/15), r8157_init (VER_16), and r8159_init (VER_17).
--- drivers/net/usb/r8152.c | 488 ++++++++++++++++++++++++++++------------ 1 file changed, 349 insertions(+), 139 deletions(-)
drivers/net/usb/r8152.c +349 -139
@@ -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
@@ -371,6 +372,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)
@@ -417,6 +421,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)
@@ -542,6 +547,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
@@ -4200,6 +4206,22 @@ 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, + 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 ret; +} +static void r8153b_ups_en (struct r8152 *tp,bool enable) {if (enable) {
@@ -4218,16 +4240,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);
@@ -4255,16 +4269,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);
@@ -7253,22 +7259,14 @@ 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 ; - } +if (wait_autoload_done (tp)) +return ;r8153_phy_status (tp,0 );
@@ -7369,22 +7367,13 @@ 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 ; - } +if (wait_autoload_done (tp)) +return ;r8153_phy_status (tp,0 );
@@ -7439,8 +7428,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 ;
@@ -7453,15 +7440,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 );
@@ -8358,90 +8338,132 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)set_bit (PHY_RESET , &tp->flags); } -static int r8159_wait_backup_restore (struct r8152 *tp) +static void r8156_init (struct r8152 *tp) { -u32 ocp_data; +u16 data; - ocp_data =ocp_read_word (tp,MCU_TYPE_USB ,USB_MISC_0 ); -if (!(ocp_data &PCUT_STATUS )) -return 0 ; +if (test_bit (RTL8152_INACCESSIBLE , &tp->flags)) +return ; -return poll_timeout_us (ocp_data =ocp_read_word (tp,MCU_TYPE_USB ,USB_GPHY_CTRL ), - ocp_data &BACKUP_RESTRORE ,200 ,20000 , false); +ocp_byte_clr_bits (tp,MCU_TYPE_USB ,USB_ECM_OP ,EN_ALL_SPEED ); + +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 ); + +r8153b_u1u2en (tp, false); + +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 )); + +r8152_mdio_test_and_clr_bit (tp,MII_BMCR ,BMCR_PDOWN ); + + data =r8153_phy_status (tp,PHY_STAT_LAN_ON ); + +r8153_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 ); + +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); + +usb_enable_lpm (tp->udev); + +r8156_mac_clk_spd (tp, true); + +ocp_word_clr_bits (tp,MCU_TYPE_PLA ,PLA_MAC_PWR_CTRL3 , +PLA_MCU_SPDWN_EN ); + +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); + +ocp_word_clr_bits (tp,MCU_TYPE_USB ,USB_USB_CTRL , +RX_AGG_DISABLE |RX_ZERO_EN ); + +ocp_byte_set_bits (tp,MCU_TYPE_USB ,USB_BMU_CONFIG ,ACT_ODMA ); + +r8156_mdio_force_mode (tp); +rtl_tally_reset (tp); + + tp->coalesce =15000 ;/* 15 us */ } -static void r8156_init (struct r8152 *tp) +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 r8156b_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 ) -ocp_word_set_bits (tp,MCU_TYPE_USB ,USB_U2P3_CTRL ,RX_DETECT8 ); +ocp_word_set_bits (tp,MCU_TYPE_USB ,USB_U2P3_CTRL ,RX_DETECT8 );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 ) -ocp_reg_clr_bits (tp,0xa466 ,BIT (0 )); +ocp_reg_clr_bits (tp,0xa466 ,BIT (0 )); } - 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); + +/* Disable Auto Speed up */ +ocp_word_clr_bits (tp,MCU_TYPE_USB ,USB_FW_CTRL ,AUTO_SPEEDUP );/* MSC timer = 0xfff * 8ms = 32760 ms */ ocp_write_word (tp,MCU_TYPE_USB ,USB_MSC_TIMER ,0x0fff );
@@ -8449,73 +8471,261 @@ 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); +r8156b_u2phy_backup (tp); +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);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_clr_bits (tp,MCU_TYPE_PLA ,PLA_RCR ,SLOT_EN ); -ocp_word_set_bits (tp,MCU_TYPE_PLA ,PLA_CPCR ,FLOW_CTRL_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 )); +/* 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); + 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 ); - } +ocp_word_set_bits (tp,MCU_TYPE_USB ,USB_FW_TASK ,FC_PATCH_TASK );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); -/* RX aggregation / 16 bytes RX descriptor -* BIT(11) is specific to RTL8159, with unknown meaning -*/ -if (tp->version ==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 ); +ocp_word_clr_bits (tp,MCU_TYPE_USB ,USB_USB_CTRL , +RX_AGG_DISABLE |RX_ZERO_EN ); + +r8156_mdio_force_mode (tp); +rtl_tally_reset (tp); + + tp->coalesce =15000 ;/* 15 us */ +} + +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); + +if (wait_autoload_done (tp)) +return ; + +r8156b_wait_loading_flash (tp); + + 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); + +/* Disable Interrupt Mitigation */ +ocp_byte_clr_bits (tp,MCU_TYPE_USB ,0xcf04 , +BIT (0 ) |BIT (1 ) |BIT (2 ) |BIT (7 )); + +/* Disable Auto Speed up */ +ocp_word_clr_bits (tp,MCU_TYPE_USB ,USB_FW_CTRL ,AUTO_SPEEDUP ); + +/* 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); + +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 )); + +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_clr_bits (tp,MCU_TYPE_USB ,USB_USB_CTRL ,RX_AGG_DISABLE |RX_ZERO_EN ); +ocp_word_w0w1 (tp,MCU_TYPE_PLA ,PLA_EXTRA_STATUS , +CUR_LINK_OK ,POLL_LINK_CHG ); -if (tp->version <RTL_VER_12 ) -ocp_byte_set_bits (tp,MCU_TYPE_USB ,USB_BMU_CONFIG ,ACT_ODMA ); +set_bit (GREEN_ETHERNET , &tp->flags); -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 )); +/* 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 )); + +/* TX descriptor Signature */ +ocp_byte_clr_bits (tp,MCU_TYPE_USB ,0xd4ae ,BIT (1 )); + +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 r8159_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); + +if (wait_autoload_done (tp)) +return ; + +if (r8159_wait_backup_restore (tp)) { +rtl_set_inaccessible (tp); +dev_err (&tp->intf->dev, +"init failed, backup-restore timed out\n" ); +return ; } +r8156b_wait_loading_flash (tp); + + 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); + +/* Disable Interrupt Mitigation */ +ocp_byte_clr_bits (tp,MCU_TYPE_USB ,0xcf04 , +BIT (0 ) |BIT (1 ) |BIT (2 ) |BIT (7 )); + +/* Disable Auto Speed up */ +ocp_word_clr_bits (tp,MCU_TYPE_USB ,USB_FW_CTRL ,AUTO_SPEEDUP ); + +/* 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); + +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 )); + +/* Disable FW u1u2 patch option */ +ocp_byte_clr_bits (tp,MCU_TYPE_USB ,0xb9a6 ,BIT (0 )); + +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 / 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 )); + +/* TX descriptor Signature */ +ocp_byte_clr_bits (tp,MCU_TYPE_USB ,0xd4ae ,BIT (1 )); + +/* Enable u3phy patch backup */ +ocp_write_word (tp,MCU_TYPE_USB ,0xb9a2 ,0x0448 ); +r8156_mdio_force_mode (tp);rtl_tally_reset (tp);
@@ -9846,7 +10056,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 ; - ops->init = r8156_init; + ops->init = r8156b_init; ops->enable = rtl8156_enable; ops->disable = rtl8153_disable; ops->up = rtl8156_up;
@@ -9885,7 +10095,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;
@@ -9907,7 +10117,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 = r8159_init; ops->enable = rtl8156_enable; ops->disable = rtl8153_disable; ops->up = rtl8156_up;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH net-next v2 1/8] r8152: refactor r8156_init
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: 3 · Low: 0
- [Medium] The commit message describes only a mechanical split ("Split r8156_init into per-chip functions"), but the new per-chip functions…
- [Medium] The new wait_autoload_done() helper (drivers/net/usb/r8152.c:4202) drops the per-iteration…
- [Medium] Converting the open-coded autoload polls into if (wait_autoload_done(tp)) return; turns a previously non-fatal, fall-…
--
r8152: refactor r8156_init
This splits the shared r8156_init() into per-chip initializers (r8156_init, r8156b_init, r8157_init, r8159_init) and introduces a new wait_autoload_done() helper based on read_poll_timeout() which replaces the open-coded autoload polling loops in the RTL8153 and RTL8156 family init and UPS paths.
A few questions below about the new helper and about init sequence changes that reach chips other than RTL8157/RTL8159.
Quoted 12 lines
drivers/net/usb/r8152.c +1
-0@@ -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
[ ... ]
Quoted 9 lines
drivers/net/usb/r8152.c +5
-0@@ -4193,6 +4199,22 @@ 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; +
drivers/net/usb/r8152.c static u16 r8153_phy_status(struct r8152 *tp, u16 desired) · line 4216
Quoted 10 lines
drivers/net/usb/r8152.c +10
-0+ ret =read_poll_timeout (ocp_read_word, ocp_data, + 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 ret; +}
[Severity: Medium]
Should this poll also bail out when RTL8152_INACCESSIBLE is set?
Every loop replaced here checked the flag on each iteration, so an unplug or a failed control transfer terminated the wait within one 20 ms step. read_poll_timeout() only exits on ocp_data & AUTOLOAD_DONE or on the 10 s timeout, so after rtl_set_unplug()/rtl_set_inaccessible() the wait keeps sleeping for up to 10 s inside tp->rtl_ops.init(), r8153b_ups_en() and r8153c_ups_en(), all of which run in process context holding tp->control (and the USB device lock for probe/reset_resume/PM).
Is the polled value trustworthy in that state? generic_ocp_read() returns -ENODEV before touching the caller's buffer when RTL8152_INACCESSIBLE is set:
drivers/net/usb/r8152.c:generic_ocp_read() {
if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
return -ENODEV;
...
}and ocp_read_word() ignores that return value while decoding an uninitialized __le32 tmp, so the loop condition can be driven by uninitialized stack data. On an ordinary transfer error get_registers() fills the buffer with 0xff, which gives 0xffff and has AUTOLOAD_DONE (BIT(1)) set, so the helper would report success.
Other polling helpers in this file, for example r8156b_wait_loading_flash(), r8153_phy_status(), rtl_phy_patch_request() and rtl_disable(), still test the flag on each iteration.
Quoted 22 lines
drivers/net/usb/r8152.c +2
-10static void r8153b_ups_en (struct r8152 *tp,bool enable) {if (enable) {@@ -4211,16 +4233,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);
[ ... ]
Quoted 25 lines
drivers/net/usb/r8152.c +2
-10@@ -7246,22 +7252,14 @@ 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 ; - } +if (wait_autoload_done (tp)) +return ;r8153_phy_status (tp,0 );
[ ... ]
Quoted 19 lines
drivers/net/usb/r8152.c +5
-7@@ -8351,90 +8331,132 @@ static void r8157_hw_phy_cfg(struct r8152 *tp)set_bit (PHY_RESET , &tp->flags); } -static int r8159_wait_backup_restore (struct r8152 *tp) +static void r8156_init (struct r8152 *tp) { -u32 ocp_data; +u16 data; - ocp_data =ocp_read_word (tp,MCU_TYPE_USB ,USB_MISC_0 ); -if (!(ocp_data &PCUT_STATUS )) -return 0 ; +if (test_bit (RTL8152_INACCESSIBLE , &tp->flags)) +return ; -return poll_timeout_us (ocp_data =ocp_read_word (tp,MCU_TYPE_USB ,USB_GPHY_CTRL ), - ocp_data &BACKUP_RESTRORE ,200 ,20000 , false); +ocp_byte_clr_bits (tp,MCU_TYPE_USB ,USB_ECM_OP ,EN_ALL_SPEED );
[ ... ]
Quoted 8 lines
drivers/net/usb/r8152.c +8
-0+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 );
[ ... ]
drivers/net/usb/r8152.c static void r8157_hw_phy_cfg(struct r8152 *tp) · line 8421
Quoted 9 lines
drivers/net/usb/r8152.c +8
-1-static void r8156_init (struct r8152 *tp) +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 ); +}
[Severity: Medium]
The commit message describes only a split ("Split r8156_init into per-chip functions"), but the new functions also change what gets programmed on chips that were already supported. Could the functional changes be split into separate patches with their own descriptions?
RTL8156 (r8156_init) and RTL8156B (r8156b_init) both gain two writes the old shared r8156_init() never did:
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);
PLA_RMT_WAKE (0xe0c8) is a newly defined register that the baseline driver never touched at all.
r8156b_init() also calls the new r8156b_u2phy_backup() magic write sequence (USB 0xd3ce..0xd3dc) unconditionally on shipping RTL8156B parts.
For RTL8157/RTL8159, r8157_init() and r8159_init() now clear AUTO_SPEEDUP in USB_FW_CTRL, which the baseline did only inside the "version >= RTL_VER_12 && version <= RTL_VER_15" block, and add new "Disable Interrupt Mitigation" (0xcf04 bits 0/1/2/7) and "Clear Warm RST / Bus RST event flag" (0xcd06 BIT(11)) writes. r8159_init() adds a 0xb9a6 BIT(0) clear and ocp_write_word(0xb9a2, 0x0448) on top of that.
Was the reordering of r8156b_wait_loading_flash() intentional too? The baseline called it before the autoload-done poll for VER_13/15/16/17, while r8157_init() and r8159_init() now call it after the poll (and, for RTL8159, after r8159_wait_backup_restore()).
[ ... ]
Quoted 15 lines
drivers/net/usb/r8152.c +2
-4@@ -8442,73 +8464,261 @@ 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); +r8156b_u2phy_backup (tp); +r8153b_power_cut_en (tp, false);r8156_ups_en (tp, false);r8153_queue_wake (tp, false);rtl_runtime_suspend_enable (tp, false);
[ ... ]
Quoted 16 lines
drivers/net/usb/r8152.c +16
-0+static void r8159_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);
drivers/net/usb/r8152.c static void r8156_init(struct r8152 *tp) · line 8656
Quoted 10 lines
drivers/net/usb/r8152.c +9
-0+ +if (wait_autoload_done (tp)) +return ; + +if (r8159_wait_backup_restore (tp)) { +rtl_set_inaccessible (tp); +dev_err (&tp->intf->dev, +"init failed, backup-restore timed out\n" ); +return ; }
[Severity: Medium]
Should the autoload timeout latch the failure the way the backup-restore timeout right below it does?
Two adjacent "the chip never came up" conditions in the same function are handled differently: r8159_wait_backup_restore() failure calls rtl_set_inaccessible() and logs, while wait_autoload_done() failure just returns.
Before this patch a timeout fell through and the rest of the init sequence still ran. Now r8153_init(), r8153b_init(), r8153c_init(), r8156_init(), r8156b_init(), r8157_init() and r8159_init() return after only the first few writes (U1/U2 disabled, EN_ALL_SPEED cleared, BYPASS_MAC_RESET set, plus the 0xcffe/0xd3ca SW reset writes for RTL8157/8159), skipping the BMCR_PDOWN clear, power-cut/UPS disable, the MSC and U1/U2 timers, usb_enable_lpm(), the USB_USB_CTRL RX aggregation and descriptor format bits, rtl_bmu_clr_bits(), r8156_mdio_force_mode(), rtl_tally_reset() and the trailing tp->coalesce = 15000.
Since ops->init returns void and nothing sets RTL8152_INACCESSIBLE on this path, can rtl8152_probe_once() still queue hw_phy_work and register a netdev whose PHY may still be in BMCR_PDOWN and whose bulk-in/RX descriptor configuration was never written? Does rtl8152_reset_resume() likewise continue into rtl8152_resume() unaware that init was abandoned?
tp->coalesce is left at 0 from the zeroed netdev private data in that case, and is later consumed as tp->coalesce / 8 by r8153_set_rx_early_timeout() and reported as 0 by rtl8152_get_coalesce().
The same conversion in r8153b_ups_en() and r8153c_ups_en() now skips tp->rtl_ops.hw_phy_cfg() and the speed restoration on a plain timeout, and in r8153c_ups_en() it additionally skips the trailing PLA_CRWECR/PLA_CONFIG34 BIT(8)/PLA_CRWECR sequence that previously always ran.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260903072852.58695-1-nic_swsd%40realtek.com
PATCH v2 2/8 r8152: support RTL8159 for different packages
In reply to Chih Kai HsuRTL8159 comes in two packages, QFN68 and QFN100, which require different handling. Split RTL_VER_17 into RTL_VER_17_QFN68 and RTL_VER_17_QFN100.
--- drivers/net/usb/r8152.c | 93 ++++++++++++++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 15 deletions(-)
drivers/net/usb/r8152.c +78 -15
@@ -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
@@ -290,6 +291,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)
@@ -1254,7 +1258,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 };
@@ -3446,7 +3451,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 ;
@@ -3486,7 +3492,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)
@@ -3674,7 +3680,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,
@@ -3719,7 +3726,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 ;
@@ -3835,6 +3843,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,
@@ -4514,6 +4524,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.
@@ -4568,7 +4580,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 ;
@@ -4680,7 +4693,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 ; }
@@ -5840,7 +5854,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);
@@ -6431,8 +6446,15 @@ 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 ) -ocp_word_clr_bits (tp,MCU_TYPE_USB ,USB_RX_AGGR_NUM ,RX_AGGR_NUM_MASK ); +switch (tp->version) { +case RTL_VER_10 : +case RTL_VER_11 : +break ; +default : +ocp_word_clr_bits (tp,MCU_TYPE_USB ,USB_RX_AGGR_NUM , +RX_AGGR_NUM_MASK ); +break ; + }r8153_set_rx_early_timeout (tp);r8153_set_rx_early_size (tp);
@@ -8120,7 +8142,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 ));
@@ -8723,6 +8746,10 @@ static void r8159_init(struct r8152 *tp)/* TX descriptor Signature */ ocp_byte_clr_bits (tp,MCU_TYPE_USB ,0xd4ae ,BIT (1 )); +/* Enable u2phy backup restore patch */ +if (tp->version ==RTL_VER_17_QFN68 ) +ocp_byte_set_bits (tp,MCU_TYPE_USB ,0xb99c ,BIT (0 )); +/* Enable u3phy patch backup */ ocp_write_word (tp,MCU_TYPE_USB ,0xb9a2 ,0x0448 );
@@ -10113,7 +10140,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 ;
@@ -10205,7 +10233,8 @@ 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 : +case RTL_VER_17_QFN100 : rtl_fw->fw_name =FIRMWARE_8159_1 ;break ;default :
@@ -10215,9 +10244,33 @@ static int rtl_fw_init(struct r8152 *tp)return 0 ; } +static u32 __rtl_get_pkg_det (struct usb_device *udev) +{ +u32 pkg_det =0 ; +__le32 *tmp; +int ret, i; + + tmp =kmalloc_obj (*tmp); +if (!tmp) +return 0 ; + +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 >0 ) + pkg_det =__le32_to_cpu (*tmp) &PKG_MASK ; + +kfree (tmp); +return pkg_det; +} +static u8 __rtl_get_hw_ver (struct usb_device *udev) {u32 ocp_data =0 ; +u32 pkg_det =0 ;__le32 *tmp;u8 version;int ret;
@@ -10300,7 +10353,16 @@ static u8 __rtl_get_hw_ver(struct usb_device *udev) version =RTL_VER_16 ;break ;case 0x2020 : - version =RTL_VER_17 ; + pkg_det =__rtl_get_pkg_det (udev); +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_info (&udev->dev,"Unknown package %#02x\n" , + pkg_det); + }break ;default : version =RTL_VER_UNKNOWN ;
@@ -10459,7 +10521,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
[email protected]
AI reviewRe: [PATCH net-next v2 2/8] r8152: support RTL8159 for different packages
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 1 · Low: 1
- [Medium] Fail-closed RTL8159 package detection with no error channel:
__rtl_get_pkg_det() (drivers/net/usb/r8152.c:10234) returns the…
- [Low] The commit message ("RTL8159 comes in two packages, QFN68 and QFN100, which require different handling.
--
r8152: support RTL8159 for different packages
This splits RTL_VER_17 into RTL_VER_17_QFN68 and RTL_VER_17_QFN100, adds a new PLA_PKG_DET register read during hardware version detection to tell the two packages apart, and adds a QFN68-only register write in r8159_init(). All existing RTL_VER_17 switch arms are expanded to cover both new values.
A couple of questions below.
Quoted 4 lines
r8152: support RTL8159 for different packages
RTL8159 comes in two packages, QFN68 and QFN100, which require different handling. Split
RTL_VER_17intoRTL_VER_17_QFN68andRTL_VER_17_QFN100.
[Severity: Low]
Could the changelog mention the behaviour changes that come along with the enum split?
Three things are not described:
- a new USB control transfer is issued at version-detection time by __rtl_get_pkg_det() to read PLA_PKG_DET
- an RTL8159 with chip id 0x2020 is now reported as RTL_VER_UNKNOWN unless PLA_PKG_DET reads back as one of exactly four values, which makes probe fail with -ENODEV where the previous code always accepted the chip
- r8159_init() gains a QFN68-only write ("u2phy backup restore patch") to 0xb99c
The last two in particular matter for anyone deciding whether to backport this.
Quoted 19 lines
drivers/net/usb/r8152.c +4
-0@@ -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@@ -290,6 +291,9 @@#define IFG_144NS BIT(9)#define IFG_96NS (BIT(9) | BIT(8)) +/* PLA_PKG_DET */ +#define PKG_MASK 0x1e +
[ ... ]
Quoted 8 lines
drivers/net/usb/r8152.c +4
-0@@ -8716,6 +8739,10 @@ static void r8159_init(struct r8152 *tp)/* TX descriptor Signature */ ocp_byte_clr_bits (tp,MCU_TYPE_USB ,0xd4ae ,BIT (1 )); +/* Enable u2phy backup restore patch */ +if (tp->version ==RTL_VER_17_QFN68 ) +ocp_byte_set_bits (tp,MCU_TYPE_USB ,0xb99c ,BIT (0 )); +
[ ... ]
Quoted 17 lines
drivers/net/usb/r8152.c +13
-0@@ -10202,9 +10231,33 @@ static int rtl_fw_init(struct r8152 *tp)return 0 ; } +static u32 __rtl_get_pkg_det (struct usb_device *udev) +{ +u32 pkg_det =0 ; +__le32 *tmp; +int ret, i; + + tmp =kmalloc_obj (*tmp); +if (!tmp) +return 0 ; + +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,
drivers/net/usb/r8152.c static int rtl_fw_init(struct r8152 *tp) · line 10256
Quoted 10 lines
drivers/net/usb/r8152.c +10
-0+PLA_PKG_DET ,MCU_TYPE_PLA , tmp, + sizeof(*tmp), USB_CTRL_GET_TIMEOUT); + +if (ret >0 ) + pkg_det =__le32_to_cpu (*tmp) &PKG_MASK ; + +kfree (tmp); +return pkg_det; +} +
[Severity: Medium]
Should __rtl_get_pkg_det() report the transfer status separately from the register value?
Right now 0 is returned in three different situations:
- kmalloc_obj(*tmp) failed
- all three usb_control_msg() attempts failed, so the "if (ret > 0)" block is skipped and pkg_det keeps its initial value
- PLA_PKG_DET genuinely read back with no bits of PKG_MASK (0x1e) set
Since 0 is also a legal value of the new 0x1e field, can the caller tell "register never read" from "package encoding 0"?
Quoted 6 lines
drivers/net/usb/r8152.c +1
-1@@ -10287,7 +10340,16 @@ static u8 __rtl_get_hw_ver(struct usb_device *udev) version =RTL_VER_16 ;break ;case 0x2020 : - version =RTL_VER_17 ; + pkg_det =__rtl_get_pkg_det (udev);
drivers/net/usb/r8152.c static u8 __rtl_get_hw_ver(struct usb_device *udev) · line 10353
Quoted 10 lines
drivers/net/usb/r8152.c +9
-0+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_info (&udev->dev,"Unknown package %#02x\n" , + pkg_det); + }break ;
Following on from the above: with the failure sentinel 0 landing in the else branch, does a memory allocation failure or three failed control transfers turn a perfectly good RTL8159 into an unsupported device?
The chip id read itself already succeeded here, and before this patch every 0x2020 was driven as RTL_VER_17. Now both consumers of the result treat RTL_VER_UNKNOWN as "not our device":
rtl8152_probe()
version = rtl8152_get_version(intf);
if (version == RTL_VER_UNKNOWN)
return -ENODEV;
for (i = 0; i < RTL8152_PROBE_TRIES; i++) {so the -ENODEV happens before the RTL8152_PROBE_TRIES retry loop gets a chance to run, and:
rtl8152_cfgselector_probe()
if (__rtl_get_hw_ver(udev) == RTL_VER_UNKNOWN) return -ENODEV;
which leaves the adapter in CDC/NCM mode handled by r8153_ecm or cdc_ether rather than switching it to vendor mode. Meanwhile rtl8152_table still lists product 0x815a, rtl_ops_init() has an RTL_VER_17_* arm and MODULE_FIRMWARE(FIRMWARE_8159_1) is still declared, so the device looks supported everywhere else.
Would propagating the usb_control_msg() status out of __rtl_get_pkg_det() (return code or out-parameter) and only reporting "Unknown package" when the register was actually read work better here? Similarly, for a PLA_PKG_DET strap encoding other than 0x18/0x1a/0x1c/0x1e (all four of which need bits 4 and 3 set), is rejecting the device preferable to defaulting to one of the two variants as the baseline driver effectively did?
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260903072852.58695-1-nic_swsd%40realtek.com
PATCH v2 3/8 r8152: refactor rtl8156_enable, rtl8156_up, and rtl8156_down
In reply to Chih Kai HsuRTL8156B and RTL8157/8159 have different enable, up, and down sequences from RTL8156. Add dedicated rtl8156b_enable (VER_12/13/15), rtl8157_enable, rtl8157_up, and rtl8157_down (VER_16/17) instead of handling per-version differences with inline version guards.
--- drivers/net/usb/r8152.c | 235 ++++++++++++++++++++++++++++++++-------- 1 file changed, 189 insertions(+), 46 deletions(-)
drivers/net/usb/r8152.c +189 -46
@@ -6440,31 +6440,16 @@ 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); -switch (tp->version) { -case RTL_VER_10 : -case RTL_VER_11 : -break ; -default : -ocp_word_clr_bits (tp,MCU_TYPE_USB ,USB_RX_AGGR_NUM , -RX_AGGR_NUM_MASK ); -break ; - } -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 ) -return rtl_enable (tp); -if (speed & _2500bps)ocp_word_clr_bits (tp,MCU_TYPE_PLA ,PLA_MAC_PWR_CTRL4 ,IDLE_SPDWN_EN );
@@ -6472,12 +6457,10 @@ static int rtl8156_enable(struct r8152 *tp)ocp_word_set_bits (tp,MCU_TYPE_PLA ,PLA_MAC_PWR_CTRL4 ,IDLE_SPDWN_EN ); -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 ); - } +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->udev->speed ==USB_SPEED_HIGH ) {/* USB 0xb45e[3:0] l1_nyet_hird */
@@ -6502,6 +6485,67 @@ static void rtl8156_disable(struct r8152 *tp)rtl8153_disable (tp); } +static int rtl8156b_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_set_ifg (tp, 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 ); + +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 ); + } + +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 ); + +return rtl_enable (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_set_ifg (tp, speed); + +return rtl_enable (tp); +} +static int rtl8152_set_speed (struct r8152 *tp,u8 autoneg,u32 speed,u8 duplex,u32 advertising) {
@@ -6862,8 +6906,7 @@ static void rtl8156_up(struct r8152 *tp)return ;r8153b_u1u2en (tp, false); -if (tp->version <RTL_VER_16 ) -r8153_u2p3en (tp, false); +r8153_u2p3en (tp, false);r8153_aldps_en (tp, false);rxdy_gated_en (tp, true);
@@ -6876,8 +6919,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 );
@@ -6899,11 +6941,11 @@ 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 ); +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_USB ,USB_SPEED_OPTION , +RG_PWRDN_EN |ALL_SPEED_OFF );ocp_write_dword (tp,MCU_TYPE_USB ,USB_RX_BUF_TH ,0x00600400 );
@@ -6913,10 +6955,19 @@ 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); -if (tp->version <RTL_VER_16 && tp->udev->speed >=USB_SPEED_SUPER ) +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 ; +default : +break ; + } + +if (tp->udev->speed >=USB_SPEED_SUPER )r8153b_u1u2en (tp, true); }
@@ -6929,12 +6980,9 @@ static void rtl8156_down(struct r8152 *tp)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_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 );
@@ -6956,7 +7004,102 @@ 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 ); + +rtl_rx_vlan_en (tp, true); +rxdy_gated_en (tp, false); + +ocp_dword_set_bits (tp,MCU_TYPE_PLA ,PLA_RCR , +RCR_APM |RCR_AM |RCR_AB ); + +r8153_aldps_en (tp, true); +} + +static void rtl8157_up (struct r8152 *tp) +{ +if (test_bit (RTL8152_INACCESSIBLE , &tp->flags)) +return ; + +r8153b_u1u2en (tp, false); +r8153_aldps_en (tp, false); + +rxdy_gated_en (tp, true); +r8153_teredo_off (tp); + +ocp_dword_clr_bits (tp,MCU_TYPE_PLA ,PLA_RCR ,RCR_ACPT_ALL ); + +rtl8152_nic_reset (tp); +rtl_reset_bmu (tp); + +ocp_byte_clr_bits (tp,MCU_TYPE_PLA ,PLA_OOB_CTRL ,NOW_IS_OOB ); + +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 ); + +rtl_rx_vlan_en (tp, tp->netdev->features &NETIF_F_HW_VLAN_CTAG_RX ); + +rtl8156_change_mtu (tp); + +/* share FIFO settings */ +ocp_word_w0w1 (tp,MCU_TYPE_PLA ,PLA_RXFIFO_FULL ,RXFIFO_FULL_MASK , +0x08 ); + +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); + +/* Clear_SDR */ +ocp_byte_set_bits (tp,MCU_TYPE_USB ,0xd378 ,BIT (7 )); +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); + +ocp_byte_clr_bits (tp,MCU_TYPE_PLA ,PLA_OOB_CTRL ,NOW_IS_OOB ); + +/* RX FIFO settings for OOB */ +ocp_write_word (tp,MCU_TYPE_PLA ,PLA_RXFIFO_FULL ,64 /16 ); +ocp_write_word (tp,MCU_TYPE_PLA ,PLA_RX_FIFO_FULL ,1024 /16 ); +ocp_write_word (tp,MCU_TYPE_PLA ,PLA_RX_FIFO_EMPTY ,4096 /16 ); + +rtl_disable (tp); +rtl_reset_bmu (tp); + +ocp_write_word (tp,MCU_TYPE_PLA ,PLA_RMS ,1526 ); +ocp_write_byte (tp,MCU_TYPE_PLA ,PLA_MTPS ,10 *1024 /64 ); + +/* Clear teredo wake event. bit[15:8] is the teredo wakeup +* type. Set it to zero. bits[7:0] are the W1C bits about +* the events. Set them to all 1 to clear them. +*/ +ocp_write_word (tp,MCU_TYPE_PLA ,PLA_TEREDO_WAKE_BASE ,0x00ff ); + +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 );
@@ -10084,7 +10227,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 = r8156b_init; - ops->enable = rtl8156_enable; + ops->enable = rtl8156b_enable; ops->disable = rtl8153_disable; ops->up = rtl8156_up; ops->down = rtl8156_down;
@@ -10123,10 +10266,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 = rtl8153_unload; ops->eee_get = r8153_get_eee; ops->eee_set = r8152_set_eee;
@@ -10146,10 +10289,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 = r8159_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 = rtl8153_unload; ops->eee_get = r8153_get_eee; ops->eee_set = r8152_set_eee;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH net-next v2 3/8] r8152: refactor rtl8156_enable, rtl8156_up, and rtl8156_down
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Critical: 0 · High: 2 · Medium: 2 · Low: 1
- [High] Guard inversion during the function split:
ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR1, BIT(3)) was gated by…
- [High] Second guard inversion: the new rtl8157_up() unconditionally executes `ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_SPEED_OPTION,…
- [Medium] The commit is presented as a mechanical refactor ("instead of handling per-version differences with inline version guards"), but it…
- [Medium] rtl8157_up() omits the ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, PLA_MCU_SPDWN_EN) that the shared rtl8156_up()…
- [Low] The commit message states that "RTL8156B and RTL8157/8159 have different enable, up, and down sequences from RTL8156" and that…
--
r8152: refactor rtl8156_enable, rtl8156_up, and rtl8156_down
This splits the shared RTL8156 enable/up/down handlers into per-generation
callbacks: rtl8156b_enable for VER_12/13/15, and rtl8157_enable, rtl8157_up and rtl8157_down for VER_16/17. rtl_ops_init() is updated to install the new callbacks.
A few questions below about the version guards that were removed during the split, and about how the commit message describes the change.
[Severity: Medium]
The commit message describes this as replacing inline version guards with dedicated functions:
"Add dedicated rtl8156b_enable (VER_12/13/15), rtl8157_enable,
rtl8157_up, and rtl8157_down (VER_16/17) instead of handling
per-version differences with inline version guards."That reads as behaviour preserving, but the up/down register sequences change for both families:
- rtl8156_up() now clears PLA_RCR1 bit 3 for VER_10 through VER_15, which was previously done only for VER_16 and later
- rtl8157_up() clears USB_SPEED_OPTION RG_PWRDN_EN | ALL_SPEED_OFF, which the old shared path skipped for VER_16 and later, and it no longer clears PLA_MAC_PWR_CTRL3 PLA_MCU_SPDWN_EN
- rtl8157_down() no longer sets PLA_MCU_SPDWN_EN, and changes PLA_RMS from 1522 to 1526 and PLA_MTPS from MTPS_DEFAULT to 10 * 1024 / 64
- both down paths now program PLA_BDC_CR ALDPS_PROXY_MODE and PLA_OOB_CTRL DIS_MCU_CLROOB
- new Clear_SDR writes are added to rtl8156_up() and rtl8157_up()
Could the commit message describe these hardware sequence changes, or could they be split into separate patches?
Quoted 4 lines
drivers/net/usb/r8152.c +0
-0
[ ... ]
drivers/net/usb/r8152.c static void rtl8156_up(struct r8152 *tp) · line 6915
Quoted 7 lines
drivers/net/usb/r8152.c +1
-2@@ -6869,8 +6912,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 ));
[Severity: High]
Is this write inverted rather than moved? Before the patch it ran only for VER_16 and later:
if (tp->version >= RTL_VER_16) ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR1, BIT(3));
After the split, rtl_ops_init() installs rtl8156_up as ops->up only for RTL_VER_10/RTL_VER_11 and RTL_VER_12/RTL_VER_13/RTL_VER_15, while RTL_VER_16 and both RTL_VER_17 packages get rtl8157_up, which already contains the same clear:
rtl8157_up() {
...
ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR1, BIT(3));
...
}So the copy left in rtl8156_up() now clears PLA_RCR1 bit 3 on exactly the RTL8156/RTL8156B versions the old guard excluded, on every interface bring-up. Should this line have been deleted from rtl8156_up() instead? PLA_RCR1 bit 3 has no name in the driver (only OUTER_VLAN and INNER_VLAN are defined for that register), so it is hard to tell what the RX side does with it on these parts.
Quoted 27 lines
drivers/net/usb/r8152.c +5
-6ocp_word_clr_bits (tp,MCU_TYPE_PLA ,PLA_SFF_STS_7 ,MCU_BORW_EN );@@ -6892,11 +6934,11 @@ 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 ); +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_USB ,USB_SPEED_OPTION , +RG_PWRDN_EN |ALL_SPEED_OFF );ocp_write_dword (tp,MCU_TYPE_USB ,USB_RX_BUF_TH ,0x00600400 );@@ -6906,10 +6948,19 @@ 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);
drivers/net/usb/r8152.c static void rtl8156_up(struct r8152 *tp) · line 6961
Quoted 10 lines
drivers/net/usb/r8152.c +9
-1-if (tp->version <RTL_VER_16 && tp->udev->speed >=USB_SPEED_SUPER ) +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 ; +default : +break ; + }
[Severity: Low]
This isn't a bug, but the commit message says the goal is to add dedicated functions "instead of handling per-version differences with inline version guards", and this adds a new inline per-version distinction for RTL_VER_13/RTL_VER_15 in the shared path.
The message also says RTL8156B has different up and down sequences from RTL8156, yet rtl_ops_init() still assigns the shared handlers for the 8156B case:
ops->init = r8156b_init; ops->enable = rtl8156b_enable; ... ops->up = rtl8156_up; ops->down = rtl8156_down;
Was a dedicated rtl8156b_up/rtl8156b_down intended here too, or should the message only claim the enable path is split for VER_12/13/15?
Quoted 5 lines
drivers/net/usb/r8152.c +2
-0+ +if (tp->udev->speed >=USB_SPEED_SUPER )r8153b_u1u2en (tp, true); }
[ ... ]
Quoted 41 lines
drivers/net/usb/r8152.c +36
-1@@ -6949,7 +6997,102 @@ 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 ); + +rtl_rx_vlan_en (tp, true); +rxdy_gated_en (tp, false); + +ocp_dword_set_bits (tp,MCU_TYPE_PLA ,PLA_RCR , +RCR_APM |RCR_AM |RCR_AB ); + +r8153_aldps_en (tp, true); +} + +static void rtl8157_up (struct r8152 *tp) +{ +if (test_bit (RTL8152_INACCESSIBLE , &tp->flags)) +return ; + +r8153b_u1u2en (tp, false); +r8153_aldps_en (tp, false); + +rxdy_gated_en (tp, true); +r8153_teredo_off (tp); + +ocp_dword_clr_bits (tp,MCU_TYPE_PLA ,PLA_RCR ,RCR_ACPT_ALL ); + +rtl8152_nic_reset (tp); +rtl_reset_bmu (tp); + +ocp_byte_clr_bits (tp,MCU_TYPE_PLA ,PLA_OOB_CTRL ,NOW_IS_OOB ); + +ocp_word_clr_bits (tp,MCU_TYPE_PLA ,PLA_RCR1 ,BIT (3 )); +
drivers/net/usb/r8152.c static void rtl8156_down(struct r8152 *tp) · line 7045
Quoted 10 lines
drivers/net/usb/r8152.c +10
-0+ocp_word_clr_bits (tp,MCU_TYPE_PLA ,PLA_SFF_STS_7 ,MCU_BORW_EN ); + +rtl_rx_vlan_en (tp, tp->netdev->features &NETIF_F_HW_VLAN_CTAG_RX ); + +rtl8156_change_mtu (tp); + +/* share FIFO settings */ +ocp_word_w0w1 (tp,MCU_TYPE_PLA ,PLA_RXFIFO_FULL ,RXFIFO_FULL_MASK , +0x08 ); +
[Severity: Medium]
The shared rtl8156_up() clears PLA_MCU_SPDWN_EN at this point in the sequence, unconditionally, including for VER_16/17:
ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, PLA_MCU_SPDWN_EN);
rtl8157_up() drops it, and rtl8157_down() drops the matching ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, PLA_MCU_SPDWN_EN) that rtl8156_down() still performs. After this patch no code path for RTL8157/8159 touches PLA_MCU_SPDWN_EN at all - r8157_init() and r8159_init() never write PLA_MAC_PWR_CTRL3, while r8156_init() and r8156b_init() do.
If the bit is already set when the interface is brought up (OOB firmware default, or an older kernel that set it in its down path before unbind), can the MAC MCU stay armed for clock/power-down while the interface is running? Is dropping the arm/disarm pair for VER_16/17 intentional?
drivers/net/usb/r8152.c static void rtl8156_down(struct r8152 *tp) · line 7047
Quoted 2 lines
drivers/net/usb/r8152.c +2
-0+ocp_word_clr_bits (tp,MCU_TYPE_USB ,USB_SPEED_OPTION , +RG_PWRDN_EN |ALL_SPEED_OFF );
[Severity: High]
This looks like the second inverted guard. In the shared rtl8156_up() the same write was explicitly skipped for VER_16 and later:
if (tp->version < RTL_VER_16) ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_SPEED_OPTION, RG_PWRDN_EN | ALL_SPEED_OFF);
rtl8157_up() is installed as ops->up only for RTL_VER_16 and the two RTL_VER_17 packages, so the statement ended up in the single path the guard excluded. Should it be dropped here instead?
For context, git grep USB_SPEED_OPTION shows the register is programmed by r8156_init() and r8156b_init(), but never by r8157_init() or r8159_init(), which suggests the 8157/8159 sequence intentionally leaves RG_PWRDN_EN and ALL_SPEED_OFF alone.
Quoted 37 lines
drivers/net/usb/r8152.c +37
-0+ +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); + +/* Clear_SDR */ +ocp_byte_set_bits (tp,MCU_TYPE_USB ,0xd378 ,BIT (7 )); +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); + +ocp_byte_clr_bits (tp,MCU_TYPE_PLA ,PLA_OOB_CTRL ,NOW_IS_OOB ); + +/* RX FIFO settings for OOB */ +ocp_write_word (tp,MCU_TYPE_PLA ,PLA_RXFIFO_FULL ,64 /16 ); +ocp_write_word (tp,MCU_TYPE_PLA ,PLA_RX_FIFO_FULL ,1024 /16 ); +ocp_write_word (tp,MCU_TYPE_PLA ,PLA_RX_FIFO_EMPTY ,4096 /16 ); + +rtl_disable (tp); +rtl_reset_bmu (tp); + +ocp_write_word (tp,MCU_TYPE_PLA ,PLA_RMS ,1526 ); +ocp_write_byte (tp,MCU_TYPE_PLA ,PLA_MTPS ,10 *1024 /64 );
[ ... ]
Quoted 13 lines
drivers/net/usb/r8152.c +1
-1@@ -10071,7 +10214,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 = r8156b_init; - ops->enable = rtl8156_enable; + ops->enable = rtl8156b_enable; ops->disable = rtl8153_disable; ops->up = rtl8156_up; ops->down = rtl8156_down;@@ -10110,10 +10253,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;
drivers/net/usb/r8152.c static int rtl_ops_init(struct r8152 *tp) · line 10262
Quoted 10 lines
drivers/net/usb/r8152.c +3
-3- 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 = rtl8153_unload; ops->eee_get = r8153_get_eee; ops->eee_set = r8152_set_eee;
[ ... ]
For reference, none of the above changes appear to be adjusted by the later patches in this series.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260903072852.58695-1-nic_swsd%40realtek.com
PATCH v2 4/8 r8152: refactor r8157_hw_phy_cfg
In reply to Chih Kai HsuRTL8159 has a different hw_phy_cfg sequence from RTL8157. Split r8157_hw_phy_cfg into r8157_hw_phy_cfg (VER_16) and r8159_hw_phy_cfg (VER_17_*), update PHY parameters for RTL8156B/57/59, and add sram2 bitwise operation helpers.
--- drivers/net/usb/r8152.c | 168 +++++++++++++++++++++++++++++++++------- 1 file changed, 139 insertions(+), 29 deletions(-)
drivers/net/usb/r8152.c +139 -29
@@ -655,6 +655,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 */
@@ -1992,6 +1993,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;
@@ -8082,6 +8093,9 @@ static void r8156b_hw_phy_cfg(struct r8152 *tp)sram_write (tp,0x8074 ,0x2417 );sram_write (tp,0x807a ,0x2417 ); +/* Nway DACONB parameters */ +ocp_reg_w0w1 (tp,0xa4ca ,0x6000 ,0x0040 ); +/* XG PLL */ ocp_reg_w0w1 (tp,0xbf84 ,0xe000 ,0xa000 );break ;
@@ -8158,11 +8172,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 */
@@ -8178,7 +8195,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 );
@@ -8284,12 +8301,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 */
@@ -8299,22 +8389,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 );
@@ -8378,10 +8481,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 */
@@ -8390,14 +8493,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 );
@@ -8408,7 +8511,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 );
@@ -8467,8 +8570,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 ; }
@@ -8478,9 +8588,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;
@@ -8492,7 +8602,7 @@ 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)
@@ -10297,7 +10407,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 = rtl8156_change_mtu; tp->rx_buf_sz =48 *1024 ;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH net-next v2 4/8] r8152: refactor r8157_hw_phy_cfg
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: 1 (1) · Low: 2
- [Medium] r8159_hw_phy_cfg() (new RTL_VER_17_QFN68/QFN100 callback, drivers/net/usb/r8152.c:8332) drops the unconditional…
- [Low] The commit is presented as a refactor ('Split r8157_hw_phy_cfg into ... and add sram2 bitwise operation helpers') and only vaguely…
- [Low] The newly added EN_ALDPS_PLLOFF define in the OCP_POWER_CFG block uses space alignment while all neighbouring defines in the same block…
Pre-existing issues:
- [Medium] Both r8157_hw_phy_cfg() and the new r8159_hw_phy_cfg() return early when rtl_phy_patch_request(tp, true, true) fails, after ALDPS…
--
r8152: refactor r8157_hw_phy_cfg
This splits the shared 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, adds sram2_set_bits()/sram2_clr_bits() helpers, and updates PHY parameters for RTL8156B/8157/8159. A few questions below.
drivers/net/usb/r8152.c enum spd_duplex { · line 659
Quoted 10 lines
drivers/net/usb/r8152.c +1
-0@@ -655,6 +655,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
[Severity: Low]
This isn't a bug, but the new define uses spaces for alignment while EEE_CLKDIV_EN, EN_ALDPS and EN_10M_PLLOFF in the same block use tabs. Could it use a tab as well?
[ ... ]
drivers/net/usb/r8152.c static void r8156b_hw_phy_cfg(struct r8152 *tp) · line 8094
Quoted 10 lines
drivers/net/usb/r8152.c +3
-0@@ -8075,6 +8086,9 @@ static void r8156b_hw_phy_cfg(struct r8152 *tp)sram_write (tp,0x8074 ,0x2417 );sram_write (tp,0x807a ,0x2417 ); +/* Nway DACONB parameters */ +ocp_reg_w0w1 (tp,0xa4ca ,0x6000 ,0x0040 ); +/* XG PLL */ ocp_reg_w0w1 (tp,0xbf84 ,0xe000 ,0xa000 );break ;
[Severity: Low]
The subject says "refactor" and the changelog says "update PHY parameters for RTL8156B/57/59", but several hardware behaviour changes for already shipped parts are mixed in with the mechanical sram/sram2 helper conversions. Could these be split out, or at least described?
This new Nway DACONB write lands in r8156b_hw_phy_cfg(), reached through the RTL_VER_13 fallthrough and the RTL_VER_15 case, so it applies to shipped RTL8156B devices even though the commit is about the r8157/r8159 split.
In r8157_hw_phy_cfg() below, the 0xd3c8 BIT(0) clear moves out of the RTL_VER_17 case into the common preamble, so RTL8157 (RTL_VER_16) now also gets the ALDPS clock-bypass change.
In the r8159_hw_phy_cfg() tail below, EN_10M_CLKDIV changes from set to cleared and tp->ups_info._10m_ckdiv flips from true to false, which changes the UPS flag word programmed on every runtime suspend:
if (tp->ups_info._10m_ckdiv) ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
0xa5ea additionally clears BIT(2), sram2 0x8ffe changes from 0xf450 to 0xf4ff, and there are new CLKS latency, CMP_Timer, spdchg_pga1_lpf_cap and ENET PLL writes. What failure modes do these address, and which package or revision were they tested on? Should any of them carry a Fixes tag?
Quoted 16 lines
drivers/net/usb/r8152.c +4
-1@@ -8151,11 +8165,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 */
[ ... ]
drivers/net/usb/r8152.c static void r8157_hw_phy_cfg(struct r8152 *tp) · line 8302
Quoted 10 lines
drivers/net/usb/r8152.c +6
-0@@ -8277,12 +8294,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 ;
[Severity: Medium]
This isn't a bug introduced by this patch, but the early return without rollback is now duplicated into the RTL8159 path as well. By this point r8153_aldps_en(tp, false) and rtl_eee_enable(tp, false) have already run and most per-version parameters have been written, and rtl_phy_patch_request() can fail on timeout or when RTL8152_INACCESSIBLE is set:
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;
}On that path r8153_aldps_en(tp, true), r8152b_enable_fc() and set_bit(PHY_RESET, &tp->flags) are all skipped, so the device is left with ALDPS and EEE off, pause advertisement unrefreshed, the freshly written SRAM/OCP parameters never latched by a PHY reset, and tp->ups_info out of sync with the hardware. Would it make sense to undo the ALDPS/EEE disable before returning?
Quoted 47 lines
drivers/net/usb/r8152.c +47
-0+ +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 :
drivers/net/usb/r8152.c static void r8157_hw_phy_cfg(struct r8152 *tp) · line 8366
Quoted 10 lines
drivers/net/usb/r8152.c +9
-0+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 );
[Severity: Medium]
Is the missing rtl_eee_enable(tp, false) here intentional? Before the split, RTL_VER_17_QFN68/QFN100 went through the shared r8157_hw_phy_cfg() which did:
r8153_aldps_en(tp, false); rtl_eee_enable(tp, false);
The retained r8157_hw_phy_cfg() still has both calls, and r8156b_hw_phy_cfg() carries the same step with the comment "/* disable EEE before updating the PHY parameters */", but r8159_hw_phy_cfg() only disables ALDPS, so the whole RTL8159 SRAM/SRAM2/OCP block and the rtl_phy_patch_request() window run with EEE still enabled.
The tail of the function is enable-only:
if (tp->eee_en) rtl_eee_enable(tp, true);
so when the user has turned EEE off via r8152_set_eee() (tp->eee_en == false), nothing clears OCP_EEE_CFG/OCP_EEE_ADV after hw_phy_cfg() runs again from rtl_hw_phy_work_func_t() following a power cut, firmware reload or resume. Can hardware EEE state and the tp->eee_en / tp->ups_info.eee shadow diverge in that case? rtl_eee_enable() does handle RTL_VER_17_QFN68/QFN100, so this does not look like a hardware limitation.
[ ... ]
Quoted 48 lines
drivers/net/usb/r8152.c +13
-6@@ -8460,8 +8563,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 ; }@@ -8471,9 +8581,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;@@ -8485,7 +8595,7 @@ 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)@@ -10284,7 +10394,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 = rtl8156_change_mtu; tp->rx_buf_sz =48 *1024 ;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260903072852.58695-1-nic_swsd%40realtek.com
PATCH v2 5/8 r8152: support rtl8157_unload and rtl8157_change_mtu
In reply to Chih Kai HsuRTL8157 and RTL8159 need a dedicated unload callback to disable interrupt mitigation and use r8157_power_cut_en instead of r8153_power_cut_en. They also need a dedicated change_mtu that scales MTPS between 10K and 16K depending on the MTU, unlike rtl8156_change_mtu which uses a fixed MTPS.
--- drivers/net/usb/r8152.c | 47 ++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-)
drivers/net/usb/r8152.c +37 -10
@@ -7031,6 +7031,28 @@ static void rtl8156_down(struct r8152 *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))
@@ -7055,7 +7077,7 @@ static void rtl8157_up(struct r8152 *tp)rtl_rx_vlan_en (tp, tp->netdev->features &NETIF_F_HW_VLAN_CTAG_RX ); -rtl8156_change_mtu (tp); +rtl8157_change_mtu (tp);/* share FIFO settings */ ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, RXFIFO_FULL_MASK,
@@ -10187,11 +10209,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)
@@ -10202,6 +10219,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 );
@@ -10380,13 +10407,13 @@ static int rtl_ops_init(struct r8152 *tp) ops->disable = rtl8153_disable; ops->up = rtl8157_up; ops->down = rtl8157_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; 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 ;
@@ -10403,13 +10430,13 @@ static int rtl_ops_init(struct r8152 *tp) ops->disable = rtl8153_disable; ops->up = rtl8157_up; ops->down = rtl8157_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; ops->hw_phy_cfg = r8159_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
[email protected]
AI reviewRe: [PATCH net-next v2 5/8] r8152: support rtl8157_unload and rtl8157_change_mtu
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
- [Medium] The commit is presented as adding new callback "support" for RTL8157/RTL8159, but it silently changes runtime register programming…
--
r8152: support rtl8157_unload and rtl8157_change_mtu
This adds rtl8157_unload(), which uses r8157_power_cut_en() and disables interrupt mitigation, and rtl8157_change_mtu(), which scales PLA_MTPS with the configured MTU. Both are wired up for RTL_VER_16 and RTL_VER_17_QFN68/QFN100 in rtl_ops_init(), replacing rtl8153_unload and rtl8156_change_mtu.
(no Link: tags in the commit header)
Quoted 23 lines
drivers/net/usb/r8152.c +15
-0@@ -7024,6 +7024,28 @@ static void rtl8156_down(struct r8152 *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);
[ ... ]
Quoted 14 lines
drivers/net/usb/r8152.c +10
-0@@ -10189,6 +10206,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 )); +} +
[ ... ]
drivers/net/usb/r8152.c static int rtl_ops_init(struct r8152 *tp) · line 10403
Quoted 13 lines
drivers/net/usb/r8152.c +2
-2@@ -10367,13 +10394,13 @@ static int rtl_ops_init(struct r8152 *tp) ops->disable = rtl8153_disable; ops->up = rtl8157_up; ops->down = rtl8157_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; 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;
[Severity: Medium]
The subject and changelog describe this as adding "support", but these two ops assignments change register programming for hardware that released kernels already drive. RTL8157 (RTL_VER_16) was enabled by fd3c7d080df5 ("r8152: Add support for the RTL8157 hardware") and RTL8159 (RTL_VER_17) by ad2a55b63b41 ("r8152: Add support for the RTL8159 chip"), and the baseline already assigns rtl8153_unload / rtl8156_change_mtu for both.
For the unload path, the old and new helpers are not equivalent:
r8153_power_cut_en(tp, false): 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); r8157_power_cut_en(tp, false): 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));
So PHASE2_EN is now left set and USB_MISC_2 BIT(1) is now cleared on these chips. This runs both from the probe error path and from rtl8152_disconnect() via tp->rtl_ops.unload(), so the state left on the chip after unplug or module removal differs from released kernels. Given that r8157_init() and r8159_init() already use r8157_power_cut_en() for these versions, was the earlier rtl8153_unload assignment simply wrong? If so, should this carry a Fixes: tag against fd3c7d080df5 and ad2a55b63b41, and name the observable misbehaviour (power cut, suspend or resume, WOL) that it corrects?
The change_mtu switch has the same question. rtl8156_change_mtu() writes a fixed PLA_MTPS of MTPS_JUMBO (12 * 1024 / 64), while rtl8157_change_mtu() writes max(mtu_to_size(mtu), 10K) / 64 clamped to 0xff. netdev->max_mtu for RTL_VER_16 and RTL_VER_17_* is size_to_mtu(16 * 1024), so on a released kernel a user can already configure a frame size above the fixed 12K MTPS threshold through rtl8152_change_mtu() -> tp->rtl_ops.change_mtu(). Was that mismatch causing a visible TX problem, and can the changelog say what it was instead of only "unlike rtl8156_change_mtu which uses a fixed MTPS"?
As submitted, it is hard for a stable backporter to tell whether this is a fix for shipping RTL8157/RTL8159 adapters or a cleanup with no user impact. The rest of the series does not add the missing rationale or a Fixes: tag either.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260903072852.58695-1-nic_swsd%40realtek.com
PATCH v2 6/8 r8152: add TGPHY access support
In reply to Chih Kai HsuRTL8157 and RTL8159 support a TGPHY interface via USB_TGPHY_CMD/ADDR/DATA, which allows reading/writing PHY registers without changing the OCP base address at 0xe86c. Add r8157_phy_read/phy_write implementing this path, and add phy_read/phy_write function pointers to struct rtl_ops to dispatch the correct access method per chip.
--- drivers/net/usb/r8152.c | 94 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-)
drivers/net/usb/r8152.c +92 -2
@@ -162,6 +162,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
@@ -511,6 +514,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
@@ -960,6 +967,8 @@ struct r8152 {void (*hw_phy_cfg)(struct r8152 *tp);void (*autosuspend_en)(struct r8152 *tp,bool enable);void (*change_mtu)(struct r8152 *tp); +u16 (*phy_read)(struct r8152 *tp,u16 addr); +void (*phy_write)(struct r8152 *tp,u16 addr,u16 data); } rtl_ops;struct ups_info {
@@ -1666,7 +1675,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 u16 r8152_phy_read (struct r8152 *tp,u16 addr) {u16 ocp_base, ocp_index;
@@ -1680,7 +1689,7 @@ static u16 ocp_reg_read(struct r8152 *tp, u16 addr)return ocp_read_word (tp,MCU_TYPE_PLA , ocp_index); } -static void ocp_reg_write (struct r8152 *tp,u16 addr,u16 data) +static void r8152_phy_write (struct r8152 *tp,u16 addr,u16 data) {u16 ocp_base, ocp_index;
@@ -1694,6 +1703,16 @@ static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)ocp_write_word (tp,MCU_TYPE_PLA , ocp_index, data); } +static u16 ocp_reg_read (struct r8152 *tp,u16 addr) +{ +return tp->rtl_ops.phy_read (tp, addr); +} + +static void ocp_reg_write (struct r8152 *tp,u16 addr,u16 data) +{ + tp->rtl_ops.phy_write (tp, addr, data); +} +static inline void r8152_mdio_write (struct r8152 *tp,u32 reg_addr,u32 value) {ocp_reg_write (tp,OCP_BASE_MII + reg_addr *2 , value);
@@ -2030,6 +2049,61 @@ static int r8152_mdio_test_and_clr_bit(struct r8152 *tp, u16 addr, u16 clear)return data & clear; } +static int wait_tgphy_cmd_ready (struct r8152 *tp) +{ +u16 ocp_data; + +return poll_timeout_us (ocp_data =ocp_read_word (tp,MCU_TYPE_USB , +USB_TGPHY_CMD ), + !(ocp_data &TGPHY_CMD_BUSY ),2000 ,20000 , + false); +} + +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: +return ret; +} + +static u16 r8157_phy_read (struct r8152 *tp,u16 addr) +{ +u16 data =0 ; + +rtl_tgphy_access (tp, addr, &data, false); + +return data; +} + +static void r8157_phy_write (struct r8152 *tp,u16 addr,u16 data) +{ +rtl_tgphy_access (tp, addr, &data, true); +} +static int r8152_submit_rx (struct r8152 *tp,struct rx_agg *agg,gfp_t mem_flags);
@@ -10281,6 +10355,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 ;
@@ -10303,6 +10379,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
@@ -10326,6 +10404,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 ;
@@ -10350,6 +10430,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);
@@ -10375,6 +10457,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 ;
@@ -10392,6 +10476,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 ;
@@ -10414,6 +10500,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 ;
@@ -10437,6 +10525,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
[email protected]
AI reviewRe: [PATCH net-next v2 6/8] r8152: add TGPHY access support
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 1 (1) · Low: 1
- [Medium] The new TGPHY PHY access path silently swallows failures.
- [Low] wait_tgphy_cmd_ready() (drivers/net/usb/r8152.c:2045-2053) is a functional duplicate of the pre-existing, already parameterised…
Pre-existing issues:
- [Medium] The new TGPHY helpers make control-flow decisions on, and can return to user space, uninitialized kernel stack bytes.
--
r8152: add TGPHY access support
This adds a TGPHY register access path for RTL8157 and RTL8159 using USB_TGPHY_CMD/ADDR/DATA, so PHY registers can be reached without reprogramming the OCP GPHY base. It also introduces phy_read/phy_write pointers in struct rtl_ops so ocp_reg_read()/ocp_reg_write() dispatch to either the existing r8152 path or the new r8157 path.
Quoted 4 lines
drivers/net/usb/r8152.c +0
-0
[ ... ]
Quoted 11 lines
drivers/net/usb/r8152.c +4
-0@@ -511,6 +514,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
[ ... ]
Quoted 17 lines
drivers/net/usb/r8152.c +10
-0@@ -1687,6 +1696,16 @@ static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)ocp_write_word (tp,MCU_TYPE_PLA , ocp_index, data); } +static u16 ocp_reg_read (struct r8152 *tp,u16 addr) +{ +return tp->rtl_ops.phy_read (tp, addr); +} + +static void ocp_reg_write (struct r8152 *tp,u16 addr,u16 data) +{ + tp->rtl_ops.phy_write (tp, addr, data); +} +static inline void r8152_mdio_write (struct r8152 *tp,u32 reg_addr,u32 value) {ocp_reg_write (tp,OCP_BASE_MII + reg_addr *2 , value);
[ ... ]
drivers/net/usb/r8152.c static int r8152_mdio_test_and_clr_bit(struct r8152 *tp, u16 addr, u16 clear) · line 2053
Quoted 13 lines
drivers/net/usb/r8152.c +9
-0@@ -2023,6 +2042,61 @@ static int r8152_mdio_test_and_clr_bit(struct r8152 *tp, u16 addr, u16 clear)return data & clear; } +static int wait_tgphy_cmd_ready (struct r8152 *tp) +{ +u16 ocp_data; + +return poll_timeout_us (ocp_data =ocp_read_word (tp,MCU_TYPE_USB , +USB_TGPHY_CMD ), + !(ocp_data &TGPHY_CMD_BUSY ),2000 ,20000 , + false); +}
[Severity: Low]
This isn't a bug, but is wait_tgphy_cmd_ready() needed at all? The driver already has a parameterised helper right above ocp_adv_read():
static int wait_cmd_ready(struct r8152 *tp, u16 cmd)
{
return poll_timeout_us(u16 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, cmd),
!(ocp_data & ADV_CMD_BUSY), 2000, 20000, false);
}ADV_CMD_BUSY and the new TGPHY_CMD_BUSY are both BIT(0), and the poll interval and timeout are identical, so wait_cmd_ready(tp, USB_TGPHY_CMD) looks like an exact substitute. With two copies, a later change to the timeout or the addition of an RTL8152_INACCESSIBLE early-out would only land in one of them.
[Severity: Medium]
This is a pre-existing issue and not introduced by this patch, but the new helper makes a hardware-readiness decision on a value that can be uninitialized stack data. ocp_read_word() does:
u32 data; __le32 tmp; ... generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen); data = __le32_to_cpu(tmp);
and generic_ocp_read() starts with:
if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) return -ENODEV;
That early return never touches the caller buffer, and the return value is not checked, so tmp keeps its stack contents. A genuine transfer error is not affected because get_registers() does memset(data, 0xff, size).
Can that make the poll condition !(ocp_data & TGPHY_CMD_BUSY) true on garbage, so rtl_tgphy_access() proceeds to issue the ADDR/CMD writes?
The same value also reaches user space on the read side:
r8157_phy_read() -> ocp_reg_read() -> r8152_mdio_read() -> rtl8152_ioctl(SIOCGMIIREG) -> data->val_out
rtl8152_ioctl() only tests RTL8152_INACCESSIBLE once on entry, so if rtl_set_unplug() sets the flag while the ioctl is in flight, does val_out end up carrying up to two bytes of kernel stack?
Quoted 35 lines
drivers/net/usb/r8152.c +35
-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; + +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: +return ret; +} + +static u16 r8157_phy_read (struct r8152 *tp,u16 addr) +{ +u16 data =0 ;
drivers/net/usb/r8152.c static int r8152_mdio_test_and_clr_bit(struct r8152 *tp, u16 addr, u16 clear) · line 2105
Quoted 10 lines
drivers/net/usb/r8152.c +10
-0+ +rtl_tgphy_access (tp, addr, &data, false); + +return data; +} + +static void r8157_phy_write (struct r8152 *tp,u16 addr,u16 data) +{ +rtl_tgphy_access (tp, addr, &data, true); +}
[Severity: Medium]rtl_tgphy_access() carefully produces -ETIMEDOUT when TGPHY_CMD_BUSY stays set for the full 20 ms, but both wrappers drop it. Is that intentional?
On the read side r8157_phy_read() returns the pre-initialised 0 to the caller, which cannot tell it apart from a real register value of 0. Since almost every PHY operation in this driver is a read-modify-write, that 0 gets written straight back into the live register:
ocp_reg_w0w1() data = ocp_reg_read(tp, addr); data = (data & ~clear) | set; ocp_reg_write(tp, addr, data); r8152_mdio_set_bit() data = r8152_mdio_read(tp, addr); r8152_mdio_write(tp, addr, data | set);
so every bit the hardware actually held outside the set mask is cleared. The comment in r8152_control_msg() describes what that costs:
"Much of the code in the driver doesn't check for errors...
Writing back modified garbage like this can fully wedge the adapter, requiring a power cycle"
A stuck TGPHY_CMD_BUSY is not a USB transfer error, so the RTL8152_INACCESSIBLE / usb_queue_reset_device() protection that normally catches this does not trigger.
On the write side r8157_phy_write() is void, so a timeout in the entry wait skips the DATA/ADDR/CMD programming entirely and returns silently. Does that leave tp->ups_info, the cached speed/EEE state and rtl8152_set_link_ksettings() recording settings the PHY never received, with nothing in dmesg?
The driver's existing implementation of the same ADDR/DATA/CMD plus BUSY protocol propagates the error instead: ocp_adv_read()/ocp_adv_write() return int and rtl_bmu_read()/rtl_bmu_write()/rtl_bmu_w0w1() check it. Would it be reasonable to at least log the failure, or to give the new rtl_ops.phy_read/phy_write callbacks an error channel?
A related question on wait_tgphy_cmd_ready(): it has no test_bit(RTL8152_INACCESSIBLE, &tp->flags) early-out, unlike the polling loop in rtl_phy_patch_request():
if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) return -ENODEV;
Once the device is gone, does each PHY access then burn up to 20 ms twice? r8157_hw_phy_cfg()/r8159_hw_phy_cfg() perform hundreds of accesses from rtl_hw_phy_work_func_t(), and rtl8152_disconnect() waits for that work with cancel_delayed_work_sync(&tp->hw_phy_work), so can an unplug turn into a multi-second stall?
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260903072852.58695-1-nic_swsd%40realtek.com
PATCH v2 7/8 r8152: support rtl_fc_pause_pkt_en()
In reply to Chih Kai HsuThe flow control patch was inline in r8156b_init and missing for RTL8156 and RTL8157/8159. Extract it as rtl_fc_pause_pkt_en(), call it from r8156_init and r8156b_init during chip init, and call it from rtl8157_enable with the current link speed for RTL8157 and RTL8159.
--- drivers/net/usb/r8152.c | 112 ++++++++++++++++++++++++++++++++++------ 1 file changed, 97 insertions(+), 15 deletions(-)
drivers/net/usb/r8152.c +97 -15
@@ -89,6 +89,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
@@ -301,6 +302,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
@@ -6095,6 +6100,93 @@ 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_QFN68 : +case RTL_VER_17_QFN100 : +ocp_word_clr_bits (tp,MCU_TYPE_PLA ,PLA_RCR ,SLOT_EN ); + + num_pause_pkts =0xa ; + ratio =10000 ; + +if (!(speed &LINK_STATUS )) { +dev_dbg (&tp->intf->dev,"No link\n" ); + goto no_link; + }else 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 { +dev_err (&tp->intf->dev,"Unknown link speed\n" ); + goto no_link; + } + + log2_ratio =ilog2 (ratio); + num_pause_pkts -= log2_ratio; + +/* Round up if ratio is more than halfway to the next power of 2. +* 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--; + +no_link: +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;
@@ -6626,6 +6718,8 @@ static int rtl8157_enable(struct r8152 *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);
@@ -8758,6 +8852,8 @@ static void r8156_init(struct r8152 *tp)usb_enable_lpm (tp->udev); +rtl_fc_pause_pkt_en (tp,0 ); +r8156_mac_clk_spd (tp, true); ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
@@ -8794,7 +8890,6 @@ static void r8156b_u2phy_backup(struct r8152 *tp)static void r8156b_init (struct r8152 *tp) { -u32 ocp_data;u16 data;if (test_bit (RTL8152_INACCESSIBLE , &tp->flags))
@@ -8859,20 +8954,7 @@ static void r8156b_init(struct r8152 *tp)usb_enable_lpm (tp->udev); -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 ); +rtl_fc_pause_pkt_en (tp,0 );r8156_mac_clk_spd (tp, true);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH net-next v2 7/8] r8152: support rtl_fc_pause_pkt_en()
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
- [Medium] Commit message describes the change as an extraction of the inline flow-control block from r8156b_init, but only the…
--
r8152: support rtl_fc_pause_pkt_en()
This moves the inline flow control block out of r8156b_init() into a new helper rtl_fc_pause_pkt_en(), and calls it from r8156_init(), r8156b_init() and rtl8157_enable(). It also adds a new PLA_PAUSE_LIMIT register with PAUSE_LIMIT_EN and PAUSE_LIMIT_MASK bits used by the RTL8157/RTL8159 path.
Quoted 34 lines
drivers/net/usb/r8152.c +12
-0@@ -89,6 +89,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@@ -301,6 +302,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@@ -6088,6 +6093,93 @@ 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) {
drivers/net/usb/r8152.c static void r8152b_enter_oob(struct r8152 *tp) · line 6112
Quoted 10 lines
drivers/net/usb/r8152.c +10
-0+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 ;
[Severity: Medium]
The changelog says "Extract it as rtl_fc_pause_pkt_en()", but does that describe all three arms of this switch?
Only the RTL_VER_12/13/15 arm below is code moved out of r8156b_init().
At the baseline commit (6ebcf5074cff) the USB_FC_TIMER / FLOW_CTRL_PATCH_OPT / FC_PATCH_TASK writes above have no counterpart for RTL_VER_10 or RTL_VER_11 anywhere in the tree, and PLA_PAUSE_LIMIT does not exist at all. So r8156_init() (RTL_VER_10/11) and rtl8157_enable() (RTL_VER_16 and RTL_VER_17_*) both start touching flow control registers that were previously never programmed on already-supported hardware.
Could the changelog describe the new register programming separately from the extraction, and state which user-visible symptom the new writes fix? If they fix a defect on shipped chips, would Fixes: tags be appropriate so stable tooling can tell which releases are affected?
Also, the new PLA_PAUSE_LIMIT define uses spaces instead of tabs, unlike the surrounding register defines.
Quoted 15 lines
drivers/net/usb/r8152.c +15
-0+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 );
drivers/net/usb/r8152.c static void r8152b_enter_oob(struct r8152 *tp) · line 6144
Quoted 10 lines
drivers/net/usb/r8152.c +10
-0+ +ocp_word_set_bits (tp,MCU_TYPE_USB ,USB_FW_TASK ,FC_PATCH_TASK ); +break ; +case RTL_VER_16 : +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 ; + ratio =10000 ;
[Severity: Medium]
This whole RTL_VER_16 / RTL_VER_17_QFN68 / RTL_VER_17_QFN100 arm is new register programming rather than moved code, and the two starting values (0xa and 10000) plus the log2 rounding heuristic below are unexplained.
Could a comment be added describing what PLA_PAUSE_LIMIT[7:4] means in hardware terms, and how 0xa and the ratio of 10000 relate to it?
Quoted 65 lines
drivers/net/usb/r8152.c +49
-0+ +if (!(speed &LINK_STATUS )) { +dev_dbg (&tp->intf->dev,"No link\n" ); + goto no_link; + }else 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 { +dev_err (&tp->intf->dev,"Unknown link speed\n" ); + goto no_link; + } + + log2_ratio =ilog2 (ratio); + num_pause_pkts -= log2_ratio; + +/* Round up if ratio is more than halfway to the next power of 2. +* 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--; + +no_link: +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;@@ -6619,6 +6711,8 @@ static int rtl8157_enable(struct r8152 *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);@@ -8751,6 +8845,8 @@ static void r8156_init(struct r8152 *tp)usb_enable_lpm (tp->udev); +rtl_fc_pause_pkt_en (tp,0 ); +r8156_mac_clk_spd (tp, true);
[ ... ]
Quoted 22 lines
drivers/net/usb/r8152.c +1
-14@@ -8852,20 +8947,7 @@ static void r8156b_init(struct r8152 *tp)usb_enable_lpm (tp->udev); -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 ); +rtl_fc_pause_pkt_en (tp,0 );r8156_mac_clk_spd (tp, true);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260903072852.58695-1-nic_swsd%40realtek.com
PATCH v2 8/8 r8152: support UPS for RTL8157 and RTL8159
In reply to Chih Kai HsuRTL8157 and RTL8159 support UPS but need different enable logic and EEE flag handling than RTL8156. Add r8157_ups_en() and extend r8156_ups_flags() with per-speed EEE flags (100M through 10G) and 5G/10G speed entries for VER_16/17.
--- drivers/net/usb/r8152.c | 110 +++++++++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 12 deletions(-)
drivers/net/usb/r8152.c +98 -12
@@ -132,6 +132,7 @@#define USB_BURST_SIZE 0xcfc0#define USB_FW_FIX_EN0 0xcfca#define USB_FW_FIX_EN1 0xcfcc +#define USB_FW_USE_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)
@@ -4185,8 +4191,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 ;
@@ -4237,20 +4262,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 ; }
@@ -4422,6 +4460,35 @@ static void r8156_ups_en(struct r8152 *tp, bool enable) } } +static void r8157_ups_en (struct r8152 *tp,bool enable) +{ +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 ); + +if (ocp_read_word (tp,MCU_TYPE_USB ,USB_MISC_0 ) &PCUT_STATUS ) { +/* clear USB fw_ver_reg */ +ocp_write_byte (tp,MCU_TYPE_USB ,USB_FW_USE_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)
@@ -4580,9 +4647,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);
@@ -9027,7 +9113,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);
@@ -9137,7 +9223,7 @@ static void r8159_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
[email protected]
AI reviewRe: [PATCH net-next v2 8/8] r8152: support UPS 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: 1 · Medium: 2 · Low: 0
- [High] The three new version switches added by this patch (in r8157_ups_en()'s two call sites inside rtl8157_runtime_enable(), and…
- [Medium] In the resume (!enable) path of rtl8157_runtime_enable(), r8157_ups_en(tp, false) is invoked only after r8153_queue_wake(tp,…
- [Medium] r8157_ups_en(tp, false) detects PCUT_STATUS (a UPS power cut occurred, so the PHY/GPHY domain was powered down and is re-loading)…
--
r8152: support UPS for RTL8157 and RTL8159
This adds r8157_ups_en() for the RTL8157/RTL8159 UPS enable sequence and extends r8156_ups_flags() with per-speed EEE flags and 5G/10G speed encodings. rtl8157_runtime_enable() now arms and disarms UPS around runtime suspend, and the two init paths switch from r8156_ups_en() to the new helper.
Quoted 4 lines
drivers/net/usb/r8152.c +0
-0
[ ... ]
Quoted 30 lines
drivers/net/usb/r8152.c +21
-2@@ -4178,8 +4184,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 ;
[ ... ]
Quoted 42 lines
drivers/net/usb/r8152.c +21
-8@@ -4230,20 +4255,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 ; }
[ ... ]
Quoted 20 lines
drivers/net/usb/r8152.c +16
-0@@ -4415,6 +4453,35 @@ static void r8156_ups_en(struct r8152 *tp, bool enable) } } +static void r8157_ups_en (struct r8152 *tp,bool enable) +{ +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 );
drivers/net/usb/r8152.c static void r8156_ups_en(struct r8152 *tp, bool enable) · line 4481
Quoted 10 lines
drivers/net/usb/r8152.c +10
-0+ +if (ocp_read_word (tp,MCU_TYPE_USB ,USB_MISC_0 ) &PCUT_STATUS ) { +/* clear USB fw_ver_reg */ +ocp_write_byte (tp,MCU_TYPE_USB ,USB_FW_USE_VER ,0 ); + + tp->rtl_ops.hw_phy_cfg (tp); + +rtl8152_set_speed (tp, tp->autoneg, tp->speed, + tp->duplex, tp->advertising); + }
[Severity: Medium]
Should this PCUT_STATUS branch wait for autoload to complete before touching the PHY? The two peer helpers in the same driver guard the identical branch:
drivers/net/usb/r8152.c:r8153b_ups_en() {
...
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);
...
}r8153c_ups_en() does the same. r8157_ups_en() was copied from r8156_ups_en(), which also lacks the wait, but for this family that branch was previously unreachable: r8156_ups_en()/r8157_ups_en() were only ever called with enable=false from the init paths, always after r8157_power_cut_en(tp, false) had already cleared PCUT_STATUS. This patch adds the first call with enable=true in rtl8157_runtime_enable(), so the branch becomes live on the first runtime resume that follows a real power cut.
For RTL8159 there is a second wait that looks relevant:
drivers/net/usb/r8152.c:r8159_wait_backup_restore() {
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);
}That poll is itself predicated on PCUT_STATUS, and r8159_init() treats a timeout there as fatal ("init failed, backup-restore timed out"). Can hw_phy_cfg() run here while the GPHY backup-restore engine is still running, given hw_phy_cfg() itself sets GPHY_PATCH_DONE | BACKUP_RESTRORE?
Quoted 6 lines
drivers/net/usb/r8152.c +3
-0+ } +} +static void r8153_power_cut_en (struct r8152 *tp,bool enable) {if (enable)
[ ... ]
drivers/net/usb/r8152.c static void rtl8157_runtime_enable(struct r8152 *tp, bool enable) · line 4651
Quoted 13 lines
drivers/net/usb/r8152.c +9
-0@@ -4573,9 +4640,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 ; + }
[Severity: High]
Is the omission of RTL_VER_17_QFN100 here intentional?
RTL_VER_17_QFN68 and RTL_VER_17_QFN100 are the two package variants selected by __rtl_get_pkg_det() (0x1e/0x1c versus 0x18/0x1a), and rtl_ops_init() gives them the same ops and the same EEE capabilities:
drivers/net/usb/r8152.c:rtl_ops_init() {
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;
ops->init = r8159_init;
...
ops->autosuspend_en = rtl8157_runtime_enable;
}Both also map to FIRMWARE_8159_1. With the switch above, a QFN100 device takes the default arm on both the suspend and the resume side, so r8157_ups_en() is never called for it: UPS_EN | USP_PREWAKE | PHASE2_EN and UPS_FORCE_PWR_DOWN are never programmed, USB_UPS_FLAGS is never written, and the PCUT_STATUS recovery is never run on resume. Does the commit subject "support UPS for RTL8157 and RTL8159" then only cover one of the two RTL8159 packages?
The same two-version lists appear in r8156_ups_flags(), both in the new EEE switch and in the new lite-mode switch:
switch (tp->version) {
case RTL_VER_16:
case RTL_VER_17_QFN68:
break;
default:
switch (tp->ups_info.lite_mode) {If RTL_VER_17_QFN100 is added to rtl8157_runtime_enable() later without also updating r8156_ups_flags(), it would fall into the default arms and be programmed with the legacy UPS_FLAGS_EN_EEE bit plus the lite-mode bits, which is the encoding this patch declares wrong for this family. Every other chip-family switch in this driver lists all three versions together (rtl_disable(), r8153_set_rx_early_timeout(), r8153_set_rx_early_size(), rtl_reset_bmu(), r8156_fc_parameter() and the max_mtu switch), so the two-version lists here stand out.
drivers/net/usb/r8152.c static void rtl8157_runtime_enable(struct r8152 *tp, bool enable) · line 4671
Quoted 13 lines
drivers/net/usb/r8152.c +10
-0}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 ; + } +
[Severity: Medium]
Should UPS be exited before the PLA wake and WOL registers are reprogrammed? The other two UPS-capable chips do it in the opposite order:
drivers/net/usb/r8152.c:rtl8153b_runtime_enable() {
} else {
r8153b_ups_en(tp, false);
r8153_queue_wake(tp, false);
rtl_runtime_suspend_enable(tp, false);
...
}rtl8153c_runtime_enable() begins its resume path with r8153c_ups_en(tp, false) as well.
As written, r8153_queue_wake(tp, false) and rtl_runtime_suspend_enable(tp, false) write PLA_INDICATE_FALG, PLA_SUSPEND_FLAG, PLA_EXTRA_STATUS and the CRWECR-protected PLA_CONFIG34/WOL registers while USB_POWER_CUT UPS_EN | USP_PREWAKE and USB_MISC_2 UPS_FORCE_PWR_DOWN are still set.
Can those PLA writes be dropped or overwritten while the part is still held in UPS force-power-down? If they are, the device stays armed with __rtl_set_wol(WAKE_ANY) and LINK_OFF_WAKE_EN while tp->saved_wolopts makes the driver believe the user's WOL setting was restored.
A related effect of this order: when PCUT_STATUS is set, the hw_phy_cfg() and rtl8152_set_speed() calls inside r8157_ups_en() now run after the wake/WOL restore rather than before it, which is the reverse of what the 8153b/8153c paths do.
Quoted 3 lines
drivers/net/usb/r8152.c +0
-0r8157_u2p3en (tp, true);if (tp->udev->speed >=USB_SPEED_SUPER )r8153b_u1u2en (tp, true);
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260903072852.58695-1-nic_swsd%40realtek.com