1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved. 4 */ 5 6 #include <linux/signal.h> 7 #include <linux/slab.h> 8 #include <linux/module.h> 9 #include <linux/netdevice.h> 10 #include <linux/etherdevice.h> 11 #include <linux/mii.h> 12 #include <linux/ethtool.h> 13 #include <linux/hex.h> 14 #include <linux/phy.h> 15 #include <linux/usb.h> 16 #include <linux/crc32.h> 17 #include <linux/if_vlan.h> 18 #include <linux/uaccess.h> 19 #include <linux/list.h> 20 #include <linux/ip.h> 21 #include <linux/ipv6.h> 22 #include <net/ip6_checksum.h> 23 #include <uapi/linux/mdio.h> 24 #include <linux/mdio.h> 25 #include <linux/usb/cdc.h> 26 #include <linux/suspend.h> 27 #include <linux/atomic.h> 28 #include <linux/acpi.h> 29 #include <linux/firmware.h> 30 #include <crypto/sha2.h> 31 #include <linux/usb/r8152.h> 32 #include <net/gso.h> 33 34 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" 35 #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters" 36 #define MODULENAME "r8152" 37 38 #define R8152_PHY_ID 32 39 40 #define PLA_IDR 0xc000 41 #define PLA_RCR 0xc010 42 #define PLA_RCR1 0xc012 43 #define PLA_RMS 0xc016 44 #define PLA_RXFIFO_CTRL0 0xc0a0 45 #define PLA_RXFIFO_FULL 0xc0a2 46 #define PLA_RXFIFO_CTRL1 0xc0a4 47 #define PLA_RX_FIFO_FULL 0xc0a6 48 #define PLA_RXFIFO_CTRL2 0xc0a8 49 #define PLA_RX_FIFO_EMPTY 0xc0aa 50 #define PLA_DMY_REG0 0xc0b0 51 #define PLA_FMC 0xc0b4 52 #define PLA_CFG_WOL 0xc0b6 53 #define PLA_TEREDO_CFG 0xc0bc 54 #define PLA_TEREDO_WAKE_BASE 0xc0c4 55 #define PLA_MAR 0xcd00 56 #define PLA_BACKUP 0xd000 57 #define PLA_BDC_CR 0xd1a0 58 #define PLA_TEREDO_TIMER 0xd2cc 59 #define PLA_REALWOW_TIMER 0xd2e8 60 #define PLA_UPHY_TIMER 0xd388 61 #define PLA_SUSPEND_FLAG 0xd38a 62 #define PLA_INDICATE_FALG 0xd38c 63 #define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */ 64 #define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */ 65 #define PLA_EXTRA_STATUS 0xd398 66 #define PLA_GPHY_CTRL 0xd3ae 67 #define PLA_POL_GPIO_CTRL 0xdc6a 68 #define PLA_EFUSE_DATA 0xdd00 69 #define PLA_EFUSE_CMD 0xdd02 70 #define PLA_LEDSEL 0xdd90 71 #define PLA_LED_FEATURE 0xdd92 72 #define PLA_PHYAR 0xde00 73 #define PLA_BOOT_CTRL 0xe004 74 #define PLA_LWAKE_CTRL_REG 0xe007 75 #define PLA_GPHY_INTR_IMR 0xe022 76 #define PLA_EEE_CR 0xe040 77 #define PLA_EEE_TXTWSYS 0xe04c 78 #define PLA_EEE_TXTWSYS_2P5G 0xe058 79 #define PLA_EEEP_CR 0xe080 80 #define PLA_MAC_PWR_CTRL 0xe0c0 81 #define PLA_MAC_PWR_CTRL2 0xe0ca 82 #define PLA_MAC_PWR_CTRL3 0xe0cc 83 #define PLA_MAC_PWR_CTRL4 0xe0ce 84 #define PLA_WDT6_CTRL 0xe428 85 #define PLA_TCR0 0xe610 86 #define PLA_TCR1 0xe612 87 #define PLA_MTPS 0xe615 88 #define PLA_TXFIFO_CTRL 0xe618 89 #define PLA_TXFIFO_FULL 0xe61a 90 #define PLA_RSTTALLY 0xe800 91 #define PLA_CR 0xe813 92 #define PLA_CRWECR 0xe81c 93 #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */ 94 #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */ 95 #define PLA_CONFIG5 0xe822 96 #define PLA_PHY_PWR 0xe84c 97 #define PLA_OOB_CTRL 0xe84f 98 #define PLA_CPCR 0xe854 99 #define PLA_MISC_0 0xe858 100 #define PLA_MISC_1 0xe85a 101 #define PLA_OCP_GPHY_BASE 0xe86c 102 #define PLA_TALLYCNT 0xe890 103 #define PLA_SFF_STS_7 0xe8de 104 #define PLA_PHYSTATUS 0xe908 105 #define PLA_CONFIG6 0xe90a /* CONFIG6 */ 106 #define PLA_USB_CFG 0xe952 107 #define PLA_BP_BA 0xfc26 108 #define PLA_BP_0 0xfc28 109 #define PLA_BP_1 0xfc2a 110 #define PLA_BP_2 0xfc2c 111 #define PLA_BP_3 0xfc2e 112 #define PLA_BP_4 0xfc30 113 #define PLA_BP_5 0xfc32 114 #define PLA_BP_6 0xfc34 115 #define PLA_BP_7 0xfc36 116 #define PLA_BP_EN 0xfc38 117 118 #define USB_USB2PHY 0xb41e 119 #define USB_SSPHYLINK1 0xb426 120 #define USB_SSPHYLINK2 0xb428 121 #define USB_L1_CTRL 0xb45e 122 #define USB_U2P3_CTRL 0xb460 123 #define USB_CSR_DUMMY1 0xb464 124 #define USB_CSR_DUMMY2 0xb466 125 #define USB_DEV_STAT 0xb808 126 #define USB_U2P3_V2_CTRL 0xc2c0 127 #define USB_CONNECT_TIMER 0xcbf8 128 #define USB_MSC_TIMER 0xcbfc 129 #define USB_BURST_SIZE 0xcfc0 130 #define USB_FW_FIX_EN0 0xcfca 131 #define USB_FW_FIX_EN1 0xcfcc 132 #define USB_LPM_CONFIG 0xcfd8 133 #define USB_ECM_OPTION 0xcfee 134 #define USB_CSTMR 0xcfef /* RTL8153A */ 135 #define USB_MISC_2 0xcfff 136 #define USB_ECM_OP 0xd26b 137 #define USB_GPHY_CTRL 0xd284 138 #define USB_SPEED_OPTION 0xd32a 139 #define USB_FW_CTRL 0xd334 /* RTL8153B */ 140 #define USB_FC_TIMER 0xd340 141 #define USB_USB_CTRL 0xd406 142 #define USB_PHY_CTRL 0xd408 143 #define USB_TX_AGG 0xd40a 144 #define USB_RX_BUF_TH 0xd40c 145 #define USB_USB_TIMER 0xd428 146 #define USB_RX_EARLY_TIMEOUT 0xd42c 147 #define USB_RX_EARLY_SIZE 0xd42e 148 #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */ 149 #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */ 150 #define USB_TX_DMA 0xd434 151 #define USB_UPT_RXDMA_OWN 0xd437 152 #define USB_UPHY3_MDCMDIO 0xd480 153 #define USB_TOLERANCE 0xd490 154 #define USB_LPM_CTRL 0xd41a 155 #define USB_BMU_RESET 0xd4b0 156 #define USB_BMU_CONFIG 0xd4b4 157 #define USB_U1U2_TIMER 0xd4da 158 #define USB_FW_TASK 0xd4e8 /* RTL8153B */ 159 #define USB_RX_AGGR_NUM 0xd4ee 160 #define USB_ADV_ADDR 0xd5d6 161 #define USB_ADV_DATA 0xd5d8 162 #define USB_ADV_CMD 0xd5dc 163 #define USB_UPS_CTRL 0xd800 164 #define USB_POWER_CUT 0xd80a 165 #define USB_MISC_0 0xd81a 166 #define USB_MISC_1 0xd81f 167 #define USB_AFE_CTRL2 0xd824 168 #define USB_UPHY_XTAL 0xd826 169 #define USB_UPS_CFG 0xd842 170 #define USB_UPS_FLAGS 0xd848 171 #define USB_WDT1_CTRL 0xe404 172 #define USB_WDT11_CTRL 0xe43c 173 #define USB_BP_BA PLA_BP_BA 174 #define USB_BP_0 PLA_BP_0 175 #define USB_BP_1 PLA_BP_1 176 #define USB_BP_2 PLA_BP_2 177 #define USB_BP_3 PLA_BP_3 178 #define USB_BP_4 PLA_BP_4 179 #define USB_BP_5 PLA_BP_5 180 #define USB_BP_6 PLA_BP_6 181 #define USB_BP_7 PLA_BP_7 182 #define USB_BP_EN PLA_BP_EN /* RTL8153A */ 183 #define USB_BP_8 0xfc38 /* RTL8153B */ 184 #define USB_BP_9 0xfc3a 185 #define USB_BP_10 0xfc3c 186 #define USB_BP_11 0xfc3e 187 #define USB_BP_12 0xfc40 188 #define USB_BP_13 0xfc42 189 #define USB_BP_14 0xfc44 190 #define USB_BP_15 0xfc46 191 #define USB_BP2_EN 0xfc48 192 193 /* OCP Registers */ 194 #define OCP_ALDPS_CONFIG 0x2010 195 #define OCP_EEE_CONFIG1 0x2080 196 #define OCP_EEE_CONFIG2 0x2092 197 #define OCP_EEE_CONFIG3 0x2094 198 #define OCP_BASE_MII 0xa400 199 #define OCP_EEE_AR 0xa41a 200 #define OCP_EEE_DATA 0xa41c 201 #define OCP_PHY_STATUS 0xa420 202 #define OCP_INTR_EN 0xa424 203 #define OCP_NCTL_CFG 0xa42c 204 #define OCP_POWER_CFG 0xa430 205 #define OCP_EEE_CFG 0xa432 206 #define OCP_SRAM_ADDR 0xa436 207 #define OCP_SRAM_DATA 0xa438 208 #define OCP_DOWN_SPEED 0xa442 209 #define OCP_EEE_ABLE 0xa5c4 210 #define OCP_EEE_ADV 0xa5d0 211 #define OCP_EEE_LPABLE 0xa5d2 212 #define OCP_10GBT_CTRL 0xa5d4 213 #define OCP_10GBT_STAT 0xa5d6 214 #define OCP_EEE_LPABLE2 0xa6d0 215 #define OCP_EEE_ADV2 0xa6d4 216 #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */ 217 #define OCP_PHY_PATCH_STAT 0xb800 218 #define OCP_PHY_PATCH_CMD 0xb820 219 #define OCP_PHY_LOCK 0xb82e 220 #define OCP_SRAM2_ADDR 0xb87c 221 #define OCP_SRAM2_DATA 0xb87e 222 #define OCP_ADC_IOFFSET 0xbcfc 223 #define OCP_ADC_CFG 0xbc06 224 #define OCP_SYSCLK_CFG 0xc416 225 226 /* SRAM Register */ 227 #define SRAM_GREEN_CFG 0x8011 228 #define SRAM_LPF_CFG 0x8012 229 #define SRAM_GPHY_FW_VER 0x801e 230 #define SRAM_10M_AMP1 0x8080 231 #define SRAM_10M_AMP2 0x8082 232 #define SRAM_IMPEDANCE 0x8084 233 #define SRAM_PHY_LOCK 0xb82e 234 235 /* PLA_RCR */ 236 #define RCR_AAP 0x00000001 237 #define RCR_APM 0x00000002 238 #define RCR_AM 0x00000004 239 #define RCR_AB 0x00000008 240 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB) 241 #define SLOT_EN BIT(11) 242 243 /* PLA_RCR1 */ 244 #define OUTER_VLAN BIT(7) 245 #define INNER_VLAN BIT(6) 246 247 /* PLA_RXFIFO_CTRL0 */ 248 #define RXFIFO_THR1_NORMAL 0x00080002 249 #define RXFIFO_THR1_OOB 0x01800003 250 251 /* PLA_RXFIFO_FULL */ 252 #define RXFIFO_FULL_MASK 0xfff 253 254 /* PLA_RXFIFO_CTRL1 */ 255 #define RXFIFO_THR2_FULL 0x00000060 256 #define RXFIFO_THR2_HIGH 0x00000038 257 #define RXFIFO_THR2_OOB 0x0000004a 258 #define RXFIFO_THR2_NORMAL 0x00a0 259 260 /* PLA_RXFIFO_CTRL2 */ 261 #define RXFIFO_THR3_FULL 0x00000078 262 #define RXFIFO_THR3_HIGH 0x00000048 263 #define RXFIFO_THR3_OOB 0x0000005a 264 #define RXFIFO_THR3_NORMAL 0x0110 265 266 /* PLA_TXFIFO_CTRL */ 267 #define TXFIFO_THR_NORMAL 0x00400008 268 #define TXFIFO_THR_NORMAL2 0x01000008 269 270 /* PLA_DMY_REG0 */ 271 #define ECM_ALDPS 0x0002 272 273 /* PLA_FMC */ 274 #define FMC_FCR_MCU_EN 0x0001 275 276 /* PLA_EEEP_CR */ 277 #define EEEP_CR_EEEP_TX 0x0002 278 279 /* PLA_WDT6_CTRL */ 280 #define WDT6_SET_MODE 0x0010 281 282 /* PLA_TCR0 */ 283 #define TCR0_TX_EMPTY 0x0800 284 #define TCR0_AUTO_FIFO 0x0080 285 286 /* PLA_TCR1 */ 287 #define VERSION_MASK 0x7cf0 288 #define IFG_MASK (BIT(3) | BIT(9) | BIT(8)) 289 #define IFG_144NS BIT(9) 290 #define IFG_96NS (BIT(9) | BIT(8)) 291 292 /* PLA_MTPS */ 293 #define MTPS_JUMBO (12 * 1024 / 64) 294 #define MTPS_DEFAULT (6 * 1024 / 64) 295 296 /* PLA_RSTTALLY */ 297 #define TALLY_RESET 0x0001 298 299 /* PLA_CR */ 300 #define CR_RST 0x10 301 #define CR_RE 0x08 302 #define CR_TE 0x04 303 304 /* PLA_CRWECR */ 305 #define CRWECR_NORAML 0x00 306 #define CRWECR_CONFIG 0xc0 307 308 /* PLA_OOB_CTRL */ 309 #define NOW_IS_OOB 0x80 310 #define TXFIFO_EMPTY 0x20 311 #define RXFIFO_EMPTY 0x10 312 #define LINK_LIST_READY 0x02 313 #define DIS_MCU_CLROOB 0x01 314 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY) 315 316 /* PLA_MISC_1 */ 317 #define RXDY_GATED_EN 0x0008 318 319 /* PLA_SFF_STS_7 */ 320 #define RE_INIT_LL 0x8000 321 #define MCU_BORW_EN 0x4000 322 323 /* PLA_CPCR */ 324 #define FLOW_CTRL_EN BIT(0) 325 #define CPCR_RX_VLAN 0x0040 326 327 /* PLA_CFG_WOL */ 328 #define MAGIC_EN 0x0001 329 330 /* PLA_TEREDO_CFG */ 331 #define TEREDO_SEL 0x8000 332 #define TEREDO_WAKE_MASK 0x7f00 333 #define TEREDO_RS_EVENT_MASK 0x00fe 334 #define OOB_TEREDO_EN 0x0001 335 336 /* PLA_BDC_CR */ 337 #define ALDPS_PROXY_MODE 0x0001 338 339 /* PLA_EFUSE_CMD */ 340 #define EFUSE_READ_CMD BIT(15) 341 #define EFUSE_DATA_BIT16 BIT(7) 342 343 /* PLA_CONFIG34 */ 344 #define LINK_ON_WAKE_EN 0x0010 345 #define LINK_OFF_WAKE_EN 0x0008 346 347 /* PLA_CONFIG6 */ 348 #define LANWAKE_CLR_EN BIT(0) 349 350 /* PLA_USB_CFG */ 351 #define EN_XG_LIP BIT(1) 352 #define EN_G_LIP BIT(2) 353 354 /* PLA_CONFIG5 */ 355 #define BWF_EN 0x0040 356 #define MWF_EN 0x0020 357 #define UWF_EN 0x0010 358 #define LAN_WAKE_EN 0x0002 359 360 /* PLA_LED_FEATURE */ 361 #define LED_MODE_MASK 0x0700 362 363 /* PLA_PHY_PWR */ 364 #define TX_10M_IDLE_EN 0x0080 365 #define PFM_PWM_SWITCH 0x0040 366 #define TEST_IO_OFF BIT(4) 367 368 /* PLA_MAC_PWR_CTRL */ 369 #define D3_CLK_GATED_EN 0x00004000 370 #define MCU_CLK_RATIO 0x07010f07 371 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f 372 #define ALDPS_SPDWN_RATIO 0x0f87 373 374 /* PLA_MAC_PWR_CTRL2 */ 375 #define EEE_SPDWN_RATIO 0x8007 376 #define MAC_CLK_SPDWN_EN BIT(15) 377 #define EEE_SPDWN_RATIO_MASK 0xff 378 379 /* PLA_MAC_PWR_CTRL3 */ 380 #define PLA_MCU_SPDWN_EN BIT(14) 381 #define PKT_AVAIL_SPDWN_EN 0x0100 382 #define SUSPEND_SPDWN_EN 0x0004 383 #define U1U2_SPDWN_EN 0x0002 384 #define L1_SPDWN_EN 0x0001 385 386 /* PLA_MAC_PWR_CTRL4 */ 387 #define PWRSAVE_SPDWN_EN 0x1000 388 #define RXDV_SPDWN_EN 0x0800 389 #define TX10MIDLE_EN 0x0100 390 #define IDLE_SPDWN_EN BIT(6) 391 #define TP100_SPDWN_EN 0x0020 392 #define TP500_SPDWN_EN 0x0010 393 #define TP1000_SPDWN_EN 0x0008 394 #define EEE_SPDWN_EN 0x0001 395 396 /* PLA_GPHY_INTR_IMR */ 397 #define GPHY_STS_MSK 0x0001 398 #define SPEED_DOWN_MSK 0x0002 399 #define SPDWN_RXDV_MSK 0x0004 400 #define SPDWN_LINKCHG_MSK 0x0008 401 402 /* PLA_PHYAR */ 403 #define PHYAR_FLAG 0x80000000 404 405 /* PLA_EEE_CR */ 406 #define EEE_RX_EN 0x0001 407 #define EEE_TX_EN 0x0002 408 409 /* PLA_BOOT_CTRL */ 410 #define AUTOLOAD_DONE 0x0002 411 412 /* PLA_LWAKE_CTRL_REG */ 413 #define LANWAKE_PIN BIT(7) 414 415 /* PLA_SUSPEND_FLAG */ 416 #define LINK_CHG_EVENT BIT(0) 417 418 /* PLA_INDICATE_FALG */ 419 #define UPCOMING_RUNTIME_D3 BIT(0) 420 421 /* PLA_MACDBG_PRE and PLA_MACDBG_POST */ 422 #define DEBUG_OE BIT(0) 423 #define DEBUG_LTSSM 0x0082 424 425 /* PLA_EXTRA_STATUS */ 426 #define CUR_LINK_OK BIT(15) 427 #define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */ 428 #define LINK_CHANGE_FLAG BIT(8) 429 #define POLL_LINK_CHG BIT(0) 430 431 /* PLA_GPHY_CTRL */ 432 #define GPHY_FLASH BIT(1) 433 434 /* PLA_POL_GPIO_CTRL */ 435 #define DACK_DET_EN BIT(15) 436 #define POL_GPHY_PATCH BIT(4) 437 438 /* USB_USB2PHY */ 439 #define USB2PHY_SUSPEND 0x0001 440 #define USB2PHY_L1 0x0002 441 442 /* USB_SSPHYLINK1 */ 443 #define DELAY_PHY_PWR_CHG BIT(1) 444 445 /* USB_SSPHYLINK2 */ 446 #define pwd_dn_scale_mask 0x3ffe 447 #define pwd_dn_scale(x) ((x) << 1) 448 449 /* USB_CSR_DUMMY1 */ 450 #define DYNAMIC_BURST 0x0001 451 452 /* USB_CSR_DUMMY2 */ 453 #define EP4_FULL_FC 0x0001 454 455 /* USB_DEV_STAT */ 456 #define STAT_SPEED_MASK 0x0006 457 #define STAT_SPEED_HIGH 0x0000 458 #define STAT_SPEED_FULL 0x0002 459 460 /* USB_FW_FIX_EN0 */ 461 #define FW_FIX_SUSPEND BIT(14) 462 463 /* USB_FW_FIX_EN1 */ 464 #define FW_IP_RESET_EN BIT(9) 465 466 /* USB_LPM_CONFIG */ 467 #define LPM_U1U2_EN BIT(0) 468 469 /* USB_TX_AGG */ 470 #define TX_AGG_MAX_THRESHOLD 0x03 471 472 /* USB_RX_BUF_TH */ 473 #define RX_THR_SUPPER 0x0c350180 474 #define RX_THR_HIGH 0x7a120180 475 #define RX_THR_SLOW 0xffff0180 476 #define RX_THR_B 0x00010001 477 478 /* USB_TX_DMA */ 479 #define TEST_MODE_DISABLE 0x00000001 480 #define TX_SIZE_ADJUST1 0x00000100 481 482 /* USB_BMU_RESET */ 483 #define BMU_RESET_EP_IN 0x01 484 #define BMU_RESET_EP_OUT 0x02 485 486 /* USB_BMU_CONFIG */ 487 #define ACT_ODMA BIT(1) 488 489 /* USB_UPT_RXDMA_OWN */ 490 #define OWN_UPDATE BIT(0) 491 #define OWN_CLEAR BIT(1) 492 493 /* USB_FW_TASK */ 494 #define FC_PATCH_TASK BIT(1) 495 496 /* USB_RX_AGGR_NUM */ 497 #define RX_AGGR_NUM_MASK 0x1ff 498 499 /* USB_ADV_CMD */ 500 #define ADV_CMD_BMU 0 501 #define ADV_CMD_BUSY BIT(0) 502 #define ADV_CMD_WR BIT(1) 503 #define ADV_CMD_IP BIT(2) 504 505 /* USB_UPS_CTRL */ 506 #define POWER_CUT 0x0100 507 508 /* USB_PM_CTRL_STATUS */ 509 #define RESUME_INDICATE 0x0001 510 511 /* USB_ECM_OPTION */ 512 #define BYPASS_MAC_RESET BIT(5) 513 514 /* USB_CSTMR */ 515 #define FORCE_SUPER BIT(0) 516 517 /* USB_MISC_2 */ 518 #define UPS_FORCE_PWR_DOWN BIT(0) 519 520 /* USB_ECM_OP */ 521 #define EN_ALL_SPEED BIT(0) 522 523 /* USB_GPHY_CTRL */ 524 #define GPHY_PATCH_DONE BIT(2) 525 #define BYPASS_FLASH BIT(5) 526 #define BACKUP_RESTRORE BIT(6) 527 528 /* USB_SPEED_OPTION */ 529 #define RG_PWRDN_EN BIT(8) 530 #define ALL_SPEED_OFF BIT(9) 531 532 /* USB_FW_CTRL */ 533 #define FLOW_CTRL_PATCH_OPT BIT(1) 534 #define AUTO_SPEEDUP BIT(3) 535 #define FLOW_CTRL_PATCH_2 BIT(8) 536 537 /* USB_FC_TIMER */ 538 #define CTRL_TIMER_EN BIT(15) 539 540 /* USB_USB_CTRL */ 541 #define CDC_ECM_EN BIT(3) 542 #define RX_AGG_DISABLE 0x0010 543 #define RX_ZERO_EN 0x0080 544 #define RX_DESC_16B 0x0400 545 546 /* USB_U2P3_CTRL */ 547 #define U2P3_ENABLE 0x0001 548 #define RX_DETECT8 BIT(3) 549 550 /* USB_U2P3_V2_CTRL */ 551 #define U2P3_V2_ENABLE BIT(29) 552 553 /* USB_POWER_CUT */ 554 #define PWR_EN 0x0001 555 #define PHASE2_EN 0x0008 556 #define UPS_EN BIT(4) 557 #define USP_PREWAKE BIT(5) 558 559 /* USB_MISC_0 */ 560 #define PCUT_STATUS 0x0001 561 562 /* USB_RX_EARLY_TIMEOUT */ 563 #define COALESCE_SUPER 85000U 564 #define COALESCE_HIGH 250000U 565 #define COALESCE_SLOW 524280U 566 567 /* USB_WDT1_CTRL */ 568 #define WTD1_EN BIT(0) 569 570 /* USB_WDT11_CTRL */ 571 #define TIMER11_EN 0x0001 572 573 /* USB_LPM_CTRL */ 574 /* bit 4 ~ 5: fifo empty boundary */ 575 #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */ 576 /* bit 2 ~ 3: LMP timer */ 577 #define LPM_TIMER_MASK 0x0c 578 #define LPM_TIMER_500MS 0x04 /* 500 ms */ 579 #define LPM_TIMER_500US 0x0c /* 500 us */ 580 #define ROK_EXIT_LPM 0x02 581 582 /* USB_AFE_CTRL2 */ 583 #define SEN_VAL_MASK 0xf800 584 #define SEN_VAL_NORMAL 0xa000 585 #define SEL_RXIDLE 0x0100 586 587 /* USB_UPHY_XTAL */ 588 #define OOBS_POLLING BIT(8) 589 590 /* USB_UPS_CFG */ 591 #define SAW_CNT_1MS_MASK 0x0fff 592 #define MID_REVERSE BIT(5) /* RTL8156A */ 593 594 /* USB_UPS_FLAGS */ 595 #define UPS_FLAGS_R_TUNE BIT(0) 596 #define UPS_FLAGS_EN_10M_CKDIV BIT(1) 597 #define UPS_FLAGS_250M_CKDIV BIT(2) 598 #define UPS_FLAGS_EN_ALDPS BIT(3) 599 #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4) 600 #define UPS_FLAGS_SPEED_MASK (0xf << 16) 601 #define ups_flags_speed(x) ((x) << 16) 602 #define UPS_FLAGS_EN_EEE BIT(20) 603 #define UPS_FLAGS_EN_500M_EEE BIT(21) 604 #define UPS_FLAGS_EN_EEE_CKDIV BIT(22) 605 #define UPS_FLAGS_EEE_PLLOFF_100 BIT(23) 606 #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24) 607 #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25) 608 #define UPS_FLAGS_EN_GREEN BIT(26) 609 #define UPS_FLAGS_EN_FLOW_CTR BIT(27) 610 611 enum spd_duplex { 612 NWAY_10M_HALF, 613 NWAY_10M_FULL, 614 NWAY_100M_HALF, 615 NWAY_100M_FULL, 616 NWAY_1000M_FULL, 617 FORCE_10M_HALF, 618 FORCE_10M_FULL, 619 FORCE_100M_HALF, 620 FORCE_100M_FULL, 621 FORCE_1000M_FULL, 622 NWAY_2500M_FULL, 623 NWAY_5000M_FULL, 624 }; 625 626 /* OCP_ALDPS_CONFIG */ 627 #define ENPWRSAVE 0x8000 628 #define ENPDNPS 0x0200 629 #define LINKENA 0x0100 630 #define DIS_SDSAVE 0x0010 631 632 /* OCP_PHY_STATUS */ 633 #define PHY_STAT_MASK 0x0007 634 #define PHY_STAT_EXT_INIT 2 635 #define PHY_STAT_LAN_ON 3 636 #define PHY_STAT_PWRDN 5 637 638 /* OCP_INTR_EN */ 639 #define INTR_SPEED_FORCE BIT(3) 640 641 /* OCP_NCTL_CFG */ 642 #define PGA_RETURN_EN BIT(1) 643 644 /* OCP_POWER_CFG */ 645 #define EEE_CLKDIV_EN 0x8000 646 #define EN_ALDPS 0x0004 647 #define EN_10M_PLLOFF 0x0001 648 649 /* OCP_EEE_CONFIG1 */ 650 #define RG_TXLPI_MSK_HFDUP 0x8000 651 #define RG_MATCLR_EN 0x4000 652 #define EEE_10_CAP 0x2000 653 #define EEE_NWAY_EN 0x1000 654 #define TX_QUIET_EN 0x0200 655 #define RX_QUIET_EN 0x0100 656 #define sd_rise_time_mask 0x0070 657 #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */ 658 #define RG_RXLPI_MSK_HFDUP 0x0008 659 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ 660 661 /* OCP_EEE_CONFIG2 */ 662 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ 663 #define RG_DACQUIET_EN 0x0400 664 #define RG_LDVQUIET_EN 0x0200 665 #define RG_CKRSEL 0x0020 666 #define RG_EEEPRG_EN 0x0010 667 668 /* OCP_EEE_CONFIG3 */ 669 #define fast_snr_mask 0xff80 670 #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */ 671 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ 672 #define MSK_PH 0x0006 /* bit 0 ~ 3 */ 673 674 /* OCP_EEE_AR */ 675 /* bit[15:14] function */ 676 #define FUN_ADDR 0x0000 677 #define FUN_DATA 0x4000 678 /* bit[4:0] device addr */ 679 680 /* OCP_EEE_CFG */ 681 #define CTAP_SHORT_EN 0x0040 682 #define EEE10_EN 0x0010 683 684 /* OCP_DOWN_SPEED */ 685 #define EN_EEE_CMODE BIT(14) 686 #define EN_EEE_1000 BIT(13) 687 #define EN_EEE_100 BIT(12) 688 #define EN_10M_CLKDIV BIT(11) 689 #define EN_10M_BGOFF 0x0080 690 691 /* OCP_10GBT_CTRL */ 692 #define RTL_ADV2_5G_F_R BIT(5) /* Advertise 2.5GBASE-T fast-retrain */ 693 694 /* OCP_PHY_STATE */ 695 #define TXDIS_STATE 0x01 696 #define ABD_STATE 0x02 697 698 /* OCP_PHY_PATCH_STAT */ 699 #define PATCH_READY BIT(6) 700 701 /* OCP_PHY_PATCH_CMD */ 702 #define PATCH_REQUEST BIT(4) 703 704 /* OCP_PHY_LOCK */ 705 #define PATCH_LOCK BIT(0) 706 707 /* OCP_ADC_CFG */ 708 #define CKADSEL_L 0x0100 709 #define ADC_EN 0x0080 710 #define EN_EMI_L 0x0040 711 712 /* OCP_SYSCLK_CFG */ 713 #define sysclk_div_expo(x) (min(x, 5) << 8) 714 #define clk_div_expo(x) (min(x, 5) << 4) 715 716 /* SRAM_GREEN_CFG */ 717 #define GREEN_ETH_EN BIT(15) 718 #define R_TUNE_EN BIT(11) 719 720 /* SRAM_LPF_CFG */ 721 #define LPF_AUTO_TUNE 0x8000 722 723 /* SRAM_10M_AMP1 */ 724 #define GDAC_IB_UPALL 0x0008 725 726 /* SRAM_10M_AMP2 */ 727 #define AMP_DN 0x0200 728 729 /* SRAM_IMPEDANCE */ 730 #define RX_DRIVING_MASK 0x6000 731 732 /* SRAM_PHY_LOCK */ 733 #define PHY_PATCH_LOCK 0x0001 734 735 /* MAC PASSTHRU */ 736 #define AD_MASK 0xfee0 737 #define BND_MASK 0x0004 738 #define BD_MASK 0x0001 739 #define EFUSE 0xcfdb 740 #define PASS_THRU_MASK 0x1 741 742 #define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */ 743 744 enum rtl_register_content { 745 _5000bps = BIT(12), 746 _2500bps = BIT(10), 747 _1250bps = BIT(9), 748 _500bps = BIT(8), 749 _tx_flow = BIT(6), 750 _rx_flow = BIT(5), 751 _1000bps = 0x10, 752 _100bps = 0x08, 753 _10bps = 0x04, 754 LINK_STATUS = 0x02, 755 FULL_DUP = 0x01, 756 }; 757 758 #define is_speed_2500(_speed) (((_speed) & (_2500bps | LINK_STATUS)) == (_2500bps | LINK_STATUS)) 759 #define is_speed_5000(_speed) (((_speed) & (_5000bps | LINK_STATUS)) == (_5000bps | LINK_STATUS)) 760 #define is_flow_control(_speed) (((_speed) & (_tx_flow | _rx_flow)) == (_tx_flow | _rx_flow)) 761 762 #define RTL8152_MAX_TX 4 763 #define RTL8152_MAX_RX 10 764 #define INTBUFSIZE 2 765 766 #define RTL8152_RX_MAX_PENDING 4096 767 #define RTL8152_RXFG_HEADSZ 256 768 769 #define INTR_LINK 0x0004 770 771 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) 772 #define RTL8153_RMS RTL8153_MAX_PACKET 773 #define RTL8152_TX_TIMEOUT (5 * HZ) 774 #define mtu_to_size(m) ((m) + VLAN_ETH_HLEN + ETH_FCS_LEN) 775 #define size_to_mtu(s) ((s) - VLAN_ETH_HLEN - ETH_FCS_LEN) 776 777 /* rtl8152 flags */ 778 enum rtl8152_flags { 779 RTL8152_INACCESSIBLE = 0, 780 RTL8152_SET_RX_MODE, 781 WORK_ENABLE, 782 RTL8152_LINK_CHG, 783 SELECTIVE_SUSPEND, 784 PHY_RESET, 785 SCHEDULE_TASKLET, 786 GREEN_ETHERNET, 787 RX_EPROTO, 788 IN_PRE_RESET, 789 PROBED_WITH_NO_ERRORS, 790 PROBE_SHOULD_RETRY, 791 }; 792 793 #define DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB 0x721e 794 #define DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK 0x3054 795 #define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082 796 #define DEVICE_ID_THINKPAD_USB_C_DONGLE 0x720c 797 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387 798 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3 0x3062 799 #define DEVICE_ID_THINKPAD_HYBRID_USB_C_DOCK 0xa359 800 801 struct tally_counter { 802 __le64 tx_packets; 803 __le64 rx_packets; 804 __le64 tx_errors; 805 __le32 rx_errors; 806 __le16 rx_missed; 807 __le16 align_errors; 808 __le32 tx_one_collision; 809 __le32 tx_multi_collision; 810 __le64 rx_unicast; 811 __le64 rx_broadcast; 812 __le32 rx_multicast; 813 __le16 tx_aborted; 814 __le16 tx_underrun; 815 }; 816 817 struct rx_desc { 818 __le32 opts1; 819 #define RX_LEN_MASK 0x7fff 820 821 __le32 opts2; 822 #define RD_UDP_CS BIT(23) 823 #define RD_TCP_CS BIT(22) 824 #define RD_IPV6_CS BIT(20) 825 #define RD_IPV4_CS BIT(19) 826 827 __le32 opts3; 828 #define IPF BIT(23) /* IP checksum fail */ 829 #define UDPF BIT(22) /* UDP checksum fail */ 830 #define TCPF BIT(21) /* TCP checksum fail */ 831 #define RX_VLAN_TAG BIT(16) 832 833 __le32 opts4; 834 __le32 opts5; 835 __le32 opts6; 836 }; 837 838 struct tx_desc { 839 __le32 opts1; 840 #define TX_FS BIT(31) /* First segment of a packet */ 841 #define TX_LS BIT(30) /* Final segment of a packet */ 842 #define GTSENDV4 BIT(28) 843 #define GTSENDV6 BIT(27) 844 #define GTTCPHO_SHIFT 18 845 #define GTTCPHO_MAX 0x7fU 846 #define TX_LEN_MAX 0x3ffffU 847 848 __le32 opts2; 849 #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */ 850 #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */ 851 #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */ 852 #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */ 853 #define MSS_SHIFT 17 854 #define MSS_MAX 0x7ffU 855 #define TCPHO_SHIFT 17 856 #define TCPHO_MAX 0x7ffU 857 #define TX_VLAN_TAG BIT(16) 858 }; 859 860 struct rx_desc_v2 { 861 __le32 opts1; 862 #define RX_LEN_MASK_2 0xfffe0000 863 #define rx_v2_get_len(x) (((x) & RX_LEN_MASK_2) >> 17) 864 #define RX_VLAN_TAG_2 BIT(3) 865 #define RX_VER_MASK 0x3 866 867 __le32 opts2; 868 869 __le32 opts3; 870 #define IPF_2 BIT(26) /* IP checksum fail */ 871 #define UDPF_2 BIT(25) /* UDP checksum fail */ 872 #define TCPF_2 BIT(24) /* TCP checksum fail */ 873 #define RD_IPV6_CS_2 BIT(15) 874 #define RD_IPV4_CS_2 BIT(14) 875 #define RD_UDP_CS_2 BIT(11) 876 #define RD_TCP_CS_2 BIT(10) 877 878 __le32 opts4; 879 }; 880 881 struct tx_desc_v2 { 882 __le32 opts1; 883 884 __le32 opts2; 885 #define TCPHO_MAX_2 0x3ffU 886 887 __le32 opts3; 888 #define tx_v2_set_len(x) ((x) << 4) 889 890 __le32 opts4; 891 #define TX_SIG (0x15 << 27) 892 }; 893 894 struct r8152; 895 896 struct rx_agg { 897 struct list_head list, info_list; 898 struct urb *urb; 899 struct r8152 *context; 900 struct page *page; 901 void *buffer; 902 }; 903 904 struct tx_agg { 905 struct list_head list; 906 struct urb *urb; 907 struct r8152 *context; 908 void *buffer; 909 void *head; 910 u32 skb_num; 911 u32 skb_len; 912 }; 913 914 struct r8152 { 915 unsigned long flags; 916 struct usb_device *udev; 917 struct napi_struct napi; 918 struct usb_interface *intf; 919 struct net_device *netdev; 920 struct urb *intr_urb; 921 struct tx_agg tx_info[RTL8152_MAX_TX]; 922 struct list_head rx_info, rx_used; 923 struct list_head rx_done, tx_free; 924 struct sk_buff_head tx_queue, rx_queue; 925 spinlock_t rx_lock, tx_lock; 926 struct delayed_work schedule, hw_phy_work; 927 struct mii_if_info mii; 928 struct mutex control; /* use for hw setting */ 929 #ifdef CONFIG_PM_SLEEP 930 struct notifier_block pm_notifier; 931 #endif 932 struct tasklet_struct tx_tl; 933 934 struct rtl_ops { 935 void (*init)(struct r8152 *tp); 936 int (*enable)(struct r8152 *tp); 937 void (*disable)(struct r8152 *tp); 938 void (*up)(struct r8152 *tp); 939 void (*down)(struct r8152 *tp); 940 void (*unload)(struct r8152 *tp); 941 int (*eee_get)(struct r8152 *tp, struct ethtool_keee *eee); 942 int (*eee_set)(struct r8152 *tp, struct ethtool_keee *eee); 943 bool (*in_nway)(struct r8152 *tp); 944 void (*hw_phy_cfg)(struct r8152 *tp); 945 void (*autosuspend_en)(struct r8152 *tp, bool enable); 946 void (*change_mtu)(struct r8152 *tp); 947 } rtl_ops; 948 949 struct ups_info { 950 u32 r_tune:1; 951 u32 _10m_ckdiv:1; 952 u32 _250m_ckdiv:1; 953 u32 aldps:1; 954 u32 lite_mode:2; 955 u32 speed_duplex:4; 956 u32 eee:1; 957 u32 eee_lite:1; 958 u32 eee_ckdiv:1; 959 u32 eee_plloff_100:1; 960 u32 eee_plloff_giga:1; 961 u32 eee_cmod_lv:1; 962 u32 green:1; 963 u32 flow_control:1; 964 u32 ctap_short_off:1; 965 } ups_info; 966 967 struct desc_info { 968 void (*vlan_tag)(void *desc, struct sk_buff *skb); 969 u8 align; 970 u8 size; 971 } rx_desc, tx_desc; 972 973 struct desc_ops { 974 void (*tx_len)(struct r8152 *tp, void *desc, u32 len); 975 u32 (*rx_len)(struct r8152 *tp, void *desc); 976 u8 (*rx_csum)(struct r8152 *tp, void *desc); 977 int (*tx_csum)(struct r8152 *tp, void *desc, struct sk_buff *skb, u32 len); 978 } desc_ops; 979 980 #define RTL_VER_SIZE 32 981 982 struct rtl_fw { 983 const char *fw_name; 984 const struct firmware *fw; 985 986 char version[RTL_VER_SIZE]; 987 int (*pre_fw)(struct r8152 *tp); 988 int (*post_fw)(struct r8152 *tp); 989 990 bool retry; 991 } rtl_fw; 992 993 atomic_t rx_count; 994 995 bool eee_en; 996 int intr_interval; 997 u32 saved_wolopts; 998 u32 msg_enable; 999 u32 tx_qlen; 1000 u32 coalesce; 1001 u32 advertising; 1002 u32 rx_buf_sz; 1003 u32 rx_copybreak; 1004 u32 rx_pending; 1005 u32 fc_pause_on, fc_pause_off; 1006 1007 unsigned int pipe_in, pipe_out, pipe_intr, pipe_ctrl_in, pipe_ctrl_out; 1008 1009 u32 support_2500full:1; 1010 u32 support_5000full:1; 1011 u32 lenovo_macpassthru:1; 1012 u32 dell_tb_rx_agg_bug:1; 1013 u16 ocp_base; 1014 u16 speed; 1015 u16 eee_adv; 1016 u16 eee_adv2; 1017 u8 *intr_buff; 1018 u8 version; 1019 u8 duplex; 1020 u8 autoneg; 1021 1022 unsigned int reg_access_reset_count; 1023 }; 1024 1025 /** 1026 * struct fw_block - block type and total length 1027 * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA, 1028 * RTL_FW_USB and so on. 1029 * @length: total length of the current block. 1030 */ 1031 struct fw_block { 1032 __le32 type; 1033 __le32 length; 1034 } __packed; 1035 1036 /** 1037 * struct fw_header - header of the firmware file 1038 * @checksum: checksum of sha256 which is calculated from the whole file 1039 * except the checksum field of the file. That is, calculate sha256 1040 * from the version field to the end of the file. 1041 * @version: version of this firmware. 1042 * @blocks: the first firmware block of the file 1043 */ 1044 struct fw_header { 1045 u8 checksum[32]; 1046 char version[RTL_VER_SIZE]; 1047 struct fw_block blocks[]; 1048 } __packed; 1049 1050 enum rtl8152_fw_flags { 1051 FW_FLAGS_USB = 0, 1052 FW_FLAGS_PLA, 1053 FW_FLAGS_START, 1054 FW_FLAGS_STOP, 1055 FW_FLAGS_NC, 1056 FW_FLAGS_NC1, 1057 FW_FLAGS_NC2, 1058 FW_FLAGS_UC2, 1059 FW_FLAGS_UC, 1060 FW_FLAGS_SPEED_UP, 1061 FW_FLAGS_VER, 1062 }; 1063 1064 enum rtl8152_fw_fixup_cmd { 1065 FW_FIXUP_AND = 0, 1066 FW_FIXUP_OR, 1067 FW_FIXUP_NOT, 1068 FW_FIXUP_XOR, 1069 }; 1070 1071 struct fw_phy_set { 1072 __le16 addr; 1073 __le16 data; 1074 } __packed; 1075 1076 struct fw_phy_speed_up { 1077 struct fw_block blk_hdr; 1078 __le16 fw_offset; 1079 __le16 version; 1080 __le16 fw_reg; 1081 __le16 reserved; 1082 char info[]; 1083 } __packed; 1084 1085 struct fw_phy_ver { 1086 struct fw_block blk_hdr; 1087 struct fw_phy_set ver; 1088 __le32 reserved; 1089 } __packed; 1090 1091 struct fw_phy_fixup { 1092 struct fw_block blk_hdr; 1093 struct fw_phy_set setting; 1094 __le16 bit_cmd; 1095 __le16 reserved; 1096 } __packed; 1097 1098 struct fw_phy_union { 1099 struct fw_block blk_hdr; 1100 __le16 fw_offset; 1101 __le16 fw_reg; 1102 struct fw_phy_set pre_set[2]; 1103 struct fw_phy_set bp[8]; 1104 struct fw_phy_set bp_en; 1105 u8 pre_num; 1106 u8 bp_num; 1107 char info[]; 1108 } __packed; 1109 1110 /** 1111 * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB. 1112 * The layout of the firmware block is: 1113 * <struct fw_mac> + <info> + <firmware data>. 1114 * @blk_hdr: firmware descriptor (type, length) 1115 * @fw_offset: offset of the firmware binary data. The start address of 1116 * the data would be the address of struct fw_mac + @fw_offset. 1117 * @fw_reg: the register to load the firmware. Depends on chip. 1118 * @bp_ba_addr: the register to write break point base address. Depends on 1119 * chip. 1120 * @bp_ba_value: break point base address. Depends on chip. 1121 * @bp_en_addr: the register to write break point enabled mask. Depends 1122 * on chip. 1123 * @bp_en_value: break point enabled mask. Depends on the firmware. 1124 * @bp_start: the start register of break points. Depends on chip. 1125 * @bp_num: the break point number which needs to be set for this firmware. 1126 * Depends on the firmware. 1127 * @bp: break points. Depends on firmware. 1128 * @reserved: reserved space (unused) 1129 * @fw_ver_reg: the register to store the fw version. 1130 * @fw_ver_data: the firmware version of the current type. 1131 * @info: additional information for debugging, and is followed by the 1132 * binary data of firmware. 1133 */ 1134 struct fw_mac { 1135 struct fw_block blk_hdr; 1136 __le16 fw_offset; 1137 __le16 fw_reg; 1138 __le16 bp_ba_addr; 1139 __le16 bp_ba_value; 1140 __le16 bp_en_addr; 1141 __le16 bp_en_value; 1142 __le16 bp_start; 1143 __le16 bp_num; 1144 __le16 bp[16]; /* any value determined by firmware */ 1145 __le32 reserved; 1146 __le16 fw_ver_reg; 1147 u8 fw_ver_data; 1148 char info[]; 1149 } __packed; 1150 1151 /** 1152 * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START. 1153 * This is used to set patch key when loading the firmware of PHY. 1154 * @blk_hdr: firmware descriptor (type, length) 1155 * @key_reg: the register to write the patch key. 1156 * @key_data: patch key. 1157 * @reserved: reserved space (unused) 1158 */ 1159 struct fw_phy_patch_key { 1160 struct fw_block blk_hdr; 1161 __le16 key_reg; 1162 __le16 key_data; 1163 __le32 reserved; 1164 } __packed; 1165 1166 /** 1167 * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC. 1168 * The layout of the firmware block is: 1169 * <struct fw_phy_nc> + <info> + <firmware data>. 1170 * @blk_hdr: firmware descriptor (type, length) 1171 * @fw_offset: offset of the firmware binary data. The start address of 1172 * the data would be the address of struct fw_phy_nc + @fw_offset. 1173 * @fw_reg: the register to load the firmware. Depends on chip. 1174 * @ba_reg: the register to write the base address. Depends on chip. 1175 * @ba_data: base address. Depends on chip. 1176 * @patch_en_addr: the register of enabling patch mode. Depends on chip. 1177 * @patch_en_value: patch mode enabled mask. Depends on the firmware. 1178 * @mode_reg: the regitster of switching the mode. 1179 * @mode_pre: the mode needing to be set before loading the firmware. 1180 * @mode_post: the mode to be set when finishing to load the firmware. 1181 * @reserved: reserved space (unused) 1182 * @bp_start: the start register of break points. Depends on chip. 1183 * @bp_num: the break point number which needs to be set for this firmware. 1184 * Depends on the firmware. 1185 * @bp: break points. Depends on firmware. 1186 * @info: additional information for debugging, and is followed by the 1187 * binary data of firmware. 1188 */ 1189 struct fw_phy_nc { 1190 struct fw_block blk_hdr; 1191 __le16 fw_offset; 1192 __le16 fw_reg; 1193 __le16 ba_reg; 1194 __le16 ba_data; 1195 __le16 patch_en_addr; 1196 __le16 patch_en_value; 1197 __le16 mode_reg; 1198 __le16 mode_pre; 1199 __le16 mode_post; 1200 __le16 reserved; 1201 __le16 bp_start; 1202 __le16 bp_num; 1203 __le16 bp[4]; 1204 char info[]; 1205 } __packed; 1206 1207 enum rtl_fw_type { 1208 RTL_FW_END = 0, 1209 RTL_FW_PLA, 1210 RTL_FW_USB, 1211 RTL_FW_PHY_START, 1212 RTL_FW_PHY_STOP, 1213 RTL_FW_PHY_NC, 1214 RTL_FW_PHY_FIXUP, 1215 RTL_FW_PHY_UNION_NC, 1216 RTL_FW_PHY_UNION_NC1, 1217 RTL_FW_PHY_UNION_NC2, 1218 RTL_FW_PHY_UNION_UC2, 1219 RTL_FW_PHY_UNION_UC, 1220 RTL_FW_PHY_UNION_MISC, 1221 RTL_FW_PHY_SPEED_UP, 1222 RTL_FW_PHY_VER, 1223 }; 1224 1225 enum rtl_version { 1226 RTL_VER_UNKNOWN = 0, 1227 RTL_VER_01, 1228 RTL_VER_02, 1229 RTL_VER_03, 1230 RTL_VER_04, 1231 RTL_VER_05, 1232 RTL_VER_06, 1233 RTL_VER_07, 1234 RTL_VER_08, 1235 RTL_VER_09, 1236 1237 RTL_TEST_01, 1238 RTL_VER_10, 1239 RTL_VER_11, 1240 RTL_VER_12, 1241 RTL_VER_13, 1242 RTL_VER_14, 1243 RTL_VER_15, 1244 RTL_VER_16, 1245 1246 RTL_VER_MAX 1247 }; 1248 1249 enum tx_csum_stat { 1250 TX_CSUM_SUCCESS = 0, 1251 TX_CSUM_TSO, 1252 TX_CSUM_NONE 1253 }; 1254 1255 #define RTL_ADVERTISED_10_HALF BIT(0) 1256 #define RTL_ADVERTISED_10_FULL BIT(1) 1257 #define RTL_ADVERTISED_100_HALF BIT(2) 1258 #define RTL_ADVERTISED_100_FULL BIT(3) 1259 #define RTL_ADVERTISED_1000_HALF BIT(4) 1260 #define RTL_ADVERTISED_1000_FULL BIT(5) 1261 #define RTL_ADVERTISED_2500_FULL BIT(6) 1262 #define RTL_ADVERTISED_5000_FULL BIT(7) 1263 1264 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). 1265 * The RTL chips use a 64 element hash table based on the Ethernet CRC. 1266 */ 1267 static const int multicast_filter_limit = 32; 1268 static unsigned int agg_buf_sz = 16384; 1269 1270 #define RTL_LIMITED_TSO_SIZE (size_to_mtu(agg_buf_sz) - tp->tx_desc.size) 1271 1272 /* If register access fails then we block access and issue a reset. If this 1273 * happens too many times in a row without a successful access then we stop 1274 * trying to reset and just leave access blocked. 1275 */ 1276 #define REGISTER_ACCESS_MAX_RESETS 3 1277 1278 static void rtl_set_inaccessible(struct r8152 *tp) 1279 { 1280 set_bit(RTL8152_INACCESSIBLE, &tp->flags); 1281 smp_mb__after_atomic(); 1282 } 1283 1284 static void rtl_set_accessible(struct r8152 *tp) 1285 { 1286 clear_bit(RTL8152_INACCESSIBLE, &tp->flags); 1287 smp_mb__after_atomic(); 1288 } 1289 1290 static 1291 int r8152_control_msg(struct r8152 *tp, unsigned int pipe, __u8 request, 1292 __u8 requesttype, __u16 value, __u16 index, void *data, 1293 __u16 size, const char *msg_tag) 1294 { 1295 struct usb_device *udev = tp->udev; 1296 int ret; 1297 1298 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 1299 return -ENODEV; 1300 1301 ret = usb_control_msg(udev, pipe, request, requesttype, 1302 value, index, data, size, 1303 USB_CTRL_GET_TIMEOUT); 1304 1305 /* No need to issue a reset to report an error if the USB device got 1306 * unplugged; just return immediately. 1307 */ 1308 if (ret == -ENODEV) 1309 return ret; 1310 1311 /* If the write was successful then we're done */ 1312 if (ret >= 0) { 1313 tp->reg_access_reset_count = 0; 1314 return ret; 1315 } 1316 1317 dev_err(&udev->dev, 1318 "Failed to %s %d bytes at %#06x/%#06x (%d)\n", 1319 msg_tag, size, value, index, ret); 1320 1321 /* Block all future register access until we reset. Much of the code 1322 * in the driver doesn't check for errors. Notably, many parts of the 1323 * driver do a read/modify/write of a register value without 1324 * confirming that the read succeeded. Writing back modified garbage 1325 * like this can fully wedge the adapter, requiring a power cycle. 1326 */ 1327 rtl_set_inaccessible(tp); 1328 1329 /* If probe hasn't yet finished, then we'll request a retry of the 1330 * whole probe routine if we get any control transfer errors. We 1331 * never have to clear this bit since we free/reallocate the whole "tp" 1332 * structure if we retry probe. 1333 */ 1334 if (!test_bit(PROBED_WITH_NO_ERRORS, &tp->flags)) { 1335 set_bit(PROBE_SHOULD_RETRY, &tp->flags); 1336 return ret; 1337 } 1338 1339 /* Failing to access registers in pre-reset is not surprising since we 1340 * wouldn't be resetting if things were behaving normally. The register 1341 * access we do in pre-reset isn't truly mandatory--we're just reusing 1342 * the disable() function and trying to be nice by powering the 1343 * adapter down before resetting it. Thus, if we're in pre-reset, 1344 * we'll return right away and not try to queue up yet another reset. 1345 * We know the post-reset is already coming. 1346 */ 1347 if (test_bit(IN_PRE_RESET, &tp->flags)) 1348 return ret; 1349 1350 if (tp->reg_access_reset_count < REGISTER_ACCESS_MAX_RESETS) { 1351 usb_queue_reset_device(tp->intf); 1352 tp->reg_access_reset_count++; 1353 } else if (tp->reg_access_reset_count == REGISTER_ACCESS_MAX_RESETS) { 1354 dev_err(&udev->dev, 1355 "Tried to reset %d times; giving up.\n", 1356 REGISTER_ACCESS_MAX_RESETS); 1357 } 1358 1359 return ret; 1360 } 1361 1362 static 1363 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) 1364 { 1365 int ret; 1366 void *tmp; 1367 1368 tmp = kmalloc(size, GFP_KERNEL); 1369 if (!tmp) 1370 return -ENOMEM; 1371 1372 ret = r8152_control_msg(tp, tp->pipe_ctrl_in, 1373 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, 1374 value, index, tmp, size, "read"); 1375 1376 if (ret < 0) 1377 memset(data, 0xff, size); 1378 else 1379 memcpy(data, tmp, size); 1380 1381 kfree(tmp); 1382 1383 return ret; 1384 } 1385 1386 static 1387 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) 1388 { 1389 int ret; 1390 void *tmp; 1391 1392 tmp = kmemdup(data, size, GFP_KERNEL); 1393 if (!tmp) 1394 return -ENOMEM; 1395 1396 ret = r8152_control_msg(tp, tp->pipe_ctrl_out, 1397 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, 1398 value, index, tmp, size, "write"); 1399 1400 kfree(tmp); 1401 1402 return ret; 1403 } 1404 1405 static void rtl_set_unplug(struct r8152 *tp) 1406 { 1407 if (tp->udev->state == USB_STATE_NOTATTACHED) 1408 rtl_set_inaccessible(tp); 1409 } 1410 1411 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, 1412 void *data, u16 type) 1413 { 1414 u16 limit = 64; 1415 int ret = 0; 1416 1417 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 1418 return -ENODEV; 1419 1420 /* both size and indix must be 4 bytes align */ 1421 if ((size & 3) || !size || (index & 3) || !data) 1422 return -EPERM; 1423 1424 if ((u32)index + (u32)size > 0xffff) 1425 return -EPERM; 1426 1427 while (size) { 1428 if (size > limit) { 1429 ret = get_registers(tp, index, type, limit, data); 1430 if (ret < 0) 1431 break; 1432 1433 index += limit; 1434 data += limit; 1435 size -= limit; 1436 } else { 1437 ret = get_registers(tp, index, type, size, data); 1438 if (ret < 0) 1439 break; 1440 1441 index += size; 1442 data += size; 1443 size = 0; 1444 break; 1445 } 1446 } 1447 1448 if (ret == -ENODEV) 1449 rtl_set_unplug(tp); 1450 1451 return ret; 1452 } 1453 1454 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, 1455 u16 size, void *data, u16 type) 1456 { 1457 int ret; 1458 u16 byteen_start, byteen_end, byen; 1459 u16 limit = 512; 1460 1461 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 1462 return -ENODEV; 1463 1464 /* both size and indix must be 4 bytes align */ 1465 if ((size & 3) || !size || (index & 3) || !data) 1466 return -EPERM; 1467 1468 if ((u32)index + (u32)size > 0xffff) 1469 return -EPERM; 1470 1471 byteen_start = byteen & BYTE_EN_START_MASK; 1472 byteen_end = byteen & BYTE_EN_END_MASK; 1473 1474 byen = byteen_start | (byteen_start << 4); 1475 1476 /* Split the first DWORD if the byte_en is not 0xff */ 1477 if (byen != BYTE_EN_DWORD) { 1478 ret = set_registers(tp, index, type | byen, 4, data); 1479 if (ret < 0) 1480 goto error1; 1481 1482 index += 4; 1483 data += 4; 1484 size -= 4; 1485 } 1486 1487 if (size) { 1488 byen = byteen_end | (byteen_end >> 4); 1489 1490 /* Split the last DWORD if the byte_en is not 0xff */ 1491 if (byen != BYTE_EN_DWORD) 1492 size -= 4; 1493 1494 while (size) { 1495 if (size > limit) { 1496 ret = set_registers(tp, index, 1497 type | BYTE_EN_DWORD, 1498 limit, data); 1499 if (ret < 0) 1500 goto error1; 1501 1502 index += limit; 1503 data += limit; 1504 size -= limit; 1505 } else { 1506 ret = set_registers(tp, index, 1507 type | BYTE_EN_DWORD, 1508 size, data); 1509 if (ret < 0) 1510 goto error1; 1511 1512 index += size; 1513 data += size; 1514 size = 0; 1515 break; 1516 } 1517 } 1518 1519 /* Set the last DWORD */ 1520 if (byen != BYTE_EN_DWORD) 1521 ret = set_registers(tp, index, type | byen, 4, data); 1522 } 1523 1524 error1: 1525 if (ret == -ENODEV) 1526 rtl_set_unplug(tp); 1527 1528 return ret; 1529 } 1530 1531 static inline 1532 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) 1533 { 1534 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); 1535 } 1536 1537 static inline 1538 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) 1539 { 1540 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); 1541 } 1542 1543 static inline 1544 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) 1545 { 1546 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); 1547 } 1548 1549 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) 1550 { 1551 __le32 data; 1552 1553 generic_ocp_read(tp, index, sizeof(data), &data, type); 1554 1555 return __le32_to_cpu(data); 1556 } 1557 1558 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) 1559 { 1560 __le32 tmp = __cpu_to_le32(data); 1561 1562 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); 1563 } 1564 1565 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) 1566 { 1567 u32 data; 1568 __le32 tmp; 1569 u16 byen = BYTE_EN_WORD; 1570 u8 shift = index & 2; 1571 1572 index &= ~3; 1573 byen <<= shift; 1574 1575 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen); 1576 1577 data = __le32_to_cpu(tmp); 1578 data >>= (shift * 8); 1579 data &= 0xffff; 1580 1581 return (u16)data; 1582 } 1583 1584 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) 1585 { 1586 u32 mask = 0xffff; 1587 __le32 tmp; 1588 u16 byen = BYTE_EN_WORD; 1589 u8 shift = index & 2; 1590 1591 data &= mask; 1592 1593 if (index & 2) { 1594 byen <<= shift; 1595 mask <<= (shift * 8); 1596 data <<= (shift * 8); 1597 index &= ~3; 1598 } 1599 1600 tmp = __cpu_to_le32(data); 1601 1602 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); 1603 } 1604 1605 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) 1606 { 1607 u32 data; 1608 __le32 tmp; 1609 u8 shift = index & 3; 1610 1611 index &= ~3; 1612 1613 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); 1614 1615 data = __le32_to_cpu(tmp); 1616 data >>= (shift * 8); 1617 data &= 0xff; 1618 1619 return (u8)data; 1620 } 1621 1622 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) 1623 { 1624 u32 mask = 0xff; 1625 __le32 tmp; 1626 u16 byen = BYTE_EN_BYTE; 1627 u8 shift = index & 3; 1628 1629 data &= mask; 1630 1631 if (index & 3) { 1632 byen <<= shift; 1633 mask <<= (shift * 8); 1634 data <<= (shift * 8); 1635 index &= ~3; 1636 } 1637 1638 tmp = __cpu_to_le32(data); 1639 1640 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); 1641 } 1642 1643 static u16 ocp_reg_read(struct r8152 *tp, u16 addr) 1644 { 1645 u16 ocp_base, ocp_index; 1646 1647 ocp_base = addr & 0xf000; 1648 if (ocp_base != tp->ocp_base) { 1649 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); 1650 tp->ocp_base = ocp_base; 1651 } 1652 1653 ocp_index = (addr & 0x0fff) | 0xb000; 1654 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); 1655 } 1656 1657 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) 1658 { 1659 u16 ocp_base, ocp_index; 1660 1661 ocp_base = addr & 0xf000; 1662 if (ocp_base != tp->ocp_base) { 1663 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); 1664 tp->ocp_base = ocp_base; 1665 } 1666 1667 ocp_index = (addr & 0x0fff) | 0xb000; 1668 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); 1669 } 1670 1671 static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value) 1672 { 1673 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); 1674 } 1675 1676 static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) 1677 { 1678 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); 1679 } 1680 1681 static int wait_cmd_ready(struct r8152 *tp, u16 cmd) 1682 { 1683 return poll_timeout_us(u16 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, cmd), 1684 !(ocp_data & ADV_CMD_BUSY), 2000, 20000, false); 1685 } 1686 1687 static int ocp_adv_read(struct r8152 *tp, u16 cmd, u16 addr, u32 *data) 1688 { 1689 int ret; 1690 1691 ret = wait_cmd_ready(tp, USB_ADV_CMD); 1692 if (ret < 0) 1693 goto out; 1694 1695 ocp_write_word(tp, MCU_TYPE_USB, USB_ADV_ADDR, addr); 1696 1697 cmd |= ADV_CMD_BUSY; 1698 ocp_write_word(tp, MCU_TYPE_USB, USB_ADV_CMD, cmd); 1699 1700 ret = wait_cmd_ready(tp, USB_ADV_CMD); 1701 if (ret < 0) 1702 goto out; 1703 1704 *data = ocp_read_dword(tp, MCU_TYPE_USB, USB_ADV_DATA); 1705 1706 out: 1707 return ret; 1708 } 1709 1710 static int ocp_adv_write(struct r8152 *tp, u16 cmd, u16 addr, u32 data) 1711 { 1712 int ret; 1713 1714 ret = wait_cmd_ready(tp, USB_ADV_CMD); 1715 if (ret < 0) 1716 goto out; 1717 1718 cmd |= ADV_CMD_WR; 1719 ocp_write_dword(tp, MCU_TYPE_USB, USB_ADV_DATA, data); 1720 1721 ocp_write_word(tp, MCU_TYPE_USB, USB_ADV_ADDR, addr); 1722 1723 cmd |= ADV_CMD_BUSY; 1724 ocp_write_word(tp, MCU_TYPE_USB, USB_ADV_CMD, cmd); 1725 1726 out: 1727 return ret; 1728 } 1729 1730 static int rtl_bmu_read(struct r8152 *tp, u16 addr, u32 *data) 1731 { 1732 return ocp_adv_read(tp, ADV_CMD_BMU, addr, data); 1733 } 1734 1735 static int rtl_bmu_write(struct r8152 *tp, u16 addr, u32 data) 1736 { 1737 return ocp_adv_write(tp, ADV_CMD_BMU, addr, data); 1738 } 1739 1740 static int rtl_bmu_w0w1(struct r8152 *tp, u16 addr, u32 clear, u32 set) 1741 { 1742 u32 bmu; 1743 int ret; 1744 1745 ret = rtl_bmu_read(tp, addr, &bmu); 1746 if (ret < 0) 1747 goto out; 1748 1749 bmu = (bmu & ~clear) | set; 1750 ret = rtl_bmu_write(tp, addr, bmu); 1751 1752 out: 1753 return ret; 1754 } 1755 1756 static int rtl_bmu_clr_bits(struct r8152 *tp, u16 addr, u32 clear) 1757 { 1758 return rtl_bmu_w0w1(tp, addr, clear, 0); 1759 } 1760 1761 static int rtl_ip_read(struct r8152 *tp, u16 addr, u32 *data) 1762 { 1763 return ocp_adv_read(tp, ADV_CMD_IP, addr, data); 1764 } 1765 1766 static int rtl_ip_write(struct r8152 *tp, u16 addr, u32 data) 1767 { 1768 return ocp_adv_write(tp, ADV_CMD_IP, addr, data); 1769 } 1770 1771 static int rtl_ip_w0w1(struct r8152 *tp, u16 addr, u32 clear, u32 set) 1772 { 1773 int ret; 1774 u32 ip; 1775 1776 ret = rtl_ip_read(tp, addr, &ip); 1777 if (ret < 0) 1778 goto out; 1779 1780 ip = (ip & ~clear) | set; 1781 ret = rtl_ip_write(tp, addr, ip); 1782 1783 out: 1784 return ret; 1785 } 1786 1787 static int rtl_ip_clr_bits(struct r8152 *tp, u16 addr, u32 clear) 1788 { 1789 return rtl_ip_w0w1(tp, addr, clear, 0); 1790 } 1791 1792 static int rtl_ip_set_bits(struct r8152 *tp, u16 addr, u32 set) 1793 { 1794 return rtl_ip_w0w1(tp, addr, 0, set); 1795 } 1796 1797 static void sram_write(struct r8152 *tp, u16 addr, u16 data) 1798 { 1799 ocp_reg_write(tp, OCP_SRAM_ADDR, addr); 1800 ocp_reg_write(tp, OCP_SRAM_DATA, data); 1801 } 1802 1803 static u16 sram_read(struct r8152 *tp, u16 addr) 1804 { 1805 ocp_reg_write(tp, OCP_SRAM_ADDR, addr); 1806 return ocp_reg_read(tp, OCP_SRAM_DATA); 1807 } 1808 1809 static int read_mii_word(struct net_device *netdev, int phy_id, int reg) 1810 { 1811 struct r8152 *tp = netdev_priv(netdev); 1812 int ret; 1813 1814 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 1815 return -ENODEV; 1816 1817 if (phy_id != R8152_PHY_ID) 1818 return -EINVAL; 1819 1820 ret = r8152_mdio_read(tp, reg); 1821 1822 return ret; 1823 } 1824 1825 static 1826 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) 1827 { 1828 struct r8152 *tp = netdev_priv(netdev); 1829 1830 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 1831 return; 1832 1833 if (phy_id != R8152_PHY_ID) 1834 return; 1835 1836 r8152_mdio_write(tp, reg, val); 1837 } 1838 1839 static void 1840 ocp_dword_w0w1(struct r8152 *tp, u16 type, u16 index, u32 clear, u32 set) 1841 { 1842 u32 ocp_data; 1843 1844 ocp_data = ocp_read_dword(tp, type, index); 1845 ocp_data = (ocp_data & ~clear) | set; 1846 ocp_write_dword(tp, type, index, ocp_data); 1847 } 1848 1849 static void 1850 ocp_word_w0w1(struct r8152 *tp, u16 type, u16 index, u16 clear, u16 set) 1851 { 1852 u16 ocp_data; 1853 1854 ocp_data = ocp_read_word(tp, type, index); 1855 ocp_data = (ocp_data & ~clear) | set; 1856 ocp_write_word(tp, type, index, ocp_data); 1857 } 1858 1859 static void 1860 ocp_byte_w0w1(struct r8152 *tp, u16 type, u16 index, u8 clear, u8 set) 1861 { 1862 u8 ocp_data; 1863 1864 ocp_data = ocp_read_byte(tp, type, index); 1865 ocp_data = (ocp_data & ~clear) | set; 1866 ocp_write_byte(tp, type, index, ocp_data); 1867 } 1868 1869 static void ocp_dword_clr_bits(struct r8152 *tp, u16 type, u16 index, u32 clear) 1870 { 1871 ocp_dword_w0w1(tp, type, index, clear, 0); 1872 } 1873 1874 static void ocp_dword_set_bits(struct r8152 *tp, u16 type, u16 index, u32 set) 1875 { 1876 ocp_dword_w0w1(tp, type, index, 0, set); 1877 } 1878 1879 static void ocp_word_clr_bits(struct r8152 *tp, u16 type, u16 index, u16 clear) 1880 { 1881 ocp_word_w0w1(tp, type, index, clear, 0); 1882 } 1883 1884 static void ocp_word_set_bits(struct r8152 *tp, u16 type, u16 index, u16 set) 1885 { 1886 ocp_word_w0w1(tp, type, index, 0, set); 1887 } 1888 1889 static int 1890 ocp_word_test_and_clr_bits(struct r8152 *tp, u16 type, u16 index, u16 clear) 1891 { 1892 u16 ocp_data; 1893 1894 ocp_data = ocp_read_word(tp, type, index); 1895 if (ocp_data & clear) 1896 ocp_write_word(tp, type, index, ocp_data & ~clear); 1897 1898 return ocp_data & clear; 1899 } 1900 1901 static void ocp_byte_clr_bits(struct r8152 *tp, u16 type, u16 index, u8 clear) 1902 { 1903 ocp_byte_w0w1(tp, type, index, clear, 0); 1904 } 1905 1906 static void ocp_byte_set_bits(struct r8152 *tp, u16 type, u16 index, u8 set) 1907 { 1908 ocp_byte_w0w1(tp, type, index, 0, set); 1909 } 1910 1911 static void ocp_reg_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set) 1912 { 1913 u16 data; 1914 1915 data = ocp_reg_read(tp, addr); 1916 data = (data & ~clear) | set; 1917 ocp_reg_write(tp, addr, data); 1918 } 1919 1920 static void ocp_reg_clr_bits(struct r8152 *tp, u16 addr, u16 clear) 1921 { 1922 ocp_reg_w0w1(tp, addr, clear, 0); 1923 } 1924 1925 static void ocp_reg_set_bits(struct r8152 *tp, u16 addr, u16 set) 1926 { 1927 ocp_reg_w0w1(tp, addr, 0, set); 1928 } 1929 1930 static void sram_write_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set) 1931 { 1932 u16 data; 1933 1934 data = sram_read(tp, addr); 1935 data = (data & ~clear) | set; 1936 ocp_reg_write(tp, OCP_SRAM_DATA, data); 1937 } 1938 1939 static void sram_clr_bits(struct r8152 *tp, u16 addr, u16 clear) 1940 { 1941 sram_write_w0w1(tp, addr, clear, 0); 1942 } 1943 1944 static void sram_set_bits(struct r8152 *tp, u16 addr, u16 set) 1945 { 1946 sram_write_w0w1(tp, addr, 0, set); 1947 } 1948 1949 static void sram2_write(struct r8152 *tp, u16 addr, u16 data) 1950 { 1951 ocp_reg_write(tp, OCP_SRAM2_ADDR, addr); 1952 ocp_reg_write(tp, OCP_SRAM2_DATA, data); 1953 } 1954 1955 static u16 sram2_read(struct r8152 *tp, u16 addr) 1956 { 1957 ocp_reg_write(tp, OCP_SRAM2_ADDR, addr); 1958 return ocp_reg_read(tp, OCP_SRAM2_DATA); 1959 } 1960 1961 static void sram2_write_w0w1(struct r8152 *tp, u16 addr, u16 clear, u16 set) 1962 { 1963 u16 data; 1964 1965 data = sram2_read(tp, addr); 1966 data = (data & ~clear) | set; 1967 ocp_reg_write(tp, OCP_SRAM2_DATA, data); 1968 } 1969 1970 static void r8152_mdio_clr_bit(struct r8152 *tp, u16 addr, u16 clear) 1971 { 1972 int data; 1973 1974 data = r8152_mdio_read(tp, addr); 1975 r8152_mdio_write(tp, addr, data & ~clear); 1976 } 1977 1978 static void r8152_mdio_set_bit(struct r8152 *tp, u16 addr, u16 set) 1979 { 1980 int data; 1981 1982 data = r8152_mdio_read(tp, addr); 1983 r8152_mdio_write(tp, addr, data | set); 1984 } 1985 1986 static int r8152_mdio_test_and_clr_bit(struct r8152 *tp, u16 addr, u16 clear) 1987 { 1988 int data; 1989 1990 data = r8152_mdio_read(tp, addr); 1991 if (data & clear) 1992 r8152_mdio_write(tp, addr, data & ~clear); 1993 1994 return data & clear; 1995 } 1996 1997 static int 1998 r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags); 1999 2000 static int 2001 rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex, 2002 u32 advertising); 2003 2004 static int __rtl8152_set_mac_address(struct net_device *netdev, 2005 struct sockaddr_storage *addr, 2006 bool in_resume) 2007 { 2008 struct r8152 *tp = netdev_priv(netdev); 2009 int ret = -EADDRNOTAVAIL; 2010 2011 if (!is_valid_ether_addr(addr->__data)) 2012 goto out1; 2013 2014 if (!in_resume) { 2015 ret = usb_autopm_get_interface(tp->intf); 2016 if (ret < 0) 2017 goto out1; 2018 } 2019 2020 mutex_lock(&tp->control); 2021 2022 eth_hw_addr_set(netdev, addr->__data); 2023 2024 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 2025 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->__data); 2026 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 2027 2028 mutex_unlock(&tp->control); 2029 2030 if (!in_resume) 2031 usb_autopm_put_interface(tp->intf); 2032 out1: 2033 return ret; 2034 } 2035 2036 static int rtl8152_set_mac_address(struct net_device *netdev, void *p) 2037 { 2038 return __rtl8152_set_mac_address(netdev, p, false); 2039 } 2040 2041 /* Devices containing proper chips can support a persistent 2042 * host system provided MAC address. 2043 * Examples of this are Dell TB15 and Dell WD15 docks 2044 */ 2045 static int vendor_mac_passthru_addr_read(struct r8152 *tp, 2046 struct sockaddr_storage *ss) 2047 { 2048 acpi_status status; 2049 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 2050 union acpi_object *obj; 2051 int ret = -EINVAL; 2052 u32 ocp_data; 2053 unsigned char buf[6]; 2054 char *mac_obj_name; 2055 acpi_object_type mac_obj_type; 2056 int mac_strlen; 2057 2058 if (tp->lenovo_macpassthru) { 2059 mac_obj_name = "\\MACA"; 2060 mac_obj_type = ACPI_TYPE_STRING; 2061 mac_strlen = 0x16; 2062 } else { 2063 /* test for -AD variant of RTL8153 */ 2064 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 2065 if ((ocp_data & AD_MASK) == 0x1000) { 2066 /* test for MAC address pass-through bit */ 2067 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE); 2068 if ((ocp_data & PASS_THRU_MASK) != 1) { 2069 netif_dbg(tp, probe, tp->netdev, 2070 "No efuse for RTL8153-AD MAC pass through\n"); 2071 return -ENODEV; 2072 } 2073 } else { 2074 /* test for RTL8153-BND and RTL8153-BD */ 2075 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); 2076 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) { 2077 netif_dbg(tp, probe, tp->netdev, 2078 "Invalid variant for MAC pass through\n"); 2079 return -ENODEV; 2080 } 2081 } 2082 2083 mac_obj_name = "\\_SB.AMAC"; 2084 mac_obj_type = ACPI_TYPE_BUFFER; 2085 mac_strlen = 0x17; 2086 } 2087 2088 /* returns _AUXMAC_#AABBCCDDEEFF# */ 2089 status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer); 2090 obj = (union acpi_object *)buffer.pointer; 2091 if (!ACPI_SUCCESS(status)) 2092 return -ENODEV; 2093 if (obj->type != mac_obj_type || obj->string.length != mac_strlen) { 2094 netif_warn(tp, probe, tp->netdev, 2095 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n", 2096 obj->type, obj->string.length); 2097 goto amacout; 2098 } 2099 2100 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 || 2101 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) { 2102 netif_warn(tp, probe, tp->netdev, 2103 "Invalid header when reading pass-thru MAC addr\n"); 2104 goto amacout; 2105 } 2106 ret = hex2bin(buf, obj->string.pointer + 9, 6); 2107 if (!(ret == 0 && is_valid_ether_addr(buf))) { 2108 netif_warn(tp, probe, tp->netdev, 2109 "Invalid MAC for pass-thru MAC addr: %d, %pM\n", 2110 ret, buf); 2111 ret = -EINVAL; 2112 goto amacout; 2113 } 2114 memcpy(ss->__data, buf, 6); 2115 tp->netdev->addr_assign_type = NET_ADDR_STOLEN; 2116 netif_info(tp, probe, tp->netdev, 2117 "Using pass-thru MAC addr %pM\n", ss->__data); 2118 2119 amacout: 2120 kfree(obj); 2121 return ret; 2122 } 2123 2124 static int determine_ethernet_addr(struct r8152 *tp, 2125 struct sockaddr_storage *ss) 2126 { 2127 struct net_device *dev = tp->netdev; 2128 int ret; 2129 2130 ss->ss_family = dev->type; 2131 2132 ret = eth_platform_get_mac_address(&tp->udev->dev, ss->__data); 2133 if (ret < 0) { 2134 if (tp->version == RTL_VER_01) { 2135 ret = pla_ocp_read(tp, PLA_IDR, 8, ss->__data); 2136 } else { 2137 /* if device doesn't support MAC pass through this will 2138 * be expected to be non-zero 2139 */ 2140 ret = vendor_mac_passthru_addr_read(tp, ss); 2141 if (ret < 0) 2142 ret = pla_ocp_read(tp, PLA_BACKUP, 8, 2143 ss->__data); 2144 } 2145 } 2146 2147 if (ret < 0) { 2148 netif_err(tp, probe, dev, "Get ether addr fail\n"); 2149 } else if (!is_valid_ether_addr(ss->__data)) { 2150 netif_err(tp, probe, dev, "Invalid ether addr %pM\n", 2151 ss->__data); 2152 eth_hw_addr_random(dev); 2153 ether_addr_copy(ss->__data, dev->dev_addr); 2154 netif_info(tp, probe, dev, "Random ether addr %pM\n", 2155 ss->__data); 2156 return 0; 2157 } 2158 2159 return ret; 2160 } 2161 2162 static int set_ethernet_addr(struct r8152 *tp, bool in_resume) 2163 { 2164 struct net_device *dev = tp->netdev; 2165 struct sockaddr_storage ss; 2166 int ret; 2167 2168 ret = determine_ethernet_addr(tp, &ss); 2169 if (ret < 0) 2170 return ret; 2171 2172 if (tp->version == RTL_VER_01) 2173 eth_hw_addr_set(dev, ss.__data); 2174 else 2175 ret = __rtl8152_set_mac_address(dev, &ss, in_resume); 2176 2177 return ret; 2178 } 2179 2180 static void read_bulk_callback(struct urb *urb) 2181 { 2182 struct net_device *netdev; 2183 int status = urb->status; 2184 struct rx_agg *agg; 2185 struct r8152 *tp; 2186 unsigned long flags; 2187 2188 agg = urb->context; 2189 if (!agg) 2190 return; 2191 2192 tp = agg->context; 2193 if (!tp) 2194 return; 2195 2196 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 2197 return; 2198 2199 if (!test_bit(WORK_ENABLE, &tp->flags)) 2200 return; 2201 2202 netdev = tp->netdev; 2203 2204 /* When link down, the driver would cancel all bulks. */ 2205 /* This avoid the re-submitting bulk */ 2206 if (!netif_carrier_ok(netdev)) 2207 return; 2208 2209 usb_mark_last_busy(tp->udev); 2210 2211 switch (status) { 2212 case 0: 2213 if (urb->actual_length < ETH_ZLEN) 2214 break; 2215 2216 spin_lock_irqsave(&tp->rx_lock, flags); 2217 list_add_tail(&agg->list, &tp->rx_done); 2218 spin_unlock_irqrestore(&tp->rx_lock, flags); 2219 napi_schedule(&tp->napi); 2220 return; 2221 case -ESHUTDOWN: 2222 rtl_set_unplug(tp); 2223 netif_device_detach(tp->netdev); 2224 return; 2225 case -EPROTO: 2226 urb->actual_length = 0; 2227 spin_lock_irqsave(&tp->rx_lock, flags); 2228 list_add_tail(&agg->list, &tp->rx_done); 2229 spin_unlock_irqrestore(&tp->rx_lock, flags); 2230 set_bit(RX_EPROTO, &tp->flags); 2231 schedule_delayed_work(&tp->schedule, 1); 2232 return; 2233 case -ENOENT: 2234 return; /* the urb is in unlink state */ 2235 case -ETIME: 2236 if (net_ratelimit()) 2237 netdev_warn(netdev, "maybe reset is needed?\n"); 2238 break; 2239 default: 2240 if (net_ratelimit()) 2241 netdev_warn(netdev, "Rx status %d\n", status); 2242 break; 2243 } 2244 2245 r8152_submit_rx(tp, agg, GFP_ATOMIC); 2246 } 2247 2248 static void write_bulk_callback(struct urb *urb) 2249 { 2250 struct net_device_stats *stats; 2251 struct net_device *netdev; 2252 struct tx_agg *agg; 2253 struct r8152 *tp; 2254 unsigned long flags; 2255 int status = urb->status; 2256 2257 agg = urb->context; 2258 if (!agg) 2259 return; 2260 2261 tp = agg->context; 2262 if (!tp) 2263 return; 2264 2265 netdev = tp->netdev; 2266 stats = &netdev->stats; 2267 if (status) { 2268 if (net_ratelimit()) 2269 netdev_warn(netdev, "Tx status %d\n", status); 2270 stats->tx_errors += agg->skb_num; 2271 } else { 2272 stats->tx_packets += agg->skb_num; 2273 stats->tx_bytes += agg->skb_len; 2274 } 2275 2276 spin_lock_irqsave(&tp->tx_lock, flags); 2277 list_add_tail(&agg->list, &tp->tx_free); 2278 spin_unlock_irqrestore(&tp->tx_lock, flags); 2279 2280 usb_autopm_put_interface_async(tp->intf); 2281 2282 if (!netif_carrier_ok(netdev)) 2283 return; 2284 2285 if (!test_bit(WORK_ENABLE, &tp->flags)) 2286 return; 2287 2288 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 2289 return; 2290 2291 if (!skb_queue_empty(&tp->tx_queue)) 2292 tasklet_schedule(&tp->tx_tl); 2293 } 2294 2295 static void intr_callback(struct urb *urb) 2296 { 2297 struct r8152 *tp; 2298 __le16 *d; 2299 int status = urb->status; 2300 int res; 2301 2302 tp = urb->context; 2303 if (!tp) 2304 return; 2305 2306 if (!test_bit(WORK_ENABLE, &tp->flags)) 2307 return; 2308 2309 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 2310 return; 2311 2312 switch (status) { 2313 case 0: /* success */ 2314 break; 2315 case -ECONNRESET: /* unlink */ 2316 case -ESHUTDOWN: 2317 netif_device_detach(tp->netdev); 2318 fallthrough; 2319 case -ENOENT: 2320 case -EPROTO: 2321 netif_info(tp, intr, tp->netdev, 2322 "Stop submitting intr, status %d\n", status); 2323 return; 2324 case -EOVERFLOW: 2325 if (net_ratelimit()) 2326 netif_info(tp, intr, tp->netdev, 2327 "intr status -EOVERFLOW\n"); 2328 goto resubmit; 2329 /* -EPIPE: should clear the halt */ 2330 default: 2331 netif_info(tp, intr, tp->netdev, "intr status %d\n", status); 2332 goto resubmit; 2333 } 2334 2335 d = urb->transfer_buffer; 2336 if (INTR_LINK & __le16_to_cpu(d[0])) { 2337 if (!netif_carrier_ok(tp->netdev)) { 2338 set_bit(RTL8152_LINK_CHG, &tp->flags); 2339 schedule_delayed_work(&tp->schedule, 0); 2340 } 2341 } else { 2342 if (netif_carrier_ok(tp->netdev)) { 2343 netif_stop_queue(tp->netdev); 2344 set_bit(RTL8152_LINK_CHG, &tp->flags); 2345 schedule_delayed_work(&tp->schedule, 0); 2346 } 2347 } 2348 2349 resubmit: 2350 res = usb_submit_urb(urb, GFP_ATOMIC); 2351 if (res == -ENODEV) { 2352 rtl_set_unplug(tp); 2353 netif_device_detach(tp->netdev); 2354 } else if (res) { 2355 netif_err(tp, intr, tp->netdev, 2356 "can't resubmit intr, status %d\n", res); 2357 } 2358 } 2359 2360 static void *rx_agg_align(struct r8152 *tp, void *data) 2361 { 2362 return (void *)ALIGN((uintptr_t)data, tp->rx_desc.align); 2363 } 2364 2365 static void *tx_agg_align(struct r8152 *tp, void *data) 2366 { 2367 return (void *)ALIGN((uintptr_t)data, tp->tx_desc.align); 2368 } 2369 2370 static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg) 2371 { 2372 list_del(&agg->info_list); 2373 2374 usb_free_urb(agg->urb); 2375 put_page(agg->page); 2376 kfree(agg); 2377 2378 atomic_dec(&tp->rx_count); 2379 } 2380 2381 static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags) 2382 { 2383 struct net_device *netdev = tp->netdev; 2384 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; 2385 unsigned int order = get_order(tp->rx_buf_sz); 2386 struct rx_agg *rx_agg; 2387 unsigned long flags; 2388 2389 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node); 2390 if (!rx_agg) 2391 return NULL; 2392 2393 rx_agg->page = alloc_pages(mflags | __GFP_COMP | __GFP_NOWARN, order); 2394 if (!rx_agg->page) 2395 goto free_rx; 2396 2397 rx_agg->buffer = page_address(rx_agg->page); 2398 2399 rx_agg->urb = usb_alloc_urb(0, mflags); 2400 if (!rx_agg->urb) 2401 goto free_buf; 2402 2403 rx_agg->context = tp; 2404 2405 INIT_LIST_HEAD(&rx_agg->list); 2406 INIT_LIST_HEAD(&rx_agg->info_list); 2407 spin_lock_irqsave(&tp->rx_lock, flags); 2408 list_add_tail(&rx_agg->info_list, &tp->rx_info); 2409 spin_unlock_irqrestore(&tp->rx_lock, flags); 2410 2411 atomic_inc(&tp->rx_count); 2412 2413 return rx_agg; 2414 2415 free_buf: 2416 __free_pages(rx_agg->page, order); 2417 free_rx: 2418 kfree(rx_agg); 2419 return NULL; 2420 } 2421 2422 static void free_all_mem(struct r8152 *tp) 2423 { 2424 struct rx_agg *agg, *agg_next; 2425 unsigned long flags; 2426 int i; 2427 2428 spin_lock_irqsave(&tp->rx_lock, flags); 2429 2430 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list) 2431 free_rx_agg(tp, agg); 2432 2433 spin_unlock_irqrestore(&tp->rx_lock, flags); 2434 2435 WARN_ON(atomic_read(&tp->rx_count)); 2436 2437 for (i = 0; i < RTL8152_MAX_TX; i++) { 2438 usb_free_urb(tp->tx_info[i].urb); 2439 tp->tx_info[i].urb = NULL; 2440 2441 kfree(tp->tx_info[i].buffer); 2442 tp->tx_info[i].buffer = NULL; 2443 tp->tx_info[i].head = NULL; 2444 } 2445 2446 usb_free_urb(tp->intr_urb); 2447 tp->intr_urb = NULL; 2448 2449 kfree(tp->intr_buff); 2450 tp->intr_buff = NULL; 2451 } 2452 2453 static int alloc_all_mem(struct r8152 *tp) 2454 { 2455 struct net_device *netdev = tp->netdev; 2456 struct usb_interface *intf = tp->intf; 2457 struct usb_host_interface *alt = intf->cur_altsetting; 2458 struct usb_host_endpoint *ep_intr = alt->endpoint + 2; 2459 int node, i; 2460 2461 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; 2462 2463 spin_lock_init(&tp->rx_lock); 2464 spin_lock_init(&tp->tx_lock); 2465 INIT_LIST_HEAD(&tp->rx_info); 2466 INIT_LIST_HEAD(&tp->tx_free); 2467 INIT_LIST_HEAD(&tp->rx_done); 2468 skb_queue_head_init(&tp->tx_queue); 2469 skb_queue_head_init(&tp->rx_queue); 2470 atomic_set(&tp->rx_count, 0); 2471 2472 for (i = 0; i < RTL8152_MAX_RX; i++) { 2473 if (!alloc_rx_agg(tp, GFP_KERNEL)) 2474 goto err1; 2475 } 2476 2477 for (i = 0; i < RTL8152_MAX_TX; i++) { 2478 struct urb *urb; 2479 u8 *buf; 2480 2481 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node); 2482 if (!buf) 2483 goto err1; 2484 2485 if (buf != tx_agg_align(tp, buf)) { 2486 kfree(buf); 2487 buf = kmalloc_node(agg_buf_sz + tp->tx_desc.align, GFP_KERNEL, 2488 node); 2489 if (!buf) 2490 goto err1; 2491 } 2492 2493 urb = usb_alloc_urb(0, GFP_KERNEL); 2494 if (!urb) { 2495 kfree(buf); 2496 goto err1; 2497 } 2498 2499 INIT_LIST_HEAD(&tp->tx_info[i].list); 2500 tp->tx_info[i].context = tp; 2501 tp->tx_info[i].urb = urb; 2502 tp->tx_info[i].buffer = buf; 2503 tp->tx_info[i].head = tx_agg_align(tp, buf); 2504 2505 list_add_tail(&tp->tx_info[i].list, &tp->tx_free); 2506 } 2507 2508 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL); 2509 if (!tp->intr_urb) 2510 goto err1; 2511 2512 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); 2513 if (!tp->intr_buff) 2514 goto err1; 2515 2516 tp->intr_interval = (int)ep_intr->desc.bInterval; 2517 usb_fill_int_urb(tp->intr_urb, tp->udev, tp->pipe_intr, 2518 tp->intr_buff, INTBUFSIZE, intr_callback, 2519 tp, tp->intr_interval); 2520 2521 return 0; 2522 2523 err1: 2524 free_all_mem(tp); 2525 return -ENOMEM; 2526 } 2527 2528 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp) 2529 { 2530 struct tx_agg *agg = NULL; 2531 unsigned long flags; 2532 2533 if (list_empty(&tp->tx_free)) 2534 return NULL; 2535 2536 spin_lock_irqsave(&tp->tx_lock, flags); 2537 if (!list_empty(&tp->tx_free)) { 2538 struct list_head *cursor; 2539 2540 cursor = tp->tx_free.next; 2541 list_del_init(cursor); 2542 agg = list_entry(cursor, struct tx_agg, list); 2543 } 2544 spin_unlock_irqrestore(&tp->tx_lock, flags); 2545 2546 return agg; 2547 } 2548 2549 /* r8152_csum_workaround() 2550 * The hw limits the value of the transport offset. When the offset is out of 2551 * range, calculate the checksum by sw. 2552 */ 2553 static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb, 2554 struct sk_buff_head *list) 2555 { 2556 if (skb_shinfo(skb)->gso_size) { 2557 netdev_features_t features = tp->netdev->features; 2558 struct sk_buff *segs, *seg, *next; 2559 struct sk_buff_head seg_list; 2560 2561 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6); 2562 segs = skb_gso_segment(skb, features); 2563 if (IS_ERR(segs) || !segs) 2564 goto drop; 2565 2566 __skb_queue_head_init(&seg_list); 2567 2568 skb_list_walk_safe(segs, seg, next) { 2569 skb_mark_not_on_list(seg); 2570 __skb_queue_tail(&seg_list, seg); 2571 } 2572 2573 skb_queue_splice(&seg_list, list); 2574 dev_kfree_skb(skb); 2575 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 2576 if (skb_checksum_help(skb) < 0) 2577 goto drop; 2578 2579 __skb_queue_head(list, skb); 2580 } else { 2581 struct net_device_stats *stats; 2582 2583 drop: 2584 stats = &tp->netdev->stats; 2585 stats->tx_dropped++; 2586 dev_kfree_skb(skb); 2587 } 2588 } 2589 2590 static void r8152_tx_len(struct r8152 *tp, void *tx_desc, u32 len) 2591 { 2592 struct tx_desc *desc = tx_desc; 2593 2594 desc->opts1 |= cpu_to_le32(len); 2595 } 2596 2597 static void r8152_tx_vlan_tag(void *d, struct sk_buff *skb) 2598 { 2599 struct tx_desc *desc = d; 2600 2601 if (skb_vlan_tag_present(skb)) { 2602 u32 opts2; 2603 2604 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb)); 2605 desc->opts2 |= cpu_to_le32(opts2); 2606 } 2607 } 2608 2609 static void r8152_rx_vlan_tag(void *d, struct sk_buff *skb) 2610 { 2611 struct rx_desc *desc = d; 2612 2613 u32 opts2 = le32_to_cpu(desc->opts2); 2614 2615 if (opts2 & RX_VLAN_TAG) 2616 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 2617 swab16(opts2 & 0xffff)); 2618 } 2619 2620 static int r8152_tx_csum(struct r8152 *tp, void *d, 2621 struct sk_buff *skb, u32 len) 2622 { 2623 u32 mss = skb_shinfo(skb)->gso_size; 2624 struct tx_desc *desc = d; 2625 u32 opts1, opts2 = 0; 2626 int ret = TX_CSUM_SUCCESS; 2627 2628 WARN_ON_ONCE(len > TX_LEN_MAX); 2629 2630 opts1 = len | TX_FS | TX_LS; 2631 2632 if (mss) { 2633 u32 transport_offset = (u32)skb_transport_offset(skb); 2634 2635 if (transport_offset > GTTCPHO_MAX) { 2636 netif_warn(tp, tx_err, tp->netdev, 2637 "Invalid transport offset 0x%x for TSO\n", 2638 transport_offset); 2639 ret = TX_CSUM_TSO; 2640 goto unavailable; 2641 } 2642 2643 switch (vlan_get_protocol(skb)) { 2644 case htons(ETH_P_IP): 2645 opts1 |= GTSENDV4; 2646 break; 2647 2648 case htons(ETH_P_IPV6): 2649 if (skb_cow_head(skb, 0)) { 2650 ret = TX_CSUM_TSO; 2651 goto unavailable; 2652 } 2653 tcp_v6_gso_csum_prep(skb); 2654 opts1 |= GTSENDV6; 2655 break; 2656 2657 default: 2658 WARN_ON_ONCE(1); 2659 break; 2660 } 2661 2662 opts1 |= transport_offset << GTTCPHO_SHIFT; 2663 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT; 2664 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 2665 u32 transport_offset = (u32)skb_transport_offset(skb); 2666 u8 ip_protocol; 2667 2668 if (transport_offset > TCPHO_MAX) { 2669 netif_warn(tp, tx_err, tp->netdev, 2670 "Invalid transport offset 0x%x\n", 2671 transport_offset); 2672 ret = TX_CSUM_NONE; 2673 goto unavailable; 2674 } 2675 2676 switch (vlan_get_protocol(skb)) { 2677 case htons(ETH_P_IP): 2678 opts2 |= IPV4_CS; 2679 ip_protocol = ip_hdr(skb)->protocol; 2680 break; 2681 2682 case htons(ETH_P_IPV6): 2683 opts2 |= IPV6_CS; 2684 ip_protocol = ipv6_hdr(skb)->nexthdr; 2685 break; 2686 2687 default: 2688 ip_protocol = IPPROTO_RAW; 2689 break; 2690 } 2691 2692 if (ip_protocol == IPPROTO_TCP) 2693 opts2 |= TCP_CS; 2694 else if (ip_protocol == IPPROTO_UDP) 2695 opts2 |= UDP_CS; 2696 else 2697 WARN_ON_ONCE(1); 2698 2699 opts2 |= transport_offset << TCPHO_SHIFT; 2700 } 2701 2702 desc->opts2 = cpu_to_le32(opts2); 2703 desc->opts1 = cpu_to_le32(opts1); 2704 2705 unavailable: 2706 return ret; 2707 } 2708 2709 static u32 r8152_rx_len(struct r8152 *tp, void *d) 2710 { 2711 struct rx_desc *desc = d; 2712 2713 return le32_to_cpu(desc->opts1) & RX_LEN_MASK; 2714 } 2715 2716 static u32 r8157_rx_len(struct r8152 *tp, void *d) 2717 { 2718 struct rx_desc_v2 *desc = d; 2719 2720 return rx_v2_get_len(le32_to_cpu(desc->opts1)); 2721 } 2722 2723 static void r8157_rx_vlan_tag(void *desc, struct sk_buff *skb) 2724 { 2725 struct rx_desc_v2 *d = desc; 2726 u32 opts1; 2727 2728 opts1 = le32_to_cpu(d->opts1); 2729 if (opts1 & RX_VLAN_TAG_2) { 2730 u32 opts2 = le32_to_cpu(d->opts2); 2731 2732 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 2733 swab16((opts2 >> 16) & 0xffff)); 2734 } 2735 } 2736 2737 static int r8157_tx_csum(struct r8152 *tp, void *tx_desc, struct sk_buff *skb, u32 len) 2738 { 2739 u32 mss = skb_shinfo(skb)->gso_size; 2740 2741 if (!mss && skb->ip_summed == CHECKSUM_PARTIAL) { 2742 u32 transport_offset = (u32)skb_transport_offset(skb); 2743 2744 if (transport_offset > TCPHO_MAX_2) { 2745 netif_warn(tp, tx_err, tp->netdev, 2746 "Invalid transport offset 0x%x\n", 2747 transport_offset); 2748 return TX_CSUM_NONE; 2749 } 2750 } 2751 2752 return r8152_tx_csum(tp, tx_desc, skb, len); 2753 } 2754 2755 static void r8157_tx_len(struct r8152 *tp, void *tx_desc, u32 len) 2756 { 2757 struct tx_desc_v2 *desc = tx_desc; 2758 2759 desc->opts3 = cpu_to_le32(tx_v2_set_len(len)); 2760 desc->opts4 = cpu_to_le32(TX_SIG); 2761 } 2762 2763 static int rtl_tx_csum(struct r8152 *tp, void *desc, struct sk_buff *skb, 2764 u32 len) 2765 { 2766 int ret = TX_CSUM_SUCCESS; 2767 2768 WARN_ON_ONCE(len > TX_LEN_MAX); 2769 2770 ret = tp->desc_ops.tx_csum(tp, desc, skb, len); 2771 if (!ret) 2772 tp->desc_ops.tx_len(tp, desc, len); 2773 2774 return ret; 2775 } 2776 2777 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg) 2778 { 2779 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; 2780 int remain, ret; 2781 u8 *tx_data; 2782 2783 __skb_queue_head_init(&skb_head); 2784 spin_lock(&tx_queue->lock); 2785 skb_queue_splice_init(tx_queue, &skb_head); 2786 spin_unlock(&tx_queue->lock); 2787 2788 tx_data = agg->head; 2789 agg->skb_num = 0; 2790 agg->skb_len = 0; 2791 remain = agg_buf_sz; 2792 2793 while (remain >= ETH_ZLEN + tp->tx_desc.size) { 2794 struct sk_buff *skb; 2795 unsigned int len; 2796 void *tx_desc; 2797 2798 skb = __skb_dequeue(&skb_head); 2799 if (!skb) 2800 break; 2801 2802 len = skb->len + tp->tx_desc.size; 2803 2804 if (len > remain) { 2805 __skb_queue_head(&skb_head, skb); 2806 break; 2807 } 2808 2809 tx_data = tx_agg_align(tp, tx_data); 2810 tx_desc = (void *)tx_data; 2811 2812 if (rtl_tx_csum(tp, tx_desc, skb, skb->len)) { 2813 r8152_csum_workaround(tp, skb, &skb_head); 2814 continue; 2815 } 2816 2817 tp->tx_desc.vlan_tag(tx_desc, skb); 2818 2819 tx_data += tp->tx_desc.size; 2820 2821 len = skb->len; 2822 if (skb_copy_bits(skb, 0, tx_data, len) < 0) { 2823 struct net_device_stats *stats = &tp->netdev->stats; 2824 2825 stats->tx_dropped++; 2826 dev_kfree_skb_any(skb); 2827 tx_data -= tp->tx_desc.size; 2828 continue; 2829 } 2830 2831 tx_data += len; 2832 agg->skb_len += len; 2833 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1; 2834 2835 dev_kfree_skb_any(skb); 2836 2837 remain = agg_buf_sz - (int)(tx_agg_align(tp, tx_data) - agg->head); 2838 2839 if (tp->dell_tb_rx_agg_bug) 2840 break; 2841 } 2842 2843 if (!skb_queue_empty(&skb_head)) { 2844 spin_lock(&tx_queue->lock); 2845 skb_queue_splice(&skb_head, tx_queue); 2846 spin_unlock(&tx_queue->lock); 2847 } 2848 2849 netif_tx_lock(tp->netdev); 2850 2851 if (netif_queue_stopped(tp->netdev) && 2852 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) 2853 netif_wake_queue(tp->netdev); 2854 2855 netif_tx_unlock(tp->netdev); 2856 2857 ret = usb_autopm_get_interface_async(tp->intf); 2858 if (ret < 0) 2859 goto out_tx_fill; 2860 2861 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_out, 2862 agg->head, (int)(tx_data - (u8 *)agg->head), 2863 (usb_complete_t)write_bulk_callback, agg); 2864 2865 ret = usb_submit_urb(agg->urb, GFP_ATOMIC); 2866 if (ret < 0) 2867 usb_autopm_put_interface_async(tp->intf); 2868 else 2869 netif_trans_update(tp->netdev); 2870 2871 out_tx_fill: 2872 return ret; 2873 } 2874 2875 static u8 r8152_rx_csum(struct r8152 *tp, void *d) 2876 { 2877 struct rx_desc *rx_desc = d; 2878 u8 checksum = CHECKSUM_NONE; 2879 u32 opts2, opts3; 2880 2881 if (!(tp->netdev->features & NETIF_F_RXCSUM)) 2882 goto return_result; 2883 2884 opts2 = le32_to_cpu(rx_desc->opts2); 2885 opts3 = le32_to_cpu(rx_desc->opts3); 2886 2887 if (opts2 & RD_IPV4_CS) { 2888 if (opts3 & IPF) 2889 checksum = CHECKSUM_NONE; 2890 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) 2891 checksum = CHECKSUM_UNNECESSARY; 2892 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) 2893 checksum = CHECKSUM_UNNECESSARY; 2894 } else if (opts2 & RD_IPV6_CS) { 2895 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) 2896 checksum = CHECKSUM_UNNECESSARY; 2897 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) 2898 checksum = CHECKSUM_UNNECESSARY; 2899 } 2900 2901 return_result: 2902 return checksum; 2903 } 2904 2905 static u8 r8157_rx_csum(struct r8152 *tp, void *desc) 2906 { 2907 struct rx_desc_v2 *d = desc; 2908 u8 checksum = CHECKSUM_NONE; 2909 u32 opts3; 2910 2911 if (!(tp->netdev->features & NETIF_F_RXCSUM)) 2912 goto return_result; 2913 2914 opts3 = le32_to_cpu(d->opts3); 2915 2916 if ((opts3 & (RD_IPV4_CS_2 | IPF_2)) == (RD_IPV4_CS_2 | IPF_2)) { 2917 checksum = CHECKSUM_NONE; 2918 } else if (opts3 & (RD_IPV4_CS_2 | RD_IPV6_CS_2)) { 2919 if ((opts3 & (RD_UDP_CS_2 | UDPF_2)) == RD_UDP_CS_2) 2920 checksum = CHECKSUM_UNNECESSARY; 2921 else if ((opts3 & (RD_TCP_CS_2 | TCPF_2)) == RD_TCP_CS_2) 2922 checksum = CHECKSUM_UNNECESSARY; 2923 } 2924 2925 return_result: 2926 return checksum; 2927 } 2928 2929 static inline bool rx_count_exceed(struct r8152 *tp) 2930 { 2931 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX; 2932 } 2933 2934 static inline int agg_offset(struct rx_agg *agg, void *addr) 2935 { 2936 return (int)(addr - agg->buffer); 2937 } 2938 2939 static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags) 2940 { 2941 struct rx_agg *agg, *agg_next, *agg_free = NULL; 2942 unsigned long flags; 2943 2944 spin_lock_irqsave(&tp->rx_lock, flags); 2945 2946 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) { 2947 if (page_count(agg->page) == 1) { 2948 if (!agg_free) { 2949 list_del_init(&agg->list); 2950 agg_free = agg; 2951 continue; 2952 } 2953 if (rx_count_exceed(tp)) { 2954 list_del_init(&agg->list); 2955 free_rx_agg(tp, agg); 2956 } 2957 break; 2958 } 2959 } 2960 2961 spin_unlock_irqrestore(&tp->rx_lock, flags); 2962 2963 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending) 2964 agg_free = alloc_rx_agg(tp, mflags); 2965 2966 return agg_free; 2967 } 2968 2969 static int rx_bottom(struct r8152 *tp, int budget) 2970 { 2971 unsigned long flags; 2972 struct list_head *cursor, *next, rx_queue; 2973 int ret = 0, work_done = 0; 2974 struct napi_struct *napi = &tp->napi; 2975 2976 if (!skb_queue_empty(&tp->rx_queue)) { 2977 while (work_done < budget) { 2978 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue); 2979 struct net_device *netdev = tp->netdev; 2980 struct net_device_stats *stats = &netdev->stats; 2981 unsigned int pkt_len; 2982 2983 if (!skb) 2984 break; 2985 2986 pkt_len = skb->len; 2987 napi_gro_receive(napi, skb); 2988 work_done++; 2989 stats->rx_packets++; 2990 stats->rx_bytes += pkt_len; 2991 } 2992 } 2993 2994 if (list_empty(&tp->rx_done) || work_done >= budget) 2995 goto out1; 2996 2997 clear_bit(RX_EPROTO, &tp->flags); 2998 INIT_LIST_HEAD(&rx_queue); 2999 spin_lock_irqsave(&tp->rx_lock, flags); 3000 list_splice_init(&tp->rx_done, &rx_queue); 3001 spin_unlock_irqrestore(&tp->rx_lock, flags); 3002 3003 list_for_each_safe(cursor, next, &rx_queue) { 3004 struct rx_agg *agg, *agg_free; 3005 int len_used = 0; 3006 struct urb *urb; 3007 void *rx_desc; 3008 u8 *rx_data; 3009 3010 /* A bulk transfer of USB may contain may packets, so the 3011 * total packets may more than the budget. Deal with all 3012 * packets in current bulk transfer, and stop to handle the 3013 * next bulk transfer until next schedule, if budget is 3014 * exhausted. 3015 */ 3016 if (work_done >= budget) 3017 break; 3018 3019 list_del_init(cursor); 3020 3021 agg = list_entry(cursor, struct rx_agg, list); 3022 urb = agg->urb; 3023 if (urb->status != 0 || urb->actual_length < ETH_ZLEN) 3024 goto submit; 3025 3026 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC); 3027 3028 rx_desc = agg->buffer; 3029 rx_data = agg->buffer; 3030 len_used += tp->rx_desc.size; 3031 3032 while (urb->actual_length > len_used) { 3033 struct net_device *netdev = tp->netdev; 3034 struct net_device_stats *stats = &netdev->stats; 3035 unsigned int pkt_len, rx_frag_head_sz, len; 3036 struct sk_buff *skb; 3037 bool use_frags; 3038 3039 WARN_ON_ONCE(skb_queue_len(&tp->rx_queue) >= 1000); 3040 3041 pkt_len = tp->desc_ops.rx_len(tp, rx_desc); 3042 if (pkt_len < ETH_ZLEN) 3043 break; 3044 3045 len_used += pkt_len; 3046 if (urb->actual_length < len_used) 3047 break; 3048 3049 pkt_len -= ETH_FCS_LEN; 3050 len = pkt_len; 3051 rx_data += tp->rx_desc.size; 3052 3053 if (!agg_free || tp->rx_copybreak > len) 3054 use_frags = false; 3055 else 3056 use_frags = true; 3057 3058 if (use_frags) { 3059 /* If the budget is exhausted, the packet 3060 * would be queued in the driver. That is, 3061 * napi_gro_frags() wouldn't be called, so 3062 * we couldn't use napi_get_frags(). 3063 */ 3064 if (work_done >= budget) { 3065 rx_frag_head_sz = tp->rx_copybreak; 3066 skb = napi_alloc_skb(napi, 3067 rx_frag_head_sz); 3068 } else { 3069 rx_frag_head_sz = 0; 3070 skb = napi_get_frags(napi); 3071 } 3072 } else { 3073 rx_frag_head_sz = 0; 3074 skb = napi_alloc_skb(napi, len); 3075 } 3076 3077 if (!skb) { 3078 stats->rx_dropped++; 3079 goto find_next_rx; 3080 } 3081 3082 skb->ip_summed = tp->desc_ops.rx_csum(tp, rx_desc); 3083 tp->rx_desc.vlan_tag(rx_desc, skb); 3084 3085 if (use_frags) { 3086 if (rx_frag_head_sz) { 3087 memcpy(skb->data, rx_data, 3088 rx_frag_head_sz); 3089 skb_put(skb, rx_frag_head_sz); 3090 len -= rx_frag_head_sz; 3091 rx_data += rx_frag_head_sz; 3092 skb->protocol = eth_type_trans(skb, 3093 netdev); 3094 } 3095 3096 skb_add_rx_frag(skb, 0, agg->page, 3097 agg_offset(agg, rx_data), 3098 len, SKB_DATA_ALIGN(len)); 3099 get_page(agg->page); 3100 } else { 3101 memcpy(skb->data, rx_data, len); 3102 skb_put(skb, len); 3103 skb->protocol = eth_type_trans(skb, netdev); 3104 } 3105 3106 if (work_done < budget) { 3107 if (use_frags) 3108 napi_gro_frags(napi); 3109 else 3110 napi_gro_receive(napi, skb); 3111 3112 work_done++; 3113 stats->rx_packets++; 3114 stats->rx_bytes += pkt_len; 3115 } else { 3116 __skb_queue_tail(&tp->rx_queue, skb); 3117 } 3118 3119 find_next_rx: 3120 rx_data = rx_agg_align(tp, rx_data + len + ETH_FCS_LEN); 3121 rx_desc = rx_data; 3122 len_used = agg_offset(agg, rx_data); 3123 len_used += tp->rx_desc.size; 3124 } 3125 3126 WARN_ON(!agg_free && page_count(agg->page) > 1); 3127 3128 if (agg_free) { 3129 spin_lock_irqsave(&tp->rx_lock, flags); 3130 if (page_count(agg->page) == 1) { 3131 list_add(&agg_free->list, &tp->rx_used); 3132 } else { 3133 list_add_tail(&agg->list, &tp->rx_used); 3134 agg = agg_free; 3135 urb = agg->urb; 3136 } 3137 spin_unlock_irqrestore(&tp->rx_lock, flags); 3138 } 3139 3140 submit: 3141 if (!ret) { 3142 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); 3143 } else { 3144 urb->actual_length = 0; 3145 list_add_tail(&agg->list, next); 3146 } 3147 } 3148 3149 /* Splice the remained list back to rx_done for next schedule */ 3150 if (!list_empty(&rx_queue)) { 3151 spin_lock_irqsave(&tp->rx_lock, flags); 3152 list_splice(&rx_queue, &tp->rx_done); 3153 spin_unlock_irqrestore(&tp->rx_lock, flags); 3154 } 3155 3156 out1: 3157 return work_done; 3158 } 3159 3160 static void tx_bottom(struct r8152 *tp) 3161 { 3162 int res; 3163 3164 do { 3165 struct net_device *netdev = tp->netdev; 3166 struct tx_agg *agg; 3167 3168 if (skb_queue_empty(&tp->tx_queue)) 3169 break; 3170 3171 agg = r8152_get_tx_agg(tp); 3172 if (!agg) 3173 break; 3174 3175 res = r8152_tx_agg_fill(tp, agg); 3176 if (!res) 3177 continue; 3178 3179 if (res == -ENODEV) { 3180 rtl_set_unplug(tp); 3181 netif_device_detach(netdev); 3182 } else { 3183 struct net_device_stats *stats = &netdev->stats; 3184 unsigned long flags; 3185 3186 netif_warn(tp, tx_err, netdev, 3187 "failed tx_urb %d\n", res); 3188 stats->tx_dropped += agg->skb_num; 3189 3190 spin_lock_irqsave(&tp->tx_lock, flags); 3191 list_add_tail(&agg->list, &tp->tx_free); 3192 spin_unlock_irqrestore(&tp->tx_lock, flags); 3193 } 3194 } while (res == 0); 3195 } 3196 3197 static void bottom_half(struct tasklet_struct *t) 3198 { 3199 struct r8152 *tp = from_tasklet(tp, t, tx_tl); 3200 3201 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3202 return; 3203 3204 if (!test_bit(WORK_ENABLE, &tp->flags)) 3205 return; 3206 3207 /* When link down, the driver would cancel all bulks. */ 3208 /* This avoid the re-submitting bulk */ 3209 if (!netif_carrier_ok(tp->netdev)) 3210 return; 3211 3212 clear_bit(SCHEDULE_TASKLET, &tp->flags); 3213 3214 tx_bottom(tp); 3215 } 3216 3217 static int r8152_poll(struct napi_struct *napi, int budget) 3218 { 3219 struct r8152 *tp = container_of(napi, struct r8152, napi); 3220 int work_done; 3221 3222 if (!budget) 3223 return 0; 3224 3225 work_done = rx_bottom(tp, budget); 3226 3227 if (work_done < budget) { 3228 if (!napi_complete_done(napi, work_done)) 3229 goto out; 3230 if (!list_empty(&tp->rx_done)) 3231 napi_schedule(napi); 3232 } 3233 3234 out: 3235 return work_done; 3236 } 3237 3238 static 3239 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) 3240 { 3241 int ret; 3242 3243 /* The rx would be stopped, so skip submitting */ 3244 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags) || 3245 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev)) 3246 return 0; 3247 3248 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_in, 3249 agg->buffer, tp->rx_buf_sz, 3250 (usb_complete_t)read_bulk_callback, agg); 3251 3252 ret = usb_submit_urb(agg->urb, mem_flags); 3253 if (ret == -ENODEV) { 3254 rtl_set_unplug(tp); 3255 netif_device_detach(tp->netdev); 3256 } else if (ret) { 3257 struct urb *urb = agg->urb; 3258 unsigned long flags; 3259 3260 urb->actual_length = 0; 3261 spin_lock_irqsave(&tp->rx_lock, flags); 3262 list_add_tail(&agg->list, &tp->rx_done); 3263 spin_unlock_irqrestore(&tp->rx_lock, flags); 3264 3265 netif_err(tp, rx_err, tp->netdev, 3266 "Couldn't submit rx[%p], ret = %d\n", agg, ret); 3267 3268 napi_schedule(&tp->napi); 3269 } 3270 3271 return ret; 3272 } 3273 3274 static void rtl_drop_queued_tx(struct r8152 *tp) 3275 { 3276 struct net_device_stats *stats = &tp->netdev->stats; 3277 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; 3278 struct sk_buff *skb; 3279 3280 if (skb_queue_empty(tx_queue)) 3281 return; 3282 3283 __skb_queue_head_init(&skb_head); 3284 spin_lock_bh(&tx_queue->lock); 3285 skb_queue_splice_init(tx_queue, &skb_head); 3286 spin_unlock_bh(&tx_queue->lock); 3287 3288 while ((skb = __skb_dequeue(&skb_head))) { 3289 dev_kfree_skb(skb); 3290 stats->tx_dropped++; 3291 } 3292 } 3293 3294 static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue) 3295 { 3296 struct r8152 *tp = netdev_priv(netdev); 3297 3298 netif_warn(tp, tx_err, netdev, "Tx timeout\n"); 3299 3300 usb_queue_reset_device(tp->intf); 3301 } 3302 3303 static void rtl8152_set_rx_mode(struct net_device *netdev) 3304 { 3305 struct r8152 *tp = netdev_priv(netdev); 3306 3307 if (netif_carrier_ok(netdev)) { 3308 set_bit(RTL8152_SET_RX_MODE, &tp->flags); 3309 schedule_delayed_work(&tp->schedule, 0); 3310 } 3311 } 3312 3313 static void _rtl8152_set_rx_mode(struct net_device *netdev) 3314 { 3315 struct r8152 *tp = netdev_priv(netdev); 3316 u32 mc_filter[2]; /* Multicast hash filter */ 3317 __le32 tmp[2]; 3318 u32 ocp_data; 3319 3320 netif_stop_queue(netdev); 3321 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 3322 ocp_data &= ~RCR_ACPT_ALL; 3323 ocp_data |= RCR_AB | RCR_APM; 3324 3325 if (netdev->flags & IFF_PROMISC) { 3326 /* Unconditionally log net taps. */ 3327 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); 3328 ocp_data |= RCR_AM | RCR_AAP; 3329 mc_filter[1] = 0xffffffff; 3330 mc_filter[0] = 0xffffffff; 3331 } else if ((netdev->flags & IFF_MULTICAST && 3332 netdev_mc_count(netdev) > multicast_filter_limit) || 3333 (netdev->flags & IFF_ALLMULTI)) { 3334 /* Too many to filter perfectly -- accept all multicasts. */ 3335 ocp_data |= RCR_AM; 3336 mc_filter[1] = 0xffffffff; 3337 mc_filter[0] = 0xffffffff; 3338 } else { 3339 mc_filter[1] = 0; 3340 mc_filter[0] = 0; 3341 3342 if (netdev->flags & IFF_MULTICAST) { 3343 struct netdev_hw_addr *ha; 3344 3345 netdev_for_each_mc_addr(ha, netdev) { 3346 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; 3347 3348 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); 3349 ocp_data |= RCR_AM; 3350 } 3351 } 3352 } 3353 3354 tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); 3355 tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); 3356 3357 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); 3358 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 3359 netif_wake_queue(netdev); 3360 } 3361 3362 static netdev_features_t 3363 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev, 3364 netdev_features_t features) 3365 { 3366 struct r8152 *tp = netdev_priv(dev); 3367 u32 mss = skb_shinfo(skb)->gso_size; 3368 int max_offset; 3369 3370 if (tp->version < RTL_VER_16) 3371 max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX; 3372 else 3373 max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX_2; 3374 3375 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && 3376 skb_transport_offset(skb) > max_offset) 3377 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 3378 else if ((skb->len + tp->tx_desc.size) > agg_buf_sz) 3379 features &= ~NETIF_F_GSO_MASK; 3380 3381 return features; 3382 } 3383 3384 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, 3385 struct net_device *netdev) 3386 { 3387 struct r8152 *tp = netdev_priv(netdev); 3388 3389 skb_tx_timestamp(skb); 3390 3391 skb_queue_tail(&tp->tx_queue, skb); 3392 3393 if (!list_empty(&tp->tx_free)) { 3394 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { 3395 set_bit(SCHEDULE_TASKLET, &tp->flags); 3396 schedule_delayed_work(&tp->schedule, 0); 3397 } else { 3398 usb_mark_last_busy(tp->udev); 3399 tasklet_schedule(&tp->tx_tl); 3400 } 3401 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) { 3402 netif_stop_queue(netdev); 3403 } 3404 3405 return NETDEV_TX_OK; 3406 } 3407 3408 static void r8152b_reset_packet_filter(struct r8152 *tp) 3409 { 3410 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_FMC, FMC_FCR_MCU_EN); 3411 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_FMC, FMC_FCR_MCU_EN); 3412 } 3413 3414 static void rtl8152_nic_reset(struct r8152 *tp) 3415 { 3416 switch (tp->version) { 3417 case RTL_TEST_01: 3418 case RTL_VER_10: 3419 case RTL_VER_11: 3420 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_CR, CR_TE); 3421 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_BMU_RESET, BMU_RESET_EP_IN); 3422 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, CDC_ECM_EN); 3423 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_CR, CR_RE); 3424 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_BMU_RESET, BMU_RESET_EP_IN); 3425 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, CDC_ECM_EN); 3426 break; 3427 3428 case RTL_VER_16: 3429 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_CR, CR_RE | CR_TE); 3430 break; 3431 3432 default: 3433 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); 3434 3435 for (int i = 0; i < 1000; i++) { 3436 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3437 break; 3438 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) 3439 break; 3440 usleep_range(100, 400); 3441 } 3442 break; 3443 } 3444 } 3445 3446 static void set_tx_qlen(struct r8152 *tp) 3447 { 3448 tp->tx_qlen = agg_buf_sz / (mtu_to_size(tp->netdev->mtu) + tp->tx_desc.size); 3449 } 3450 3451 static inline u16 rtl8152_get_speed(struct r8152 *tp) 3452 { 3453 return ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); 3454 } 3455 3456 static void rtl_eee_plus_en(struct r8152 *tp, bool enable) 3457 { 3458 if (enable) 3459 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_EEEP_CR, 3460 EEEP_CR_EEEP_TX); 3461 else 3462 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_EEEP_CR, 3463 EEEP_CR_EEEP_TX); 3464 } 3465 3466 static void rtl_set_eee_plus(struct r8152 *tp) 3467 { 3468 if (rtl8152_get_speed(tp) & _10bps) 3469 rtl_eee_plus_en(tp, true); 3470 else 3471 rtl_eee_plus_en(tp, false); 3472 } 3473 3474 static void rxdy_gated_en(struct r8152 *tp, bool enable) 3475 { 3476 if (enable) 3477 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MISC_1, RXDY_GATED_EN); 3478 else 3479 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MISC_1, RXDY_GATED_EN); 3480 } 3481 3482 static int rtl_start_rx(struct r8152 *tp) 3483 { 3484 struct rx_agg *agg, *agg_next; 3485 struct list_head tmp_list; 3486 unsigned long flags; 3487 int ret = 0, i = 0; 3488 3489 INIT_LIST_HEAD(&tmp_list); 3490 3491 spin_lock_irqsave(&tp->rx_lock, flags); 3492 3493 INIT_LIST_HEAD(&tp->rx_done); 3494 INIT_LIST_HEAD(&tp->rx_used); 3495 3496 list_splice_init(&tp->rx_info, &tmp_list); 3497 3498 spin_unlock_irqrestore(&tp->rx_lock, flags); 3499 3500 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { 3501 INIT_LIST_HEAD(&agg->list); 3502 3503 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */ 3504 if (++i > RTL8152_MAX_RX) { 3505 spin_lock_irqsave(&tp->rx_lock, flags); 3506 list_add_tail(&agg->list, &tp->rx_used); 3507 spin_unlock_irqrestore(&tp->rx_lock, flags); 3508 } else if (unlikely(ret < 0)) { 3509 spin_lock_irqsave(&tp->rx_lock, flags); 3510 list_add_tail(&agg->list, &tp->rx_done); 3511 spin_unlock_irqrestore(&tp->rx_lock, flags); 3512 } else { 3513 ret = r8152_submit_rx(tp, agg, GFP_KERNEL); 3514 } 3515 } 3516 3517 spin_lock_irqsave(&tp->rx_lock, flags); 3518 WARN_ON(!list_empty(&tp->rx_info)); 3519 list_splice(&tmp_list, &tp->rx_info); 3520 spin_unlock_irqrestore(&tp->rx_lock, flags); 3521 3522 return ret; 3523 } 3524 3525 static int rtl_stop_rx(struct r8152 *tp) 3526 { 3527 struct rx_agg *agg, *agg_next; 3528 struct list_head tmp_list; 3529 unsigned long flags; 3530 3531 INIT_LIST_HEAD(&tmp_list); 3532 3533 /* The usb_kill_urb() couldn't be used in atomic. 3534 * Therefore, move the list of rx_info to a tmp one. 3535 * Then, list_for_each_entry_safe could be used without 3536 * spin lock. 3537 */ 3538 3539 spin_lock_irqsave(&tp->rx_lock, flags); 3540 list_splice_init(&tp->rx_info, &tmp_list); 3541 spin_unlock_irqrestore(&tp->rx_lock, flags); 3542 3543 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { 3544 /* At least RTL8152_MAX_RX rx_agg have the page_count being 3545 * equal to 1, so the other ones could be freed safely. 3546 */ 3547 if (page_count(agg->page) > 1) 3548 free_rx_agg(tp, agg); 3549 else 3550 usb_kill_urb(agg->urb); 3551 } 3552 3553 /* Move back the list of temp to the rx_info */ 3554 spin_lock_irqsave(&tp->rx_lock, flags); 3555 WARN_ON(!list_empty(&tp->rx_info)); 3556 list_splice(&tmp_list, &tp->rx_info); 3557 spin_unlock_irqrestore(&tp->rx_lock, flags); 3558 3559 while (!skb_queue_empty(&tp->rx_queue)) 3560 dev_kfree_skb(__skb_dequeue(&tp->rx_queue)); 3561 3562 return 0; 3563 } 3564 3565 static void rtl_set_ifg(struct r8152 *tp, u16 speed) 3566 { 3567 if ((speed & (_10bps | _100bps)) && !(speed & FULL_DUP)) { 3568 ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_TCR1, IFG_MASK, IFG_144NS); 3569 3570 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 3571 TX10MIDLE_EN); 3572 } else { 3573 ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_TCR1, IFG_MASK, IFG_96NS); 3574 3575 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 3576 TX10MIDLE_EN); 3577 } 3578 } 3579 3580 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp) 3581 { 3582 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN, 3583 OWN_UPDATE | OWN_CLEAR); 3584 } 3585 3586 static int rtl_enable(struct r8152 *tp) 3587 { 3588 r8152b_reset_packet_filter(tp); 3589 3590 ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_CR, CR_RE | CR_TE); 3591 3592 switch (tp->version) { 3593 case RTL_VER_01: 3594 case RTL_VER_02: 3595 case RTL_VER_03: 3596 case RTL_VER_04: 3597 case RTL_VER_05: 3598 case RTL_VER_06: 3599 case RTL_VER_07: 3600 break; 3601 default: 3602 r8153b_rx_agg_chg_indicate(tp); 3603 break; 3604 } 3605 3606 rxdy_gated_en(tp, false); 3607 3608 return 0; 3609 } 3610 3611 static int rtl8152_enable(struct r8152 *tp) 3612 { 3613 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3614 return -ENODEV; 3615 3616 set_tx_qlen(tp); 3617 rtl_set_eee_plus(tp); 3618 3619 return rtl_enable(tp); 3620 } 3621 3622 static void r8153_set_rx_early_timeout(struct r8152 *tp) 3623 { 3624 u32 ocp_data = tp->coalesce / 8; 3625 3626 switch (tp->version) { 3627 case RTL_VER_03: 3628 case RTL_VER_04: 3629 case RTL_VER_05: 3630 case RTL_VER_06: 3631 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 3632 ocp_data); 3633 break; 3634 3635 case RTL_VER_08: 3636 case RTL_VER_09: 3637 case RTL_VER_14: 3638 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout 3639 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns. 3640 */ 3641 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 3642 128 / 8); 3643 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, 3644 ocp_data); 3645 break; 3646 3647 case RTL_VER_10: 3648 case RTL_VER_11: 3649 case RTL_VER_12: 3650 case RTL_VER_13: 3651 case RTL_VER_15: 3652 case RTL_VER_16: 3653 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 3654 640 / 8); 3655 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, 3656 ocp_data); 3657 break; 3658 3659 default: 3660 break; 3661 } 3662 } 3663 3664 static u32 rx_reserved_size(struct r8152 *tp, u32 mtu) 3665 { 3666 return mtu_to_size(mtu) + tp->rx_desc.size + tp->rx_desc.align; 3667 } 3668 3669 static void r8153_set_rx_early_size(struct r8152 *tp) 3670 { 3671 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp, tp->netdev->mtu); 3672 3673 switch (tp->version) { 3674 case RTL_VER_03: 3675 case RTL_VER_04: 3676 case RTL_VER_05: 3677 case RTL_VER_06: 3678 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 3679 ocp_data / 4); 3680 break; 3681 case RTL_VER_08: 3682 case RTL_VER_09: 3683 case RTL_VER_14: 3684 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 3685 ocp_data / 8); 3686 break; 3687 case RTL_TEST_01: 3688 case RTL_VER_10: 3689 case RTL_VER_11: 3690 case RTL_VER_12: 3691 case RTL_VER_13: 3692 case RTL_VER_15: 3693 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 3694 ocp_data / 8); 3695 break; 3696 case RTL_VER_16: 3697 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 3698 ocp_data / 16); 3699 break; 3700 default: 3701 WARN_ON_ONCE(1); 3702 break; 3703 } 3704 } 3705 3706 static int rtl8153_enable(struct r8152 *tp) 3707 { 3708 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3709 return -ENODEV; 3710 3711 set_tx_qlen(tp); 3712 rtl_set_eee_plus(tp); 3713 r8153_set_rx_early_timeout(tp); 3714 r8153_set_rx_early_size(tp); 3715 3716 rtl_set_ifg(tp, rtl8152_get_speed(tp)); 3717 3718 switch (tp->version) { 3719 case RTL_VER_09: 3720 case RTL_VER_14: 3721 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK); 3722 usleep_range(1000, 2000); 3723 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK); 3724 break; 3725 default: 3726 break; 3727 } 3728 3729 return rtl_enable(tp); 3730 } 3731 3732 static void rtl_disable(struct r8152 *tp) 3733 { 3734 u32 ocp_data; 3735 int i; 3736 3737 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 3738 rtl_drop_queued_tx(tp); 3739 return; 3740 } 3741 3742 ocp_dword_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, RCR_ACPT_ALL); 3743 3744 rtl_drop_queued_tx(tp); 3745 3746 for (i = 0; i < RTL8152_MAX_TX; i++) 3747 usb_kill_urb(tp->tx_info[i].urb); 3748 3749 rxdy_gated_en(tp, true); 3750 3751 for (i = 0; i < 1000; i++) { 3752 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3753 break; 3754 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 3755 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) 3756 break; 3757 usleep_range(1000, 2000); 3758 } 3759 3760 for (i = 0; i < 1000; i++) { 3761 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3762 break; 3763 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) 3764 break; 3765 usleep_range(1000, 2000); 3766 } 3767 3768 rtl_stop_rx(tp); 3769 3770 rtl8152_nic_reset(tp); 3771 } 3772 3773 static void r8152_power_cut_en(struct r8152 *tp, bool enable) 3774 { 3775 if (enable) 3776 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_UPS_CTRL, POWER_CUT); 3777 else 3778 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_UPS_CTRL, POWER_CUT); 3779 3780 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, 3781 RESUME_INDICATE); 3782 } 3783 3784 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) 3785 { 3786 switch (tp->version) { 3787 case RTL_VER_01: 3788 case RTL_VER_02: 3789 case RTL_VER_03: 3790 case RTL_VER_04: 3791 case RTL_VER_05: 3792 case RTL_VER_06: 3793 case RTL_VER_07: 3794 case RTL_VER_08: 3795 case RTL_VER_09: 3796 case RTL_VER_14: 3797 if (enable) 3798 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_CPCR, 3799 CPCR_RX_VLAN); 3800 else 3801 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_CPCR, 3802 CPCR_RX_VLAN); 3803 break; 3804 3805 case RTL_TEST_01: 3806 case RTL_VER_10: 3807 case RTL_VER_11: 3808 case RTL_VER_12: 3809 case RTL_VER_13: 3810 case RTL_VER_15: 3811 case RTL_VER_16: 3812 default: 3813 if (enable) 3814 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_RCR1, 3815 OUTER_VLAN | INNER_VLAN); 3816 else 3817 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR1, 3818 OUTER_VLAN | INNER_VLAN); 3819 break; 3820 } 3821 } 3822 3823 static int rtl8152_set_features(struct net_device *dev, 3824 netdev_features_t features) 3825 { 3826 netdev_features_t changed = features ^ dev->features; 3827 struct r8152 *tp = netdev_priv(dev); 3828 int ret; 3829 3830 ret = usb_autopm_get_interface(tp->intf); 3831 if (ret < 0) 3832 goto out; 3833 3834 mutex_lock(&tp->control); 3835 3836 if (changed & NETIF_F_HW_VLAN_CTAG_RX) { 3837 if (features & NETIF_F_HW_VLAN_CTAG_RX) 3838 rtl_rx_vlan_en(tp, true); 3839 else 3840 rtl_rx_vlan_en(tp, false); 3841 } 3842 3843 mutex_unlock(&tp->control); 3844 3845 usb_autopm_put_interface(tp->intf); 3846 3847 out: 3848 return ret; 3849 } 3850 3851 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) 3852 3853 static u32 __rtl_get_wol(struct r8152 *tp) 3854 { 3855 u32 ocp_data; 3856 u32 wolopts = 0; 3857 3858 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3859 if (ocp_data & LINK_ON_WAKE_EN) 3860 wolopts |= WAKE_PHY; 3861 3862 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); 3863 if (ocp_data & UWF_EN) 3864 wolopts |= WAKE_UCAST; 3865 if (ocp_data & BWF_EN) 3866 wolopts |= WAKE_BCAST; 3867 if (ocp_data & MWF_EN) 3868 wolopts |= WAKE_MCAST; 3869 3870 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); 3871 if (ocp_data & MAGIC_EN) 3872 wolopts |= WAKE_MAGIC; 3873 3874 return wolopts; 3875 } 3876 3877 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts) 3878 { 3879 u16 ocp_data; 3880 3881 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3882 3883 if (wolopts & WAKE_PHY) 3884 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_CONFIG34, 3885 LINK_ON_WAKE_EN); 3886 else 3887 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_CONFIG34, 3888 LINK_ON_WAKE_EN); 3889 3890 ocp_data = 0; 3891 if (wolopts & WAKE_UCAST) 3892 ocp_data |= UWF_EN; 3893 if (wolopts & WAKE_BCAST) 3894 ocp_data |= BWF_EN; 3895 if (wolopts & WAKE_MCAST) 3896 ocp_data |= MWF_EN; 3897 ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_CONFIG5, UWF_EN | BWF_EN | MWF_EN, 3898 ocp_data); 3899 3900 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3901 3902 if (wolopts & WAKE_MAGIC) 3903 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_CFG_WOL, MAGIC_EN); 3904 else 3905 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_CFG_WOL, MAGIC_EN); 3906 3907 if (wolopts & WAKE_ANY) 3908 device_set_wakeup_enable(&tp->udev->dev, true); 3909 else 3910 device_set_wakeup_enable(&tp->udev->dev, false); 3911 } 3912 3913 static void r8153_mac_clk_speed_down(struct r8152 *tp, bool enable) 3914 { 3915 /* MAC clock speed down */ 3916 if (enable) 3917 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 3918 MAC_CLK_SPDWN_EN); 3919 else 3920 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 3921 MAC_CLK_SPDWN_EN); 3922 } 3923 3924 static void r8156_mac_clk_spd(struct r8152 *tp, bool enable) 3925 { 3926 /* MAC clock speed down */ 3927 if (enable) { 3928 /* aldps_spdwn_ratio, tp10_spdwn_ratio */ 3929 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0x0403); 3930 3931 ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 3932 EEE_SPDWN_RATIO_MASK, MAC_CLK_SPDWN_EN | 0x03); 3933 } else { 3934 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 3935 MAC_CLK_SPDWN_EN); 3936 } 3937 } 3938 3939 static void r8153_u1u2en(struct r8152 *tp, bool enable) 3940 { 3941 u8 u1u2[8]; 3942 3943 if (enable) 3944 memset(u1u2, 0xff, sizeof(u1u2)); 3945 else 3946 memset(u1u2, 0x00, sizeof(u1u2)); 3947 3948 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); 3949 } 3950 3951 static void r8153b_u1u2en(struct r8152 *tp, bool enable) 3952 { 3953 if (enable) 3954 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_LPM_CONFIG, 3955 LPM_U1U2_EN); 3956 else 3957 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_LPM_CONFIG, 3958 LPM_U1U2_EN); 3959 } 3960 3961 static void r8153_u2p3en(struct r8152 *tp, bool enable) 3962 { 3963 if (enable) 3964 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_U2P3_CTRL, U2P3_ENABLE); 3965 else 3966 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_U2P3_CTRL, U2P3_ENABLE); 3967 } 3968 3969 static int r8157_u2p3en(struct r8152 *tp, bool enable) 3970 { 3971 if (enable) 3972 return rtl_ip_set_bits(tp, USB_U2P3_V2_CTRL, U2P3_V2_ENABLE); 3973 else 3974 return rtl_ip_clr_bits(tp, USB_U2P3_V2_CTRL, U2P3_V2_ENABLE); 3975 } 3976 3977 static void r8153b_ups_flags(struct r8152 *tp) 3978 { 3979 u32 ups_flags = 0; 3980 3981 if (tp->ups_info.green) 3982 ups_flags |= UPS_FLAGS_EN_GREEN; 3983 3984 if (tp->ups_info.aldps) 3985 ups_flags |= UPS_FLAGS_EN_ALDPS; 3986 3987 if (tp->ups_info.eee) 3988 ups_flags |= UPS_FLAGS_EN_EEE; 3989 3990 if (tp->ups_info.flow_control) 3991 ups_flags |= UPS_FLAGS_EN_FLOW_CTR; 3992 3993 if (tp->ups_info.eee_ckdiv) 3994 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV; 3995 3996 if (tp->ups_info.eee_cmod_lv) 3997 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN; 3998 3999 if (tp->ups_info.r_tune) 4000 ups_flags |= UPS_FLAGS_R_TUNE; 4001 4002 if (tp->ups_info._10m_ckdiv) 4003 ups_flags |= UPS_FLAGS_EN_10M_CKDIV; 4004 4005 if (tp->ups_info.eee_plloff_100) 4006 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100; 4007 4008 if (tp->ups_info.eee_plloff_giga) 4009 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA; 4010 4011 if (tp->ups_info._250m_ckdiv) 4012 ups_flags |= UPS_FLAGS_250M_CKDIV; 4013 4014 if (tp->ups_info.ctap_short_off) 4015 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS; 4016 4017 switch (tp->ups_info.speed_duplex) { 4018 case NWAY_10M_HALF: 4019 ups_flags |= ups_flags_speed(1); 4020 break; 4021 case NWAY_10M_FULL: 4022 ups_flags |= ups_flags_speed(2); 4023 break; 4024 case NWAY_100M_HALF: 4025 ups_flags |= ups_flags_speed(3); 4026 break; 4027 case NWAY_100M_FULL: 4028 ups_flags |= ups_flags_speed(4); 4029 break; 4030 case NWAY_1000M_FULL: 4031 ups_flags |= ups_flags_speed(5); 4032 break; 4033 case FORCE_10M_HALF: 4034 ups_flags |= ups_flags_speed(6); 4035 break; 4036 case FORCE_10M_FULL: 4037 ups_flags |= ups_flags_speed(7); 4038 break; 4039 case FORCE_100M_HALF: 4040 ups_flags |= ups_flags_speed(8); 4041 break; 4042 case FORCE_100M_FULL: 4043 ups_flags |= ups_flags_speed(9); 4044 break; 4045 default: 4046 break; 4047 } 4048 4049 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags); 4050 } 4051 4052 static void r8156_ups_flags(struct r8152 *tp) 4053 { 4054 u32 ups_flags = 0; 4055 4056 if (tp->ups_info.green) 4057 ups_flags |= UPS_FLAGS_EN_GREEN; 4058 4059 if (tp->ups_info.aldps) 4060 ups_flags |= UPS_FLAGS_EN_ALDPS; 4061 4062 if (tp->ups_info.eee) 4063 ups_flags |= UPS_FLAGS_EN_EEE; 4064 4065 if (tp->ups_info.flow_control) 4066 ups_flags |= UPS_FLAGS_EN_FLOW_CTR; 4067 4068 if (tp->ups_info.eee_ckdiv) 4069 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV; 4070 4071 if (tp->ups_info._10m_ckdiv) 4072 ups_flags |= UPS_FLAGS_EN_10M_CKDIV; 4073 4074 if (tp->ups_info.eee_plloff_100) 4075 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100; 4076 4077 if (tp->ups_info.eee_plloff_giga) 4078 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA; 4079 4080 if (tp->ups_info._250m_ckdiv) 4081 ups_flags |= UPS_FLAGS_250M_CKDIV; 4082 4083 switch (tp->ups_info.speed_duplex) { 4084 case FORCE_10M_HALF: 4085 ups_flags |= ups_flags_speed(0); 4086 break; 4087 case FORCE_10M_FULL: 4088 ups_flags |= ups_flags_speed(1); 4089 break; 4090 case FORCE_100M_HALF: 4091 ups_flags |= ups_flags_speed(2); 4092 break; 4093 case FORCE_100M_FULL: 4094 ups_flags |= ups_flags_speed(3); 4095 break; 4096 case NWAY_10M_HALF: 4097 ups_flags |= ups_flags_speed(4); 4098 break; 4099 case NWAY_10M_FULL: 4100 ups_flags |= ups_flags_speed(5); 4101 break; 4102 case NWAY_100M_HALF: 4103 ups_flags |= ups_flags_speed(6); 4104 break; 4105 case NWAY_100M_FULL: 4106 ups_flags |= ups_flags_speed(7); 4107 break; 4108 case NWAY_1000M_FULL: 4109 ups_flags |= ups_flags_speed(8); 4110 break; 4111 case NWAY_2500M_FULL: 4112 ups_flags |= ups_flags_speed(9); 4113 break; 4114 default: 4115 break; 4116 } 4117 4118 switch (tp->ups_info.lite_mode) { 4119 case 1: 4120 ups_flags |= 0 << 5; 4121 break; 4122 case 2: 4123 ups_flags |= 2 << 5; 4124 break; 4125 case 0: 4126 default: 4127 ups_flags |= 1 << 5; 4128 break; 4129 } 4130 4131 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags); 4132 } 4133 4134 static void rtl_green_en(struct r8152 *tp, bool enable) 4135 { 4136 if (enable) 4137 sram_set_bits(tp, SRAM_GREEN_CFG, GREEN_ETH_EN); 4138 else 4139 sram_clr_bits(tp, SRAM_GREEN_CFG, GREEN_ETH_EN); 4140 4141 tp->ups_info.green = enable; 4142 } 4143 4144 static void r8153b_green_en(struct r8152 *tp, bool enable) 4145 { 4146 if (enable) { 4147 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */ 4148 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */ 4149 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */ 4150 } else { 4151 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */ 4152 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */ 4153 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */ 4154 } 4155 4156 rtl_green_en(tp, true); 4157 } 4158 4159 static u16 r8153_phy_status(struct r8152 *tp, u16 desired) 4160 { 4161 u16 data; 4162 int i; 4163 4164 for (i = 0; i < 500; i++) { 4165 data = ocp_reg_read(tp, OCP_PHY_STATUS); 4166 data &= PHY_STAT_MASK; 4167 if (desired) { 4168 if (data == desired) 4169 break; 4170 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN || 4171 data == PHY_STAT_EXT_INIT) { 4172 break; 4173 } 4174 4175 msleep(20); 4176 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 4177 break; 4178 } 4179 4180 return data; 4181 } 4182 4183 static void r8153b_ups_en(struct r8152 *tp, bool enable) 4184 { 4185 if (enable) { 4186 r8153b_ups_flags(tp); 4187 4188 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, 4189 UPS_EN | USP_PREWAKE | PHASE2_EN); 4190 4191 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_MISC_2, 4192 UPS_FORCE_PWR_DOWN); 4193 } else { 4194 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, 4195 UPS_EN | USP_PREWAKE); 4196 4197 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2, 4198 UPS_FORCE_PWR_DOWN); 4199 4200 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) { 4201 int i; 4202 4203 for (i = 0; i < 500; i++) { 4204 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 4205 return; 4206 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 4207 AUTOLOAD_DONE) 4208 break; 4209 msleep(20); 4210 } 4211 4212 tp->rtl_ops.hw_phy_cfg(tp); 4213 4214 rtl8152_set_speed(tp, tp->autoneg, tp->speed, 4215 tp->duplex, tp->advertising); 4216 } 4217 } 4218 } 4219 4220 static void r8153c_ups_en(struct r8152 *tp, bool enable) 4221 { 4222 if (enable) { 4223 r8153b_ups_flags(tp); 4224 4225 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, 4226 UPS_EN | USP_PREWAKE | PHASE2_EN); 4227 4228 ocp_byte_w0w1(tp, MCU_TYPE_USB, USB_MISC_2, BIT(7), 4229 UPS_FORCE_PWR_DOWN); 4230 } else { 4231 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, 4232 UPS_EN | USP_PREWAKE); 4233 4234 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2, 4235 UPS_FORCE_PWR_DOWN); 4236 4237 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) { 4238 int i; 4239 4240 for (i = 0; i < 500; i++) { 4241 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 4242 return; 4243 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 4244 AUTOLOAD_DONE) 4245 break; 4246 msleep(20); 4247 } 4248 4249 tp->rtl_ops.hw_phy_cfg(tp); 4250 4251 rtl8152_set_speed(tp, tp->autoneg, tp->speed, 4252 tp->duplex, tp->advertising); 4253 } 4254 4255 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 4256 4257 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_CONFIG34, BIT(8)); 4258 4259 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 4260 } 4261 } 4262 4263 static void r8156_ups_en(struct r8152 *tp, bool enable) 4264 { 4265 if (enable) { 4266 r8156_ups_flags(tp); 4267 4268 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, 4269 UPS_EN | USP_PREWAKE | PHASE2_EN); 4270 4271 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_MISC_2, 4272 UPS_FORCE_PWR_DOWN); 4273 4274 switch (tp->version) { 4275 case RTL_VER_13: 4276 case RTL_VER_15: 4277 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_UPHY_XTAL, 4278 OOBS_POLLING); 4279 break; 4280 default: 4281 break; 4282 } 4283 } else { 4284 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, 4285 UPS_EN | USP_PREWAKE); 4286 4287 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2, 4288 UPS_FORCE_PWR_DOWN); 4289 4290 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) { 4291 tp->rtl_ops.hw_phy_cfg(tp); 4292 4293 rtl8152_set_speed(tp, tp->autoneg, tp->speed, 4294 tp->duplex, tp->advertising); 4295 } 4296 } 4297 } 4298 4299 static void r8153_power_cut_en(struct r8152 *tp, bool enable) 4300 { 4301 if (enable) 4302 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, 4303 PWR_EN | PHASE2_EN); 4304 else 4305 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, 4306 PWR_EN | PHASE2_EN); 4307 4308 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS); 4309 } 4310 4311 static void r8153b_power_cut_en(struct r8152 *tp, bool enable) 4312 { 4313 if (enable) 4314 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, 4315 PWR_EN | PHASE2_EN); 4316 else 4317 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, PWR_EN); 4318 4319 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS); 4320 } 4321 4322 static void r8157_power_cut_en(struct r8152 *tp, bool enable) 4323 { 4324 if (enable) { 4325 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, PWR_EN | PHASE2_EN); 4326 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_MISC_2, BIT(1)); 4327 } else { 4328 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, PWR_EN); 4329 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS); 4330 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2, BIT(1)); 4331 } 4332 } 4333 4334 static void r8153_queue_wake(struct r8152 *tp, bool enable) 4335 { 4336 if (enable) 4337 ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, 4338 UPCOMING_RUNTIME_D3); 4339 else 4340 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, 4341 UPCOMING_RUNTIME_D3); 4342 4343 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, LINK_CHG_EVENT); 4344 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, LINK_CHANGE_FLAG); 4345 } 4346 4347 static bool rtl_can_wakeup(struct r8152 *tp) 4348 { 4349 struct usb_device *udev = tp->udev; 4350 4351 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP); 4352 } 4353 4354 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable) 4355 { 4356 if (enable) { 4357 __rtl_set_wol(tp, WAKE_ANY); 4358 4359 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 4360 4361 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_CONFIG34, 4362 LINK_OFF_WAKE_EN); 4363 4364 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 4365 } else { 4366 __rtl_set_wol(tp, tp->saved_wolopts); 4367 4368 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 4369 4370 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_CONFIG34, 4371 LINK_OFF_WAKE_EN); 4372 4373 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 4374 } 4375 } 4376 4377 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable) 4378 { 4379 if (enable) { 4380 r8153_u1u2en(tp, false); 4381 r8153_u2p3en(tp, false); 4382 rtl_runtime_suspend_enable(tp, true); 4383 } else { 4384 rtl_runtime_suspend_enable(tp, false); 4385 4386 switch (tp->version) { 4387 case RTL_VER_03: 4388 case RTL_VER_04: 4389 break; 4390 case RTL_VER_05: 4391 case RTL_VER_06: 4392 default: 4393 r8153_u2p3en(tp, true); 4394 break; 4395 } 4396 4397 r8153_u1u2en(tp, true); 4398 } 4399 } 4400 4401 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable) 4402 { 4403 if (enable) { 4404 r8153_queue_wake(tp, true); 4405 r8153b_u1u2en(tp, false); 4406 r8153_u2p3en(tp, false); 4407 rtl_runtime_suspend_enable(tp, true); 4408 r8153b_ups_en(tp, true); 4409 } else { 4410 r8153b_ups_en(tp, false); 4411 r8153_queue_wake(tp, false); 4412 rtl_runtime_suspend_enable(tp, false); 4413 if (tp->udev->speed >= USB_SPEED_SUPER) 4414 r8153b_u1u2en(tp, true); 4415 } 4416 } 4417 4418 static void rtl8153c_runtime_enable(struct r8152 *tp, bool enable) 4419 { 4420 if (enable) { 4421 r8153_queue_wake(tp, true); 4422 r8153b_u1u2en(tp, false); 4423 r8153_u2p3en(tp, false); 4424 rtl_runtime_suspend_enable(tp, true); 4425 r8153c_ups_en(tp, true); 4426 } else { 4427 r8153c_ups_en(tp, false); 4428 r8153_queue_wake(tp, false); 4429 rtl_runtime_suspend_enable(tp, false); 4430 r8153b_u1u2en(tp, true); 4431 } 4432 } 4433 4434 static void rtl8156_runtime_enable(struct r8152 *tp, bool enable) 4435 { 4436 if (enable) { 4437 r8153_queue_wake(tp, true); 4438 r8153b_u1u2en(tp, false); 4439 r8153_u2p3en(tp, false); 4440 rtl_runtime_suspend_enable(tp, true); 4441 } else { 4442 r8153_queue_wake(tp, false); 4443 rtl_runtime_suspend_enable(tp, false); 4444 r8153_u2p3en(tp, true); 4445 if (tp->udev->speed >= USB_SPEED_SUPER) 4446 r8153b_u1u2en(tp, true); 4447 } 4448 } 4449 4450 static void rtl8157_runtime_enable(struct r8152 *tp, bool enable) 4451 { 4452 if (enable) { 4453 r8153_queue_wake(tp, true); 4454 r8153b_u1u2en(tp, false); 4455 r8157_u2p3en(tp, false); 4456 rtl_runtime_suspend_enable(tp, true); 4457 } else { 4458 r8153_queue_wake(tp, false); 4459 rtl_runtime_suspend_enable(tp, false); 4460 r8157_u2p3en(tp, true); 4461 if (tp->udev->speed >= USB_SPEED_SUPER) 4462 r8153b_u1u2en(tp, true); 4463 } 4464 } 4465 4466 static void r8153_teredo_off(struct r8152 *tp) 4467 { 4468 switch (tp->version) { 4469 case RTL_VER_01: 4470 case RTL_VER_02: 4471 case RTL_VER_03: 4472 case RTL_VER_04: 4473 case RTL_VER_05: 4474 case RTL_VER_06: 4475 case RTL_VER_07: 4476 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 4477 TEREDO_SEL | TEREDO_RS_EVENT_MASK | 4478 OOB_TEREDO_EN); 4479 break; 4480 4481 case RTL_VER_08: 4482 case RTL_VER_09: 4483 case RTL_TEST_01: 4484 case RTL_VER_10: 4485 case RTL_VER_11: 4486 case RTL_VER_12: 4487 case RTL_VER_13: 4488 case RTL_VER_14: 4489 case RTL_VER_15: 4490 case RTL_VER_16: 4491 default: 4492 /* The bit 0 ~ 7 are relative with teredo settings. They are 4493 * W1C (write 1 to clear), so set all 1 to disable it. 4494 */ 4495 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff); 4496 break; 4497 } 4498 4499 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); 4500 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); 4501 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); 4502 } 4503 4504 static void rtl_reset_bmu(struct r8152 *tp) 4505 { 4506 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_BMU_RESET, 4507 BMU_RESET_EP_IN | BMU_RESET_EP_OUT); 4508 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_BMU_RESET, 4509 BMU_RESET_EP_IN | BMU_RESET_EP_OUT); 4510 } 4511 4512 /* Clear the bp to stop the firmware before loading a new one */ 4513 static void rtl_clear_bp(struct r8152 *tp, u16 type) 4514 { 4515 u16 bp[16] = {0}; 4516 u16 bp_num; 4517 4518 switch (tp->version) { 4519 case RTL_VER_08: 4520 case RTL_VER_09: 4521 case RTL_VER_10: 4522 case RTL_VER_11: 4523 case RTL_VER_12: 4524 case RTL_VER_13: 4525 case RTL_VER_15: 4526 if (type == MCU_TYPE_USB) { 4527 ocp_write_word(tp, MCU_TYPE_USB, USB_BP2_EN, 0); 4528 bp_num = 16; 4529 break; 4530 } 4531 fallthrough; 4532 case RTL_VER_03: 4533 case RTL_VER_04: 4534 case RTL_VER_05: 4535 case RTL_VER_06: 4536 ocp_write_byte(tp, type, PLA_BP_EN, 0); 4537 fallthrough; 4538 case RTL_VER_01: 4539 case RTL_VER_02: 4540 case RTL_VER_07: 4541 bp_num = 8; 4542 break; 4543 case RTL_VER_14: 4544 case RTL_VER_16: 4545 default: 4546 ocp_write_word(tp, type, USB_BP2_EN, 0); 4547 bp_num = 16; 4548 break; 4549 } 4550 4551 generic_ocp_write(tp, PLA_BP_0, BYTE_EN_DWORD, bp_num << 1, bp, type); 4552 4553 /* wait 3 ms to make sure the firmware is stopped */ 4554 usleep_range(3000, 6000); 4555 ocp_write_word(tp, type, PLA_BP_BA, 0); 4556 } 4557 4558 static inline void rtl_reset_ocp_base(struct r8152 *tp) 4559 { 4560 tp->ocp_base = -1; 4561 } 4562 4563 static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait) 4564 { 4565 u16 check; 4566 int i; 4567 4568 if (request) { 4569 ocp_reg_set_bits(tp, OCP_PHY_PATCH_CMD, PATCH_REQUEST); 4570 check = 0; 4571 } else { 4572 ocp_reg_clr_bits(tp, OCP_PHY_PATCH_CMD, PATCH_REQUEST); 4573 check = PATCH_READY; 4574 } 4575 4576 for (i = 0; wait && i < 5000; i++) { 4577 u32 ocp_data; 4578 4579 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 4580 return -ENODEV; 4581 4582 usleep_range(1000, 2000); 4583 ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT); 4584 if ((ocp_data & PATCH_READY) ^ check) 4585 break; 4586 } 4587 4588 if (request && wait && 4589 !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) { 4590 dev_err(&tp->intf->dev, "PHY patch request fail\n"); 4591 rtl_phy_patch_request(tp, false, false); 4592 return -ETIME; 4593 } else { 4594 return 0; 4595 } 4596 } 4597 4598 static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key) 4599 { 4600 if (patch_key && key_addr) { 4601 sram_write(tp, key_addr, patch_key); 4602 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK); 4603 } else if (key_addr) { 4604 sram_write(tp, 0x0000, 0x0000); 4605 ocp_reg_clr_bits(tp, OCP_PHY_LOCK, PATCH_LOCK); 4606 sram_write(tp, key_addr, 0x0000); 4607 } else { 4608 WARN_ON_ONCE(1); 4609 } 4610 } 4611 4612 static int 4613 rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait) 4614 { 4615 if (rtl_phy_patch_request(tp, true, wait)) 4616 return -ETIME; 4617 4618 rtl_patch_key_set(tp, key_addr, patch_key); 4619 4620 return 0; 4621 } 4622 4623 static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait) 4624 { 4625 rtl_patch_key_set(tp, key_addr, 0); 4626 4627 rtl_phy_patch_request(tp, false, wait); 4628 4629 return 0; 4630 } 4631 4632 static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_up *phy) 4633 { 4634 u16 fw_offset; 4635 u32 length; 4636 bool rc = false; 4637 4638 switch (tp->version) { 4639 case RTL_VER_01: 4640 case RTL_VER_02: 4641 case RTL_VER_03: 4642 case RTL_VER_04: 4643 case RTL_VER_05: 4644 case RTL_VER_06: 4645 case RTL_VER_07: 4646 case RTL_VER_08: 4647 case RTL_VER_09: 4648 case RTL_VER_10: 4649 case RTL_VER_11: 4650 case RTL_VER_12: 4651 case RTL_VER_14: 4652 case RTL_VER_16: 4653 goto out; 4654 case RTL_VER_13: 4655 case RTL_VER_15: 4656 default: 4657 break; 4658 } 4659 4660 fw_offset = __le16_to_cpu(phy->fw_offset); 4661 length = __le32_to_cpu(phy->blk_hdr.length); 4662 if (fw_offset < sizeof(*phy) || length <= fw_offset) { 4663 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4664 goto out; 4665 } 4666 4667 length -= fw_offset; 4668 if (length & 3) { 4669 dev_err(&tp->intf->dev, "invalid block length\n"); 4670 goto out; 4671 } 4672 4673 if (__le16_to_cpu(phy->fw_reg) != 0x9A00) { 4674 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 4675 goto out; 4676 } 4677 4678 rc = true; 4679 out: 4680 return rc; 4681 } 4682 4683 static bool rtl8152_is_fw_phy_ver_ok(struct r8152 *tp, struct fw_phy_ver *ver) 4684 { 4685 bool rc = false; 4686 4687 switch (tp->version) { 4688 case RTL_VER_10: 4689 case RTL_VER_11: 4690 case RTL_VER_12: 4691 case RTL_VER_13: 4692 case RTL_VER_15: 4693 break; 4694 default: 4695 goto out; 4696 } 4697 4698 if (__le32_to_cpu(ver->blk_hdr.length) != sizeof(*ver)) { 4699 dev_err(&tp->intf->dev, "invalid block length\n"); 4700 goto out; 4701 } 4702 4703 if (__le16_to_cpu(ver->ver.addr) != SRAM_GPHY_FW_VER) { 4704 dev_err(&tp->intf->dev, "invalid phy ver addr\n"); 4705 goto out; 4706 } 4707 4708 rc = true; 4709 out: 4710 return rc; 4711 } 4712 4713 static bool rtl8152_is_fw_phy_fixup_ok(struct r8152 *tp, struct fw_phy_fixup *fix) 4714 { 4715 bool rc = false; 4716 4717 switch (tp->version) { 4718 case RTL_VER_10: 4719 case RTL_VER_11: 4720 case RTL_VER_12: 4721 case RTL_VER_13: 4722 case RTL_VER_15: 4723 break; 4724 default: 4725 goto out; 4726 } 4727 4728 if (__le32_to_cpu(fix->blk_hdr.length) != sizeof(*fix)) { 4729 dev_err(&tp->intf->dev, "invalid block length\n"); 4730 goto out; 4731 } 4732 4733 if (__le16_to_cpu(fix->setting.addr) != OCP_PHY_PATCH_CMD || 4734 __le16_to_cpu(fix->setting.data) != BIT(7)) { 4735 dev_err(&tp->intf->dev, "invalid phy fixup\n"); 4736 goto out; 4737 } 4738 4739 rc = true; 4740 out: 4741 return rc; 4742 } 4743 4744 static bool rtl8152_is_fw_phy_union_ok(struct r8152 *tp, struct fw_phy_union *phy) 4745 { 4746 u16 fw_offset; 4747 u32 length; 4748 bool rc = false; 4749 4750 switch (tp->version) { 4751 case RTL_VER_10: 4752 case RTL_VER_11: 4753 case RTL_VER_12: 4754 case RTL_VER_13: 4755 case RTL_VER_15: 4756 break; 4757 default: 4758 goto out; 4759 } 4760 4761 fw_offset = __le16_to_cpu(phy->fw_offset); 4762 length = __le32_to_cpu(phy->blk_hdr.length); 4763 if (fw_offset < sizeof(*phy) || length <= fw_offset) { 4764 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4765 goto out; 4766 } 4767 4768 length -= fw_offset; 4769 if (length & 1) { 4770 dev_err(&tp->intf->dev, "invalid block length\n"); 4771 goto out; 4772 } 4773 4774 if (phy->pre_num > 2) { 4775 dev_err(&tp->intf->dev, "invalid pre_num %d\n", phy->pre_num); 4776 goto out; 4777 } 4778 4779 if (phy->bp_num > 8) { 4780 dev_err(&tp->intf->dev, "invalid bp_num %d\n", phy->bp_num); 4781 goto out; 4782 } 4783 4784 rc = true; 4785 out: 4786 return rc; 4787 } 4788 4789 static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy) 4790 { 4791 u32 length; 4792 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start; 4793 bool rc = false; 4794 4795 switch (tp->version) { 4796 case RTL_VER_04: 4797 case RTL_VER_05: 4798 case RTL_VER_06: 4799 fw_reg = 0xa014; 4800 ba_reg = 0xa012; 4801 patch_en_addr = 0xa01a; 4802 mode_reg = 0xb820; 4803 bp_start = 0xa000; 4804 break; 4805 default: 4806 goto out; 4807 } 4808 4809 fw_offset = __le16_to_cpu(phy->fw_offset); 4810 if (fw_offset < sizeof(*phy)) { 4811 dev_err(&tp->intf->dev, "fw_offset too small\n"); 4812 goto out; 4813 } 4814 4815 length = __le32_to_cpu(phy->blk_hdr.length); 4816 if (length < fw_offset) { 4817 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4818 goto out; 4819 } 4820 4821 length -= __le16_to_cpu(phy->fw_offset); 4822 if (!length || (length & 1)) { 4823 dev_err(&tp->intf->dev, "invalid block length\n"); 4824 goto out; 4825 } 4826 4827 if (__le16_to_cpu(phy->fw_reg) != fw_reg) { 4828 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 4829 goto out; 4830 } 4831 4832 if (__le16_to_cpu(phy->ba_reg) != ba_reg) { 4833 dev_err(&tp->intf->dev, "invalid base address register\n"); 4834 goto out; 4835 } 4836 4837 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) { 4838 dev_err(&tp->intf->dev, 4839 "invalid patch mode enabled register\n"); 4840 goto out; 4841 } 4842 4843 if (__le16_to_cpu(phy->mode_reg) != mode_reg) { 4844 dev_err(&tp->intf->dev, 4845 "invalid register to switch the mode\n"); 4846 goto out; 4847 } 4848 4849 if (__le16_to_cpu(phy->bp_start) != bp_start) { 4850 dev_err(&tp->intf->dev, 4851 "invalid start register of break point\n"); 4852 goto out; 4853 } 4854 4855 if (__le16_to_cpu(phy->bp_num) > 4) { 4856 dev_err(&tp->intf->dev, "invalid break point number\n"); 4857 goto out; 4858 } 4859 4860 rc = true; 4861 out: 4862 return rc; 4863 } 4864 4865 static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac) 4866 { 4867 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset; 4868 bool rc = false; 4869 u32 length, type; 4870 int i, max_bp; 4871 4872 type = __le32_to_cpu(mac->blk_hdr.type); 4873 if (type == RTL_FW_PLA) { 4874 switch (tp->version) { 4875 case RTL_VER_01: 4876 case RTL_VER_02: 4877 case RTL_VER_07: 4878 fw_reg = 0xf800; 4879 bp_ba_addr = PLA_BP_BA; 4880 bp_en_addr = 0; 4881 bp_start = PLA_BP_0; 4882 max_bp = 8; 4883 break; 4884 case RTL_VER_03: 4885 case RTL_VER_04: 4886 case RTL_VER_05: 4887 case RTL_VER_06: 4888 case RTL_VER_08: 4889 case RTL_VER_09: 4890 case RTL_VER_11: 4891 case RTL_VER_12: 4892 case RTL_VER_13: 4893 case RTL_VER_15: 4894 fw_reg = 0xf800; 4895 bp_ba_addr = PLA_BP_BA; 4896 bp_en_addr = PLA_BP_EN; 4897 bp_start = PLA_BP_0; 4898 max_bp = 8; 4899 break; 4900 case RTL_VER_14: 4901 fw_reg = 0xf800; 4902 bp_ba_addr = PLA_BP_BA; 4903 bp_en_addr = USB_BP2_EN; 4904 bp_start = PLA_BP_0; 4905 max_bp = 16; 4906 break; 4907 default: 4908 goto out; 4909 } 4910 } else if (type == RTL_FW_USB) { 4911 switch (tp->version) { 4912 case RTL_VER_03: 4913 case RTL_VER_04: 4914 case RTL_VER_05: 4915 case RTL_VER_06: 4916 fw_reg = 0xf800; 4917 bp_ba_addr = USB_BP_BA; 4918 bp_en_addr = USB_BP_EN; 4919 bp_start = USB_BP_0; 4920 max_bp = 8; 4921 break; 4922 case RTL_VER_08: 4923 case RTL_VER_09: 4924 case RTL_VER_11: 4925 case RTL_VER_12: 4926 case RTL_VER_13: 4927 case RTL_VER_14: 4928 case RTL_VER_15: 4929 fw_reg = 0xe600; 4930 bp_ba_addr = USB_BP_BA; 4931 bp_en_addr = USB_BP2_EN; 4932 bp_start = USB_BP_0; 4933 max_bp = 16; 4934 break; 4935 case RTL_VER_01: 4936 case RTL_VER_02: 4937 case RTL_VER_07: 4938 default: 4939 goto out; 4940 } 4941 } else { 4942 goto out; 4943 } 4944 4945 fw_offset = __le16_to_cpu(mac->fw_offset); 4946 if (fw_offset < sizeof(*mac)) { 4947 dev_err(&tp->intf->dev, "fw_offset too small\n"); 4948 goto out; 4949 } 4950 4951 length = __le32_to_cpu(mac->blk_hdr.length); 4952 if (length < fw_offset) { 4953 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4954 goto out; 4955 } 4956 4957 length -= fw_offset; 4958 if (length < 4 || (length & 3)) { 4959 dev_err(&tp->intf->dev, "invalid block length\n"); 4960 goto out; 4961 } 4962 4963 if (__le16_to_cpu(mac->fw_reg) != fw_reg) { 4964 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 4965 goto out; 4966 } 4967 4968 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) { 4969 dev_err(&tp->intf->dev, "invalid base address register\n"); 4970 goto out; 4971 } 4972 4973 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) { 4974 dev_err(&tp->intf->dev, "invalid enabled mask register\n"); 4975 goto out; 4976 } 4977 4978 if (__le16_to_cpu(mac->bp_start) != bp_start) { 4979 dev_err(&tp->intf->dev, 4980 "invalid start register of break point\n"); 4981 goto out; 4982 } 4983 4984 if (__le16_to_cpu(mac->bp_num) > max_bp) { 4985 dev_err(&tp->intf->dev, "invalid break point number\n"); 4986 goto out; 4987 } 4988 4989 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) { 4990 if (mac->bp[i]) { 4991 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i); 4992 goto out; 4993 } 4994 } 4995 4996 rc = true; 4997 out: 4998 return rc; 4999 } 5000 5001 /* Verify the checksum for the firmware file. It is calculated from the version 5002 * field to the end of the file. Compare the result with the checksum field to 5003 * make sure the file is correct. 5004 */ 5005 static long rtl8152_fw_verify_checksum(struct r8152 *tp, 5006 struct fw_header *fw_hdr, size_t size) 5007 { 5008 u8 checksum[sizeof(fw_hdr->checksum)]; 5009 5010 BUILD_BUG_ON(sizeof(checksum) != SHA256_DIGEST_SIZE); 5011 sha256(fw_hdr->version, size - sizeof(checksum), checksum); 5012 5013 if (memcmp(fw_hdr->checksum, checksum, sizeof(checksum))) { 5014 dev_err(&tp->intf->dev, "checksum fail\n"); 5015 return -EFAULT; 5016 } 5017 return 0; 5018 } 5019 5020 static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw) 5021 { 5022 const struct firmware *fw = rtl_fw->fw; 5023 struct fw_header *fw_hdr = (struct fw_header *)fw->data; 5024 unsigned long fw_flags = 0; 5025 long ret = -EFAULT; 5026 int i; 5027 5028 if (fw->size < sizeof(*fw_hdr)) { 5029 dev_err(&tp->intf->dev, "file too small\n"); 5030 goto fail; 5031 } 5032 5033 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size); 5034 if (ret) 5035 goto fail; 5036 5037 ret = -EFAULT; 5038 5039 for (i = sizeof(*fw_hdr); i < fw->size;) { 5040 struct fw_block *block = (struct fw_block *)&fw->data[i]; 5041 u32 type; 5042 5043 if ((i + sizeof(*block)) > fw->size) 5044 goto fail; 5045 5046 type = __le32_to_cpu(block->type); 5047 switch (type) { 5048 case RTL_FW_END: 5049 if (__le32_to_cpu(block->length) != sizeof(*block)) 5050 goto fail; 5051 goto fw_end; 5052 case RTL_FW_PLA: 5053 if (test_bit(FW_FLAGS_PLA, &fw_flags)) { 5054 dev_err(&tp->intf->dev, 5055 "multiple PLA firmware encountered"); 5056 goto fail; 5057 } 5058 5059 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) { 5060 dev_err(&tp->intf->dev, 5061 "check PLA firmware failed\n"); 5062 goto fail; 5063 } 5064 __set_bit(FW_FLAGS_PLA, &fw_flags); 5065 break; 5066 case RTL_FW_USB: 5067 if (test_bit(FW_FLAGS_USB, &fw_flags)) { 5068 dev_err(&tp->intf->dev, 5069 "multiple USB firmware encountered"); 5070 goto fail; 5071 } 5072 5073 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) { 5074 dev_err(&tp->intf->dev, 5075 "check USB firmware failed\n"); 5076 goto fail; 5077 } 5078 __set_bit(FW_FLAGS_USB, &fw_flags); 5079 break; 5080 case RTL_FW_PHY_START: 5081 if (test_bit(FW_FLAGS_START, &fw_flags) || 5082 test_bit(FW_FLAGS_NC, &fw_flags) || 5083 test_bit(FW_FLAGS_NC1, &fw_flags) || 5084 test_bit(FW_FLAGS_NC2, &fw_flags) || 5085 test_bit(FW_FLAGS_UC2, &fw_flags) || 5086 test_bit(FW_FLAGS_UC, &fw_flags) || 5087 test_bit(FW_FLAGS_STOP, &fw_flags)) { 5088 dev_err(&tp->intf->dev, 5089 "check PHY_START fail\n"); 5090 goto fail; 5091 } 5092 5093 if (__le32_to_cpu(block->length) != sizeof(struct fw_phy_patch_key)) { 5094 dev_err(&tp->intf->dev, 5095 "Invalid length for PHY_START\n"); 5096 goto fail; 5097 } 5098 __set_bit(FW_FLAGS_START, &fw_flags); 5099 break; 5100 case RTL_FW_PHY_STOP: 5101 if (test_bit(FW_FLAGS_STOP, &fw_flags) || 5102 !test_bit(FW_FLAGS_START, &fw_flags)) { 5103 dev_err(&tp->intf->dev, 5104 "Check PHY_STOP fail\n"); 5105 goto fail; 5106 } 5107 5108 if (__le32_to_cpu(block->length) != sizeof(*block)) { 5109 dev_err(&tp->intf->dev, 5110 "Invalid length for PHY_STOP\n"); 5111 goto fail; 5112 } 5113 __set_bit(FW_FLAGS_STOP, &fw_flags); 5114 break; 5115 case RTL_FW_PHY_NC: 5116 if (!test_bit(FW_FLAGS_START, &fw_flags) || 5117 test_bit(FW_FLAGS_STOP, &fw_flags)) { 5118 dev_err(&tp->intf->dev, 5119 "check PHY_NC fail\n"); 5120 goto fail; 5121 } 5122 5123 if (test_bit(FW_FLAGS_NC, &fw_flags)) { 5124 dev_err(&tp->intf->dev, 5125 "multiple PHY NC encountered\n"); 5126 goto fail; 5127 } 5128 5129 if (!rtl8152_is_fw_phy_nc_ok(tp, (struct fw_phy_nc *)block)) { 5130 dev_err(&tp->intf->dev, 5131 "check PHY NC firmware failed\n"); 5132 goto fail; 5133 } 5134 __set_bit(FW_FLAGS_NC, &fw_flags); 5135 break; 5136 case RTL_FW_PHY_UNION_NC: 5137 if (!test_bit(FW_FLAGS_START, &fw_flags) || 5138 test_bit(FW_FLAGS_NC1, &fw_flags) || 5139 test_bit(FW_FLAGS_NC2, &fw_flags) || 5140 test_bit(FW_FLAGS_UC2, &fw_flags) || 5141 test_bit(FW_FLAGS_UC, &fw_flags) || 5142 test_bit(FW_FLAGS_STOP, &fw_flags)) { 5143 dev_err(&tp->intf->dev, "PHY_UNION_NC out of order\n"); 5144 goto fail; 5145 } 5146 5147 if (test_bit(FW_FLAGS_NC, &fw_flags)) { 5148 dev_err(&tp->intf->dev, "multiple PHY_UNION_NC encountered\n"); 5149 goto fail; 5150 } 5151 5152 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 5153 dev_err(&tp->intf->dev, "check PHY_UNION_NC failed\n"); 5154 goto fail; 5155 } 5156 __set_bit(FW_FLAGS_NC, &fw_flags); 5157 break; 5158 case RTL_FW_PHY_UNION_NC1: 5159 if (!test_bit(FW_FLAGS_START, &fw_flags) || 5160 test_bit(FW_FLAGS_NC2, &fw_flags) || 5161 test_bit(FW_FLAGS_UC2, &fw_flags) || 5162 test_bit(FW_FLAGS_UC, &fw_flags) || 5163 test_bit(FW_FLAGS_STOP, &fw_flags)) { 5164 dev_err(&tp->intf->dev, "PHY_UNION_NC1 out of order\n"); 5165 goto fail; 5166 } 5167 5168 if (test_bit(FW_FLAGS_NC1, &fw_flags)) { 5169 dev_err(&tp->intf->dev, "multiple PHY NC1 encountered\n"); 5170 goto fail; 5171 } 5172 5173 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 5174 dev_err(&tp->intf->dev, "check PHY_UNION_NC1 failed\n"); 5175 goto fail; 5176 } 5177 __set_bit(FW_FLAGS_NC1, &fw_flags); 5178 break; 5179 case RTL_FW_PHY_UNION_NC2: 5180 if (!test_bit(FW_FLAGS_START, &fw_flags) || 5181 test_bit(FW_FLAGS_UC2, &fw_flags) || 5182 test_bit(FW_FLAGS_UC, &fw_flags) || 5183 test_bit(FW_FLAGS_STOP, &fw_flags)) { 5184 dev_err(&tp->intf->dev, "PHY_UNION_NC2 out of order\n"); 5185 goto fail; 5186 } 5187 5188 if (test_bit(FW_FLAGS_NC2, &fw_flags)) { 5189 dev_err(&tp->intf->dev, "multiple PHY NC2 encountered\n"); 5190 goto fail; 5191 } 5192 5193 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 5194 dev_err(&tp->intf->dev, "check PHY_UNION_NC2 failed\n"); 5195 goto fail; 5196 } 5197 __set_bit(FW_FLAGS_NC2, &fw_flags); 5198 break; 5199 case RTL_FW_PHY_UNION_UC2: 5200 if (!test_bit(FW_FLAGS_START, &fw_flags) || 5201 test_bit(FW_FLAGS_UC, &fw_flags) || 5202 test_bit(FW_FLAGS_STOP, &fw_flags)) { 5203 dev_err(&tp->intf->dev, "PHY_UNION_UC2 out of order\n"); 5204 goto fail; 5205 } 5206 5207 if (test_bit(FW_FLAGS_UC2, &fw_flags)) { 5208 dev_err(&tp->intf->dev, "multiple PHY UC2 encountered\n"); 5209 goto fail; 5210 } 5211 5212 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 5213 dev_err(&tp->intf->dev, "check PHY_UNION_UC2 failed\n"); 5214 goto fail; 5215 } 5216 __set_bit(FW_FLAGS_UC2, &fw_flags); 5217 break; 5218 case RTL_FW_PHY_UNION_UC: 5219 if (!test_bit(FW_FLAGS_START, &fw_flags) || 5220 test_bit(FW_FLAGS_STOP, &fw_flags)) { 5221 dev_err(&tp->intf->dev, "PHY_UNION_UC out of order\n"); 5222 goto fail; 5223 } 5224 5225 if (test_bit(FW_FLAGS_UC, &fw_flags)) { 5226 dev_err(&tp->intf->dev, "multiple PHY UC encountered\n"); 5227 goto fail; 5228 } 5229 5230 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 5231 dev_err(&tp->intf->dev, "check PHY_UNION_UC failed\n"); 5232 goto fail; 5233 } 5234 __set_bit(FW_FLAGS_UC, &fw_flags); 5235 break; 5236 case RTL_FW_PHY_UNION_MISC: 5237 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 5238 dev_err(&tp->intf->dev, "check RTL_FW_PHY_UNION_MISC failed\n"); 5239 goto fail; 5240 } 5241 break; 5242 case RTL_FW_PHY_FIXUP: 5243 if (!rtl8152_is_fw_phy_fixup_ok(tp, (struct fw_phy_fixup *)block)) { 5244 dev_err(&tp->intf->dev, "check PHY fixup failed\n"); 5245 goto fail; 5246 } 5247 break; 5248 case RTL_FW_PHY_SPEED_UP: 5249 if (test_bit(FW_FLAGS_SPEED_UP, &fw_flags)) { 5250 dev_err(&tp->intf->dev, "multiple PHY firmware encountered"); 5251 goto fail; 5252 } 5253 5254 if (!rtl8152_is_fw_phy_speed_up_ok(tp, (struct fw_phy_speed_up *)block)) { 5255 dev_err(&tp->intf->dev, "check PHY speed up failed\n"); 5256 goto fail; 5257 } 5258 __set_bit(FW_FLAGS_SPEED_UP, &fw_flags); 5259 break; 5260 case RTL_FW_PHY_VER: 5261 if (test_bit(FW_FLAGS_START, &fw_flags) || 5262 test_bit(FW_FLAGS_NC, &fw_flags) || 5263 test_bit(FW_FLAGS_NC1, &fw_flags) || 5264 test_bit(FW_FLAGS_NC2, &fw_flags) || 5265 test_bit(FW_FLAGS_UC2, &fw_flags) || 5266 test_bit(FW_FLAGS_UC, &fw_flags) || 5267 test_bit(FW_FLAGS_STOP, &fw_flags)) { 5268 dev_err(&tp->intf->dev, "Invalid order to set PHY version\n"); 5269 goto fail; 5270 } 5271 5272 if (test_bit(FW_FLAGS_VER, &fw_flags)) { 5273 dev_err(&tp->intf->dev, "multiple PHY version encountered"); 5274 goto fail; 5275 } 5276 5277 if (!rtl8152_is_fw_phy_ver_ok(tp, (struct fw_phy_ver *)block)) { 5278 dev_err(&tp->intf->dev, "check PHY version failed\n"); 5279 goto fail; 5280 } 5281 __set_bit(FW_FLAGS_VER, &fw_flags); 5282 break; 5283 default: 5284 dev_warn(&tp->intf->dev, "Unknown type %u is found\n", 5285 type); 5286 break; 5287 } 5288 5289 /* next block */ 5290 i += ALIGN(__le32_to_cpu(block->length), 8); 5291 } 5292 5293 fw_end: 5294 if (test_bit(FW_FLAGS_START, &fw_flags) && !test_bit(FW_FLAGS_STOP, &fw_flags)) { 5295 dev_err(&tp->intf->dev, "without PHY_STOP\n"); 5296 goto fail; 5297 } 5298 5299 return 0; 5300 fail: 5301 return ret; 5302 } 5303 5304 static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy, bool wait) 5305 { 5306 u32 len; 5307 u8 *data; 5308 5309 rtl_reset_ocp_base(tp); 5310 5311 if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) { 5312 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n"); 5313 return; 5314 } 5315 5316 len = __le32_to_cpu(phy->blk_hdr.length); 5317 len -= __le16_to_cpu(phy->fw_offset); 5318 data = (u8 *)phy + __le16_to_cpu(phy->fw_offset); 5319 5320 if (rtl_phy_patch_request(tp, true, wait)) 5321 return; 5322 5323 while (len) { 5324 u32 size; 5325 int i; 5326 5327 if (len < 2048) 5328 size = len; 5329 else 5330 size = 2048; 5331 5332 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_GPHY_CTRL, 5333 GPHY_PATCH_DONE | BACKUP_RESTRORE); 5334 5335 generic_ocp_write(tp, __le16_to_cpu(phy->fw_reg), 0xff, size, data, MCU_TYPE_USB); 5336 5337 data += size; 5338 len -= size; 5339 5340 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL, 5341 POL_GPHY_PATCH); 5342 5343 for (i = 0; i < 1000; i++) { 5344 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & POL_GPHY_PATCH)) 5345 break; 5346 } 5347 5348 if (i == 1000) { 5349 dev_err(&tp->intf->dev, "ram code speedup mode timeout\n"); 5350 break; 5351 } 5352 } 5353 5354 rtl_reset_ocp_base(tp); 5355 5356 rtl_phy_patch_request(tp, false, wait); 5357 5358 if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version)) 5359 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 5360 else 5361 dev_err(&tp->intf->dev, "ram code speedup mode fail\n"); 5362 } 5363 5364 static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver) 5365 { 5366 u16 ver_addr, ver; 5367 5368 ver_addr = __le16_to_cpu(phy_ver->ver.addr); 5369 ver = __le16_to_cpu(phy_ver->ver.data); 5370 5371 rtl_reset_ocp_base(tp); 5372 5373 if (sram_read(tp, ver_addr) >= ver) { 5374 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n"); 5375 return 0; 5376 } 5377 5378 sram_write(tp, ver_addr, ver); 5379 5380 dev_dbg(&tp->intf->dev, "PHY firmware version %x\n", ver); 5381 5382 return ver; 5383 } 5384 5385 static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix) 5386 { 5387 u16 addr, data; 5388 5389 rtl_reset_ocp_base(tp); 5390 5391 addr = __le16_to_cpu(fix->setting.addr); 5392 data = ocp_reg_read(tp, addr); 5393 5394 switch (__le16_to_cpu(fix->bit_cmd)) { 5395 case FW_FIXUP_AND: 5396 data &= __le16_to_cpu(fix->setting.data); 5397 break; 5398 case FW_FIXUP_OR: 5399 data |= __le16_to_cpu(fix->setting.data); 5400 break; 5401 case FW_FIXUP_NOT: 5402 data &= ~__le16_to_cpu(fix->setting.data); 5403 break; 5404 case FW_FIXUP_XOR: 5405 data ^= __le16_to_cpu(fix->setting.data); 5406 break; 5407 default: 5408 return; 5409 } 5410 5411 ocp_reg_write(tp, addr, data); 5412 5413 dev_dbg(&tp->intf->dev, "applied ocp %x %x\n", addr, data); 5414 } 5415 5416 static void rtl8152_fw_phy_union_apply(struct r8152 *tp, struct fw_phy_union *phy) 5417 { 5418 __le16 *data; 5419 u32 length; 5420 int i, num; 5421 5422 rtl_reset_ocp_base(tp); 5423 5424 num = phy->pre_num; 5425 for (i = 0; i < num; i++) 5426 sram_write(tp, __le16_to_cpu(phy->pre_set[i].addr), 5427 __le16_to_cpu(phy->pre_set[i].data)); 5428 5429 length = __le32_to_cpu(phy->blk_hdr.length); 5430 length -= __le16_to_cpu(phy->fw_offset); 5431 num = length / 2; 5432 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset)); 5433 5434 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg)); 5435 for (i = 0; i < num; i++) 5436 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i])); 5437 5438 num = phy->bp_num; 5439 for (i = 0; i < num; i++) 5440 sram_write(tp, __le16_to_cpu(phy->bp[i].addr), __le16_to_cpu(phy->bp[i].data)); 5441 5442 if (phy->bp_num && phy->bp_en.addr) 5443 sram_write(tp, __le16_to_cpu(phy->bp_en.addr), __le16_to_cpu(phy->bp_en.data)); 5444 5445 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 5446 } 5447 5448 static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy) 5449 { 5450 u16 mode_reg, bp_index; 5451 u32 length, i, num; 5452 __le16 *data; 5453 5454 rtl_reset_ocp_base(tp); 5455 5456 mode_reg = __le16_to_cpu(phy->mode_reg); 5457 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre)); 5458 sram_write(tp, __le16_to_cpu(phy->ba_reg), 5459 __le16_to_cpu(phy->ba_data)); 5460 5461 length = __le32_to_cpu(phy->blk_hdr.length); 5462 length -= __le16_to_cpu(phy->fw_offset); 5463 num = length / 2; 5464 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset)); 5465 5466 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg)); 5467 for (i = 0; i < num; i++) 5468 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i])); 5469 5470 sram_write(tp, __le16_to_cpu(phy->patch_en_addr), 5471 __le16_to_cpu(phy->patch_en_value)); 5472 5473 bp_index = __le16_to_cpu(phy->bp_start); 5474 num = __le16_to_cpu(phy->bp_num); 5475 for (i = 0; i < num; i++) { 5476 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i])); 5477 bp_index += 2; 5478 } 5479 5480 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post)); 5481 5482 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 5483 } 5484 5485 static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac) 5486 { 5487 u16 bp_en_addr, type, fw_ver_reg; 5488 u32 length; 5489 u8 *data; 5490 5491 switch (__le32_to_cpu(mac->blk_hdr.type)) { 5492 case RTL_FW_PLA: 5493 type = MCU_TYPE_PLA; 5494 break; 5495 case RTL_FW_USB: 5496 type = MCU_TYPE_USB; 5497 break; 5498 default: 5499 return; 5500 } 5501 5502 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg); 5503 if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) { 5504 dev_dbg(&tp->intf->dev, "%s firmware has been the newest\n", type ? "PLA" : "USB"); 5505 return; 5506 } 5507 5508 rtl_clear_bp(tp, type); 5509 5510 /* Enable backup/restore of MACDBG. This is required after clearing PLA 5511 * break points and before applying the PLA firmware. 5512 */ 5513 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA && 5514 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) { 5515 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM); 5516 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM); 5517 } 5518 5519 length = __le32_to_cpu(mac->blk_hdr.length); 5520 length -= __le16_to_cpu(mac->fw_offset); 5521 5522 data = (u8 *)mac; 5523 data += __le16_to_cpu(mac->fw_offset); 5524 5525 if (generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, 5526 data, type) < 0) { 5527 dev_err(&tp->intf->dev, "Write %s fw fail\n", 5528 type ? "PLA" : "USB"); 5529 return; 5530 } 5531 5532 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr), 5533 __le16_to_cpu(mac->bp_ba_value)); 5534 5535 if (generic_ocp_write(tp, __le16_to_cpu(mac->bp_start), BYTE_EN_DWORD, 5536 ALIGN(__le16_to_cpu(mac->bp_num) << 1, 4), 5537 mac->bp, type) < 0) { 5538 dev_err(&tp->intf->dev, "Write %s bp fail\n", 5539 type ? "PLA" : "USB"); 5540 return; 5541 } 5542 5543 bp_en_addr = __le16_to_cpu(mac->bp_en_addr); 5544 if (bp_en_addr) 5545 ocp_write_word(tp, type, bp_en_addr, 5546 __le16_to_cpu(mac->bp_en_value)); 5547 5548 if (fw_ver_reg) 5549 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg, 5550 mac->fw_ver_data); 5551 5552 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info); 5553 } 5554 5555 static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut) 5556 { 5557 struct rtl_fw *rtl_fw = &tp->rtl_fw; 5558 const struct firmware *fw; 5559 struct fw_header *fw_hdr; 5560 struct fw_phy_patch_key *key; 5561 u16 key_addr = 0; 5562 int i, patch_phy = 1; 5563 5564 if (IS_ERR_OR_NULL(rtl_fw->fw)) 5565 return; 5566 5567 fw = rtl_fw->fw; 5568 fw_hdr = (struct fw_header *)fw->data; 5569 5570 if (rtl_fw->pre_fw) 5571 rtl_fw->pre_fw(tp); 5572 5573 for (i = offsetof(struct fw_header, blocks); i < fw->size;) { 5574 struct fw_block *block = (struct fw_block *)&fw->data[i]; 5575 5576 switch (__le32_to_cpu(block->type)) { 5577 case RTL_FW_END: 5578 goto post_fw; 5579 case RTL_FW_PLA: 5580 case RTL_FW_USB: 5581 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block); 5582 break; 5583 case RTL_FW_PHY_START: 5584 if (!patch_phy) 5585 break; 5586 key = (struct fw_phy_patch_key *)block; 5587 key_addr = __le16_to_cpu(key->key_reg); 5588 rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut); 5589 break; 5590 case RTL_FW_PHY_STOP: 5591 if (!patch_phy) 5592 break; 5593 WARN_ON(!key_addr); 5594 rtl_post_ram_code(tp, key_addr, !power_cut); 5595 break; 5596 case RTL_FW_PHY_NC: 5597 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block); 5598 break; 5599 case RTL_FW_PHY_VER: 5600 patch_phy = rtl8152_fw_phy_ver(tp, (struct fw_phy_ver *)block); 5601 break; 5602 case RTL_FW_PHY_UNION_NC: 5603 case RTL_FW_PHY_UNION_NC1: 5604 case RTL_FW_PHY_UNION_NC2: 5605 case RTL_FW_PHY_UNION_UC2: 5606 case RTL_FW_PHY_UNION_UC: 5607 case RTL_FW_PHY_UNION_MISC: 5608 if (patch_phy) 5609 rtl8152_fw_phy_union_apply(tp, (struct fw_phy_union *)block); 5610 break; 5611 case RTL_FW_PHY_FIXUP: 5612 if (patch_phy) 5613 rtl8152_fw_phy_fixup(tp, (struct fw_phy_fixup *)block); 5614 break; 5615 case RTL_FW_PHY_SPEED_UP: 5616 rtl_ram_code_speed_up(tp, (struct fw_phy_speed_up *)block, !power_cut); 5617 break; 5618 default: 5619 break; 5620 } 5621 5622 i += ALIGN(__le32_to_cpu(block->length), 8); 5623 } 5624 5625 post_fw: 5626 if (rtl_fw->post_fw) 5627 rtl_fw->post_fw(tp); 5628 5629 rtl_reset_ocp_base(tp); 5630 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE); 5631 dev_dbg(&tp->intf->dev, "load %s successfully\n", rtl_fw->version); 5632 } 5633 5634 static void rtl8152_release_firmware(struct r8152 *tp) 5635 { 5636 struct rtl_fw *rtl_fw = &tp->rtl_fw; 5637 5638 if (!IS_ERR_OR_NULL(rtl_fw->fw)) { 5639 release_firmware(rtl_fw->fw); 5640 rtl_fw->fw = NULL; 5641 } 5642 } 5643 5644 static int rtl8152_request_firmware(struct r8152 *tp) 5645 { 5646 struct rtl_fw *rtl_fw = &tp->rtl_fw; 5647 long rc; 5648 5649 if (rtl_fw->fw || !rtl_fw->fw_name) { 5650 dev_info(&tp->intf->dev, "skip request firmware\n"); 5651 rc = 0; 5652 goto result; 5653 } 5654 5655 rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev); 5656 if (rc < 0) 5657 goto result; 5658 5659 rc = rtl8152_check_firmware(tp, rtl_fw); 5660 if (rc < 0) 5661 release_firmware(rtl_fw->fw); 5662 5663 result: 5664 if (rc) { 5665 rtl_fw->fw = ERR_PTR(rc); 5666 5667 dev_warn(&tp->intf->dev, 5668 "unable to load firmware patch %s (%ld)\n", 5669 rtl_fw->fw_name, rc); 5670 } 5671 5672 return rc; 5673 } 5674 5675 static void r8152_aldps_en(struct r8152 *tp, bool enable) 5676 { 5677 if (enable) { 5678 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | 5679 LINKENA | DIS_SDSAVE); 5680 } else { 5681 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | 5682 DIS_SDSAVE); 5683 msleep(20); 5684 } 5685 } 5686 5687 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg) 5688 { 5689 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev); 5690 ocp_reg_write(tp, OCP_EEE_DATA, reg); 5691 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev); 5692 } 5693 5694 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg) 5695 { 5696 u16 data; 5697 5698 r8152_mmd_indirect(tp, dev, reg); 5699 data = ocp_reg_read(tp, OCP_EEE_DATA); 5700 ocp_reg_write(tp, OCP_EEE_AR, 0x0000); 5701 5702 return data; 5703 } 5704 5705 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data) 5706 { 5707 r8152_mmd_indirect(tp, dev, reg); 5708 ocp_reg_write(tp, OCP_EEE_DATA, data); 5709 ocp_reg_write(tp, OCP_EEE_AR, 0x0000); 5710 } 5711 5712 static void r8152_eee_en(struct r8152 *tp, bool enable) 5713 { 5714 if (enable) { 5715 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_EEE_CR, 5716 EEE_RX_EN | EEE_TX_EN); 5717 5718 ocp_reg_w0w1(tp, OCP_EEE_CONFIG1, sd_rise_time_mask, 5719 EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | 5720 RX_QUIET_EN | sd_rise_time(1)); 5721 5722 ocp_reg_set_bits(tp, OCP_EEE_CONFIG2, 5723 RG_DACQUIET_EN | RG_LDVQUIET_EN); 5724 5725 ocp_reg_w0w1(tp, OCP_EEE_CONFIG3, fast_snr_mask, fast_snr(42)); 5726 } else { 5727 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_EEE_CR, 5728 EEE_RX_EN | EEE_TX_EN); 5729 5730 ocp_reg_w0w1(tp, OCP_EEE_CONFIG1, sd_rise_time_mask | 5731 EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | 5732 RX_QUIET_EN, sd_rise_time(7)); 5733 5734 ocp_reg_clr_bits(tp, OCP_EEE_CONFIG2, 5735 RG_DACQUIET_EN | RG_LDVQUIET_EN); 5736 5737 ocp_reg_w0w1(tp, OCP_EEE_CONFIG3, fast_snr_mask, fast_snr(511)); 5738 } 5739 } 5740 5741 static void r8153_eee_en(struct r8152 *tp, bool enable) 5742 { 5743 if (enable) { 5744 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_EEE_CR, 5745 EEE_RX_EN | EEE_TX_EN); 5746 5747 ocp_reg_set_bits(tp, OCP_EEE_CFG, EEE10_EN); 5748 } else { 5749 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_EEE_CR, 5750 EEE_RX_EN | EEE_TX_EN); 5751 5752 ocp_reg_clr_bits(tp, OCP_EEE_CFG, EEE10_EN); 5753 } 5754 5755 tp->ups_info.eee = enable; 5756 } 5757 5758 static void r8156_eee_en(struct r8152 *tp, bool enable) 5759 { 5760 u16 config; 5761 5762 r8153_eee_en(tp, enable); 5763 5764 config = ocp_reg_read(tp, OCP_EEE_ADV2); 5765 5766 if (enable && (tp->eee_adv2 & MDIO_EEE_2_5GT)) 5767 config |= MDIO_EEE_2_5GT; 5768 else 5769 config &= ~MDIO_EEE_2_5GT; 5770 5771 if (enable && (tp->eee_adv2 & MDIO_EEE_5GT)) 5772 config |= MDIO_EEE_5GT; 5773 else 5774 config &= ~MDIO_EEE_5GT; 5775 5776 ocp_reg_write(tp, OCP_EEE_ADV2, config); 5777 } 5778 5779 static void rtl_eee_enable(struct r8152 *tp, bool enable) 5780 { 5781 switch (tp->version) { 5782 case RTL_VER_01: 5783 case RTL_VER_02: 5784 case RTL_VER_07: 5785 if (enable) { 5786 r8152_eee_en(tp, true); 5787 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 5788 tp->eee_adv); 5789 } else { 5790 r8152_eee_en(tp, false); 5791 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0); 5792 } 5793 break; 5794 case RTL_VER_03: 5795 case RTL_VER_04: 5796 case RTL_VER_05: 5797 case RTL_VER_06: 5798 case RTL_VER_08: 5799 case RTL_VER_09: 5800 case RTL_VER_14: 5801 if (enable) { 5802 r8153_eee_en(tp, true); 5803 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); 5804 } else { 5805 r8153_eee_en(tp, false); 5806 ocp_reg_write(tp, OCP_EEE_ADV, 0); 5807 } 5808 break; 5809 case RTL_VER_10: 5810 case RTL_VER_11: 5811 case RTL_VER_12: 5812 case RTL_VER_13: 5813 case RTL_VER_15: 5814 case RTL_VER_16: 5815 if (enable) { 5816 r8156_eee_en(tp, true); 5817 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); 5818 } else { 5819 r8156_eee_en(tp, false); 5820 ocp_reg_write(tp, OCP_EEE_ADV, 0); 5821 } 5822 break; 5823 default: 5824 break; 5825 } 5826 } 5827 5828 static void r8152b_enable_fc(struct r8152 *tp) 5829 { 5830 r8152_mdio_set_bit(tp, MII_ADVERTISE, 5831 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM); 5832 5833 tp->ups_info.flow_control = true; 5834 } 5835 5836 static void rtl8152_disable(struct r8152 *tp) 5837 { 5838 r8152_aldps_en(tp, false); 5839 rtl_disable(tp); 5840 r8152_aldps_en(tp, true); 5841 } 5842 5843 static void r8152b_hw_phy_cfg(struct r8152 *tp) 5844 { 5845 rtl8152_apply_firmware(tp, false); 5846 rtl_eee_enable(tp, tp->eee_en); 5847 r8152_aldps_en(tp, true); 5848 r8152b_enable_fc(tp); 5849 5850 set_bit(PHY_RESET, &tp->flags); 5851 } 5852 5853 static void wait_oob_link_list_ready(struct r8152 *tp) 5854 { 5855 u32 ocp_data; 5856 int i; 5857 5858 for (i = 0; i < 1000; i++) { 5859 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 5860 break; 5861 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5862 if (ocp_data & LINK_LIST_READY) 5863 break; 5864 usleep_range(1000, 2000); 5865 } 5866 } 5867 5868 static void r8156b_wait_loading_flash(struct r8152 *tp) 5869 { 5870 if ((ocp_read_word(tp, MCU_TYPE_PLA, PLA_GPHY_CTRL) & GPHY_FLASH) && 5871 !(ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & BYPASS_FLASH)) { 5872 int i; 5873 5874 for (i = 0; i < 100; i++) { 5875 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 5876 break; 5877 if (ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & GPHY_PATCH_DONE) 5878 break; 5879 usleep_range(1000, 2000); 5880 } 5881 } 5882 } 5883 5884 static void r8152b_exit_oob(struct r8152 *tp) 5885 { 5886 ocp_dword_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, RCR_ACPT_ALL); 5887 5888 rxdy_gated_en(tp, true); 5889 r8153_teredo_off(tp); 5890 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 5891 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); 5892 5893 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB); 5894 5895 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, MCU_BORW_EN); 5896 5897 wait_oob_link_list_ready(tp); 5898 5899 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, RE_INIT_LL); 5900 5901 wait_oob_link_list_ready(tp); 5902 5903 rtl8152_nic_reset(tp); 5904 5905 /* rx share fifo credit full threshold */ 5906 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); 5907 5908 if (tp->udev->speed == USB_SPEED_FULL || 5909 tp->udev->speed == USB_SPEED_LOW) { 5910 /* rx share fifo credit near full threshold */ 5911 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 5912 RXFIFO_THR2_FULL); 5913 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 5914 RXFIFO_THR3_FULL); 5915 } else { 5916 /* rx share fifo credit near full threshold */ 5917 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 5918 RXFIFO_THR2_HIGH); 5919 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 5920 RXFIFO_THR3_HIGH); 5921 } 5922 5923 /* TX share fifo free credit full threshold */ 5924 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); 5925 5926 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); 5927 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH); 5928 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, 5929 TEST_MODE_DISABLE | TX_SIZE_ADJUST1); 5930 5931 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 5932 5933 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 5934 5935 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_TCR0, TCR0_AUTO_FIFO); 5936 } 5937 5938 static void r8152b_enter_oob(struct r8152 *tp) 5939 { 5940 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB); 5941 5942 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); 5943 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); 5944 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); 5945 5946 rtl_disable(tp); 5947 5948 wait_oob_link_list_ready(tp); 5949 5950 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, RE_INIT_LL); 5951 5952 wait_oob_link_list_ready(tp); 5953 5954 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 5955 5956 rtl_rx_vlan_en(tp, true); 5957 5958 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_BDC_CR, ALDPS_PROXY_MODE); 5959 5960 ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, 5961 NOW_IS_OOB | DIS_MCU_CLROOB); 5962 5963 rxdy_gated_en(tp, false); 5964 5965 ocp_dword_set_bits(tp, MCU_TYPE_PLA, PLA_RCR, 5966 RCR_APM | RCR_AM | RCR_AB); 5967 } 5968 5969 static int r8153_pre_firmware_1(struct r8152 *tp) 5970 { 5971 int i; 5972 5973 /* Wait till the WTD timer is ready. It would take at most 104 ms. */ 5974 for (i = 0; i < 104; i++) { 5975 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL); 5976 5977 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 5978 return -ENODEV; 5979 if (!(ocp_data & WTD1_EN)) 5980 break; 5981 usleep_range(1000, 2000); 5982 } 5983 5984 return 0; 5985 } 5986 5987 static int r8153_post_firmware_1(struct r8152 *tp) 5988 { 5989 /* set USB_BP_4 to support USB_SPEED_SUPER only */ 5990 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) 5991 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY); 5992 5993 /* reset UPHY timer to 36 ms */ 5994 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); 5995 5996 return 0; 5997 } 5998 5999 static int r8153_pre_firmware_2(struct r8152 *tp) 6000 { 6001 r8153_pre_firmware_1(tp); 6002 6003 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, FW_FIX_SUSPEND); 6004 6005 return 0; 6006 } 6007 6008 static int r8153_post_firmware_2(struct r8152 *tp) 6009 { 6010 /* enable bp0 if support USB_SPEED_SUPER only */ 6011 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) 6012 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_BP_EN, BIT(0)); 6013 6014 /* reset UPHY timer to 36 ms */ 6015 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); 6016 6017 /* enable U3P3 check, set the counter to 4 */ 6018 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4); 6019 6020 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, FW_FIX_SUSPEND); 6021 6022 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_USB2PHY, 6023 USB2PHY_L1 | USB2PHY_SUSPEND); 6024 6025 return 0; 6026 } 6027 6028 static int r8153_post_firmware_3(struct r8152 *tp) 6029 { 6030 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_USB2PHY, 6031 USB2PHY_L1 | USB2PHY_SUSPEND); 6032 6033 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, FW_IP_RESET_EN); 6034 6035 return 0; 6036 } 6037 6038 static int r8153b_pre_firmware_1(struct r8152 *tp) 6039 { 6040 /* enable fc timer and set timer to 1 second. */ 6041 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, 6042 CTRL_TIMER_EN | (1000 / 8)); 6043 6044 return 0; 6045 } 6046 6047 static int r8153b_post_firmware_1(struct r8152 *tp) 6048 { 6049 u32 ocp_data; 6050 6051 /* enable bp0 for RTL8153-BND */ 6052 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); 6053 if (ocp_data & BND_MASK) 6054 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_BP_EN, BIT(0)); 6055 6056 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_CTRL, FLOW_CTRL_PATCH_OPT); 6057 6058 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK); 6059 6060 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, FW_IP_RESET_EN); 6061 6062 return 0; 6063 } 6064 6065 static int r8153c_post_firmware_1(struct r8152 *tp) 6066 { 6067 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_CTRL, FLOW_CTRL_PATCH_2); 6068 6069 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK); 6070 6071 return 0; 6072 } 6073 6074 static int r8156a_post_firmware_1(struct r8152 *tp) 6075 { 6076 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, FW_IP_RESET_EN); 6077 6078 /* Modify U3PHY parameter for compatibility issue */ 6079 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4026840e); 6080 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4001acc9); 6081 6082 return 0; 6083 } 6084 6085 static void r8153_aldps_en(struct r8152 *tp, bool enable) 6086 { 6087 if (enable) { 6088 ocp_reg_set_bits(tp, OCP_POWER_CFG, EN_ALDPS); 6089 } else { 6090 int i; 6091 6092 ocp_reg_clr_bits(tp, OCP_POWER_CFG, EN_ALDPS); 6093 for (i = 0; i < 20; i++) { 6094 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6095 return; 6096 usleep_range(1000, 2000); 6097 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100) 6098 break; 6099 } 6100 } 6101 6102 tp->ups_info.aldps = enable; 6103 } 6104 6105 static void r8153_hw_phy_cfg(struct r8152 *tp) 6106 { 6107 /* disable ALDPS before updating the PHY parameters */ 6108 r8153_aldps_en(tp, false); 6109 6110 /* disable EEE before updating the PHY parameters */ 6111 rtl_eee_enable(tp, false); 6112 6113 rtl8152_apply_firmware(tp, false); 6114 6115 if (tp->version == RTL_VER_03) 6116 ocp_reg_clr_bits(tp, OCP_EEE_CFG, CTAP_SHORT_EN); 6117 6118 ocp_reg_set_bits(tp, OCP_POWER_CFG, EEE_CLKDIV_EN); 6119 6120 ocp_reg_set_bits(tp, OCP_DOWN_SPEED, EN_10M_BGOFF); 6121 6122 ocp_reg_set_bits(tp, OCP_POWER_CFG, EN_10M_PLLOFF); 6123 6124 sram_write(tp, SRAM_IMPEDANCE, 0x0b13); 6125 6126 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_PHY_PWR, PFM_PWM_SWITCH); 6127 6128 /* Enable LPF corner auto tune */ 6129 sram_write(tp, SRAM_LPF_CFG, 0xf70f); 6130 6131 /* Adjust 10M Amplitude */ 6132 sram_write(tp, SRAM_10M_AMP1, 0x00af); 6133 sram_write(tp, SRAM_10M_AMP2, 0x0208); 6134 6135 if (tp->eee_en) 6136 rtl_eee_enable(tp, true); 6137 6138 r8153_aldps_en(tp, true); 6139 r8152b_enable_fc(tp); 6140 6141 switch (tp->version) { 6142 case RTL_VER_03: 6143 case RTL_VER_04: 6144 break; 6145 case RTL_VER_05: 6146 case RTL_VER_06: 6147 default: 6148 r8153_u2p3en(tp, true); 6149 break; 6150 } 6151 6152 set_bit(PHY_RESET, &tp->flags); 6153 } 6154 6155 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr) 6156 { 6157 u32 ocp_data; 6158 6159 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr); 6160 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD); 6161 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */ 6162 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA); 6163 6164 return ocp_data; 6165 } 6166 6167 static void r8153b_hw_phy_cfg(struct r8152 *tp) 6168 { 6169 u32 ocp_data; 6170 u16 data; 6171 6172 ocp_word_test_and_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS); 6173 6174 /* disable ALDPS before updating the PHY parameters */ 6175 r8153_aldps_en(tp, false); 6176 6177 /* disable EEE before updating the PHY parameters */ 6178 rtl_eee_enable(tp, false); 6179 6180 /* U1/U2/L1 idle timer. 500 us */ 6181 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); 6182 6183 data = r8153_phy_status(tp, 0); 6184 6185 switch (data) { 6186 case PHY_STAT_PWRDN: 6187 case PHY_STAT_EXT_INIT: 6188 rtl8152_apply_firmware(tp, true); 6189 6190 r8152_mdio_clr_bit(tp, MII_BMCR, BMCR_PDOWN); 6191 break; 6192 case PHY_STAT_LAN_ON: 6193 default: 6194 rtl8152_apply_firmware(tp, false); 6195 break; 6196 } 6197 6198 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 6199 6200 sram_set_bits(tp, SRAM_GREEN_CFG, R_TUNE_EN); 6201 6202 ocp_reg_set_bits(tp, OCP_NCTL_CFG, PGA_RETURN_EN); 6203 6204 /* ADC Bias Calibration: 6205 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake 6206 * bit (bit3) to rebuild the real 16-bit data. Write the data to the 6207 * ADC ioffset. 6208 */ 6209 ocp_data = r8152_efuse_read(tp, 0x7d); 6210 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7)); 6211 if (data != 0xffff) 6212 ocp_reg_write(tp, OCP_ADC_IOFFSET, data); 6213 6214 /* ups mode tx-link-pulse timing adjustment: 6215 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0] 6216 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt 6217 */ 6218 ocp_data = ocp_reg_read(tp, 0xc426); 6219 ocp_data &= 0x3fff; 6220 if (ocp_data) { 6221 u32 swr_cnt_1ms_ini; 6222 6223 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK; 6224 ocp_word_w0w1(tp, MCU_TYPE_USB, USB_UPS_CFG, SAW_CNT_1MS_MASK, 6225 swr_cnt_1ms_ini); 6226 } 6227 6228 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_PHY_PWR, PFM_PWM_SWITCH); 6229 6230 /* Advnace EEE */ 6231 if (!rtl_phy_patch_request(tp, true, true)) { 6232 ocp_reg_set_bits(tp, OCP_POWER_CFG, EEE_CLKDIV_EN); 6233 tp->ups_info.eee_ckdiv = true; 6234 6235 ocp_reg_set_bits(tp, OCP_DOWN_SPEED, 6236 EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV); 6237 tp->ups_info.eee_cmod_lv = true; 6238 tp->ups_info._10m_ckdiv = true; 6239 tp->ups_info.eee_plloff_giga = true; 6240 6241 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); 6242 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5)); 6243 tp->ups_info._250m_ckdiv = true; 6244 6245 rtl_phy_patch_request(tp, false, true); 6246 } 6247 6248 if (tp->eee_en) 6249 rtl_eee_enable(tp, true); 6250 6251 r8153_aldps_en(tp, true); 6252 r8152b_enable_fc(tp); 6253 6254 set_bit(PHY_RESET, &tp->flags); 6255 } 6256 6257 static void r8153c_hw_phy_cfg(struct r8152 *tp) 6258 { 6259 r8153b_hw_phy_cfg(tp); 6260 6261 tp->ups_info.r_tune = true; 6262 } 6263 6264 static void rtl8153_change_mtu(struct r8152 *tp) 6265 { 6266 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu)); 6267 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); 6268 } 6269 6270 static void r8153_first_init(struct r8152 *tp) 6271 { 6272 rxdy_gated_en(tp, true); 6273 r8153_teredo_off(tp); 6274 6275 ocp_dword_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, RCR_ACPT_ALL); 6276 6277 rtl8152_nic_reset(tp); 6278 rtl_reset_bmu(tp); 6279 6280 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB); 6281 6282 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, MCU_BORW_EN); 6283 6284 wait_oob_link_list_ready(tp); 6285 6286 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, RE_INIT_LL); 6287 6288 wait_oob_link_list_ready(tp); 6289 6290 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 6291 6292 rtl8153_change_mtu(tp); 6293 6294 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_TCR0, TCR0_AUTO_FIFO); 6295 6296 rtl8152_nic_reset(tp); 6297 6298 /* rx share fifo credit full threshold */ 6299 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); 6300 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); 6301 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); 6302 /* TX share fifo free credit full threshold */ 6303 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); 6304 } 6305 6306 static void r8153_enter_oob(struct r8152 *tp) 6307 { 6308 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB); 6309 6310 /* RX FIFO settings for OOB */ 6311 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); 6312 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); 6313 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); 6314 6315 rtl_disable(tp); 6316 rtl_reset_bmu(tp); 6317 6318 wait_oob_link_list_ready(tp); 6319 6320 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, RE_INIT_LL); 6321 6322 wait_oob_link_list_ready(tp); 6323 6324 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522); 6325 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT); 6326 6327 switch (tp->version) { 6328 case RTL_VER_03: 6329 case RTL_VER_04: 6330 case RTL_VER_05: 6331 case RTL_VER_06: 6332 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 6333 TEREDO_WAKE_MASK); 6334 break; 6335 6336 case RTL_VER_08: 6337 case RTL_VER_09: 6338 case RTL_VER_14: 6339 /* Clear teredo wake event. bit[15:8] is the teredo wakeup 6340 * type. Set it to zero. bits[7:0] are the W1C bits about 6341 * the events. Set them to all 1 to clear them. 6342 */ 6343 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); 6344 break; 6345 6346 default: 6347 break; 6348 } 6349 6350 rtl_rx_vlan_en(tp, true); 6351 6352 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_BDC_CR, ALDPS_PROXY_MODE); 6353 6354 ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, 6355 NOW_IS_OOB | DIS_MCU_CLROOB); 6356 6357 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, MCU_BORW_EN); 6358 6359 rxdy_gated_en(tp, false); 6360 6361 ocp_dword_set_bits(tp, MCU_TYPE_PLA, PLA_RCR, 6362 RCR_APM | RCR_AM | RCR_AB); 6363 } 6364 6365 static void rtl8153_disable(struct r8152 *tp) 6366 { 6367 r8153_aldps_en(tp, false); 6368 rtl_disable(tp); 6369 rtl_reset_bmu(tp); 6370 r8153_aldps_en(tp, true); 6371 } 6372 6373 static u32 fc_pause_on_auto(struct r8152 *tp) 6374 { 6375 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 6 * 1024); 6376 } 6377 6378 static u32 fc_pause_off_auto(struct r8152 *tp) 6379 { 6380 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 14 * 1024); 6381 } 6382 6383 static void r8156_fc_parameter(struct r8152 *tp) 6384 { 6385 u32 pause_on = tp->fc_pause_on ? tp->fc_pause_on : fc_pause_on_auto(tp); 6386 u32 pause_off = tp->fc_pause_off ? tp->fc_pause_off : fc_pause_off_auto(tp); 6387 6388 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 16); 6389 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16); 6390 } 6391 6392 static int rtl8156_enable(struct r8152 *tp) 6393 { 6394 u16 speed; 6395 6396 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6397 return -ENODEV; 6398 6399 if (tp->version < RTL_VER_12) 6400 r8156_fc_parameter(tp); 6401 6402 set_tx_qlen(tp); 6403 rtl_set_eee_plus(tp); 6404 6405 if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_16) 6406 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, RX_AGGR_NUM_MASK); 6407 6408 r8153_set_rx_early_timeout(tp); 6409 r8153_set_rx_early_size(tp); 6410 6411 speed = rtl8152_get_speed(tp); 6412 rtl_set_ifg(tp, speed); 6413 6414 if (tp->version >= RTL_VER_16) 6415 return rtl_enable(tp); 6416 6417 if (speed & _2500bps) 6418 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 6419 IDLE_SPDWN_EN); 6420 else 6421 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 6422 IDLE_SPDWN_EN); 6423 6424 if (tp->version < RTL_VER_12) { 6425 if (speed & _1000bps) 6426 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11); 6427 else if (speed & _500bps) 6428 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d); 6429 } 6430 6431 if (tp->udev->speed == USB_SPEED_HIGH) { 6432 /* USB 0xb45e[3:0] l1_nyet_hird */ 6433 if (is_flow_control(speed)) 6434 ocp_word_w0w1(tp, MCU_TYPE_USB, USB_L1_CTRL, 0xf, 0xf); 6435 else 6436 ocp_word_w0w1(tp, MCU_TYPE_USB, USB_L1_CTRL, 0xf, 0x1); 6437 } 6438 6439 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK); 6440 usleep_range(1000, 2000); 6441 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK); 6442 6443 return rtl_enable(tp); 6444 } 6445 6446 static void rtl8156_disable(struct r8152 *tp) 6447 { 6448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 0); 6449 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 0); 6450 6451 rtl8153_disable(tp); 6452 } 6453 6454 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex, 6455 u32 advertising) 6456 { 6457 u16 bmcr; 6458 int ret = 0; 6459 6460 if (autoneg == AUTONEG_DISABLE) { 6461 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL) 6462 return -EINVAL; 6463 6464 switch (speed) { 6465 case SPEED_10: 6466 bmcr = BMCR_SPEED10; 6467 if (duplex == DUPLEX_FULL) { 6468 bmcr |= BMCR_FULLDPLX; 6469 tp->ups_info.speed_duplex = FORCE_10M_FULL; 6470 } else { 6471 tp->ups_info.speed_duplex = FORCE_10M_HALF; 6472 } 6473 break; 6474 case SPEED_100: 6475 bmcr = BMCR_SPEED100; 6476 if (duplex == DUPLEX_FULL) { 6477 bmcr |= BMCR_FULLDPLX; 6478 tp->ups_info.speed_duplex = FORCE_100M_FULL; 6479 } else { 6480 tp->ups_info.speed_duplex = FORCE_100M_HALF; 6481 } 6482 break; 6483 case SPEED_1000: 6484 if (tp->mii.supports_gmii) { 6485 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX; 6486 tp->ups_info.speed_duplex = NWAY_1000M_FULL; 6487 break; 6488 } 6489 fallthrough; 6490 default: 6491 ret = -EINVAL; 6492 goto out; 6493 } 6494 6495 if (duplex == DUPLEX_FULL) 6496 tp->mii.full_duplex = 1; 6497 else 6498 tp->mii.full_duplex = 0; 6499 6500 tp->mii.force_media = 1; 6501 } else { 6502 u16 orig, new1; 6503 u32 support; 6504 6505 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | 6506 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; 6507 6508 if (tp->mii.supports_gmii) { 6509 support |= RTL_ADVERTISED_1000_FULL; 6510 6511 if (tp->support_2500full) 6512 support |= RTL_ADVERTISED_2500_FULL; 6513 6514 if (tp->support_5000full) 6515 support |= RTL_ADVERTISED_5000_FULL; 6516 } 6517 6518 advertising &= support; 6519 if (!advertising) 6520 return -EINVAL; 6521 6522 orig = r8152_mdio_read(tp, MII_ADVERTISE); 6523 new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL | 6524 ADVERTISE_100HALF | ADVERTISE_100FULL); 6525 if (advertising & RTL_ADVERTISED_10_HALF) { 6526 new1 |= ADVERTISE_10HALF; 6527 tp->ups_info.speed_duplex = NWAY_10M_HALF; 6528 } 6529 if (advertising & RTL_ADVERTISED_10_FULL) { 6530 new1 |= ADVERTISE_10FULL; 6531 tp->ups_info.speed_duplex = NWAY_10M_FULL; 6532 } 6533 6534 if (advertising & RTL_ADVERTISED_100_HALF) { 6535 new1 |= ADVERTISE_100HALF; 6536 tp->ups_info.speed_duplex = NWAY_100M_HALF; 6537 } 6538 if (advertising & RTL_ADVERTISED_100_FULL) { 6539 new1 |= ADVERTISE_100FULL; 6540 tp->ups_info.speed_duplex = NWAY_100M_FULL; 6541 } 6542 6543 if (orig != new1) { 6544 r8152_mdio_write(tp, MII_ADVERTISE, new1); 6545 tp->mii.advertising = new1; 6546 } 6547 6548 if (tp->mii.supports_gmii) { 6549 orig = r8152_mdio_read(tp, MII_CTRL1000); 6550 new1 = orig & ~(ADVERTISE_1000FULL | 6551 ADVERTISE_1000HALF); 6552 6553 if (advertising & RTL_ADVERTISED_1000_FULL) { 6554 new1 |= ADVERTISE_1000FULL; 6555 tp->ups_info.speed_duplex = NWAY_1000M_FULL; 6556 } 6557 6558 if (orig != new1) 6559 r8152_mdio_write(tp, MII_CTRL1000, new1); 6560 } 6561 6562 if (tp->support_2500full || tp->support_5000full) { 6563 orig = ocp_reg_read(tp, OCP_10GBT_CTRL); 6564 new1 = orig & ~(MDIO_AN_10GBT_CTRL_ADV2_5G | MDIO_AN_10GBT_CTRL_ADV5G); 6565 6566 if (advertising & RTL_ADVERTISED_2500_FULL) { 6567 new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G; 6568 tp->ups_info.speed_duplex = NWAY_2500M_FULL; 6569 } 6570 6571 if (advertising & RTL_ADVERTISED_5000_FULL) { 6572 new1 |= MDIO_AN_10GBT_CTRL_ADV5G; 6573 tp->ups_info.speed_duplex = NWAY_5000M_FULL; 6574 } 6575 6576 if (orig != new1) 6577 ocp_reg_write(tp, OCP_10GBT_CTRL, new1); 6578 } 6579 6580 bmcr = BMCR_ANENABLE | BMCR_ANRESTART; 6581 6582 tp->mii.force_media = 0; 6583 } 6584 6585 if (test_and_clear_bit(PHY_RESET, &tp->flags)) 6586 bmcr |= BMCR_RESET; 6587 6588 r8152_mdio_write(tp, MII_BMCR, bmcr); 6589 6590 if (bmcr & BMCR_RESET) { 6591 int i; 6592 6593 for (i = 0; i < 50; i++) { 6594 msleep(20); 6595 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) 6596 break; 6597 } 6598 } 6599 6600 out: 6601 return ret; 6602 } 6603 6604 static void rtl8152_up(struct r8152 *tp) 6605 { 6606 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6607 return; 6608 6609 r8152_aldps_en(tp, false); 6610 r8152b_exit_oob(tp); 6611 r8152_aldps_en(tp, true); 6612 } 6613 6614 static void rtl8152_down(struct r8152 *tp) 6615 { 6616 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 6617 rtl_drop_queued_tx(tp); 6618 return; 6619 } 6620 6621 r8152_power_cut_en(tp, false); 6622 r8152_aldps_en(tp, false); 6623 r8152b_enter_oob(tp); 6624 r8152_aldps_en(tp, true); 6625 } 6626 6627 static void rtl8153_up(struct r8152 *tp) 6628 { 6629 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6630 return; 6631 6632 r8153_u1u2en(tp, false); 6633 r8153_u2p3en(tp, false); 6634 r8153_aldps_en(tp, false); 6635 r8153_first_init(tp); 6636 6637 ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_CONFIG6, LANWAKE_CLR_EN); 6638 6639 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, LANWAKE_PIN); 6640 6641 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_SSPHYLINK1, DELAY_PHY_PWR_CHG); 6642 6643 r8153_aldps_en(tp, true); 6644 6645 switch (tp->version) { 6646 case RTL_VER_03: 6647 case RTL_VER_04: 6648 break; 6649 case RTL_VER_05: 6650 case RTL_VER_06: 6651 default: 6652 r8153_u2p3en(tp, true); 6653 break; 6654 } 6655 6656 r8153_u1u2en(tp, true); 6657 } 6658 6659 static void rtl8153_down(struct r8152 *tp) 6660 { 6661 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 6662 rtl_drop_queued_tx(tp); 6663 return; 6664 } 6665 6666 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_CONFIG6, LANWAKE_CLR_EN); 6667 6668 r8153_u1u2en(tp, false); 6669 r8153_u2p3en(tp, false); 6670 r8153_power_cut_en(tp, false); 6671 r8153_aldps_en(tp, false); 6672 r8153_enter_oob(tp); 6673 r8153_aldps_en(tp, true); 6674 } 6675 6676 static void rtl8153b_up(struct r8152 *tp) 6677 { 6678 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6679 return; 6680 6681 r8153b_u1u2en(tp, false); 6682 r8153_u2p3en(tp, false); 6683 r8153_aldps_en(tp, false); 6684 6685 r8153_first_init(tp); 6686 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); 6687 6688 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 6689 PLA_MCU_SPDWN_EN); 6690 6691 r8153_aldps_en(tp, true); 6692 6693 if (tp->udev->speed >= USB_SPEED_SUPER) 6694 r8153b_u1u2en(tp, true); 6695 } 6696 6697 static void rtl8153b_down(struct r8152 *tp) 6698 { 6699 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 6700 rtl_drop_queued_tx(tp); 6701 return; 6702 } 6703 6704 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 6705 PLA_MCU_SPDWN_EN); 6706 6707 r8153b_u1u2en(tp, false); 6708 r8153_u2p3en(tp, false); 6709 r8153b_power_cut_en(tp, false); 6710 r8153_aldps_en(tp, false); 6711 r8153_enter_oob(tp); 6712 r8153_aldps_en(tp, true); 6713 } 6714 6715 static void rtl8153c_change_mtu(struct r8152 *tp) 6716 { 6717 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu)); 6718 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64); 6719 6720 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64); 6721 6722 /* Adjust the tx fifo free credit full threshold, otherwise 6723 * the fifo would be too small to send a jumbo frame packet. 6724 */ 6725 if (tp->netdev->mtu < 8000) 6726 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 2048 / 8); 6727 else 6728 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 900 / 8); 6729 } 6730 6731 static void rtl8153c_up(struct r8152 *tp) 6732 { 6733 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6734 return; 6735 6736 r8153b_u1u2en(tp, false); 6737 r8153_u2p3en(tp, false); 6738 r8153_aldps_en(tp, false); 6739 6740 rxdy_gated_en(tp, true); 6741 r8153_teredo_off(tp); 6742 6743 ocp_dword_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, RCR_ACPT_ALL); 6744 6745 rtl8152_nic_reset(tp); 6746 rtl_reset_bmu(tp); 6747 6748 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB); 6749 6750 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, MCU_BORW_EN); 6751 6752 wait_oob_link_list_ready(tp); 6753 6754 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, RE_INIT_LL); 6755 6756 wait_oob_link_list_ready(tp); 6757 6758 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 6759 6760 rtl8153c_change_mtu(tp); 6761 6762 rtl8152_nic_reset(tp); 6763 6764 /* rx share fifo credit full threshold */ 6765 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, 0x02); 6766 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 0x08); 6767 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); 6768 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); 6769 6770 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); 6771 6772 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 6773 6774 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_CONFIG34, BIT(8)); 6775 6776 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 6777 6778 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 6779 PLA_MCU_SPDWN_EN); 6780 6781 r8153_aldps_en(tp, true); 6782 r8153b_u1u2en(tp, true); 6783 } 6784 6785 static void rtl8156_change_mtu(struct r8152 *tp) 6786 { 6787 u32 rx_max_size = mtu_to_size(tp->netdev->mtu); 6788 6789 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rx_max_size); 6790 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); 6791 r8156_fc_parameter(tp); 6792 6793 /* TX share fifo free credit full threshold */ 6794 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64); 6795 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 6796 ALIGN(rx_max_size + tp->tx_desc.size, 1024) / 16); 6797 } 6798 6799 static void rtl8156_up(struct r8152 *tp) 6800 { 6801 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6802 return; 6803 6804 r8153b_u1u2en(tp, false); 6805 if (tp->version != RTL_VER_16) 6806 r8153_u2p3en(tp, false); 6807 r8153_aldps_en(tp, false); 6808 6809 rxdy_gated_en(tp, true); 6810 r8153_teredo_off(tp); 6811 6812 ocp_dword_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, RCR_ACPT_ALL); 6813 6814 rtl8152_nic_reset(tp); 6815 rtl_reset_bmu(tp); 6816 6817 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB); 6818 6819 if (tp->version == RTL_VER_16) 6820 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR1, BIT(3)); 6821 6822 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, MCU_BORW_EN); 6823 6824 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 6825 6826 rtl8156_change_mtu(tp); 6827 6828 switch (tp->version) { 6829 case RTL_TEST_01: 6830 case RTL_VER_10: 6831 case RTL_VER_11: 6832 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ACT_ODMA); 6833 break; 6834 default: 6835 break; 6836 } 6837 6838 /* share FIFO settings */ 6839 ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, RXFIFO_FULL_MASK, 6840 0x08); 6841 6842 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 6843 PLA_MCU_SPDWN_EN); 6844 6845 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, PLA_MCU_SPDWN_EN); 6846 6847 if (tp->version != RTL_VER_16) 6848 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 6849 RG_PWRDN_EN | ALL_SPEED_OFF); 6850 6851 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400); 6852 6853 if (tp->saved_wolopts != __rtl_get_wol(tp)) { 6854 netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n"); 6855 __rtl_set_wol(tp, tp->saved_wolopts); 6856 } 6857 6858 r8153_aldps_en(tp, true); 6859 if (tp->version != RTL_VER_16) 6860 r8153_u2p3en(tp, true); 6861 6862 if (tp->version != RTL_VER_16 && tp->udev->speed >= USB_SPEED_SUPER) 6863 r8153b_u1u2en(tp, true); 6864 } 6865 6866 static void rtl8156_down(struct r8152 *tp) 6867 { 6868 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 6869 rtl_drop_queued_tx(tp); 6870 return; 6871 } 6872 6873 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 6874 PLA_MCU_SPDWN_EN); 6875 6876 r8153b_u1u2en(tp, false); 6877 if (tp->version != RTL_VER_16) { 6878 r8153_u2p3en(tp, false); 6879 r8153b_power_cut_en(tp, false); 6880 } 6881 r8153_aldps_en(tp, false); 6882 6883 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB); 6884 6885 /* RX FIFO settings for OOB */ 6886 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 64 / 16); 6887 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 1024 / 16); 6888 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 4096 / 16); 6889 6890 rtl_disable(tp); 6891 rtl_reset_bmu(tp); 6892 6893 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522); 6894 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT); 6895 6896 /* Clear teredo wake event. bit[15:8] is the teredo wakeup 6897 * type. Set it to zero. bits[7:0] are the W1C bits about 6898 * the events. Set them to all 1 to clear them. 6899 */ 6900 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); 6901 6902 ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, NOW_IS_OOB); 6903 6904 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, MCU_BORW_EN); 6905 6906 rtl_rx_vlan_en(tp, true); 6907 rxdy_gated_en(tp, false); 6908 6909 ocp_dword_set_bits(tp, MCU_TYPE_PLA, PLA_RCR, 6910 RCR_APM | RCR_AM | RCR_AB); 6911 6912 r8153_aldps_en(tp, true); 6913 } 6914 6915 static bool rtl8152_in_nway(struct r8152 *tp) 6916 { 6917 u16 nway_state; 6918 6919 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000); 6920 tp->ocp_base = 0x2000; 6921 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */ 6922 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a); 6923 6924 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */ 6925 if (nway_state & 0xc000) 6926 return false; 6927 else 6928 return true; 6929 } 6930 6931 static bool rtl8153_in_nway(struct r8152 *tp) 6932 { 6933 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff; 6934 6935 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE) 6936 return false; 6937 else 6938 return true; 6939 } 6940 6941 static void r8156_mdio_force_mode(struct r8152 *tp) 6942 { 6943 u16 data; 6944 6945 /* Select force mode through 0xa5b4 bit 15 6946 * 0: MDIO force mode 6947 * 1: MMD force mode 6948 */ 6949 data = ocp_reg_read(tp, 0xa5b4); 6950 if (data & BIT(15)) { 6951 data &= ~BIT(15); 6952 ocp_reg_write(tp, 0xa5b4, data); 6953 } 6954 } 6955 6956 static void set_carrier(struct r8152 *tp) 6957 { 6958 struct net_device *netdev = tp->netdev; 6959 struct napi_struct *napi = &tp->napi; 6960 u16 speed; 6961 6962 speed = rtl8152_get_speed(tp); 6963 6964 if (speed & LINK_STATUS) { 6965 if (!netif_carrier_ok(netdev)) { 6966 tp->rtl_ops.enable(tp); 6967 netif_stop_queue(netdev); 6968 napi_disable(napi); 6969 netif_carrier_on(netdev); 6970 rtl_start_rx(tp); 6971 clear_bit(RTL8152_SET_RX_MODE, &tp->flags); 6972 _rtl8152_set_rx_mode(netdev); 6973 napi_enable(napi); 6974 netif_wake_queue(netdev); 6975 netif_info(tp, link, netdev, "carrier on\n"); 6976 } else if (netif_queue_stopped(netdev) && 6977 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) { 6978 netif_wake_queue(netdev); 6979 } 6980 } else { 6981 if (netif_carrier_ok(netdev)) { 6982 netif_carrier_off(netdev); 6983 tasklet_disable(&tp->tx_tl); 6984 napi_disable(napi); 6985 tp->rtl_ops.disable(tp); 6986 napi_enable(napi); 6987 tasklet_enable(&tp->tx_tl); 6988 netif_info(tp, link, netdev, "carrier off\n"); 6989 } 6990 } 6991 } 6992 6993 static void rtl_work_func_t(struct work_struct *work) 6994 { 6995 struct r8152 *tp = container_of(work, struct r8152, schedule.work); 6996 6997 /* If the device is unplugged or !netif_running(), the workqueue 6998 * doesn't need to wake the device, and could return directly. 6999 */ 7000 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags) || !netif_running(tp->netdev)) 7001 return; 7002 7003 if (usb_autopm_get_interface(tp->intf) < 0) 7004 return; 7005 7006 if (!test_bit(WORK_ENABLE, &tp->flags)) 7007 goto out1; 7008 7009 if (!mutex_trylock(&tp->control)) { 7010 schedule_delayed_work(&tp->schedule, 0); 7011 goto out1; 7012 } 7013 7014 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags)) 7015 set_carrier(tp); 7016 7017 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags)) 7018 _rtl8152_set_rx_mode(tp->netdev); 7019 7020 /* don't schedule tasket before linking */ 7021 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) && 7022 netif_carrier_ok(tp->netdev)) 7023 tasklet_schedule(&tp->tx_tl); 7024 7025 if (test_and_clear_bit(RX_EPROTO, &tp->flags) && 7026 !list_empty(&tp->rx_done)) 7027 napi_schedule(&tp->napi); 7028 7029 mutex_unlock(&tp->control); 7030 7031 out1: 7032 usb_autopm_put_interface(tp->intf); 7033 } 7034 7035 static void rtl_hw_phy_work_func_t(struct work_struct *work) 7036 { 7037 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work); 7038 7039 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7040 return; 7041 7042 if (usb_autopm_get_interface(tp->intf) < 0) 7043 return; 7044 7045 mutex_lock(&tp->control); 7046 7047 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) { 7048 tp->rtl_fw.retry = false; 7049 tp->rtl_fw.fw = NULL; 7050 7051 /* Delay execution in case request_firmware() is not ready yet. 7052 */ 7053 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10); 7054 goto ignore_once; 7055 } 7056 7057 tp->rtl_ops.hw_phy_cfg(tp); 7058 7059 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex, 7060 tp->advertising); 7061 7062 ignore_once: 7063 mutex_unlock(&tp->control); 7064 7065 usb_autopm_put_interface(tp->intf); 7066 } 7067 7068 #ifdef CONFIG_PM_SLEEP 7069 static int rtl_notifier(struct notifier_block *nb, unsigned long action, 7070 void *data) 7071 { 7072 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier); 7073 7074 switch (action) { 7075 case PM_HIBERNATION_PREPARE: 7076 case PM_SUSPEND_PREPARE: 7077 usb_autopm_get_interface(tp->intf); 7078 break; 7079 7080 case PM_POST_HIBERNATION: 7081 case PM_POST_SUSPEND: 7082 usb_autopm_put_interface(tp->intf); 7083 break; 7084 7085 case PM_POST_RESTORE: 7086 case PM_RESTORE_PREPARE: 7087 default: 7088 break; 7089 } 7090 7091 return NOTIFY_DONE; 7092 } 7093 #endif 7094 7095 static int rtl8152_open(struct net_device *netdev) 7096 { 7097 struct r8152 *tp = netdev_priv(netdev); 7098 int res = 0; 7099 7100 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) { 7101 cancel_delayed_work_sync(&tp->hw_phy_work); 7102 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work); 7103 } 7104 7105 res = alloc_all_mem(tp); 7106 if (res) 7107 goto out; 7108 7109 res = usb_autopm_get_interface(tp->intf); 7110 if (res < 0) 7111 goto out_free; 7112 7113 mutex_lock(&tp->control); 7114 7115 tp->rtl_ops.up(tp); 7116 7117 netif_carrier_off(netdev); 7118 netif_start_queue(netdev); 7119 set_bit(WORK_ENABLE, &tp->flags); 7120 7121 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); 7122 if (res) { 7123 if (res == -ENODEV) 7124 netif_device_detach(tp->netdev); 7125 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", 7126 res); 7127 goto out_unlock; 7128 } 7129 napi_enable(&tp->napi); 7130 tasklet_enable(&tp->tx_tl); 7131 7132 mutex_unlock(&tp->control); 7133 7134 usb_autopm_put_interface(tp->intf); 7135 #ifdef CONFIG_PM_SLEEP 7136 tp->pm_notifier.notifier_call = rtl_notifier; 7137 register_pm_notifier(&tp->pm_notifier); 7138 #endif 7139 return 0; 7140 7141 out_unlock: 7142 mutex_unlock(&tp->control); 7143 usb_autopm_put_interface(tp->intf); 7144 out_free: 7145 free_all_mem(tp); 7146 out: 7147 return res; 7148 } 7149 7150 static int rtl8152_close(struct net_device *netdev) 7151 { 7152 struct r8152 *tp = netdev_priv(netdev); 7153 int res = 0; 7154 7155 #ifdef CONFIG_PM_SLEEP 7156 unregister_pm_notifier(&tp->pm_notifier); 7157 #endif 7158 tasklet_disable(&tp->tx_tl); 7159 clear_bit(WORK_ENABLE, &tp->flags); 7160 usb_kill_urb(tp->intr_urb); 7161 cancel_delayed_work_sync(&tp->schedule); 7162 napi_disable(&tp->napi); 7163 netif_stop_queue(netdev); 7164 7165 res = usb_autopm_get_interface(tp->intf); 7166 if (res < 0 || test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 7167 rtl_drop_queued_tx(tp); 7168 rtl_stop_rx(tp); 7169 } else { 7170 mutex_lock(&tp->control); 7171 7172 tp->rtl_ops.down(tp); 7173 7174 mutex_unlock(&tp->control); 7175 } 7176 7177 if (!res) 7178 usb_autopm_put_interface(tp->intf); 7179 7180 free_all_mem(tp); 7181 7182 return res; 7183 } 7184 7185 static void rtl_tally_reset(struct r8152 *tp) 7186 { 7187 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_RSTTALLY, TALLY_RESET); 7188 } 7189 7190 static void r8152b_init(struct r8152 *tp) 7191 { 7192 u32 ocp_data; 7193 7194 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7195 return; 7196 7197 r8152_mdio_test_and_clr_bit(tp, MII_BMCR, BMCR_PDOWN); 7198 7199 r8152_aldps_en(tp, false); 7200 7201 if (tp->version == RTL_VER_01) 7202 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, 7203 LED_MODE_MASK); 7204 7205 r8152_power_cut_en(tp, false); 7206 7207 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_PHY_PWR, 7208 TX_10M_IDLE_EN | PFM_PWM_SWITCH); 7209 7210 ocp_dword_w0w1(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, MCU_CLK_RATIO_MASK, 7211 MCU_CLK_RATIO | D3_CLK_GATED_EN); 7212 7213 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | 7214 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; 7215 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); 7216 7217 rtl_tally_reset(tp); 7218 7219 /* enable rx aggregation */ 7220 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, 7221 RX_AGG_DISABLE | RX_ZERO_EN); 7222 } 7223 7224 static void r8153_init(struct r8152 *tp) 7225 { 7226 u32 ocp_data; 7227 int i; 7228 7229 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7230 return; 7231 7232 r8153_u1u2en(tp, false); 7233 7234 for (i = 0; i < 500; i++) { 7235 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 7236 AUTOLOAD_DONE) 7237 break; 7238 7239 msleep(20); 7240 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7241 break; 7242 } 7243 7244 r8153_phy_status(tp, 0); 7245 7246 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || 7247 tp->version == RTL_VER_05) 7248 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); 7249 7250 r8152_mdio_test_and_clr_bit(tp, MII_BMCR, BMCR_PDOWN); 7251 7252 r8153_phy_status(tp, PHY_STAT_LAN_ON); 7253 7254 r8153_u2p3en(tp, false); 7255 7256 if (tp->version == RTL_VER_04) { 7257 ocp_word_w0w1(tp, MCU_TYPE_USB, USB_SSPHYLINK2, 7258 pwd_dn_scale_mask, pwd_dn_scale(96)); 7259 7260 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_USB2PHY, 7261 USB2PHY_L1 | USB2PHY_SUSPEND); 7262 } else if (tp->version == RTL_VER_05) { 7263 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ECM_ALDPS); 7264 7265 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) 7266 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, 7267 DYNAMIC_BURST); 7268 else 7269 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, 7270 DYNAMIC_BURST); 7271 } else if (tp->version == RTL_VER_06) { 7272 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) 7273 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, 7274 DYNAMIC_BURST); 7275 else 7276 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, 7277 DYNAMIC_BURST); 7278 7279 r8153_queue_wake(tp, false); 7280 7281 if (rtl8152_get_speed(tp) & LINK_STATUS) 7282 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, 7283 CUR_LINK_OK | POLL_LINK_CHG); 7284 else 7285 ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, 7286 CUR_LINK_OK, POLL_LINK_CHG); 7287 } 7288 7289 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, EP4_FULL_FC); 7290 7291 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_WDT11_CTRL, TIMER11_EN); 7292 7293 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, LED_MODE_MASK); 7294 7295 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; 7296 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER) 7297 ocp_data |= LPM_TIMER_500MS; 7298 else 7299 ocp_data |= LPM_TIMER_500US; 7300 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); 7301 7302 ocp_word_w0w1(tp, MCU_TYPE_USB, USB_AFE_CTRL2, SEN_VAL_MASK, 7303 SEN_VAL_NORMAL | SEL_RXIDLE); 7304 7305 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); 7306 7307 r8153_power_cut_en(tp, false); 7308 rtl_runtime_suspend_enable(tp, false); 7309 r8153_mac_clk_speed_down(tp, false); 7310 r8153_u1u2en(tp, true); 7311 usb_enable_lpm(tp->udev); 7312 7313 ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_CONFIG6, LANWAKE_CLR_EN); 7314 7315 ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, LANWAKE_PIN); 7316 7317 /* rx aggregation */ 7318 if (tp->dell_tb_rx_agg_bug) 7319 ocp_word_w0w1(tp, MCU_TYPE_USB, USB_USB_CTRL, RX_ZERO_EN, 7320 RX_AGG_DISABLE); 7321 else 7322 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, 7323 RX_AGG_DISABLE | RX_ZERO_EN); 7324 7325 rtl_tally_reset(tp); 7326 7327 switch (tp->udev->speed) { 7328 case USB_SPEED_SUPER: 7329 case USB_SPEED_SUPER_PLUS: 7330 tp->coalesce = COALESCE_SUPER; 7331 break; 7332 case USB_SPEED_HIGH: 7333 tp->coalesce = COALESCE_HIGH; 7334 break; 7335 default: 7336 tp->coalesce = COALESCE_SLOW; 7337 break; 7338 } 7339 } 7340 7341 static void r8153b_init(struct r8152 *tp) 7342 { 7343 int i; 7344 7345 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7346 return; 7347 7348 r8153b_u1u2en(tp, false); 7349 7350 for (i = 0; i < 500; i++) { 7351 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 7352 AUTOLOAD_DONE) 7353 break; 7354 7355 msleep(20); 7356 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7357 break; 7358 } 7359 7360 r8153_phy_status(tp, 0); 7361 7362 r8152_mdio_test_and_clr_bit(tp, MII_BMCR, BMCR_PDOWN); 7363 7364 r8153_phy_status(tp, PHY_STAT_LAN_ON); 7365 7366 r8153_u2p3en(tp, false); 7367 7368 /* MSC timer = 0xfff * 8ms = 32760 ms */ 7369 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 7370 7371 r8153b_power_cut_en(tp, false); 7372 r8153b_ups_en(tp, false); 7373 r8153_queue_wake(tp, false); 7374 rtl_runtime_suspend_enable(tp, false); 7375 7376 if (rtl8152_get_speed(tp) & LINK_STATUS) 7377 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, 7378 CUR_LINK_OK | POLL_LINK_CHG); 7379 else 7380 ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, CUR_LINK_OK, 7381 POLL_LINK_CHG); 7382 7383 if (tp->udev->speed >= USB_SPEED_SUPER) 7384 r8153b_u1u2en(tp, true); 7385 7386 usb_enable_lpm(tp->udev); 7387 7388 /* MAC clock speed down */ 7389 r8153_mac_clk_speed_down(tp, true); 7390 7391 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 7392 PLA_MCU_SPDWN_EN); 7393 7394 if (tp->version == RTL_VER_09) 7395 /* Disable Test IO for 32QFN */ 7396 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) 7397 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_PHY_PWR, 7398 TEST_IO_OFF); 7399 7400 set_bit(GREEN_ETHERNET, &tp->flags); 7401 7402 /* rx aggregation */ 7403 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, 7404 RX_AGG_DISABLE | RX_ZERO_EN); 7405 7406 rtl_tally_reset(tp); 7407 7408 tp->coalesce = 15000; /* 15 us */ 7409 } 7410 7411 static void r8153c_init(struct r8152 *tp) 7412 { 7413 int i; 7414 7415 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7416 return; 7417 7418 r8153b_u1u2en(tp, false); 7419 7420 /* Disable spi_en */ 7421 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 7422 7423 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_CONFIG5, BIT(3)); 7424 7425 ocp_word_set_bits(tp, MCU_TYPE_USB, 0xcbf0, BIT(1)); 7426 7427 for (i = 0; i < 500; i++) { 7428 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 7429 AUTOLOAD_DONE) 7430 break; 7431 7432 msleep(20); 7433 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7434 return; 7435 } 7436 7437 r8153_phy_status(tp, 0); 7438 7439 r8152_mdio_test_and_clr_bit(tp, MII_BMCR, BMCR_PDOWN); 7440 7441 r8153_phy_status(tp, PHY_STAT_LAN_ON); 7442 7443 r8153_u2p3en(tp, false); 7444 7445 /* MSC timer = 0xfff * 8ms = 32760 ms */ 7446 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 7447 7448 r8153b_power_cut_en(tp, false); 7449 r8153c_ups_en(tp, false); 7450 r8153_queue_wake(tp, false); 7451 rtl_runtime_suspend_enable(tp, false); 7452 7453 if (rtl8152_get_speed(tp) & LINK_STATUS) 7454 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, 7455 CUR_LINK_OK | POLL_LINK_CHG); 7456 else 7457 ocp_word_w0w1(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, CUR_LINK_OK, 7458 POLL_LINK_CHG); 7459 7460 r8153b_u1u2en(tp, true); 7461 7462 usb_enable_lpm(tp->udev); 7463 7464 /* MAC clock speed down */ 7465 r8153_mac_clk_speed_down(tp, true); 7466 7467 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2, BIT(7)); 7468 7469 set_bit(GREEN_ETHERNET, &tp->flags); 7470 7471 /* rx aggregation */ 7472 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, 7473 RX_AGG_DISABLE | RX_ZERO_EN); 7474 7475 rtl_tally_reset(tp); 7476 7477 tp->coalesce = 15000; /* 15 us */ 7478 } 7479 7480 static void r8156_hw_phy_cfg(struct r8152 *tp) 7481 { 7482 u16 data; 7483 7484 ocp_word_test_and_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS); 7485 7486 data = r8153_phy_status(tp, 0); 7487 switch (data) { 7488 case PHY_STAT_EXT_INIT: 7489 rtl8152_apply_firmware(tp, true); 7490 7491 ocp_reg_clr_bits(tp, 0xa468, BIT(3) | BIT(1)); 7492 break; 7493 case PHY_STAT_LAN_ON: 7494 case PHY_STAT_PWRDN: 7495 default: 7496 rtl8152_apply_firmware(tp, false); 7497 break; 7498 } 7499 7500 /* disable ALDPS before updating the PHY parameters */ 7501 r8153_aldps_en(tp, false); 7502 7503 /* disable EEE before updating the PHY parameters */ 7504 rtl_eee_enable(tp, false); 7505 7506 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7507 WARN_ON_ONCE(data != PHY_STAT_LAN_ON); 7508 7509 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_PHY_PWR, PFM_PWM_SWITCH); 7510 7511 switch (tp->version) { 7512 case RTL_VER_10: 7513 ocp_reg_w0w1(tp, 0xad40, 0x3ff, BIT(7) | BIT(2)); 7514 7515 ocp_reg_set_bits(tp, 0xad4e, BIT(4)); 7516 ocp_reg_w0w1(tp, 0xad16, 0x3ff, 0x6); 7517 ocp_reg_w0w1(tp, 0xad32, 0x3f, 0x6); 7518 ocp_reg_clr_bits(tp, 0xac08, BIT(12) | BIT(8)); 7519 ocp_reg_w0w1(tp, 0xac8a, BIT(15), BIT(12) | BIT(13) | BIT(14)); 7520 ocp_reg_set_bits(tp, 0xad18, BIT(10)); 7521 ocp_reg_set_bits(tp, 0xad1a, 0x3ff); 7522 ocp_reg_set_bits(tp, 0xad1c, 0x3ff); 7523 7524 sram_write_w0w1(tp, 0x80ea, 0xff00, 0xc400); 7525 sram_write_w0w1(tp, 0x80eb, 0x0700, 0x0300); 7526 sram_write_w0w1(tp, 0x80f8, 0xff00, 0x1c00); 7527 sram_write_w0w1(tp, 0x80f1, 0xff00, 0x3000); 7528 7529 sram_write_w0w1(tp, 0x80fe, 0xff00, 0xa500); 7530 sram_write_w0w1(tp, 0x8102, 0xff00, 0x5000); 7531 sram_write_w0w1(tp, 0x8015, 0xff00, 0x3300); 7532 sram_write_w0w1(tp, 0x8100, 0xff00, 0x7000); 7533 sram_write_w0w1(tp, 0x8014, 0xff00, 0xf000); 7534 sram_write_w0w1(tp, 0x8016, 0xff00, 0x6500); 7535 sram_write_w0w1(tp, 0x80dc, 0xff00, 0xed00); 7536 sram_set_bits(tp, 0x80df, BIT(8)); 7537 sram_clr_bits(tp, 0x80e1, BIT(8)); 7538 7539 ocp_reg_w0w1(tp, 0xbf06, 0x003f, 0x0038); 7540 7541 sram_write(tp, 0x819f, 0xddb6); 7542 7543 ocp_reg_write(tp, 0xbc34, 0x5555); 7544 ocp_reg_w0w1(tp, 0xbf0a, 0x0e00, 0x0a00); 7545 7546 ocp_reg_clr_bits(tp, 0xbd2c, BIT(13)); 7547 break; 7548 case RTL_VER_11: 7549 ocp_reg_set_bits(tp, 0xad16, 0x3ff); 7550 ocp_reg_w0w1(tp, 0xad32, 0x3f, 0x6); 7551 ocp_reg_clr_bits(tp, 0xac08, BIT(12) | BIT(8)); 7552 ocp_reg_w0w1(tp, 0xacc0, 0x3, BIT(1)); 7553 ocp_reg_w0w1(tp, 0xad40, 0xe7, BIT(6) | BIT(2)); 7554 ocp_reg_clr_bits(tp, 0xac14, BIT(7)); 7555 ocp_reg_clr_bits(tp, 0xac80, BIT(8) | BIT(9)); 7556 ocp_reg_w0w1(tp, 0xac5e, 0x7, BIT(1)); 7557 ocp_reg_write(tp, 0xad4c, 0x00a8); 7558 ocp_reg_write(tp, 0xac5c, 0x01ff); 7559 ocp_reg_w0w1(tp, 0xac8a, 0xf0, BIT(4) | BIT(5)); 7560 sram2_write_w0w1(tp, 0x8157, 0xff00, 0x0500); 7561 sram2_write_w0w1(tp, 0x8159, 0xff00, 0x0700); 7562 7563 /* AAGC */ 7564 sram2_write(tp, 0x80a2, 0x0153); 7565 sram2_write(tp, 0x809c, 0x0153); 7566 7567 /* EEE parameter */ 7568 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS_2P5G, 0x0056); 7569 7570 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_USB_CFG, 7571 EN_XG_LIP | EN_G_LIP); 7572 7573 sram_write(tp, 0x8257, 0x020f); /* XG PLL */ 7574 sram_write(tp, 0x80ea, 0x7843); /* GIGA Master */ 7575 7576 if (rtl_phy_patch_request(tp, true, true)) 7577 return; 7578 7579 /* Advance EEE */ 7580 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 7581 EEE_SPDWN_EN); 7582 7583 ocp_reg_w0w1(tp, OCP_DOWN_SPEED, EN_EEE_100 | EN_EEE_1000, 7584 EN_10M_CLKDIV); 7585 tp->ups_info._10m_ckdiv = true; 7586 tp->ups_info.eee_plloff_100 = false; 7587 tp->ups_info.eee_plloff_giga = false; 7588 7589 ocp_reg_clr_bits(tp, OCP_POWER_CFG, EEE_CLKDIV_EN); 7590 tp->ups_info.eee_ckdiv = false; 7591 7592 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); 7593 ocp_reg_write(tp, OCP_SYSCLK_CFG, sysclk_div_expo(5)); 7594 tp->ups_info._250m_ckdiv = false; 7595 7596 rtl_phy_patch_request(tp, false, true); 7597 7598 /* enable ADC Ibias Cal */ 7599 ocp_reg_set_bits(tp, 0xd068, BIT(13)); 7600 7601 /* enable Thermal Sensor */ 7602 sram_clr_bits(tp, 0x81a2, BIT(8)); 7603 ocp_reg_w0w1(tp, 0xb54c, 0xff00, 0xdb00); 7604 7605 /* Nway 2.5G Lite */ 7606 ocp_reg_clr_bits(tp, 0xa454, BIT(0)); 7607 7608 /* CS DSP solution */ 7609 ocp_reg_set_bits(tp, OCP_10GBT_CTRL, RTL_ADV2_5G_F_R); 7610 ocp_reg_clr_bits(tp, 0xad4e, BIT(4)); 7611 ocp_reg_clr_bits(tp, 0xa86a, BIT(0)); 7612 7613 /* MDI SWAP */ 7614 if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) && 7615 (ocp_reg_read(tp, 0xd068) & BIT(1))) { 7616 u16 swap_a, swap_b; 7617 7618 data = ocp_reg_read(tp, 0xd068); 7619 data &= ~0x1f; 7620 data |= 0x1; /* p0 */ 7621 ocp_reg_write(tp, 0xd068, data); 7622 swap_a = ocp_reg_read(tp, 0xd06a); 7623 data &= ~0x18; 7624 data |= 0x18; /* p3 */ 7625 ocp_reg_write(tp, 0xd068, data); 7626 swap_b = ocp_reg_read(tp, 0xd06a); 7627 data &= ~0x18; /* p0 */ 7628 ocp_reg_write(tp, 0xd068, data); 7629 ocp_reg_write(tp, 0xd06a, 7630 (swap_a & ~0x7ff) | (swap_b & 0x7ff)); 7631 data |= 0x18; /* p3 */ 7632 ocp_reg_write(tp, 0xd068, data); 7633 ocp_reg_write(tp, 0xd06a, 7634 (swap_b & ~0x7ff) | (swap_a & 0x7ff)); 7635 data &= ~0x18; 7636 data |= 0x08; /* p1 */ 7637 ocp_reg_write(tp, 0xd068, data); 7638 swap_a = ocp_reg_read(tp, 0xd06a); 7639 data &= ~0x18; 7640 data |= 0x10; /* p2 */ 7641 ocp_reg_write(tp, 0xd068, data); 7642 swap_b = ocp_reg_read(tp, 0xd06a); 7643 data &= ~0x18; 7644 data |= 0x08; /* p1 */ 7645 ocp_reg_write(tp, 0xd068, data); 7646 ocp_reg_write(tp, 0xd06a, 7647 (swap_a & ~0x7ff) | (swap_b & 0x7ff)); 7648 data &= ~0x18; 7649 data |= 0x10; /* p2 */ 7650 ocp_reg_write(tp, 0xd068, data); 7651 ocp_reg_write(tp, 0xd06a, 7652 (swap_b & ~0x7ff) | (swap_a & 0x7ff)); 7653 swap_a = ocp_reg_read(tp, 0xbd5a); 7654 swap_b = ocp_reg_read(tp, 0xbd5c); 7655 ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) | 7656 ((swap_b & 0x1f) << 8) | 7657 ((swap_b >> 8) & 0x1f)); 7658 ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) | 7659 ((swap_a & 0x1f) << 8) | 7660 ((swap_a >> 8) & 0x1f)); 7661 swap_a = ocp_reg_read(tp, 0xbc18); 7662 swap_b = ocp_reg_read(tp, 0xbc1a); 7663 ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) | 7664 ((swap_b & 0x1f) << 8) | 7665 ((swap_b >> 8) & 0x1f)); 7666 ocp_reg_write(tp, 0xbc1a, (swap_b & ~0x1f1f) | 7667 ((swap_a & 0x1f) << 8) | 7668 ((swap_a >> 8) & 0x1f)); 7669 } 7670 7671 /* Notify the MAC when the speed is changed to force mode. */ 7672 ocp_reg_set_bits(tp, OCP_INTR_EN, INTR_SPEED_FORCE); 7673 break; 7674 default: 7675 break; 7676 } 7677 7678 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 7679 7680 ocp_reg_clr_bits(tp, 0xa428, BIT(9)); 7681 ocp_reg_clr_bits(tp, 0xa5ea, BIT(0)); 7682 tp->ups_info.lite_mode = 0; 7683 7684 if (tp->eee_en) 7685 rtl_eee_enable(tp, true); 7686 7687 r8153_aldps_en(tp, true); 7688 r8152b_enable_fc(tp); 7689 r8153_u2p3en(tp, true); 7690 7691 set_bit(PHY_RESET, &tp->flags); 7692 } 7693 7694 static void r8156b_hw_phy_cfg(struct r8152 *tp) 7695 { 7696 u16 data; 7697 7698 switch (tp->version) { 7699 case RTL_VER_12: 7700 ocp_reg_write(tp, 0xbf86, 0x9000); 7701 ocp_reg_set_bits(tp, 0xc402, BIT(10)); 7702 ocp_reg_clr_bits(tp, 0xc402, BIT(10)); 7703 ocp_reg_write(tp, 0xbd86, 0x1010); 7704 ocp_reg_write(tp, 0xbd88, 0x1010); 7705 ocp_reg_w0w1(tp, 0xbd4e, BIT(10) | BIT(11), BIT(11)); 7706 ocp_reg_w0w1(tp, 0xbf46, 0xf00, 0x700); 7707 break; 7708 case RTL_VER_13: 7709 case RTL_VER_15: 7710 r8156b_wait_loading_flash(tp); 7711 break; 7712 default: 7713 break; 7714 } 7715 7716 ocp_word_test_and_clr_bits(tp, MCU_TYPE_USB, USB_MISC_0, PCUT_STATUS); 7717 7718 data = r8153_phy_status(tp, 0); 7719 switch (data) { 7720 case PHY_STAT_EXT_INIT: 7721 rtl8152_apply_firmware(tp, true); 7722 7723 ocp_reg_clr_bits(tp, 0xa466, BIT(0)); 7724 ocp_reg_clr_bits(tp, 0xa468, BIT(3) | BIT(1)); 7725 break; 7726 case PHY_STAT_LAN_ON: 7727 case PHY_STAT_PWRDN: 7728 default: 7729 rtl8152_apply_firmware(tp, false); 7730 break; 7731 } 7732 7733 r8152_mdio_test_and_clr_bit(tp, MII_BMCR, BMCR_PDOWN); 7734 7735 /* disable ALDPS before updating the PHY parameters */ 7736 r8153_aldps_en(tp, false); 7737 7738 /* disable EEE before updating the PHY parameters */ 7739 rtl_eee_enable(tp, false); 7740 7741 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7742 WARN_ON_ONCE(data != PHY_STAT_LAN_ON); 7743 7744 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_PHY_PWR, PFM_PWM_SWITCH); 7745 7746 switch (tp->version) { 7747 case RTL_VER_12: 7748 ocp_reg_set_bits(tp, 0xbc08, BIT(3) | BIT(2)); 7749 7750 sram_write_w0w1(tp, 0x8fff, 0xff00, 0x0400); 7751 7752 ocp_reg_set_bits(tp, 0xacda, 0xff00); 7753 ocp_reg_set_bits(tp, 0xacde, 0xf000); 7754 ocp_reg_write(tp, 0xac8c, 0x0ffc); 7755 ocp_reg_write(tp, 0xac46, 0xb7b4); 7756 ocp_reg_write(tp, 0xac50, 0x0fbc); 7757 ocp_reg_write(tp, 0xac3c, 0x9240); 7758 ocp_reg_write(tp, 0xac4e, 0x0db4); 7759 ocp_reg_write(tp, 0xacc6, 0x0707); 7760 ocp_reg_write(tp, 0xacc8, 0xa0d3); 7761 ocp_reg_write(tp, 0xad08, 0x0007); 7762 7763 sram2_write(tp, 0x8560, 0x19cc); 7764 sram2_write(tp, 0x8562, 0x19cc); 7765 sram2_write(tp, 0x8564, 0x19cc); 7766 sram2_write(tp, 0x8566, 0x147d); 7767 sram2_write(tp, 0x8568, 0x147d); 7768 sram2_write(tp, 0x856a, 0x147d); 7769 sram2_write(tp, 0x8ffe, 0x0907); 7770 sram2_write(tp, 0x80d6, 0x2801); 7771 sram2_write(tp, 0x80f2, 0x2801); 7772 sram2_write(tp, 0x80f4, 0x6077); 7773 ocp_reg_write(tp, 0xb506, 0x01e7); 7774 7775 sram2_write(tp, 0x8013, 0x0700); 7776 sram2_write(tp, 0x8fb9, 0x2801); 7777 sram2_write(tp, 0x8fba, 0x0100); 7778 sram2_write(tp, 0x8fbc, 0x1900); 7779 sram2_write(tp, 0x8fbe, 0xe100); 7780 sram2_write(tp, 0x8fc0, 0x0800); 7781 sram2_write(tp, 0x8fc2, 0xe500); 7782 sram2_write(tp, 0x8fc4, 0x0f00); 7783 sram2_write(tp, 0x8fc6, 0xf100); 7784 sram2_write(tp, 0x8fc8, 0x0400); 7785 sram2_write(tp, 0x8fca, 0xf300); 7786 sram2_write(tp, 0x8fcc, 0xfd00); 7787 sram2_write(tp, 0x8fce, 0xff00); 7788 sram2_write(tp, 0x8fd0, 0xfb00); 7789 sram2_write(tp, 0x8fd2, 0x0100); 7790 sram2_write(tp, 0x8fd4, 0xf400); 7791 sram2_write(tp, 0x8fd6, 0xff00); 7792 sram2_write(tp, 0x8fd8, 0xf600); 7793 7794 ocp_byte_set_bits(tp, MCU_TYPE_PLA, PLA_USB_CFG, 7795 EN_XG_LIP | EN_G_LIP); 7796 7797 sram2_write(tp, 0x813d, 0x390e); 7798 sram2_write(tp, 0x814f, 0x790e); 7799 sram2_write(tp, 0x80b0, 0x0f31); 7800 ocp_reg_set_bits(tp, 0xbf4c, BIT(1)); 7801 ocp_reg_set_bits(tp, 0xbcca, BIT(9) | BIT(8)); 7802 sram2_write(tp, 0x8141, 0x320e); 7803 sram2_write(tp, 0x8153, 0x720e); 7804 sram2_write(tp, 0x8529, 0x050e); 7805 ocp_reg_clr_bits(tp, OCP_EEE_CFG, CTAP_SHORT_EN); 7806 7807 sram_write(tp, 0x816c, 0xc4a0); 7808 sram_write(tp, 0x8170, 0xc4a0); 7809 sram_write(tp, 0x8174, 0x04a0); 7810 sram_write(tp, 0x8178, 0x04a0); 7811 sram_write(tp, 0x817c, 0x0719); 7812 sram_write(tp, 0x8ff4, 0x0400); 7813 sram_write(tp, 0x8ff1, 0x0404); 7814 7815 ocp_reg_write(tp, 0xbf4a, 0x001b); 7816 sram2_write(tp, 0x8033, 0x7c13); 7817 sram2_write(tp, 0x8037, 0x7c13); 7818 sram2_write(tp, 0x803b, 0xfc32); 7819 sram2_write(tp, 0x803f, 0x7c13); 7820 sram2_write(tp, 0x8043, 0x7c13); 7821 sram2_write(tp, 0x8047, 0x7c13); 7822 7823 sram2_write(tp, 0x8145, 0x370e); 7824 sram2_write(tp, 0x8157, 0x770e); 7825 sram2_write(tp, 0x8169, 0x0d0a); 7826 sram2_write(tp, 0x817b, 0x1d0a); 7827 7828 sram_write_w0w1(tp, 0x8217, 0xff00, 0x5000); 7829 sram_write_w0w1(tp, 0x821a, 0xff00, 0x5000); 7830 sram_write(tp, 0x80da, 0x0403); 7831 sram_write_w0w1(tp, 0x80dc, 0xff00, 0x1000); 7832 sram_write(tp, 0x80b3, 0x0384); 7833 sram_write(tp, 0x80b7, 0x2007); 7834 sram_write_w0w1(tp, 0x80ba, 0xff00, 0x6c00); 7835 sram_write(tp, 0x80b5, 0xf009); 7836 sram_write_w0w1(tp, 0x80bd, 0xff00, 0x9f00); 7837 sram_write(tp, 0x80c7, 0xf083); 7838 sram_write(tp, 0x80dd, 0x03f0); 7839 sram_write_w0w1(tp, 0x80df, 0xff00, 0x1000); 7840 sram_write(tp, 0x80cb, 0x2007); 7841 sram_write_w0w1(tp, 0x80ce, 0xff00, 0x6c00); 7842 sram_write(tp, 0x80c9, 0x8009); 7843 sram_write_w0w1(tp, 0x80d1, 0xff00, 0x8000); 7844 sram_write(tp, 0x80a3, 0x200a); 7845 sram_write(tp, 0x80a5, 0xf0ad); 7846 sram_write(tp, 0x809f, 0x6073); 7847 sram_write(tp, 0x80a1, 0x000b); 7848 sram_write_w0w1(tp, 0x80a9, 0xff00, 0xc000); 7849 7850 if (rtl_phy_patch_request(tp, true, true)) 7851 return; 7852 7853 ocp_reg_clr_bits(tp, 0xb896, BIT(0)); 7854 ocp_reg_clr_bits(tp, 0xb892, 0xff00); 7855 ocp_reg_write(tp, 0xb88e, 0xc23e); 7856 ocp_reg_write(tp, 0xb890, 0x0000); 7857 ocp_reg_write(tp, 0xb88e, 0xc240); 7858 ocp_reg_write(tp, 0xb890, 0x0103); 7859 ocp_reg_write(tp, 0xb88e, 0xc242); 7860 ocp_reg_write(tp, 0xb890, 0x0507); 7861 ocp_reg_write(tp, 0xb88e, 0xc244); 7862 ocp_reg_write(tp, 0xb890, 0x090b); 7863 ocp_reg_write(tp, 0xb88e, 0xc246); 7864 ocp_reg_write(tp, 0xb890, 0x0c0e); 7865 ocp_reg_write(tp, 0xb88e, 0xc248); 7866 ocp_reg_write(tp, 0xb890, 0x1012); 7867 ocp_reg_write(tp, 0xb88e, 0xc24a); 7868 ocp_reg_write(tp, 0xb890, 0x1416); 7869 ocp_reg_set_bits(tp, 0xb896, BIT(0)); 7870 7871 rtl_phy_patch_request(tp, false, true); 7872 7873 ocp_reg_set_bits(tp, 0xa86a, BIT(0)); 7874 ocp_reg_set_bits(tp, 0xa6f0, BIT(0)); 7875 7876 ocp_reg_write(tp, 0xbfa0, 0xd70d); 7877 ocp_reg_write(tp, 0xbfa2, 0x4100); 7878 ocp_reg_write(tp, 0xbfa4, 0xe868); 7879 ocp_reg_write(tp, 0xbfa6, 0xdc59); 7880 ocp_reg_write(tp, 0xb54c, 0x3c18); 7881 ocp_reg_clr_bits(tp, 0xbfa4, BIT(5)); 7882 sram_set_bits(tp, 0x817d, BIT(12)); 7883 break; 7884 case RTL_VER_13: 7885 /* 2.5G INRX */ 7886 ocp_reg_w0w1(tp, 0xac46, 0x00f0, 0x0090); 7887 ocp_reg_w0w1(tp, 0xad30, 0x0003, 0x0001); 7888 fallthrough; 7889 case RTL_VER_15: 7890 /* EEE parameter */ 7891 sram2_write(tp, 0x80f5, 0x760e); 7892 sram2_write(tp, 0x8107, 0x360e); 7893 sram2_write_w0w1(tp, 0x8551, 0xff00, 0x0800); 7894 7895 /* ADC_PGA parameter */ 7896 ocp_reg_w0w1(tp, 0xbf00, 0xe000, 0xa000); 7897 ocp_reg_w0w1(tp, 0xbf46, 0x0f00, 0x0300); 7898 7899 /* Green Table-PGA, 1G full viterbi */ 7900 sram_write(tp, 0x8044, 0x2417); 7901 sram_write(tp, 0x804a, 0x2417); 7902 sram_write(tp, 0x8050, 0x2417); 7903 sram_write(tp, 0x8056, 0x2417); 7904 sram_write(tp, 0x805c, 0x2417); 7905 sram_write(tp, 0x8062, 0x2417); 7906 sram_write(tp, 0x8068, 0x2417); 7907 sram_write(tp, 0x806e, 0x2417); 7908 sram_write(tp, 0x8074, 0x2417); 7909 sram_write(tp, 0x807a, 0x2417); 7910 7911 /* XG PLL */ 7912 ocp_reg_w0w1(tp, 0xbf84, 0xe000, 0xa000); 7913 break; 7914 default: 7915 break; 7916 } 7917 7918 /* Notify the MAC when the speed is changed to force mode. */ 7919 ocp_reg_set_bits(tp, OCP_INTR_EN, INTR_SPEED_FORCE); 7920 7921 if (rtl_phy_patch_request(tp, true, true)) 7922 return; 7923 7924 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, EEE_SPDWN_EN); 7925 7926 ocp_reg_w0w1(tp, OCP_DOWN_SPEED, EN_EEE_100 | EN_EEE_1000, 7927 EN_10M_CLKDIV); 7928 tp->ups_info._10m_ckdiv = true; 7929 tp->ups_info.eee_plloff_100 = false; 7930 tp->ups_info.eee_plloff_giga = false; 7931 7932 ocp_reg_clr_bits(tp, OCP_POWER_CFG, EEE_CLKDIV_EN); 7933 tp->ups_info.eee_ckdiv = false; 7934 7935 rtl_phy_patch_request(tp, false, true); 7936 7937 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 7938 7939 ocp_reg_clr_bits(tp, 0xa428, BIT(9)); 7940 ocp_reg_clr_bits(tp, 0xa5ea, BIT(0)); 7941 tp->ups_info.lite_mode = 0; 7942 7943 if (tp->eee_en) 7944 rtl_eee_enable(tp, true); 7945 7946 r8153_aldps_en(tp, true); 7947 r8152b_enable_fc(tp); 7948 r8153_u2p3en(tp, true); 7949 7950 set_bit(PHY_RESET, &tp->flags); 7951 } 7952 7953 static void r8157_hw_phy_cfg(struct r8152 *tp) 7954 { 7955 u32 ocp_data; 7956 u16 data; 7957 int ret; 7958 7959 r8156b_wait_loading_flash(tp); 7960 7961 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 7962 if (ocp_data & PCUT_STATUS) { 7963 ocp_data &= ~PCUT_STATUS; 7964 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 7965 } 7966 7967 data = r8153_phy_status(tp, 0); 7968 switch (data) { 7969 case PHY_STAT_EXT_INIT: 7970 ocp_reg_clr_bits(tp, 0xa466, BIT(0)); 7971 ocp_reg_clr_bits(tp, 0xa468, BIT(3) | BIT(1)); 7972 break; 7973 case PHY_STAT_LAN_ON: 7974 case PHY_STAT_PWRDN: 7975 default: 7976 break; 7977 } 7978 7979 data = r8152_mdio_read(tp, MII_BMCR); 7980 if (data & BMCR_PDOWN) { 7981 data &= ~BMCR_PDOWN; 7982 r8152_mdio_write(tp, MII_BMCR, data); 7983 } 7984 7985 r8153_aldps_en(tp, false); 7986 rtl_eee_enable(tp, false); 7987 7988 ret = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7989 if (ret < 0) 7990 return; 7991 WARN_ON_ONCE(ret != PHY_STAT_LAN_ON); 7992 7993 /* PFM mode */ 7994 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_PHY_PWR, PFM_PWM_SWITCH); 7995 7996 /* Advanced Power Saving parameter */ 7997 ocp_reg_set_bits(tp, 0xa430, BIT(0) | BIT(1)); 7998 7999 /* aldpsce force mode */ 8000 ocp_reg_clr_bits(tp, 0xa44a, BIT(2)); 8001 8002 switch (tp->version) { 8003 case RTL_VER_16: 8004 /* XG_INRX parameter */ 8005 sram_write_w0w1(tp, 0x8183, 0xff00, 0x5900); 8006 ocp_reg_set_bits(tp, 0xa654, BIT(11)); 8007 ocp_reg_set_bits(tp, 0xb648, BIT(14)); 8008 ocp_reg_clr_bits(tp, 0xad2c, BIT(15)); 8009 ocp_reg_set_bits(tp, 0xad94, BIT(5)); 8010 ocp_reg_set_bits(tp, 0xada0, BIT(1)); 8011 ocp_reg_w0w1(tp, 0xae06, 0xfc00, 0x7c00); 8012 sram2_write_w0w1(tp, 0x8647, 0xff00, 0xe600); 8013 sram2_write_w0w1(tp, 0x8036, 0xff00, 0x3000); 8014 sram2_write_w0w1(tp, 0x8078, 0xff00, 0x3000); 8015 8016 /* green mode */ 8017 sram2_write_w0w1(tp, 0x89e9, 0xff00, 0); 8018 sram2_write_w0w1(tp, 0x8ffd, 0xff00, 0x0100); 8019 sram2_write_w0w1(tp, 0x8ffe, 0xff00, 0x0200); 8020 sram2_write_w0w1(tp, 0x8fff, 0xff00, 0x0400); 8021 8022 /* recognize AQC/Bcom function */ 8023 sram_write_w0w1(tp, 0x8018, 0xff00, 0x7700); 8024 ocp_reg_write(tp, OCP_SRAM_ADDR, 0x8f9c); 8025 ocp_reg_write(tp, OCP_SRAM_DATA, 0x0005); 8026 ocp_reg_write(tp, OCP_SRAM_DATA, 0x0000); 8027 ocp_reg_write(tp, OCP_SRAM_DATA, 0x00ed); 8028 ocp_reg_write(tp, OCP_SRAM_DATA, 0x0502); 8029 ocp_reg_write(tp, OCP_SRAM_DATA, 0x0b00); 8030 ocp_reg_write(tp, OCP_SRAM_DATA, 0xd401); 8031 sram_write_w0w1(tp, 0x8fa8, 0xff00, 0x2900); 8032 8033 /* RFI_corr_thd 5g */ 8034 sram2_write_w0w1(tp, 0x814b, 0xff00, 0x1100); 8035 sram2_write_w0w1(tp, 0x814d, 0xff00, 0x1100); 8036 sram2_write_w0w1(tp, 0x814f, 0xff00, 0x0b00); 8037 sram2_write_w0w1(tp, 0x8142, 0xff00, 0x0100); 8038 sram2_write_w0w1(tp, 0x8144, 0xff00, 0x0100); 8039 sram2_write_w0w1(tp, 0x8150, 0xff00, 0x0100); 8040 8041 /* RFI_corr_thd 2p5g */ 8042 sram2_write_w0w1(tp, 0x8118, 0xff00, 0x0700); 8043 sram2_write_w0w1(tp, 0x811a, 0xff00, 0x0700); 8044 sram2_write_w0w1(tp, 0x811c, 0xff00, 0x0500); 8045 sram2_write_w0w1(tp, 0x810f, 0xff00, 0x0100); 8046 sram2_write_w0w1(tp, 0x8111, 0xff00, 0x0100); 8047 sram2_write_w0w1(tp, 0x811d, 0xff00, 0x0100); 8048 8049 /* RFI parameter */ 8050 ocp_reg_clr_bits(tp, 0xad1c, BIT(8)); 8051 ocp_reg_w0w1(tp, 0xade8, 0xffc0, 0x1400); 8052 sram2_write_w0w1(tp, 0x864b, 0xff00, 0x9d00); 8053 sram2_write_w0w1(tp, 0x862c, 0xff00, 0x1200); 8054 ocp_reg_write(tp, OCP_SRAM_ADDR, 0x8566); 8055 ocp_reg_write(tp, OCP_SRAM_DATA, 0x003f); 8056 ocp_reg_write(tp, OCP_SRAM_DATA, 0x3f02); 8057 ocp_reg_write(tp, OCP_SRAM_DATA, 0x023c); 8058 ocp_reg_write(tp, OCP_SRAM_DATA, 0x3b0a); 8059 ocp_reg_write(tp, OCP_SRAM_DATA, 0x1c00); 8060 ocp_reg_write(tp, OCP_SRAM_DATA, 0x0000); 8061 ocp_reg_write(tp, OCP_SRAM_DATA, 0x0000); 8062 ocp_reg_write(tp, OCP_SRAM_DATA, 0x0000); 8063 ocp_reg_write(tp, OCP_SRAM_DATA, 0x0000); 8064 8065 /* RFI-color noise gen parameter 5g */ 8066 ocp_reg_set_bits(tp, 0xad9c, BIT(5)); 8067 sram2_write_w0w1(tp, 0x8122, 0xff00, 0x0c00); 8068 ocp_reg_write(tp, OCP_SRAM2_ADDR, 0x82c8); 8069 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03ed); 8070 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03ff); 8071 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0009); 8072 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03fe); 8073 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x000b); 8074 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0021); 8075 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03f7); 8076 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03b8); 8077 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03e0); 8078 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0049); 8079 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0049); 8080 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03e0); 8081 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03b8); 8082 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03f7); 8083 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0021); 8084 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x000b); 8085 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03fe); 8086 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0009); 8087 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03ff); 8088 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03ed); 8089 8090 /* RFI-color noise gen parameter 2p5g */ 8091 sram2_write_w0w1(tp, 0x80ef, 0xff00, 0x0c00); 8092 ocp_reg_write(tp, OCP_SRAM2_ADDR, 0x82a0); 8093 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x000e); 8094 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03fe); 8095 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03ed); 8096 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0006); 8097 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x001a); 8098 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03f1); 8099 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03d8); 8100 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0023); 8101 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0054); 8102 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0322); 8103 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x00dd); 8104 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03ab); 8105 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03dc); 8106 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0027); 8107 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x000e); 8108 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03e5); 8109 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03f9); 8110 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0012); 8111 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x0001); 8112 ocp_reg_write(tp, OCP_SRAM2_DATA, 0x03f1); 8113 8114 /* modify thermal speed down threshold */ 8115 ocp_reg_w0w1(tp, 0xb54c, 0xffc0, 0x3700); 8116 8117 /* XG compatibility modification */ 8118 ocp_reg_set_bits(tp, 0xb648, BIT(6)); 8119 sram2_write_w0w1(tp, 0x8082, 0xff00, 0x5d00); 8120 sram2_write_w0w1(tp, 0x807c, 0xff00, 0x5000); 8121 sram2_write_w0w1(tp, 0x809d, 0xff00, 0x5000); 8122 break; 8123 default: 8124 break; 8125 } 8126 8127 if (rtl_phy_patch_request(tp, true, true)) 8128 return; 8129 8130 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, EEE_SPDWN_EN); 8131 8132 ocp_reg_w0w1(tp, OCP_DOWN_SPEED, EN_EEE_100 | EN_EEE_1000, EN_10M_CLKDIV); 8133 8134 tp->ups_info._10m_ckdiv = true; 8135 tp->ups_info.eee_plloff_100 = false; 8136 tp->ups_info.eee_plloff_giga = false; 8137 8138 ocp_reg_set_bits(tp, OCP_POWER_CFG, EEE_CLKDIV_EN); 8139 tp->ups_info.eee_ckdiv = true; 8140 8141 rtl_phy_patch_request(tp, false, true); 8142 8143 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 8144 8145 ocp_reg_clr_bits(tp, 0xa428, BIT(9)); 8146 ocp_reg_clr_bits(tp, 0xa5ea, BIT(0) | BIT(1)); 8147 tp->ups_info.lite_mode = 0; 8148 8149 if (tp->eee_en) 8150 rtl_eee_enable(tp, true); 8151 8152 r8153_aldps_en(tp, true); 8153 r8152b_enable_fc(tp); 8154 8155 set_bit(PHY_RESET, &tp->flags); 8156 } 8157 8158 static void r8156_init(struct r8152 *tp) 8159 { 8160 u32 ocp_data; 8161 u16 data; 8162 int i; 8163 8164 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 8165 return; 8166 8167 if (tp->version == RTL_VER_16) { 8168 ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xcffe, BIT(3)); 8169 ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xd3ca, BIT(0)); 8170 } 8171 8172 ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_ECM_OP, EN_ALL_SPEED); 8173 8174 if (tp->version != RTL_VER_16) 8175 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0); 8176 8177 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_ECM_OPTION, BYPASS_MAC_RESET); 8178 8179 if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_15) 8180 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_U2P3_CTRL, RX_DETECT8); 8181 8182 r8153b_u1u2en(tp, false); 8183 8184 switch (tp->version) { 8185 case RTL_VER_13: 8186 case RTL_VER_15: 8187 case RTL_VER_16: 8188 r8156b_wait_loading_flash(tp); 8189 break; 8190 default: 8191 break; 8192 } 8193 8194 for (i = 0; i < 500; i++) { 8195 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 8196 AUTOLOAD_DONE) 8197 break; 8198 8199 msleep(20); 8200 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 8201 return; 8202 } 8203 8204 data = r8153_phy_status(tp, 0); 8205 if (data == PHY_STAT_EXT_INIT) { 8206 ocp_reg_clr_bits(tp, 0xa468, BIT(3) | BIT(1)); 8207 if (tp->version >= RTL_VER_12) 8208 ocp_reg_clr_bits(tp, 0xa466, BIT(0)); 8209 } 8210 8211 data = r8152_mdio_read(tp, MII_BMCR); 8212 if (data & BMCR_PDOWN) { 8213 data &= ~BMCR_PDOWN; 8214 r8152_mdio_write(tp, MII_BMCR, data); 8215 } 8216 8217 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 8218 8219 if (tp->version == RTL_VER_16) 8220 r8157_u2p3en(tp, false); 8221 else 8222 r8153_u2p3en(tp, false); 8223 8224 /* MSC timer = 0xfff * 8ms = 32760 ms */ 8225 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 8226 8227 /* U1/U2/L1 idle timer. 500 us */ 8228 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); 8229 8230 if (tp->version == RTL_VER_16) 8231 r8157_power_cut_en(tp, false); 8232 else 8233 r8153b_power_cut_en(tp, false); 8234 8235 r8156_ups_en(tp, false); 8236 r8153_queue_wake(tp, false); 8237 rtl_runtime_suspend_enable(tp, false); 8238 8239 if (tp->udev->speed >= USB_SPEED_SUPER) 8240 r8153b_u1u2en(tp, true); 8241 8242 usb_enable_lpm(tp->udev); 8243 8244 if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_15) { 8245 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, SLOT_EN); 8246 8247 ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_CPCR, FLOW_CTRL_EN); 8248 8249 /* enable fc timer and set timer to 600 ms. */ 8250 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, CTRL_TIMER_EN | (600 / 8)); 8251 8252 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); 8253 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN)) 8254 ocp_data |= FLOW_CTRL_PATCH_2; 8255 ocp_data &= ~AUTO_SPEEDUP; 8256 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); 8257 8258 ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK); 8259 } 8260 8261 r8156_mac_clk_spd(tp, true); 8262 8263 if (tp->version != RTL_VER_16) 8264 ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, PLA_MCU_SPDWN_EN); 8265 8266 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 8267 if (rtl8152_get_speed(tp) & LINK_STATUS) 8268 ocp_data |= CUR_LINK_OK; 8269 else 8270 ocp_data &= ~CUR_LINK_OK; 8271 ocp_data |= POLL_LINK_CHG; 8272 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 8273 8274 set_bit(GREEN_ETHERNET, &tp->flags); 8275 8276 /* rx aggregation / 16 bytes Rx descriptor */ 8277 if (tp->version == RTL_VER_16) 8278 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, RX_AGG_DISABLE | RX_DESC_16B); 8279 else 8280 ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_USB_CTRL, RX_AGG_DISABLE | RX_ZERO_EN); 8281 8282 if (tp->version < RTL_VER_12) 8283 ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ACT_ODMA); 8284 8285 if (tp->version == RTL_VER_16) { 8286 /* Disable Rx Zero Len */ 8287 rtl_bmu_clr_bits(tp, 0x2300, BIT(3)); 8288 /* TX descriptor Signature */ 8289 ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xd4ae, BIT(1)); 8290 } 8291 8292 r8156_mdio_force_mode(tp); 8293 rtl_tally_reset(tp); 8294 8295 tp->coalesce = 15000; /* 15 us */ 8296 } 8297 8298 static bool rtl_check_vendor_ok(struct usb_interface *intf) 8299 { 8300 struct usb_host_interface *alt = intf->cur_altsetting; 8301 struct usb_endpoint_descriptor *in, *out, *intr; 8302 8303 if (usb_find_common_endpoints(alt, &in, &out, &intr, NULL) < 0) { 8304 dev_err(&intf->dev, "Expected endpoints are not found\n"); 8305 return false; 8306 } 8307 8308 /* Check Rx endpoint address */ 8309 if (usb_endpoint_num(in) != 1) { 8310 dev_err(&intf->dev, "Invalid Rx endpoint address\n"); 8311 return false; 8312 } 8313 8314 /* Check Tx endpoint address */ 8315 if (usb_endpoint_num(out) != 2) { 8316 dev_err(&intf->dev, "Invalid Tx endpoint address\n"); 8317 return false; 8318 } 8319 8320 /* Check interrupt endpoint address */ 8321 if (usb_endpoint_num(intr) != 3) { 8322 dev_err(&intf->dev, "Invalid interrupt endpoint address\n"); 8323 return false; 8324 } 8325 8326 return true; 8327 } 8328 8329 static int rtl8152_pre_reset(struct usb_interface *intf) 8330 { 8331 struct r8152 *tp = usb_get_intfdata(intf); 8332 struct net_device *netdev; 8333 8334 rtnl_lock(); 8335 8336 if (!tp || !test_bit(PROBED_WITH_NO_ERRORS, &tp->flags)) 8337 return 0; 8338 8339 netdev = tp->netdev; 8340 if (!netif_running(netdev)) 8341 return 0; 8342 8343 netif_stop_queue(netdev); 8344 tasklet_disable(&tp->tx_tl); 8345 clear_bit(WORK_ENABLE, &tp->flags); 8346 usb_kill_urb(tp->intr_urb); 8347 cancel_delayed_work_sync(&tp->schedule); 8348 napi_disable(&tp->napi); 8349 if (netif_carrier_ok(netdev)) { 8350 mutex_lock(&tp->control); 8351 set_bit(IN_PRE_RESET, &tp->flags); 8352 tp->rtl_ops.disable(tp); 8353 clear_bit(IN_PRE_RESET, &tp->flags); 8354 mutex_unlock(&tp->control); 8355 } 8356 8357 return 0; 8358 } 8359 8360 static int rtl8152_post_reset(struct usb_interface *intf) 8361 { 8362 struct r8152 *tp = usb_get_intfdata(intf); 8363 struct net_device *netdev; 8364 struct sockaddr_storage ss; 8365 8366 if (!tp || !test_bit(PROBED_WITH_NO_ERRORS, &tp->flags)) 8367 goto exit; 8368 8369 rtl_set_accessible(tp); 8370 8371 /* reset the MAC address in case of policy change */ 8372 if (determine_ethernet_addr(tp, &ss) >= 0) 8373 dev_set_mac_address(tp->netdev, &ss, NULL); 8374 8375 netdev = tp->netdev; 8376 if (!netif_running(netdev)) 8377 goto exit; 8378 8379 set_bit(WORK_ENABLE, &tp->flags); 8380 if (netif_carrier_ok(netdev)) { 8381 mutex_lock(&tp->control); 8382 tp->rtl_ops.enable(tp); 8383 rtl_start_rx(tp); 8384 _rtl8152_set_rx_mode(netdev); 8385 mutex_unlock(&tp->control); 8386 } 8387 8388 napi_enable(&tp->napi); 8389 tasklet_enable(&tp->tx_tl); 8390 netif_wake_queue(netdev); 8391 usb_submit_urb(tp->intr_urb, GFP_KERNEL); 8392 8393 if (!list_empty(&tp->rx_done)) 8394 napi_schedule(&tp->napi); 8395 8396 exit: 8397 rtnl_unlock(); 8398 return 0; 8399 } 8400 8401 static bool delay_autosuspend(struct r8152 *tp) 8402 { 8403 bool sw_linking = !!netif_carrier_ok(tp->netdev); 8404 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS); 8405 8406 /* This means a linking change occurs and the driver doesn't detect it, 8407 * yet. If the driver has disabled tx/rx and hw is linking on, the 8408 * device wouldn't wake up by receiving any packet. 8409 */ 8410 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking) 8411 return true; 8412 8413 /* If the linking down is occurred by nway, the device may miss the 8414 * linking change event. And it wouldn't wake when linking on. 8415 */ 8416 if (!sw_linking && tp->rtl_ops.in_nway(tp)) 8417 return true; 8418 else if (!skb_queue_empty(&tp->tx_queue)) 8419 return true; 8420 else 8421 return false; 8422 } 8423 8424 static int rtl8152_runtime_resume(struct r8152 *tp) 8425 { 8426 struct net_device *netdev = tp->netdev; 8427 8428 if (netif_running(netdev) && netdev->flags & IFF_UP) { 8429 struct napi_struct *napi = &tp->napi; 8430 8431 tp->rtl_ops.autosuspend_en(tp, false); 8432 napi_disable(napi); 8433 set_bit(WORK_ENABLE, &tp->flags); 8434 8435 if (netif_carrier_ok(netdev)) { 8436 if (rtl8152_get_speed(tp) & LINK_STATUS) { 8437 rtl_start_rx(tp); 8438 } else { 8439 netif_carrier_off(netdev); 8440 tp->rtl_ops.disable(tp); 8441 netif_info(tp, link, netdev, "linking down\n"); 8442 } 8443 } 8444 8445 napi_enable(napi); 8446 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8447 smp_mb__after_atomic(); 8448 8449 if (!list_empty(&tp->rx_done)) 8450 napi_schedule(&tp->napi); 8451 8452 usb_submit_urb(tp->intr_urb, GFP_NOIO); 8453 } else { 8454 if (netdev->flags & IFF_UP) 8455 tp->rtl_ops.autosuspend_en(tp, false); 8456 8457 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8458 } 8459 8460 return 0; 8461 } 8462 8463 static int rtl8152_system_resume(struct r8152 *tp) 8464 { 8465 struct net_device *netdev = tp->netdev; 8466 8467 netif_device_attach(netdev); 8468 8469 if (netif_running(netdev) && (netdev->flags & IFF_UP)) { 8470 tp->rtl_ops.up(tp); 8471 netif_carrier_off(netdev); 8472 set_bit(WORK_ENABLE, &tp->flags); 8473 usb_submit_urb(tp->intr_urb, GFP_NOIO); 8474 } 8475 8476 return 0; 8477 } 8478 8479 static int rtl8152_runtime_suspend(struct r8152 *tp) 8480 { 8481 struct net_device *netdev = tp->netdev; 8482 int ret = 0; 8483 8484 if (!tp->rtl_ops.autosuspend_en) 8485 return -EBUSY; 8486 8487 set_bit(SELECTIVE_SUSPEND, &tp->flags); 8488 smp_mb__after_atomic(); 8489 8490 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { 8491 u32 rcr = 0; 8492 8493 if (netif_carrier_ok(netdev)) { 8494 u32 ocp_data; 8495 8496 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 8497 ocp_data = rcr & ~RCR_ACPT_ALL; 8498 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 8499 rxdy_gated_en(tp, true); 8500 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, 8501 PLA_OOB_CTRL); 8502 if (!(ocp_data & RXFIFO_EMPTY)) { 8503 rxdy_gated_en(tp, false); 8504 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); 8505 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8506 smp_mb__after_atomic(); 8507 ret = -EBUSY; 8508 goto out1; 8509 } 8510 } 8511 8512 clear_bit(WORK_ENABLE, &tp->flags); 8513 usb_kill_urb(tp->intr_urb); 8514 8515 tp->rtl_ops.autosuspend_en(tp, true); 8516 8517 if (netif_carrier_ok(netdev)) { 8518 struct napi_struct *napi = &tp->napi; 8519 8520 napi_disable(napi); 8521 rtl_stop_rx(tp); 8522 rxdy_gated_en(tp, false); 8523 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); 8524 napi_enable(napi); 8525 } 8526 8527 if (delay_autosuspend(tp)) { 8528 rtl8152_runtime_resume(tp); 8529 ret = -EBUSY; 8530 } 8531 } 8532 8533 out1: 8534 return ret; 8535 } 8536 8537 static int rtl8152_system_suspend(struct r8152 *tp) 8538 { 8539 struct net_device *netdev = tp->netdev; 8540 8541 netif_device_detach(netdev); 8542 8543 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { 8544 struct napi_struct *napi = &tp->napi; 8545 8546 clear_bit(WORK_ENABLE, &tp->flags); 8547 usb_kill_urb(tp->intr_urb); 8548 tasklet_disable(&tp->tx_tl); 8549 napi_disable(napi); 8550 cancel_delayed_work_sync(&tp->schedule); 8551 tp->rtl_ops.down(tp); 8552 napi_enable(napi); 8553 tasklet_enable(&tp->tx_tl); 8554 } 8555 8556 /* If we're inaccessible here then some of the work that we did to 8557 * get the adapter ready for suspend didn't work. Queue up a wakeup 8558 * event so we can try again. 8559 */ 8560 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 8561 pm_wakeup_event(&tp->udev->dev, 0); 8562 8563 return 0; 8564 } 8565 8566 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) 8567 { 8568 struct r8152 *tp = usb_get_intfdata(intf); 8569 int ret; 8570 8571 mutex_lock(&tp->control); 8572 8573 if (PMSG_IS_AUTO(message)) 8574 ret = rtl8152_runtime_suspend(tp); 8575 else 8576 ret = rtl8152_system_suspend(tp); 8577 8578 mutex_unlock(&tp->control); 8579 8580 return ret; 8581 } 8582 8583 static int rtl8152_resume(struct usb_interface *intf) 8584 { 8585 struct r8152 *tp = usb_get_intfdata(intf); 8586 bool runtime_resume = test_bit(SELECTIVE_SUSPEND, &tp->flags); 8587 int ret; 8588 8589 mutex_lock(&tp->control); 8590 8591 rtl_reset_ocp_base(tp); 8592 8593 if (runtime_resume) 8594 ret = rtl8152_runtime_resume(tp); 8595 else 8596 ret = rtl8152_system_resume(tp); 8597 8598 mutex_unlock(&tp->control); 8599 8600 /* If the device is RTL8152_INACCESSIBLE here then we should do a 8601 * reset. This is important because the usb_lock_device_for_reset() 8602 * that happens as a result of usb_queue_reset_device() will silently 8603 * fail if the device was suspended or if too much time passed. 8604 * 8605 * NOTE: The device is locked here so we can directly do the reset. 8606 * We don't need usb_lock_device_for_reset() because that's just a 8607 * wrapper over device_lock() and device_resume() (which calls us) 8608 * does that for us. 8609 */ 8610 if (!runtime_resume && test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 8611 usb_reset_device(tp->udev); 8612 8613 return ret; 8614 } 8615 8616 static int rtl8152_reset_resume(struct usb_interface *intf) 8617 { 8618 struct r8152 *tp = usb_get_intfdata(intf); 8619 8620 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8621 rtl_reset_ocp_base(tp); 8622 tp->rtl_ops.init(tp); 8623 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); 8624 set_ethernet_addr(tp, true); 8625 return rtl8152_resume(intf); 8626 } 8627 8628 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 8629 { 8630 struct r8152 *tp = netdev_priv(dev); 8631 8632 if (usb_autopm_get_interface(tp->intf) < 0) 8633 return; 8634 8635 if (!rtl_can_wakeup(tp)) { 8636 wol->supported = 0; 8637 wol->wolopts = 0; 8638 } else { 8639 mutex_lock(&tp->control); 8640 wol->supported = WAKE_ANY; 8641 wol->wolopts = __rtl_get_wol(tp); 8642 mutex_unlock(&tp->control); 8643 } 8644 8645 usb_autopm_put_interface(tp->intf); 8646 } 8647 8648 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 8649 { 8650 struct r8152 *tp = netdev_priv(dev); 8651 int ret; 8652 8653 if (!rtl_can_wakeup(tp)) 8654 return -EOPNOTSUPP; 8655 8656 if (wol->wolopts & ~WAKE_ANY) 8657 return -EINVAL; 8658 8659 ret = usb_autopm_get_interface(tp->intf); 8660 if (ret < 0) 8661 goto out_set_wol; 8662 8663 mutex_lock(&tp->control); 8664 8665 __rtl_set_wol(tp, wol->wolopts); 8666 tp->saved_wolopts = wol->wolopts & WAKE_ANY; 8667 8668 mutex_unlock(&tp->control); 8669 8670 usb_autopm_put_interface(tp->intf); 8671 8672 out_set_wol: 8673 return ret; 8674 } 8675 8676 static u32 rtl8152_get_msglevel(struct net_device *dev) 8677 { 8678 struct r8152 *tp = netdev_priv(dev); 8679 8680 return tp->msg_enable; 8681 } 8682 8683 static void rtl8152_set_msglevel(struct net_device *dev, u32 value) 8684 { 8685 struct r8152 *tp = netdev_priv(dev); 8686 8687 tp->msg_enable = value; 8688 } 8689 8690 static void rtl8152_get_drvinfo(struct net_device *netdev, 8691 struct ethtool_drvinfo *info) 8692 { 8693 struct r8152 *tp = netdev_priv(netdev); 8694 8695 strscpy(info->driver, MODULENAME, sizeof(info->driver)); 8696 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); 8697 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw)) 8698 strscpy(info->fw_version, tp->rtl_fw.version, 8699 sizeof(info->fw_version)); 8700 } 8701 8702 static 8703 int rtl8152_get_link_ksettings(struct net_device *netdev, 8704 struct ethtool_link_ksettings *cmd) 8705 { 8706 struct r8152 *tp = netdev_priv(netdev); 8707 int ret; 8708 8709 if (!tp->mii.mdio_read) 8710 return -EOPNOTSUPP; 8711 8712 ret = usb_autopm_get_interface(tp->intf); 8713 if (ret < 0) 8714 goto out; 8715 8716 mutex_lock(&tp->control); 8717 8718 mii_ethtool_get_link_ksettings(&tp->mii, cmd); 8719 8720 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8721 cmd->link_modes.supported, tp->support_2500full); 8722 8723 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 8724 cmd->link_modes.supported, tp->support_5000full); 8725 8726 if (tp->support_2500full || tp->support_5000full) { 8727 u16 ocp_10gbt_ctrl = ocp_reg_read(tp, OCP_10GBT_CTRL); 8728 u16 ocp_10gbt_stat = ocp_reg_read(tp, OCP_10GBT_STAT); 8729 8730 if (tp->support_2500full) { 8731 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8732 cmd->link_modes.advertising, 8733 ocp_10gbt_ctrl & MDIO_AN_10GBT_CTRL_ADV2_5G); 8734 8735 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8736 cmd->link_modes.lp_advertising, 8737 ocp_10gbt_stat & MDIO_AN_10GBT_STAT_LP2_5G); 8738 8739 if (is_speed_2500(rtl8152_get_speed(tp))) 8740 cmd->base.speed = SPEED_2500; 8741 } 8742 8743 if (tp->support_5000full) { 8744 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 8745 cmd->link_modes.advertising, 8746 ocp_10gbt_ctrl & MDIO_AN_10GBT_CTRL_ADV5G); 8747 8748 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 8749 cmd->link_modes.lp_advertising, 8750 ocp_10gbt_stat & MDIO_AN_10GBT_STAT_LP5G); 8751 8752 if (is_speed_5000(rtl8152_get_speed(tp))) 8753 cmd->base.speed = SPEED_5000; 8754 } 8755 } 8756 8757 mutex_unlock(&tp->control); 8758 8759 usb_autopm_put_interface(tp->intf); 8760 8761 out: 8762 return ret; 8763 } 8764 8765 static int rtl8152_set_link_ksettings(struct net_device *dev, 8766 const struct ethtool_link_ksettings *cmd) 8767 { 8768 struct r8152 *tp = netdev_priv(dev); 8769 u32 advertising = 0; 8770 int ret; 8771 8772 ret = usb_autopm_get_interface(tp->intf); 8773 if (ret < 0) 8774 goto out; 8775 8776 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, 8777 cmd->link_modes.advertising)) 8778 advertising |= RTL_ADVERTISED_10_HALF; 8779 8780 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, 8781 cmd->link_modes.advertising)) 8782 advertising |= RTL_ADVERTISED_10_FULL; 8783 8784 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, 8785 cmd->link_modes.advertising)) 8786 advertising |= RTL_ADVERTISED_100_HALF; 8787 8788 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 8789 cmd->link_modes.advertising)) 8790 advertising |= RTL_ADVERTISED_100_FULL; 8791 8792 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 8793 cmd->link_modes.advertising)) 8794 advertising |= RTL_ADVERTISED_1000_HALF; 8795 8796 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 8797 cmd->link_modes.advertising)) 8798 advertising |= RTL_ADVERTISED_1000_FULL; 8799 8800 if (test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8801 cmd->link_modes.advertising)) 8802 advertising |= RTL_ADVERTISED_2500_FULL; 8803 8804 if (test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 8805 cmd->link_modes.advertising)) 8806 advertising |= RTL_ADVERTISED_5000_FULL; 8807 8808 mutex_lock(&tp->control); 8809 8810 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed, 8811 cmd->base.duplex, advertising); 8812 if (!ret) { 8813 tp->autoneg = cmd->base.autoneg; 8814 tp->speed = cmd->base.speed; 8815 tp->duplex = cmd->base.duplex; 8816 tp->advertising = advertising; 8817 } 8818 8819 mutex_unlock(&tp->control); 8820 8821 usb_autopm_put_interface(tp->intf); 8822 8823 out: 8824 return ret; 8825 } 8826 8827 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = { 8828 "tx_packets", 8829 "rx_packets", 8830 "tx_errors", 8831 "rx_errors", 8832 "rx_missed", 8833 "align_errors", 8834 "tx_single_collisions", 8835 "tx_multi_collisions", 8836 "rx_unicast", 8837 "rx_broadcast", 8838 "rx_multicast", 8839 "tx_aborted", 8840 "tx_underrun", 8841 }; 8842 8843 static int rtl8152_get_sset_count(struct net_device *dev, int sset) 8844 { 8845 switch (sset) { 8846 case ETH_SS_STATS: 8847 return ARRAY_SIZE(rtl8152_gstrings); 8848 default: 8849 return -EOPNOTSUPP; 8850 } 8851 } 8852 8853 static void rtl8152_get_ethtool_stats(struct net_device *dev, 8854 struct ethtool_stats *stats, u64 *data) 8855 { 8856 struct r8152 *tp = netdev_priv(dev); 8857 struct tally_counter tally; 8858 8859 if (usb_autopm_get_interface(tp->intf) < 0) 8860 return; 8861 8862 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA); 8863 8864 usb_autopm_put_interface(tp->intf); 8865 8866 data[0] = le64_to_cpu(tally.tx_packets); 8867 data[1] = le64_to_cpu(tally.rx_packets); 8868 data[2] = le64_to_cpu(tally.tx_errors); 8869 data[3] = le32_to_cpu(tally.rx_errors); 8870 data[4] = le16_to_cpu(tally.rx_missed); 8871 data[5] = le16_to_cpu(tally.align_errors); 8872 data[6] = le32_to_cpu(tally.tx_one_collision); 8873 data[7] = le32_to_cpu(tally.tx_multi_collision); 8874 data[8] = le64_to_cpu(tally.rx_unicast); 8875 data[9] = le64_to_cpu(tally.rx_broadcast); 8876 data[10] = le32_to_cpu(tally.rx_multicast); 8877 data[11] = le16_to_cpu(tally.tx_aborted); 8878 data[12] = le16_to_cpu(tally.tx_underrun); 8879 } 8880 8881 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data) 8882 { 8883 switch (stringset) { 8884 case ETH_SS_STATS: 8885 memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings)); 8886 break; 8887 } 8888 } 8889 8890 static int r8152_get_eee(struct r8152 *tp, struct ethtool_keee *eee) 8891 { 8892 __ETHTOOL_DECLARE_LINK_MODE_MASK(common) = {}; 8893 u16 speed = rtl8152_get_speed(tp); 8894 u16 val; 8895 8896 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 8897 mii_eee_cap1_mod_linkmode_t(eee->supported, val); 8898 8899 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 8900 mii_eee_cap1_mod_linkmode_t(eee->advertised, val); 8901 8902 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); 8903 mii_eee_cap1_mod_linkmode_t(eee->lp_advertised, val); 8904 8905 eee->eee_enabled = tp->eee_en; 8906 8907 if (speed & _1000bps) 8908 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, common); 8909 if (speed & _100bps) 8910 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, common); 8911 8912 linkmode_and(common, common, eee->advertised); 8913 linkmode_and(common, common, eee->lp_advertised); 8914 eee->eee_active = !linkmode_empty(common); 8915 8916 return 0; 8917 } 8918 8919 static int r8152_set_eee(struct r8152 *tp, struct ethtool_keee *eee) 8920 { 8921 u16 val = linkmode_to_mii_eee_cap1_t(eee->advertised); 8922 8923 tp->eee_en = eee->eee_enabled; 8924 tp->eee_adv = val; 8925 if (tp->support_2500full || tp->support_5000full) { 8926 val = linkmode_to_mii_eee_cap2_t(eee->advertised); 8927 tp->eee_adv2 = val; 8928 } 8929 rtl_eee_enable(tp, tp->eee_en); 8930 8931 return 0; 8932 } 8933 8934 static int r8153_get_eee(struct r8152 *tp, struct ethtool_keee *eee) 8935 { 8936 __ETHTOOL_DECLARE_LINK_MODE_MASK(common) = {}; 8937 u16 speed = rtl8152_get_speed(tp); 8938 u16 val; 8939 8940 val = ocp_reg_read(tp, OCP_EEE_ABLE); 8941 mii_eee_cap1_mod_linkmode_t(eee->supported, val); 8942 8943 val = ocp_reg_read(tp, OCP_EEE_ADV); 8944 mii_eee_cap1_mod_linkmode_t(eee->advertised, val); 8945 8946 val = ocp_reg_read(tp, OCP_EEE_LPABLE); 8947 mii_eee_cap1_mod_linkmode_t(eee->lp_advertised, val); 8948 8949 if (tp->support_2500full || tp->support_5000full) { 8950 val = ocp_reg_read(tp, OCP_EEE_ADV2); 8951 mii_eee_cap2_mod_linkmode_adv_t(eee->advertised, val); 8952 8953 val = ocp_reg_read(tp, OCP_EEE_LPABLE2); 8954 mii_eee_cap2_mod_linkmode_adv_t(eee->lp_advertised, val); 8955 } 8956 8957 if (tp->support_2500full) { 8958 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, eee->supported); 8959 8960 if (speed & _2500bps) 8961 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, common); 8962 } 8963 8964 if (tp->support_5000full) { 8965 linkmode_set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, eee->supported); 8966 8967 if (speed & _5000bps) 8968 linkmode_set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, common); 8969 } 8970 8971 eee->eee_enabled = tp->eee_en; 8972 8973 if (speed & _1000bps) 8974 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, common); 8975 if (speed & _100bps) 8976 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, common); 8977 8978 linkmode_and(common, common, eee->advertised); 8979 linkmode_and(common, common, eee->lp_advertised); 8980 eee->eee_active = !linkmode_empty(common); 8981 8982 return 0; 8983 } 8984 8985 static int 8986 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_keee *edata) 8987 { 8988 struct r8152 *tp = netdev_priv(net); 8989 int ret; 8990 8991 if (!tp->rtl_ops.eee_get) { 8992 ret = -EOPNOTSUPP; 8993 goto out; 8994 } 8995 8996 ret = usb_autopm_get_interface(tp->intf); 8997 if (ret < 0) 8998 goto out; 8999 9000 mutex_lock(&tp->control); 9001 9002 ret = tp->rtl_ops.eee_get(tp, edata); 9003 9004 mutex_unlock(&tp->control); 9005 9006 usb_autopm_put_interface(tp->intf); 9007 9008 out: 9009 return ret; 9010 } 9011 9012 static int 9013 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_keee *edata) 9014 { 9015 struct r8152 *tp = netdev_priv(net); 9016 int ret; 9017 9018 if (!tp->rtl_ops.eee_set) { 9019 ret = -EOPNOTSUPP; 9020 goto out; 9021 } 9022 9023 ret = usb_autopm_get_interface(tp->intf); 9024 if (ret < 0) 9025 goto out; 9026 9027 mutex_lock(&tp->control); 9028 9029 ret = tp->rtl_ops.eee_set(tp, edata); 9030 if (!ret) 9031 ret = mii_nway_restart(&tp->mii); 9032 9033 mutex_unlock(&tp->control); 9034 9035 usb_autopm_put_interface(tp->intf); 9036 9037 out: 9038 return ret; 9039 } 9040 9041 static int rtl8152_nway_reset(struct net_device *dev) 9042 { 9043 struct r8152 *tp = netdev_priv(dev); 9044 int ret; 9045 9046 ret = usb_autopm_get_interface(tp->intf); 9047 if (ret < 0) 9048 goto out; 9049 9050 mutex_lock(&tp->control); 9051 9052 ret = mii_nway_restart(&tp->mii); 9053 9054 mutex_unlock(&tp->control); 9055 9056 usb_autopm_put_interface(tp->intf); 9057 9058 out: 9059 return ret; 9060 } 9061 9062 static int rtl8152_get_coalesce(struct net_device *netdev, 9063 struct ethtool_coalesce *coalesce, 9064 struct kernel_ethtool_coalesce *kernel_coal, 9065 struct netlink_ext_ack *extack) 9066 { 9067 struct r8152 *tp = netdev_priv(netdev); 9068 9069 switch (tp->version) { 9070 case RTL_VER_01: 9071 case RTL_VER_02: 9072 case RTL_VER_07: 9073 return -EOPNOTSUPP; 9074 default: 9075 break; 9076 } 9077 9078 coalesce->rx_coalesce_usecs = tp->coalesce; 9079 9080 return 0; 9081 } 9082 9083 static int rtl8152_set_coalesce(struct net_device *netdev, 9084 struct ethtool_coalesce *coalesce, 9085 struct kernel_ethtool_coalesce *kernel_coal, 9086 struct netlink_ext_ack *extack) 9087 { 9088 struct r8152 *tp = netdev_priv(netdev); 9089 int ret; 9090 9091 switch (tp->version) { 9092 case RTL_VER_01: 9093 case RTL_VER_02: 9094 case RTL_VER_07: 9095 return -EOPNOTSUPP; 9096 default: 9097 break; 9098 } 9099 9100 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW) 9101 return -EINVAL; 9102 9103 ret = usb_autopm_get_interface(tp->intf); 9104 if (ret < 0) 9105 return ret; 9106 9107 mutex_lock(&tp->control); 9108 9109 if (tp->coalesce != coalesce->rx_coalesce_usecs) { 9110 tp->coalesce = coalesce->rx_coalesce_usecs; 9111 9112 if (netif_running(netdev) && netif_carrier_ok(netdev)) { 9113 netif_stop_queue(netdev); 9114 napi_disable(&tp->napi); 9115 tp->rtl_ops.disable(tp); 9116 tp->rtl_ops.enable(tp); 9117 rtl_start_rx(tp); 9118 clear_bit(RTL8152_SET_RX_MODE, &tp->flags); 9119 _rtl8152_set_rx_mode(netdev); 9120 napi_enable(&tp->napi); 9121 netif_wake_queue(netdev); 9122 } 9123 } 9124 9125 mutex_unlock(&tp->control); 9126 9127 usb_autopm_put_interface(tp->intf); 9128 9129 return ret; 9130 } 9131 9132 static int rtl8152_get_tunable(struct net_device *netdev, 9133 const struct ethtool_tunable *tunable, void *d) 9134 { 9135 struct r8152 *tp = netdev_priv(netdev); 9136 9137 switch (tunable->id) { 9138 case ETHTOOL_RX_COPYBREAK: 9139 *(u32 *)d = tp->rx_copybreak; 9140 break; 9141 default: 9142 return -EOPNOTSUPP; 9143 } 9144 9145 return 0; 9146 } 9147 9148 static int rtl8152_set_tunable(struct net_device *netdev, 9149 const struct ethtool_tunable *tunable, 9150 const void *d) 9151 { 9152 struct r8152 *tp = netdev_priv(netdev); 9153 u32 val; 9154 9155 switch (tunable->id) { 9156 case ETHTOOL_RX_COPYBREAK: 9157 val = *(u32 *)d; 9158 if (val < ETH_ZLEN) { 9159 netif_err(tp, rx_err, netdev, 9160 "Invalid rx copy break value\n"); 9161 return -EINVAL; 9162 } 9163 9164 if (tp->rx_copybreak != val) { 9165 if (netdev->flags & IFF_UP) { 9166 mutex_lock(&tp->control); 9167 napi_disable(&tp->napi); 9168 tp->rx_copybreak = val; 9169 napi_enable(&tp->napi); 9170 mutex_unlock(&tp->control); 9171 } else { 9172 tp->rx_copybreak = val; 9173 } 9174 } 9175 break; 9176 default: 9177 return -EOPNOTSUPP; 9178 } 9179 9180 return 0; 9181 } 9182 9183 static void rtl8152_get_ringparam(struct net_device *netdev, 9184 struct ethtool_ringparam *ring, 9185 struct kernel_ethtool_ringparam *kernel_ring, 9186 struct netlink_ext_ack *extack) 9187 { 9188 struct r8152 *tp = netdev_priv(netdev); 9189 9190 ring->rx_max_pending = RTL8152_RX_MAX_PENDING; 9191 ring->rx_pending = tp->rx_pending; 9192 } 9193 9194 static int rtl8152_set_ringparam(struct net_device *netdev, 9195 struct ethtool_ringparam *ring, 9196 struct kernel_ethtool_ringparam *kernel_ring, 9197 struct netlink_ext_ack *extack) 9198 { 9199 struct r8152 *tp = netdev_priv(netdev); 9200 9201 if (ring->rx_pending < (RTL8152_MAX_RX * 2)) 9202 return -EINVAL; 9203 9204 if (tp->rx_pending != ring->rx_pending) { 9205 if (netdev->flags & IFF_UP) { 9206 mutex_lock(&tp->control); 9207 napi_disable(&tp->napi); 9208 tp->rx_pending = ring->rx_pending; 9209 napi_enable(&tp->napi); 9210 mutex_unlock(&tp->control); 9211 } else { 9212 tp->rx_pending = ring->rx_pending; 9213 } 9214 } 9215 9216 return 0; 9217 } 9218 9219 static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause) 9220 { 9221 struct r8152 *tp = netdev_priv(netdev); 9222 u16 bmcr, lcladv, rmtadv; 9223 u8 cap; 9224 9225 if (usb_autopm_get_interface(tp->intf) < 0) 9226 return; 9227 9228 mutex_lock(&tp->control); 9229 9230 bmcr = r8152_mdio_read(tp, MII_BMCR); 9231 lcladv = r8152_mdio_read(tp, MII_ADVERTISE); 9232 rmtadv = r8152_mdio_read(tp, MII_LPA); 9233 9234 mutex_unlock(&tp->control); 9235 9236 usb_autopm_put_interface(tp->intf); 9237 9238 if (!(bmcr & BMCR_ANENABLE)) { 9239 pause->autoneg = 0; 9240 pause->rx_pause = 0; 9241 pause->tx_pause = 0; 9242 return; 9243 } 9244 9245 pause->autoneg = 1; 9246 9247 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv); 9248 9249 if (cap & FLOW_CTRL_RX) 9250 pause->rx_pause = 1; 9251 9252 if (cap & FLOW_CTRL_TX) 9253 pause->tx_pause = 1; 9254 } 9255 9256 static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause) 9257 { 9258 struct r8152 *tp = netdev_priv(netdev); 9259 u16 old, new1; 9260 u8 cap = 0; 9261 int ret; 9262 9263 ret = usb_autopm_get_interface(tp->intf); 9264 if (ret < 0) 9265 return ret; 9266 9267 mutex_lock(&tp->control); 9268 9269 if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) { 9270 ret = -EINVAL; 9271 goto out; 9272 } 9273 9274 if (pause->rx_pause) 9275 cap |= FLOW_CTRL_RX; 9276 9277 if (pause->tx_pause) 9278 cap |= FLOW_CTRL_TX; 9279 9280 old = r8152_mdio_read(tp, MII_ADVERTISE); 9281 new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap); 9282 if (old != new1) 9283 r8152_mdio_write(tp, MII_ADVERTISE, new1); 9284 9285 out: 9286 mutex_unlock(&tp->control); 9287 usb_autopm_put_interface(tp->intf); 9288 9289 return ret; 9290 } 9291 9292 static const struct ethtool_ops ops = { 9293 .supported_coalesce_params = ETHTOOL_COALESCE_USECS, 9294 .get_drvinfo = rtl8152_get_drvinfo, 9295 .get_link = ethtool_op_get_link, 9296 .nway_reset = rtl8152_nway_reset, 9297 .get_msglevel = rtl8152_get_msglevel, 9298 .set_msglevel = rtl8152_set_msglevel, 9299 .get_wol = rtl8152_get_wol, 9300 .set_wol = rtl8152_set_wol, 9301 .get_strings = rtl8152_get_strings, 9302 .get_sset_count = rtl8152_get_sset_count, 9303 .get_ethtool_stats = rtl8152_get_ethtool_stats, 9304 .get_coalesce = rtl8152_get_coalesce, 9305 .set_coalesce = rtl8152_set_coalesce, 9306 .get_eee = rtl_ethtool_get_eee, 9307 .set_eee = rtl_ethtool_set_eee, 9308 .get_link_ksettings = rtl8152_get_link_ksettings, 9309 .set_link_ksettings = rtl8152_set_link_ksettings, 9310 .get_tunable = rtl8152_get_tunable, 9311 .set_tunable = rtl8152_set_tunable, 9312 .get_ringparam = rtl8152_get_ringparam, 9313 .set_ringparam = rtl8152_set_ringparam, 9314 .get_pauseparam = rtl8152_get_pauseparam, 9315 .set_pauseparam = rtl8152_set_pauseparam, 9316 .get_ts_info = ethtool_op_get_ts_info, 9317 }; 9318 9319 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) 9320 { 9321 struct r8152 *tp = netdev_priv(netdev); 9322 struct mii_ioctl_data *data = if_mii(rq); 9323 int res; 9324 9325 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 9326 return -ENODEV; 9327 9328 res = usb_autopm_get_interface(tp->intf); 9329 if (res < 0) 9330 goto out; 9331 9332 switch (cmd) { 9333 case SIOCGMIIPHY: 9334 data->phy_id = R8152_PHY_ID; /* Internal PHY */ 9335 break; 9336 9337 case SIOCGMIIREG: 9338 mutex_lock(&tp->control); 9339 data->val_out = r8152_mdio_read(tp, data->reg_num); 9340 mutex_unlock(&tp->control); 9341 break; 9342 9343 case SIOCSMIIREG: 9344 if (!capable(CAP_NET_ADMIN)) { 9345 res = -EPERM; 9346 break; 9347 } 9348 mutex_lock(&tp->control); 9349 r8152_mdio_write(tp, data->reg_num, data->val_in); 9350 mutex_unlock(&tp->control); 9351 break; 9352 9353 default: 9354 res = -EOPNOTSUPP; 9355 } 9356 9357 usb_autopm_put_interface(tp->intf); 9358 9359 out: 9360 return res; 9361 } 9362 9363 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu) 9364 { 9365 struct r8152 *tp = netdev_priv(dev); 9366 int ret; 9367 9368 switch (tp->version) { 9369 case RTL_VER_01: 9370 case RTL_VER_02: 9371 case RTL_VER_07: 9372 WRITE_ONCE(dev->mtu, new_mtu); 9373 return 0; 9374 default: 9375 break; 9376 } 9377 9378 ret = usb_autopm_get_interface(tp->intf); 9379 if (ret < 0) 9380 return ret; 9381 9382 mutex_lock(&tp->control); 9383 9384 WRITE_ONCE(dev->mtu, new_mtu); 9385 9386 if (netif_running(dev)) { 9387 if (tp->rtl_ops.change_mtu) 9388 tp->rtl_ops.change_mtu(tp); 9389 9390 if (netif_carrier_ok(dev)) { 9391 netif_stop_queue(dev); 9392 napi_disable(&tp->napi); 9393 tasklet_disable(&tp->tx_tl); 9394 tp->rtl_ops.disable(tp); 9395 tp->rtl_ops.enable(tp); 9396 rtl_start_rx(tp); 9397 tasklet_enable(&tp->tx_tl); 9398 napi_enable(&tp->napi); 9399 rtl8152_set_rx_mode(dev); 9400 netif_wake_queue(dev); 9401 } 9402 } 9403 9404 mutex_unlock(&tp->control); 9405 9406 usb_autopm_put_interface(tp->intf); 9407 9408 return ret; 9409 } 9410 9411 static const struct net_device_ops rtl8152_netdev_ops = { 9412 .ndo_open = rtl8152_open, 9413 .ndo_stop = rtl8152_close, 9414 .ndo_eth_ioctl = rtl8152_ioctl, 9415 .ndo_start_xmit = rtl8152_start_xmit, 9416 .ndo_tx_timeout = rtl8152_tx_timeout, 9417 .ndo_set_features = rtl8152_set_features, 9418 .ndo_set_rx_mode = rtl8152_set_rx_mode, 9419 .ndo_set_mac_address = rtl8152_set_mac_address, 9420 .ndo_change_mtu = rtl8152_change_mtu, 9421 .ndo_validate_addr = eth_validate_addr, 9422 .ndo_features_check = rtl8152_features_check, 9423 }; 9424 9425 static void rtl8152_unload(struct r8152 *tp) 9426 { 9427 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 9428 return; 9429 9430 if (tp->version != RTL_VER_01) 9431 r8152_power_cut_en(tp, true); 9432 } 9433 9434 static void rtl8153_unload(struct r8152 *tp) 9435 { 9436 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 9437 return; 9438 9439 r8153_power_cut_en(tp, false); 9440 9441 if (tp->version >= RTL_VER_16) { 9442 /* Disable Interrupt Mitigation */ 9443 ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xcf04, BIT(0) | BIT(1) | BIT(2) | BIT(7)); 9444 } 9445 } 9446 9447 static void rtl8153b_unload(struct r8152 *tp) 9448 { 9449 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 9450 return; 9451 9452 r8153b_power_cut_en(tp, false); 9453 } 9454 9455 static int r8152_desc_init(struct r8152 *tp) 9456 { 9457 tp->rx_desc.size = sizeof(struct rx_desc); 9458 tp->rx_desc.align = 8; 9459 tp->rx_desc.vlan_tag = r8152_rx_vlan_tag; 9460 tp->desc_ops.rx_csum = r8152_rx_csum; 9461 tp->desc_ops.rx_len = r8152_rx_len; 9462 tp->tx_desc.size = sizeof(struct tx_desc); 9463 tp->tx_desc.align = 4; 9464 tp->tx_desc.vlan_tag = r8152_tx_vlan_tag; 9465 tp->desc_ops.tx_csum = r8152_tx_csum; 9466 tp->desc_ops.tx_len = r8152_tx_len; 9467 9468 return 0; 9469 } 9470 9471 static int r8157_desc_init(struct r8152 *tp) 9472 { 9473 tp->rx_desc.size = sizeof(struct rx_desc_v2); 9474 tp->rx_desc.align = 16; 9475 tp->rx_desc.vlan_tag = r8157_rx_vlan_tag; 9476 tp->desc_ops.rx_csum = r8157_rx_csum; 9477 tp->desc_ops.rx_len = r8157_rx_len; 9478 tp->tx_desc.size = sizeof(struct tx_desc_v2); 9479 tp->tx_desc.align = 16; 9480 tp->tx_desc.vlan_tag = r8152_tx_vlan_tag; 9481 tp->desc_ops.tx_csum = r8157_tx_csum; 9482 tp->desc_ops.tx_len = r8157_tx_len; 9483 9484 return 0; 9485 } 9486 9487 static int rtl_ops_init(struct r8152 *tp) 9488 { 9489 struct rtl_ops *ops = &tp->rtl_ops; 9490 int ret = 0; 9491 9492 switch (tp->version) { 9493 case RTL_VER_01: 9494 case RTL_VER_02: 9495 case RTL_VER_07: 9496 ops->init = r8152b_init; 9497 ops->enable = rtl8152_enable; 9498 ops->disable = rtl8152_disable; 9499 ops->up = rtl8152_up; 9500 ops->down = rtl8152_down; 9501 ops->unload = rtl8152_unload; 9502 ops->eee_get = r8152_get_eee; 9503 ops->eee_set = r8152_set_eee; 9504 ops->in_nway = rtl8152_in_nway; 9505 ops->hw_phy_cfg = r8152b_hw_phy_cfg; 9506 ops->autosuspend_en = rtl_runtime_suspend_enable; 9507 tp->rx_buf_sz = 16 * 1024; 9508 tp->eee_en = true; 9509 tp->eee_adv = MDIO_EEE_100TX; 9510 r8152_desc_init(tp); 9511 break; 9512 9513 case RTL_VER_03: 9514 case RTL_VER_04: 9515 case RTL_VER_05: 9516 case RTL_VER_06: 9517 ops->init = r8153_init; 9518 ops->enable = rtl8153_enable; 9519 ops->disable = rtl8153_disable; 9520 ops->up = rtl8153_up; 9521 ops->down = rtl8153_down; 9522 ops->unload = rtl8153_unload; 9523 ops->eee_get = r8153_get_eee; 9524 ops->eee_set = r8152_set_eee; 9525 ops->in_nway = rtl8153_in_nway; 9526 ops->hw_phy_cfg = r8153_hw_phy_cfg; 9527 ops->autosuspend_en = rtl8153_runtime_enable; 9528 ops->change_mtu = rtl8153_change_mtu; 9529 if (tp->udev->speed < USB_SPEED_SUPER) 9530 tp->rx_buf_sz = 16 * 1024; 9531 else 9532 tp->rx_buf_sz = 32 * 1024; 9533 tp->eee_en = true; 9534 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9535 r8152_desc_init(tp); 9536 break; 9537 9538 case RTL_VER_08: 9539 case RTL_VER_09: 9540 ops->init = r8153b_init; 9541 ops->enable = rtl8153_enable; 9542 ops->disable = rtl8153_disable; 9543 ops->up = rtl8153b_up; 9544 ops->down = rtl8153b_down; 9545 ops->unload = rtl8153b_unload; 9546 ops->eee_get = r8153_get_eee; 9547 ops->eee_set = r8152_set_eee; 9548 ops->in_nway = rtl8153_in_nway; 9549 ops->hw_phy_cfg = r8153b_hw_phy_cfg; 9550 ops->autosuspend_en = rtl8153b_runtime_enable; 9551 ops->change_mtu = rtl8153_change_mtu; 9552 tp->rx_buf_sz = 32 * 1024; 9553 tp->eee_en = true; 9554 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9555 r8152_desc_init(tp); 9556 break; 9557 9558 case RTL_VER_11: 9559 tp->eee_en = true; 9560 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9561 tp->eee_adv2 = MDIO_EEE_2_5GT; 9562 fallthrough; 9563 case RTL_VER_10: 9564 ops->init = r8156_init; 9565 ops->enable = rtl8156_enable; 9566 ops->disable = rtl8156_disable; 9567 ops->up = rtl8156_up; 9568 ops->down = rtl8156_down; 9569 ops->unload = rtl8153_unload; 9570 ops->eee_get = r8153_get_eee; 9571 ops->eee_set = r8152_set_eee; 9572 ops->in_nway = rtl8153_in_nway; 9573 ops->hw_phy_cfg = r8156_hw_phy_cfg; 9574 ops->autosuspend_en = rtl8156_runtime_enable; 9575 ops->change_mtu = rtl8156_change_mtu; 9576 tp->rx_buf_sz = 48 * 1024; 9577 tp->support_2500full = 1; 9578 r8152_desc_init(tp); 9579 break; 9580 9581 case RTL_VER_12: 9582 case RTL_VER_13: 9583 tp->support_2500full = 1; 9584 fallthrough; 9585 case RTL_VER_15: 9586 tp->eee_en = true; 9587 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9588 tp->eee_adv2 = MDIO_EEE_2_5GT; 9589 ops->init = r8156_init; 9590 ops->enable = rtl8156_enable; 9591 ops->disable = rtl8153_disable; 9592 ops->up = rtl8156_up; 9593 ops->down = rtl8156_down; 9594 ops->unload = rtl8153_unload; 9595 ops->eee_get = r8153_get_eee; 9596 ops->eee_set = r8152_set_eee; 9597 ops->in_nway = rtl8153_in_nway; 9598 ops->hw_phy_cfg = r8156b_hw_phy_cfg; 9599 ops->autosuspend_en = rtl8156_runtime_enable; 9600 ops->change_mtu = rtl8156_change_mtu; 9601 tp->rx_buf_sz = 48 * 1024; 9602 r8152_desc_init(tp); 9603 break; 9604 9605 case RTL_VER_14: 9606 ops->init = r8153c_init; 9607 ops->enable = rtl8153_enable; 9608 ops->disable = rtl8153_disable; 9609 ops->up = rtl8153c_up; 9610 ops->down = rtl8153b_down; 9611 ops->unload = rtl8153_unload; 9612 ops->eee_get = r8153_get_eee; 9613 ops->eee_set = r8152_set_eee; 9614 ops->in_nway = rtl8153_in_nway; 9615 ops->hw_phy_cfg = r8153c_hw_phy_cfg; 9616 ops->autosuspend_en = rtl8153c_runtime_enable; 9617 ops->change_mtu = rtl8153c_change_mtu; 9618 tp->rx_buf_sz = 32 * 1024; 9619 tp->eee_en = true; 9620 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9621 r8152_desc_init(tp); 9622 break; 9623 9624 case RTL_VER_16: 9625 tp->eee_en = true; 9626 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9627 tp->eee_adv2 = MDIO_EEE_2_5GT | MDIO_EEE_5GT; 9628 ops->init = r8156_init; 9629 ops->enable = rtl8156_enable; 9630 ops->disable = rtl8153_disable; 9631 ops->up = rtl8156_up; 9632 ops->down = rtl8156_down; 9633 ops->unload = rtl8153_unload; 9634 ops->eee_get = r8153_get_eee; 9635 ops->eee_set = r8152_set_eee; 9636 ops->in_nway = rtl8153_in_nway; 9637 ops->hw_phy_cfg = r8157_hw_phy_cfg; 9638 ops->autosuspend_en = rtl8157_runtime_enable; 9639 ops->change_mtu = rtl8156_change_mtu; 9640 tp->rx_buf_sz = 32 * 1024; 9641 tp->support_2500full = 1; 9642 tp->support_5000full = 1; 9643 r8157_desc_init(tp); 9644 break; 9645 9646 default: 9647 ret = -ENODEV; 9648 dev_err(&tp->intf->dev, "Unknown Device\n"); 9649 break; 9650 } 9651 9652 return ret; 9653 } 9654 9655 #define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw" 9656 #define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw" 9657 #define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw" 9658 #define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw" 9659 #define FIRMWARE_8153C_1 "rtl_nic/rtl8153c-1.fw" 9660 #define FIRMWARE_8156A_2 "rtl_nic/rtl8156a-2.fw" 9661 #define FIRMWARE_8156B_2 "rtl_nic/rtl8156b-2.fw" 9662 9663 MODULE_FIRMWARE(FIRMWARE_8153A_2); 9664 MODULE_FIRMWARE(FIRMWARE_8153A_3); 9665 MODULE_FIRMWARE(FIRMWARE_8153A_4); 9666 MODULE_FIRMWARE(FIRMWARE_8153B_2); 9667 MODULE_FIRMWARE(FIRMWARE_8153C_1); 9668 MODULE_FIRMWARE(FIRMWARE_8156A_2); 9669 MODULE_FIRMWARE(FIRMWARE_8156B_2); 9670 9671 static int rtl_fw_init(struct r8152 *tp) 9672 { 9673 struct rtl_fw *rtl_fw = &tp->rtl_fw; 9674 9675 switch (tp->version) { 9676 case RTL_VER_04: 9677 rtl_fw->fw_name = FIRMWARE_8153A_2; 9678 rtl_fw->pre_fw = r8153_pre_firmware_1; 9679 rtl_fw->post_fw = r8153_post_firmware_1; 9680 break; 9681 case RTL_VER_05: 9682 rtl_fw->fw_name = FIRMWARE_8153A_3; 9683 rtl_fw->pre_fw = r8153_pre_firmware_2; 9684 rtl_fw->post_fw = r8153_post_firmware_2; 9685 break; 9686 case RTL_VER_06: 9687 rtl_fw->fw_name = FIRMWARE_8153A_4; 9688 rtl_fw->post_fw = r8153_post_firmware_3; 9689 break; 9690 case RTL_VER_09: 9691 rtl_fw->fw_name = FIRMWARE_8153B_2; 9692 rtl_fw->pre_fw = r8153b_pre_firmware_1; 9693 rtl_fw->post_fw = r8153b_post_firmware_1; 9694 break; 9695 case RTL_VER_11: 9696 rtl_fw->fw_name = FIRMWARE_8156A_2; 9697 rtl_fw->post_fw = r8156a_post_firmware_1; 9698 break; 9699 case RTL_VER_13: 9700 case RTL_VER_15: 9701 rtl_fw->fw_name = FIRMWARE_8156B_2; 9702 break; 9703 case RTL_VER_14: 9704 rtl_fw->fw_name = FIRMWARE_8153C_1; 9705 rtl_fw->pre_fw = r8153b_pre_firmware_1; 9706 rtl_fw->post_fw = r8153c_post_firmware_1; 9707 break; 9708 default: 9709 break; 9710 } 9711 9712 return 0; 9713 } 9714 9715 static u8 __rtl_get_hw_ver(struct usb_device *udev) 9716 { 9717 u32 ocp_data = 0; 9718 __le32 *tmp; 9719 u8 version; 9720 int ret; 9721 int i; 9722 9723 tmp = kmalloc_obj(*tmp); 9724 if (!tmp) 9725 return 0; 9726 9727 /* Retry up to 3 times in case there is a transitory error. We do this 9728 * since retrying a read of the version is always safe and this 9729 * function doesn't take advantage of r8152_control_msg(). 9730 */ 9731 for (i = 0; i < 3; i++) { 9732 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 9733 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, 9734 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 9735 USB_CTRL_GET_TIMEOUT); 9736 if (ret > 0) { 9737 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK; 9738 break; 9739 } 9740 } 9741 9742 if (i != 0 && ret > 0) 9743 dev_warn(&udev->dev, "Needed %d retries to read version\n", i); 9744 9745 kfree(tmp); 9746 9747 switch (ocp_data) { 9748 case 0x4c00: 9749 version = RTL_VER_01; 9750 break; 9751 case 0x4c10: 9752 version = RTL_VER_02; 9753 break; 9754 case 0x5c00: 9755 version = RTL_VER_03; 9756 break; 9757 case 0x5c10: 9758 version = RTL_VER_04; 9759 break; 9760 case 0x5c20: 9761 version = RTL_VER_05; 9762 break; 9763 case 0x5c30: 9764 version = RTL_VER_06; 9765 break; 9766 case 0x4800: 9767 version = RTL_VER_07; 9768 break; 9769 case 0x6000: 9770 version = RTL_VER_08; 9771 break; 9772 case 0x6010: 9773 version = RTL_VER_09; 9774 break; 9775 case 0x7010: 9776 version = RTL_TEST_01; 9777 break; 9778 case 0x7020: 9779 version = RTL_VER_10; 9780 break; 9781 case 0x7030: 9782 version = RTL_VER_11; 9783 break; 9784 case 0x7400: 9785 version = RTL_VER_12; 9786 break; 9787 case 0x7410: 9788 version = RTL_VER_13; 9789 break; 9790 case 0x6400: 9791 version = RTL_VER_14; 9792 break; 9793 case 0x7420: 9794 version = RTL_VER_15; 9795 break; 9796 case 0x1030: 9797 version = RTL_VER_16; 9798 break; 9799 default: 9800 version = RTL_VER_UNKNOWN; 9801 dev_info(&udev->dev, "Unknown version 0x%04x\n", ocp_data); 9802 break; 9803 } 9804 9805 return version; 9806 } 9807 9808 u8 rtl8152_get_version(struct usb_interface *intf) 9809 { 9810 u8 version; 9811 9812 version = __rtl_get_hw_ver(interface_to_usbdev(intf)); 9813 9814 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version); 9815 9816 return version; 9817 } 9818 EXPORT_SYMBOL_GPL(rtl8152_get_version); 9819 9820 static bool rtl8152_supports_lenovo_macpassthru(struct usb_device *udev) 9821 { 9822 int parent_vendor_id = le16_to_cpu(udev->parent->descriptor.idVendor); 9823 int product_id = le16_to_cpu(udev->descriptor.idProduct); 9824 int vendor_id = le16_to_cpu(udev->descriptor.idVendor); 9825 9826 if (vendor_id == VENDOR_ID_LENOVO) { 9827 switch (product_id) { 9828 case DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB: 9829 case DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK: 9830 case DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2: 9831 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2: 9832 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3: 9833 case DEVICE_ID_THINKPAD_USB_C_DONGLE: 9834 case DEVICE_ID_THINKPAD_HYBRID_USB_C_DOCK: 9835 return 1; 9836 } 9837 } else if (vendor_id == VENDOR_ID_REALTEK && parent_vendor_id == VENDOR_ID_LENOVO) { 9838 switch (product_id) { 9839 case 0x8153: 9840 return 1; 9841 } 9842 } 9843 return 0; 9844 } 9845 9846 static int rtl8152_probe_once(struct usb_interface *intf, 9847 const struct usb_device_id *id, u8 version) 9848 { 9849 struct usb_device *udev = interface_to_usbdev(intf); 9850 struct r8152 *tp; 9851 struct net_device *netdev; 9852 int ret; 9853 9854 usb_reset_device(udev); 9855 netdev = alloc_etherdev(sizeof(struct r8152)); 9856 if (!netdev) { 9857 dev_err(&intf->dev, "Out of memory\n"); 9858 return -ENOMEM; 9859 } 9860 9861 SET_NETDEV_DEV(netdev, &intf->dev); 9862 tp = netdev_priv(netdev); 9863 tp->msg_enable = 0x7FFF; 9864 9865 tp->udev = udev; 9866 tp->netdev = netdev; 9867 tp->intf = intf; 9868 tp->version = version; 9869 9870 tp->pipe_ctrl_in = usb_rcvctrlpipe(udev, 0); 9871 tp->pipe_ctrl_out = usb_sndctrlpipe(udev, 0); 9872 tp->pipe_in = usb_rcvbulkpipe(udev, 1); 9873 tp->pipe_out = usb_sndbulkpipe(udev, 2); 9874 tp->pipe_intr = usb_rcvintpipe(udev, 3); 9875 9876 switch (version) { 9877 case RTL_VER_01: 9878 case RTL_VER_02: 9879 case RTL_VER_07: 9880 tp->mii.supports_gmii = 0; 9881 break; 9882 default: 9883 tp->mii.supports_gmii = 1; 9884 break; 9885 } 9886 9887 ret = rtl_ops_init(tp); 9888 if (ret) 9889 goto out; 9890 9891 rtl_fw_init(tp); 9892 9893 mutex_init(&tp->control); 9894 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); 9895 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t); 9896 tasklet_setup(&tp->tx_tl, bottom_half); 9897 tasklet_disable(&tp->tx_tl); 9898 9899 netdev->netdev_ops = &rtl8152_netdev_ops; 9900 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; 9901 9902 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | 9903 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM | 9904 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX | 9905 NETIF_F_HW_VLAN_CTAG_TX; 9906 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | 9907 NETIF_F_TSO | NETIF_F_FRAGLIST | 9908 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | 9909 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX; 9910 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | 9911 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | 9912 NETIF_F_IPV6_CSUM | NETIF_F_TSO6; 9913 9914 if (tp->version == RTL_VER_01) { 9915 netdev->features &= ~NETIF_F_RXCSUM; 9916 netdev->hw_features &= ~NETIF_F_RXCSUM; 9917 } 9918 9919 tp->lenovo_macpassthru = rtl8152_supports_lenovo_macpassthru(udev); 9920 9921 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial && 9922 (!strcmp(udev->serial, "000001000000") || 9923 !strcmp(udev->serial, "000002000000"))) { 9924 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation"); 9925 tp->dell_tb_rx_agg_bug = 1; 9926 } 9927 9928 netdev->ethtool_ops = &ops; 9929 netif_set_tso_max_size(netdev, RTL_LIMITED_TSO_SIZE); 9930 9931 /* MTU range: 68 - 1500 or 9194 */ 9932 netdev->min_mtu = ETH_MIN_MTU; 9933 switch (tp->version) { 9934 case RTL_VER_03: 9935 case RTL_VER_04: 9936 case RTL_VER_05: 9937 case RTL_VER_06: 9938 case RTL_VER_08: 9939 case RTL_VER_09: 9940 case RTL_VER_14: 9941 netdev->max_mtu = size_to_mtu(9 * 1024); 9942 break; 9943 case RTL_VER_10: 9944 case RTL_VER_11: 9945 netdev->max_mtu = size_to_mtu(15 * 1024); 9946 break; 9947 case RTL_VER_12: 9948 case RTL_VER_13: 9949 case RTL_VER_15: 9950 case RTL_VER_16: 9951 netdev->max_mtu = size_to_mtu(16 * 1024); 9952 break; 9953 case RTL_VER_01: 9954 case RTL_VER_02: 9955 case RTL_VER_07: 9956 default: 9957 netdev->max_mtu = ETH_DATA_LEN; 9958 break; 9959 } 9960 9961 tp->mii.dev = netdev; 9962 tp->mii.mdio_read = read_mii_word; 9963 tp->mii.mdio_write = write_mii_word; 9964 tp->mii.phy_id_mask = 0x3f; 9965 tp->mii.reg_num_mask = 0x1f; 9966 tp->mii.phy_id = R8152_PHY_ID; 9967 9968 tp->autoneg = AUTONEG_ENABLE; 9969 tp->speed = SPEED_100; 9970 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | 9971 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; 9972 if (tp->mii.supports_gmii) { 9973 if (tp->support_2500full && 9974 tp->udev->speed >= USB_SPEED_SUPER) { 9975 tp->speed = SPEED_2500; 9976 tp->advertising |= RTL_ADVERTISED_2500_FULL; 9977 } else { 9978 tp->speed = SPEED_1000; 9979 } 9980 if (tp->support_5000full && 9981 tp->udev->speed >= USB_SPEED_SUPER) { 9982 tp->speed = SPEED_5000; 9983 tp->advertising |= RTL_ADVERTISED_5000_FULL; 9984 } 9985 tp->advertising |= RTL_ADVERTISED_1000_FULL; 9986 } 9987 tp->duplex = DUPLEX_FULL; 9988 9989 tp->rx_copybreak = RTL8152_RXFG_HEADSZ; 9990 tp->rx_pending = 10 * RTL8152_MAX_RX; 9991 9992 intf->needs_remote_wakeup = 1; 9993 9994 if (!rtl_can_wakeup(tp)) 9995 __rtl_set_wol(tp, 0); 9996 else 9997 tp->saved_wolopts = __rtl_get_wol(tp); 9998 9999 tp->rtl_ops.init(tp); 10000 #if IS_BUILTIN(CONFIG_USB_RTL8152) 10001 /* Retry in case request_firmware() is not ready yet. */ 10002 tp->rtl_fw.retry = true; 10003 #endif 10004 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); 10005 set_ethernet_addr(tp, false); 10006 10007 usb_set_intfdata(intf, tp); 10008 10009 netif_napi_add(netdev, &tp->napi, r8152_poll); 10010 10011 ret = register_netdev(netdev); 10012 if (ret != 0) { 10013 dev_err(&intf->dev, "couldn't register the device\n"); 10014 goto out1; 10015 } 10016 10017 if (tp->saved_wolopts) 10018 device_set_wakeup_enable(&udev->dev, true); 10019 else 10020 device_set_wakeup_enable(&udev->dev, false); 10021 10022 /* If we saw a control transfer error while probing then we may 10023 * want to try probe() again. Consider this an error. 10024 */ 10025 if (test_bit(PROBE_SHOULD_RETRY, &tp->flags)) 10026 goto out2; 10027 10028 set_bit(PROBED_WITH_NO_ERRORS, &tp->flags); 10029 10030 return 0; 10031 10032 out2: 10033 unregister_netdev(netdev); 10034 10035 out1: 10036 tasklet_kill(&tp->tx_tl); 10037 cancel_delayed_work_sync(&tp->hw_phy_work); 10038 if (tp->rtl_ops.unload) 10039 tp->rtl_ops.unload(tp); 10040 rtl8152_release_firmware(tp); 10041 usb_set_intfdata(intf, NULL); 10042 out: 10043 if (test_bit(PROBE_SHOULD_RETRY, &tp->flags)) 10044 ret = -EAGAIN; 10045 10046 free_netdev(netdev); 10047 return ret; 10048 } 10049 10050 #define RTL8152_PROBE_TRIES 3 10051 10052 static int rtl8152_probe(struct usb_interface *intf, 10053 const struct usb_device_id *id) 10054 { 10055 u8 version; 10056 int ret; 10057 int i; 10058 10059 if (intf->cur_altsetting->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC) 10060 return -ENODEV; 10061 10062 if (!rtl_check_vendor_ok(intf)) 10063 return -ENODEV; 10064 10065 version = rtl8152_get_version(intf); 10066 if (version == RTL_VER_UNKNOWN) 10067 return -ENODEV; 10068 10069 for (i = 0; i < RTL8152_PROBE_TRIES; i++) { 10070 ret = rtl8152_probe_once(intf, id, version); 10071 if (ret != -EAGAIN) 10072 break; 10073 } 10074 if (ret == -EAGAIN) { 10075 dev_err(&intf->dev, 10076 "r8152 failed probe after %d tries; giving up\n", i); 10077 return -ENODEV; 10078 } 10079 10080 return ret; 10081 } 10082 10083 static void rtl8152_disconnect(struct usb_interface *intf) 10084 { 10085 struct r8152 *tp = usb_get_intfdata(intf); 10086 10087 usb_set_intfdata(intf, NULL); 10088 if (tp) { 10089 rtl_set_unplug(tp); 10090 10091 unregister_netdev(tp->netdev); 10092 tasklet_kill(&tp->tx_tl); 10093 cancel_delayed_work_sync(&tp->hw_phy_work); 10094 if (tp->rtl_ops.unload) 10095 tp->rtl_ops.unload(tp); 10096 rtl8152_release_firmware(tp); 10097 free_netdev(tp->netdev); 10098 } 10099 } 10100 10101 /* table of devices that work with this driver */ 10102 static const struct usb_device_id rtl8152_table[] = { 10103 /* Realtek */ 10104 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8050) }, 10105 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8053) }, 10106 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8152) }, 10107 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8153) }, 10108 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8155) }, 10109 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8156) }, 10110 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8157) }, 10111 10112 /* Microsoft */ 10113 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab) }, 10114 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6) }, 10115 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927) }, 10116 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0c5e) }, 10117 { USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101) }, 10118 10119 /* Lenovo */ 10120 { USB_DEVICE(VENDOR_ID_LENOVO, 0x304f) }, 10121 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3054) }, 10122 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3062) }, 10123 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3069) }, 10124 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3082) }, 10125 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3098) }, 10126 { USB_DEVICE(VENDOR_ID_LENOVO, 0x7205) }, 10127 { USB_DEVICE(VENDOR_ID_LENOVO, 0x720c) }, 10128 { USB_DEVICE(VENDOR_ID_LENOVO, 0x7214) }, 10129 { USB_DEVICE(VENDOR_ID_LENOVO, 0x721e) }, 10130 { USB_DEVICE(VENDOR_ID_LENOVO, 0xa359) }, 10131 { USB_DEVICE(VENDOR_ID_LENOVO, 0xa387) }, 10132 10133 { USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041) }, 10134 { USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff) }, 10135 { USB_DEVICE(VENDOR_ID_TPLINK, 0x0601) }, 10136 { USB_DEVICE(VENDOR_ID_TPLINK, 0x0602) }, 10137 { USB_DEVICE(VENDOR_ID_DLINK, 0xb301) }, 10138 { USB_DEVICE(VENDOR_ID_DELL, 0xb097) }, 10139 { USB_DEVICE(VENDOR_ID_ASUS, 0x1976) }, 10140 { USB_DEVICE(VENDOR_ID_TRENDNET, 0xe02b) }, 10141 {} 10142 }; 10143 10144 MODULE_DEVICE_TABLE(usb, rtl8152_table); 10145 10146 static struct usb_driver rtl8152_driver = { 10147 .name = MODULENAME, 10148 .id_table = rtl8152_table, 10149 .probe = rtl8152_probe, 10150 .disconnect = rtl8152_disconnect, 10151 .suspend = rtl8152_suspend, 10152 .resume = rtl8152_resume, 10153 .reset_resume = rtl8152_reset_resume, 10154 .pre_reset = rtl8152_pre_reset, 10155 .post_reset = rtl8152_post_reset, 10156 .supports_autosuspend = 1, 10157 .disable_hub_initiated_lpm = 1, 10158 }; 10159 10160 static int rtl8152_cfgselector_choose_configuration(struct usb_device *udev) 10161 { 10162 struct usb_host_config *c; 10163 int i, num_configs; 10164 10165 /* Switch the device to vendor mode, if and only if the vendor mode 10166 * driver supports it. 10167 */ 10168 if (__rtl_get_hw_ver(udev) == RTL_VER_UNKNOWN) 10169 return -ENODEV; 10170 10171 /* The vendor mode is not always config #1, so to find it out. */ 10172 c = udev->config; 10173 num_configs = udev->descriptor.bNumConfigurations; 10174 for (i = 0; i < num_configs; (i++, c++)) { 10175 struct usb_interface_descriptor *desc = NULL; 10176 10177 if (!c->desc.bNumInterfaces) 10178 continue; 10179 desc = &c->intf_cache[0]->altsetting->desc; 10180 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC) 10181 break; 10182 } 10183 10184 if (i == num_configs) 10185 return -ENODEV; 10186 10187 return c->desc.bConfigurationValue; 10188 } 10189 10190 static struct usb_device_driver rtl8152_cfgselector_driver = { 10191 .name = MODULENAME "-cfgselector", 10192 .choose_configuration = rtl8152_cfgselector_choose_configuration, 10193 .id_table = rtl8152_table, 10194 .generic_subclass = 1, 10195 .supports_autosuspend = 1, 10196 }; 10197 10198 static int __init rtl8152_driver_init(void) 10199 { 10200 int ret; 10201 10202 ret = usb_register_device_driver(&rtl8152_cfgselector_driver, THIS_MODULE); 10203 if (ret) 10204 return ret; 10205 10206 ret = usb_register(&rtl8152_driver); 10207 if (ret) 10208 usb_deregister_device_driver(&rtl8152_cfgselector_driver); 10209 10210 return ret; 10211 } 10212 10213 static void __exit rtl8152_driver_exit(void) 10214 { 10215 usb_deregister(&rtl8152_driver); 10216 usb_deregister_device_driver(&rtl8152_cfgselector_driver); 10217 } 10218 10219 module_init(rtl8152_driver_init); 10220 module_exit(rtl8152_driver_exit); 10221 10222 MODULE_AUTHOR(DRIVER_AUTHOR); 10223 MODULE_DESCRIPTION(DRIVER_DESC); 10224 MODULE_LICENSE("GPL"); 10225