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/usb.h> 14 #include <linux/crc32.h> 15 #include <linux/if_vlan.h> 16 #include <linux/uaccess.h> 17 #include <linux/list.h> 18 #include <linux/ip.h> 19 #include <linux/ipv6.h> 20 #include <net/ip6_checksum.h> 21 #include <uapi/linux/mdio.h> 22 #include <linux/mdio.h> 23 #include <linux/usb/cdc.h> 24 #include <linux/suspend.h> 25 #include <linux/atomic.h> 26 #include <linux/acpi.h> 27 #include <linux/firmware.h> 28 #include <crypto/hash.h> 29 30 /* Information for net-next */ 31 #define NETNEXT_VERSION "11" 32 33 /* Information for net */ 34 #define NET_VERSION "11" 35 36 #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION 37 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" 38 #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters" 39 #define MODULENAME "r8152" 40 41 #define R8152_PHY_ID 32 42 43 #define PLA_IDR 0xc000 44 #define PLA_RCR 0xc010 45 #define PLA_RMS 0xc016 46 #define PLA_RXFIFO_CTRL0 0xc0a0 47 #define PLA_RXFIFO_CTRL1 0xc0a4 48 #define PLA_RXFIFO_CTRL2 0xc0a8 49 #define PLA_DMY_REG0 0xc0b0 50 #define PLA_FMC 0xc0b4 51 #define PLA_CFG_WOL 0xc0b6 52 #define PLA_TEREDO_CFG 0xc0bc 53 #define PLA_TEREDO_WAKE_BASE 0xc0c4 54 #define PLA_MAR 0xcd00 55 #define PLA_BACKUP 0xd000 56 #define PLA_BDC_CR 0xd1a0 57 #define PLA_TEREDO_TIMER 0xd2cc 58 #define PLA_REALWOW_TIMER 0xd2e8 59 #define PLA_UPHY_TIMER 0xd388 60 #define PLA_SUSPEND_FLAG 0xd38a 61 #define PLA_INDICATE_FALG 0xd38c 62 #define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */ 63 #define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */ 64 #define PLA_EXTRA_STATUS 0xd398 65 #define PLA_EFUSE_DATA 0xdd00 66 #define PLA_EFUSE_CMD 0xdd02 67 #define PLA_LEDSEL 0xdd90 68 #define PLA_LED_FEATURE 0xdd92 69 #define PLA_PHYAR 0xde00 70 #define PLA_BOOT_CTRL 0xe004 71 #define PLA_LWAKE_CTRL_REG 0xe007 72 #define PLA_GPHY_INTR_IMR 0xe022 73 #define PLA_EEE_CR 0xe040 74 #define PLA_EEEP_CR 0xe080 75 #define PLA_MAC_PWR_CTRL 0xe0c0 76 #define PLA_MAC_PWR_CTRL2 0xe0ca 77 #define PLA_MAC_PWR_CTRL3 0xe0cc 78 #define PLA_MAC_PWR_CTRL4 0xe0ce 79 #define PLA_WDT6_CTRL 0xe428 80 #define PLA_TCR0 0xe610 81 #define PLA_TCR1 0xe612 82 #define PLA_MTPS 0xe615 83 #define PLA_TXFIFO_CTRL 0xe618 84 #define PLA_RSTTALLY 0xe800 85 #define PLA_CR 0xe813 86 #define PLA_CRWECR 0xe81c 87 #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */ 88 #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */ 89 #define PLA_CONFIG5 0xe822 90 #define PLA_PHY_PWR 0xe84c 91 #define PLA_OOB_CTRL 0xe84f 92 #define PLA_CPCR 0xe854 93 #define PLA_MISC_0 0xe858 94 #define PLA_MISC_1 0xe85a 95 #define PLA_OCP_GPHY_BASE 0xe86c 96 #define PLA_TALLYCNT 0xe890 97 #define PLA_SFF_STS_7 0xe8de 98 #define PLA_PHYSTATUS 0xe908 99 #define PLA_CONFIG6 0xe90a /* CONFIG6 */ 100 #define PLA_BP_BA 0xfc26 101 #define PLA_BP_0 0xfc28 102 #define PLA_BP_1 0xfc2a 103 #define PLA_BP_2 0xfc2c 104 #define PLA_BP_3 0xfc2e 105 #define PLA_BP_4 0xfc30 106 #define PLA_BP_5 0xfc32 107 #define PLA_BP_6 0xfc34 108 #define PLA_BP_7 0xfc36 109 #define PLA_BP_EN 0xfc38 110 111 #define USB_USB2PHY 0xb41e 112 #define USB_SSPHYLINK1 0xb426 113 #define USB_SSPHYLINK2 0xb428 114 #define USB_U2P3_CTRL 0xb460 115 #define USB_CSR_DUMMY1 0xb464 116 #define USB_CSR_DUMMY2 0xb466 117 #define USB_DEV_STAT 0xb808 118 #define USB_CONNECT_TIMER 0xcbf8 119 #define USB_MSC_TIMER 0xcbfc 120 #define USB_BURST_SIZE 0xcfc0 121 #define USB_FW_FIX_EN0 0xcfca 122 #define USB_FW_FIX_EN1 0xcfcc 123 #define USB_LPM_CONFIG 0xcfd8 124 #define USB_CSTMR 0xcfef /* RTL8153A */ 125 #define USB_FW_CTRL 0xd334 /* RTL8153B */ 126 #define USB_FC_TIMER 0xd340 127 #define USB_USB_CTRL 0xd406 128 #define USB_PHY_CTRL 0xd408 129 #define USB_TX_AGG 0xd40a 130 #define USB_RX_BUF_TH 0xd40c 131 #define USB_USB_TIMER 0xd428 132 #define USB_RX_EARLY_TIMEOUT 0xd42c 133 #define USB_RX_EARLY_SIZE 0xd42e 134 #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */ 135 #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */ 136 #define USB_TX_DMA 0xd434 137 #define USB_UPT_RXDMA_OWN 0xd437 138 #define USB_TOLERANCE 0xd490 139 #define USB_LPM_CTRL 0xd41a 140 #define USB_BMU_RESET 0xd4b0 141 #define USB_U1U2_TIMER 0xd4da 142 #define USB_FW_TASK 0xd4e8 /* RTL8153B */ 143 #define USB_UPS_CTRL 0xd800 144 #define USB_POWER_CUT 0xd80a 145 #define USB_MISC_0 0xd81a 146 #define USB_MISC_1 0xd81f 147 #define USB_AFE_CTRL2 0xd824 148 #define USB_UPS_CFG 0xd842 149 #define USB_UPS_FLAGS 0xd848 150 #define USB_WDT1_CTRL 0xe404 151 #define USB_WDT11_CTRL 0xe43c 152 #define USB_BP_BA PLA_BP_BA 153 #define USB_BP_0 PLA_BP_0 154 #define USB_BP_1 PLA_BP_1 155 #define USB_BP_2 PLA_BP_2 156 #define USB_BP_3 PLA_BP_3 157 #define USB_BP_4 PLA_BP_4 158 #define USB_BP_5 PLA_BP_5 159 #define USB_BP_6 PLA_BP_6 160 #define USB_BP_7 PLA_BP_7 161 #define USB_BP_EN PLA_BP_EN /* RTL8153A */ 162 #define USB_BP_8 0xfc38 /* RTL8153B */ 163 #define USB_BP_9 0xfc3a 164 #define USB_BP_10 0xfc3c 165 #define USB_BP_11 0xfc3e 166 #define USB_BP_12 0xfc40 167 #define USB_BP_13 0xfc42 168 #define USB_BP_14 0xfc44 169 #define USB_BP_15 0xfc46 170 #define USB_BP2_EN 0xfc48 171 172 /* OCP Registers */ 173 #define OCP_ALDPS_CONFIG 0x2010 174 #define OCP_EEE_CONFIG1 0x2080 175 #define OCP_EEE_CONFIG2 0x2092 176 #define OCP_EEE_CONFIG3 0x2094 177 #define OCP_BASE_MII 0xa400 178 #define OCP_EEE_AR 0xa41a 179 #define OCP_EEE_DATA 0xa41c 180 #define OCP_PHY_STATUS 0xa420 181 #define OCP_NCTL_CFG 0xa42c 182 #define OCP_POWER_CFG 0xa430 183 #define OCP_EEE_CFG 0xa432 184 #define OCP_SRAM_ADDR 0xa436 185 #define OCP_SRAM_DATA 0xa438 186 #define OCP_DOWN_SPEED 0xa442 187 #define OCP_EEE_ABLE 0xa5c4 188 #define OCP_EEE_ADV 0xa5d0 189 #define OCP_EEE_LPABLE 0xa5d2 190 #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */ 191 #define OCP_PHY_PATCH_STAT 0xb800 192 #define OCP_PHY_PATCH_CMD 0xb820 193 #define OCP_PHY_LOCK 0xb82e 194 #define OCP_ADC_IOFFSET 0xbcfc 195 #define OCP_ADC_CFG 0xbc06 196 #define OCP_SYSCLK_CFG 0xc416 197 198 /* SRAM Register */ 199 #define SRAM_GREEN_CFG 0x8011 200 #define SRAM_LPF_CFG 0x8012 201 #define SRAM_10M_AMP1 0x8080 202 #define SRAM_10M_AMP2 0x8082 203 #define SRAM_IMPEDANCE 0x8084 204 #define SRAM_PHY_LOCK 0xb82e 205 206 /* PLA_RCR */ 207 #define RCR_AAP 0x00000001 208 #define RCR_APM 0x00000002 209 #define RCR_AM 0x00000004 210 #define RCR_AB 0x00000008 211 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB) 212 213 /* PLA_RXFIFO_CTRL0 */ 214 #define RXFIFO_THR1_NORMAL 0x00080002 215 #define RXFIFO_THR1_OOB 0x01800003 216 217 /* PLA_RXFIFO_CTRL1 */ 218 #define RXFIFO_THR2_FULL 0x00000060 219 #define RXFIFO_THR2_HIGH 0x00000038 220 #define RXFIFO_THR2_OOB 0x0000004a 221 #define RXFIFO_THR2_NORMAL 0x00a0 222 223 /* PLA_RXFIFO_CTRL2 */ 224 #define RXFIFO_THR3_FULL 0x00000078 225 #define RXFIFO_THR3_HIGH 0x00000048 226 #define RXFIFO_THR3_OOB 0x0000005a 227 #define RXFIFO_THR3_NORMAL 0x0110 228 229 /* PLA_TXFIFO_CTRL */ 230 #define TXFIFO_THR_NORMAL 0x00400008 231 #define TXFIFO_THR_NORMAL2 0x01000008 232 233 /* PLA_DMY_REG0 */ 234 #define ECM_ALDPS 0x0002 235 236 /* PLA_FMC */ 237 #define FMC_FCR_MCU_EN 0x0001 238 239 /* PLA_EEEP_CR */ 240 #define EEEP_CR_EEEP_TX 0x0002 241 242 /* PLA_WDT6_CTRL */ 243 #define WDT6_SET_MODE 0x0010 244 245 /* PLA_TCR0 */ 246 #define TCR0_TX_EMPTY 0x0800 247 #define TCR0_AUTO_FIFO 0x0080 248 249 /* PLA_TCR1 */ 250 #define VERSION_MASK 0x7cf0 251 252 /* PLA_MTPS */ 253 #define MTPS_JUMBO (12 * 1024 / 64) 254 #define MTPS_DEFAULT (6 * 1024 / 64) 255 256 /* PLA_RSTTALLY */ 257 #define TALLY_RESET 0x0001 258 259 /* PLA_CR */ 260 #define CR_RST 0x10 261 #define CR_RE 0x08 262 #define CR_TE 0x04 263 264 /* PLA_CRWECR */ 265 #define CRWECR_NORAML 0x00 266 #define CRWECR_CONFIG 0xc0 267 268 /* PLA_OOB_CTRL */ 269 #define NOW_IS_OOB 0x80 270 #define TXFIFO_EMPTY 0x20 271 #define RXFIFO_EMPTY 0x10 272 #define LINK_LIST_READY 0x02 273 #define DIS_MCU_CLROOB 0x01 274 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY) 275 276 /* PLA_MISC_1 */ 277 #define RXDY_GATED_EN 0x0008 278 279 /* PLA_SFF_STS_7 */ 280 #define RE_INIT_LL 0x8000 281 #define MCU_BORW_EN 0x4000 282 283 /* PLA_CPCR */ 284 #define CPCR_RX_VLAN 0x0040 285 286 /* PLA_CFG_WOL */ 287 #define MAGIC_EN 0x0001 288 289 /* PLA_TEREDO_CFG */ 290 #define TEREDO_SEL 0x8000 291 #define TEREDO_WAKE_MASK 0x7f00 292 #define TEREDO_RS_EVENT_MASK 0x00fe 293 #define OOB_TEREDO_EN 0x0001 294 295 /* PLA_BDC_CR */ 296 #define ALDPS_PROXY_MODE 0x0001 297 298 /* PLA_EFUSE_CMD */ 299 #define EFUSE_READ_CMD BIT(15) 300 #define EFUSE_DATA_BIT16 BIT(7) 301 302 /* PLA_CONFIG34 */ 303 #define LINK_ON_WAKE_EN 0x0010 304 #define LINK_OFF_WAKE_EN 0x0008 305 306 /* PLA_CONFIG6 */ 307 #define LANWAKE_CLR_EN BIT(0) 308 309 /* PLA_CONFIG5 */ 310 #define BWF_EN 0x0040 311 #define MWF_EN 0x0020 312 #define UWF_EN 0x0010 313 #define LAN_WAKE_EN 0x0002 314 315 /* PLA_LED_FEATURE */ 316 #define LED_MODE_MASK 0x0700 317 318 /* PLA_PHY_PWR */ 319 #define TX_10M_IDLE_EN 0x0080 320 #define PFM_PWM_SWITCH 0x0040 321 #define TEST_IO_OFF BIT(4) 322 323 /* PLA_MAC_PWR_CTRL */ 324 #define D3_CLK_GATED_EN 0x00004000 325 #define MCU_CLK_RATIO 0x07010f07 326 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f 327 #define ALDPS_SPDWN_RATIO 0x0f87 328 329 /* PLA_MAC_PWR_CTRL2 */ 330 #define EEE_SPDWN_RATIO 0x8007 331 #define MAC_CLK_SPDWN_EN BIT(15) 332 333 /* PLA_MAC_PWR_CTRL3 */ 334 #define PLA_MCU_SPDWN_EN BIT(14) 335 #define PKT_AVAIL_SPDWN_EN 0x0100 336 #define SUSPEND_SPDWN_EN 0x0004 337 #define U1U2_SPDWN_EN 0x0002 338 #define L1_SPDWN_EN 0x0001 339 340 /* PLA_MAC_PWR_CTRL4 */ 341 #define PWRSAVE_SPDWN_EN 0x1000 342 #define RXDV_SPDWN_EN 0x0800 343 #define TX10MIDLE_EN 0x0100 344 #define TP100_SPDWN_EN 0x0020 345 #define TP500_SPDWN_EN 0x0010 346 #define TP1000_SPDWN_EN 0x0008 347 #define EEE_SPDWN_EN 0x0001 348 349 /* PLA_GPHY_INTR_IMR */ 350 #define GPHY_STS_MSK 0x0001 351 #define SPEED_DOWN_MSK 0x0002 352 #define SPDWN_RXDV_MSK 0x0004 353 #define SPDWN_LINKCHG_MSK 0x0008 354 355 /* PLA_PHYAR */ 356 #define PHYAR_FLAG 0x80000000 357 358 /* PLA_EEE_CR */ 359 #define EEE_RX_EN 0x0001 360 #define EEE_TX_EN 0x0002 361 362 /* PLA_BOOT_CTRL */ 363 #define AUTOLOAD_DONE 0x0002 364 365 /* PLA_LWAKE_CTRL_REG */ 366 #define LANWAKE_PIN BIT(7) 367 368 /* PLA_SUSPEND_FLAG */ 369 #define LINK_CHG_EVENT BIT(0) 370 371 /* PLA_INDICATE_FALG */ 372 #define UPCOMING_RUNTIME_D3 BIT(0) 373 374 /* PLA_MACDBG_PRE and PLA_MACDBG_POST */ 375 #define DEBUG_OE BIT(0) 376 #define DEBUG_LTSSM 0x0082 377 378 /* PLA_EXTRA_STATUS */ 379 #define CUR_LINK_OK BIT(15) 380 #define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */ 381 #define LINK_CHANGE_FLAG BIT(8) 382 #define POLL_LINK_CHG BIT(0) 383 384 /* USB_USB2PHY */ 385 #define USB2PHY_SUSPEND 0x0001 386 #define USB2PHY_L1 0x0002 387 388 /* USB_SSPHYLINK1 */ 389 #define DELAY_PHY_PWR_CHG BIT(1) 390 391 /* USB_SSPHYLINK2 */ 392 #define pwd_dn_scale_mask 0x3ffe 393 #define pwd_dn_scale(x) ((x) << 1) 394 395 /* USB_CSR_DUMMY1 */ 396 #define DYNAMIC_BURST 0x0001 397 398 /* USB_CSR_DUMMY2 */ 399 #define EP4_FULL_FC 0x0001 400 401 /* USB_DEV_STAT */ 402 #define STAT_SPEED_MASK 0x0006 403 #define STAT_SPEED_HIGH 0x0000 404 #define STAT_SPEED_FULL 0x0002 405 406 /* USB_FW_FIX_EN0 */ 407 #define FW_FIX_SUSPEND BIT(14) 408 409 /* USB_FW_FIX_EN1 */ 410 #define FW_IP_RESET_EN BIT(9) 411 412 /* USB_LPM_CONFIG */ 413 #define LPM_U1U2_EN BIT(0) 414 415 /* USB_TX_AGG */ 416 #define TX_AGG_MAX_THRESHOLD 0x03 417 418 /* USB_RX_BUF_TH */ 419 #define RX_THR_SUPPER 0x0c350180 420 #define RX_THR_HIGH 0x7a120180 421 #define RX_THR_SLOW 0xffff0180 422 #define RX_THR_B 0x00010001 423 424 /* USB_TX_DMA */ 425 #define TEST_MODE_DISABLE 0x00000001 426 #define TX_SIZE_ADJUST1 0x00000100 427 428 /* USB_BMU_RESET */ 429 #define BMU_RESET_EP_IN 0x01 430 #define BMU_RESET_EP_OUT 0x02 431 432 /* USB_UPT_RXDMA_OWN */ 433 #define OWN_UPDATE BIT(0) 434 #define OWN_CLEAR BIT(1) 435 436 /* USB_FW_TASK */ 437 #define FC_PATCH_TASK BIT(1) 438 439 /* USB_UPS_CTRL */ 440 #define POWER_CUT 0x0100 441 442 /* USB_PM_CTRL_STATUS */ 443 #define RESUME_INDICATE 0x0001 444 445 /* USB_CSTMR */ 446 #define FORCE_SUPER BIT(0) 447 448 /* USB_FW_CTRL */ 449 #define FLOW_CTRL_PATCH_OPT BIT(1) 450 451 /* USB_FC_TIMER */ 452 #define CTRL_TIMER_EN BIT(15) 453 454 /* USB_USB_CTRL */ 455 #define RX_AGG_DISABLE 0x0010 456 #define RX_ZERO_EN 0x0080 457 458 /* USB_U2P3_CTRL */ 459 #define U2P3_ENABLE 0x0001 460 461 /* USB_POWER_CUT */ 462 #define PWR_EN 0x0001 463 #define PHASE2_EN 0x0008 464 #define UPS_EN BIT(4) 465 #define USP_PREWAKE BIT(5) 466 467 /* USB_MISC_0 */ 468 #define PCUT_STATUS 0x0001 469 470 /* USB_RX_EARLY_TIMEOUT */ 471 #define COALESCE_SUPER 85000U 472 #define COALESCE_HIGH 250000U 473 #define COALESCE_SLOW 524280U 474 475 /* USB_WDT1_CTRL */ 476 #define WTD1_EN BIT(0) 477 478 /* USB_WDT11_CTRL */ 479 #define TIMER11_EN 0x0001 480 481 /* USB_LPM_CTRL */ 482 /* bit 4 ~ 5: fifo empty boundary */ 483 #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */ 484 /* bit 2 ~ 3: LMP timer */ 485 #define LPM_TIMER_MASK 0x0c 486 #define LPM_TIMER_500MS 0x04 /* 500 ms */ 487 #define LPM_TIMER_500US 0x0c /* 500 us */ 488 #define ROK_EXIT_LPM 0x02 489 490 /* USB_AFE_CTRL2 */ 491 #define SEN_VAL_MASK 0xf800 492 #define SEN_VAL_NORMAL 0xa000 493 #define SEL_RXIDLE 0x0100 494 495 /* USB_UPS_CFG */ 496 #define SAW_CNT_1MS_MASK 0x0fff 497 498 /* USB_UPS_FLAGS */ 499 #define UPS_FLAGS_R_TUNE BIT(0) 500 #define UPS_FLAGS_EN_10M_CKDIV BIT(1) 501 #define UPS_FLAGS_250M_CKDIV BIT(2) 502 #define UPS_FLAGS_EN_ALDPS BIT(3) 503 #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4) 504 #define ups_flags_speed(x) ((x) << 16) 505 #define UPS_FLAGS_EN_EEE BIT(20) 506 #define UPS_FLAGS_EN_500M_EEE BIT(21) 507 #define UPS_FLAGS_EN_EEE_CKDIV BIT(22) 508 #define UPS_FLAGS_EEE_PLLOFF_100 BIT(23) 509 #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24) 510 #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25) 511 #define UPS_FLAGS_EN_GREEN BIT(26) 512 #define UPS_FLAGS_EN_FLOW_CTR BIT(27) 513 514 enum spd_duplex { 515 NWAY_10M_HALF, 516 NWAY_10M_FULL, 517 NWAY_100M_HALF, 518 NWAY_100M_FULL, 519 NWAY_1000M_FULL, 520 FORCE_10M_HALF, 521 FORCE_10M_FULL, 522 FORCE_100M_HALF, 523 FORCE_100M_FULL, 524 }; 525 526 /* OCP_ALDPS_CONFIG */ 527 #define ENPWRSAVE 0x8000 528 #define ENPDNPS 0x0200 529 #define LINKENA 0x0100 530 #define DIS_SDSAVE 0x0010 531 532 /* OCP_PHY_STATUS */ 533 #define PHY_STAT_MASK 0x0007 534 #define PHY_STAT_EXT_INIT 2 535 #define PHY_STAT_LAN_ON 3 536 #define PHY_STAT_PWRDN 5 537 538 /* OCP_NCTL_CFG */ 539 #define PGA_RETURN_EN BIT(1) 540 541 /* OCP_POWER_CFG */ 542 #define EEE_CLKDIV_EN 0x8000 543 #define EN_ALDPS 0x0004 544 #define EN_10M_PLLOFF 0x0001 545 546 /* OCP_EEE_CONFIG1 */ 547 #define RG_TXLPI_MSK_HFDUP 0x8000 548 #define RG_MATCLR_EN 0x4000 549 #define EEE_10_CAP 0x2000 550 #define EEE_NWAY_EN 0x1000 551 #define TX_QUIET_EN 0x0200 552 #define RX_QUIET_EN 0x0100 553 #define sd_rise_time_mask 0x0070 554 #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */ 555 #define RG_RXLPI_MSK_HFDUP 0x0008 556 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ 557 558 /* OCP_EEE_CONFIG2 */ 559 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ 560 #define RG_DACQUIET_EN 0x0400 561 #define RG_LDVQUIET_EN 0x0200 562 #define RG_CKRSEL 0x0020 563 #define RG_EEEPRG_EN 0x0010 564 565 /* OCP_EEE_CONFIG3 */ 566 #define fast_snr_mask 0xff80 567 #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */ 568 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ 569 #define MSK_PH 0x0006 /* bit 0 ~ 3 */ 570 571 /* OCP_EEE_AR */ 572 /* bit[15:14] function */ 573 #define FUN_ADDR 0x0000 574 #define FUN_DATA 0x4000 575 /* bit[4:0] device addr */ 576 577 /* OCP_EEE_CFG */ 578 #define CTAP_SHORT_EN 0x0040 579 #define EEE10_EN 0x0010 580 581 /* OCP_DOWN_SPEED */ 582 #define EN_EEE_CMODE BIT(14) 583 #define EN_EEE_1000 BIT(13) 584 #define EN_EEE_100 BIT(12) 585 #define EN_10M_CLKDIV BIT(11) 586 #define EN_10M_BGOFF 0x0080 587 588 /* OCP_PHY_STATE */ 589 #define TXDIS_STATE 0x01 590 #define ABD_STATE 0x02 591 592 /* OCP_PHY_PATCH_STAT */ 593 #define PATCH_READY BIT(6) 594 595 /* OCP_PHY_PATCH_CMD */ 596 #define PATCH_REQUEST BIT(4) 597 598 /* OCP_PHY_LOCK */ 599 #define PATCH_LOCK BIT(0) 600 601 /* OCP_ADC_CFG */ 602 #define CKADSEL_L 0x0100 603 #define ADC_EN 0x0080 604 #define EN_EMI_L 0x0040 605 606 /* OCP_SYSCLK_CFG */ 607 #define clk_div_expo(x) (min(x, 5) << 8) 608 609 /* SRAM_GREEN_CFG */ 610 #define GREEN_ETH_EN BIT(15) 611 #define R_TUNE_EN BIT(11) 612 613 /* SRAM_LPF_CFG */ 614 #define LPF_AUTO_TUNE 0x8000 615 616 /* SRAM_10M_AMP1 */ 617 #define GDAC_IB_UPALL 0x0008 618 619 /* SRAM_10M_AMP2 */ 620 #define AMP_DN 0x0200 621 622 /* SRAM_IMPEDANCE */ 623 #define RX_DRIVING_MASK 0x6000 624 625 /* SRAM_PHY_LOCK */ 626 #define PHY_PATCH_LOCK 0x0001 627 628 /* MAC PASSTHRU */ 629 #define AD_MASK 0xfee0 630 #define BND_MASK 0x0004 631 #define BD_MASK 0x0001 632 #define EFUSE 0xcfdb 633 #define PASS_THRU_MASK 0x1 634 635 #define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */ 636 637 enum rtl_register_content { 638 _1000bps = 0x10, 639 _100bps = 0x08, 640 _10bps = 0x04, 641 LINK_STATUS = 0x02, 642 FULL_DUP = 0x01, 643 }; 644 645 #define RTL8152_MAX_TX 4 646 #define RTL8152_MAX_RX 10 647 #define INTBUFSIZE 2 648 #define TX_ALIGN 4 649 #define RX_ALIGN 8 650 651 #define RTL8152_RX_MAX_PENDING 4096 652 #define RTL8152_RXFG_HEADSZ 256 653 654 #define INTR_LINK 0x0004 655 656 #define RTL8152_REQT_READ 0xc0 657 #define RTL8152_REQT_WRITE 0x40 658 #define RTL8152_REQ_GET_REGS 0x05 659 #define RTL8152_REQ_SET_REGS 0x05 660 661 #define BYTE_EN_DWORD 0xff 662 #define BYTE_EN_WORD 0x33 663 #define BYTE_EN_BYTE 0x11 664 #define BYTE_EN_SIX_BYTES 0x3f 665 #define BYTE_EN_START_MASK 0x0f 666 #define BYTE_EN_END_MASK 0xf0 667 668 #define RTL8153_MAX_PACKET 9216 /* 9K */ 669 #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \ 670 ETH_FCS_LEN) 671 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) 672 #define RTL8153_RMS RTL8153_MAX_PACKET 673 #define RTL8152_TX_TIMEOUT (5 * HZ) 674 #define RTL8152_NAPI_WEIGHT 64 675 #define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \ 676 sizeof(struct rx_desc) + RX_ALIGN) 677 678 /* rtl8152 flags */ 679 enum rtl8152_flags { 680 RTL8152_UNPLUG = 0, 681 RTL8152_SET_RX_MODE, 682 WORK_ENABLE, 683 RTL8152_LINK_CHG, 684 SELECTIVE_SUSPEND, 685 PHY_RESET, 686 SCHEDULE_TASKLET, 687 GREEN_ETHERNET, 688 DELL_TB_RX_AGG_BUG, 689 LENOVO_MACPASSTHRU, 690 }; 691 692 /* Define these values to match your device */ 693 #define VENDOR_ID_REALTEK 0x0bda 694 #define VENDOR_ID_MICROSOFT 0x045e 695 #define VENDOR_ID_SAMSUNG 0x04e8 696 #define VENDOR_ID_LENOVO 0x17ef 697 #define VENDOR_ID_LINKSYS 0x13b1 698 #define VENDOR_ID_NVIDIA 0x0955 699 #define VENDOR_ID_TPLINK 0x2357 700 701 #define MCU_TYPE_PLA 0x0100 702 #define MCU_TYPE_USB 0x0000 703 704 struct tally_counter { 705 __le64 tx_packets; 706 __le64 rx_packets; 707 __le64 tx_errors; 708 __le32 rx_errors; 709 __le16 rx_missed; 710 __le16 align_errors; 711 __le32 tx_one_collision; 712 __le32 tx_multi_collision; 713 __le64 rx_unicast; 714 __le64 rx_broadcast; 715 __le32 rx_multicast; 716 __le16 tx_aborted; 717 __le16 tx_underrun; 718 }; 719 720 struct rx_desc { 721 __le32 opts1; 722 #define RX_LEN_MASK 0x7fff 723 724 __le32 opts2; 725 #define RD_UDP_CS BIT(23) 726 #define RD_TCP_CS BIT(22) 727 #define RD_IPV6_CS BIT(20) 728 #define RD_IPV4_CS BIT(19) 729 730 __le32 opts3; 731 #define IPF BIT(23) /* IP checksum fail */ 732 #define UDPF BIT(22) /* UDP checksum fail */ 733 #define TCPF BIT(21) /* TCP checksum fail */ 734 #define RX_VLAN_TAG BIT(16) 735 736 __le32 opts4; 737 __le32 opts5; 738 __le32 opts6; 739 }; 740 741 struct tx_desc { 742 __le32 opts1; 743 #define TX_FS BIT(31) /* First segment of a packet */ 744 #define TX_LS BIT(30) /* Final segment of a packet */ 745 #define GTSENDV4 BIT(28) 746 #define GTSENDV6 BIT(27) 747 #define GTTCPHO_SHIFT 18 748 #define GTTCPHO_MAX 0x7fU 749 #define TX_LEN_MAX 0x3ffffU 750 751 __le32 opts2; 752 #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */ 753 #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */ 754 #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */ 755 #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */ 756 #define MSS_SHIFT 17 757 #define MSS_MAX 0x7ffU 758 #define TCPHO_SHIFT 17 759 #define TCPHO_MAX 0x7ffU 760 #define TX_VLAN_TAG BIT(16) 761 }; 762 763 struct r8152; 764 765 struct rx_agg { 766 struct list_head list, info_list; 767 struct urb *urb; 768 struct r8152 *context; 769 struct page *page; 770 void *buffer; 771 }; 772 773 struct tx_agg { 774 struct list_head list; 775 struct urb *urb; 776 struct r8152 *context; 777 void *buffer; 778 void *head; 779 u32 skb_num; 780 u32 skb_len; 781 }; 782 783 struct r8152 { 784 unsigned long flags; 785 struct usb_device *udev; 786 struct napi_struct napi; 787 struct usb_interface *intf; 788 struct net_device *netdev; 789 struct urb *intr_urb; 790 struct tx_agg tx_info[RTL8152_MAX_TX]; 791 struct list_head rx_info, rx_used; 792 struct list_head rx_done, tx_free; 793 struct sk_buff_head tx_queue, rx_queue; 794 spinlock_t rx_lock, tx_lock; 795 struct delayed_work schedule, hw_phy_work; 796 struct mii_if_info mii; 797 struct mutex control; /* use for hw setting */ 798 #ifdef CONFIG_PM_SLEEP 799 struct notifier_block pm_notifier; 800 #endif 801 struct tasklet_struct tx_tl; 802 803 struct rtl_ops { 804 void (*init)(struct r8152 *tp); 805 int (*enable)(struct r8152 *tp); 806 void (*disable)(struct r8152 *tp); 807 void (*up)(struct r8152 *tp); 808 void (*down)(struct r8152 *tp); 809 void (*unload)(struct r8152 *tp); 810 int (*eee_get)(struct r8152 *tp, struct ethtool_eee *eee); 811 int (*eee_set)(struct r8152 *tp, struct ethtool_eee *eee); 812 bool (*in_nway)(struct r8152 *tp); 813 void (*hw_phy_cfg)(struct r8152 *tp); 814 void (*autosuspend_en)(struct r8152 *tp, bool enable); 815 } rtl_ops; 816 817 struct ups_info { 818 u32 _10m_ckdiv:1; 819 u32 _250m_ckdiv:1; 820 u32 aldps:1; 821 u32 lite_mode:2; 822 u32 speed_duplex:4; 823 u32 eee:1; 824 u32 eee_lite:1; 825 u32 eee_ckdiv:1; 826 u32 eee_plloff_100:1; 827 u32 eee_plloff_giga:1; 828 u32 eee_cmod_lv:1; 829 u32 green:1; 830 u32 flow_control:1; 831 u32 ctap_short_off:1; 832 } ups_info; 833 834 #define RTL_VER_SIZE 32 835 836 struct rtl_fw { 837 const char *fw_name; 838 const struct firmware *fw; 839 840 char version[RTL_VER_SIZE]; 841 int (*pre_fw)(struct r8152 *tp); 842 int (*post_fw)(struct r8152 *tp); 843 844 bool retry; 845 } rtl_fw; 846 847 atomic_t rx_count; 848 849 bool eee_en; 850 int intr_interval; 851 u32 saved_wolopts; 852 u32 msg_enable; 853 u32 tx_qlen; 854 u32 coalesce; 855 u32 advertising; 856 u32 rx_buf_sz; 857 u32 rx_copybreak; 858 u32 rx_pending; 859 860 u16 ocp_base; 861 u16 speed; 862 u16 eee_adv; 863 u8 *intr_buff; 864 u8 version; 865 u8 duplex; 866 u8 autoneg; 867 }; 868 869 /** 870 * struct fw_block - block type and total length 871 * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA, 872 * RTL_FW_USB and so on. 873 * @length: total length of the current block. 874 */ 875 struct fw_block { 876 __le32 type; 877 __le32 length; 878 } __packed; 879 880 /** 881 * struct fw_header - header of the firmware file 882 * @checksum: checksum of sha256 which is calculated from the whole file 883 * except the checksum field of the file. That is, calculate sha256 884 * from the version field to the end of the file. 885 * @version: version of this firmware. 886 * @blocks: the first firmware block of the file 887 */ 888 struct fw_header { 889 u8 checksum[32]; 890 char version[RTL_VER_SIZE]; 891 struct fw_block blocks[0]; 892 } __packed; 893 894 /** 895 * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB. 896 * The layout of the firmware block is: 897 * <struct fw_mac> + <info> + <firmware data>. 898 * @fw_offset: offset of the firmware binary data. The start address of 899 * the data would be the address of struct fw_mac + @fw_offset. 900 * @fw_reg: the register to load the firmware. Depends on chip. 901 * @bp_ba_addr: the register to write break point base address. Depends on 902 * chip. 903 * @bp_ba_value: break point base address. Depends on chip. 904 * @bp_en_addr: the register to write break point enabled mask. Depends 905 * on chip. 906 * @bp_en_value: break point enabled mask. Depends on the firmware. 907 * @bp_start: the start register of break points. Depends on chip. 908 * @bp_num: the break point number which needs to be set for this firmware. 909 * Depends on the firmware. 910 * @bp: break points. Depends on firmware. 911 * @fw_ver_reg: the register to store the fw version. 912 * @fw_ver_data: the firmware version of the current type. 913 * @info: additional information for debugging, and is followed by the 914 * binary data of firmware. 915 */ 916 struct fw_mac { 917 struct fw_block blk_hdr; 918 __le16 fw_offset; 919 __le16 fw_reg; 920 __le16 bp_ba_addr; 921 __le16 bp_ba_value; 922 __le16 bp_en_addr; 923 __le16 bp_en_value; 924 __le16 bp_start; 925 __le16 bp_num; 926 __le16 bp[16]; /* any value determined by firmware */ 927 __le32 reserved; 928 __le16 fw_ver_reg; 929 u8 fw_ver_data; 930 char info[0]; 931 } __packed; 932 933 /** 934 * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START. 935 * This is used to set patch key when loading the firmware of PHY. 936 * @key_reg: the register to write the patch key. 937 * @key_data: patch key. 938 */ 939 struct fw_phy_patch_key { 940 struct fw_block blk_hdr; 941 __le16 key_reg; 942 __le16 key_data; 943 __le32 reserved; 944 } __packed; 945 946 /** 947 * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC. 948 * The layout of the firmware block is: 949 * <struct fw_phy_nc> + <info> + <firmware data>. 950 * @fw_offset: offset of the firmware binary data. The start address of 951 * the data would be the address of struct fw_phy_nc + @fw_offset. 952 * @fw_reg: the register to load the firmware. Depends on chip. 953 * @ba_reg: the register to write the base address. Depends on chip. 954 * @ba_data: base address. Depends on chip. 955 * @patch_en_addr: the register of enabling patch mode. Depends on chip. 956 * @patch_en_value: patch mode enabled mask. Depends on the firmware. 957 * @mode_reg: the regitster of switching the mode. 958 * @mod_pre: the mode needing to be set before loading the firmware. 959 * @mod_post: the mode to be set when finishing to load the firmware. 960 * @bp_start: the start register of break points. Depends on chip. 961 * @bp_num: the break point number which needs to be set for this firmware. 962 * Depends on the firmware. 963 * @bp: break points. Depends on firmware. 964 * @info: additional information for debugging, and is followed by the 965 * binary data of firmware. 966 */ 967 struct fw_phy_nc { 968 struct fw_block blk_hdr; 969 __le16 fw_offset; 970 __le16 fw_reg; 971 __le16 ba_reg; 972 __le16 ba_data; 973 __le16 patch_en_addr; 974 __le16 patch_en_value; 975 __le16 mode_reg; 976 __le16 mode_pre; 977 __le16 mode_post; 978 __le16 reserved; 979 __le16 bp_start; 980 __le16 bp_num; 981 __le16 bp[4]; 982 char info[0]; 983 } __packed; 984 985 enum rtl_fw_type { 986 RTL_FW_END = 0, 987 RTL_FW_PLA, 988 RTL_FW_USB, 989 RTL_FW_PHY_START, 990 RTL_FW_PHY_STOP, 991 RTL_FW_PHY_NC, 992 }; 993 994 enum rtl_version { 995 RTL_VER_UNKNOWN = 0, 996 RTL_VER_01, 997 RTL_VER_02, 998 RTL_VER_03, 999 RTL_VER_04, 1000 RTL_VER_05, 1001 RTL_VER_06, 1002 RTL_VER_07, 1003 RTL_VER_08, 1004 RTL_VER_09, 1005 RTL_VER_MAX 1006 }; 1007 1008 enum tx_csum_stat { 1009 TX_CSUM_SUCCESS = 0, 1010 TX_CSUM_TSO, 1011 TX_CSUM_NONE 1012 }; 1013 1014 #define RTL_ADVERTISED_10_HALF BIT(0) 1015 #define RTL_ADVERTISED_10_FULL BIT(1) 1016 #define RTL_ADVERTISED_100_HALF BIT(2) 1017 #define RTL_ADVERTISED_100_FULL BIT(3) 1018 #define RTL_ADVERTISED_1000_HALF BIT(4) 1019 #define RTL_ADVERTISED_1000_FULL BIT(5) 1020 1021 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). 1022 * The RTL chips use a 64 element hash table based on the Ethernet CRC. 1023 */ 1024 static const int multicast_filter_limit = 32; 1025 static unsigned int agg_buf_sz = 16384; 1026 1027 #define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \ 1028 VLAN_ETH_HLEN - ETH_FCS_LEN) 1029 1030 static 1031 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) 1032 { 1033 int ret; 1034 void *tmp; 1035 1036 tmp = kmalloc(size, GFP_KERNEL); 1037 if (!tmp) 1038 return -ENOMEM; 1039 1040 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0), 1041 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, 1042 value, index, tmp, size, 500); 1043 if (ret < 0) 1044 memset(data, 0xff, size); 1045 else 1046 memcpy(data, tmp, size); 1047 1048 kfree(tmp); 1049 1050 return ret; 1051 } 1052 1053 static 1054 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) 1055 { 1056 int ret; 1057 void *tmp; 1058 1059 tmp = kmemdup(data, size, GFP_KERNEL); 1060 if (!tmp) 1061 return -ENOMEM; 1062 1063 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0), 1064 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, 1065 value, index, tmp, size, 500); 1066 1067 kfree(tmp); 1068 1069 return ret; 1070 } 1071 1072 static void rtl_set_unplug(struct r8152 *tp) 1073 { 1074 if (tp->udev->state == USB_STATE_NOTATTACHED) { 1075 set_bit(RTL8152_UNPLUG, &tp->flags); 1076 smp_mb__after_atomic(); 1077 } 1078 } 1079 1080 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, 1081 void *data, u16 type) 1082 { 1083 u16 limit = 64; 1084 int ret = 0; 1085 1086 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1087 return -ENODEV; 1088 1089 /* both size and indix must be 4 bytes align */ 1090 if ((size & 3) || !size || (index & 3) || !data) 1091 return -EPERM; 1092 1093 if ((u32)index + (u32)size > 0xffff) 1094 return -EPERM; 1095 1096 while (size) { 1097 if (size > limit) { 1098 ret = get_registers(tp, index, type, limit, data); 1099 if (ret < 0) 1100 break; 1101 1102 index += limit; 1103 data += limit; 1104 size -= limit; 1105 } else { 1106 ret = get_registers(tp, index, type, size, data); 1107 if (ret < 0) 1108 break; 1109 1110 index += size; 1111 data += size; 1112 size = 0; 1113 break; 1114 } 1115 } 1116 1117 if (ret == -ENODEV) 1118 rtl_set_unplug(tp); 1119 1120 return ret; 1121 } 1122 1123 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, 1124 u16 size, void *data, u16 type) 1125 { 1126 int ret; 1127 u16 byteen_start, byteen_end, byen; 1128 u16 limit = 512; 1129 1130 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1131 return -ENODEV; 1132 1133 /* both size and indix must be 4 bytes align */ 1134 if ((size & 3) || !size || (index & 3) || !data) 1135 return -EPERM; 1136 1137 if ((u32)index + (u32)size > 0xffff) 1138 return -EPERM; 1139 1140 byteen_start = byteen & BYTE_EN_START_MASK; 1141 byteen_end = byteen & BYTE_EN_END_MASK; 1142 1143 byen = byteen_start | (byteen_start << 4); 1144 ret = set_registers(tp, index, type | byen, 4, data); 1145 if (ret < 0) 1146 goto error1; 1147 1148 index += 4; 1149 data += 4; 1150 size -= 4; 1151 1152 if (size) { 1153 size -= 4; 1154 1155 while (size) { 1156 if (size > limit) { 1157 ret = set_registers(tp, index, 1158 type | BYTE_EN_DWORD, 1159 limit, data); 1160 if (ret < 0) 1161 goto error1; 1162 1163 index += limit; 1164 data += limit; 1165 size -= limit; 1166 } else { 1167 ret = set_registers(tp, index, 1168 type | BYTE_EN_DWORD, 1169 size, data); 1170 if (ret < 0) 1171 goto error1; 1172 1173 index += size; 1174 data += size; 1175 size = 0; 1176 break; 1177 } 1178 } 1179 1180 byen = byteen_end | (byteen_end >> 4); 1181 ret = set_registers(tp, index, type | byen, 4, data); 1182 if (ret < 0) 1183 goto error1; 1184 } 1185 1186 error1: 1187 if (ret == -ENODEV) 1188 rtl_set_unplug(tp); 1189 1190 return ret; 1191 } 1192 1193 static inline 1194 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) 1195 { 1196 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); 1197 } 1198 1199 static inline 1200 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) 1201 { 1202 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); 1203 } 1204 1205 static inline 1206 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) 1207 { 1208 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); 1209 } 1210 1211 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) 1212 { 1213 __le32 data; 1214 1215 generic_ocp_read(tp, index, sizeof(data), &data, type); 1216 1217 return __le32_to_cpu(data); 1218 } 1219 1220 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) 1221 { 1222 __le32 tmp = __cpu_to_le32(data); 1223 1224 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); 1225 } 1226 1227 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) 1228 { 1229 u32 data; 1230 __le32 tmp; 1231 u16 byen = BYTE_EN_WORD; 1232 u8 shift = index & 2; 1233 1234 index &= ~3; 1235 byen <<= shift; 1236 1237 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen); 1238 1239 data = __le32_to_cpu(tmp); 1240 data >>= (shift * 8); 1241 data &= 0xffff; 1242 1243 return (u16)data; 1244 } 1245 1246 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) 1247 { 1248 u32 mask = 0xffff; 1249 __le32 tmp; 1250 u16 byen = BYTE_EN_WORD; 1251 u8 shift = index & 2; 1252 1253 data &= mask; 1254 1255 if (index & 2) { 1256 byen <<= shift; 1257 mask <<= (shift * 8); 1258 data <<= (shift * 8); 1259 index &= ~3; 1260 } 1261 1262 tmp = __cpu_to_le32(data); 1263 1264 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); 1265 } 1266 1267 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) 1268 { 1269 u32 data; 1270 __le32 tmp; 1271 u8 shift = index & 3; 1272 1273 index &= ~3; 1274 1275 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); 1276 1277 data = __le32_to_cpu(tmp); 1278 data >>= (shift * 8); 1279 data &= 0xff; 1280 1281 return (u8)data; 1282 } 1283 1284 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) 1285 { 1286 u32 mask = 0xff; 1287 __le32 tmp; 1288 u16 byen = BYTE_EN_BYTE; 1289 u8 shift = index & 3; 1290 1291 data &= mask; 1292 1293 if (index & 3) { 1294 byen <<= shift; 1295 mask <<= (shift * 8); 1296 data <<= (shift * 8); 1297 index &= ~3; 1298 } 1299 1300 tmp = __cpu_to_le32(data); 1301 1302 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); 1303 } 1304 1305 static u16 ocp_reg_read(struct r8152 *tp, u16 addr) 1306 { 1307 u16 ocp_base, ocp_index; 1308 1309 ocp_base = addr & 0xf000; 1310 if (ocp_base != tp->ocp_base) { 1311 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); 1312 tp->ocp_base = ocp_base; 1313 } 1314 1315 ocp_index = (addr & 0x0fff) | 0xb000; 1316 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); 1317 } 1318 1319 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) 1320 { 1321 u16 ocp_base, ocp_index; 1322 1323 ocp_base = addr & 0xf000; 1324 if (ocp_base != tp->ocp_base) { 1325 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); 1326 tp->ocp_base = ocp_base; 1327 } 1328 1329 ocp_index = (addr & 0x0fff) | 0xb000; 1330 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); 1331 } 1332 1333 static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value) 1334 { 1335 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); 1336 } 1337 1338 static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) 1339 { 1340 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); 1341 } 1342 1343 static void sram_write(struct r8152 *tp, u16 addr, u16 data) 1344 { 1345 ocp_reg_write(tp, OCP_SRAM_ADDR, addr); 1346 ocp_reg_write(tp, OCP_SRAM_DATA, data); 1347 } 1348 1349 static u16 sram_read(struct r8152 *tp, u16 addr) 1350 { 1351 ocp_reg_write(tp, OCP_SRAM_ADDR, addr); 1352 return ocp_reg_read(tp, OCP_SRAM_DATA); 1353 } 1354 1355 static int read_mii_word(struct net_device *netdev, int phy_id, int reg) 1356 { 1357 struct r8152 *tp = netdev_priv(netdev); 1358 int ret; 1359 1360 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1361 return -ENODEV; 1362 1363 if (phy_id != R8152_PHY_ID) 1364 return -EINVAL; 1365 1366 ret = r8152_mdio_read(tp, reg); 1367 1368 return ret; 1369 } 1370 1371 static 1372 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) 1373 { 1374 struct r8152 *tp = netdev_priv(netdev); 1375 1376 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1377 return; 1378 1379 if (phy_id != R8152_PHY_ID) 1380 return; 1381 1382 r8152_mdio_write(tp, reg, val); 1383 } 1384 1385 static int 1386 r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags); 1387 1388 static int rtl8152_set_mac_address(struct net_device *netdev, void *p) 1389 { 1390 struct r8152 *tp = netdev_priv(netdev); 1391 struct sockaddr *addr = p; 1392 int ret = -EADDRNOTAVAIL; 1393 1394 if (!is_valid_ether_addr(addr->sa_data)) 1395 goto out1; 1396 1397 ret = usb_autopm_get_interface(tp->intf); 1398 if (ret < 0) 1399 goto out1; 1400 1401 mutex_lock(&tp->control); 1402 1403 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); 1404 1405 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 1406 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data); 1407 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 1408 1409 mutex_unlock(&tp->control); 1410 1411 usb_autopm_put_interface(tp->intf); 1412 out1: 1413 return ret; 1414 } 1415 1416 /* Devices containing proper chips can support a persistent 1417 * host system provided MAC address. 1418 * Examples of this are Dell TB15 and Dell WD15 docks 1419 */ 1420 static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa) 1421 { 1422 acpi_status status; 1423 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 1424 union acpi_object *obj; 1425 int ret = -EINVAL; 1426 u32 ocp_data; 1427 unsigned char buf[6]; 1428 char *mac_obj_name; 1429 acpi_object_type mac_obj_type; 1430 int mac_strlen; 1431 1432 if (test_bit(LENOVO_MACPASSTHRU, &tp->flags)) { 1433 mac_obj_name = "\\MACA"; 1434 mac_obj_type = ACPI_TYPE_STRING; 1435 mac_strlen = 0x16; 1436 } else { 1437 /* test for -AD variant of RTL8153 */ 1438 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 1439 if ((ocp_data & AD_MASK) == 0x1000) { 1440 /* test for MAC address pass-through bit */ 1441 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE); 1442 if ((ocp_data & PASS_THRU_MASK) != 1) { 1443 netif_dbg(tp, probe, tp->netdev, 1444 "No efuse for RTL8153-AD MAC pass through\n"); 1445 return -ENODEV; 1446 } 1447 } else { 1448 /* test for RTL8153-BND and RTL8153-BD */ 1449 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); 1450 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) { 1451 netif_dbg(tp, probe, tp->netdev, 1452 "Invalid variant for MAC pass through\n"); 1453 return -ENODEV; 1454 } 1455 } 1456 1457 mac_obj_name = "\\_SB.AMAC"; 1458 mac_obj_type = ACPI_TYPE_BUFFER; 1459 mac_strlen = 0x17; 1460 } 1461 1462 /* returns _AUXMAC_#AABBCCDDEEFF# */ 1463 status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer); 1464 obj = (union acpi_object *)buffer.pointer; 1465 if (!ACPI_SUCCESS(status)) 1466 return -ENODEV; 1467 if (obj->type != mac_obj_type || obj->string.length != mac_strlen) { 1468 netif_warn(tp, probe, tp->netdev, 1469 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n", 1470 obj->type, obj->string.length); 1471 goto amacout; 1472 } 1473 1474 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 || 1475 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) { 1476 netif_warn(tp, probe, tp->netdev, 1477 "Invalid header when reading pass-thru MAC addr\n"); 1478 goto amacout; 1479 } 1480 ret = hex2bin(buf, obj->string.pointer + 9, 6); 1481 if (!(ret == 0 && is_valid_ether_addr(buf))) { 1482 netif_warn(tp, probe, tp->netdev, 1483 "Invalid MAC for pass-thru MAC addr: %d, %pM\n", 1484 ret, buf); 1485 ret = -EINVAL; 1486 goto amacout; 1487 } 1488 memcpy(sa->sa_data, buf, 6); 1489 netif_info(tp, probe, tp->netdev, 1490 "Using pass-thru MAC addr %pM\n", sa->sa_data); 1491 1492 amacout: 1493 kfree(obj); 1494 return ret; 1495 } 1496 1497 static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa) 1498 { 1499 struct net_device *dev = tp->netdev; 1500 int ret; 1501 1502 sa->sa_family = dev->type; 1503 1504 if (tp->version == RTL_VER_01) { 1505 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data); 1506 } else { 1507 /* if device doesn't support MAC pass through this will 1508 * be expected to be non-zero 1509 */ 1510 ret = vendor_mac_passthru_addr_read(tp, sa); 1511 if (ret < 0) 1512 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data); 1513 } 1514 1515 if (ret < 0) { 1516 netif_err(tp, probe, dev, "Get ether addr fail\n"); 1517 } else if (!is_valid_ether_addr(sa->sa_data)) { 1518 netif_err(tp, probe, dev, "Invalid ether addr %pM\n", 1519 sa->sa_data); 1520 eth_hw_addr_random(dev); 1521 ether_addr_copy(sa->sa_data, dev->dev_addr); 1522 netif_info(tp, probe, dev, "Random ether addr %pM\n", 1523 sa->sa_data); 1524 return 0; 1525 } 1526 1527 return ret; 1528 } 1529 1530 static int set_ethernet_addr(struct r8152 *tp) 1531 { 1532 struct net_device *dev = tp->netdev; 1533 struct sockaddr sa; 1534 int ret; 1535 1536 ret = determine_ethernet_addr(tp, &sa); 1537 if (ret < 0) 1538 return ret; 1539 1540 if (tp->version == RTL_VER_01) 1541 ether_addr_copy(dev->dev_addr, sa.sa_data); 1542 else 1543 ret = rtl8152_set_mac_address(dev, &sa); 1544 1545 return ret; 1546 } 1547 1548 static void read_bulk_callback(struct urb *urb) 1549 { 1550 struct net_device *netdev; 1551 int status = urb->status; 1552 struct rx_agg *agg; 1553 struct r8152 *tp; 1554 unsigned long flags; 1555 1556 agg = urb->context; 1557 if (!agg) 1558 return; 1559 1560 tp = agg->context; 1561 if (!tp) 1562 return; 1563 1564 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1565 return; 1566 1567 if (!test_bit(WORK_ENABLE, &tp->flags)) 1568 return; 1569 1570 netdev = tp->netdev; 1571 1572 /* When link down, the driver would cancel all bulks. */ 1573 /* This avoid the re-submitting bulk */ 1574 if (!netif_carrier_ok(netdev)) 1575 return; 1576 1577 usb_mark_last_busy(tp->udev); 1578 1579 switch (status) { 1580 case 0: 1581 if (urb->actual_length < ETH_ZLEN) 1582 break; 1583 1584 spin_lock_irqsave(&tp->rx_lock, flags); 1585 list_add_tail(&agg->list, &tp->rx_done); 1586 spin_unlock_irqrestore(&tp->rx_lock, flags); 1587 napi_schedule(&tp->napi); 1588 return; 1589 case -ESHUTDOWN: 1590 rtl_set_unplug(tp); 1591 netif_device_detach(tp->netdev); 1592 return; 1593 case -ENOENT: 1594 return; /* the urb is in unlink state */ 1595 case -ETIME: 1596 if (net_ratelimit()) 1597 netdev_warn(netdev, "maybe reset is needed?\n"); 1598 break; 1599 default: 1600 if (net_ratelimit()) 1601 netdev_warn(netdev, "Rx status %d\n", status); 1602 break; 1603 } 1604 1605 r8152_submit_rx(tp, agg, GFP_ATOMIC); 1606 } 1607 1608 static void write_bulk_callback(struct urb *urb) 1609 { 1610 struct net_device_stats *stats; 1611 struct net_device *netdev; 1612 struct tx_agg *agg; 1613 struct r8152 *tp; 1614 unsigned long flags; 1615 int status = urb->status; 1616 1617 agg = urb->context; 1618 if (!agg) 1619 return; 1620 1621 tp = agg->context; 1622 if (!tp) 1623 return; 1624 1625 netdev = tp->netdev; 1626 stats = &netdev->stats; 1627 if (status) { 1628 if (net_ratelimit()) 1629 netdev_warn(netdev, "Tx status %d\n", status); 1630 stats->tx_errors += agg->skb_num; 1631 } else { 1632 stats->tx_packets += agg->skb_num; 1633 stats->tx_bytes += agg->skb_len; 1634 } 1635 1636 spin_lock_irqsave(&tp->tx_lock, flags); 1637 list_add_tail(&agg->list, &tp->tx_free); 1638 spin_unlock_irqrestore(&tp->tx_lock, flags); 1639 1640 usb_autopm_put_interface_async(tp->intf); 1641 1642 if (!netif_carrier_ok(netdev)) 1643 return; 1644 1645 if (!test_bit(WORK_ENABLE, &tp->flags)) 1646 return; 1647 1648 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1649 return; 1650 1651 if (!skb_queue_empty(&tp->tx_queue)) 1652 tasklet_schedule(&tp->tx_tl); 1653 } 1654 1655 static void intr_callback(struct urb *urb) 1656 { 1657 struct r8152 *tp; 1658 __le16 *d; 1659 int status = urb->status; 1660 int res; 1661 1662 tp = urb->context; 1663 if (!tp) 1664 return; 1665 1666 if (!test_bit(WORK_ENABLE, &tp->flags)) 1667 return; 1668 1669 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1670 return; 1671 1672 switch (status) { 1673 case 0: /* success */ 1674 break; 1675 case -ECONNRESET: /* unlink */ 1676 case -ESHUTDOWN: 1677 netif_device_detach(tp->netdev); 1678 /* fall through */ 1679 case -ENOENT: 1680 case -EPROTO: 1681 netif_info(tp, intr, tp->netdev, 1682 "Stop submitting intr, status %d\n", status); 1683 return; 1684 case -EOVERFLOW: 1685 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n"); 1686 goto resubmit; 1687 /* -EPIPE: should clear the halt */ 1688 default: 1689 netif_info(tp, intr, tp->netdev, "intr status %d\n", status); 1690 goto resubmit; 1691 } 1692 1693 d = urb->transfer_buffer; 1694 if (INTR_LINK & __le16_to_cpu(d[0])) { 1695 if (!netif_carrier_ok(tp->netdev)) { 1696 set_bit(RTL8152_LINK_CHG, &tp->flags); 1697 schedule_delayed_work(&tp->schedule, 0); 1698 } 1699 } else { 1700 if (netif_carrier_ok(tp->netdev)) { 1701 netif_stop_queue(tp->netdev); 1702 set_bit(RTL8152_LINK_CHG, &tp->flags); 1703 schedule_delayed_work(&tp->schedule, 0); 1704 } 1705 } 1706 1707 resubmit: 1708 res = usb_submit_urb(urb, GFP_ATOMIC); 1709 if (res == -ENODEV) { 1710 rtl_set_unplug(tp); 1711 netif_device_detach(tp->netdev); 1712 } else if (res) { 1713 netif_err(tp, intr, tp->netdev, 1714 "can't resubmit intr, status %d\n", res); 1715 } 1716 } 1717 1718 static inline void *rx_agg_align(void *data) 1719 { 1720 return (void *)ALIGN((uintptr_t)data, RX_ALIGN); 1721 } 1722 1723 static inline void *tx_agg_align(void *data) 1724 { 1725 return (void *)ALIGN((uintptr_t)data, TX_ALIGN); 1726 } 1727 1728 static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg) 1729 { 1730 list_del(&agg->info_list); 1731 1732 usb_free_urb(agg->urb); 1733 put_page(agg->page); 1734 kfree(agg); 1735 1736 atomic_dec(&tp->rx_count); 1737 } 1738 1739 static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags) 1740 { 1741 struct net_device *netdev = tp->netdev; 1742 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; 1743 unsigned int order = get_order(tp->rx_buf_sz); 1744 struct rx_agg *rx_agg; 1745 unsigned long flags; 1746 1747 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node); 1748 if (!rx_agg) 1749 return NULL; 1750 1751 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order); 1752 if (!rx_agg->page) 1753 goto free_rx; 1754 1755 rx_agg->buffer = page_address(rx_agg->page); 1756 1757 rx_agg->urb = usb_alloc_urb(0, mflags); 1758 if (!rx_agg->urb) 1759 goto free_buf; 1760 1761 rx_agg->context = tp; 1762 1763 INIT_LIST_HEAD(&rx_agg->list); 1764 INIT_LIST_HEAD(&rx_agg->info_list); 1765 spin_lock_irqsave(&tp->rx_lock, flags); 1766 list_add_tail(&rx_agg->info_list, &tp->rx_info); 1767 spin_unlock_irqrestore(&tp->rx_lock, flags); 1768 1769 atomic_inc(&tp->rx_count); 1770 1771 return rx_agg; 1772 1773 free_buf: 1774 __free_pages(rx_agg->page, order); 1775 free_rx: 1776 kfree(rx_agg); 1777 return NULL; 1778 } 1779 1780 static void free_all_mem(struct r8152 *tp) 1781 { 1782 struct rx_agg *agg, *agg_next; 1783 unsigned long flags; 1784 int i; 1785 1786 spin_lock_irqsave(&tp->rx_lock, flags); 1787 1788 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list) 1789 free_rx_agg(tp, agg); 1790 1791 spin_unlock_irqrestore(&tp->rx_lock, flags); 1792 1793 WARN_ON(atomic_read(&tp->rx_count)); 1794 1795 for (i = 0; i < RTL8152_MAX_TX; i++) { 1796 usb_free_urb(tp->tx_info[i].urb); 1797 tp->tx_info[i].urb = NULL; 1798 1799 kfree(tp->tx_info[i].buffer); 1800 tp->tx_info[i].buffer = NULL; 1801 tp->tx_info[i].head = NULL; 1802 } 1803 1804 usb_free_urb(tp->intr_urb); 1805 tp->intr_urb = NULL; 1806 1807 kfree(tp->intr_buff); 1808 tp->intr_buff = NULL; 1809 } 1810 1811 static int alloc_all_mem(struct r8152 *tp) 1812 { 1813 struct net_device *netdev = tp->netdev; 1814 struct usb_interface *intf = tp->intf; 1815 struct usb_host_interface *alt = intf->cur_altsetting; 1816 struct usb_host_endpoint *ep_intr = alt->endpoint + 2; 1817 int node, i; 1818 1819 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; 1820 1821 spin_lock_init(&tp->rx_lock); 1822 spin_lock_init(&tp->tx_lock); 1823 INIT_LIST_HEAD(&tp->rx_info); 1824 INIT_LIST_HEAD(&tp->tx_free); 1825 INIT_LIST_HEAD(&tp->rx_done); 1826 skb_queue_head_init(&tp->tx_queue); 1827 skb_queue_head_init(&tp->rx_queue); 1828 atomic_set(&tp->rx_count, 0); 1829 1830 for (i = 0; i < RTL8152_MAX_RX; i++) { 1831 if (!alloc_rx_agg(tp, GFP_KERNEL)) 1832 goto err1; 1833 } 1834 1835 for (i = 0; i < RTL8152_MAX_TX; i++) { 1836 struct urb *urb; 1837 u8 *buf; 1838 1839 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node); 1840 if (!buf) 1841 goto err1; 1842 1843 if (buf != tx_agg_align(buf)) { 1844 kfree(buf); 1845 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL, 1846 node); 1847 if (!buf) 1848 goto err1; 1849 } 1850 1851 urb = usb_alloc_urb(0, GFP_KERNEL); 1852 if (!urb) { 1853 kfree(buf); 1854 goto err1; 1855 } 1856 1857 INIT_LIST_HEAD(&tp->tx_info[i].list); 1858 tp->tx_info[i].context = tp; 1859 tp->tx_info[i].urb = urb; 1860 tp->tx_info[i].buffer = buf; 1861 tp->tx_info[i].head = tx_agg_align(buf); 1862 1863 list_add_tail(&tp->tx_info[i].list, &tp->tx_free); 1864 } 1865 1866 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL); 1867 if (!tp->intr_urb) 1868 goto err1; 1869 1870 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); 1871 if (!tp->intr_buff) 1872 goto err1; 1873 1874 tp->intr_interval = (int)ep_intr->desc.bInterval; 1875 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3), 1876 tp->intr_buff, INTBUFSIZE, intr_callback, 1877 tp, tp->intr_interval); 1878 1879 return 0; 1880 1881 err1: 1882 free_all_mem(tp); 1883 return -ENOMEM; 1884 } 1885 1886 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp) 1887 { 1888 struct tx_agg *agg = NULL; 1889 unsigned long flags; 1890 1891 if (list_empty(&tp->tx_free)) 1892 return NULL; 1893 1894 spin_lock_irqsave(&tp->tx_lock, flags); 1895 if (!list_empty(&tp->tx_free)) { 1896 struct list_head *cursor; 1897 1898 cursor = tp->tx_free.next; 1899 list_del_init(cursor); 1900 agg = list_entry(cursor, struct tx_agg, list); 1901 } 1902 spin_unlock_irqrestore(&tp->tx_lock, flags); 1903 1904 return agg; 1905 } 1906 1907 /* r8152_csum_workaround() 1908 * The hw limits the value of the transport offset. When the offset is out of 1909 * range, calculate the checksum by sw. 1910 */ 1911 static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb, 1912 struct sk_buff_head *list) 1913 { 1914 if (skb_shinfo(skb)->gso_size) { 1915 netdev_features_t features = tp->netdev->features; 1916 struct sk_buff_head seg_list; 1917 struct sk_buff *segs, *nskb; 1918 1919 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6); 1920 segs = skb_gso_segment(skb, features); 1921 if (IS_ERR(segs) || !segs) 1922 goto drop; 1923 1924 __skb_queue_head_init(&seg_list); 1925 1926 do { 1927 nskb = segs; 1928 segs = segs->next; 1929 nskb->next = NULL; 1930 __skb_queue_tail(&seg_list, nskb); 1931 } while (segs); 1932 1933 skb_queue_splice(&seg_list, list); 1934 dev_kfree_skb(skb); 1935 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 1936 if (skb_checksum_help(skb) < 0) 1937 goto drop; 1938 1939 __skb_queue_head(list, skb); 1940 } else { 1941 struct net_device_stats *stats; 1942 1943 drop: 1944 stats = &tp->netdev->stats; 1945 stats->tx_dropped++; 1946 dev_kfree_skb(skb); 1947 } 1948 } 1949 1950 /* msdn_giant_send_check() 1951 * According to the document of microsoft, the TCP Pseudo Header excludes the 1952 * packet length for IPv6 TCP large packets. 1953 */ 1954 static int msdn_giant_send_check(struct sk_buff *skb) 1955 { 1956 const struct ipv6hdr *ipv6h; 1957 struct tcphdr *th; 1958 int ret; 1959 1960 ret = skb_cow_head(skb, 0); 1961 if (ret) 1962 return ret; 1963 1964 ipv6h = ipv6_hdr(skb); 1965 th = tcp_hdr(skb); 1966 1967 th->check = 0; 1968 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0); 1969 1970 return ret; 1971 } 1972 1973 static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb) 1974 { 1975 if (skb_vlan_tag_present(skb)) { 1976 u32 opts2; 1977 1978 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb)); 1979 desc->opts2 |= cpu_to_le32(opts2); 1980 } 1981 } 1982 1983 static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb) 1984 { 1985 u32 opts2 = le32_to_cpu(desc->opts2); 1986 1987 if (opts2 & RX_VLAN_TAG) 1988 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 1989 swab16(opts2 & 0xffff)); 1990 } 1991 1992 static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, 1993 struct sk_buff *skb, u32 len, u32 transport_offset) 1994 { 1995 u32 mss = skb_shinfo(skb)->gso_size; 1996 u32 opts1, opts2 = 0; 1997 int ret = TX_CSUM_SUCCESS; 1998 1999 WARN_ON_ONCE(len > TX_LEN_MAX); 2000 2001 opts1 = len | TX_FS | TX_LS; 2002 2003 if (mss) { 2004 if (transport_offset > GTTCPHO_MAX) { 2005 netif_warn(tp, tx_err, tp->netdev, 2006 "Invalid transport offset 0x%x for TSO\n", 2007 transport_offset); 2008 ret = TX_CSUM_TSO; 2009 goto unavailable; 2010 } 2011 2012 switch (vlan_get_protocol(skb)) { 2013 case htons(ETH_P_IP): 2014 opts1 |= GTSENDV4; 2015 break; 2016 2017 case htons(ETH_P_IPV6): 2018 if (msdn_giant_send_check(skb)) { 2019 ret = TX_CSUM_TSO; 2020 goto unavailable; 2021 } 2022 opts1 |= GTSENDV6; 2023 break; 2024 2025 default: 2026 WARN_ON_ONCE(1); 2027 break; 2028 } 2029 2030 opts1 |= transport_offset << GTTCPHO_SHIFT; 2031 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT; 2032 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 2033 u8 ip_protocol; 2034 2035 if (transport_offset > TCPHO_MAX) { 2036 netif_warn(tp, tx_err, tp->netdev, 2037 "Invalid transport offset 0x%x\n", 2038 transport_offset); 2039 ret = TX_CSUM_NONE; 2040 goto unavailable; 2041 } 2042 2043 switch (vlan_get_protocol(skb)) { 2044 case htons(ETH_P_IP): 2045 opts2 |= IPV4_CS; 2046 ip_protocol = ip_hdr(skb)->protocol; 2047 break; 2048 2049 case htons(ETH_P_IPV6): 2050 opts2 |= IPV6_CS; 2051 ip_protocol = ipv6_hdr(skb)->nexthdr; 2052 break; 2053 2054 default: 2055 ip_protocol = IPPROTO_RAW; 2056 break; 2057 } 2058 2059 if (ip_protocol == IPPROTO_TCP) 2060 opts2 |= TCP_CS; 2061 else if (ip_protocol == IPPROTO_UDP) 2062 opts2 |= UDP_CS; 2063 else 2064 WARN_ON_ONCE(1); 2065 2066 opts2 |= transport_offset << TCPHO_SHIFT; 2067 } 2068 2069 desc->opts2 = cpu_to_le32(opts2); 2070 desc->opts1 = cpu_to_le32(opts1); 2071 2072 unavailable: 2073 return ret; 2074 } 2075 2076 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg) 2077 { 2078 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; 2079 int remain, ret; 2080 u8 *tx_data; 2081 2082 __skb_queue_head_init(&skb_head); 2083 spin_lock(&tx_queue->lock); 2084 skb_queue_splice_init(tx_queue, &skb_head); 2085 spin_unlock(&tx_queue->lock); 2086 2087 tx_data = agg->head; 2088 agg->skb_num = 0; 2089 agg->skb_len = 0; 2090 remain = agg_buf_sz; 2091 2092 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) { 2093 struct tx_desc *tx_desc; 2094 struct sk_buff *skb; 2095 unsigned int len; 2096 u32 offset; 2097 2098 skb = __skb_dequeue(&skb_head); 2099 if (!skb) 2100 break; 2101 2102 len = skb->len + sizeof(*tx_desc); 2103 2104 if (len > remain) { 2105 __skb_queue_head(&skb_head, skb); 2106 break; 2107 } 2108 2109 tx_data = tx_agg_align(tx_data); 2110 tx_desc = (struct tx_desc *)tx_data; 2111 2112 offset = (u32)skb_transport_offset(skb); 2113 2114 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) { 2115 r8152_csum_workaround(tp, skb, &skb_head); 2116 continue; 2117 } 2118 2119 rtl_tx_vlan_tag(tx_desc, skb); 2120 2121 tx_data += sizeof(*tx_desc); 2122 2123 len = skb->len; 2124 if (skb_copy_bits(skb, 0, tx_data, len) < 0) { 2125 struct net_device_stats *stats = &tp->netdev->stats; 2126 2127 stats->tx_dropped++; 2128 dev_kfree_skb_any(skb); 2129 tx_data -= sizeof(*tx_desc); 2130 continue; 2131 } 2132 2133 tx_data += len; 2134 agg->skb_len += len; 2135 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1; 2136 2137 dev_kfree_skb_any(skb); 2138 2139 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head); 2140 2141 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags)) 2142 break; 2143 } 2144 2145 if (!skb_queue_empty(&skb_head)) { 2146 spin_lock(&tx_queue->lock); 2147 skb_queue_splice(&skb_head, tx_queue); 2148 spin_unlock(&tx_queue->lock); 2149 } 2150 2151 netif_tx_lock(tp->netdev); 2152 2153 if (netif_queue_stopped(tp->netdev) && 2154 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) 2155 netif_wake_queue(tp->netdev); 2156 2157 netif_tx_unlock(tp->netdev); 2158 2159 ret = usb_autopm_get_interface_async(tp->intf); 2160 if (ret < 0) 2161 goto out_tx_fill; 2162 2163 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2), 2164 agg->head, (int)(tx_data - (u8 *)agg->head), 2165 (usb_complete_t)write_bulk_callback, agg); 2166 2167 ret = usb_submit_urb(agg->urb, GFP_ATOMIC); 2168 if (ret < 0) 2169 usb_autopm_put_interface_async(tp->intf); 2170 2171 out_tx_fill: 2172 return ret; 2173 } 2174 2175 static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc) 2176 { 2177 u8 checksum = CHECKSUM_NONE; 2178 u32 opts2, opts3; 2179 2180 if (!(tp->netdev->features & NETIF_F_RXCSUM)) 2181 goto return_result; 2182 2183 opts2 = le32_to_cpu(rx_desc->opts2); 2184 opts3 = le32_to_cpu(rx_desc->opts3); 2185 2186 if (opts2 & RD_IPV4_CS) { 2187 if (opts3 & IPF) 2188 checksum = CHECKSUM_NONE; 2189 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) 2190 checksum = CHECKSUM_UNNECESSARY; 2191 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) 2192 checksum = CHECKSUM_UNNECESSARY; 2193 } else if (opts2 & RD_IPV6_CS) { 2194 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) 2195 checksum = CHECKSUM_UNNECESSARY; 2196 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) 2197 checksum = CHECKSUM_UNNECESSARY; 2198 } 2199 2200 return_result: 2201 return checksum; 2202 } 2203 2204 static inline bool rx_count_exceed(struct r8152 *tp) 2205 { 2206 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX; 2207 } 2208 2209 static inline int agg_offset(struct rx_agg *agg, void *addr) 2210 { 2211 return (int)(addr - agg->buffer); 2212 } 2213 2214 static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags) 2215 { 2216 struct rx_agg *agg, *agg_next, *agg_free = NULL; 2217 unsigned long flags; 2218 2219 spin_lock_irqsave(&tp->rx_lock, flags); 2220 2221 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) { 2222 if (page_count(agg->page) == 1) { 2223 if (!agg_free) { 2224 list_del_init(&agg->list); 2225 agg_free = agg; 2226 continue; 2227 } 2228 if (rx_count_exceed(tp)) { 2229 list_del_init(&agg->list); 2230 free_rx_agg(tp, agg); 2231 } 2232 break; 2233 } 2234 } 2235 2236 spin_unlock_irqrestore(&tp->rx_lock, flags); 2237 2238 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending) 2239 agg_free = alloc_rx_agg(tp, mflags); 2240 2241 return agg_free; 2242 } 2243 2244 static int rx_bottom(struct r8152 *tp, int budget) 2245 { 2246 unsigned long flags; 2247 struct list_head *cursor, *next, rx_queue; 2248 int ret = 0, work_done = 0; 2249 struct napi_struct *napi = &tp->napi; 2250 2251 if (!skb_queue_empty(&tp->rx_queue)) { 2252 while (work_done < budget) { 2253 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue); 2254 struct net_device *netdev = tp->netdev; 2255 struct net_device_stats *stats = &netdev->stats; 2256 unsigned int pkt_len; 2257 2258 if (!skb) 2259 break; 2260 2261 pkt_len = skb->len; 2262 napi_gro_receive(napi, skb); 2263 work_done++; 2264 stats->rx_packets++; 2265 stats->rx_bytes += pkt_len; 2266 } 2267 } 2268 2269 if (list_empty(&tp->rx_done)) 2270 goto out1; 2271 2272 INIT_LIST_HEAD(&rx_queue); 2273 spin_lock_irqsave(&tp->rx_lock, flags); 2274 list_splice_init(&tp->rx_done, &rx_queue); 2275 spin_unlock_irqrestore(&tp->rx_lock, flags); 2276 2277 list_for_each_safe(cursor, next, &rx_queue) { 2278 struct rx_desc *rx_desc; 2279 struct rx_agg *agg, *agg_free; 2280 int len_used = 0; 2281 struct urb *urb; 2282 u8 *rx_data; 2283 2284 list_del_init(cursor); 2285 2286 agg = list_entry(cursor, struct rx_agg, list); 2287 urb = agg->urb; 2288 if (urb->actual_length < ETH_ZLEN) 2289 goto submit; 2290 2291 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC); 2292 2293 rx_desc = agg->buffer; 2294 rx_data = agg->buffer; 2295 len_used += sizeof(struct rx_desc); 2296 2297 while (urb->actual_length > len_used) { 2298 struct net_device *netdev = tp->netdev; 2299 struct net_device_stats *stats = &netdev->stats; 2300 unsigned int pkt_len, rx_frag_head_sz; 2301 struct sk_buff *skb; 2302 2303 /* limite the skb numbers for rx_queue */ 2304 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000)) 2305 break; 2306 2307 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; 2308 if (pkt_len < ETH_ZLEN) 2309 break; 2310 2311 len_used += pkt_len; 2312 if (urb->actual_length < len_used) 2313 break; 2314 2315 pkt_len -= ETH_FCS_LEN; 2316 rx_data += sizeof(struct rx_desc); 2317 2318 if (!agg_free || tp->rx_copybreak > pkt_len) 2319 rx_frag_head_sz = pkt_len; 2320 else 2321 rx_frag_head_sz = tp->rx_copybreak; 2322 2323 skb = napi_alloc_skb(napi, rx_frag_head_sz); 2324 if (!skb) { 2325 stats->rx_dropped++; 2326 goto find_next_rx; 2327 } 2328 2329 skb->ip_summed = r8152_rx_csum(tp, rx_desc); 2330 memcpy(skb->data, rx_data, rx_frag_head_sz); 2331 skb_put(skb, rx_frag_head_sz); 2332 pkt_len -= rx_frag_head_sz; 2333 rx_data += rx_frag_head_sz; 2334 if (pkt_len) { 2335 skb_add_rx_frag(skb, 0, agg->page, 2336 agg_offset(agg, rx_data), 2337 pkt_len, 2338 SKB_DATA_ALIGN(pkt_len)); 2339 get_page(agg->page); 2340 } 2341 2342 skb->protocol = eth_type_trans(skb, netdev); 2343 rtl_rx_vlan_tag(rx_desc, skb); 2344 if (work_done < budget) { 2345 work_done++; 2346 stats->rx_packets++; 2347 stats->rx_bytes += skb->len; 2348 napi_gro_receive(napi, skb); 2349 } else { 2350 __skb_queue_tail(&tp->rx_queue, skb); 2351 } 2352 2353 find_next_rx: 2354 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN); 2355 rx_desc = (struct rx_desc *)rx_data; 2356 len_used = agg_offset(agg, rx_data); 2357 len_used += sizeof(struct rx_desc); 2358 } 2359 2360 WARN_ON(!agg_free && page_count(agg->page) > 1); 2361 2362 if (agg_free) { 2363 spin_lock_irqsave(&tp->rx_lock, flags); 2364 if (page_count(agg->page) == 1) { 2365 list_add(&agg_free->list, &tp->rx_used); 2366 } else { 2367 list_add_tail(&agg->list, &tp->rx_used); 2368 agg = agg_free; 2369 urb = agg->urb; 2370 } 2371 spin_unlock_irqrestore(&tp->rx_lock, flags); 2372 } 2373 2374 submit: 2375 if (!ret) { 2376 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); 2377 } else { 2378 urb->actual_length = 0; 2379 list_add_tail(&agg->list, next); 2380 } 2381 } 2382 2383 if (!list_empty(&rx_queue)) { 2384 spin_lock_irqsave(&tp->rx_lock, flags); 2385 list_splice_tail(&rx_queue, &tp->rx_done); 2386 spin_unlock_irqrestore(&tp->rx_lock, flags); 2387 } 2388 2389 out1: 2390 return work_done; 2391 } 2392 2393 static void tx_bottom(struct r8152 *tp) 2394 { 2395 int res; 2396 2397 do { 2398 struct net_device *netdev = tp->netdev; 2399 struct tx_agg *agg; 2400 2401 if (skb_queue_empty(&tp->tx_queue)) 2402 break; 2403 2404 agg = r8152_get_tx_agg(tp); 2405 if (!agg) 2406 break; 2407 2408 res = r8152_tx_agg_fill(tp, agg); 2409 if (!res) 2410 continue; 2411 2412 if (res == -ENODEV) { 2413 rtl_set_unplug(tp); 2414 netif_device_detach(netdev); 2415 } else { 2416 struct net_device_stats *stats = &netdev->stats; 2417 unsigned long flags; 2418 2419 netif_warn(tp, tx_err, netdev, 2420 "failed tx_urb %d\n", res); 2421 stats->tx_dropped += agg->skb_num; 2422 2423 spin_lock_irqsave(&tp->tx_lock, flags); 2424 list_add_tail(&agg->list, &tp->tx_free); 2425 spin_unlock_irqrestore(&tp->tx_lock, flags); 2426 } 2427 } while (res == 0); 2428 } 2429 2430 static void bottom_half(unsigned long data) 2431 { 2432 struct r8152 *tp; 2433 2434 tp = (struct r8152 *)data; 2435 2436 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 2437 return; 2438 2439 if (!test_bit(WORK_ENABLE, &tp->flags)) 2440 return; 2441 2442 /* When link down, the driver would cancel all bulks. */ 2443 /* This avoid the re-submitting bulk */ 2444 if (!netif_carrier_ok(tp->netdev)) 2445 return; 2446 2447 clear_bit(SCHEDULE_TASKLET, &tp->flags); 2448 2449 tx_bottom(tp); 2450 } 2451 2452 static int r8152_poll(struct napi_struct *napi, int budget) 2453 { 2454 struct r8152 *tp = container_of(napi, struct r8152, napi); 2455 int work_done; 2456 2457 work_done = rx_bottom(tp, budget); 2458 2459 if (work_done < budget) { 2460 if (!napi_complete_done(napi, work_done)) 2461 goto out; 2462 if (!list_empty(&tp->rx_done)) 2463 napi_schedule(napi); 2464 } 2465 2466 out: 2467 return work_done; 2468 } 2469 2470 static 2471 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) 2472 { 2473 int ret; 2474 2475 /* The rx would be stopped, so skip submitting */ 2476 if (test_bit(RTL8152_UNPLUG, &tp->flags) || 2477 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev)) 2478 return 0; 2479 2480 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1), 2481 agg->buffer, tp->rx_buf_sz, 2482 (usb_complete_t)read_bulk_callback, agg); 2483 2484 ret = usb_submit_urb(agg->urb, mem_flags); 2485 if (ret == -ENODEV) { 2486 rtl_set_unplug(tp); 2487 netif_device_detach(tp->netdev); 2488 } else if (ret) { 2489 struct urb *urb = agg->urb; 2490 unsigned long flags; 2491 2492 urb->actual_length = 0; 2493 spin_lock_irqsave(&tp->rx_lock, flags); 2494 list_add_tail(&agg->list, &tp->rx_done); 2495 spin_unlock_irqrestore(&tp->rx_lock, flags); 2496 2497 netif_err(tp, rx_err, tp->netdev, 2498 "Couldn't submit rx[%p], ret = %d\n", agg, ret); 2499 2500 napi_schedule(&tp->napi); 2501 } 2502 2503 return ret; 2504 } 2505 2506 static void rtl_drop_queued_tx(struct r8152 *tp) 2507 { 2508 struct net_device_stats *stats = &tp->netdev->stats; 2509 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; 2510 struct sk_buff *skb; 2511 2512 if (skb_queue_empty(tx_queue)) 2513 return; 2514 2515 __skb_queue_head_init(&skb_head); 2516 spin_lock_bh(&tx_queue->lock); 2517 skb_queue_splice_init(tx_queue, &skb_head); 2518 spin_unlock_bh(&tx_queue->lock); 2519 2520 while ((skb = __skb_dequeue(&skb_head))) { 2521 dev_kfree_skb(skb); 2522 stats->tx_dropped++; 2523 } 2524 } 2525 2526 static void rtl8152_tx_timeout(struct net_device *netdev) 2527 { 2528 struct r8152 *tp = netdev_priv(netdev); 2529 2530 netif_warn(tp, tx_err, netdev, "Tx timeout\n"); 2531 2532 usb_queue_reset_device(tp->intf); 2533 } 2534 2535 static void rtl8152_set_rx_mode(struct net_device *netdev) 2536 { 2537 struct r8152 *tp = netdev_priv(netdev); 2538 2539 if (netif_carrier_ok(netdev)) { 2540 set_bit(RTL8152_SET_RX_MODE, &tp->flags); 2541 schedule_delayed_work(&tp->schedule, 0); 2542 } 2543 } 2544 2545 static void _rtl8152_set_rx_mode(struct net_device *netdev) 2546 { 2547 struct r8152 *tp = netdev_priv(netdev); 2548 u32 mc_filter[2]; /* Multicast hash filter */ 2549 __le32 tmp[2]; 2550 u32 ocp_data; 2551 2552 netif_stop_queue(netdev); 2553 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 2554 ocp_data &= ~RCR_ACPT_ALL; 2555 ocp_data |= RCR_AB | RCR_APM; 2556 2557 if (netdev->flags & IFF_PROMISC) { 2558 /* Unconditionally log net taps. */ 2559 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); 2560 ocp_data |= RCR_AM | RCR_AAP; 2561 mc_filter[1] = 0xffffffff; 2562 mc_filter[0] = 0xffffffff; 2563 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) || 2564 (netdev->flags & IFF_ALLMULTI)) { 2565 /* Too many to filter perfectly -- accept all multicasts. */ 2566 ocp_data |= RCR_AM; 2567 mc_filter[1] = 0xffffffff; 2568 mc_filter[0] = 0xffffffff; 2569 } else { 2570 struct netdev_hw_addr *ha; 2571 2572 mc_filter[1] = 0; 2573 mc_filter[0] = 0; 2574 netdev_for_each_mc_addr(ha, netdev) { 2575 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; 2576 2577 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); 2578 ocp_data |= RCR_AM; 2579 } 2580 } 2581 2582 tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); 2583 tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); 2584 2585 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); 2586 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 2587 netif_wake_queue(netdev); 2588 } 2589 2590 static netdev_features_t 2591 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev, 2592 netdev_features_t features) 2593 { 2594 u32 mss = skb_shinfo(skb)->gso_size; 2595 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX; 2596 int offset = skb_transport_offset(skb); 2597 2598 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset) 2599 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 2600 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz) 2601 features &= ~NETIF_F_GSO_MASK; 2602 2603 return features; 2604 } 2605 2606 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, 2607 struct net_device *netdev) 2608 { 2609 struct r8152 *tp = netdev_priv(netdev); 2610 2611 skb_tx_timestamp(skb); 2612 2613 skb_queue_tail(&tp->tx_queue, skb); 2614 2615 if (!list_empty(&tp->tx_free)) { 2616 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { 2617 set_bit(SCHEDULE_TASKLET, &tp->flags); 2618 schedule_delayed_work(&tp->schedule, 0); 2619 } else { 2620 usb_mark_last_busy(tp->udev); 2621 tasklet_schedule(&tp->tx_tl); 2622 } 2623 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) { 2624 netif_stop_queue(netdev); 2625 } 2626 2627 return NETDEV_TX_OK; 2628 } 2629 2630 static void r8152b_reset_packet_filter(struct r8152 *tp) 2631 { 2632 u32 ocp_data; 2633 2634 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); 2635 ocp_data &= ~FMC_FCR_MCU_EN; 2636 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); 2637 ocp_data |= FMC_FCR_MCU_EN; 2638 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); 2639 } 2640 2641 static void rtl8152_nic_reset(struct r8152 *tp) 2642 { 2643 int i; 2644 2645 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); 2646 2647 for (i = 0; i < 1000; i++) { 2648 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) 2649 break; 2650 usleep_range(100, 400); 2651 } 2652 } 2653 2654 static void set_tx_qlen(struct r8152 *tp) 2655 { 2656 struct net_device *netdev = tp->netdev; 2657 2658 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN + 2659 sizeof(struct tx_desc)); 2660 } 2661 2662 static inline u8 rtl8152_get_speed(struct r8152 *tp) 2663 { 2664 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); 2665 } 2666 2667 static void rtl_set_eee_plus(struct r8152 *tp) 2668 { 2669 u32 ocp_data; 2670 u8 speed; 2671 2672 speed = rtl8152_get_speed(tp); 2673 if (speed & _10bps) { 2674 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); 2675 ocp_data |= EEEP_CR_EEEP_TX; 2676 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); 2677 } else { 2678 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); 2679 ocp_data &= ~EEEP_CR_EEEP_TX; 2680 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); 2681 } 2682 } 2683 2684 static void rxdy_gated_en(struct r8152 *tp, bool enable) 2685 { 2686 u32 ocp_data; 2687 2688 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); 2689 if (enable) 2690 ocp_data |= RXDY_GATED_EN; 2691 else 2692 ocp_data &= ~RXDY_GATED_EN; 2693 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); 2694 } 2695 2696 static int rtl_start_rx(struct r8152 *tp) 2697 { 2698 struct rx_agg *agg, *agg_next; 2699 struct list_head tmp_list; 2700 unsigned long flags; 2701 int ret = 0, i = 0; 2702 2703 INIT_LIST_HEAD(&tmp_list); 2704 2705 spin_lock_irqsave(&tp->rx_lock, flags); 2706 2707 INIT_LIST_HEAD(&tp->rx_done); 2708 INIT_LIST_HEAD(&tp->rx_used); 2709 2710 list_splice_init(&tp->rx_info, &tmp_list); 2711 2712 spin_unlock_irqrestore(&tp->rx_lock, flags); 2713 2714 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { 2715 INIT_LIST_HEAD(&agg->list); 2716 2717 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */ 2718 if (++i > RTL8152_MAX_RX) { 2719 spin_lock_irqsave(&tp->rx_lock, flags); 2720 list_add_tail(&agg->list, &tp->rx_used); 2721 spin_unlock_irqrestore(&tp->rx_lock, flags); 2722 } else if (unlikely(ret < 0)) { 2723 spin_lock_irqsave(&tp->rx_lock, flags); 2724 list_add_tail(&agg->list, &tp->rx_done); 2725 spin_unlock_irqrestore(&tp->rx_lock, flags); 2726 } else { 2727 ret = r8152_submit_rx(tp, agg, GFP_KERNEL); 2728 } 2729 } 2730 2731 spin_lock_irqsave(&tp->rx_lock, flags); 2732 WARN_ON(!list_empty(&tp->rx_info)); 2733 list_splice(&tmp_list, &tp->rx_info); 2734 spin_unlock_irqrestore(&tp->rx_lock, flags); 2735 2736 return ret; 2737 } 2738 2739 static int rtl_stop_rx(struct r8152 *tp) 2740 { 2741 struct rx_agg *agg, *agg_next; 2742 struct list_head tmp_list; 2743 unsigned long flags; 2744 2745 INIT_LIST_HEAD(&tmp_list); 2746 2747 /* The usb_kill_urb() couldn't be used in atomic. 2748 * Therefore, move the list of rx_info to a tmp one. 2749 * Then, list_for_each_entry_safe could be used without 2750 * spin lock. 2751 */ 2752 2753 spin_lock_irqsave(&tp->rx_lock, flags); 2754 list_splice_init(&tp->rx_info, &tmp_list); 2755 spin_unlock_irqrestore(&tp->rx_lock, flags); 2756 2757 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { 2758 /* At least RTL8152_MAX_RX rx_agg have the page_count being 2759 * equal to 1, so the other ones could be freed safely. 2760 */ 2761 if (page_count(agg->page) > 1) 2762 free_rx_agg(tp, agg); 2763 else 2764 usb_kill_urb(agg->urb); 2765 } 2766 2767 /* Move back the list of temp to the rx_info */ 2768 spin_lock_irqsave(&tp->rx_lock, flags); 2769 WARN_ON(!list_empty(&tp->rx_info)); 2770 list_splice(&tmp_list, &tp->rx_info); 2771 spin_unlock_irqrestore(&tp->rx_lock, flags); 2772 2773 while (!skb_queue_empty(&tp->rx_queue)) 2774 dev_kfree_skb(__skb_dequeue(&tp->rx_queue)); 2775 2776 return 0; 2777 } 2778 2779 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp) 2780 { 2781 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN, 2782 OWN_UPDATE | OWN_CLEAR); 2783 } 2784 2785 static int rtl_enable(struct r8152 *tp) 2786 { 2787 u32 ocp_data; 2788 2789 r8152b_reset_packet_filter(tp); 2790 2791 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); 2792 ocp_data |= CR_RE | CR_TE; 2793 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); 2794 2795 switch (tp->version) { 2796 case RTL_VER_08: 2797 case RTL_VER_09: 2798 r8153b_rx_agg_chg_indicate(tp); 2799 break; 2800 default: 2801 break; 2802 } 2803 2804 rxdy_gated_en(tp, false); 2805 2806 return 0; 2807 } 2808 2809 static int rtl8152_enable(struct r8152 *tp) 2810 { 2811 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 2812 return -ENODEV; 2813 2814 set_tx_qlen(tp); 2815 rtl_set_eee_plus(tp); 2816 2817 return rtl_enable(tp); 2818 } 2819 2820 static void r8153_set_rx_early_timeout(struct r8152 *tp) 2821 { 2822 u32 ocp_data = tp->coalesce / 8; 2823 2824 switch (tp->version) { 2825 case RTL_VER_03: 2826 case RTL_VER_04: 2827 case RTL_VER_05: 2828 case RTL_VER_06: 2829 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 2830 ocp_data); 2831 break; 2832 2833 case RTL_VER_08: 2834 case RTL_VER_09: 2835 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout 2836 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns. 2837 */ 2838 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 2839 128 / 8); 2840 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, 2841 ocp_data); 2842 break; 2843 2844 default: 2845 break; 2846 } 2847 } 2848 2849 static void r8153_set_rx_early_size(struct r8152 *tp) 2850 { 2851 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu); 2852 2853 switch (tp->version) { 2854 case RTL_VER_03: 2855 case RTL_VER_04: 2856 case RTL_VER_05: 2857 case RTL_VER_06: 2858 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 2859 ocp_data / 4); 2860 break; 2861 case RTL_VER_08: 2862 case RTL_VER_09: 2863 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 2864 ocp_data / 8); 2865 break; 2866 default: 2867 WARN_ON_ONCE(1); 2868 break; 2869 } 2870 } 2871 2872 static int rtl8153_enable(struct r8152 *tp) 2873 { 2874 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 2875 return -ENODEV; 2876 2877 set_tx_qlen(tp); 2878 rtl_set_eee_plus(tp); 2879 r8153_set_rx_early_timeout(tp); 2880 r8153_set_rx_early_size(tp); 2881 2882 if (tp->version == RTL_VER_09) { 2883 u32 ocp_data; 2884 2885 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 2886 ocp_data &= ~FC_PATCH_TASK; 2887 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 2888 usleep_range(1000, 2000); 2889 ocp_data |= FC_PATCH_TASK; 2890 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 2891 } 2892 2893 return rtl_enable(tp); 2894 } 2895 2896 static void rtl_disable(struct r8152 *tp) 2897 { 2898 u32 ocp_data; 2899 int i; 2900 2901 if (test_bit(RTL8152_UNPLUG, &tp->flags)) { 2902 rtl_drop_queued_tx(tp); 2903 return; 2904 } 2905 2906 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 2907 ocp_data &= ~RCR_ACPT_ALL; 2908 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 2909 2910 rtl_drop_queued_tx(tp); 2911 2912 for (i = 0; i < RTL8152_MAX_TX; i++) 2913 usb_kill_urb(tp->tx_info[i].urb); 2914 2915 rxdy_gated_en(tp, true); 2916 2917 for (i = 0; i < 1000; i++) { 2918 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 2919 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) 2920 break; 2921 usleep_range(1000, 2000); 2922 } 2923 2924 for (i = 0; i < 1000; i++) { 2925 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) 2926 break; 2927 usleep_range(1000, 2000); 2928 } 2929 2930 rtl_stop_rx(tp); 2931 2932 rtl8152_nic_reset(tp); 2933 } 2934 2935 static void r8152_power_cut_en(struct r8152 *tp, bool enable) 2936 { 2937 u32 ocp_data; 2938 2939 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); 2940 if (enable) 2941 ocp_data |= POWER_CUT; 2942 else 2943 ocp_data &= ~POWER_CUT; 2944 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); 2945 2946 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); 2947 ocp_data &= ~RESUME_INDICATE; 2948 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); 2949 } 2950 2951 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) 2952 { 2953 u32 ocp_data; 2954 2955 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); 2956 if (enable) 2957 ocp_data |= CPCR_RX_VLAN; 2958 else 2959 ocp_data &= ~CPCR_RX_VLAN; 2960 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); 2961 } 2962 2963 static int rtl8152_set_features(struct net_device *dev, 2964 netdev_features_t features) 2965 { 2966 netdev_features_t changed = features ^ dev->features; 2967 struct r8152 *tp = netdev_priv(dev); 2968 int ret; 2969 2970 ret = usb_autopm_get_interface(tp->intf); 2971 if (ret < 0) 2972 goto out; 2973 2974 mutex_lock(&tp->control); 2975 2976 if (changed & NETIF_F_HW_VLAN_CTAG_RX) { 2977 if (features & NETIF_F_HW_VLAN_CTAG_RX) 2978 rtl_rx_vlan_en(tp, true); 2979 else 2980 rtl_rx_vlan_en(tp, false); 2981 } 2982 2983 mutex_unlock(&tp->control); 2984 2985 usb_autopm_put_interface(tp->intf); 2986 2987 out: 2988 return ret; 2989 } 2990 2991 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) 2992 2993 static u32 __rtl_get_wol(struct r8152 *tp) 2994 { 2995 u32 ocp_data; 2996 u32 wolopts = 0; 2997 2998 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 2999 if (ocp_data & LINK_ON_WAKE_EN) 3000 wolopts |= WAKE_PHY; 3001 3002 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); 3003 if (ocp_data & UWF_EN) 3004 wolopts |= WAKE_UCAST; 3005 if (ocp_data & BWF_EN) 3006 wolopts |= WAKE_BCAST; 3007 if (ocp_data & MWF_EN) 3008 wolopts |= WAKE_MCAST; 3009 3010 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); 3011 if (ocp_data & MAGIC_EN) 3012 wolopts |= WAKE_MAGIC; 3013 3014 return wolopts; 3015 } 3016 3017 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts) 3018 { 3019 u32 ocp_data; 3020 3021 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3022 3023 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3024 ocp_data &= ~LINK_ON_WAKE_EN; 3025 if (wolopts & WAKE_PHY) 3026 ocp_data |= LINK_ON_WAKE_EN; 3027 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3028 3029 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); 3030 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN); 3031 if (wolopts & WAKE_UCAST) 3032 ocp_data |= UWF_EN; 3033 if (wolopts & WAKE_BCAST) 3034 ocp_data |= BWF_EN; 3035 if (wolopts & WAKE_MCAST) 3036 ocp_data |= MWF_EN; 3037 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); 3038 3039 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3040 3041 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); 3042 ocp_data &= ~MAGIC_EN; 3043 if (wolopts & WAKE_MAGIC) 3044 ocp_data |= MAGIC_EN; 3045 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); 3046 3047 if (wolopts & WAKE_ANY) 3048 device_set_wakeup_enable(&tp->udev->dev, true); 3049 else 3050 device_set_wakeup_enable(&tp->udev->dev, false); 3051 } 3052 3053 static void r8153_mac_clk_spd(struct r8152 *tp, bool enable) 3054 { 3055 /* MAC clock speed down */ 3056 if (enable) { 3057 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 3058 ALDPS_SPDWN_RATIO); 3059 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 3060 EEE_SPDWN_RATIO); 3061 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 3062 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN | 3063 U1U2_SPDWN_EN | L1_SPDWN_EN); 3064 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 3065 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN | 3066 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN | 3067 TP1000_SPDWN_EN); 3068 } else { 3069 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0); 3070 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0); 3071 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0); 3072 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0); 3073 } 3074 } 3075 3076 static void r8153_u1u2en(struct r8152 *tp, bool enable) 3077 { 3078 u8 u1u2[8]; 3079 3080 if (enable) 3081 memset(u1u2, 0xff, sizeof(u1u2)); 3082 else 3083 memset(u1u2, 0x00, sizeof(u1u2)); 3084 3085 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); 3086 } 3087 3088 static void r8153b_u1u2en(struct r8152 *tp, bool enable) 3089 { 3090 u32 ocp_data; 3091 3092 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG); 3093 if (enable) 3094 ocp_data |= LPM_U1U2_EN; 3095 else 3096 ocp_data &= ~LPM_U1U2_EN; 3097 3098 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data); 3099 } 3100 3101 static void r8153_u2p3en(struct r8152 *tp, bool enable) 3102 { 3103 u32 ocp_data; 3104 3105 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); 3106 if (enable) 3107 ocp_data |= U2P3_ENABLE; 3108 else 3109 ocp_data &= ~U2P3_ENABLE; 3110 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); 3111 } 3112 3113 static void r8153b_ups_flags(struct r8152 *tp) 3114 { 3115 u32 ups_flags = 0; 3116 3117 if (tp->ups_info.green) 3118 ups_flags |= UPS_FLAGS_EN_GREEN; 3119 3120 if (tp->ups_info.aldps) 3121 ups_flags |= UPS_FLAGS_EN_ALDPS; 3122 3123 if (tp->ups_info.eee) 3124 ups_flags |= UPS_FLAGS_EN_EEE; 3125 3126 if (tp->ups_info.flow_control) 3127 ups_flags |= UPS_FLAGS_EN_FLOW_CTR; 3128 3129 if (tp->ups_info.eee_ckdiv) 3130 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV; 3131 3132 if (tp->ups_info.eee_cmod_lv) 3133 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN; 3134 3135 if (tp->ups_info._10m_ckdiv) 3136 ups_flags |= UPS_FLAGS_EN_10M_CKDIV; 3137 3138 if (tp->ups_info.eee_plloff_100) 3139 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100; 3140 3141 if (tp->ups_info.eee_plloff_giga) 3142 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA; 3143 3144 if (tp->ups_info._250m_ckdiv) 3145 ups_flags |= UPS_FLAGS_250M_CKDIV; 3146 3147 if (tp->ups_info.ctap_short_off) 3148 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS; 3149 3150 switch (tp->ups_info.speed_duplex) { 3151 case NWAY_10M_HALF: 3152 ups_flags |= ups_flags_speed(1); 3153 break; 3154 case NWAY_10M_FULL: 3155 ups_flags |= ups_flags_speed(2); 3156 break; 3157 case NWAY_100M_HALF: 3158 ups_flags |= ups_flags_speed(3); 3159 break; 3160 case NWAY_100M_FULL: 3161 ups_flags |= ups_flags_speed(4); 3162 break; 3163 case NWAY_1000M_FULL: 3164 ups_flags |= ups_flags_speed(5); 3165 break; 3166 case FORCE_10M_HALF: 3167 ups_flags |= ups_flags_speed(6); 3168 break; 3169 case FORCE_10M_FULL: 3170 ups_flags |= ups_flags_speed(7); 3171 break; 3172 case FORCE_100M_HALF: 3173 ups_flags |= ups_flags_speed(8); 3174 break; 3175 case FORCE_100M_FULL: 3176 ups_flags |= ups_flags_speed(9); 3177 break; 3178 default: 3179 break; 3180 } 3181 3182 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags); 3183 } 3184 3185 static void r8153b_green_en(struct r8152 *tp, bool enable) 3186 { 3187 u16 data; 3188 3189 if (enable) { 3190 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */ 3191 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */ 3192 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */ 3193 } else { 3194 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */ 3195 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */ 3196 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */ 3197 } 3198 3199 data = sram_read(tp, SRAM_GREEN_CFG); 3200 data |= GREEN_ETH_EN; 3201 sram_write(tp, SRAM_GREEN_CFG, data); 3202 3203 tp->ups_info.green = enable; 3204 } 3205 3206 static u16 r8153_phy_status(struct r8152 *tp, u16 desired) 3207 { 3208 u16 data; 3209 int i; 3210 3211 for (i = 0; i < 500; i++) { 3212 data = ocp_reg_read(tp, OCP_PHY_STATUS); 3213 data &= PHY_STAT_MASK; 3214 if (desired) { 3215 if (data == desired) 3216 break; 3217 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN || 3218 data == PHY_STAT_EXT_INIT) { 3219 break; 3220 } 3221 3222 msleep(20); 3223 } 3224 3225 return data; 3226 } 3227 3228 static void r8153b_ups_en(struct r8152 *tp, bool enable) 3229 { 3230 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); 3231 3232 if (enable) { 3233 r8153b_ups_flags(tp); 3234 3235 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; 3236 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3237 3238 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); 3239 ocp_data |= BIT(0); 3240 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); 3241 } else { 3242 u16 data; 3243 3244 ocp_data &= ~(UPS_EN | USP_PREWAKE); 3245 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3246 3247 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); 3248 ocp_data &= ~BIT(0); 3249 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); 3250 3251 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 3252 ocp_data &= ~PCUT_STATUS; 3253 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 3254 3255 data = r8153_phy_status(tp, 0); 3256 3257 switch (data) { 3258 case PHY_STAT_PWRDN: 3259 case PHY_STAT_EXT_INIT: 3260 r8153b_green_en(tp, 3261 test_bit(GREEN_ETHERNET, &tp->flags)); 3262 3263 data = r8152_mdio_read(tp, MII_BMCR); 3264 data &= ~BMCR_PDOWN; 3265 data |= BMCR_RESET; 3266 r8152_mdio_write(tp, MII_BMCR, data); 3267 3268 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 3269 /* fall through */ 3270 3271 default: 3272 if (data != PHY_STAT_LAN_ON) 3273 netif_warn(tp, link, tp->netdev, 3274 "PHY not ready"); 3275 break; 3276 } 3277 } 3278 } 3279 3280 static void r8153_power_cut_en(struct r8152 *tp, bool enable) 3281 { 3282 u32 ocp_data; 3283 3284 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); 3285 if (enable) 3286 ocp_data |= PWR_EN | PHASE2_EN; 3287 else 3288 ocp_data &= ~(PWR_EN | PHASE2_EN); 3289 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3290 3291 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 3292 ocp_data &= ~PCUT_STATUS; 3293 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 3294 } 3295 3296 static void r8153b_power_cut_en(struct r8152 *tp, bool enable) 3297 { 3298 u32 ocp_data; 3299 3300 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); 3301 if (enable) 3302 ocp_data |= PWR_EN | PHASE2_EN; 3303 else 3304 ocp_data &= ~PWR_EN; 3305 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3306 3307 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 3308 ocp_data &= ~PCUT_STATUS; 3309 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 3310 } 3311 3312 static void r8153_queue_wake(struct r8152 *tp, bool enable) 3313 { 3314 u32 ocp_data; 3315 3316 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG); 3317 if (enable) 3318 ocp_data |= UPCOMING_RUNTIME_D3; 3319 else 3320 ocp_data &= ~UPCOMING_RUNTIME_D3; 3321 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data); 3322 3323 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG); 3324 ocp_data &= ~LINK_CHG_EVENT; 3325 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data); 3326 3327 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 3328 ocp_data &= ~LINK_CHANGE_FLAG; 3329 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 3330 } 3331 3332 static bool rtl_can_wakeup(struct r8152 *tp) 3333 { 3334 struct usb_device *udev = tp->udev; 3335 3336 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP); 3337 } 3338 3339 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable) 3340 { 3341 if (enable) { 3342 u32 ocp_data; 3343 3344 __rtl_set_wol(tp, WAKE_ANY); 3345 3346 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3347 3348 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3349 ocp_data |= LINK_OFF_WAKE_EN; 3350 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3351 3352 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3353 } else { 3354 u32 ocp_data; 3355 3356 __rtl_set_wol(tp, tp->saved_wolopts); 3357 3358 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3359 3360 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3361 ocp_data &= ~LINK_OFF_WAKE_EN; 3362 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3363 3364 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3365 } 3366 } 3367 3368 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable) 3369 { 3370 if (enable) { 3371 r8153_u1u2en(tp, false); 3372 r8153_u2p3en(tp, false); 3373 r8153_mac_clk_spd(tp, true); 3374 rtl_runtime_suspend_enable(tp, true); 3375 } else { 3376 rtl_runtime_suspend_enable(tp, false); 3377 r8153_mac_clk_spd(tp, false); 3378 3379 switch (tp->version) { 3380 case RTL_VER_03: 3381 case RTL_VER_04: 3382 break; 3383 case RTL_VER_05: 3384 case RTL_VER_06: 3385 default: 3386 r8153_u2p3en(tp, true); 3387 break; 3388 } 3389 3390 r8153_u1u2en(tp, true); 3391 } 3392 } 3393 3394 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable) 3395 { 3396 if (enable) { 3397 r8153_queue_wake(tp, true); 3398 r8153b_u1u2en(tp, false); 3399 r8153_u2p3en(tp, false); 3400 rtl_runtime_suspend_enable(tp, true); 3401 r8153b_ups_en(tp, true); 3402 } else { 3403 r8153b_ups_en(tp, false); 3404 r8153_queue_wake(tp, false); 3405 rtl_runtime_suspend_enable(tp, false); 3406 if (tp->udev->speed != USB_SPEED_HIGH) 3407 r8153b_u1u2en(tp, true); 3408 } 3409 } 3410 3411 static void r8153_teredo_off(struct r8152 *tp) 3412 { 3413 u32 ocp_data; 3414 3415 switch (tp->version) { 3416 case RTL_VER_01: 3417 case RTL_VER_02: 3418 case RTL_VER_03: 3419 case RTL_VER_04: 3420 case RTL_VER_05: 3421 case RTL_VER_06: 3422 case RTL_VER_07: 3423 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); 3424 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | 3425 OOB_TEREDO_EN); 3426 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); 3427 break; 3428 3429 case RTL_VER_08: 3430 case RTL_VER_09: 3431 /* The bit 0 ~ 7 are relative with teredo settings. They are 3432 * W1C (write 1 to clear), so set all 1 to disable it. 3433 */ 3434 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff); 3435 break; 3436 3437 default: 3438 break; 3439 } 3440 3441 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); 3442 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); 3443 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); 3444 } 3445 3446 static void rtl_reset_bmu(struct r8152 *tp) 3447 { 3448 u32 ocp_data; 3449 3450 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET); 3451 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT); 3452 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 3453 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT; 3454 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 3455 } 3456 3457 /* Clear the bp to stop the firmware before loading a new one */ 3458 static void rtl_clear_bp(struct r8152 *tp, u16 type) 3459 { 3460 switch (tp->version) { 3461 case RTL_VER_01: 3462 case RTL_VER_02: 3463 case RTL_VER_07: 3464 break; 3465 case RTL_VER_03: 3466 case RTL_VER_04: 3467 case RTL_VER_05: 3468 case RTL_VER_06: 3469 ocp_write_byte(tp, type, PLA_BP_EN, 0); 3470 break; 3471 case RTL_VER_08: 3472 case RTL_VER_09: 3473 default: 3474 if (type == MCU_TYPE_USB) { 3475 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP2_EN, 0); 3476 3477 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0); 3478 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0); 3479 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0); 3480 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0); 3481 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0); 3482 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0); 3483 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0); 3484 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0); 3485 } else { 3486 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0); 3487 } 3488 break; 3489 } 3490 3491 ocp_write_word(tp, type, PLA_BP_0, 0); 3492 ocp_write_word(tp, type, PLA_BP_1, 0); 3493 ocp_write_word(tp, type, PLA_BP_2, 0); 3494 ocp_write_word(tp, type, PLA_BP_3, 0); 3495 ocp_write_word(tp, type, PLA_BP_4, 0); 3496 ocp_write_word(tp, type, PLA_BP_5, 0); 3497 ocp_write_word(tp, type, PLA_BP_6, 0); 3498 ocp_write_word(tp, type, PLA_BP_7, 0); 3499 3500 /* wait 3 ms to make sure the firmware is stopped */ 3501 usleep_range(3000, 6000); 3502 ocp_write_word(tp, type, PLA_BP_BA, 0); 3503 } 3504 3505 static int r8153_patch_request(struct r8152 *tp, bool request) 3506 { 3507 u16 data; 3508 int i; 3509 3510 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD); 3511 if (request) 3512 data |= PATCH_REQUEST; 3513 else 3514 data &= ~PATCH_REQUEST; 3515 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data); 3516 3517 for (i = 0; request && i < 5000; i++) { 3518 usleep_range(1000, 2000); 3519 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY) 3520 break; 3521 } 3522 3523 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) { 3524 netif_err(tp, drv, tp->netdev, "patch request fail\n"); 3525 r8153_patch_request(tp, false); 3526 return -ETIME; 3527 } else { 3528 return 0; 3529 } 3530 } 3531 3532 static int r8153_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key) 3533 { 3534 if (r8153_patch_request(tp, true)) { 3535 dev_err(&tp->intf->dev, "patch request fail\n"); 3536 return -ETIME; 3537 } 3538 3539 sram_write(tp, key_addr, patch_key); 3540 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK); 3541 3542 return 0; 3543 } 3544 3545 static int r8153_post_ram_code(struct r8152 *tp, u16 key_addr) 3546 { 3547 u16 data; 3548 3549 sram_write(tp, 0x0000, 0x0000); 3550 3551 data = ocp_reg_read(tp, OCP_PHY_LOCK); 3552 data &= ~PATCH_LOCK; 3553 ocp_reg_write(tp, OCP_PHY_LOCK, data); 3554 3555 sram_write(tp, key_addr, 0x0000); 3556 3557 r8153_patch_request(tp, false); 3558 3559 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base); 3560 3561 return 0; 3562 } 3563 3564 static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy) 3565 { 3566 u32 length; 3567 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start; 3568 bool rc = false; 3569 3570 switch (tp->version) { 3571 case RTL_VER_04: 3572 case RTL_VER_05: 3573 case RTL_VER_06: 3574 fw_reg = 0xa014; 3575 ba_reg = 0xa012; 3576 patch_en_addr = 0xa01a; 3577 mode_reg = 0xb820; 3578 bp_start = 0xa000; 3579 break; 3580 default: 3581 goto out; 3582 } 3583 3584 fw_offset = __le16_to_cpu(phy->fw_offset); 3585 if (fw_offset < sizeof(*phy)) { 3586 dev_err(&tp->intf->dev, "fw_offset too small\n"); 3587 goto out; 3588 } 3589 3590 length = __le32_to_cpu(phy->blk_hdr.length); 3591 if (length < fw_offset) { 3592 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 3593 goto out; 3594 } 3595 3596 length -= __le16_to_cpu(phy->fw_offset); 3597 if (!length || (length & 1)) { 3598 dev_err(&tp->intf->dev, "invalid block length\n"); 3599 goto out; 3600 } 3601 3602 if (__le16_to_cpu(phy->fw_reg) != fw_reg) { 3603 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 3604 goto out; 3605 } 3606 3607 if (__le16_to_cpu(phy->ba_reg) != ba_reg) { 3608 dev_err(&tp->intf->dev, "invalid base address register\n"); 3609 goto out; 3610 } 3611 3612 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) { 3613 dev_err(&tp->intf->dev, 3614 "invalid patch mode enabled register\n"); 3615 goto out; 3616 } 3617 3618 if (__le16_to_cpu(phy->mode_reg) != mode_reg) { 3619 dev_err(&tp->intf->dev, 3620 "invalid register to switch the mode\n"); 3621 goto out; 3622 } 3623 3624 if (__le16_to_cpu(phy->bp_start) != bp_start) { 3625 dev_err(&tp->intf->dev, 3626 "invalid start register of break point\n"); 3627 goto out; 3628 } 3629 3630 if (__le16_to_cpu(phy->bp_num) > 4) { 3631 dev_err(&tp->intf->dev, "invalid break point number\n"); 3632 goto out; 3633 } 3634 3635 rc = true; 3636 out: 3637 return rc; 3638 } 3639 3640 static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac) 3641 { 3642 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset; 3643 bool rc = false; 3644 u32 length, type; 3645 int i, max_bp; 3646 3647 type = __le32_to_cpu(mac->blk_hdr.type); 3648 if (type == RTL_FW_PLA) { 3649 switch (tp->version) { 3650 case RTL_VER_01: 3651 case RTL_VER_02: 3652 case RTL_VER_07: 3653 fw_reg = 0xf800; 3654 bp_ba_addr = PLA_BP_BA; 3655 bp_en_addr = 0; 3656 bp_start = PLA_BP_0; 3657 max_bp = 8; 3658 break; 3659 case RTL_VER_03: 3660 case RTL_VER_04: 3661 case RTL_VER_05: 3662 case RTL_VER_06: 3663 case RTL_VER_08: 3664 case RTL_VER_09: 3665 fw_reg = 0xf800; 3666 bp_ba_addr = PLA_BP_BA; 3667 bp_en_addr = PLA_BP_EN; 3668 bp_start = PLA_BP_0; 3669 max_bp = 8; 3670 break; 3671 default: 3672 goto out; 3673 } 3674 } else if (type == RTL_FW_USB) { 3675 switch (tp->version) { 3676 case RTL_VER_03: 3677 case RTL_VER_04: 3678 case RTL_VER_05: 3679 case RTL_VER_06: 3680 fw_reg = 0xf800; 3681 bp_ba_addr = USB_BP_BA; 3682 bp_en_addr = USB_BP_EN; 3683 bp_start = USB_BP_0; 3684 max_bp = 8; 3685 break; 3686 case RTL_VER_08: 3687 case RTL_VER_09: 3688 fw_reg = 0xe600; 3689 bp_ba_addr = USB_BP_BA; 3690 bp_en_addr = USB_BP2_EN; 3691 bp_start = USB_BP_0; 3692 max_bp = 16; 3693 break; 3694 case RTL_VER_01: 3695 case RTL_VER_02: 3696 case RTL_VER_07: 3697 default: 3698 goto out; 3699 } 3700 } else { 3701 goto out; 3702 } 3703 3704 fw_offset = __le16_to_cpu(mac->fw_offset); 3705 if (fw_offset < sizeof(*mac)) { 3706 dev_err(&tp->intf->dev, "fw_offset too small\n"); 3707 goto out; 3708 } 3709 3710 length = __le32_to_cpu(mac->blk_hdr.length); 3711 if (length < fw_offset) { 3712 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 3713 goto out; 3714 } 3715 3716 length -= fw_offset; 3717 if (length < 4 || (length & 3)) { 3718 dev_err(&tp->intf->dev, "invalid block length\n"); 3719 goto out; 3720 } 3721 3722 if (__le16_to_cpu(mac->fw_reg) != fw_reg) { 3723 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 3724 goto out; 3725 } 3726 3727 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) { 3728 dev_err(&tp->intf->dev, "invalid base address register\n"); 3729 goto out; 3730 } 3731 3732 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) { 3733 dev_err(&tp->intf->dev, "invalid enabled mask register\n"); 3734 goto out; 3735 } 3736 3737 if (__le16_to_cpu(mac->bp_start) != bp_start) { 3738 dev_err(&tp->intf->dev, 3739 "invalid start register of break point\n"); 3740 goto out; 3741 } 3742 3743 if (__le16_to_cpu(mac->bp_num) > max_bp) { 3744 dev_err(&tp->intf->dev, "invalid break point number\n"); 3745 goto out; 3746 } 3747 3748 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) { 3749 if (mac->bp[i]) { 3750 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i); 3751 goto out; 3752 } 3753 } 3754 3755 rc = true; 3756 out: 3757 return rc; 3758 } 3759 3760 /* Verify the checksum for the firmware file. It is calculated from the version 3761 * field to the end of the file. Compare the result with the checksum field to 3762 * make sure the file is correct. 3763 */ 3764 static long rtl8152_fw_verify_checksum(struct r8152 *tp, 3765 struct fw_header *fw_hdr, size_t size) 3766 { 3767 unsigned char checksum[sizeof(fw_hdr->checksum)]; 3768 struct crypto_shash *alg; 3769 struct shash_desc *sdesc; 3770 size_t len; 3771 long rc; 3772 3773 alg = crypto_alloc_shash("sha256", 0, 0); 3774 if (IS_ERR(alg)) { 3775 rc = PTR_ERR(alg); 3776 goto out; 3777 } 3778 3779 if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) { 3780 rc = -EFAULT; 3781 dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n", 3782 crypto_shash_digestsize(alg)); 3783 goto free_shash; 3784 } 3785 3786 len = sizeof(*sdesc) + crypto_shash_descsize(alg); 3787 sdesc = kmalloc(len, GFP_KERNEL); 3788 if (!sdesc) { 3789 rc = -ENOMEM; 3790 goto free_shash; 3791 } 3792 sdesc->tfm = alg; 3793 3794 len = size - sizeof(fw_hdr->checksum); 3795 rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum); 3796 kfree(sdesc); 3797 if (rc) 3798 goto free_shash; 3799 3800 if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) { 3801 dev_err(&tp->intf->dev, "checksum fail\n"); 3802 rc = -EFAULT; 3803 } 3804 3805 free_shash: 3806 crypto_free_shash(alg); 3807 out: 3808 return rc; 3809 } 3810 3811 static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw) 3812 { 3813 const struct firmware *fw = rtl_fw->fw; 3814 struct fw_header *fw_hdr = (struct fw_header *)fw->data; 3815 struct fw_mac *pla = NULL, *usb = NULL; 3816 struct fw_phy_patch_key *start = NULL; 3817 struct fw_phy_nc *phy_nc = NULL; 3818 struct fw_block *stop = NULL; 3819 long ret = -EFAULT; 3820 int i; 3821 3822 if (fw->size < sizeof(*fw_hdr)) { 3823 dev_err(&tp->intf->dev, "file too small\n"); 3824 goto fail; 3825 } 3826 3827 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size); 3828 if (ret) 3829 goto fail; 3830 3831 ret = -EFAULT; 3832 3833 for (i = sizeof(*fw_hdr); i < fw->size;) { 3834 struct fw_block *block = (struct fw_block *)&fw->data[i]; 3835 u32 type; 3836 3837 if ((i + sizeof(*block)) > fw->size) 3838 goto fail; 3839 3840 type = __le32_to_cpu(block->type); 3841 switch (type) { 3842 case RTL_FW_END: 3843 if (__le32_to_cpu(block->length) != sizeof(*block)) 3844 goto fail; 3845 goto fw_end; 3846 case RTL_FW_PLA: 3847 if (pla) { 3848 dev_err(&tp->intf->dev, 3849 "multiple PLA firmware encountered"); 3850 goto fail; 3851 } 3852 3853 pla = (struct fw_mac *)block; 3854 if (!rtl8152_is_fw_mac_ok(tp, pla)) { 3855 dev_err(&tp->intf->dev, 3856 "check PLA firmware failed\n"); 3857 goto fail; 3858 } 3859 break; 3860 case RTL_FW_USB: 3861 if (usb) { 3862 dev_err(&tp->intf->dev, 3863 "multiple USB firmware encountered"); 3864 goto fail; 3865 } 3866 3867 usb = (struct fw_mac *)block; 3868 if (!rtl8152_is_fw_mac_ok(tp, usb)) { 3869 dev_err(&tp->intf->dev, 3870 "check USB firmware failed\n"); 3871 goto fail; 3872 } 3873 break; 3874 case RTL_FW_PHY_START: 3875 if (start || phy_nc || stop) { 3876 dev_err(&tp->intf->dev, 3877 "check PHY_START fail\n"); 3878 goto fail; 3879 } 3880 3881 if (__le32_to_cpu(block->length) != sizeof(*start)) { 3882 dev_err(&tp->intf->dev, 3883 "Invalid length for PHY_START\n"); 3884 goto fail; 3885 } 3886 3887 start = (struct fw_phy_patch_key *)block; 3888 break; 3889 case RTL_FW_PHY_STOP: 3890 if (stop || !start) { 3891 dev_err(&tp->intf->dev, 3892 "Check PHY_STOP fail\n"); 3893 goto fail; 3894 } 3895 3896 if (__le32_to_cpu(block->length) != sizeof(*block)) { 3897 dev_err(&tp->intf->dev, 3898 "Invalid length for PHY_STOP\n"); 3899 goto fail; 3900 } 3901 3902 stop = block; 3903 break; 3904 case RTL_FW_PHY_NC: 3905 if (!start || stop) { 3906 dev_err(&tp->intf->dev, 3907 "check PHY_NC fail\n"); 3908 goto fail; 3909 } 3910 3911 if (phy_nc) { 3912 dev_err(&tp->intf->dev, 3913 "multiple PHY NC encountered\n"); 3914 goto fail; 3915 } 3916 3917 phy_nc = (struct fw_phy_nc *)block; 3918 if (!rtl8152_is_fw_phy_nc_ok(tp, phy_nc)) { 3919 dev_err(&tp->intf->dev, 3920 "check PHY NC firmware failed\n"); 3921 goto fail; 3922 } 3923 3924 break; 3925 default: 3926 dev_warn(&tp->intf->dev, "Unknown type %u is found\n", 3927 type); 3928 break; 3929 } 3930 3931 /* next block */ 3932 i += ALIGN(__le32_to_cpu(block->length), 8); 3933 } 3934 3935 fw_end: 3936 if ((phy_nc || start) && !stop) { 3937 dev_err(&tp->intf->dev, "without PHY_STOP\n"); 3938 goto fail; 3939 } 3940 3941 return 0; 3942 fail: 3943 return ret; 3944 } 3945 3946 static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy) 3947 { 3948 u16 mode_reg, bp_index; 3949 u32 length, i, num; 3950 __le16 *data; 3951 3952 mode_reg = __le16_to_cpu(phy->mode_reg); 3953 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre)); 3954 sram_write(tp, __le16_to_cpu(phy->ba_reg), 3955 __le16_to_cpu(phy->ba_data)); 3956 3957 length = __le32_to_cpu(phy->blk_hdr.length); 3958 length -= __le16_to_cpu(phy->fw_offset); 3959 num = length / 2; 3960 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset)); 3961 3962 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg)); 3963 for (i = 0; i < num; i++) 3964 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i])); 3965 3966 sram_write(tp, __le16_to_cpu(phy->patch_en_addr), 3967 __le16_to_cpu(phy->patch_en_value)); 3968 3969 bp_index = __le16_to_cpu(phy->bp_start); 3970 num = __le16_to_cpu(phy->bp_num); 3971 for (i = 0; i < num; i++) { 3972 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i])); 3973 bp_index += 2; 3974 } 3975 3976 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post)); 3977 3978 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 3979 } 3980 3981 static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac) 3982 { 3983 u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg; 3984 u32 length; 3985 u8 *data; 3986 int i; 3987 3988 switch (__le32_to_cpu(mac->blk_hdr.type)) { 3989 case RTL_FW_PLA: 3990 type = MCU_TYPE_PLA; 3991 break; 3992 case RTL_FW_USB: 3993 type = MCU_TYPE_USB; 3994 break; 3995 default: 3996 return; 3997 } 3998 3999 rtl_clear_bp(tp, type); 4000 4001 /* Enable backup/restore of MACDBG. This is required after clearing PLA 4002 * break points and before applying the PLA firmware. 4003 */ 4004 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA && 4005 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) { 4006 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM); 4007 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM); 4008 } 4009 4010 length = __le32_to_cpu(mac->blk_hdr.length); 4011 length -= __le16_to_cpu(mac->fw_offset); 4012 4013 data = (u8 *)mac; 4014 data += __le16_to_cpu(mac->fw_offset); 4015 4016 generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data, 4017 type); 4018 4019 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr), 4020 __le16_to_cpu(mac->bp_ba_value)); 4021 4022 bp_index = __le16_to_cpu(mac->bp_start); 4023 bp_num = __le16_to_cpu(mac->bp_num); 4024 for (i = 0; i < bp_num; i++) { 4025 ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i])); 4026 bp_index += 2; 4027 } 4028 4029 bp_en_addr = __le16_to_cpu(mac->bp_en_addr); 4030 if (bp_en_addr) 4031 ocp_write_word(tp, type, bp_en_addr, 4032 __le16_to_cpu(mac->bp_en_value)); 4033 4034 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg); 4035 if (fw_ver_reg) 4036 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg, 4037 mac->fw_ver_data); 4038 4039 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info); 4040 } 4041 4042 static void rtl8152_apply_firmware(struct r8152 *tp) 4043 { 4044 struct rtl_fw *rtl_fw = &tp->rtl_fw; 4045 const struct firmware *fw; 4046 struct fw_header *fw_hdr; 4047 struct fw_phy_patch_key *key; 4048 u16 key_addr = 0; 4049 int i; 4050 4051 if (IS_ERR_OR_NULL(rtl_fw->fw)) 4052 return; 4053 4054 fw = rtl_fw->fw; 4055 fw_hdr = (struct fw_header *)fw->data; 4056 4057 if (rtl_fw->pre_fw) 4058 rtl_fw->pre_fw(tp); 4059 4060 for (i = offsetof(struct fw_header, blocks); i < fw->size;) { 4061 struct fw_block *block = (struct fw_block *)&fw->data[i]; 4062 4063 switch (__le32_to_cpu(block->type)) { 4064 case RTL_FW_END: 4065 goto post_fw; 4066 case RTL_FW_PLA: 4067 case RTL_FW_USB: 4068 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block); 4069 break; 4070 case RTL_FW_PHY_START: 4071 key = (struct fw_phy_patch_key *)block; 4072 key_addr = __le16_to_cpu(key->key_reg); 4073 r8153_pre_ram_code(tp, key_addr, 4074 __le16_to_cpu(key->key_data)); 4075 break; 4076 case RTL_FW_PHY_STOP: 4077 WARN_ON(!key_addr); 4078 r8153_post_ram_code(tp, key_addr); 4079 break; 4080 case RTL_FW_PHY_NC: 4081 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block); 4082 break; 4083 default: 4084 break; 4085 } 4086 4087 i += ALIGN(__le32_to_cpu(block->length), 8); 4088 } 4089 4090 post_fw: 4091 if (rtl_fw->post_fw) 4092 rtl_fw->post_fw(tp); 4093 4094 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE); 4095 dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version); 4096 } 4097 4098 static void rtl8152_release_firmware(struct r8152 *tp) 4099 { 4100 struct rtl_fw *rtl_fw = &tp->rtl_fw; 4101 4102 if (!IS_ERR_OR_NULL(rtl_fw->fw)) { 4103 release_firmware(rtl_fw->fw); 4104 rtl_fw->fw = NULL; 4105 } 4106 } 4107 4108 static int rtl8152_request_firmware(struct r8152 *tp) 4109 { 4110 struct rtl_fw *rtl_fw = &tp->rtl_fw; 4111 long rc; 4112 4113 if (rtl_fw->fw || !rtl_fw->fw_name) { 4114 dev_info(&tp->intf->dev, "skip request firmware\n"); 4115 rc = 0; 4116 goto result; 4117 } 4118 4119 rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev); 4120 if (rc < 0) 4121 goto result; 4122 4123 rc = rtl8152_check_firmware(tp, rtl_fw); 4124 if (rc < 0) 4125 release_firmware(rtl_fw->fw); 4126 4127 result: 4128 if (rc) { 4129 rtl_fw->fw = ERR_PTR(rc); 4130 4131 dev_warn(&tp->intf->dev, 4132 "unable to load firmware patch %s (%ld)\n", 4133 rtl_fw->fw_name, rc); 4134 } 4135 4136 return rc; 4137 } 4138 4139 static void r8152_aldps_en(struct r8152 *tp, bool enable) 4140 { 4141 if (enable) { 4142 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | 4143 LINKENA | DIS_SDSAVE); 4144 } else { 4145 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | 4146 DIS_SDSAVE); 4147 msleep(20); 4148 } 4149 } 4150 4151 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg) 4152 { 4153 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev); 4154 ocp_reg_write(tp, OCP_EEE_DATA, reg); 4155 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev); 4156 } 4157 4158 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg) 4159 { 4160 u16 data; 4161 4162 r8152_mmd_indirect(tp, dev, reg); 4163 data = ocp_reg_read(tp, OCP_EEE_DATA); 4164 ocp_reg_write(tp, OCP_EEE_AR, 0x0000); 4165 4166 return data; 4167 } 4168 4169 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data) 4170 { 4171 r8152_mmd_indirect(tp, dev, reg); 4172 ocp_reg_write(tp, OCP_EEE_DATA, data); 4173 ocp_reg_write(tp, OCP_EEE_AR, 0x0000); 4174 } 4175 4176 static void r8152_eee_en(struct r8152 *tp, bool enable) 4177 { 4178 u16 config1, config2, config3; 4179 u32 ocp_data; 4180 4181 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); 4182 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask; 4183 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2); 4184 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask; 4185 4186 if (enable) { 4187 ocp_data |= EEE_RX_EN | EEE_TX_EN; 4188 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN; 4189 config1 |= sd_rise_time(1); 4190 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN; 4191 config3 |= fast_snr(42); 4192 } else { 4193 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); 4194 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | 4195 RX_QUIET_EN); 4196 config1 |= sd_rise_time(7); 4197 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN); 4198 config3 |= fast_snr(511); 4199 } 4200 4201 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); 4202 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1); 4203 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2); 4204 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3); 4205 } 4206 4207 static void r8153_eee_en(struct r8152 *tp, bool enable) 4208 { 4209 u32 ocp_data; 4210 u16 config; 4211 4212 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); 4213 config = ocp_reg_read(tp, OCP_EEE_CFG); 4214 4215 if (enable) { 4216 ocp_data |= EEE_RX_EN | EEE_TX_EN; 4217 config |= EEE10_EN; 4218 } else { 4219 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); 4220 config &= ~EEE10_EN; 4221 } 4222 4223 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); 4224 ocp_reg_write(tp, OCP_EEE_CFG, config); 4225 4226 tp->ups_info.eee = enable; 4227 } 4228 4229 static void rtl_eee_enable(struct r8152 *tp, bool enable) 4230 { 4231 switch (tp->version) { 4232 case RTL_VER_01: 4233 case RTL_VER_02: 4234 case RTL_VER_07: 4235 if (enable) { 4236 r8152_eee_en(tp, true); 4237 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 4238 tp->eee_adv); 4239 } else { 4240 r8152_eee_en(tp, false); 4241 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0); 4242 } 4243 break; 4244 case RTL_VER_03: 4245 case RTL_VER_04: 4246 case RTL_VER_05: 4247 case RTL_VER_06: 4248 case RTL_VER_08: 4249 case RTL_VER_09: 4250 if (enable) { 4251 r8153_eee_en(tp, true); 4252 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); 4253 } else { 4254 r8153_eee_en(tp, false); 4255 ocp_reg_write(tp, OCP_EEE_ADV, 0); 4256 } 4257 break; 4258 default: 4259 break; 4260 } 4261 } 4262 4263 static void r8152b_enable_fc(struct r8152 *tp) 4264 { 4265 u16 anar; 4266 4267 anar = r8152_mdio_read(tp, MII_ADVERTISE); 4268 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; 4269 r8152_mdio_write(tp, MII_ADVERTISE, anar); 4270 4271 tp->ups_info.flow_control = true; 4272 } 4273 4274 static void rtl8152_disable(struct r8152 *tp) 4275 { 4276 r8152_aldps_en(tp, false); 4277 rtl_disable(tp); 4278 r8152_aldps_en(tp, true); 4279 } 4280 4281 static void r8152b_hw_phy_cfg(struct r8152 *tp) 4282 { 4283 rtl8152_apply_firmware(tp); 4284 rtl_eee_enable(tp, tp->eee_en); 4285 r8152_aldps_en(tp, true); 4286 r8152b_enable_fc(tp); 4287 4288 set_bit(PHY_RESET, &tp->flags); 4289 } 4290 4291 static void wait_oob_link_list_ready(struct r8152 *tp) 4292 { 4293 u32 ocp_data; 4294 int i; 4295 4296 for (i = 0; i < 1000; i++) { 4297 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 4298 if (ocp_data & LINK_LIST_READY) 4299 break; 4300 usleep_range(1000, 2000); 4301 } 4302 } 4303 4304 static void r8152b_exit_oob(struct r8152 *tp) 4305 { 4306 u32 ocp_data; 4307 4308 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 4309 ocp_data &= ~RCR_ACPT_ALL; 4310 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 4311 4312 rxdy_gated_en(tp, true); 4313 r8153_teredo_off(tp); 4314 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 4315 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); 4316 4317 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 4318 ocp_data &= ~NOW_IS_OOB; 4319 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 4320 4321 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 4322 ocp_data &= ~MCU_BORW_EN; 4323 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 4324 4325 wait_oob_link_list_ready(tp); 4326 4327 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 4328 ocp_data |= RE_INIT_LL; 4329 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 4330 4331 wait_oob_link_list_ready(tp); 4332 4333 rtl8152_nic_reset(tp); 4334 4335 /* rx share fifo credit full threshold */ 4336 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); 4337 4338 if (tp->udev->speed == USB_SPEED_FULL || 4339 tp->udev->speed == USB_SPEED_LOW) { 4340 /* rx share fifo credit near full threshold */ 4341 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 4342 RXFIFO_THR2_FULL); 4343 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 4344 RXFIFO_THR3_FULL); 4345 } else { 4346 /* rx share fifo credit near full threshold */ 4347 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 4348 RXFIFO_THR2_HIGH); 4349 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 4350 RXFIFO_THR3_HIGH); 4351 } 4352 4353 /* TX share fifo free credit full threshold */ 4354 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); 4355 4356 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); 4357 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH); 4358 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, 4359 TEST_MODE_DISABLE | TX_SIZE_ADJUST1); 4360 4361 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 4362 4363 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 4364 4365 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); 4366 ocp_data |= TCR0_AUTO_FIFO; 4367 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); 4368 } 4369 4370 static void r8152b_enter_oob(struct r8152 *tp) 4371 { 4372 u32 ocp_data; 4373 4374 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 4375 ocp_data &= ~NOW_IS_OOB; 4376 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 4377 4378 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); 4379 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); 4380 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); 4381 4382 rtl_disable(tp); 4383 4384 wait_oob_link_list_ready(tp); 4385 4386 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 4387 ocp_data |= RE_INIT_LL; 4388 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 4389 4390 wait_oob_link_list_ready(tp); 4391 4392 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 4393 4394 rtl_rx_vlan_en(tp, true); 4395 4396 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); 4397 ocp_data |= ALDPS_PROXY_MODE; 4398 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); 4399 4400 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 4401 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; 4402 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 4403 4404 rxdy_gated_en(tp, false); 4405 4406 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 4407 ocp_data |= RCR_APM | RCR_AM | RCR_AB; 4408 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 4409 } 4410 4411 static int r8153_pre_firmware_1(struct r8152 *tp) 4412 { 4413 int i; 4414 4415 /* Wait till the WTD timer is ready. It would take at most 104 ms. */ 4416 for (i = 0; i < 104; i++) { 4417 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL); 4418 4419 if (!(ocp_data & WTD1_EN)) 4420 break; 4421 usleep_range(1000, 2000); 4422 } 4423 4424 return 0; 4425 } 4426 4427 static int r8153_post_firmware_1(struct r8152 *tp) 4428 { 4429 /* set USB_BP_4 to support USB_SPEED_SUPER only */ 4430 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) 4431 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY); 4432 4433 /* reset UPHY timer to 36 ms */ 4434 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); 4435 4436 return 0; 4437 } 4438 4439 static int r8153_pre_firmware_2(struct r8152 *tp) 4440 { 4441 u32 ocp_data; 4442 4443 r8153_pre_firmware_1(tp); 4444 4445 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); 4446 ocp_data &= ~FW_FIX_SUSPEND; 4447 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data); 4448 4449 return 0; 4450 } 4451 4452 static int r8153_post_firmware_2(struct r8152 *tp) 4453 { 4454 u32 ocp_data; 4455 4456 /* enable bp0 if support USB_SPEED_SUPER only */ 4457 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) { 4458 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); 4459 ocp_data |= BIT(0); 4460 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); 4461 } 4462 4463 /* reset UPHY timer to 36 ms */ 4464 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); 4465 4466 /* enable U3P3 check, set the counter to 4 */ 4467 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4); 4468 4469 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); 4470 ocp_data |= FW_FIX_SUSPEND; 4471 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data); 4472 4473 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); 4474 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; 4475 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); 4476 4477 return 0; 4478 } 4479 4480 static int r8153_post_firmware_3(struct r8152 *tp) 4481 { 4482 u32 ocp_data; 4483 4484 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); 4485 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; 4486 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); 4487 4488 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); 4489 ocp_data |= FW_IP_RESET_EN; 4490 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); 4491 4492 return 0; 4493 } 4494 4495 static int r8153b_pre_firmware_1(struct r8152 *tp) 4496 { 4497 /* enable fc timer and set timer to 1 second. */ 4498 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, 4499 CTRL_TIMER_EN | (1000 / 8)); 4500 4501 return 0; 4502 } 4503 4504 static int r8153b_post_firmware_1(struct r8152 *tp) 4505 { 4506 u32 ocp_data; 4507 4508 /* enable bp0 for RTL8153-BND */ 4509 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); 4510 if (ocp_data & BND_MASK) { 4511 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); 4512 ocp_data |= BIT(0); 4513 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); 4514 } 4515 4516 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); 4517 ocp_data |= FLOW_CTRL_PATCH_OPT; 4518 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); 4519 4520 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 4521 ocp_data |= FC_PATCH_TASK; 4522 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 4523 4524 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); 4525 ocp_data |= FW_IP_RESET_EN; 4526 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); 4527 4528 return 0; 4529 } 4530 4531 static void r8153_aldps_en(struct r8152 *tp, bool enable) 4532 { 4533 u16 data; 4534 4535 data = ocp_reg_read(tp, OCP_POWER_CFG); 4536 if (enable) { 4537 data |= EN_ALDPS; 4538 ocp_reg_write(tp, OCP_POWER_CFG, data); 4539 } else { 4540 int i; 4541 4542 data &= ~EN_ALDPS; 4543 ocp_reg_write(tp, OCP_POWER_CFG, data); 4544 for (i = 0; i < 20; i++) { 4545 usleep_range(1000, 2000); 4546 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100) 4547 break; 4548 } 4549 } 4550 4551 tp->ups_info.aldps = enable; 4552 } 4553 4554 static void r8153_hw_phy_cfg(struct r8152 *tp) 4555 { 4556 u32 ocp_data; 4557 u16 data; 4558 4559 /* disable ALDPS before updating the PHY parameters */ 4560 r8153_aldps_en(tp, false); 4561 4562 /* disable EEE before updating the PHY parameters */ 4563 rtl_eee_enable(tp, false); 4564 4565 rtl8152_apply_firmware(tp); 4566 4567 if (tp->version == RTL_VER_03) { 4568 data = ocp_reg_read(tp, OCP_EEE_CFG); 4569 data &= ~CTAP_SHORT_EN; 4570 ocp_reg_write(tp, OCP_EEE_CFG, data); 4571 } 4572 4573 data = ocp_reg_read(tp, OCP_POWER_CFG); 4574 data |= EEE_CLKDIV_EN; 4575 ocp_reg_write(tp, OCP_POWER_CFG, data); 4576 4577 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 4578 data |= EN_10M_BGOFF; 4579 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 4580 data = ocp_reg_read(tp, OCP_POWER_CFG); 4581 data |= EN_10M_PLLOFF; 4582 ocp_reg_write(tp, OCP_POWER_CFG, data); 4583 sram_write(tp, SRAM_IMPEDANCE, 0x0b13); 4584 4585 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 4586 ocp_data |= PFM_PWM_SWITCH; 4587 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 4588 4589 /* Enable LPF corner auto tune */ 4590 sram_write(tp, SRAM_LPF_CFG, 0xf70f); 4591 4592 /* Adjust 10M Amplitude */ 4593 sram_write(tp, SRAM_10M_AMP1, 0x00af); 4594 sram_write(tp, SRAM_10M_AMP2, 0x0208); 4595 4596 if (tp->eee_en) 4597 rtl_eee_enable(tp, true); 4598 4599 r8153_aldps_en(tp, true); 4600 r8152b_enable_fc(tp); 4601 4602 switch (tp->version) { 4603 case RTL_VER_03: 4604 case RTL_VER_04: 4605 break; 4606 case RTL_VER_05: 4607 case RTL_VER_06: 4608 default: 4609 r8153_u2p3en(tp, true); 4610 break; 4611 } 4612 4613 set_bit(PHY_RESET, &tp->flags); 4614 } 4615 4616 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr) 4617 { 4618 u32 ocp_data; 4619 4620 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr); 4621 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD); 4622 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */ 4623 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA); 4624 4625 return ocp_data; 4626 } 4627 4628 static void r8153b_hw_phy_cfg(struct r8152 *tp) 4629 { 4630 u32 ocp_data; 4631 u16 data; 4632 4633 /* disable ALDPS before updating the PHY parameters */ 4634 r8153_aldps_en(tp, false); 4635 4636 /* disable EEE before updating the PHY parameters */ 4637 rtl_eee_enable(tp, false); 4638 4639 rtl8152_apply_firmware(tp); 4640 4641 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 4642 4643 data = sram_read(tp, SRAM_GREEN_CFG); 4644 data |= R_TUNE_EN; 4645 sram_write(tp, SRAM_GREEN_CFG, data); 4646 data = ocp_reg_read(tp, OCP_NCTL_CFG); 4647 data |= PGA_RETURN_EN; 4648 ocp_reg_write(tp, OCP_NCTL_CFG, data); 4649 4650 /* ADC Bias Calibration: 4651 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake 4652 * bit (bit3) to rebuild the real 16-bit data. Write the data to the 4653 * ADC ioffset. 4654 */ 4655 ocp_data = r8152_efuse_read(tp, 0x7d); 4656 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7)); 4657 if (data != 0xffff) 4658 ocp_reg_write(tp, OCP_ADC_IOFFSET, data); 4659 4660 /* ups mode tx-link-pulse timing adjustment: 4661 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0] 4662 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt 4663 */ 4664 ocp_data = ocp_reg_read(tp, 0xc426); 4665 ocp_data &= 0x3fff; 4666 if (ocp_data) { 4667 u32 swr_cnt_1ms_ini; 4668 4669 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK; 4670 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG); 4671 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini; 4672 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data); 4673 } 4674 4675 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 4676 ocp_data |= PFM_PWM_SWITCH; 4677 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 4678 4679 /* Advnace EEE */ 4680 if (!r8153_patch_request(tp, true)) { 4681 data = ocp_reg_read(tp, OCP_POWER_CFG); 4682 data |= EEE_CLKDIV_EN; 4683 ocp_reg_write(tp, OCP_POWER_CFG, data); 4684 tp->ups_info.eee_ckdiv = true; 4685 4686 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 4687 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV; 4688 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 4689 tp->ups_info.eee_cmod_lv = true; 4690 tp->ups_info._10m_ckdiv = true; 4691 tp->ups_info.eee_plloff_giga = true; 4692 4693 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); 4694 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5)); 4695 tp->ups_info._250m_ckdiv = true; 4696 4697 r8153_patch_request(tp, false); 4698 } 4699 4700 if (tp->eee_en) 4701 rtl_eee_enable(tp, true); 4702 4703 r8153_aldps_en(tp, true); 4704 r8152b_enable_fc(tp); 4705 4706 set_bit(PHY_RESET, &tp->flags); 4707 } 4708 4709 static void r8153_first_init(struct r8152 *tp) 4710 { 4711 u32 ocp_data; 4712 4713 r8153_mac_clk_spd(tp, false); 4714 rxdy_gated_en(tp, true); 4715 r8153_teredo_off(tp); 4716 4717 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 4718 ocp_data &= ~RCR_ACPT_ALL; 4719 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 4720 4721 rtl8152_nic_reset(tp); 4722 rtl_reset_bmu(tp); 4723 4724 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 4725 ocp_data &= ~NOW_IS_OOB; 4726 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 4727 4728 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 4729 ocp_data &= ~MCU_BORW_EN; 4730 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 4731 4732 wait_oob_link_list_ready(tp); 4733 4734 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 4735 ocp_data |= RE_INIT_LL; 4736 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 4737 4738 wait_oob_link_list_ready(tp); 4739 4740 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 4741 4742 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; 4743 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); 4744 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); 4745 4746 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); 4747 ocp_data |= TCR0_AUTO_FIFO; 4748 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); 4749 4750 rtl8152_nic_reset(tp); 4751 4752 /* rx share fifo credit full threshold */ 4753 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); 4754 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); 4755 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); 4756 /* TX share fifo free credit full threshold */ 4757 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); 4758 } 4759 4760 static void r8153_enter_oob(struct r8152 *tp) 4761 { 4762 u32 ocp_data; 4763 4764 r8153_mac_clk_spd(tp, true); 4765 4766 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 4767 ocp_data &= ~NOW_IS_OOB; 4768 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 4769 4770 rtl_disable(tp); 4771 rtl_reset_bmu(tp); 4772 4773 wait_oob_link_list_ready(tp); 4774 4775 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 4776 ocp_data |= RE_INIT_LL; 4777 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 4778 4779 wait_oob_link_list_ready(tp); 4780 4781 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; 4782 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); 4783 4784 switch (tp->version) { 4785 case RTL_VER_03: 4786 case RTL_VER_04: 4787 case RTL_VER_05: 4788 case RTL_VER_06: 4789 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); 4790 ocp_data &= ~TEREDO_WAKE_MASK; 4791 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); 4792 break; 4793 4794 case RTL_VER_08: 4795 case RTL_VER_09: 4796 /* Clear teredo wake event. bit[15:8] is the teredo wakeup 4797 * type. Set it to zero. bits[7:0] are the W1C bits about 4798 * the events. Set them to all 1 to clear them. 4799 */ 4800 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); 4801 break; 4802 4803 default: 4804 break; 4805 } 4806 4807 rtl_rx_vlan_en(tp, true); 4808 4809 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); 4810 ocp_data |= ALDPS_PROXY_MODE; 4811 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); 4812 4813 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 4814 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; 4815 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 4816 4817 rxdy_gated_en(tp, false); 4818 4819 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 4820 ocp_data |= RCR_APM | RCR_AM | RCR_AB; 4821 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 4822 } 4823 4824 static void rtl8153_disable(struct r8152 *tp) 4825 { 4826 r8153_aldps_en(tp, false); 4827 rtl_disable(tp); 4828 rtl_reset_bmu(tp); 4829 r8153_aldps_en(tp, true); 4830 } 4831 4832 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex, 4833 u32 advertising) 4834 { 4835 u16 bmcr; 4836 int ret = 0; 4837 4838 if (autoneg == AUTONEG_DISABLE) { 4839 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL) 4840 return -EINVAL; 4841 4842 switch (speed) { 4843 case SPEED_10: 4844 bmcr = BMCR_SPEED10; 4845 if (duplex == DUPLEX_FULL) { 4846 bmcr |= BMCR_FULLDPLX; 4847 tp->ups_info.speed_duplex = FORCE_10M_FULL; 4848 } else { 4849 tp->ups_info.speed_duplex = FORCE_10M_HALF; 4850 } 4851 break; 4852 case SPEED_100: 4853 bmcr = BMCR_SPEED100; 4854 if (duplex == DUPLEX_FULL) { 4855 bmcr |= BMCR_FULLDPLX; 4856 tp->ups_info.speed_duplex = FORCE_100M_FULL; 4857 } else { 4858 tp->ups_info.speed_duplex = FORCE_100M_HALF; 4859 } 4860 break; 4861 case SPEED_1000: 4862 if (tp->mii.supports_gmii) { 4863 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX; 4864 tp->ups_info.speed_duplex = NWAY_1000M_FULL; 4865 break; 4866 } 4867 /* fall through */ 4868 default: 4869 ret = -EINVAL; 4870 goto out; 4871 } 4872 4873 if (duplex == DUPLEX_FULL) 4874 tp->mii.full_duplex = 1; 4875 else 4876 tp->mii.full_duplex = 0; 4877 4878 tp->mii.force_media = 1; 4879 } else { 4880 u16 anar, tmp1; 4881 u32 support; 4882 4883 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | 4884 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; 4885 4886 if (tp->mii.supports_gmii) 4887 support |= RTL_ADVERTISED_1000_FULL; 4888 4889 if (!(advertising & support)) 4890 return -EINVAL; 4891 4892 anar = r8152_mdio_read(tp, MII_ADVERTISE); 4893 tmp1 = anar & ~(ADVERTISE_10HALF | ADVERTISE_10FULL | 4894 ADVERTISE_100HALF | ADVERTISE_100FULL); 4895 if (advertising & RTL_ADVERTISED_10_HALF) { 4896 tmp1 |= ADVERTISE_10HALF; 4897 tp->ups_info.speed_duplex = NWAY_10M_HALF; 4898 } 4899 if (advertising & RTL_ADVERTISED_10_FULL) { 4900 tmp1 |= ADVERTISE_10FULL; 4901 tp->ups_info.speed_duplex = NWAY_10M_FULL; 4902 } 4903 4904 if (advertising & RTL_ADVERTISED_100_HALF) { 4905 tmp1 |= ADVERTISE_100HALF; 4906 tp->ups_info.speed_duplex = NWAY_100M_HALF; 4907 } 4908 if (advertising & RTL_ADVERTISED_100_FULL) { 4909 tmp1 |= ADVERTISE_100FULL; 4910 tp->ups_info.speed_duplex = NWAY_100M_FULL; 4911 } 4912 4913 if (anar != tmp1) { 4914 r8152_mdio_write(tp, MII_ADVERTISE, tmp1); 4915 tp->mii.advertising = tmp1; 4916 } 4917 4918 if (tp->mii.supports_gmii) { 4919 u16 gbcr; 4920 4921 gbcr = r8152_mdio_read(tp, MII_CTRL1000); 4922 tmp1 = gbcr & ~(ADVERTISE_1000FULL | 4923 ADVERTISE_1000HALF); 4924 4925 if (advertising & RTL_ADVERTISED_1000_FULL) { 4926 tmp1 |= ADVERTISE_1000FULL; 4927 tp->ups_info.speed_duplex = NWAY_1000M_FULL; 4928 } 4929 4930 if (gbcr != tmp1) 4931 r8152_mdio_write(tp, MII_CTRL1000, tmp1); 4932 } 4933 4934 bmcr = BMCR_ANENABLE | BMCR_ANRESTART; 4935 4936 tp->mii.force_media = 0; 4937 } 4938 4939 if (test_and_clear_bit(PHY_RESET, &tp->flags)) 4940 bmcr |= BMCR_RESET; 4941 4942 r8152_mdio_write(tp, MII_BMCR, bmcr); 4943 4944 if (bmcr & BMCR_RESET) { 4945 int i; 4946 4947 for (i = 0; i < 50; i++) { 4948 msleep(20); 4949 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) 4950 break; 4951 } 4952 } 4953 4954 out: 4955 return ret; 4956 } 4957 4958 static void rtl8152_up(struct r8152 *tp) 4959 { 4960 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 4961 return; 4962 4963 r8152_aldps_en(tp, false); 4964 r8152b_exit_oob(tp); 4965 r8152_aldps_en(tp, true); 4966 } 4967 4968 static void rtl8152_down(struct r8152 *tp) 4969 { 4970 if (test_bit(RTL8152_UNPLUG, &tp->flags)) { 4971 rtl_drop_queued_tx(tp); 4972 return; 4973 } 4974 4975 r8152_power_cut_en(tp, false); 4976 r8152_aldps_en(tp, false); 4977 r8152b_enter_oob(tp); 4978 r8152_aldps_en(tp, true); 4979 } 4980 4981 static void rtl8153_up(struct r8152 *tp) 4982 { 4983 u32 ocp_data; 4984 4985 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 4986 return; 4987 4988 r8153_u1u2en(tp, false); 4989 r8153_u2p3en(tp, false); 4990 r8153_aldps_en(tp, false); 4991 r8153_first_init(tp); 4992 4993 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6); 4994 ocp_data |= LANWAKE_CLR_EN; 4995 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data); 4996 4997 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG); 4998 ocp_data &= ~LANWAKE_PIN; 4999 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data); 5000 5001 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1); 5002 ocp_data &= ~DELAY_PHY_PWR_CHG; 5003 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data); 5004 5005 r8153_aldps_en(tp, true); 5006 5007 switch (tp->version) { 5008 case RTL_VER_03: 5009 case RTL_VER_04: 5010 break; 5011 case RTL_VER_05: 5012 case RTL_VER_06: 5013 default: 5014 r8153_u2p3en(tp, true); 5015 break; 5016 } 5017 5018 r8153_u1u2en(tp, true); 5019 } 5020 5021 static void rtl8153_down(struct r8152 *tp) 5022 { 5023 u32 ocp_data; 5024 5025 if (test_bit(RTL8152_UNPLUG, &tp->flags)) { 5026 rtl_drop_queued_tx(tp); 5027 return; 5028 } 5029 5030 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6); 5031 ocp_data &= ~LANWAKE_CLR_EN; 5032 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data); 5033 5034 r8153_u1u2en(tp, false); 5035 r8153_u2p3en(tp, false); 5036 r8153_power_cut_en(tp, false); 5037 r8153_aldps_en(tp, false); 5038 r8153_enter_oob(tp); 5039 r8153_aldps_en(tp, true); 5040 } 5041 5042 static void rtl8153b_up(struct r8152 *tp) 5043 { 5044 u32 ocp_data; 5045 5046 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 5047 return; 5048 5049 r8153b_u1u2en(tp, false); 5050 r8153_u2p3en(tp, false); 5051 r8153_aldps_en(tp, false); 5052 5053 r8153_first_init(tp); 5054 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); 5055 5056 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 5057 ocp_data &= ~PLA_MCU_SPDWN_EN; 5058 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 5059 5060 r8153_aldps_en(tp, true); 5061 5062 if (tp->udev->speed != USB_SPEED_HIGH) 5063 r8153b_u1u2en(tp, true); 5064 } 5065 5066 static void rtl8153b_down(struct r8152 *tp) 5067 { 5068 u32 ocp_data; 5069 5070 if (test_bit(RTL8152_UNPLUG, &tp->flags)) { 5071 rtl_drop_queued_tx(tp); 5072 return; 5073 } 5074 5075 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 5076 ocp_data |= PLA_MCU_SPDWN_EN; 5077 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 5078 5079 r8153b_u1u2en(tp, false); 5080 r8153_u2p3en(tp, false); 5081 r8153b_power_cut_en(tp, false); 5082 r8153_aldps_en(tp, false); 5083 r8153_enter_oob(tp); 5084 r8153_aldps_en(tp, true); 5085 } 5086 5087 static bool rtl8152_in_nway(struct r8152 *tp) 5088 { 5089 u16 nway_state; 5090 5091 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000); 5092 tp->ocp_base = 0x2000; 5093 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */ 5094 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a); 5095 5096 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */ 5097 if (nway_state & 0xc000) 5098 return false; 5099 else 5100 return true; 5101 } 5102 5103 static bool rtl8153_in_nway(struct r8152 *tp) 5104 { 5105 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff; 5106 5107 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE) 5108 return false; 5109 else 5110 return true; 5111 } 5112 5113 static void set_carrier(struct r8152 *tp) 5114 { 5115 struct net_device *netdev = tp->netdev; 5116 struct napi_struct *napi = &tp->napi; 5117 u8 speed; 5118 5119 speed = rtl8152_get_speed(tp); 5120 5121 if (speed & LINK_STATUS) { 5122 if (!netif_carrier_ok(netdev)) { 5123 tp->rtl_ops.enable(tp); 5124 netif_stop_queue(netdev); 5125 napi_disable(napi); 5126 netif_carrier_on(netdev); 5127 rtl_start_rx(tp); 5128 clear_bit(RTL8152_SET_RX_MODE, &tp->flags); 5129 _rtl8152_set_rx_mode(netdev); 5130 napi_enable(&tp->napi); 5131 netif_wake_queue(netdev); 5132 netif_info(tp, link, netdev, "carrier on\n"); 5133 } else if (netif_queue_stopped(netdev) && 5134 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) { 5135 netif_wake_queue(netdev); 5136 } 5137 } else { 5138 if (netif_carrier_ok(netdev)) { 5139 netif_carrier_off(netdev); 5140 tasklet_disable(&tp->tx_tl); 5141 napi_disable(napi); 5142 tp->rtl_ops.disable(tp); 5143 napi_enable(napi); 5144 tasklet_enable(&tp->tx_tl); 5145 netif_info(tp, link, netdev, "carrier off\n"); 5146 } 5147 } 5148 } 5149 5150 static void rtl_work_func_t(struct work_struct *work) 5151 { 5152 struct r8152 *tp = container_of(work, struct r8152, schedule.work); 5153 5154 /* If the device is unplugged or !netif_running(), the workqueue 5155 * doesn't need to wake the device, and could return directly. 5156 */ 5157 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev)) 5158 return; 5159 5160 if (usb_autopm_get_interface(tp->intf) < 0) 5161 return; 5162 5163 if (!test_bit(WORK_ENABLE, &tp->flags)) 5164 goto out1; 5165 5166 if (!mutex_trylock(&tp->control)) { 5167 schedule_delayed_work(&tp->schedule, 0); 5168 goto out1; 5169 } 5170 5171 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags)) 5172 set_carrier(tp); 5173 5174 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags)) 5175 _rtl8152_set_rx_mode(tp->netdev); 5176 5177 /* don't schedule tasket before linking */ 5178 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) && 5179 netif_carrier_ok(tp->netdev)) 5180 tasklet_schedule(&tp->tx_tl); 5181 5182 mutex_unlock(&tp->control); 5183 5184 out1: 5185 usb_autopm_put_interface(tp->intf); 5186 } 5187 5188 static void rtl_hw_phy_work_func_t(struct work_struct *work) 5189 { 5190 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work); 5191 5192 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 5193 return; 5194 5195 if (usb_autopm_get_interface(tp->intf) < 0) 5196 return; 5197 5198 mutex_lock(&tp->control); 5199 5200 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) { 5201 tp->rtl_fw.retry = false; 5202 tp->rtl_fw.fw = NULL; 5203 5204 /* Delay execution in case request_firmware() is not ready yet. 5205 */ 5206 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10); 5207 goto ignore_once; 5208 } 5209 5210 tp->rtl_ops.hw_phy_cfg(tp); 5211 5212 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex, 5213 tp->advertising); 5214 5215 ignore_once: 5216 mutex_unlock(&tp->control); 5217 5218 usb_autopm_put_interface(tp->intf); 5219 } 5220 5221 #ifdef CONFIG_PM_SLEEP 5222 static int rtl_notifier(struct notifier_block *nb, unsigned long action, 5223 void *data) 5224 { 5225 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier); 5226 5227 switch (action) { 5228 case PM_HIBERNATION_PREPARE: 5229 case PM_SUSPEND_PREPARE: 5230 usb_autopm_get_interface(tp->intf); 5231 break; 5232 5233 case PM_POST_HIBERNATION: 5234 case PM_POST_SUSPEND: 5235 usb_autopm_put_interface(tp->intf); 5236 break; 5237 5238 case PM_POST_RESTORE: 5239 case PM_RESTORE_PREPARE: 5240 default: 5241 break; 5242 } 5243 5244 return NOTIFY_DONE; 5245 } 5246 #endif 5247 5248 static int rtl8152_open(struct net_device *netdev) 5249 { 5250 struct r8152 *tp = netdev_priv(netdev); 5251 int res = 0; 5252 5253 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) { 5254 cancel_delayed_work_sync(&tp->hw_phy_work); 5255 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work); 5256 } 5257 5258 res = alloc_all_mem(tp); 5259 if (res) 5260 goto out; 5261 5262 res = usb_autopm_get_interface(tp->intf); 5263 if (res < 0) 5264 goto out_free; 5265 5266 mutex_lock(&tp->control); 5267 5268 tp->rtl_ops.up(tp); 5269 5270 netif_carrier_off(netdev); 5271 netif_start_queue(netdev); 5272 set_bit(WORK_ENABLE, &tp->flags); 5273 5274 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); 5275 if (res) { 5276 if (res == -ENODEV) 5277 netif_device_detach(tp->netdev); 5278 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", 5279 res); 5280 goto out_unlock; 5281 } 5282 napi_enable(&tp->napi); 5283 tasklet_enable(&tp->tx_tl); 5284 5285 mutex_unlock(&tp->control); 5286 5287 usb_autopm_put_interface(tp->intf); 5288 #ifdef CONFIG_PM_SLEEP 5289 tp->pm_notifier.notifier_call = rtl_notifier; 5290 register_pm_notifier(&tp->pm_notifier); 5291 #endif 5292 return 0; 5293 5294 out_unlock: 5295 mutex_unlock(&tp->control); 5296 usb_autopm_put_interface(tp->intf); 5297 out_free: 5298 free_all_mem(tp); 5299 out: 5300 return res; 5301 } 5302 5303 static int rtl8152_close(struct net_device *netdev) 5304 { 5305 struct r8152 *tp = netdev_priv(netdev); 5306 int res = 0; 5307 5308 #ifdef CONFIG_PM_SLEEP 5309 unregister_pm_notifier(&tp->pm_notifier); 5310 #endif 5311 tasklet_disable(&tp->tx_tl); 5312 clear_bit(WORK_ENABLE, &tp->flags); 5313 usb_kill_urb(tp->intr_urb); 5314 cancel_delayed_work_sync(&tp->schedule); 5315 napi_disable(&tp->napi); 5316 netif_stop_queue(netdev); 5317 5318 res = usb_autopm_get_interface(tp->intf); 5319 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) { 5320 rtl_drop_queued_tx(tp); 5321 rtl_stop_rx(tp); 5322 } else { 5323 mutex_lock(&tp->control); 5324 5325 tp->rtl_ops.down(tp); 5326 5327 mutex_unlock(&tp->control); 5328 5329 usb_autopm_put_interface(tp->intf); 5330 } 5331 5332 free_all_mem(tp); 5333 5334 return res; 5335 } 5336 5337 static void rtl_tally_reset(struct r8152 *tp) 5338 { 5339 u32 ocp_data; 5340 5341 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY); 5342 ocp_data |= TALLY_RESET; 5343 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data); 5344 } 5345 5346 static void r8152b_init(struct r8152 *tp) 5347 { 5348 u32 ocp_data; 5349 u16 data; 5350 5351 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 5352 return; 5353 5354 data = r8152_mdio_read(tp, MII_BMCR); 5355 if (data & BMCR_PDOWN) { 5356 data &= ~BMCR_PDOWN; 5357 r8152_mdio_write(tp, MII_BMCR, data); 5358 } 5359 5360 r8152_aldps_en(tp, false); 5361 5362 if (tp->version == RTL_VER_01) { 5363 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); 5364 ocp_data &= ~LED_MODE_MASK; 5365 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); 5366 } 5367 5368 r8152_power_cut_en(tp, false); 5369 5370 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 5371 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; 5372 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 5373 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); 5374 ocp_data &= ~MCU_CLK_RATIO_MASK; 5375 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; 5376 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); 5377 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | 5378 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; 5379 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); 5380 5381 rtl_tally_reset(tp); 5382 5383 /* enable rx aggregation */ 5384 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 5385 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 5386 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 5387 } 5388 5389 static void r8153_init(struct r8152 *tp) 5390 { 5391 u32 ocp_data; 5392 u16 data; 5393 int i; 5394 5395 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 5396 return; 5397 5398 r8153_u1u2en(tp, false); 5399 5400 for (i = 0; i < 500; i++) { 5401 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 5402 AUTOLOAD_DONE) 5403 break; 5404 msleep(20); 5405 } 5406 5407 data = r8153_phy_status(tp, 0); 5408 5409 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || 5410 tp->version == RTL_VER_05) 5411 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); 5412 5413 data = r8152_mdio_read(tp, MII_BMCR); 5414 if (data & BMCR_PDOWN) { 5415 data &= ~BMCR_PDOWN; 5416 r8152_mdio_write(tp, MII_BMCR, data); 5417 } 5418 5419 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 5420 5421 r8153_u2p3en(tp, false); 5422 5423 if (tp->version == RTL_VER_04) { 5424 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2); 5425 ocp_data &= ~pwd_dn_scale_mask; 5426 ocp_data |= pwd_dn_scale(96); 5427 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data); 5428 5429 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); 5430 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; 5431 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); 5432 } else if (tp->version == RTL_VER_05) { 5433 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0); 5434 ocp_data &= ~ECM_ALDPS; 5435 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data); 5436 5437 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); 5438 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) 5439 ocp_data &= ~DYNAMIC_BURST; 5440 else 5441 ocp_data |= DYNAMIC_BURST; 5442 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); 5443 } else if (tp->version == RTL_VER_06) { 5444 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); 5445 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) 5446 ocp_data &= ~DYNAMIC_BURST; 5447 else 5448 ocp_data |= DYNAMIC_BURST; 5449 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); 5450 5451 r8153_queue_wake(tp, false); 5452 5453 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 5454 if (rtl8152_get_speed(tp) & LINK_STATUS) 5455 ocp_data |= CUR_LINK_OK; 5456 else 5457 ocp_data &= ~CUR_LINK_OK; 5458 ocp_data |= POLL_LINK_CHG; 5459 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 5460 } 5461 5462 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2); 5463 ocp_data |= EP4_FULL_FC; 5464 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data); 5465 5466 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); 5467 ocp_data &= ~TIMER11_EN; 5468 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); 5469 5470 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); 5471 ocp_data &= ~LED_MODE_MASK; 5472 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); 5473 5474 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; 5475 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER) 5476 ocp_data |= LPM_TIMER_500MS; 5477 else 5478 ocp_data |= LPM_TIMER_500US; 5479 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); 5480 5481 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); 5482 ocp_data &= ~SEN_VAL_MASK; 5483 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; 5484 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); 5485 5486 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); 5487 5488 r8153_power_cut_en(tp, false); 5489 rtl_runtime_suspend_enable(tp, false); 5490 r8153_u1u2en(tp, true); 5491 r8153_mac_clk_spd(tp, false); 5492 usb_enable_lpm(tp->udev); 5493 5494 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6); 5495 ocp_data |= LANWAKE_CLR_EN; 5496 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data); 5497 5498 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG); 5499 ocp_data &= ~LANWAKE_PIN; 5500 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data); 5501 5502 /* rx aggregation */ 5503 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 5504 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 5505 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags)) 5506 ocp_data |= RX_AGG_DISABLE; 5507 5508 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 5509 5510 rtl_tally_reset(tp); 5511 5512 switch (tp->udev->speed) { 5513 case USB_SPEED_SUPER: 5514 case USB_SPEED_SUPER_PLUS: 5515 tp->coalesce = COALESCE_SUPER; 5516 break; 5517 case USB_SPEED_HIGH: 5518 tp->coalesce = COALESCE_HIGH; 5519 break; 5520 default: 5521 tp->coalesce = COALESCE_SLOW; 5522 break; 5523 } 5524 } 5525 5526 static void r8153b_init(struct r8152 *tp) 5527 { 5528 u32 ocp_data; 5529 u16 data; 5530 int i; 5531 5532 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 5533 return; 5534 5535 r8153b_u1u2en(tp, false); 5536 5537 for (i = 0; i < 500; i++) { 5538 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 5539 AUTOLOAD_DONE) 5540 break; 5541 msleep(20); 5542 } 5543 5544 data = r8153_phy_status(tp, 0); 5545 5546 data = r8152_mdio_read(tp, MII_BMCR); 5547 if (data & BMCR_PDOWN) { 5548 data &= ~BMCR_PDOWN; 5549 r8152_mdio_write(tp, MII_BMCR, data); 5550 } 5551 5552 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 5553 5554 r8153_u2p3en(tp, false); 5555 5556 /* MSC timer = 0xfff * 8ms = 32760 ms */ 5557 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 5558 5559 /* U1/U2/L1 idle timer. 500 us */ 5560 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); 5561 5562 r8153b_power_cut_en(tp, false); 5563 r8153b_ups_en(tp, false); 5564 r8153_queue_wake(tp, false); 5565 rtl_runtime_suspend_enable(tp, false); 5566 5567 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 5568 if (rtl8152_get_speed(tp) & LINK_STATUS) 5569 ocp_data |= CUR_LINK_OK; 5570 else 5571 ocp_data &= ~CUR_LINK_OK; 5572 ocp_data |= POLL_LINK_CHG; 5573 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 5574 5575 if (tp->udev->speed != USB_SPEED_HIGH) 5576 r8153b_u1u2en(tp, true); 5577 usb_enable_lpm(tp->udev); 5578 5579 /* MAC clock speed down */ 5580 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); 5581 ocp_data |= MAC_CLK_SPDWN_EN; 5582 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); 5583 5584 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 5585 ocp_data &= ~PLA_MCU_SPDWN_EN; 5586 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 5587 5588 if (tp->version == RTL_VER_09) { 5589 /* Disable Test IO for 32QFN */ 5590 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) { 5591 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 5592 ocp_data |= TEST_IO_OFF; 5593 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 5594 } 5595 } 5596 5597 set_bit(GREEN_ETHERNET, &tp->flags); 5598 5599 /* rx aggregation */ 5600 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 5601 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 5602 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 5603 5604 rtl_tally_reset(tp); 5605 5606 tp->coalesce = 15000; /* 15 us */ 5607 } 5608 5609 static int rtl8152_pre_reset(struct usb_interface *intf) 5610 { 5611 struct r8152 *tp = usb_get_intfdata(intf); 5612 struct net_device *netdev; 5613 5614 if (!tp) 5615 return 0; 5616 5617 netdev = tp->netdev; 5618 if (!netif_running(netdev)) 5619 return 0; 5620 5621 netif_stop_queue(netdev); 5622 tasklet_disable(&tp->tx_tl); 5623 clear_bit(WORK_ENABLE, &tp->flags); 5624 usb_kill_urb(tp->intr_urb); 5625 cancel_delayed_work_sync(&tp->schedule); 5626 napi_disable(&tp->napi); 5627 if (netif_carrier_ok(netdev)) { 5628 mutex_lock(&tp->control); 5629 tp->rtl_ops.disable(tp); 5630 mutex_unlock(&tp->control); 5631 } 5632 5633 return 0; 5634 } 5635 5636 static int rtl8152_post_reset(struct usb_interface *intf) 5637 { 5638 struct r8152 *tp = usb_get_intfdata(intf); 5639 struct net_device *netdev; 5640 struct sockaddr sa; 5641 5642 if (!tp) 5643 return 0; 5644 5645 /* reset the MAC adddress in case of policy change */ 5646 if (determine_ethernet_addr(tp, &sa) >= 0) { 5647 rtnl_lock(); 5648 dev_set_mac_address (tp->netdev, &sa, NULL); 5649 rtnl_unlock(); 5650 } 5651 5652 netdev = tp->netdev; 5653 if (!netif_running(netdev)) 5654 return 0; 5655 5656 set_bit(WORK_ENABLE, &tp->flags); 5657 if (netif_carrier_ok(netdev)) { 5658 mutex_lock(&tp->control); 5659 tp->rtl_ops.enable(tp); 5660 rtl_start_rx(tp); 5661 _rtl8152_set_rx_mode(netdev); 5662 mutex_unlock(&tp->control); 5663 } 5664 5665 napi_enable(&tp->napi); 5666 tasklet_enable(&tp->tx_tl); 5667 netif_wake_queue(netdev); 5668 usb_submit_urb(tp->intr_urb, GFP_KERNEL); 5669 5670 if (!list_empty(&tp->rx_done)) 5671 napi_schedule(&tp->napi); 5672 5673 return 0; 5674 } 5675 5676 static bool delay_autosuspend(struct r8152 *tp) 5677 { 5678 bool sw_linking = !!netif_carrier_ok(tp->netdev); 5679 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS); 5680 5681 /* This means a linking change occurs and the driver doesn't detect it, 5682 * yet. If the driver has disabled tx/rx and hw is linking on, the 5683 * device wouldn't wake up by receiving any packet. 5684 */ 5685 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking) 5686 return true; 5687 5688 /* If the linking down is occurred by nway, the device may miss the 5689 * linking change event. And it wouldn't wake when linking on. 5690 */ 5691 if (!sw_linking && tp->rtl_ops.in_nway(tp)) 5692 return true; 5693 else if (!skb_queue_empty(&tp->tx_queue)) 5694 return true; 5695 else 5696 return false; 5697 } 5698 5699 static int rtl8152_runtime_resume(struct r8152 *tp) 5700 { 5701 struct net_device *netdev = tp->netdev; 5702 5703 if (netif_running(netdev) && netdev->flags & IFF_UP) { 5704 struct napi_struct *napi = &tp->napi; 5705 5706 tp->rtl_ops.autosuspend_en(tp, false); 5707 napi_disable(napi); 5708 set_bit(WORK_ENABLE, &tp->flags); 5709 5710 if (netif_carrier_ok(netdev)) { 5711 if (rtl8152_get_speed(tp) & LINK_STATUS) { 5712 rtl_start_rx(tp); 5713 } else { 5714 netif_carrier_off(netdev); 5715 tp->rtl_ops.disable(tp); 5716 netif_info(tp, link, netdev, "linking down\n"); 5717 } 5718 } 5719 5720 napi_enable(napi); 5721 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 5722 smp_mb__after_atomic(); 5723 5724 if (!list_empty(&tp->rx_done)) 5725 napi_schedule(&tp->napi); 5726 5727 usb_submit_urb(tp->intr_urb, GFP_NOIO); 5728 } else { 5729 if (netdev->flags & IFF_UP) 5730 tp->rtl_ops.autosuspend_en(tp, false); 5731 5732 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 5733 } 5734 5735 return 0; 5736 } 5737 5738 static int rtl8152_system_resume(struct r8152 *tp) 5739 { 5740 struct net_device *netdev = tp->netdev; 5741 5742 netif_device_attach(netdev); 5743 5744 if (netif_running(netdev) && (netdev->flags & IFF_UP)) { 5745 tp->rtl_ops.up(tp); 5746 netif_carrier_off(netdev); 5747 set_bit(WORK_ENABLE, &tp->flags); 5748 usb_submit_urb(tp->intr_urb, GFP_NOIO); 5749 } 5750 5751 return 0; 5752 } 5753 5754 static int rtl8152_runtime_suspend(struct r8152 *tp) 5755 { 5756 struct net_device *netdev = tp->netdev; 5757 int ret = 0; 5758 5759 set_bit(SELECTIVE_SUSPEND, &tp->flags); 5760 smp_mb__after_atomic(); 5761 5762 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { 5763 u32 rcr = 0; 5764 5765 if (netif_carrier_ok(netdev)) { 5766 u32 ocp_data; 5767 5768 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 5769 ocp_data = rcr & ~RCR_ACPT_ALL; 5770 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 5771 rxdy_gated_en(tp, true); 5772 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, 5773 PLA_OOB_CTRL); 5774 if (!(ocp_data & RXFIFO_EMPTY)) { 5775 rxdy_gated_en(tp, false); 5776 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); 5777 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 5778 smp_mb__after_atomic(); 5779 ret = -EBUSY; 5780 goto out1; 5781 } 5782 } 5783 5784 clear_bit(WORK_ENABLE, &tp->flags); 5785 usb_kill_urb(tp->intr_urb); 5786 5787 tp->rtl_ops.autosuspend_en(tp, true); 5788 5789 if (netif_carrier_ok(netdev)) { 5790 struct napi_struct *napi = &tp->napi; 5791 5792 napi_disable(napi); 5793 rtl_stop_rx(tp); 5794 rxdy_gated_en(tp, false); 5795 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); 5796 napi_enable(napi); 5797 } 5798 5799 if (delay_autosuspend(tp)) { 5800 rtl8152_runtime_resume(tp); 5801 ret = -EBUSY; 5802 } 5803 } 5804 5805 out1: 5806 return ret; 5807 } 5808 5809 static int rtl8152_system_suspend(struct r8152 *tp) 5810 { 5811 struct net_device *netdev = tp->netdev; 5812 5813 netif_device_detach(netdev); 5814 5815 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { 5816 struct napi_struct *napi = &tp->napi; 5817 5818 clear_bit(WORK_ENABLE, &tp->flags); 5819 usb_kill_urb(tp->intr_urb); 5820 tasklet_disable(&tp->tx_tl); 5821 napi_disable(napi); 5822 cancel_delayed_work_sync(&tp->schedule); 5823 tp->rtl_ops.down(tp); 5824 napi_enable(napi); 5825 tasklet_enable(&tp->tx_tl); 5826 } 5827 5828 return 0; 5829 } 5830 5831 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) 5832 { 5833 struct r8152 *tp = usb_get_intfdata(intf); 5834 int ret; 5835 5836 mutex_lock(&tp->control); 5837 5838 if (PMSG_IS_AUTO(message)) 5839 ret = rtl8152_runtime_suspend(tp); 5840 else 5841 ret = rtl8152_system_suspend(tp); 5842 5843 mutex_unlock(&tp->control); 5844 5845 return ret; 5846 } 5847 5848 static int rtl8152_resume(struct usb_interface *intf) 5849 { 5850 struct r8152 *tp = usb_get_intfdata(intf); 5851 int ret; 5852 5853 mutex_lock(&tp->control); 5854 5855 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) 5856 ret = rtl8152_runtime_resume(tp); 5857 else 5858 ret = rtl8152_system_resume(tp); 5859 5860 mutex_unlock(&tp->control); 5861 5862 return ret; 5863 } 5864 5865 static int rtl8152_reset_resume(struct usb_interface *intf) 5866 { 5867 struct r8152 *tp = usb_get_intfdata(intf); 5868 5869 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 5870 tp->rtl_ops.init(tp); 5871 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); 5872 set_ethernet_addr(tp); 5873 return rtl8152_resume(intf); 5874 } 5875 5876 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 5877 { 5878 struct r8152 *tp = netdev_priv(dev); 5879 5880 if (usb_autopm_get_interface(tp->intf) < 0) 5881 return; 5882 5883 if (!rtl_can_wakeup(tp)) { 5884 wol->supported = 0; 5885 wol->wolopts = 0; 5886 } else { 5887 mutex_lock(&tp->control); 5888 wol->supported = WAKE_ANY; 5889 wol->wolopts = __rtl_get_wol(tp); 5890 mutex_unlock(&tp->control); 5891 } 5892 5893 usb_autopm_put_interface(tp->intf); 5894 } 5895 5896 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 5897 { 5898 struct r8152 *tp = netdev_priv(dev); 5899 int ret; 5900 5901 if (!rtl_can_wakeup(tp)) 5902 return -EOPNOTSUPP; 5903 5904 if (wol->wolopts & ~WAKE_ANY) 5905 return -EINVAL; 5906 5907 ret = usb_autopm_get_interface(tp->intf); 5908 if (ret < 0) 5909 goto out_set_wol; 5910 5911 mutex_lock(&tp->control); 5912 5913 __rtl_set_wol(tp, wol->wolopts); 5914 tp->saved_wolopts = wol->wolopts & WAKE_ANY; 5915 5916 mutex_unlock(&tp->control); 5917 5918 usb_autopm_put_interface(tp->intf); 5919 5920 out_set_wol: 5921 return ret; 5922 } 5923 5924 static u32 rtl8152_get_msglevel(struct net_device *dev) 5925 { 5926 struct r8152 *tp = netdev_priv(dev); 5927 5928 return tp->msg_enable; 5929 } 5930 5931 static void rtl8152_set_msglevel(struct net_device *dev, u32 value) 5932 { 5933 struct r8152 *tp = netdev_priv(dev); 5934 5935 tp->msg_enable = value; 5936 } 5937 5938 static void rtl8152_get_drvinfo(struct net_device *netdev, 5939 struct ethtool_drvinfo *info) 5940 { 5941 struct r8152 *tp = netdev_priv(netdev); 5942 5943 strlcpy(info->driver, MODULENAME, sizeof(info->driver)); 5944 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); 5945 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); 5946 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw)) 5947 strlcpy(info->fw_version, tp->rtl_fw.version, 5948 sizeof(info->fw_version)); 5949 } 5950 5951 static 5952 int rtl8152_get_link_ksettings(struct net_device *netdev, 5953 struct ethtool_link_ksettings *cmd) 5954 { 5955 struct r8152 *tp = netdev_priv(netdev); 5956 int ret; 5957 5958 if (!tp->mii.mdio_read) 5959 return -EOPNOTSUPP; 5960 5961 ret = usb_autopm_get_interface(tp->intf); 5962 if (ret < 0) 5963 goto out; 5964 5965 mutex_lock(&tp->control); 5966 5967 mii_ethtool_get_link_ksettings(&tp->mii, cmd); 5968 5969 mutex_unlock(&tp->control); 5970 5971 usb_autopm_put_interface(tp->intf); 5972 5973 out: 5974 return ret; 5975 } 5976 5977 static int rtl8152_set_link_ksettings(struct net_device *dev, 5978 const struct ethtool_link_ksettings *cmd) 5979 { 5980 struct r8152 *tp = netdev_priv(dev); 5981 u32 advertising = 0; 5982 int ret; 5983 5984 ret = usb_autopm_get_interface(tp->intf); 5985 if (ret < 0) 5986 goto out; 5987 5988 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, 5989 cmd->link_modes.advertising)) 5990 advertising |= RTL_ADVERTISED_10_HALF; 5991 5992 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, 5993 cmd->link_modes.advertising)) 5994 advertising |= RTL_ADVERTISED_10_FULL; 5995 5996 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, 5997 cmd->link_modes.advertising)) 5998 advertising |= RTL_ADVERTISED_100_HALF; 5999 6000 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 6001 cmd->link_modes.advertising)) 6002 advertising |= RTL_ADVERTISED_100_FULL; 6003 6004 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 6005 cmd->link_modes.advertising)) 6006 advertising |= RTL_ADVERTISED_1000_HALF; 6007 6008 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 6009 cmd->link_modes.advertising)) 6010 advertising |= RTL_ADVERTISED_1000_FULL; 6011 6012 mutex_lock(&tp->control); 6013 6014 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed, 6015 cmd->base.duplex, advertising); 6016 if (!ret) { 6017 tp->autoneg = cmd->base.autoneg; 6018 tp->speed = cmd->base.speed; 6019 tp->duplex = cmd->base.duplex; 6020 tp->advertising = advertising; 6021 } 6022 6023 mutex_unlock(&tp->control); 6024 6025 usb_autopm_put_interface(tp->intf); 6026 6027 out: 6028 return ret; 6029 } 6030 6031 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = { 6032 "tx_packets", 6033 "rx_packets", 6034 "tx_errors", 6035 "rx_errors", 6036 "rx_missed", 6037 "align_errors", 6038 "tx_single_collisions", 6039 "tx_multi_collisions", 6040 "rx_unicast", 6041 "rx_broadcast", 6042 "rx_multicast", 6043 "tx_aborted", 6044 "tx_underrun", 6045 }; 6046 6047 static int rtl8152_get_sset_count(struct net_device *dev, int sset) 6048 { 6049 switch (sset) { 6050 case ETH_SS_STATS: 6051 return ARRAY_SIZE(rtl8152_gstrings); 6052 default: 6053 return -EOPNOTSUPP; 6054 } 6055 } 6056 6057 static void rtl8152_get_ethtool_stats(struct net_device *dev, 6058 struct ethtool_stats *stats, u64 *data) 6059 { 6060 struct r8152 *tp = netdev_priv(dev); 6061 struct tally_counter tally; 6062 6063 if (usb_autopm_get_interface(tp->intf) < 0) 6064 return; 6065 6066 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA); 6067 6068 usb_autopm_put_interface(tp->intf); 6069 6070 data[0] = le64_to_cpu(tally.tx_packets); 6071 data[1] = le64_to_cpu(tally.rx_packets); 6072 data[2] = le64_to_cpu(tally.tx_errors); 6073 data[3] = le32_to_cpu(tally.rx_errors); 6074 data[4] = le16_to_cpu(tally.rx_missed); 6075 data[5] = le16_to_cpu(tally.align_errors); 6076 data[6] = le32_to_cpu(tally.tx_one_collision); 6077 data[7] = le32_to_cpu(tally.tx_multi_collision); 6078 data[8] = le64_to_cpu(tally.rx_unicast); 6079 data[9] = le64_to_cpu(tally.rx_broadcast); 6080 data[10] = le32_to_cpu(tally.rx_multicast); 6081 data[11] = le16_to_cpu(tally.tx_aborted); 6082 data[12] = le16_to_cpu(tally.tx_underrun); 6083 } 6084 6085 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data) 6086 { 6087 switch (stringset) { 6088 case ETH_SS_STATS: 6089 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings)); 6090 break; 6091 } 6092 } 6093 6094 static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee) 6095 { 6096 u32 lp, adv, supported = 0; 6097 u16 val; 6098 6099 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 6100 supported = mmd_eee_cap_to_ethtool_sup_t(val); 6101 6102 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 6103 adv = mmd_eee_adv_to_ethtool_adv_t(val); 6104 6105 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); 6106 lp = mmd_eee_adv_to_ethtool_adv_t(val); 6107 6108 eee->eee_enabled = tp->eee_en; 6109 eee->eee_active = !!(supported & adv & lp); 6110 eee->supported = supported; 6111 eee->advertised = tp->eee_adv; 6112 eee->lp_advertised = lp; 6113 6114 return 0; 6115 } 6116 6117 static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee) 6118 { 6119 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); 6120 6121 tp->eee_en = eee->eee_enabled; 6122 tp->eee_adv = val; 6123 6124 rtl_eee_enable(tp, tp->eee_en); 6125 6126 return 0; 6127 } 6128 6129 static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee) 6130 { 6131 u32 lp, adv, supported = 0; 6132 u16 val; 6133 6134 val = ocp_reg_read(tp, OCP_EEE_ABLE); 6135 supported = mmd_eee_cap_to_ethtool_sup_t(val); 6136 6137 val = ocp_reg_read(tp, OCP_EEE_ADV); 6138 adv = mmd_eee_adv_to_ethtool_adv_t(val); 6139 6140 val = ocp_reg_read(tp, OCP_EEE_LPABLE); 6141 lp = mmd_eee_adv_to_ethtool_adv_t(val); 6142 6143 eee->eee_enabled = tp->eee_en; 6144 eee->eee_active = !!(supported & adv & lp); 6145 eee->supported = supported; 6146 eee->advertised = tp->eee_adv; 6147 eee->lp_advertised = lp; 6148 6149 return 0; 6150 } 6151 6152 static int 6153 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata) 6154 { 6155 struct r8152 *tp = netdev_priv(net); 6156 int ret; 6157 6158 ret = usb_autopm_get_interface(tp->intf); 6159 if (ret < 0) 6160 goto out; 6161 6162 mutex_lock(&tp->control); 6163 6164 ret = tp->rtl_ops.eee_get(tp, edata); 6165 6166 mutex_unlock(&tp->control); 6167 6168 usb_autopm_put_interface(tp->intf); 6169 6170 out: 6171 return ret; 6172 } 6173 6174 static int 6175 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata) 6176 { 6177 struct r8152 *tp = netdev_priv(net); 6178 int ret; 6179 6180 ret = usb_autopm_get_interface(tp->intf); 6181 if (ret < 0) 6182 goto out; 6183 6184 mutex_lock(&tp->control); 6185 6186 ret = tp->rtl_ops.eee_set(tp, edata); 6187 if (!ret) 6188 ret = mii_nway_restart(&tp->mii); 6189 6190 mutex_unlock(&tp->control); 6191 6192 usb_autopm_put_interface(tp->intf); 6193 6194 out: 6195 return ret; 6196 } 6197 6198 static int rtl8152_nway_reset(struct net_device *dev) 6199 { 6200 struct r8152 *tp = netdev_priv(dev); 6201 int ret; 6202 6203 ret = usb_autopm_get_interface(tp->intf); 6204 if (ret < 0) 6205 goto out; 6206 6207 mutex_lock(&tp->control); 6208 6209 ret = mii_nway_restart(&tp->mii); 6210 6211 mutex_unlock(&tp->control); 6212 6213 usb_autopm_put_interface(tp->intf); 6214 6215 out: 6216 return ret; 6217 } 6218 6219 static int rtl8152_get_coalesce(struct net_device *netdev, 6220 struct ethtool_coalesce *coalesce) 6221 { 6222 struct r8152 *tp = netdev_priv(netdev); 6223 6224 switch (tp->version) { 6225 case RTL_VER_01: 6226 case RTL_VER_02: 6227 case RTL_VER_07: 6228 return -EOPNOTSUPP; 6229 default: 6230 break; 6231 } 6232 6233 coalesce->rx_coalesce_usecs = tp->coalesce; 6234 6235 return 0; 6236 } 6237 6238 static int rtl8152_set_coalesce(struct net_device *netdev, 6239 struct ethtool_coalesce *coalesce) 6240 { 6241 struct r8152 *tp = netdev_priv(netdev); 6242 int ret; 6243 6244 switch (tp->version) { 6245 case RTL_VER_01: 6246 case RTL_VER_02: 6247 case RTL_VER_07: 6248 return -EOPNOTSUPP; 6249 default: 6250 break; 6251 } 6252 6253 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW) 6254 return -EINVAL; 6255 6256 ret = usb_autopm_get_interface(tp->intf); 6257 if (ret < 0) 6258 return ret; 6259 6260 mutex_lock(&tp->control); 6261 6262 if (tp->coalesce != coalesce->rx_coalesce_usecs) { 6263 tp->coalesce = coalesce->rx_coalesce_usecs; 6264 6265 if (netif_running(netdev) && netif_carrier_ok(netdev)) { 6266 netif_stop_queue(netdev); 6267 napi_disable(&tp->napi); 6268 tp->rtl_ops.disable(tp); 6269 tp->rtl_ops.enable(tp); 6270 rtl_start_rx(tp); 6271 clear_bit(RTL8152_SET_RX_MODE, &tp->flags); 6272 _rtl8152_set_rx_mode(netdev); 6273 napi_enable(&tp->napi); 6274 netif_wake_queue(netdev); 6275 } 6276 } 6277 6278 mutex_unlock(&tp->control); 6279 6280 usb_autopm_put_interface(tp->intf); 6281 6282 return ret; 6283 } 6284 6285 static int rtl8152_get_tunable(struct net_device *netdev, 6286 const struct ethtool_tunable *tunable, void *d) 6287 { 6288 struct r8152 *tp = netdev_priv(netdev); 6289 6290 switch (tunable->id) { 6291 case ETHTOOL_RX_COPYBREAK: 6292 *(u32 *)d = tp->rx_copybreak; 6293 break; 6294 default: 6295 return -EOPNOTSUPP; 6296 } 6297 6298 return 0; 6299 } 6300 6301 static int rtl8152_set_tunable(struct net_device *netdev, 6302 const struct ethtool_tunable *tunable, 6303 const void *d) 6304 { 6305 struct r8152 *tp = netdev_priv(netdev); 6306 u32 val; 6307 6308 switch (tunable->id) { 6309 case ETHTOOL_RX_COPYBREAK: 6310 val = *(u32 *)d; 6311 if (val < ETH_ZLEN) { 6312 netif_err(tp, rx_err, netdev, 6313 "Invalid rx copy break value\n"); 6314 return -EINVAL; 6315 } 6316 6317 if (tp->rx_copybreak != val) { 6318 if (netdev->flags & IFF_UP) { 6319 mutex_lock(&tp->control); 6320 napi_disable(&tp->napi); 6321 tp->rx_copybreak = val; 6322 napi_enable(&tp->napi); 6323 mutex_unlock(&tp->control); 6324 } else { 6325 tp->rx_copybreak = val; 6326 } 6327 } 6328 break; 6329 default: 6330 return -EOPNOTSUPP; 6331 } 6332 6333 return 0; 6334 } 6335 6336 static void rtl8152_get_ringparam(struct net_device *netdev, 6337 struct ethtool_ringparam *ring) 6338 { 6339 struct r8152 *tp = netdev_priv(netdev); 6340 6341 ring->rx_max_pending = RTL8152_RX_MAX_PENDING; 6342 ring->rx_pending = tp->rx_pending; 6343 } 6344 6345 static int rtl8152_set_ringparam(struct net_device *netdev, 6346 struct ethtool_ringparam *ring) 6347 { 6348 struct r8152 *tp = netdev_priv(netdev); 6349 6350 if (ring->rx_pending < (RTL8152_MAX_RX * 2)) 6351 return -EINVAL; 6352 6353 if (tp->rx_pending != ring->rx_pending) { 6354 if (netdev->flags & IFF_UP) { 6355 mutex_lock(&tp->control); 6356 napi_disable(&tp->napi); 6357 tp->rx_pending = ring->rx_pending; 6358 napi_enable(&tp->napi); 6359 mutex_unlock(&tp->control); 6360 } else { 6361 tp->rx_pending = ring->rx_pending; 6362 } 6363 } 6364 6365 return 0; 6366 } 6367 6368 static const struct ethtool_ops ops = { 6369 .get_drvinfo = rtl8152_get_drvinfo, 6370 .get_link = ethtool_op_get_link, 6371 .nway_reset = rtl8152_nway_reset, 6372 .get_msglevel = rtl8152_get_msglevel, 6373 .set_msglevel = rtl8152_set_msglevel, 6374 .get_wol = rtl8152_get_wol, 6375 .set_wol = rtl8152_set_wol, 6376 .get_strings = rtl8152_get_strings, 6377 .get_sset_count = rtl8152_get_sset_count, 6378 .get_ethtool_stats = rtl8152_get_ethtool_stats, 6379 .get_coalesce = rtl8152_get_coalesce, 6380 .set_coalesce = rtl8152_set_coalesce, 6381 .get_eee = rtl_ethtool_get_eee, 6382 .set_eee = rtl_ethtool_set_eee, 6383 .get_link_ksettings = rtl8152_get_link_ksettings, 6384 .set_link_ksettings = rtl8152_set_link_ksettings, 6385 .get_tunable = rtl8152_get_tunable, 6386 .set_tunable = rtl8152_set_tunable, 6387 .get_ringparam = rtl8152_get_ringparam, 6388 .set_ringparam = rtl8152_set_ringparam, 6389 }; 6390 6391 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) 6392 { 6393 struct r8152 *tp = netdev_priv(netdev); 6394 struct mii_ioctl_data *data = if_mii(rq); 6395 int res; 6396 6397 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6398 return -ENODEV; 6399 6400 res = usb_autopm_get_interface(tp->intf); 6401 if (res < 0) 6402 goto out; 6403 6404 switch (cmd) { 6405 case SIOCGMIIPHY: 6406 data->phy_id = R8152_PHY_ID; /* Internal PHY */ 6407 break; 6408 6409 case SIOCGMIIREG: 6410 mutex_lock(&tp->control); 6411 data->val_out = r8152_mdio_read(tp, data->reg_num); 6412 mutex_unlock(&tp->control); 6413 break; 6414 6415 case SIOCSMIIREG: 6416 if (!capable(CAP_NET_ADMIN)) { 6417 res = -EPERM; 6418 break; 6419 } 6420 mutex_lock(&tp->control); 6421 r8152_mdio_write(tp, data->reg_num, data->val_in); 6422 mutex_unlock(&tp->control); 6423 break; 6424 6425 default: 6426 res = -EOPNOTSUPP; 6427 } 6428 6429 usb_autopm_put_interface(tp->intf); 6430 6431 out: 6432 return res; 6433 } 6434 6435 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu) 6436 { 6437 struct r8152 *tp = netdev_priv(dev); 6438 int ret; 6439 6440 switch (tp->version) { 6441 case RTL_VER_01: 6442 case RTL_VER_02: 6443 case RTL_VER_07: 6444 dev->mtu = new_mtu; 6445 return 0; 6446 default: 6447 break; 6448 } 6449 6450 ret = usb_autopm_get_interface(tp->intf); 6451 if (ret < 0) 6452 return ret; 6453 6454 mutex_lock(&tp->control); 6455 6456 dev->mtu = new_mtu; 6457 6458 if (netif_running(dev)) { 6459 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; 6460 6461 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms); 6462 6463 if (netif_carrier_ok(dev)) 6464 r8153_set_rx_early_size(tp); 6465 } 6466 6467 mutex_unlock(&tp->control); 6468 6469 usb_autopm_put_interface(tp->intf); 6470 6471 return ret; 6472 } 6473 6474 static const struct net_device_ops rtl8152_netdev_ops = { 6475 .ndo_open = rtl8152_open, 6476 .ndo_stop = rtl8152_close, 6477 .ndo_do_ioctl = rtl8152_ioctl, 6478 .ndo_start_xmit = rtl8152_start_xmit, 6479 .ndo_tx_timeout = rtl8152_tx_timeout, 6480 .ndo_set_features = rtl8152_set_features, 6481 .ndo_set_rx_mode = rtl8152_set_rx_mode, 6482 .ndo_set_mac_address = rtl8152_set_mac_address, 6483 .ndo_change_mtu = rtl8152_change_mtu, 6484 .ndo_validate_addr = eth_validate_addr, 6485 .ndo_features_check = rtl8152_features_check, 6486 }; 6487 6488 static void rtl8152_unload(struct r8152 *tp) 6489 { 6490 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6491 return; 6492 6493 if (tp->version != RTL_VER_01) 6494 r8152_power_cut_en(tp, true); 6495 } 6496 6497 static void rtl8153_unload(struct r8152 *tp) 6498 { 6499 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6500 return; 6501 6502 r8153_power_cut_en(tp, false); 6503 } 6504 6505 static void rtl8153b_unload(struct r8152 *tp) 6506 { 6507 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6508 return; 6509 6510 r8153b_power_cut_en(tp, false); 6511 } 6512 6513 static int rtl_ops_init(struct r8152 *tp) 6514 { 6515 struct rtl_ops *ops = &tp->rtl_ops; 6516 int ret = 0; 6517 6518 switch (tp->version) { 6519 case RTL_VER_01: 6520 case RTL_VER_02: 6521 case RTL_VER_07: 6522 ops->init = r8152b_init; 6523 ops->enable = rtl8152_enable; 6524 ops->disable = rtl8152_disable; 6525 ops->up = rtl8152_up; 6526 ops->down = rtl8152_down; 6527 ops->unload = rtl8152_unload; 6528 ops->eee_get = r8152_get_eee; 6529 ops->eee_set = r8152_set_eee; 6530 ops->in_nway = rtl8152_in_nway; 6531 ops->hw_phy_cfg = r8152b_hw_phy_cfg; 6532 ops->autosuspend_en = rtl_runtime_suspend_enable; 6533 tp->rx_buf_sz = 16 * 1024; 6534 tp->eee_en = true; 6535 tp->eee_adv = MDIO_EEE_100TX; 6536 break; 6537 6538 case RTL_VER_03: 6539 case RTL_VER_04: 6540 case RTL_VER_05: 6541 case RTL_VER_06: 6542 ops->init = r8153_init; 6543 ops->enable = rtl8153_enable; 6544 ops->disable = rtl8153_disable; 6545 ops->up = rtl8153_up; 6546 ops->down = rtl8153_down; 6547 ops->unload = rtl8153_unload; 6548 ops->eee_get = r8153_get_eee; 6549 ops->eee_set = r8152_set_eee; 6550 ops->in_nway = rtl8153_in_nway; 6551 ops->hw_phy_cfg = r8153_hw_phy_cfg; 6552 ops->autosuspend_en = rtl8153_runtime_enable; 6553 tp->rx_buf_sz = 32 * 1024; 6554 tp->eee_en = true; 6555 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 6556 break; 6557 6558 case RTL_VER_08: 6559 case RTL_VER_09: 6560 ops->init = r8153b_init; 6561 ops->enable = rtl8153_enable; 6562 ops->disable = rtl8153_disable; 6563 ops->up = rtl8153b_up; 6564 ops->down = rtl8153b_down; 6565 ops->unload = rtl8153b_unload; 6566 ops->eee_get = r8153_get_eee; 6567 ops->eee_set = r8152_set_eee; 6568 ops->in_nway = rtl8153_in_nway; 6569 ops->hw_phy_cfg = r8153b_hw_phy_cfg; 6570 ops->autosuspend_en = rtl8153b_runtime_enable; 6571 tp->rx_buf_sz = 32 * 1024; 6572 tp->eee_en = true; 6573 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 6574 break; 6575 6576 default: 6577 ret = -ENODEV; 6578 netif_err(tp, probe, tp->netdev, "Unknown Device\n"); 6579 break; 6580 } 6581 6582 return ret; 6583 } 6584 6585 #define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw" 6586 #define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw" 6587 #define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw" 6588 #define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw" 6589 6590 MODULE_FIRMWARE(FIRMWARE_8153A_2); 6591 MODULE_FIRMWARE(FIRMWARE_8153A_3); 6592 MODULE_FIRMWARE(FIRMWARE_8153A_4); 6593 MODULE_FIRMWARE(FIRMWARE_8153B_2); 6594 6595 static int rtl_fw_init(struct r8152 *tp) 6596 { 6597 struct rtl_fw *rtl_fw = &tp->rtl_fw; 6598 6599 switch (tp->version) { 6600 case RTL_VER_04: 6601 rtl_fw->fw_name = FIRMWARE_8153A_2; 6602 rtl_fw->pre_fw = r8153_pre_firmware_1; 6603 rtl_fw->post_fw = r8153_post_firmware_1; 6604 break; 6605 case RTL_VER_05: 6606 rtl_fw->fw_name = FIRMWARE_8153A_3; 6607 rtl_fw->pre_fw = r8153_pre_firmware_2; 6608 rtl_fw->post_fw = r8153_post_firmware_2; 6609 break; 6610 case RTL_VER_06: 6611 rtl_fw->fw_name = FIRMWARE_8153A_4; 6612 rtl_fw->post_fw = r8153_post_firmware_3; 6613 break; 6614 case RTL_VER_09: 6615 rtl_fw->fw_name = FIRMWARE_8153B_2; 6616 rtl_fw->pre_fw = r8153b_pre_firmware_1; 6617 rtl_fw->post_fw = r8153b_post_firmware_1; 6618 break; 6619 default: 6620 break; 6621 } 6622 6623 return 0; 6624 } 6625 6626 static u8 rtl_get_version(struct usb_interface *intf) 6627 { 6628 struct usb_device *udev = interface_to_usbdev(intf); 6629 u32 ocp_data = 0; 6630 __le32 *tmp; 6631 u8 version; 6632 int ret; 6633 6634 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); 6635 if (!tmp) 6636 return 0; 6637 6638 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 6639 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, 6640 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500); 6641 if (ret > 0) 6642 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK; 6643 6644 kfree(tmp); 6645 6646 switch (ocp_data) { 6647 case 0x4c00: 6648 version = RTL_VER_01; 6649 break; 6650 case 0x4c10: 6651 version = RTL_VER_02; 6652 break; 6653 case 0x5c00: 6654 version = RTL_VER_03; 6655 break; 6656 case 0x5c10: 6657 version = RTL_VER_04; 6658 break; 6659 case 0x5c20: 6660 version = RTL_VER_05; 6661 break; 6662 case 0x5c30: 6663 version = RTL_VER_06; 6664 break; 6665 case 0x4800: 6666 version = RTL_VER_07; 6667 break; 6668 case 0x6000: 6669 version = RTL_VER_08; 6670 break; 6671 case 0x6010: 6672 version = RTL_VER_09; 6673 break; 6674 default: 6675 version = RTL_VER_UNKNOWN; 6676 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data); 6677 break; 6678 } 6679 6680 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version); 6681 6682 return version; 6683 } 6684 6685 static int rtl8152_probe(struct usb_interface *intf, 6686 const struct usb_device_id *id) 6687 { 6688 struct usb_device *udev = interface_to_usbdev(intf); 6689 u8 version = rtl_get_version(intf); 6690 struct r8152 *tp; 6691 struct net_device *netdev; 6692 int ret; 6693 6694 if (version == RTL_VER_UNKNOWN) 6695 return -ENODEV; 6696 6697 if (udev->actconfig->desc.bConfigurationValue != 1) { 6698 usb_driver_set_configuration(udev, 1); 6699 return -ENODEV; 6700 } 6701 6702 if (intf->cur_altsetting->desc.bNumEndpoints < 3) 6703 return -ENODEV; 6704 6705 usb_reset_device(udev); 6706 netdev = alloc_etherdev(sizeof(struct r8152)); 6707 if (!netdev) { 6708 dev_err(&intf->dev, "Out of memory\n"); 6709 return -ENOMEM; 6710 } 6711 6712 SET_NETDEV_DEV(netdev, &intf->dev); 6713 tp = netdev_priv(netdev); 6714 tp->msg_enable = 0x7FFF; 6715 6716 tp->udev = udev; 6717 tp->netdev = netdev; 6718 tp->intf = intf; 6719 tp->version = version; 6720 6721 switch (version) { 6722 case RTL_VER_01: 6723 case RTL_VER_02: 6724 case RTL_VER_07: 6725 tp->mii.supports_gmii = 0; 6726 break; 6727 default: 6728 tp->mii.supports_gmii = 1; 6729 break; 6730 } 6731 6732 ret = rtl_ops_init(tp); 6733 if (ret) 6734 goto out; 6735 6736 rtl_fw_init(tp); 6737 6738 mutex_init(&tp->control); 6739 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); 6740 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t); 6741 tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp); 6742 tasklet_disable(&tp->tx_tl); 6743 6744 netdev->netdev_ops = &rtl8152_netdev_ops; 6745 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; 6746 6747 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | 6748 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM | 6749 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX | 6750 NETIF_F_HW_VLAN_CTAG_TX; 6751 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | 6752 NETIF_F_TSO | NETIF_F_FRAGLIST | 6753 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | 6754 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX; 6755 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | 6756 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | 6757 NETIF_F_IPV6_CSUM | NETIF_F_TSO6; 6758 6759 if (tp->version == RTL_VER_01) { 6760 netdev->features &= ~NETIF_F_RXCSUM; 6761 netdev->hw_features &= ~NETIF_F_RXCSUM; 6762 } 6763 6764 if (le16_to_cpu(udev->descriptor.idVendor) == VENDOR_ID_LENOVO && 6765 le16_to_cpu(udev->descriptor.idProduct) == 0x3082) 6766 set_bit(LENOVO_MACPASSTHRU, &tp->flags); 6767 6768 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial && 6769 (!strcmp(udev->serial, "000001000000") || 6770 !strcmp(udev->serial, "000002000000"))) { 6771 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation"); 6772 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags); 6773 } 6774 6775 netdev->ethtool_ops = &ops; 6776 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE); 6777 6778 /* MTU range: 68 - 1500 or 9194 */ 6779 netdev->min_mtu = ETH_MIN_MTU; 6780 switch (tp->version) { 6781 case RTL_VER_01: 6782 case RTL_VER_02: 6783 netdev->max_mtu = ETH_DATA_LEN; 6784 break; 6785 default: 6786 netdev->max_mtu = RTL8153_MAX_MTU; 6787 break; 6788 } 6789 6790 tp->mii.dev = netdev; 6791 tp->mii.mdio_read = read_mii_word; 6792 tp->mii.mdio_write = write_mii_word; 6793 tp->mii.phy_id_mask = 0x3f; 6794 tp->mii.reg_num_mask = 0x1f; 6795 tp->mii.phy_id = R8152_PHY_ID; 6796 6797 tp->autoneg = AUTONEG_ENABLE; 6798 tp->speed = SPEED_100; 6799 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | 6800 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; 6801 if (tp->mii.supports_gmii) { 6802 tp->speed = SPEED_1000; 6803 tp->advertising |= RTL_ADVERTISED_1000_FULL; 6804 } 6805 tp->duplex = DUPLEX_FULL; 6806 6807 tp->rx_copybreak = RTL8152_RXFG_HEADSZ; 6808 tp->rx_pending = 10 * RTL8152_MAX_RX; 6809 6810 intf->needs_remote_wakeup = 1; 6811 6812 if (!rtl_can_wakeup(tp)) 6813 __rtl_set_wol(tp, 0); 6814 else 6815 tp->saved_wolopts = __rtl_get_wol(tp); 6816 6817 tp->rtl_ops.init(tp); 6818 #if IS_BUILTIN(CONFIG_USB_RTL8152) 6819 /* Retry in case request_firmware() is not ready yet. */ 6820 tp->rtl_fw.retry = true; 6821 #endif 6822 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); 6823 set_ethernet_addr(tp); 6824 6825 usb_set_intfdata(intf, tp); 6826 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT); 6827 6828 ret = register_netdev(netdev); 6829 if (ret != 0) { 6830 netif_err(tp, probe, netdev, "couldn't register the device\n"); 6831 goto out1; 6832 } 6833 6834 if (tp->saved_wolopts) 6835 device_set_wakeup_enable(&udev->dev, true); 6836 else 6837 device_set_wakeup_enable(&udev->dev, false); 6838 6839 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION); 6840 6841 return 0; 6842 6843 out1: 6844 tasklet_kill(&tp->tx_tl); 6845 usb_set_intfdata(intf, NULL); 6846 out: 6847 free_netdev(netdev); 6848 return ret; 6849 } 6850 6851 static void rtl8152_disconnect(struct usb_interface *intf) 6852 { 6853 struct r8152 *tp = usb_get_intfdata(intf); 6854 6855 usb_set_intfdata(intf, NULL); 6856 if (tp) { 6857 rtl_set_unplug(tp); 6858 6859 unregister_netdev(tp->netdev); 6860 tasklet_kill(&tp->tx_tl); 6861 cancel_delayed_work_sync(&tp->hw_phy_work); 6862 tp->rtl_ops.unload(tp); 6863 rtl8152_release_firmware(tp); 6864 free_netdev(tp->netdev); 6865 } 6866 } 6867 6868 #define REALTEK_USB_DEVICE(vend, prod) \ 6869 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 6870 USB_DEVICE_ID_MATCH_INT_CLASS, \ 6871 .idVendor = (vend), \ 6872 .idProduct = (prod), \ 6873 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \ 6874 }, \ 6875 { \ 6876 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \ 6877 USB_DEVICE_ID_MATCH_DEVICE, \ 6878 .idVendor = (vend), \ 6879 .idProduct = (prod), \ 6880 .bInterfaceClass = USB_CLASS_COMM, \ 6881 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \ 6882 .bInterfaceProtocol = USB_CDC_PROTO_NONE 6883 6884 /* table of devices that work with this driver */ 6885 static const struct usb_device_id rtl8152_table[] = { 6886 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)}, 6887 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)}, 6888 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)}, 6889 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)}, 6890 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)}, 6891 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)}, 6892 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)}, 6893 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)}, 6894 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)}, 6895 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3082)}, 6896 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)}, 6897 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)}, 6898 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)}, 6899 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387)}, 6900 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)}, 6901 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)}, 6902 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)}, 6903 {} 6904 }; 6905 6906 MODULE_DEVICE_TABLE(usb, rtl8152_table); 6907 6908 static struct usb_driver rtl8152_driver = { 6909 .name = MODULENAME, 6910 .id_table = rtl8152_table, 6911 .probe = rtl8152_probe, 6912 .disconnect = rtl8152_disconnect, 6913 .suspend = rtl8152_suspend, 6914 .resume = rtl8152_resume, 6915 .reset_resume = rtl8152_reset_resume, 6916 .pre_reset = rtl8152_pre_reset, 6917 .post_reset = rtl8152_post_reset, 6918 .supports_autosuspend = 1, 6919 .disable_hub_initiated_lpm = 1, 6920 }; 6921 6922 module_usb_driver(rtl8152_driver); 6923 6924 MODULE_AUTHOR(DRIVER_AUTHOR); 6925 MODULE_DESCRIPTION(DRIVER_DESC); 6926 MODULE_LICENSE("GPL"); 6927 MODULE_VERSION(DRIVER_VERSION); 6928