Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2 | /* |
hayeswang | c7de7de | 2014-01-15 10:42:16 +0800 | [diff] [blame] | 3 | * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved. |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6 | #include <linux/signal.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/module.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 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> |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 17 | #include <linux/list.h> |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 18 | #include <linux/ip.h> |
| 19 | #include <linux/ipv6.h> |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 20 | #include <net/ip6_checksum.h> |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 21 | #include <uapi/linux/mdio.h> |
| 22 | #include <linux/mdio.h> |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 23 | #include <linux/usb/cdc.h> |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 24 | #include <linux/suspend.h> |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 25 | #include <linux/atomic.h> |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 26 | #include <linux/acpi.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 27 | |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame] | 28 | /* Information for net-next */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 29 | #define NETNEXT_VERSION "09" |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame] | 30 | |
| 31 | /* Information for net */ |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 32 | #define NET_VERSION "10" |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame] | 33 | |
| 34 | #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 35 | #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" |
hayeswang | 44d942a | 2014-01-15 10:42:14 +0800 | [diff] [blame] | 36 | #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters" |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 37 | #define MODULENAME "r8152" |
| 38 | |
| 39 | #define R8152_PHY_ID 32 |
| 40 | |
| 41 | #define PLA_IDR 0xc000 |
| 42 | #define PLA_RCR 0xc010 |
| 43 | #define PLA_RMS 0xc016 |
| 44 | #define PLA_RXFIFO_CTRL0 0xc0a0 |
| 45 | #define PLA_RXFIFO_CTRL1 0xc0a4 |
| 46 | #define PLA_RXFIFO_CTRL2 0xc0a8 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 47 | #define PLA_DMY_REG0 0xc0b0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 48 | #define PLA_FMC 0xc0b4 |
| 49 | #define PLA_CFG_WOL 0xc0b6 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 50 | #define PLA_TEREDO_CFG 0xc0bc |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 51 | #define PLA_TEREDO_WAKE_BASE 0xc0c4 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 52 | #define PLA_MAR 0xcd00 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 53 | #define PLA_BACKUP 0xd000 |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 54 | #define PLA_BDC_CR 0xd1a0 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 55 | #define PLA_TEREDO_TIMER 0xd2cc |
| 56 | #define PLA_REALWOW_TIMER 0xd2e8 |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 57 | #define PLA_SUSPEND_FLAG 0xd38a |
| 58 | #define PLA_INDICATE_FALG 0xd38c |
| 59 | #define PLA_EXTRA_STATUS 0xd398 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 60 | #define PLA_EFUSE_DATA 0xdd00 |
| 61 | #define PLA_EFUSE_CMD 0xdd02 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 62 | #define PLA_LEDSEL 0xdd90 |
| 63 | #define PLA_LED_FEATURE 0xdd92 |
| 64 | #define PLA_PHYAR 0xde00 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 65 | #define PLA_BOOT_CTRL 0xe004 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 66 | #define PLA_GPHY_INTR_IMR 0xe022 |
| 67 | #define PLA_EEE_CR 0xe040 |
| 68 | #define PLA_EEEP_CR 0xe080 |
| 69 | #define PLA_MAC_PWR_CTRL 0xe0c0 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 70 | #define PLA_MAC_PWR_CTRL2 0xe0ca |
| 71 | #define PLA_MAC_PWR_CTRL3 0xe0cc |
| 72 | #define PLA_MAC_PWR_CTRL4 0xe0ce |
| 73 | #define PLA_WDT6_CTRL 0xe428 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 74 | #define PLA_TCR0 0xe610 |
| 75 | #define PLA_TCR1 0xe612 |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 76 | #define PLA_MTPS 0xe615 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 77 | #define PLA_TXFIFO_CTRL 0xe618 |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 78 | #define PLA_RSTTALLY 0xe800 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 79 | #define PLA_CR 0xe813 |
| 80 | #define PLA_CRWECR 0xe81c |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 81 | #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */ |
| 82 | #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 83 | #define PLA_CONFIG5 0xe822 |
| 84 | #define PLA_PHY_PWR 0xe84c |
| 85 | #define PLA_OOB_CTRL 0xe84f |
| 86 | #define PLA_CPCR 0xe854 |
| 87 | #define PLA_MISC_0 0xe858 |
| 88 | #define PLA_MISC_1 0xe85a |
| 89 | #define PLA_OCP_GPHY_BASE 0xe86c |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 90 | #define PLA_TALLYCNT 0xe890 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 91 | #define PLA_SFF_STS_7 0xe8de |
| 92 | #define PLA_PHYSTATUS 0xe908 |
| 93 | #define PLA_BP_BA 0xfc26 |
| 94 | #define PLA_BP_0 0xfc28 |
| 95 | #define PLA_BP_1 0xfc2a |
| 96 | #define PLA_BP_2 0xfc2c |
| 97 | #define PLA_BP_3 0xfc2e |
| 98 | #define PLA_BP_4 0xfc30 |
| 99 | #define PLA_BP_5 0xfc32 |
| 100 | #define PLA_BP_6 0xfc34 |
| 101 | #define PLA_BP_7 0xfc36 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 102 | #define PLA_BP_EN 0xfc38 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 103 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 104 | #define USB_USB2PHY 0xb41e |
| 105 | #define USB_SSPHYLINK2 0xb428 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 106 | #define USB_U2P3_CTRL 0xb460 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 107 | #define USB_CSR_DUMMY1 0xb464 |
| 108 | #define USB_CSR_DUMMY2 0xb466 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 109 | #define USB_DEV_STAT 0xb808 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 110 | #define USB_CONNECT_TIMER 0xcbf8 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 111 | #define USB_MSC_TIMER 0xcbfc |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 112 | #define USB_BURST_SIZE 0xcfc0 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 113 | #define USB_LPM_CONFIG 0xcfd8 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 114 | #define USB_USB_CTRL 0xd406 |
| 115 | #define USB_PHY_CTRL 0xd408 |
| 116 | #define USB_TX_AGG 0xd40a |
| 117 | #define USB_RX_BUF_TH 0xd40c |
| 118 | #define USB_USB_TIMER 0xd428 |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 119 | #define USB_RX_EARLY_TIMEOUT 0xd42c |
| 120 | #define USB_RX_EARLY_SIZE 0xd42e |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 121 | #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */ |
| 122 | #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 123 | #define USB_TX_DMA 0xd434 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 124 | #define USB_UPT_RXDMA_OWN 0xd437 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 125 | #define USB_TOLERANCE 0xd490 |
| 126 | #define USB_LPM_CTRL 0xd41a |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 127 | #define USB_BMU_RESET 0xd4b0 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 128 | #define USB_U1U2_TIMER 0xd4da |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 129 | #define USB_UPS_CTRL 0xd800 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 130 | #define USB_POWER_CUT 0xd80a |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 131 | #define USB_MISC_0 0xd81a |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 132 | #define USB_MISC_1 0xd81f |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 133 | #define USB_AFE_CTRL2 0xd824 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 134 | #define USB_UPS_CFG 0xd842 |
| 135 | #define USB_UPS_FLAGS 0xd848 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 136 | #define USB_WDT11_CTRL 0xe43c |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 137 | #define USB_BP_BA 0xfc26 |
| 138 | #define USB_BP_0 0xfc28 |
| 139 | #define USB_BP_1 0xfc2a |
| 140 | #define USB_BP_2 0xfc2c |
| 141 | #define USB_BP_3 0xfc2e |
| 142 | #define USB_BP_4 0xfc30 |
| 143 | #define USB_BP_5 0xfc32 |
| 144 | #define USB_BP_6 0xfc34 |
| 145 | #define USB_BP_7 0xfc36 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 146 | #define USB_BP_EN 0xfc38 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 147 | #define USB_BP_8 0xfc38 |
| 148 | #define USB_BP_9 0xfc3a |
| 149 | #define USB_BP_10 0xfc3c |
| 150 | #define USB_BP_11 0xfc3e |
| 151 | #define USB_BP_12 0xfc40 |
| 152 | #define USB_BP_13 0xfc42 |
| 153 | #define USB_BP_14 0xfc44 |
| 154 | #define USB_BP_15 0xfc46 |
| 155 | #define USB_BP2_EN 0xfc48 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 156 | |
| 157 | /* OCP Registers */ |
| 158 | #define OCP_ALDPS_CONFIG 0x2010 |
| 159 | #define OCP_EEE_CONFIG1 0x2080 |
| 160 | #define OCP_EEE_CONFIG2 0x2092 |
| 161 | #define OCP_EEE_CONFIG3 0x2094 |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 162 | #define OCP_BASE_MII 0xa400 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 163 | #define OCP_EEE_AR 0xa41a |
| 164 | #define OCP_EEE_DATA 0xa41c |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 165 | #define OCP_PHY_STATUS 0xa420 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 166 | #define OCP_NCTL_CFG 0xa42c |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 167 | #define OCP_POWER_CFG 0xa430 |
| 168 | #define OCP_EEE_CFG 0xa432 |
| 169 | #define OCP_SRAM_ADDR 0xa436 |
| 170 | #define OCP_SRAM_DATA 0xa438 |
| 171 | #define OCP_DOWN_SPEED 0xa442 |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 172 | #define OCP_EEE_ABLE 0xa5c4 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 173 | #define OCP_EEE_ADV 0xa5d0 |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 174 | #define OCP_EEE_LPABLE 0xa5d2 |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 175 | #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 176 | #define OCP_PHY_PATCH_STAT 0xb800 |
| 177 | #define OCP_PHY_PATCH_CMD 0xb820 |
| 178 | #define OCP_ADC_IOFFSET 0xbcfc |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 179 | #define OCP_ADC_CFG 0xbc06 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 180 | #define OCP_SYSCLK_CFG 0xc416 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 181 | |
| 182 | /* SRAM Register */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 183 | #define SRAM_GREEN_CFG 0x8011 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 184 | #define SRAM_LPF_CFG 0x8012 |
| 185 | #define SRAM_10M_AMP1 0x8080 |
| 186 | #define SRAM_10M_AMP2 0x8082 |
| 187 | #define SRAM_IMPEDANCE 0x8084 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 188 | |
| 189 | /* PLA_RCR */ |
| 190 | #define RCR_AAP 0x00000001 |
| 191 | #define RCR_APM 0x00000002 |
| 192 | #define RCR_AM 0x00000004 |
| 193 | #define RCR_AB 0x00000008 |
| 194 | #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB) |
| 195 | |
| 196 | /* PLA_RXFIFO_CTRL0 */ |
| 197 | #define RXFIFO_THR1_NORMAL 0x00080002 |
| 198 | #define RXFIFO_THR1_OOB 0x01800003 |
| 199 | |
| 200 | /* PLA_RXFIFO_CTRL1 */ |
| 201 | #define RXFIFO_THR2_FULL 0x00000060 |
| 202 | #define RXFIFO_THR2_HIGH 0x00000038 |
| 203 | #define RXFIFO_THR2_OOB 0x0000004a |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 204 | #define RXFIFO_THR2_NORMAL 0x00a0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 205 | |
| 206 | /* PLA_RXFIFO_CTRL2 */ |
| 207 | #define RXFIFO_THR3_FULL 0x00000078 |
| 208 | #define RXFIFO_THR3_HIGH 0x00000048 |
| 209 | #define RXFIFO_THR3_OOB 0x0000005a |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 210 | #define RXFIFO_THR3_NORMAL 0x0110 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 211 | |
| 212 | /* PLA_TXFIFO_CTRL */ |
| 213 | #define TXFIFO_THR_NORMAL 0x00400008 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 214 | #define TXFIFO_THR_NORMAL2 0x01000008 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 215 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 216 | /* PLA_DMY_REG0 */ |
| 217 | #define ECM_ALDPS 0x0002 |
| 218 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 219 | /* PLA_FMC */ |
| 220 | #define FMC_FCR_MCU_EN 0x0001 |
| 221 | |
| 222 | /* PLA_EEEP_CR */ |
| 223 | #define EEEP_CR_EEEP_TX 0x0002 |
| 224 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 225 | /* PLA_WDT6_CTRL */ |
| 226 | #define WDT6_SET_MODE 0x0010 |
| 227 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 228 | /* PLA_TCR0 */ |
| 229 | #define TCR0_TX_EMPTY 0x0800 |
| 230 | #define TCR0_AUTO_FIFO 0x0080 |
| 231 | |
| 232 | /* PLA_TCR1 */ |
| 233 | #define VERSION_MASK 0x7cf0 |
| 234 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 235 | /* PLA_MTPS */ |
| 236 | #define MTPS_JUMBO (12 * 1024 / 64) |
| 237 | #define MTPS_DEFAULT (6 * 1024 / 64) |
| 238 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 239 | /* PLA_RSTTALLY */ |
| 240 | #define TALLY_RESET 0x0001 |
| 241 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 242 | /* PLA_CR */ |
| 243 | #define CR_RST 0x10 |
| 244 | #define CR_RE 0x08 |
| 245 | #define CR_TE 0x04 |
| 246 | |
| 247 | /* PLA_CRWECR */ |
| 248 | #define CRWECR_NORAML 0x00 |
| 249 | #define CRWECR_CONFIG 0xc0 |
| 250 | |
| 251 | /* PLA_OOB_CTRL */ |
| 252 | #define NOW_IS_OOB 0x80 |
| 253 | #define TXFIFO_EMPTY 0x20 |
| 254 | #define RXFIFO_EMPTY 0x10 |
| 255 | #define LINK_LIST_READY 0x02 |
| 256 | #define DIS_MCU_CLROOB 0x01 |
| 257 | #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY) |
| 258 | |
| 259 | /* PLA_MISC_1 */ |
| 260 | #define RXDY_GATED_EN 0x0008 |
| 261 | |
| 262 | /* PLA_SFF_STS_7 */ |
| 263 | #define RE_INIT_LL 0x8000 |
| 264 | #define MCU_BORW_EN 0x4000 |
| 265 | |
| 266 | /* PLA_CPCR */ |
| 267 | #define CPCR_RX_VLAN 0x0040 |
| 268 | |
| 269 | /* PLA_CFG_WOL */ |
| 270 | #define MAGIC_EN 0x0001 |
| 271 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 272 | /* PLA_TEREDO_CFG */ |
| 273 | #define TEREDO_SEL 0x8000 |
| 274 | #define TEREDO_WAKE_MASK 0x7f00 |
| 275 | #define TEREDO_RS_EVENT_MASK 0x00fe |
| 276 | #define OOB_TEREDO_EN 0x0001 |
| 277 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 278 | /* PLA_BDC_CR */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 279 | #define ALDPS_PROXY_MODE 0x0001 |
| 280 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 281 | /* PLA_EFUSE_CMD */ |
| 282 | #define EFUSE_READ_CMD BIT(15) |
| 283 | #define EFUSE_DATA_BIT16 BIT(7) |
| 284 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 285 | /* PLA_CONFIG34 */ |
| 286 | #define LINK_ON_WAKE_EN 0x0010 |
| 287 | #define LINK_OFF_WAKE_EN 0x0008 |
| 288 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 289 | /* PLA_CONFIG5 */ |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 290 | #define BWF_EN 0x0040 |
| 291 | #define MWF_EN 0x0020 |
| 292 | #define UWF_EN 0x0010 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 293 | #define LAN_WAKE_EN 0x0002 |
| 294 | |
| 295 | /* PLA_LED_FEATURE */ |
| 296 | #define LED_MODE_MASK 0x0700 |
| 297 | |
| 298 | /* PLA_PHY_PWR */ |
| 299 | #define TX_10M_IDLE_EN 0x0080 |
| 300 | #define PFM_PWM_SWITCH 0x0040 |
| 301 | |
| 302 | /* PLA_MAC_PWR_CTRL */ |
| 303 | #define D3_CLK_GATED_EN 0x00004000 |
| 304 | #define MCU_CLK_RATIO 0x07010f07 |
| 305 | #define MCU_CLK_RATIO_MASK 0x0f0f0f0f |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 306 | #define ALDPS_SPDWN_RATIO 0x0f87 |
| 307 | |
| 308 | /* PLA_MAC_PWR_CTRL2 */ |
| 309 | #define EEE_SPDWN_RATIO 0x8007 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 310 | #define MAC_CLK_SPDWN_EN BIT(15) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 311 | |
| 312 | /* PLA_MAC_PWR_CTRL3 */ |
| 313 | #define PKT_AVAIL_SPDWN_EN 0x0100 |
| 314 | #define SUSPEND_SPDWN_EN 0x0004 |
| 315 | #define U1U2_SPDWN_EN 0x0002 |
| 316 | #define L1_SPDWN_EN 0x0001 |
| 317 | |
| 318 | /* PLA_MAC_PWR_CTRL4 */ |
| 319 | #define PWRSAVE_SPDWN_EN 0x1000 |
| 320 | #define RXDV_SPDWN_EN 0x0800 |
| 321 | #define TX10MIDLE_EN 0x0100 |
| 322 | #define TP100_SPDWN_EN 0x0020 |
| 323 | #define TP500_SPDWN_EN 0x0010 |
| 324 | #define TP1000_SPDWN_EN 0x0008 |
| 325 | #define EEE_SPDWN_EN 0x0001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 326 | |
| 327 | /* PLA_GPHY_INTR_IMR */ |
| 328 | #define GPHY_STS_MSK 0x0001 |
| 329 | #define SPEED_DOWN_MSK 0x0002 |
| 330 | #define SPDWN_RXDV_MSK 0x0004 |
| 331 | #define SPDWN_LINKCHG_MSK 0x0008 |
| 332 | |
| 333 | /* PLA_PHYAR */ |
| 334 | #define PHYAR_FLAG 0x80000000 |
| 335 | |
| 336 | /* PLA_EEE_CR */ |
| 337 | #define EEE_RX_EN 0x0001 |
| 338 | #define EEE_TX_EN 0x0002 |
| 339 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 340 | /* PLA_BOOT_CTRL */ |
| 341 | #define AUTOLOAD_DONE 0x0002 |
| 342 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 343 | /* PLA_SUSPEND_FLAG */ |
| 344 | #define LINK_CHG_EVENT BIT(0) |
| 345 | |
| 346 | /* PLA_INDICATE_FALG */ |
| 347 | #define UPCOMING_RUNTIME_D3 BIT(0) |
| 348 | |
| 349 | /* PLA_EXTRA_STATUS */ |
| 350 | #define LINK_CHANGE_FLAG BIT(8) |
| 351 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 352 | /* USB_USB2PHY */ |
| 353 | #define USB2PHY_SUSPEND 0x0001 |
| 354 | #define USB2PHY_L1 0x0002 |
| 355 | |
| 356 | /* USB_SSPHYLINK2 */ |
| 357 | #define pwd_dn_scale_mask 0x3ffe |
| 358 | #define pwd_dn_scale(x) ((x) << 1) |
| 359 | |
| 360 | /* USB_CSR_DUMMY1 */ |
| 361 | #define DYNAMIC_BURST 0x0001 |
| 362 | |
| 363 | /* USB_CSR_DUMMY2 */ |
| 364 | #define EP4_FULL_FC 0x0001 |
| 365 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 366 | /* USB_DEV_STAT */ |
| 367 | #define STAT_SPEED_MASK 0x0006 |
| 368 | #define STAT_SPEED_HIGH 0x0000 |
hayeswang | a3cc465 | 2014-07-24 16:37:43 +0800 | [diff] [blame] | 369 | #define STAT_SPEED_FULL 0x0002 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 370 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 371 | /* USB_LPM_CONFIG */ |
| 372 | #define LPM_U1U2_EN BIT(0) |
| 373 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 374 | /* USB_TX_AGG */ |
| 375 | #define TX_AGG_MAX_THRESHOLD 0x03 |
| 376 | |
| 377 | /* USB_RX_BUF_TH */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 378 | #define RX_THR_SUPPER 0x0c350180 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 379 | #define RX_THR_HIGH 0x7a120180 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 380 | #define RX_THR_SLOW 0xffff0180 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 381 | #define RX_THR_B 0x00010001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 382 | |
| 383 | /* USB_TX_DMA */ |
| 384 | #define TEST_MODE_DISABLE 0x00000001 |
| 385 | #define TX_SIZE_ADJUST1 0x00000100 |
| 386 | |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 387 | /* USB_BMU_RESET */ |
| 388 | #define BMU_RESET_EP_IN 0x01 |
| 389 | #define BMU_RESET_EP_OUT 0x02 |
| 390 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 391 | /* USB_UPT_RXDMA_OWN */ |
| 392 | #define OWN_UPDATE BIT(0) |
| 393 | #define OWN_CLEAR BIT(1) |
| 394 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 395 | /* USB_UPS_CTRL */ |
| 396 | #define POWER_CUT 0x0100 |
| 397 | |
| 398 | /* USB_PM_CTRL_STATUS */ |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 399 | #define RESUME_INDICATE 0x0001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 400 | |
| 401 | /* USB_USB_CTRL */ |
| 402 | #define RX_AGG_DISABLE 0x0010 |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 403 | #define RX_ZERO_EN 0x0080 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 404 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 405 | /* USB_U2P3_CTRL */ |
| 406 | #define U2P3_ENABLE 0x0001 |
| 407 | |
| 408 | /* USB_POWER_CUT */ |
| 409 | #define PWR_EN 0x0001 |
| 410 | #define PHASE2_EN 0x0008 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 411 | #define UPS_EN BIT(4) |
| 412 | #define USP_PREWAKE BIT(5) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 413 | |
| 414 | /* USB_MISC_0 */ |
| 415 | #define PCUT_STATUS 0x0001 |
| 416 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 417 | /* USB_RX_EARLY_TIMEOUT */ |
| 418 | #define COALESCE_SUPER 85000U |
| 419 | #define COALESCE_HIGH 250000U |
| 420 | #define COALESCE_SLOW 524280U |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 421 | |
| 422 | /* USB_WDT11_CTRL */ |
| 423 | #define TIMER11_EN 0x0001 |
| 424 | |
| 425 | /* USB_LPM_CTRL */ |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 426 | /* bit 4 ~ 5: fifo empty boundary */ |
| 427 | #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */ |
| 428 | /* bit 2 ~ 3: LMP timer */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 429 | #define LPM_TIMER_MASK 0x0c |
| 430 | #define LPM_TIMER_500MS 0x04 /* 500 ms */ |
| 431 | #define LPM_TIMER_500US 0x0c /* 500 us */ |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 432 | #define ROK_EXIT_LPM 0x02 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 433 | |
| 434 | /* USB_AFE_CTRL2 */ |
| 435 | #define SEN_VAL_MASK 0xf800 |
| 436 | #define SEN_VAL_NORMAL 0xa000 |
| 437 | #define SEL_RXIDLE 0x0100 |
| 438 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 439 | /* USB_UPS_CFG */ |
| 440 | #define SAW_CNT_1MS_MASK 0x0fff |
| 441 | |
| 442 | /* USB_UPS_FLAGS */ |
| 443 | #define UPS_FLAGS_R_TUNE BIT(0) |
| 444 | #define UPS_FLAGS_EN_10M_CKDIV BIT(1) |
| 445 | #define UPS_FLAGS_250M_CKDIV BIT(2) |
| 446 | #define UPS_FLAGS_EN_ALDPS BIT(3) |
| 447 | #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4) |
| 448 | #define UPS_FLAGS_SPEED_MASK (0xf << 16) |
| 449 | #define ups_flags_speed(x) ((x) << 16) |
| 450 | #define UPS_FLAGS_EN_EEE BIT(20) |
| 451 | #define UPS_FLAGS_EN_500M_EEE BIT(21) |
| 452 | #define UPS_FLAGS_EN_EEE_CKDIV BIT(22) |
| 453 | #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24) |
| 454 | #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25) |
| 455 | #define UPS_FLAGS_EN_GREEN BIT(26) |
| 456 | #define UPS_FLAGS_EN_FLOW_CTR BIT(27) |
| 457 | |
| 458 | enum spd_duplex { |
| 459 | NWAY_10M_HALF = 1, |
| 460 | NWAY_10M_FULL, |
| 461 | NWAY_100M_HALF, |
| 462 | NWAY_100M_FULL, |
| 463 | NWAY_1000M_FULL, |
| 464 | FORCE_10M_HALF, |
| 465 | FORCE_10M_FULL, |
| 466 | FORCE_100M_HALF, |
| 467 | FORCE_100M_FULL, |
| 468 | }; |
| 469 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 470 | /* OCP_ALDPS_CONFIG */ |
| 471 | #define ENPWRSAVE 0x8000 |
| 472 | #define ENPDNPS 0x0200 |
| 473 | #define LINKENA 0x0100 |
| 474 | #define DIS_SDSAVE 0x0010 |
| 475 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 476 | /* OCP_PHY_STATUS */ |
| 477 | #define PHY_STAT_MASK 0x0007 |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 478 | #define PHY_STAT_EXT_INIT 2 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 479 | #define PHY_STAT_LAN_ON 3 |
| 480 | #define PHY_STAT_PWRDN 5 |
| 481 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 482 | /* OCP_NCTL_CFG */ |
| 483 | #define PGA_RETURN_EN BIT(1) |
| 484 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 485 | /* OCP_POWER_CFG */ |
| 486 | #define EEE_CLKDIV_EN 0x8000 |
| 487 | #define EN_ALDPS 0x0004 |
| 488 | #define EN_10M_PLLOFF 0x0001 |
| 489 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 490 | /* OCP_EEE_CONFIG1 */ |
| 491 | #define RG_TXLPI_MSK_HFDUP 0x8000 |
| 492 | #define RG_MATCLR_EN 0x4000 |
| 493 | #define EEE_10_CAP 0x2000 |
| 494 | #define EEE_NWAY_EN 0x1000 |
| 495 | #define TX_QUIET_EN 0x0200 |
| 496 | #define RX_QUIET_EN 0x0100 |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 497 | #define sd_rise_time_mask 0x0070 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 498 | #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 499 | #define RG_RXLPI_MSK_HFDUP 0x0008 |
| 500 | #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ |
| 501 | |
| 502 | /* OCP_EEE_CONFIG2 */ |
| 503 | #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ |
| 504 | #define RG_DACQUIET_EN 0x0400 |
| 505 | #define RG_LDVQUIET_EN 0x0200 |
| 506 | #define RG_CKRSEL 0x0020 |
| 507 | #define RG_EEEPRG_EN 0x0010 |
| 508 | |
| 509 | /* OCP_EEE_CONFIG3 */ |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 510 | #define fast_snr_mask 0xff80 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 511 | #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 512 | #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ |
| 513 | #define MSK_PH 0x0006 /* bit 0 ~ 3 */ |
| 514 | |
| 515 | /* OCP_EEE_AR */ |
| 516 | /* bit[15:14] function */ |
| 517 | #define FUN_ADDR 0x0000 |
| 518 | #define FUN_DATA 0x4000 |
| 519 | /* bit[4:0] device addr */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 520 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 521 | /* OCP_EEE_CFG */ |
| 522 | #define CTAP_SHORT_EN 0x0040 |
| 523 | #define EEE10_EN 0x0010 |
| 524 | |
| 525 | /* OCP_DOWN_SPEED */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 526 | #define EN_EEE_CMODE BIT(14) |
| 527 | #define EN_EEE_1000 BIT(13) |
| 528 | #define EN_EEE_100 BIT(12) |
| 529 | #define EN_10M_CLKDIV BIT(11) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 530 | #define EN_10M_BGOFF 0x0080 |
| 531 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 532 | /* OCP_PHY_STATE */ |
| 533 | #define TXDIS_STATE 0x01 |
| 534 | #define ABD_STATE 0x02 |
| 535 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 536 | /* OCP_PHY_PATCH_STAT */ |
| 537 | #define PATCH_READY BIT(6) |
| 538 | |
| 539 | /* OCP_PHY_PATCH_CMD */ |
| 540 | #define PATCH_REQUEST BIT(4) |
| 541 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 542 | /* OCP_ADC_CFG */ |
| 543 | #define CKADSEL_L 0x0100 |
| 544 | #define ADC_EN 0x0080 |
| 545 | #define EN_EMI_L 0x0040 |
| 546 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 547 | /* OCP_SYSCLK_CFG */ |
| 548 | #define clk_div_expo(x) (min(x, 5) << 8) |
| 549 | |
| 550 | /* SRAM_GREEN_CFG */ |
| 551 | #define GREEN_ETH_EN BIT(15) |
| 552 | #define R_TUNE_EN BIT(11) |
| 553 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 554 | /* SRAM_LPF_CFG */ |
| 555 | #define LPF_AUTO_TUNE 0x8000 |
| 556 | |
| 557 | /* SRAM_10M_AMP1 */ |
| 558 | #define GDAC_IB_UPALL 0x0008 |
| 559 | |
| 560 | /* SRAM_10M_AMP2 */ |
| 561 | #define AMP_DN 0x0200 |
| 562 | |
| 563 | /* SRAM_IMPEDANCE */ |
| 564 | #define RX_DRIVING_MASK 0x6000 |
| 565 | |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 566 | /* MAC PASSTHRU */ |
| 567 | #define AD_MASK 0xfee0 |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 568 | #define BND_MASK 0x0004 |
David Chen | 8e29d23 | 2019-02-16 17:16:42 +0800 | [diff] [blame] | 569 | #define BD_MASK 0x0001 |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 570 | #define EFUSE 0xcfdb |
| 571 | #define PASS_THRU_MASK 0x1 |
| 572 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 573 | enum rtl_register_content { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 574 | _1000bps = 0x10, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 575 | _100bps = 0x08, |
| 576 | _10bps = 0x04, |
| 577 | LINK_STATUS = 0x02, |
| 578 | FULL_DUP = 0x01, |
| 579 | }; |
| 580 | |
hayeswang | 1764bcd | 2014-08-28 10:24:18 +0800 | [diff] [blame] | 581 | #define RTL8152_MAX_TX 4 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 582 | #define RTL8152_MAX_RX 10 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 583 | #define INTBUFSIZE 2 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 584 | #define TX_ALIGN 4 |
| 585 | #define RX_ALIGN 8 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 586 | |
| 587 | #define INTR_LINK 0x0004 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 588 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 589 | #define RTL8152_REQT_READ 0xc0 |
| 590 | #define RTL8152_REQT_WRITE 0x40 |
| 591 | #define RTL8152_REQ_GET_REGS 0x05 |
| 592 | #define RTL8152_REQ_SET_REGS 0x05 |
| 593 | |
| 594 | #define BYTE_EN_DWORD 0xff |
| 595 | #define BYTE_EN_WORD 0x33 |
| 596 | #define BYTE_EN_BYTE 0x11 |
| 597 | #define BYTE_EN_SIX_BYTES 0x3f |
| 598 | #define BYTE_EN_START_MASK 0x0f |
| 599 | #define BYTE_EN_END_MASK 0xf0 |
| 600 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 601 | #define RTL8153_MAX_PACKET 9216 /* 9K */ |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 602 | #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \ |
| 603 | ETH_FCS_LEN) |
| 604 | #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 605 | #define RTL8153_RMS RTL8153_MAX_PACKET |
hayeswang | b812540 | 2014-07-03 11:55:48 +0800 | [diff] [blame] | 606 | #define RTL8152_TX_TIMEOUT (5 * HZ) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 607 | #define RTL8152_NAPI_WEIGHT 64 |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 608 | #define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \ |
hayeswang | b20cb60 | 2017-03-20 16:13:45 +0800 | [diff] [blame] | 609 | sizeof(struct rx_desc) + RX_ALIGN) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 610 | |
| 611 | /* rtl8152 flags */ |
| 612 | enum rtl8152_flags { |
| 613 | RTL8152_UNPLUG = 0, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 614 | RTL8152_SET_RX_MODE, |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 615 | WORK_ENABLE, |
| 616 | RTL8152_LINK_CHG, |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 617 | SELECTIVE_SUSPEND, |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 618 | PHY_RESET, |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 619 | SCHEDULE_NAPI, |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 620 | GREEN_ETHERNET, |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 621 | DELL_TB_RX_AGG_BUG, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 622 | }; |
| 623 | |
| 624 | /* Define these values to match your device */ |
| 625 | #define VENDOR_ID_REALTEK 0x0bda |
René Rebe | d5b07cc | 2017-03-28 07:56:51 +0200 | [diff] [blame] | 626 | #define VENDOR_ID_MICROSOFT 0x045e |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 627 | #define VENDOR_ID_SAMSUNG 0x04e8 |
Christian Hesse | 347eec3 | 2015-03-31 14:10:07 +0200 | [diff] [blame] | 628 | #define VENDOR_ID_LENOVO 0x17ef |
Grant Grundler | 9084104 | 2017-09-28 11:35:00 -0700 | [diff] [blame] | 629 | #define VENDOR_ID_LINKSYS 0x13b1 |
Zheng Liu | d065c3c1 | 2015-07-07 13:54:12 -0700 | [diff] [blame] | 630 | #define VENDOR_ID_NVIDIA 0x0955 |
Ran Wang | 9d11b06 | 2017-10-23 18:10:23 +0800 | [diff] [blame] | 631 | #define VENDOR_ID_TPLINK 0x2357 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 632 | |
| 633 | #define MCU_TYPE_PLA 0x0100 |
| 634 | #define MCU_TYPE_USB 0x0000 |
| 635 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 636 | struct tally_counter { |
| 637 | __le64 tx_packets; |
| 638 | __le64 rx_packets; |
| 639 | __le64 tx_errors; |
| 640 | __le32 rx_errors; |
| 641 | __le16 rx_missed; |
| 642 | __le16 align_errors; |
| 643 | __le32 tx_one_collision; |
| 644 | __le32 tx_multi_collision; |
| 645 | __le64 rx_unicast; |
| 646 | __le64 rx_broadcast; |
| 647 | __le32 rx_multicast; |
| 648 | __le16 tx_aborted; |
hayeswang | f37119c | 2014-10-28 14:05:51 +0800 | [diff] [blame] | 649 | __le16 tx_underrun; |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 650 | }; |
| 651 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 652 | struct rx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 653 | __le32 opts1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 654 | #define RX_LEN_MASK 0x7fff |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 655 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 656 | __le32 opts2; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 657 | #define RD_UDP_CS BIT(23) |
| 658 | #define RD_TCP_CS BIT(22) |
| 659 | #define RD_IPV6_CS BIT(20) |
| 660 | #define RD_IPV4_CS BIT(19) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 661 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 662 | __le32 opts3; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 663 | #define IPF BIT(23) /* IP checksum fail */ |
| 664 | #define UDPF BIT(22) /* UDP checksum fail */ |
| 665 | #define TCPF BIT(21) /* TCP checksum fail */ |
| 666 | #define RX_VLAN_TAG BIT(16) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 667 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 668 | __le32 opts4; |
| 669 | __le32 opts5; |
| 670 | __le32 opts6; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | struct tx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 674 | __le32 opts1; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 675 | #define TX_FS BIT(31) /* First segment of a packet */ |
| 676 | #define TX_LS BIT(30) /* Final segment of a packet */ |
| 677 | #define GTSENDV4 BIT(28) |
| 678 | #define GTSENDV6 BIT(27) |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 679 | #define GTTCPHO_SHIFT 18 |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 680 | #define GTTCPHO_MAX 0x7fU |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 681 | #define TX_LEN_MAX 0x3ffffU |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 682 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 683 | __le32 opts2; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 684 | #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */ |
| 685 | #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */ |
| 686 | #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */ |
| 687 | #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */ |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 688 | #define MSS_SHIFT 17 |
| 689 | #define MSS_MAX 0x7ffU |
| 690 | #define TCPHO_SHIFT 17 |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 691 | #define TCPHO_MAX 0x7ffU |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 692 | #define TX_VLAN_TAG BIT(16) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 693 | }; |
| 694 | |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 695 | struct r8152; |
| 696 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 697 | struct rx_agg { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 698 | struct list_head list, info_list; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 699 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 700 | struct r8152 *context; |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame^] | 701 | struct page *page; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 702 | void *buffer; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 703 | }; |
| 704 | |
| 705 | struct tx_agg { |
| 706 | struct list_head list; |
| 707 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 708 | struct r8152 *context; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 709 | void *buffer; |
| 710 | void *head; |
| 711 | u32 skb_num; |
| 712 | u32 skb_len; |
| 713 | }; |
| 714 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 715 | struct r8152 { |
| 716 | unsigned long flags; |
| 717 | struct usb_device *udev; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 718 | struct napi_struct napi; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 719 | struct usb_interface *intf; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 720 | struct net_device *netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 721 | struct urb *intr_urb; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 722 | struct tx_agg tx_info[RTL8152_MAX_TX]; |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 723 | struct list_head rx_info; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 724 | struct list_head rx_done, tx_free; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 725 | struct sk_buff_head tx_queue, rx_queue; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 726 | spinlock_t rx_lock, tx_lock; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 727 | struct delayed_work schedule, hw_phy_work; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 728 | struct mii_if_info mii; |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 729 | struct mutex control; /* use for hw setting */ |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 730 | #ifdef CONFIG_PM_SLEEP |
| 731 | struct notifier_block pm_notifier; |
| 732 | #endif |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 733 | |
| 734 | struct rtl_ops { |
| 735 | void (*init)(struct r8152 *); |
| 736 | int (*enable)(struct r8152 *); |
| 737 | void (*disable)(struct r8152 *); |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 738 | void (*up)(struct r8152 *); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 739 | void (*down)(struct r8152 *); |
| 740 | void (*unload)(struct r8152 *); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 741 | int (*eee_get)(struct r8152 *, struct ethtool_eee *); |
| 742 | int (*eee_set)(struct r8152 *, struct ethtool_eee *); |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 743 | bool (*in_nway)(struct r8152 *); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 744 | void (*hw_phy_cfg)(struct r8152 *); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 745 | void (*autosuspend_en)(struct r8152 *tp, bool enable); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 746 | } rtl_ops; |
| 747 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 748 | atomic_t rx_count; |
| 749 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 750 | int intr_interval; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 751 | u32 saved_wolopts; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 752 | u32 msg_enable; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 753 | u32 tx_qlen; |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 754 | u32 coalesce; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 755 | u32 rx_buf_sz; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 756 | u16 ocp_base; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 757 | u16 speed; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 758 | u8 *intr_buff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 759 | u8 version; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 760 | u8 duplex; |
| 761 | u8 autoneg; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 762 | }; |
| 763 | |
| 764 | enum rtl_version { |
| 765 | RTL_VER_UNKNOWN = 0, |
| 766 | RTL_VER_01, |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 767 | RTL_VER_02, |
| 768 | RTL_VER_03, |
| 769 | RTL_VER_04, |
| 770 | RTL_VER_05, |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 771 | RTL_VER_06, |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 772 | RTL_VER_07, |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 773 | RTL_VER_08, |
| 774 | RTL_VER_09, |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 775 | RTL_VER_MAX |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 776 | }; |
| 777 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 778 | enum tx_csum_stat { |
| 779 | TX_CSUM_SUCCESS = 0, |
| 780 | TX_CSUM_TSO, |
| 781 | TX_CSUM_NONE |
| 782 | }; |
| 783 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 784 | /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). |
| 785 | * The RTL chips use a 64 element hash table based on the Ethernet CRC. |
| 786 | */ |
| 787 | static const int multicast_filter_limit = 32; |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 788 | static unsigned int agg_buf_sz = 16384; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 789 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 790 | #define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \ |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 791 | VLAN_ETH_HLEN - ETH_FCS_LEN) |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 792 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 793 | static |
| 794 | int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 795 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 796 | int ret; |
| 797 | void *tmp; |
| 798 | |
| 799 | tmp = kmalloc(size, GFP_KERNEL); |
| 800 | if (!tmp) |
| 801 | return -ENOMEM; |
| 802 | |
| 803 | ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 804 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
| 805 | value, index, tmp, size, 500); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 806 | |
| 807 | memcpy(data, tmp, size); |
| 808 | kfree(tmp); |
| 809 | |
| 810 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | static |
| 814 | int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 815 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 816 | int ret; |
| 817 | void *tmp; |
| 818 | |
Benoit Taine | c4438f0 | 2014-05-26 17:21:23 +0200 | [diff] [blame] | 819 | tmp = kmemdup(data, size, GFP_KERNEL); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 820 | if (!tmp) |
| 821 | return -ENOMEM; |
| 822 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 823 | ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 824 | RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, |
| 825 | value, index, tmp, size, 500); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 826 | |
| 827 | kfree(tmp); |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 828 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 829 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 832 | static void rtl_set_unplug(struct r8152 *tp) |
| 833 | { |
| 834 | if (tp->udev->state == USB_STATE_NOTATTACHED) { |
| 835 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 836 | smp_mb__after_atomic(); |
| 837 | } |
| 838 | } |
| 839 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 840 | static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 841 | void *data, u16 type) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 842 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 843 | u16 limit = 64; |
| 844 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 845 | |
| 846 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 847 | return -ENODEV; |
| 848 | |
| 849 | /* both size and indix must be 4 bytes align */ |
| 850 | if ((size & 3) || !size || (index & 3) || !data) |
| 851 | return -EPERM; |
| 852 | |
| 853 | if ((u32)index + (u32)size > 0xffff) |
| 854 | return -EPERM; |
| 855 | |
| 856 | while (size) { |
| 857 | if (size > limit) { |
| 858 | ret = get_registers(tp, index, type, limit, data); |
| 859 | if (ret < 0) |
| 860 | break; |
| 861 | |
| 862 | index += limit; |
| 863 | data += limit; |
| 864 | size -= limit; |
| 865 | } else { |
| 866 | ret = get_registers(tp, index, type, size, data); |
| 867 | if (ret < 0) |
| 868 | break; |
| 869 | |
| 870 | index += size; |
| 871 | data += size; |
| 872 | size = 0; |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 | |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 877 | if (ret == -ENODEV) |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 878 | rtl_set_unplug(tp); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 879 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 880 | return ret; |
| 881 | } |
| 882 | |
| 883 | static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 884 | u16 size, void *data, u16 type) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 885 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 886 | int ret; |
| 887 | u16 byteen_start, byteen_end, byen; |
| 888 | u16 limit = 512; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 889 | |
| 890 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 891 | return -ENODEV; |
| 892 | |
| 893 | /* both size and indix must be 4 bytes align */ |
| 894 | if ((size & 3) || !size || (index & 3) || !data) |
| 895 | return -EPERM; |
| 896 | |
| 897 | if ((u32)index + (u32)size > 0xffff) |
| 898 | return -EPERM; |
| 899 | |
| 900 | byteen_start = byteen & BYTE_EN_START_MASK; |
| 901 | byteen_end = byteen & BYTE_EN_END_MASK; |
| 902 | |
| 903 | byen = byteen_start | (byteen_start << 4); |
| 904 | ret = set_registers(tp, index, type | byen, 4, data); |
| 905 | if (ret < 0) |
| 906 | goto error1; |
| 907 | |
| 908 | index += 4; |
| 909 | data += 4; |
| 910 | size -= 4; |
| 911 | |
| 912 | if (size) { |
| 913 | size -= 4; |
| 914 | |
| 915 | while (size) { |
| 916 | if (size > limit) { |
| 917 | ret = set_registers(tp, index, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 918 | type | BYTE_EN_DWORD, |
| 919 | limit, data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 920 | if (ret < 0) |
| 921 | goto error1; |
| 922 | |
| 923 | index += limit; |
| 924 | data += limit; |
| 925 | size -= limit; |
| 926 | } else { |
| 927 | ret = set_registers(tp, index, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 928 | type | BYTE_EN_DWORD, |
| 929 | size, data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 930 | if (ret < 0) |
| 931 | goto error1; |
| 932 | |
| 933 | index += size; |
| 934 | data += size; |
| 935 | size = 0; |
| 936 | break; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | byen = byteen_end | (byteen_end >> 4); |
| 941 | ret = set_registers(tp, index, type | byen, 4, data); |
| 942 | if (ret < 0) |
| 943 | goto error1; |
| 944 | } |
| 945 | |
| 946 | error1: |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 947 | if (ret == -ENODEV) |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 948 | rtl_set_unplug(tp); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 949 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | static inline |
| 954 | int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 955 | { |
| 956 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); |
| 957 | } |
| 958 | |
| 959 | static inline |
| 960 | int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 961 | { |
| 962 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); |
| 963 | } |
| 964 | |
| 965 | static inline |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 966 | int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 967 | { |
| 968 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); |
| 969 | } |
| 970 | |
| 971 | static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) |
| 972 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 973 | __le32 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 974 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 975 | generic_ocp_read(tp, index, sizeof(data), &data, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 976 | |
| 977 | return __le32_to_cpu(data); |
| 978 | } |
| 979 | |
| 980 | static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 981 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 982 | __le32 tmp = __cpu_to_le32(data); |
| 983 | |
| 984 | generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) |
| 988 | { |
| 989 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 990 | __le32 tmp; |
hayeswang | d8fbd27 | 2017-06-15 14:44:04 +0800 | [diff] [blame] | 991 | u16 byen = BYTE_EN_WORD; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 992 | u8 shift = index & 2; |
| 993 | |
| 994 | index &= ~3; |
hayeswang | d8fbd27 | 2017-06-15 14:44:04 +0800 | [diff] [blame] | 995 | byen <<= shift; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 996 | |
hayeswang | d8fbd27 | 2017-06-15 14:44:04 +0800 | [diff] [blame] | 997 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 998 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 999 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1000 | data >>= (shift * 8); |
| 1001 | data &= 0xffff; |
| 1002 | |
| 1003 | return (u16)data; |
| 1004 | } |
| 1005 | |
| 1006 | static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 1007 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1008 | u32 mask = 0xffff; |
| 1009 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1010 | u16 byen = BYTE_EN_WORD; |
| 1011 | u8 shift = index & 2; |
| 1012 | |
| 1013 | data &= mask; |
| 1014 | |
| 1015 | if (index & 2) { |
| 1016 | byen <<= shift; |
| 1017 | mask <<= (shift * 8); |
| 1018 | data <<= (shift * 8); |
| 1019 | index &= ~3; |
| 1020 | } |
| 1021 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1022 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1023 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1024 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) |
| 1028 | { |
| 1029 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1030 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1031 | u8 shift = index & 3; |
| 1032 | |
| 1033 | index &= ~3; |
| 1034 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1035 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1036 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1037 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1038 | data >>= (shift * 8); |
| 1039 | data &= 0xff; |
| 1040 | |
| 1041 | return (u8)data; |
| 1042 | } |
| 1043 | |
| 1044 | static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 1045 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1046 | u32 mask = 0xff; |
| 1047 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1048 | u16 byen = BYTE_EN_BYTE; |
| 1049 | u8 shift = index & 3; |
| 1050 | |
| 1051 | data &= mask; |
| 1052 | |
| 1053 | if (index & 3) { |
| 1054 | byen <<= shift; |
| 1055 | mask <<= (shift * 8); |
| 1056 | data <<= (shift * 8); |
| 1057 | index &= ~3; |
| 1058 | } |
| 1059 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1060 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1061 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1062 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1065 | static u16 ocp_reg_read(struct r8152 *tp, u16 addr) |
| 1066 | { |
| 1067 | u16 ocp_base, ocp_index; |
| 1068 | |
| 1069 | ocp_base = addr & 0xf000; |
| 1070 | if (ocp_base != tp->ocp_base) { |
| 1071 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 1072 | tp->ocp_base = ocp_base; |
| 1073 | } |
| 1074 | |
| 1075 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 1076 | return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); |
| 1077 | } |
| 1078 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 1079 | static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) |
| 1080 | { |
| 1081 | u16 ocp_base, ocp_index; |
| 1082 | |
| 1083 | ocp_base = addr & 0xf000; |
| 1084 | if (ocp_base != tp->ocp_base) { |
| 1085 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 1086 | tp->ocp_base = ocp_base; |
| 1087 | } |
| 1088 | |
| 1089 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 1090 | ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); |
| 1091 | } |
| 1092 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1093 | static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1094 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1095 | ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1098 | static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1099 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1100 | return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 1103 | static void sram_write(struct r8152 *tp, u16 addr, u16 data) |
| 1104 | { |
| 1105 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 1106 | ocp_reg_write(tp, OCP_SRAM_DATA, data); |
| 1107 | } |
| 1108 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 1109 | static u16 sram_read(struct r8152 *tp, u16 addr) |
| 1110 | { |
| 1111 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 1112 | return ocp_reg_read(tp, OCP_SRAM_DATA); |
| 1113 | } |
| 1114 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1115 | static int read_mii_word(struct net_device *netdev, int phy_id, int reg) |
| 1116 | { |
| 1117 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1118 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1119 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 1120 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1121 | return -ENODEV; |
| 1122 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1123 | if (phy_id != R8152_PHY_ID) |
| 1124 | return -EINVAL; |
| 1125 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1126 | ret = r8152_mdio_read(tp, reg); |
| 1127 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1128 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static |
| 1132 | void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) |
| 1133 | { |
| 1134 | struct r8152 *tp = netdev_priv(netdev); |
| 1135 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 1136 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1137 | return; |
| 1138 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1139 | if (phy_id != R8152_PHY_ID) |
| 1140 | return; |
| 1141 | |
| 1142 | r8152_mdio_write(tp, reg, val); |
| 1143 | } |
| 1144 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1145 | static int |
| 1146 | r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1147 | |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1148 | static int rtl8152_set_mac_address(struct net_device *netdev, void *p) |
| 1149 | { |
| 1150 | struct r8152 *tp = netdev_priv(netdev); |
| 1151 | struct sockaddr *addr = p; |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1152 | int ret = -EADDRNOTAVAIL; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1153 | |
| 1154 | if (!is_valid_ether_addr(addr->sa_data)) |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1155 | goto out1; |
| 1156 | |
| 1157 | ret = usb_autopm_get_interface(tp->intf); |
| 1158 | if (ret < 0) |
| 1159 | goto out1; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1160 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 1161 | mutex_lock(&tp->control); |
| 1162 | |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1163 | memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); |
| 1164 | |
| 1165 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 1166 | pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data); |
| 1167 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 1168 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 1169 | mutex_unlock(&tp->control); |
| 1170 | |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1171 | usb_autopm_put_interface(tp->intf); |
| 1172 | out1: |
| 1173 | return ret; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1174 | } |
| 1175 | |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1176 | /* Devices containing proper chips can support a persistent |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1177 | * host system provided MAC address. |
| 1178 | * Examples of this are Dell TB15 and Dell WD15 docks |
| 1179 | */ |
| 1180 | static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa) |
| 1181 | { |
| 1182 | acpi_status status; |
| 1183 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 1184 | union acpi_object *obj; |
| 1185 | int ret = -EINVAL; |
| 1186 | u32 ocp_data; |
| 1187 | unsigned char buf[6]; |
| 1188 | |
| 1189 | /* test for -AD variant of RTL8153 */ |
| 1190 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1191 | if ((ocp_data & AD_MASK) == 0x1000) { |
| 1192 | /* test for MAC address pass-through bit */ |
| 1193 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE); |
| 1194 | if ((ocp_data & PASS_THRU_MASK) != 1) { |
| 1195 | netif_dbg(tp, probe, tp->netdev, |
| 1196 | "No efuse for RTL8153-AD MAC pass through\n"); |
| 1197 | return -ENODEV; |
| 1198 | } |
| 1199 | } else { |
David Chen | 8e29d23 | 2019-02-16 17:16:42 +0800 | [diff] [blame] | 1200 | /* test for RTL8153-BND and RTL8153-BD */ |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1201 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); |
David Chen | c286909 | 2019-02-20 13:47:19 +0800 | [diff] [blame] | 1202 | if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) { |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1203 | netif_dbg(tp, probe, tp->netdev, |
| 1204 | "Invalid variant for MAC pass through\n"); |
| 1205 | return -ENODEV; |
| 1206 | } |
| 1207 | } |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1208 | |
| 1209 | /* returns _AUXMAC_#AABBCCDDEEFF# */ |
| 1210 | status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer); |
| 1211 | obj = (union acpi_object *)buffer.pointer; |
| 1212 | if (!ACPI_SUCCESS(status)) |
| 1213 | return -ENODEV; |
| 1214 | if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) { |
| 1215 | netif_warn(tp, probe, tp->netdev, |
hayeswang | 53700f0 | 2016-09-01 17:01:42 +0800 | [diff] [blame] | 1216 | "Invalid buffer for pass-thru MAC addr: (%d, %d)\n", |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1217 | obj->type, obj->string.length); |
| 1218 | goto amacout; |
| 1219 | } |
| 1220 | if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 || |
| 1221 | strncmp(obj->string.pointer + 0x15, "#", 1) != 0) { |
| 1222 | netif_warn(tp, probe, tp->netdev, |
| 1223 | "Invalid header when reading pass-thru MAC addr\n"); |
| 1224 | goto amacout; |
| 1225 | } |
| 1226 | ret = hex2bin(buf, obj->string.pointer + 9, 6); |
| 1227 | if (!(ret == 0 && is_valid_ether_addr(buf))) { |
| 1228 | netif_warn(tp, probe, tp->netdev, |
hayeswang | 53700f0 | 2016-09-01 17:01:42 +0800 | [diff] [blame] | 1229 | "Invalid MAC for pass-thru MAC addr: %d, %pM\n", |
| 1230 | ret, buf); |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1231 | ret = -EINVAL; |
| 1232 | goto amacout; |
| 1233 | } |
| 1234 | memcpy(sa->sa_data, buf, 6); |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1235 | netif_info(tp, probe, tp->netdev, |
| 1236 | "Using pass-thru MAC addr %pM\n", sa->sa_data); |
| 1237 | |
| 1238 | amacout: |
| 1239 | kfree(obj); |
| 1240 | return ret; |
| 1241 | } |
| 1242 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1243 | static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa) |
| 1244 | { |
| 1245 | struct net_device *dev = tp->netdev; |
| 1246 | int ret; |
| 1247 | |
Crag.Wang | a6cbcb7 | 2019-04-22 13:03:43 +0800 | [diff] [blame] | 1248 | sa->sa_family = dev->type; |
| 1249 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1250 | if (tp->version == RTL_VER_01) { |
| 1251 | ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data); |
| 1252 | } else { |
| 1253 | /* if device doesn't support MAC pass through this will |
| 1254 | * be expected to be non-zero |
| 1255 | */ |
| 1256 | ret = vendor_mac_passthru_addr_read(tp, sa); |
| 1257 | if (ret < 0) |
| 1258 | ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data); |
| 1259 | } |
| 1260 | |
| 1261 | if (ret < 0) { |
| 1262 | netif_err(tp, probe, dev, "Get ether addr fail\n"); |
| 1263 | } else if (!is_valid_ether_addr(sa->sa_data)) { |
| 1264 | netif_err(tp, probe, dev, "Invalid ether addr %pM\n", |
| 1265 | sa->sa_data); |
| 1266 | eth_hw_addr_random(dev); |
| 1267 | ether_addr_copy(sa->sa_data, dev->dev_addr); |
| 1268 | netif_info(tp, probe, dev, "Random ether addr %pM\n", |
| 1269 | sa->sa_data); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | return ret; |
| 1274 | } |
| 1275 | |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1276 | static int set_ethernet_addr(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1277 | { |
| 1278 | struct net_device *dev = tp->netdev; |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1279 | struct sockaddr sa; |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1280 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1281 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1282 | ret = determine_ethernet_addr(tp, &sa); |
| 1283 | if (ret < 0) |
| 1284 | return ret; |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1285 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1286 | if (tp->version == RTL_VER_01) |
| 1287 | ether_addr_copy(dev->dev_addr, sa.sa_data); |
| 1288 | else |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1289 | ret = rtl8152_set_mac_address(dev, &sa); |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1290 | |
| 1291 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1294 | static void read_bulk_callback(struct urb *urb) |
| 1295 | { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1296 | struct net_device *netdev; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1297 | int status = urb->status; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1298 | struct rx_agg *agg; |
| 1299 | struct r8152 *tp; |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1300 | unsigned long flags; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1301 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1302 | agg = urb->context; |
| 1303 | if (!agg) |
| 1304 | return; |
| 1305 | |
| 1306 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1307 | if (!tp) |
| 1308 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1309 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1310 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1311 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1312 | |
| 1313 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1314 | return; |
| 1315 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1316 | netdev = tp->netdev; |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 1317 | |
| 1318 | /* When link down, the driver would cancel all bulks. */ |
| 1319 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1320 | if (!netif_carrier_ok(netdev)) |
| 1321 | return; |
| 1322 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1323 | usb_mark_last_busy(tp->udev); |
| 1324 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1325 | switch (status) { |
| 1326 | case 0: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1327 | if (urb->actual_length < ETH_ZLEN) |
| 1328 | break; |
| 1329 | |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1330 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1331 | list_add_tail(&agg->list, &tp->rx_done); |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1332 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1333 | napi_schedule(&tp->napi); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1334 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1335 | case -ESHUTDOWN: |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 1336 | rtl_set_unplug(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1337 | netif_device_detach(tp->netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1338 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1339 | case -ENOENT: |
| 1340 | return; /* the urb is in unlink state */ |
| 1341 | case -ETIME: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1342 | if (net_ratelimit()) |
| 1343 | netdev_warn(netdev, "maybe reset is needed?\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1344 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1345 | default: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1346 | if (net_ratelimit()) |
| 1347 | netdev_warn(netdev, "Rx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1348 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 1351 | r8152_submit_rx(tp, agg, GFP_ATOMIC); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | static void write_bulk_callback(struct urb *urb) |
| 1355 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1356 | struct net_device_stats *stats; |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1357 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1358 | struct tx_agg *agg; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1359 | struct r8152 *tp; |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1360 | unsigned long flags; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1361 | int status = urb->status; |
| 1362 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1363 | agg = urb->context; |
| 1364 | if (!agg) |
| 1365 | return; |
| 1366 | |
| 1367 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1368 | if (!tp) |
| 1369 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1370 | |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1371 | netdev = tp->netdev; |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 1372 | stats = &netdev->stats; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1373 | if (status) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1374 | if (net_ratelimit()) |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1375 | netdev_warn(netdev, "Tx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1376 | stats->tx_errors += agg->skb_num; |
| 1377 | } else { |
| 1378 | stats->tx_packets += agg->skb_num; |
| 1379 | stats->tx_bytes += agg->skb_len; |
| 1380 | } |
| 1381 | |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1382 | spin_lock_irqsave(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1383 | list_add_tail(&agg->list, &tp->tx_free); |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1384 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1385 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1386 | usb_autopm_put_interface_async(tp->intf); |
| 1387 | |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1388 | if (!netif_carrier_ok(netdev)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1389 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1390 | |
| 1391 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1392 | return; |
| 1393 | |
| 1394 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1395 | return; |
| 1396 | |
| 1397 | if (!skb_queue_empty(&tp->tx_queue)) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1398 | napi_schedule(&tp->napi); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1399 | } |
| 1400 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1401 | static void intr_callback(struct urb *urb) |
| 1402 | { |
| 1403 | struct r8152 *tp; |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 1404 | __le16 *d; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1405 | int status = urb->status; |
| 1406 | int res; |
| 1407 | |
| 1408 | tp = urb->context; |
| 1409 | if (!tp) |
| 1410 | return; |
| 1411 | |
| 1412 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1413 | return; |
| 1414 | |
| 1415 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1416 | return; |
| 1417 | |
| 1418 | switch (status) { |
| 1419 | case 0: /* success */ |
| 1420 | break; |
| 1421 | case -ECONNRESET: /* unlink */ |
| 1422 | case -ESHUTDOWN: |
| 1423 | netif_device_detach(tp->netdev); |
Gustavo A. R. Silva | 9ca7867 | 2018-06-28 13:50:48 -0500 | [diff] [blame] | 1424 | /* fall through */ |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1425 | case -ENOENT: |
hayeswang | d59c876 | 2014-10-31 13:35:57 +0800 | [diff] [blame] | 1426 | case -EPROTO: |
| 1427 | netif_info(tp, intr, tp->netdev, |
| 1428 | "Stop submitting intr, status %d\n", status); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1429 | return; |
| 1430 | case -EOVERFLOW: |
| 1431 | netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n"); |
| 1432 | goto resubmit; |
| 1433 | /* -EPIPE: should clear the halt */ |
| 1434 | default: |
| 1435 | netif_info(tp, intr, tp->netdev, "intr status %d\n", status); |
| 1436 | goto resubmit; |
| 1437 | } |
| 1438 | |
| 1439 | d = urb->transfer_buffer; |
| 1440 | if (INTR_LINK & __le16_to_cpu(d[0])) { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 1441 | if (!netif_carrier_ok(tp->netdev)) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1442 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1443 | schedule_delayed_work(&tp->schedule, 0); |
| 1444 | } |
| 1445 | } else { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 1446 | if (netif_carrier_ok(tp->netdev)) { |
hayeswang | 2f25abe | 2017-03-23 19:14:19 +0800 | [diff] [blame] | 1447 | netif_stop_queue(tp->netdev); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1448 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1449 | schedule_delayed_work(&tp->schedule, 0); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | resubmit: |
| 1454 | res = usb_submit_urb(urb, GFP_ATOMIC); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1455 | if (res == -ENODEV) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 1456 | rtl_set_unplug(tp); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1457 | netif_device_detach(tp->netdev); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1458 | } else if (res) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1459 | netif_err(tp, intr, tp->netdev, |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1460 | "can't resubmit intr, status %d\n", res); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1461 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1462 | } |
| 1463 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1464 | static inline void *rx_agg_align(void *data) |
| 1465 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1466 | return (void *)ALIGN((uintptr_t)data, RX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | static inline void *tx_agg_align(void *data) |
| 1470 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1471 | return (void *)ALIGN((uintptr_t)data, TX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1472 | } |
| 1473 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1474 | static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg) |
| 1475 | { |
| 1476 | list_del(&agg->info_list); |
| 1477 | |
| 1478 | usb_free_urb(agg->urb); |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame^] | 1479 | __free_pages(agg->page, get_order(tp->rx_buf_sz)); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1480 | kfree(agg); |
| 1481 | |
| 1482 | atomic_dec(&tp->rx_count); |
| 1483 | } |
| 1484 | |
| 1485 | static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags) |
| 1486 | { |
| 1487 | struct net_device *netdev = tp->netdev; |
| 1488 | int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame^] | 1489 | unsigned int order = get_order(tp->rx_buf_sz); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1490 | struct rx_agg *rx_agg; |
| 1491 | unsigned long flags; |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1492 | |
| 1493 | rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node); |
| 1494 | if (!rx_agg) |
| 1495 | return NULL; |
| 1496 | |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame^] | 1497 | rx_agg->page = alloc_pages(mflags, order); |
| 1498 | if (!rx_agg->page) |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1499 | goto free_rx; |
| 1500 | |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame^] | 1501 | rx_agg->buffer = page_address(rx_agg->page); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1502 | |
| 1503 | rx_agg->urb = usb_alloc_urb(0, mflags); |
| 1504 | if (!rx_agg->urb) |
| 1505 | goto free_buf; |
| 1506 | |
| 1507 | rx_agg->context = tp; |
| 1508 | |
| 1509 | INIT_LIST_HEAD(&rx_agg->list); |
| 1510 | INIT_LIST_HEAD(&rx_agg->info_list); |
| 1511 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 1512 | list_add_tail(&rx_agg->info_list, &tp->rx_info); |
| 1513 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1514 | |
| 1515 | atomic_inc(&tp->rx_count); |
| 1516 | |
| 1517 | return rx_agg; |
| 1518 | |
| 1519 | free_buf: |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame^] | 1520 | __free_pages(rx_agg->page, order); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1521 | free_rx: |
| 1522 | kfree(rx_agg); |
| 1523 | return NULL; |
| 1524 | } |
| 1525 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1526 | static void free_all_mem(struct r8152 *tp) |
| 1527 | { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1528 | struct rx_agg *agg, *agg_next; |
| 1529 | unsigned long flags; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1530 | int i; |
| 1531 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1532 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1533 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1534 | list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list) |
| 1535 | free_rx_agg(tp, agg); |
| 1536 | |
| 1537 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1538 | |
| 1539 | WARN_ON(atomic_read(&tp->rx_count)); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1540 | |
| 1541 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1542 | usb_free_urb(tp->tx_info[i].urb); |
| 1543 | tp->tx_info[i].urb = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1544 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1545 | kfree(tp->tx_info[i].buffer); |
| 1546 | tp->tx_info[i].buffer = NULL; |
| 1547 | tp->tx_info[i].head = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1548 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1549 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1550 | usb_free_urb(tp->intr_urb); |
| 1551 | tp->intr_urb = NULL; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1552 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1553 | kfree(tp->intr_buff); |
| 1554 | tp->intr_buff = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | static int alloc_all_mem(struct r8152 *tp) |
| 1558 | { |
| 1559 | struct net_device *netdev = tp->netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1560 | struct usb_interface *intf = tp->intf; |
| 1561 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1562 | struct usb_host_endpoint *ep_intr = alt->endpoint + 2; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1563 | int node, i; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1564 | |
| 1565 | node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; |
| 1566 | |
| 1567 | spin_lock_init(&tp->rx_lock); |
| 1568 | spin_lock_init(&tp->tx_lock); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1569 | INIT_LIST_HEAD(&tp->rx_info); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1570 | INIT_LIST_HEAD(&tp->tx_free); |
hayeswang | 98d068a | 2017-03-14 14:15:20 +0800 | [diff] [blame] | 1571 | INIT_LIST_HEAD(&tp->rx_done); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1572 | skb_queue_head_init(&tp->tx_queue); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1573 | skb_queue_head_init(&tp->rx_queue); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1574 | atomic_set(&tp->rx_count, 0); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1575 | |
| 1576 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1577 | if (!alloc_rx_agg(tp, GFP_KERNEL)) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1578 | goto err1; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1582 | struct urb *urb; |
| 1583 | u8 *buf; |
| 1584 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1585 | buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1586 | if (!buf) |
| 1587 | goto err1; |
| 1588 | |
| 1589 | if (buf != tx_agg_align(buf)) { |
| 1590 | kfree(buf); |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1591 | buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL, |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1592 | node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1593 | if (!buf) |
| 1594 | goto err1; |
| 1595 | } |
| 1596 | |
| 1597 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1598 | if (!urb) { |
| 1599 | kfree(buf); |
| 1600 | goto err1; |
| 1601 | } |
| 1602 | |
| 1603 | INIT_LIST_HEAD(&tp->tx_info[i].list); |
| 1604 | tp->tx_info[i].context = tp; |
| 1605 | tp->tx_info[i].urb = urb; |
| 1606 | tp->tx_info[i].buffer = buf; |
| 1607 | tp->tx_info[i].head = tx_agg_align(buf); |
| 1608 | |
| 1609 | list_add_tail(&tp->tx_info[i].list, &tp->tx_free); |
| 1610 | } |
| 1611 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1612 | tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1613 | if (!tp->intr_urb) |
| 1614 | goto err1; |
| 1615 | |
| 1616 | tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); |
| 1617 | if (!tp->intr_buff) |
| 1618 | goto err1; |
| 1619 | |
| 1620 | tp->intr_interval = (int)ep_intr->desc.bInterval; |
| 1621 | usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1622 | tp->intr_buff, INTBUFSIZE, intr_callback, |
| 1623 | tp, tp->intr_interval); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1624 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1625 | return 0; |
| 1626 | |
| 1627 | err1: |
| 1628 | free_all_mem(tp); |
| 1629 | return -ENOMEM; |
| 1630 | } |
| 1631 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1632 | static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp) |
| 1633 | { |
| 1634 | struct tx_agg *agg = NULL; |
| 1635 | unsigned long flags; |
| 1636 | |
hayeswang | 21949ab | 2014-03-07 11:04:35 +0800 | [diff] [blame] | 1637 | if (list_empty(&tp->tx_free)) |
| 1638 | return NULL; |
| 1639 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1640 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 1641 | if (!list_empty(&tp->tx_free)) { |
| 1642 | struct list_head *cursor; |
| 1643 | |
| 1644 | cursor = tp->tx_free.next; |
| 1645 | list_del_init(cursor); |
| 1646 | agg = list_entry(cursor, struct tx_agg, list); |
| 1647 | } |
| 1648 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 1649 | |
| 1650 | return agg; |
| 1651 | } |
| 1652 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1653 | /* r8152_csum_workaround() |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1654 | * The hw limites the value the transport offset. When the offset is out of the |
| 1655 | * range, calculate the checksum by sw. |
| 1656 | */ |
| 1657 | static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb, |
| 1658 | struct sk_buff_head *list) |
| 1659 | { |
| 1660 | if (skb_shinfo(skb)->gso_size) { |
| 1661 | netdev_features_t features = tp->netdev->features; |
| 1662 | struct sk_buff_head seg_list; |
| 1663 | struct sk_buff *segs, *nskb; |
| 1664 | |
hayeswang | a91d45f | 2014-07-11 16:48:27 +0800 | [diff] [blame] | 1665 | features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6); |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1666 | segs = skb_gso_segment(skb, features); |
| 1667 | if (IS_ERR(segs) || !segs) |
| 1668 | goto drop; |
| 1669 | |
| 1670 | __skb_queue_head_init(&seg_list); |
| 1671 | |
| 1672 | do { |
| 1673 | nskb = segs; |
| 1674 | segs = segs->next; |
| 1675 | nskb->next = NULL; |
| 1676 | __skb_queue_tail(&seg_list, nskb); |
| 1677 | } while (segs); |
| 1678 | |
| 1679 | skb_queue_splice(&seg_list, list); |
| 1680 | dev_kfree_skb(skb); |
| 1681 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1682 | if (skb_checksum_help(skb) < 0) |
| 1683 | goto drop; |
| 1684 | |
| 1685 | __skb_queue_head(list, skb); |
| 1686 | } else { |
| 1687 | struct net_device_stats *stats; |
| 1688 | |
| 1689 | drop: |
| 1690 | stats = &tp->netdev->stats; |
| 1691 | stats->tx_dropped++; |
| 1692 | dev_kfree_skb(skb); |
| 1693 | } |
| 1694 | } |
| 1695 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1696 | /* msdn_giant_send_check() |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1697 | * According to the document of microsoft, the TCP Pseudo Header excludes the |
| 1698 | * packet length for IPv6 TCP large packets. |
| 1699 | */ |
| 1700 | static int msdn_giant_send_check(struct sk_buff *skb) |
| 1701 | { |
| 1702 | const struct ipv6hdr *ipv6h; |
| 1703 | struct tcphdr *th; |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1704 | int ret; |
| 1705 | |
| 1706 | ret = skb_cow_head(skb, 0); |
| 1707 | if (ret) |
| 1708 | return ret; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1709 | |
| 1710 | ipv6h = ipv6_hdr(skb); |
| 1711 | th = tcp_hdr(skb); |
| 1712 | |
| 1713 | th->check = 0; |
| 1714 | th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0); |
| 1715 | |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1716 | return ret; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1717 | } |
| 1718 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1719 | static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb) |
| 1720 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1721 | if (skb_vlan_tag_present(skb)) { |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1722 | u32 opts2; |
| 1723 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1724 | opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb)); |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1725 | desc->opts2 |= cpu_to_le32(opts2); |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb) |
| 1730 | { |
| 1731 | u32 opts2 = le32_to_cpu(desc->opts2); |
| 1732 | |
| 1733 | if (opts2 & RX_VLAN_TAG) |
| 1734 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
| 1735 | swab16(opts2 & 0xffff)); |
| 1736 | } |
| 1737 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1738 | static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, |
| 1739 | struct sk_buff *skb, u32 len, u32 transport_offset) |
| 1740 | { |
| 1741 | u32 mss = skb_shinfo(skb)->gso_size; |
| 1742 | u32 opts1, opts2 = 0; |
| 1743 | int ret = TX_CSUM_SUCCESS; |
| 1744 | |
| 1745 | WARN_ON_ONCE(len > TX_LEN_MAX); |
| 1746 | |
| 1747 | opts1 = len | TX_FS | TX_LS; |
| 1748 | |
| 1749 | if (mss) { |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1750 | if (transport_offset > GTTCPHO_MAX) { |
| 1751 | netif_warn(tp, tx_err, tp->netdev, |
| 1752 | "Invalid transport offset 0x%x for TSO\n", |
| 1753 | transport_offset); |
| 1754 | ret = TX_CSUM_TSO; |
| 1755 | goto unavailable; |
| 1756 | } |
| 1757 | |
hayeswang | 6e74d17 | 2015-02-06 11:30:50 +0800 | [diff] [blame] | 1758 | switch (vlan_get_protocol(skb)) { |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1759 | case htons(ETH_P_IP): |
| 1760 | opts1 |= GTSENDV4; |
| 1761 | break; |
| 1762 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1763 | case htons(ETH_P_IPV6): |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1764 | if (msdn_giant_send_check(skb)) { |
| 1765 | ret = TX_CSUM_TSO; |
| 1766 | goto unavailable; |
| 1767 | } |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1768 | opts1 |= GTSENDV6; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1769 | break; |
| 1770 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1771 | default: |
| 1772 | WARN_ON_ONCE(1); |
| 1773 | break; |
| 1774 | } |
| 1775 | |
| 1776 | opts1 |= transport_offset << GTTCPHO_SHIFT; |
| 1777 | opts2 |= min(mss, MSS_MAX) << MSS_SHIFT; |
| 1778 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1779 | u8 ip_protocol; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1780 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1781 | if (transport_offset > TCPHO_MAX) { |
| 1782 | netif_warn(tp, tx_err, tp->netdev, |
| 1783 | "Invalid transport offset 0x%x\n", |
| 1784 | transport_offset); |
| 1785 | ret = TX_CSUM_NONE; |
| 1786 | goto unavailable; |
| 1787 | } |
| 1788 | |
hayeswang | 6e74d17 | 2015-02-06 11:30:50 +0800 | [diff] [blame] | 1789 | switch (vlan_get_protocol(skb)) { |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1790 | case htons(ETH_P_IP): |
| 1791 | opts2 |= IPV4_CS; |
| 1792 | ip_protocol = ip_hdr(skb)->protocol; |
| 1793 | break; |
| 1794 | |
| 1795 | case htons(ETH_P_IPV6): |
| 1796 | opts2 |= IPV6_CS; |
| 1797 | ip_protocol = ipv6_hdr(skb)->nexthdr; |
| 1798 | break; |
| 1799 | |
| 1800 | default: |
| 1801 | ip_protocol = IPPROTO_RAW; |
| 1802 | break; |
| 1803 | } |
| 1804 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1805 | if (ip_protocol == IPPROTO_TCP) |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1806 | opts2 |= TCP_CS; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1807 | else if (ip_protocol == IPPROTO_UDP) |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1808 | opts2 |= UDP_CS; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1809 | else |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1810 | WARN_ON_ONCE(1); |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1811 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1812 | opts2 |= transport_offset << TCPHO_SHIFT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1813 | } |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1814 | |
| 1815 | desc->opts2 = cpu_to_le32(opts2); |
| 1816 | desc->opts1 = cpu_to_le32(opts1); |
| 1817 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1818 | unavailable: |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1819 | return ret; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1820 | } |
| 1821 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1822 | static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg) |
| 1823 | { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1824 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1825 | int remain, ret; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1826 | u8 *tx_data; |
| 1827 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1828 | __skb_queue_head_init(&skb_head); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1829 | spin_lock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1830 | skb_queue_splice_init(tx_queue, &skb_head); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1831 | spin_unlock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1832 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1833 | tx_data = agg->head; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1834 | agg->skb_num = 0; |
| 1835 | agg->skb_len = 0; |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1836 | remain = agg_buf_sz; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1837 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1838 | while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) { |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1839 | struct tx_desc *tx_desc; |
| 1840 | struct sk_buff *skb; |
| 1841 | unsigned int len; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1842 | u32 offset; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1843 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1844 | skb = __skb_dequeue(&skb_head); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1845 | if (!skb) |
| 1846 | break; |
| 1847 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1848 | len = skb->len + sizeof(*tx_desc); |
| 1849 | |
| 1850 | if (len > remain) { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1851 | __skb_queue_head(&skb_head, skb); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1852 | break; |
| 1853 | } |
| 1854 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1855 | tx_data = tx_agg_align(tx_data); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1856 | tx_desc = (struct tx_desc *)tx_data; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1857 | |
| 1858 | offset = (u32)skb_transport_offset(skb); |
| 1859 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1860 | if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) { |
| 1861 | r8152_csum_workaround(tp, skb, &skb_head); |
| 1862 | continue; |
| 1863 | } |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1864 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1865 | rtl_tx_vlan_tag(tx_desc, skb); |
| 1866 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1867 | tx_data += sizeof(*tx_desc); |
| 1868 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1869 | len = skb->len; |
| 1870 | if (skb_copy_bits(skb, 0, tx_data, len) < 0) { |
| 1871 | struct net_device_stats *stats = &tp->netdev->stats; |
| 1872 | |
| 1873 | stats->tx_dropped++; |
| 1874 | dev_kfree_skb_any(skb); |
| 1875 | tx_data -= sizeof(*tx_desc); |
| 1876 | continue; |
| 1877 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1878 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1879 | tx_data += len; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1880 | agg->skb_len += len; |
Eric Dumazet | 4c27bf3 | 2018-02-25 19:12:10 -0800 | [diff] [blame] | 1881 | agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1882 | |
| 1883 | dev_kfree_skb_any(skb); |
| 1884 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1885 | remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head); |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 1886 | |
| 1887 | if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags)) |
| 1888 | break; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1889 | } |
| 1890 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1891 | if (!skb_queue_empty(&skb_head)) { |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1892 | spin_lock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1893 | skb_queue_splice(&skb_head, tx_queue); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1894 | spin_unlock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1895 | } |
| 1896 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1897 | netif_tx_lock(tp->netdev); |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1898 | |
| 1899 | if (netif_queue_stopped(tp->netdev) && |
| 1900 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) |
| 1901 | netif_wake_queue(tp->netdev); |
| 1902 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1903 | netif_tx_unlock(tp->netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1904 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1905 | ret = usb_autopm_get_interface_async(tp->intf); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1906 | if (ret < 0) |
| 1907 | goto out_tx_fill; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1908 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1909 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2), |
| 1910 | agg->head, (int)(tx_data - (u8 *)agg->head), |
| 1911 | (usb_complete_t)write_bulk_callback, agg); |
| 1912 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1913 | ret = usb_submit_urb(agg->urb, GFP_ATOMIC); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1914 | if (ret < 0) |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1915 | usb_autopm_put_interface_async(tp->intf); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1916 | |
| 1917 | out_tx_fill: |
| 1918 | return ret; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1919 | } |
| 1920 | |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1921 | static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc) |
| 1922 | { |
| 1923 | u8 checksum = CHECKSUM_NONE; |
| 1924 | u32 opts2, opts3; |
| 1925 | |
hayeswang | 19c0f40 | 2017-01-11 16:25:34 +0800 | [diff] [blame] | 1926 | if (!(tp->netdev->features & NETIF_F_RXCSUM)) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1927 | goto return_result; |
| 1928 | |
| 1929 | opts2 = le32_to_cpu(rx_desc->opts2); |
| 1930 | opts3 = le32_to_cpu(rx_desc->opts3); |
| 1931 | |
| 1932 | if (opts2 & RD_IPV4_CS) { |
| 1933 | if (opts3 & IPF) |
| 1934 | checksum = CHECKSUM_NONE; |
Hayes Wang | ea6499e | 2018-02-02 16:43:35 +0800 | [diff] [blame] | 1935 | else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) |
| 1936 | checksum = CHECKSUM_UNNECESSARY; |
| 1937 | else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1938 | checksum = CHECKSUM_UNNECESSARY; |
Mark Lord | b9a321b | 2016-10-30 19:28:27 -0400 | [diff] [blame] | 1939 | } else if (opts2 & RD_IPV6_CS) { |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1940 | if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) |
| 1941 | checksum = CHECKSUM_UNNECESSARY; |
| 1942 | else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) |
| 1943 | checksum = CHECKSUM_UNNECESSARY; |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | return_result: |
| 1947 | return checksum; |
| 1948 | } |
| 1949 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1950 | static int rx_bottom(struct r8152 *tp, int budget) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1951 | { |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1952 | unsigned long flags; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1953 | struct list_head *cursor, *next, rx_queue; |
hayeswang | e1a2ca9 | 2015-02-06 11:30:45 +0800 | [diff] [blame] | 1954 | int ret = 0, work_done = 0; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 1955 | struct napi_struct *napi = &tp->napi; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1956 | |
| 1957 | if (!skb_queue_empty(&tp->rx_queue)) { |
| 1958 | while (work_done < budget) { |
| 1959 | struct sk_buff *skb = __skb_dequeue(&tp->rx_queue); |
| 1960 | struct net_device *netdev = tp->netdev; |
| 1961 | struct net_device_stats *stats = &netdev->stats; |
| 1962 | unsigned int pkt_len; |
| 1963 | |
| 1964 | if (!skb) |
| 1965 | break; |
| 1966 | |
| 1967 | pkt_len = skb->len; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 1968 | napi_gro_receive(napi, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1969 | work_done++; |
| 1970 | stats->rx_packets++; |
| 1971 | stats->rx_bytes += pkt_len; |
| 1972 | } |
| 1973 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1974 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1975 | if (list_empty(&tp->rx_done)) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1976 | goto out1; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1977 | |
| 1978 | INIT_LIST_HEAD(&rx_queue); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1979 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1980 | list_splice_init(&tp->rx_done, &rx_queue); |
| 1981 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1982 | |
| 1983 | list_for_each_safe(cursor, next, &rx_queue) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1984 | struct rx_desc *rx_desc; |
| 1985 | struct rx_agg *agg; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1986 | int len_used = 0; |
| 1987 | struct urb *urb; |
| 1988 | u8 *rx_data; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1989 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1990 | list_del_init(cursor); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1991 | |
| 1992 | agg = list_entry(cursor, struct rx_agg, list); |
| 1993 | urb = agg->urb; |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1994 | if (urb->actual_length < ETH_ZLEN) |
| 1995 | goto submit; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1996 | |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame^] | 1997 | rx_desc = agg->buffer; |
| 1998 | rx_data = agg->buffer; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1999 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2000 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2001 | while (urb->actual_length > len_used) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2002 | struct net_device *netdev = tp->netdev; |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 2003 | struct net_device_stats *stats = &netdev->stats; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2004 | unsigned int pkt_len; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2005 | struct sk_buff *skb; |
| 2006 | |
hayeswang | 7454445 | 2017-06-09 17:11:47 +0800 | [diff] [blame] | 2007 | /* limite the skb numbers for rx_queue */ |
| 2008 | if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000)) |
| 2009 | break; |
| 2010 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2011 | pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2012 | if (pkt_len < ETH_ZLEN) |
| 2013 | break; |
| 2014 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2015 | len_used += pkt_len; |
| 2016 | if (urb->actual_length < len_used) |
| 2017 | break; |
| 2018 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 2019 | pkt_len -= ETH_FCS_LEN; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2020 | rx_data += sizeof(struct rx_desc); |
| 2021 | |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 2022 | skb = napi_alloc_skb(napi, pkt_len); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2023 | if (!skb) { |
| 2024 | stats->rx_dropped++; |
hayeswang | 5e2f748 | 2014-03-07 11:04:37 +0800 | [diff] [blame] | 2025 | goto find_next_rx; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2026 | } |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 2027 | |
| 2028 | skb->ip_summed = r8152_rx_csum(tp, rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2029 | memcpy(skb->data, rx_data, pkt_len); |
| 2030 | skb_put(skb, pkt_len); |
| 2031 | skb->protocol = eth_type_trans(skb, netdev); |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2032 | rtl_rx_vlan_tag(rx_desc, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2033 | if (work_done < budget) { |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 2034 | napi_gro_receive(napi, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2035 | work_done++; |
| 2036 | stats->rx_packets++; |
| 2037 | stats->rx_bytes += pkt_len; |
| 2038 | } else { |
| 2039 | __skb_queue_tail(&tp->rx_queue, skb); |
| 2040 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2041 | |
hayeswang | 5e2f748 | 2014-03-07 11:04:37 +0800 | [diff] [blame] | 2042 | find_next_rx: |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 2043 | rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2044 | rx_desc = (struct rx_desc *)rx_data; |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame^] | 2045 | len_used = (int)(rx_data - (u8 *)agg->buffer); |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2046 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2047 | } |
| 2048 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 2049 | submit: |
hayeswang | e1a2ca9 | 2015-02-06 11:30:45 +0800 | [diff] [blame] | 2050 | if (!ret) { |
| 2051 | ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); |
| 2052 | } else { |
| 2053 | urb->actual_length = 0; |
| 2054 | list_add_tail(&agg->list, next); |
| 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | if (!list_empty(&rx_queue)) { |
| 2059 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2060 | list_splice_tail(&rx_queue, &tp->rx_done); |
| 2061 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2062 | } |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2063 | |
| 2064 | out1: |
| 2065 | return work_done; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | static void tx_bottom(struct r8152 *tp) |
| 2069 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2070 | int res; |
| 2071 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2072 | do { |
| 2073 | struct tx_agg *agg; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2074 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2075 | if (skb_queue_empty(&tp->tx_queue)) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2076 | break; |
| 2077 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2078 | agg = r8152_get_tx_agg(tp); |
| 2079 | if (!agg) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2080 | break; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2081 | |
| 2082 | res = r8152_tx_agg_fill(tp, agg); |
| 2083 | if (res) { |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 2084 | struct net_device *netdev = tp->netdev; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2085 | |
| 2086 | if (res == -ENODEV) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 2087 | rtl_set_unplug(tp); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2088 | netif_device_detach(netdev); |
| 2089 | } else { |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 2090 | struct net_device_stats *stats = &netdev->stats; |
| 2091 | unsigned long flags; |
| 2092 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2093 | netif_warn(tp, tx_err, netdev, |
| 2094 | "failed tx_urb %d\n", res); |
| 2095 | stats->tx_dropped += agg->skb_num; |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 2096 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2097 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 2098 | list_add_tail(&agg->list, &tp->tx_free); |
| 2099 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 2100 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2101 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2102 | } while (res == 0); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2103 | } |
| 2104 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2105 | static void bottom_half(struct r8152 *tp) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2106 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2107 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2108 | return; |
| 2109 | |
| 2110 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 2111 | return; |
| 2112 | |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 2113 | /* When link down, the driver would cancel all bulks. */ |
| 2114 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2115 | if (!netif_carrier_ok(tp->netdev)) |
| 2116 | return; |
| 2117 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2118 | clear_bit(SCHEDULE_NAPI, &tp->flags); |
hayeswang | 9451a11 | 2014-11-12 10:05:04 +0800 | [diff] [blame] | 2119 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2120 | tx_bottom(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2121 | } |
| 2122 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2123 | static int r8152_poll(struct napi_struct *napi, int budget) |
| 2124 | { |
| 2125 | struct r8152 *tp = container_of(napi, struct r8152, napi); |
| 2126 | int work_done; |
| 2127 | |
| 2128 | work_done = rx_bottom(tp, budget); |
| 2129 | bottom_half(tp); |
| 2130 | |
| 2131 | if (work_done < budget) { |
hayeswang | a3307f9 | 2017-06-09 17:11:48 +0800 | [diff] [blame] | 2132 | if (!napi_complete_done(napi, work_done)) |
| 2133 | goto out; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2134 | if (!list_empty(&tp->rx_done)) |
| 2135 | napi_schedule(napi); |
hayeswang | 248b213 | 2017-01-26 09:38:33 +0800 | [diff] [blame] | 2136 | else if (!skb_queue_empty(&tp->tx_queue) && |
| 2137 | !list_empty(&tp->tx_free)) |
| 2138 | napi_schedule(napi); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2139 | } |
| 2140 | |
hayeswang | a3307f9 | 2017-06-09 17:11:48 +0800 | [diff] [blame] | 2141 | out: |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2142 | return work_done; |
| 2143 | } |
| 2144 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2145 | static |
| 2146 | int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) |
| 2147 | { |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2148 | int ret; |
| 2149 | |
hayeswang | ef827a5 | 2015-01-09 10:26:36 +0800 | [diff] [blame] | 2150 | /* The rx would be stopped, so skip submitting */ |
| 2151 | if (test_bit(RTL8152_UNPLUG, &tp->flags) || |
| 2152 | !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev)) |
| 2153 | return 0; |
| 2154 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2155 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1), |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame^] | 2156 | agg->buffer, tp->rx_buf_sz, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2157 | (usb_complete_t)read_bulk_callback, agg); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2158 | |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2159 | ret = usb_submit_urb(agg->urb, mem_flags); |
| 2160 | if (ret == -ENODEV) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 2161 | rtl_set_unplug(tp); |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2162 | netif_device_detach(tp->netdev); |
| 2163 | } else if (ret) { |
| 2164 | struct urb *urb = agg->urb; |
| 2165 | unsigned long flags; |
| 2166 | |
| 2167 | urb->actual_length = 0; |
| 2168 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2169 | list_add_tail(&agg->list, &tp->rx_done); |
| 2170 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2171 | |
| 2172 | netif_err(tp, rx_err, tp->netdev, |
| 2173 | "Couldn't submit rx[%p], ret = %d\n", agg, ret); |
| 2174 | |
| 2175 | napi_schedule(&tp->napi); |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2181 | static void rtl_drop_queued_tx(struct r8152 *tp) |
| 2182 | { |
| 2183 | struct net_device_stats *stats = &tp->netdev->stats; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2184 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2185 | struct sk_buff *skb; |
| 2186 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2187 | if (skb_queue_empty(tx_queue)) |
| 2188 | return; |
| 2189 | |
| 2190 | __skb_queue_head_init(&skb_head); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 2191 | spin_lock_bh(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2192 | skb_queue_splice_init(tx_queue, &skb_head); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 2193 | spin_unlock_bh(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2194 | |
| 2195 | while ((skb = __skb_dequeue(&skb_head))) { |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2196 | dev_kfree_skb(skb); |
| 2197 | stats->tx_dropped++; |
| 2198 | } |
| 2199 | } |
| 2200 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2201 | static void rtl8152_tx_timeout(struct net_device *netdev) |
| 2202 | { |
| 2203 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2204 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 2205 | netif_warn(tp, tx_err, netdev, "Tx timeout\n"); |
hayeswang | 37608f3 | 2015-07-29 20:39:09 +0800 | [diff] [blame] | 2206 | |
| 2207 | usb_queue_reset_device(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | static void rtl8152_set_rx_mode(struct net_device *netdev) |
| 2211 | { |
| 2212 | struct r8152 *tp = netdev_priv(netdev); |
| 2213 | |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 2214 | if (netif_carrier_ok(netdev)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2215 | set_bit(RTL8152_SET_RX_MODE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2216 | schedule_delayed_work(&tp->schedule, 0); |
| 2217 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | static void _rtl8152_set_rx_mode(struct net_device *netdev) |
| 2221 | { |
| 2222 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 2223 | u32 mc_filter[2]; /* Multicast hash filter */ |
| 2224 | __le32 tmp[2]; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2225 | u32 ocp_data; |
| 2226 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2227 | netif_stop_queue(netdev); |
| 2228 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2229 | ocp_data &= ~RCR_ACPT_ALL; |
| 2230 | ocp_data |= RCR_AB | RCR_APM; |
| 2231 | |
| 2232 | if (netdev->flags & IFF_PROMISC) { |
| 2233 | /* Unconditionally log net taps. */ |
| 2234 | netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); |
| 2235 | ocp_data |= RCR_AM | RCR_AAP; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2236 | mc_filter[1] = 0xffffffff; |
| 2237 | mc_filter[0] = 0xffffffff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2238 | } else if ((netdev_mc_count(netdev) > multicast_filter_limit) || |
| 2239 | (netdev->flags & IFF_ALLMULTI)) { |
| 2240 | /* Too many to filter perfectly -- accept all multicasts. */ |
| 2241 | ocp_data |= RCR_AM; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2242 | mc_filter[1] = 0xffffffff; |
| 2243 | mc_filter[0] = 0xffffffff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2244 | } else { |
| 2245 | struct netdev_hw_addr *ha; |
| 2246 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2247 | mc_filter[1] = 0; |
| 2248 | mc_filter[0] = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2249 | netdev_for_each_mc_addr(ha, netdev) { |
| 2250 | int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2251 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2252 | mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); |
| 2253 | ocp_data |= RCR_AM; |
| 2254 | } |
| 2255 | } |
| 2256 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 2257 | tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); |
| 2258 | tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2259 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 2260 | pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2261 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2262 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 2265 | static netdev_features_t |
| 2266 | rtl8152_features_check(struct sk_buff *skb, struct net_device *dev, |
| 2267 | netdev_features_t features) |
| 2268 | { |
| 2269 | u32 mss = skb_shinfo(skb)->gso_size; |
| 2270 | int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX; |
| 2271 | int offset = skb_transport_offset(skb); |
| 2272 | |
| 2273 | if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset) |
Tom Herbert | a188222 | 2015-12-14 11:19:43 -0800 | [diff] [blame] | 2274 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 2275 | else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz) |
| 2276 | features &= ~NETIF_F_GSO_MASK; |
| 2277 | |
| 2278 | return features; |
| 2279 | } |
| 2280 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2281 | static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2282 | struct net_device *netdev) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2283 | { |
| 2284 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2285 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2286 | skb_tx_timestamp(skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2287 | |
hayeswang | 6159878 | 2013-11-20 17:30:55 +0800 | [diff] [blame] | 2288 | skb_queue_tail(&tp->tx_queue, skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2289 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2290 | if (!list_empty(&tp->tx_free)) { |
| 2291 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2292 | set_bit(SCHEDULE_NAPI, &tp->flags); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2293 | schedule_delayed_work(&tp->schedule, 0); |
| 2294 | } else { |
| 2295 | usb_mark_last_busy(tp->udev); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2296 | napi_schedule(&tp->napi); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2297 | } |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2298 | } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) { |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2299 | netif_stop_queue(netdev); |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2300 | } |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2301 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2302 | return NETDEV_TX_OK; |
| 2303 | } |
| 2304 | |
| 2305 | static void r8152b_reset_packet_filter(struct r8152 *tp) |
| 2306 | { |
| 2307 | u32 ocp_data; |
| 2308 | |
| 2309 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); |
| 2310 | ocp_data &= ~FMC_FCR_MCU_EN; |
| 2311 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 2312 | ocp_data |= FMC_FCR_MCU_EN; |
| 2313 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 2314 | } |
| 2315 | |
| 2316 | static void rtl8152_nic_reset(struct r8152 *tp) |
| 2317 | { |
| 2318 | int i; |
| 2319 | |
| 2320 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); |
| 2321 | |
| 2322 | for (i = 0; i < 1000; i++) { |
| 2323 | if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) |
| 2324 | break; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2325 | usleep_range(100, 400); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2326 | } |
| 2327 | } |
| 2328 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2329 | static void set_tx_qlen(struct r8152 *tp) |
| 2330 | { |
| 2331 | struct net_device *netdev = tp->netdev; |
| 2332 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 2333 | tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN + |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 2334 | sizeof(struct tx_desc)); |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2335 | } |
| 2336 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2337 | static inline u8 rtl8152_get_speed(struct r8152 *tp) |
| 2338 | { |
| 2339 | return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); |
| 2340 | } |
| 2341 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2342 | static void rtl_set_eee_plus(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2343 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2344 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2345 | u8 speed; |
| 2346 | |
| 2347 | speed = rtl8152_get_speed(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2348 | if (speed & _10bps) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2349 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2350 | ocp_data |= EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2351 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 2352 | } else { |
| 2353 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2354 | ocp_data &= ~EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2355 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 2356 | } |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2357 | } |
| 2358 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2359 | static void rxdy_gated_en(struct r8152 *tp, bool enable) |
| 2360 | { |
| 2361 | u32 ocp_data; |
| 2362 | |
| 2363 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 2364 | if (enable) |
| 2365 | ocp_data |= RXDY_GATED_EN; |
| 2366 | else |
| 2367 | ocp_data &= ~RXDY_GATED_EN; |
| 2368 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 2369 | } |
| 2370 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2371 | static int rtl_start_rx(struct r8152 *tp) |
| 2372 | { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2373 | struct rx_agg *agg, *agg_next; |
| 2374 | struct list_head tmp_list; |
| 2375 | unsigned long flags; |
| 2376 | int ret = 0; |
| 2377 | |
| 2378 | INIT_LIST_HEAD(&tmp_list); |
| 2379 | |
| 2380 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2381 | |
| 2382 | INIT_LIST_HEAD(&tp->rx_done); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2383 | |
| 2384 | list_splice_init(&tp->rx_info, &tmp_list); |
| 2385 | |
| 2386 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2387 | |
| 2388 | list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { |
| 2389 | INIT_LIST_HEAD(&agg->list); |
| 2390 | |
| 2391 | if (ret < 0) |
| 2392 | list_add_tail(&agg->list, &tp->rx_done); |
| 2393 | else |
| 2394 | ret = r8152_submit_rx(tp, agg, GFP_KERNEL); |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2395 | } |
| 2396 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2397 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2398 | WARN_ON(!list_empty(&tp->rx_info)); |
| 2399 | list_splice(&tmp_list, &tp->rx_info); |
| 2400 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | 7bcf4f6 | 2014-11-20 10:29:06 +0800 | [diff] [blame] | 2401 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2402 | return ret; |
| 2403 | } |
| 2404 | |
| 2405 | static int rtl_stop_rx(struct r8152 *tp) |
| 2406 | { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2407 | struct rx_agg *agg, *agg_next; |
| 2408 | struct list_head tmp_list; |
| 2409 | unsigned long flags; |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2410 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2411 | INIT_LIST_HEAD(&tmp_list); |
| 2412 | |
| 2413 | /* The usb_kill_urb() couldn't be used in atomic. |
| 2414 | * Therefore, move the list of rx_info to a tmp one. |
| 2415 | * Then, list_for_each_entry_safe could be used without |
| 2416 | * spin lock. |
| 2417 | */ |
| 2418 | |
| 2419 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2420 | list_splice_init(&tp->rx_info, &tmp_list); |
| 2421 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2422 | |
| 2423 | list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) |
| 2424 | usb_kill_urb(agg->urb); |
| 2425 | |
| 2426 | /* Move back the list of temp to the rx_info */ |
| 2427 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2428 | WARN_ON(!list_empty(&tp->rx_info)); |
| 2429 | list_splice(&tmp_list, &tp->rx_info); |
| 2430 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2431 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2432 | while (!skb_queue_empty(&tp->rx_queue)) |
| 2433 | dev_kfree_skb(__skb_dequeue(&tp->rx_queue)); |
| 2434 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2435 | return 0; |
| 2436 | } |
| 2437 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 2438 | static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp) |
| 2439 | { |
| 2440 | ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN, |
| 2441 | OWN_UPDATE | OWN_CLEAR); |
| 2442 | } |
| 2443 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2444 | static int rtl_enable(struct r8152 *tp) |
| 2445 | { |
| 2446 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2447 | |
| 2448 | r8152b_reset_packet_filter(tp); |
| 2449 | |
| 2450 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); |
| 2451 | ocp_data |= CR_RE | CR_TE; |
| 2452 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); |
| 2453 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 2454 | switch (tp->version) { |
| 2455 | case RTL_VER_08: |
| 2456 | case RTL_VER_09: |
| 2457 | r8153b_rx_agg_chg_indicate(tp); |
| 2458 | break; |
| 2459 | default: |
| 2460 | break; |
| 2461 | } |
| 2462 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2463 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2464 | |
hayeswang | aa2e092 | 2015-01-09 10:26:35 +0800 | [diff] [blame] | 2465 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2468 | static int rtl8152_enable(struct r8152 *tp) |
| 2469 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2470 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2471 | return -ENODEV; |
| 2472 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2473 | set_tx_qlen(tp); |
| 2474 | rtl_set_eee_plus(tp); |
| 2475 | |
| 2476 | return rtl_enable(tp); |
| 2477 | } |
| 2478 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2479 | static void r8153_set_rx_early_timeout(struct r8152 *tp) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2480 | { |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2481 | u32 ocp_data = tp->coalesce / 8; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2482 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2483 | switch (tp->version) { |
| 2484 | case RTL_VER_03: |
| 2485 | case RTL_VER_04: |
| 2486 | case RTL_VER_05: |
| 2487 | case RTL_VER_06: |
| 2488 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, |
| 2489 | ocp_data); |
| 2490 | break; |
| 2491 | |
| 2492 | case RTL_VER_08: |
| 2493 | case RTL_VER_09: |
| 2494 | /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout |
| 2495 | * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns. |
| 2496 | */ |
| 2497 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, |
| 2498 | 128 / 8); |
| 2499 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, |
| 2500 | ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2501 | break; |
| 2502 | |
| 2503 | default: |
| 2504 | break; |
| 2505 | } |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | static void r8153_set_rx_early_size(struct r8152 *tp) |
| 2509 | { |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 2510 | u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu); |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2511 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2512 | switch (tp->version) { |
| 2513 | case RTL_VER_03: |
| 2514 | case RTL_VER_04: |
| 2515 | case RTL_VER_05: |
| 2516 | case RTL_VER_06: |
| 2517 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, |
| 2518 | ocp_data / 4); |
| 2519 | break; |
| 2520 | case RTL_VER_08: |
| 2521 | case RTL_VER_09: |
| 2522 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, |
| 2523 | ocp_data / 8); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2524 | break; |
| 2525 | default: |
| 2526 | WARN_ON_ONCE(1); |
| 2527 | break; |
| 2528 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | static int rtl8153_enable(struct r8152 *tp) |
| 2532 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2533 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2534 | return -ENODEV; |
| 2535 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2536 | set_tx_qlen(tp); |
| 2537 | rtl_set_eee_plus(tp); |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2538 | r8153_set_rx_early_timeout(tp); |
| 2539 | r8153_set_rx_early_size(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2540 | |
| 2541 | return rtl_enable(tp); |
| 2542 | } |
| 2543 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2544 | static void rtl_disable(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2545 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2546 | u32 ocp_data; |
| 2547 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2548 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2549 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 2550 | rtl_drop_queued_tx(tp); |
| 2551 | return; |
| 2552 | } |
| 2553 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2554 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2555 | ocp_data &= ~RCR_ACPT_ALL; |
| 2556 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2557 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2558 | rtl_drop_queued_tx(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2559 | |
| 2560 | for (i = 0; i < RTL8152_MAX_TX; i++) |
| 2561 | usb_kill_urb(tp->tx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2562 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2563 | rxdy_gated_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2564 | |
| 2565 | for (i = 0; i < 1000; i++) { |
| 2566 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2567 | if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) |
| 2568 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2569 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | for (i = 0; i < 1000; i++) { |
| 2573 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) |
| 2574 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2575 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2578 | rtl_stop_rx(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2579 | |
| 2580 | rtl8152_nic_reset(tp); |
| 2581 | } |
| 2582 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2583 | static void r8152_power_cut_en(struct r8152 *tp, bool enable) |
| 2584 | { |
| 2585 | u32 ocp_data; |
| 2586 | |
| 2587 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 2588 | if (enable) |
| 2589 | ocp_data |= POWER_CUT; |
| 2590 | else |
| 2591 | ocp_data &= ~POWER_CUT; |
| 2592 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 2593 | |
| 2594 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); |
| 2595 | ocp_data &= ~RESUME_INDICATE; |
| 2596 | ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2597 | } |
| 2598 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2599 | static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) |
| 2600 | { |
| 2601 | u32 ocp_data; |
| 2602 | |
| 2603 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 2604 | if (enable) |
| 2605 | ocp_data |= CPCR_RX_VLAN; |
| 2606 | else |
| 2607 | ocp_data &= ~CPCR_RX_VLAN; |
| 2608 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 2609 | } |
| 2610 | |
| 2611 | static int rtl8152_set_features(struct net_device *dev, |
| 2612 | netdev_features_t features) |
| 2613 | { |
| 2614 | netdev_features_t changed = features ^ dev->features; |
| 2615 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2616 | int ret; |
| 2617 | |
| 2618 | ret = usb_autopm_get_interface(tp->intf); |
| 2619 | if (ret < 0) |
| 2620 | goto out; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2621 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2622 | mutex_lock(&tp->control); |
| 2623 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2624 | if (changed & NETIF_F_HW_VLAN_CTAG_RX) { |
| 2625 | if (features & NETIF_F_HW_VLAN_CTAG_RX) |
| 2626 | rtl_rx_vlan_en(tp, true); |
| 2627 | else |
| 2628 | rtl_rx_vlan_en(tp, false); |
| 2629 | } |
| 2630 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2631 | mutex_unlock(&tp->control); |
| 2632 | |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2633 | usb_autopm_put_interface(tp->intf); |
| 2634 | |
| 2635 | out: |
| 2636 | return ret; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2637 | } |
| 2638 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2639 | #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) |
| 2640 | |
| 2641 | static u32 __rtl_get_wol(struct r8152 *tp) |
| 2642 | { |
| 2643 | u32 ocp_data; |
| 2644 | u32 wolopts = 0; |
| 2645 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2646 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2647 | if (ocp_data & LINK_ON_WAKE_EN) |
| 2648 | wolopts |= WAKE_PHY; |
| 2649 | |
| 2650 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
| 2651 | if (ocp_data & UWF_EN) |
| 2652 | wolopts |= WAKE_UCAST; |
| 2653 | if (ocp_data & BWF_EN) |
| 2654 | wolopts |= WAKE_BCAST; |
| 2655 | if (ocp_data & MWF_EN) |
| 2656 | wolopts |= WAKE_MCAST; |
| 2657 | |
| 2658 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 2659 | if (ocp_data & MAGIC_EN) |
| 2660 | wolopts |= WAKE_MAGIC; |
| 2661 | |
| 2662 | return wolopts; |
| 2663 | } |
| 2664 | |
| 2665 | static void __rtl_set_wol(struct r8152 *tp, u32 wolopts) |
| 2666 | { |
| 2667 | u32 ocp_data; |
| 2668 | |
| 2669 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 2670 | |
| 2671 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2672 | ocp_data &= ~LINK_ON_WAKE_EN; |
| 2673 | if (wolopts & WAKE_PHY) |
| 2674 | ocp_data |= LINK_ON_WAKE_EN; |
| 2675 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 2676 | |
| 2677 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
hayeswang | 92f7d07 | 2016-07-06 17:35:59 +0800 | [diff] [blame] | 2678 | ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN); |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2679 | if (wolopts & WAKE_UCAST) |
| 2680 | ocp_data |= UWF_EN; |
| 2681 | if (wolopts & WAKE_BCAST) |
| 2682 | ocp_data |= BWF_EN; |
| 2683 | if (wolopts & WAKE_MCAST) |
| 2684 | ocp_data |= MWF_EN; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2685 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); |
| 2686 | |
| 2687 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 2688 | |
| 2689 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 2690 | ocp_data &= ~MAGIC_EN; |
| 2691 | if (wolopts & WAKE_MAGIC) |
| 2692 | ocp_data |= MAGIC_EN; |
| 2693 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); |
| 2694 | |
| 2695 | if (wolopts & WAKE_ANY) |
| 2696 | device_set_wakeup_enable(&tp->udev->dev, true); |
| 2697 | else |
| 2698 | device_set_wakeup_enable(&tp->udev->dev, false); |
| 2699 | } |
| 2700 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 2701 | static void r8153_mac_clk_spd(struct r8152 *tp, bool enable) |
| 2702 | { |
| 2703 | /* MAC clock speed down */ |
| 2704 | if (enable) { |
| 2705 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, |
| 2706 | ALDPS_SPDWN_RATIO); |
| 2707 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, |
| 2708 | EEE_SPDWN_RATIO); |
| 2709 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, |
| 2710 | PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN | |
| 2711 | U1U2_SPDWN_EN | L1_SPDWN_EN); |
| 2712 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, |
| 2713 | PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN | |
| 2714 | TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN | |
| 2715 | TP1000_SPDWN_EN); |
| 2716 | } else { |
| 2717 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0); |
| 2718 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0); |
| 2719 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0); |
| 2720 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0); |
| 2721 | } |
| 2722 | } |
| 2723 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2724 | static void r8153_u1u2en(struct r8152 *tp, bool enable) |
| 2725 | { |
| 2726 | u8 u1u2[8]; |
| 2727 | |
| 2728 | if (enable) |
| 2729 | memset(u1u2, 0xff, sizeof(u1u2)); |
| 2730 | else |
| 2731 | memset(u1u2, 0x00, sizeof(u1u2)); |
| 2732 | |
| 2733 | usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); |
| 2734 | } |
| 2735 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2736 | static void r8153b_u1u2en(struct r8152 *tp, bool enable) |
| 2737 | { |
| 2738 | u32 ocp_data; |
| 2739 | |
| 2740 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG); |
| 2741 | if (enable) |
| 2742 | ocp_data |= LPM_U1U2_EN; |
| 2743 | else |
| 2744 | ocp_data &= ~LPM_U1U2_EN; |
| 2745 | |
| 2746 | ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data); |
| 2747 | } |
| 2748 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2749 | static void r8153_u2p3en(struct r8152 *tp, bool enable) |
| 2750 | { |
| 2751 | u32 ocp_data; |
| 2752 | |
| 2753 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 2754 | if (enable) |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2755 | ocp_data |= U2P3_ENABLE; |
| 2756 | else |
| 2757 | ocp_data &= ~U2P3_ENABLE; |
| 2758 | ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); |
| 2759 | } |
| 2760 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2761 | static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear) |
| 2762 | { |
| 2763 | u32 ocp_data; |
| 2764 | |
| 2765 | ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS); |
| 2766 | ocp_data &= ~clear; |
| 2767 | ocp_data |= set; |
| 2768 | ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data); |
| 2769 | } |
| 2770 | |
| 2771 | static void r8153b_green_en(struct r8152 *tp, bool enable) |
| 2772 | { |
| 2773 | u16 data; |
| 2774 | |
| 2775 | if (enable) { |
| 2776 | sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */ |
| 2777 | sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */ |
| 2778 | sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */ |
| 2779 | } else { |
| 2780 | sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */ |
| 2781 | sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */ |
| 2782 | sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */ |
| 2783 | } |
| 2784 | |
| 2785 | data = sram_read(tp, SRAM_GREEN_CFG); |
| 2786 | data |= GREEN_ETH_EN; |
| 2787 | sram_write(tp, SRAM_GREEN_CFG, data); |
| 2788 | |
| 2789 | r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0); |
| 2790 | } |
| 2791 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 2792 | static u16 r8153_phy_status(struct r8152 *tp, u16 desired) |
| 2793 | { |
| 2794 | u16 data; |
| 2795 | int i; |
| 2796 | |
| 2797 | for (i = 0; i < 500; i++) { |
| 2798 | data = ocp_reg_read(tp, OCP_PHY_STATUS); |
| 2799 | data &= PHY_STAT_MASK; |
| 2800 | if (desired) { |
| 2801 | if (data == desired) |
| 2802 | break; |
| 2803 | } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN || |
| 2804 | data == PHY_STAT_EXT_INIT) { |
| 2805 | break; |
| 2806 | } |
| 2807 | |
| 2808 | msleep(20); |
| 2809 | } |
| 2810 | |
| 2811 | return data; |
| 2812 | } |
| 2813 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2814 | static void r8153b_ups_en(struct r8152 *tp, bool enable) |
| 2815 | { |
| 2816 | u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 2817 | |
| 2818 | if (enable) { |
| 2819 | ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; |
| 2820 | ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2821 | |
| 2822 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); |
| 2823 | ocp_data |= BIT(0); |
| 2824 | ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); |
| 2825 | } else { |
| 2826 | u16 data; |
| 2827 | |
| 2828 | ocp_data &= ~(UPS_EN | USP_PREWAKE); |
| 2829 | ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2830 | |
| 2831 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); |
| 2832 | ocp_data &= ~BIT(0); |
| 2833 | ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); |
| 2834 | |
| 2835 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 2836 | ocp_data &= ~PCUT_STATUS; |
| 2837 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 2838 | |
| 2839 | data = r8153_phy_status(tp, 0); |
| 2840 | |
| 2841 | switch (data) { |
| 2842 | case PHY_STAT_PWRDN: |
| 2843 | case PHY_STAT_EXT_INIT: |
| 2844 | r8153b_green_en(tp, |
| 2845 | test_bit(GREEN_ETHERNET, &tp->flags)); |
| 2846 | |
| 2847 | data = r8152_mdio_read(tp, MII_BMCR); |
| 2848 | data &= ~BMCR_PDOWN; |
| 2849 | data |= BMCR_RESET; |
| 2850 | r8152_mdio_write(tp, MII_BMCR, data); |
| 2851 | |
| 2852 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
Gustavo A. R. Silva | 9ca7867 | 2018-06-28 13:50:48 -0500 | [diff] [blame] | 2853 | /* fall through */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2854 | |
| 2855 | default: |
| 2856 | if (data != PHY_STAT_LAN_ON) |
| 2857 | netif_warn(tp, link, tp->netdev, |
| 2858 | "PHY not ready"); |
| 2859 | break; |
| 2860 | } |
| 2861 | } |
| 2862 | } |
| 2863 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2864 | static void r8153_power_cut_en(struct r8152 *tp, bool enable) |
| 2865 | { |
| 2866 | u32 ocp_data; |
| 2867 | |
| 2868 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 2869 | if (enable) |
| 2870 | ocp_data |= PWR_EN | PHASE2_EN; |
| 2871 | else |
| 2872 | ocp_data &= ~(PWR_EN | PHASE2_EN); |
| 2873 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2874 | |
| 2875 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 2876 | ocp_data &= ~PCUT_STATUS; |
| 2877 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 2878 | } |
| 2879 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2880 | static void r8153b_power_cut_en(struct r8152 *tp, bool enable) |
| 2881 | { |
| 2882 | u32 ocp_data; |
| 2883 | |
| 2884 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 2885 | if (enable) |
| 2886 | ocp_data |= PWR_EN | PHASE2_EN; |
| 2887 | else |
| 2888 | ocp_data &= ~PWR_EN; |
| 2889 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2890 | |
| 2891 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 2892 | ocp_data &= ~PCUT_STATUS; |
| 2893 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 2894 | } |
| 2895 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2896 | static void r8153_queue_wake(struct r8152 *tp, bool enable) |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2897 | { |
| 2898 | u32 ocp_data; |
| 2899 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2900 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2901 | if (enable) |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2902 | ocp_data |= UPCOMING_RUNTIME_D3; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2903 | else |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2904 | ocp_data &= ~UPCOMING_RUNTIME_D3; |
| 2905 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2906 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2907 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG); |
| 2908 | ocp_data &= ~LINK_CHG_EVENT; |
| 2909 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data); |
| 2910 | |
| 2911 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); |
| 2912 | ocp_data &= ~LINK_CHANGE_FLAG; |
| 2913 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2914 | } |
| 2915 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 2916 | static bool rtl_can_wakeup(struct r8152 *tp) |
| 2917 | { |
| 2918 | struct usb_device *udev = tp->udev; |
| 2919 | |
| 2920 | return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP); |
| 2921 | } |
| 2922 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2923 | static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable) |
| 2924 | { |
| 2925 | if (enable) { |
| 2926 | u32 ocp_data; |
| 2927 | |
| 2928 | __rtl_set_wol(tp, WAKE_ANY); |
| 2929 | |
| 2930 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 2931 | |
| 2932 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2933 | ocp_data |= LINK_OFF_WAKE_EN; |
| 2934 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 2935 | |
| 2936 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 2937 | } else { |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 2938 | u32 ocp_data; |
| 2939 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2940 | __rtl_set_wol(tp, tp->saved_wolopts); |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 2941 | |
| 2942 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 2943 | |
| 2944 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2945 | ocp_data &= ~LINK_OFF_WAKE_EN; |
| 2946 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 2947 | |
| 2948 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 2949 | } |
| 2950 | } |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 2951 | |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 2952 | static void rtl8153_runtime_enable(struct r8152 *tp, bool enable) |
| 2953 | { |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 2954 | if (enable) { |
| 2955 | r8153_u1u2en(tp, false); |
| 2956 | r8153_u2p3en(tp, false); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 2957 | r8153_mac_clk_spd(tp, true); |
hayeswang | 0255275 | 2017-06-09 17:11:42 +0800 | [diff] [blame] | 2958 | rtl_runtime_suspend_enable(tp, true); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 2959 | } else { |
hayeswang | 0255275 | 2017-06-09 17:11:42 +0800 | [diff] [blame] | 2960 | rtl_runtime_suspend_enable(tp, false); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 2961 | r8153_mac_clk_spd(tp, false); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 2962 | |
| 2963 | switch (tp->version) { |
| 2964 | case RTL_VER_03: |
| 2965 | case RTL_VER_04: |
| 2966 | break; |
| 2967 | case RTL_VER_05: |
| 2968 | case RTL_VER_06: |
| 2969 | default: |
| 2970 | r8153_u2p3en(tp, true); |
| 2971 | break; |
| 2972 | } |
| 2973 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2974 | r8153_u1u2en(tp, true); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2975 | } |
| 2976 | } |
| 2977 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2978 | static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable) |
| 2979 | { |
| 2980 | if (enable) { |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2981 | r8153_queue_wake(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2982 | r8153b_u1u2en(tp, false); |
| 2983 | r8153_u2p3en(tp, false); |
| 2984 | rtl_runtime_suspend_enable(tp, true); |
| 2985 | r8153b_ups_en(tp, true); |
| 2986 | } else { |
| 2987 | r8153b_ups_en(tp, false); |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2988 | r8153_queue_wake(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2989 | rtl_runtime_suspend_enable(tp, false); |
| 2990 | r8153_u2p3en(tp, true); |
| 2991 | r8153b_u1u2en(tp, true); |
| 2992 | } |
| 2993 | } |
| 2994 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 2995 | static void r8153_teredo_off(struct r8152 *tp) |
| 2996 | { |
| 2997 | u32 ocp_data; |
| 2998 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2999 | switch (tp->version) { |
| 3000 | case RTL_VER_01: |
| 3001 | case RTL_VER_02: |
| 3002 | case RTL_VER_03: |
| 3003 | case RTL_VER_04: |
| 3004 | case RTL_VER_05: |
| 3005 | case RTL_VER_06: |
| 3006 | case RTL_VER_07: |
| 3007 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 3008 | ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | |
| 3009 | OOB_TEREDO_EN); |
| 3010 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 3011 | break; |
| 3012 | |
| 3013 | case RTL_VER_08: |
| 3014 | case RTL_VER_09: |
| 3015 | /* The bit 0 ~ 7 are relative with teredo settings. They are |
| 3016 | * W1C (write 1 to clear), so set all 1 to disable it. |
| 3017 | */ |
| 3018 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff); |
| 3019 | break; |
| 3020 | |
| 3021 | default: |
| 3022 | break; |
| 3023 | } |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3024 | |
| 3025 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); |
| 3026 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); |
| 3027 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); |
| 3028 | } |
| 3029 | |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3030 | static void rtl_reset_bmu(struct r8152 *tp) |
| 3031 | { |
| 3032 | u32 ocp_data; |
| 3033 | |
| 3034 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET); |
| 3035 | ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT); |
| 3036 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 3037 | ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT; |
| 3038 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 3039 | } |
| 3040 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3041 | static void r8152_aldps_en(struct r8152 *tp, bool enable) |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3042 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3043 | if (enable) { |
| 3044 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | |
| 3045 | LINKENA | DIS_SDSAVE); |
| 3046 | } else { |
| 3047 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | |
| 3048 | DIS_SDSAVE); |
| 3049 | msleep(20); |
| 3050 | } |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3051 | } |
| 3052 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3053 | static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg) |
| 3054 | { |
| 3055 | ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev); |
| 3056 | ocp_reg_write(tp, OCP_EEE_DATA, reg); |
| 3057 | ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev); |
| 3058 | } |
| 3059 | |
| 3060 | static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg) |
| 3061 | { |
| 3062 | u16 data; |
| 3063 | |
| 3064 | r8152_mmd_indirect(tp, dev, reg); |
| 3065 | data = ocp_reg_read(tp, OCP_EEE_DATA); |
| 3066 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 3067 | |
| 3068 | return data; |
| 3069 | } |
| 3070 | |
| 3071 | static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data) |
| 3072 | { |
| 3073 | r8152_mmd_indirect(tp, dev, reg); |
| 3074 | ocp_reg_write(tp, OCP_EEE_DATA, data); |
| 3075 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 3076 | } |
| 3077 | |
| 3078 | static void r8152_eee_en(struct r8152 *tp, bool enable) |
| 3079 | { |
| 3080 | u16 config1, config2, config3; |
| 3081 | u32 ocp_data; |
| 3082 | |
| 3083 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 3084 | config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask; |
| 3085 | config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2); |
| 3086 | config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask; |
| 3087 | |
| 3088 | if (enable) { |
| 3089 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 3090 | config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN; |
| 3091 | config1 |= sd_rise_time(1); |
| 3092 | config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN; |
| 3093 | config3 |= fast_snr(42); |
| 3094 | } else { |
| 3095 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 3096 | config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | |
| 3097 | RX_QUIET_EN); |
| 3098 | config1 |= sd_rise_time(7); |
| 3099 | config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN); |
| 3100 | config3 |= fast_snr(511); |
| 3101 | } |
| 3102 | |
| 3103 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 3104 | ocp_reg_write(tp, OCP_EEE_CONFIG1, config1); |
| 3105 | ocp_reg_write(tp, OCP_EEE_CONFIG2, config2); |
| 3106 | ocp_reg_write(tp, OCP_EEE_CONFIG3, config3); |
| 3107 | } |
| 3108 | |
| 3109 | static void r8152b_enable_eee(struct r8152 *tp) |
| 3110 | { |
| 3111 | r8152_eee_en(tp, true); |
| 3112 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX); |
| 3113 | } |
| 3114 | |
| 3115 | static void r8152b_enable_fc(struct r8152 *tp) |
| 3116 | { |
| 3117 | u16 anar; |
| 3118 | |
| 3119 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 3120 | anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; |
| 3121 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 3122 | } |
| 3123 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3124 | static void rtl8152_disable(struct r8152 *tp) |
| 3125 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3126 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3127 | rtl_disable(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3128 | r8152_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3129 | } |
| 3130 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3131 | static void r8152b_hw_phy_cfg(struct r8152 *tp) |
| 3132 | { |
hayeswang | ef39df8 | 2016-09-20 16:22:07 +0800 | [diff] [blame] | 3133 | r8152b_enable_eee(tp); |
| 3134 | r8152_aldps_en(tp, true); |
| 3135 | r8152b_enable_fc(tp); |
hayeswang | f0cbe0a | 2014-02-18 21:49:03 +0800 | [diff] [blame] | 3136 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3137 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3138 | } |
| 3139 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3140 | static void r8152b_exit_oob(struct r8152 *tp) |
| 3141 | { |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 3142 | u32 ocp_data; |
| 3143 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3144 | |
| 3145 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 3146 | ocp_data &= ~RCR_ACPT_ALL; |
| 3147 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 3148 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3149 | rxdy_gated_en(tp, true); |
hayeswang | da9bd11 | 2014-02-18 21:49:08 +0800 | [diff] [blame] | 3150 | r8153_teredo_off(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3151 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 3152 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); |
| 3153 | |
| 3154 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3155 | ocp_data &= ~NOW_IS_OOB; |
| 3156 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3157 | |
| 3158 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3159 | ocp_data &= ~MCU_BORW_EN; |
| 3160 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3161 | |
| 3162 | for (i = 0; i < 1000; i++) { |
| 3163 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3164 | if (ocp_data & LINK_LIST_READY) |
| 3165 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3166 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3170 | ocp_data |= RE_INIT_LL; |
| 3171 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3172 | |
| 3173 | for (i = 0; i < 1000; i++) { |
| 3174 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3175 | if (ocp_data & LINK_LIST_READY) |
| 3176 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3177 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | rtl8152_nic_reset(tp); |
| 3181 | |
| 3182 | /* rx share fifo credit full threshold */ |
| 3183 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 3184 | |
hayeswang | a3cc465 | 2014-07-24 16:37:43 +0800 | [diff] [blame] | 3185 | if (tp->udev->speed == USB_SPEED_FULL || |
| 3186 | tp->udev->speed == USB_SPEED_LOW) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3187 | /* rx share fifo credit near full threshold */ |
| 3188 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 3189 | RXFIFO_THR2_FULL); |
| 3190 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 3191 | RXFIFO_THR3_FULL); |
| 3192 | } else { |
| 3193 | /* rx share fifo credit near full threshold */ |
| 3194 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 3195 | RXFIFO_THR2_HIGH); |
| 3196 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 3197 | RXFIFO_THR3_HIGH); |
| 3198 | } |
| 3199 | |
| 3200 | /* TX share fifo free credit full threshold */ |
| 3201 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); |
| 3202 | |
| 3203 | ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 3204 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3205 | ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, |
| 3206 | TEST_MODE_DISABLE | TX_SIZE_ADJUST1); |
| 3207 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 3208 | rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3209 | |
| 3210 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 3211 | |
| 3212 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 3213 | ocp_data |= TCR0_AUTO_FIFO; |
| 3214 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 3215 | } |
| 3216 | |
| 3217 | static void r8152b_enter_oob(struct r8152 *tp) |
| 3218 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 3219 | u32 ocp_data; |
| 3220 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3221 | |
| 3222 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3223 | ocp_data &= ~NOW_IS_OOB; |
| 3224 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3225 | |
| 3226 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); |
| 3227 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); |
| 3228 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); |
| 3229 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3230 | rtl_disable(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3231 | |
| 3232 | for (i = 0; i < 1000; i++) { |
| 3233 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3234 | if (ocp_data & LINK_LIST_READY) |
| 3235 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3236 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3240 | ocp_data |= RE_INIT_LL; |
| 3241 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3242 | |
| 3243 | for (i = 0; i < 1000; i++) { |
| 3244 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3245 | if (ocp_data & LINK_LIST_READY) |
| 3246 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3247 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 3251 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 3252 | rtl_rx_vlan_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3253 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 3254 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3255 | ocp_data |= ALDPS_PROXY_MODE; |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 3256 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3257 | |
| 3258 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3259 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 3260 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3261 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3262 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3263 | |
| 3264 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 3265 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 3266 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 3267 | } |
| 3268 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3269 | static int r8153_patch_request(struct r8152 *tp, bool request) |
| 3270 | { |
| 3271 | u16 data; |
| 3272 | int i; |
| 3273 | |
| 3274 | data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD); |
| 3275 | if (request) |
| 3276 | data |= PATCH_REQUEST; |
| 3277 | else |
| 3278 | data &= ~PATCH_REQUEST; |
| 3279 | ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data); |
| 3280 | |
| 3281 | for (i = 0; request && i < 5000; i++) { |
| 3282 | usleep_range(1000, 2000); |
| 3283 | if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY) |
| 3284 | break; |
| 3285 | } |
| 3286 | |
| 3287 | if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) { |
| 3288 | netif_err(tp, drv, tp->netdev, "patch request fail\n"); |
| 3289 | r8153_patch_request(tp, false); |
| 3290 | return -ETIME; |
| 3291 | } else { |
| 3292 | return 0; |
| 3293 | } |
| 3294 | } |
| 3295 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3296 | static void r8153_aldps_en(struct r8152 *tp, bool enable) |
| 3297 | { |
| 3298 | u16 data; |
| 3299 | |
| 3300 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 3301 | if (enable) { |
| 3302 | data |= EN_ALDPS; |
| 3303 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 3304 | } else { |
hayeswang | 4214cc5 | 2017-06-09 17:11:46 +0800 | [diff] [blame] | 3305 | int i; |
| 3306 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3307 | data &= ~EN_ALDPS; |
| 3308 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
hayeswang | 4214cc5 | 2017-06-09 17:11:46 +0800 | [diff] [blame] | 3309 | for (i = 0; i < 20; i++) { |
| 3310 | usleep_range(1000, 2000); |
| 3311 | if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100) |
| 3312 | break; |
| 3313 | } |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3314 | } |
| 3315 | } |
| 3316 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3317 | static void r8153b_aldps_en(struct r8152 *tp, bool enable) |
| 3318 | { |
| 3319 | r8153_aldps_en(tp, enable); |
| 3320 | |
| 3321 | if (enable) |
| 3322 | r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0); |
| 3323 | else |
| 3324 | r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS); |
| 3325 | } |
| 3326 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3327 | static void r8153_eee_en(struct r8152 *tp, bool enable) |
| 3328 | { |
| 3329 | u32 ocp_data; |
| 3330 | u16 config; |
| 3331 | |
| 3332 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 3333 | config = ocp_reg_read(tp, OCP_EEE_CFG); |
| 3334 | |
| 3335 | if (enable) { |
| 3336 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 3337 | config |= EEE10_EN; |
| 3338 | } else { |
| 3339 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 3340 | config &= ~EEE10_EN; |
| 3341 | } |
| 3342 | |
| 3343 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 3344 | ocp_reg_write(tp, OCP_EEE_CFG, config); |
| 3345 | } |
| 3346 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3347 | static void r8153b_eee_en(struct r8152 *tp, bool enable) |
| 3348 | { |
| 3349 | r8153_eee_en(tp, enable); |
| 3350 | |
| 3351 | if (enable) |
| 3352 | r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0); |
| 3353 | else |
| 3354 | r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE); |
| 3355 | } |
| 3356 | |
| 3357 | static void r8153b_enable_fc(struct r8152 *tp) |
| 3358 | { |
| 3359 | r8152b_enable_fc(tp); |
| 3360 | r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0); |
| 3361 | } |
| 3362 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3363 | static void r8153_hw_phy_cfg(struct r8152 *tp) |
| 3364 | { |
| 3365 | u32 ocp_data; |
| 3366 | u16 data; |
| 3367 | |
hayeswang | d768c61 | 2016-09-20 16:22:09 +0800 | [diff] [blame] | 3368 | /* disable ALDPS before updating the PHY parameters */ |
| 3369 | r8153_aldps_en(tp, false); |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 3370 | |
hayeswang | d768c61 | 2016-09-20 16:22:09 +0800 | [diff] [blame] | 3371 | /* disable EEE before updating the PHY parameters */ |
| 3372 | r8153_eee_en(tp, false); |
| 3373 | ocp_reg_write(tp, OCP_EEE_ADV, 0); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3374 | |
| 3375 | if (tp->version == RTL_VER_03) { |
| 3376 | data = ocp_reg_read(tp, OCP_EEE_CFG); |
| 3377 | data &= ~CTAP_SHORT_EN; |
| 3378 | ocp_reg_write(tp, OCP_EEE_CFG, data); |
| 3379 | } |
| 3380 | |
| 3381 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 3382 | data |= EEE_CLKDIV_EN; |
| 3383 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 3384 | |
| 3385 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 3386 | data |= EN_10M_BGOFF; |
| 3387 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
| 3388 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 3389 | data |= EN_10M_PLLOFF; |
| 3390 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 3391 | sram_write(tp, SRAM_IMPEDANCE, 0x0b13); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3392 | |
| 3393 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 3394 | ocp_data |= PFM_PWM_SWITCH; |
| 3395 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 3396 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 3397 | /* Enable LPF corner auto tune */ |
| 3398 | sram_write(tp, SRAM_LPF_CFG, 0xf70f); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3399 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 3400 | /* Adjust 10M Amplitude */ |
| 3401 | sram_write(tp, SRAM_10M_AMP1, 0x00af); |
| 3402 | sram_write(tp, SRAM_10M_AMP2, 0x0208); |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3403 | |
hayeswang | af0287e | 2016-09-20 16:22:08 +0800 | [diff] [blame] | 3404 | r8153_eee_en(tp, true); |
| 3405 | ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX); |
| 3406 | |
hayeswang | ef39df8 | 2016-09-20 16:22:07 +0800 | [diff] [blame] | 3407 | r8153_aldps_en(tp, true); |
| 3408 | r8152b_enable_fc(tp); |
| 3409 | |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3410 | switch (tp->version) { |
| 3411 | case RTL_VER_03: |
| 3412 | case RTL_VER_04: |
| 3413 | break; |
| 3414 | case RTL_VER_05: |
| 3415 | case RTL_VER_06: |
| 3416 | default: |
| 3417 | r8153_u2p3en(tp, true); |
| 3418 | break; |
| 3419 | } |
| 3420 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3421 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3422 | } |
| 3423 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3424 | static u32 r8152_efuse_read(struct r8152 *tp, u8 addr) |
| 3425 | { |
| 3426 | u32 ocp_data; |
| 3427 | |
| 3428 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr); |
| 3429 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD); |
| 3430 | ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */ |
| 3431 | ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA); |
| 3432 | |
| 3433 | return ocp_data; |
| 3434 | } |
| 3435 | |
| 3436 | static void r8153b_hw_phy_cfg(struct r8152 *tp) |
| 3437 | { |
| 3438 | u32 ocp_data, ups_flags = 0; |
| 3439 | u16 data; |
| 3440 | |
| 3441 | /* disable ALDPS before updating the PHY parameters */ |
| 3442 | r8153b_aldps_en(tp, false); |
| 3443 | |
| 3444 | /* disable EEE before updating the PHY parameters */ |
| 3445 | r8153b_eee_en(tp, false); |
| 3446 | ocp_reg_write(tp, OCP_EEE_ADV, 0); |
| 3447 | |
| 3448 | r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); |
| 3449 | |
| 3450 | data = sram_read(tp, SRAM_GREEN_CFG); |
| 3451 | data |= R_TUNE_EN; |
| 3452 | sram_write(tp, SRAM_GREEN_CFG, data); |
| 3453 | data = ocp_reg_read(tp, OCP_NCTL_CFG); |
| 3454 | data |= PGA_RETURN_EN; |
| 3455 | ocp_reg_write(tp, OCP_NCTL_CFG, data); |
| 3456 | |
| 3457 | /* ADC Bias Calibration: |
| 3458 | * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake |
| 3459 | * bit (bit3) to rebuild the real 16-bit data. Write the data to the |
| 3460 | * ADC ioffset. |
| 3461 | */ |
| 3462 | ocp_data = r8152_efuse_read(tp, 0x7d); |
| 3463 | data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7)); |
| 3464 | if (data != 0xffff) |
| 3465 | ocp_reg_write(tp, OCP_ADC_IOFFSET, data); |
| 3466 | |
| 3467 | /* ups mode tx-link-pulse timing adjustment: |
| 3468 | * rg_saw_cnt = OCP reg 0xC426 Bit[13:0] |
| 3469 | * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt |
| 3470 | */ |
| 3471 | ocp_data = ocp_reg_read(tp, 0xc426); |
| 3472 | ocp_data &= 0x3fff; |
| 3473 | if (ocp_data) { |
| 3474 | u32 swr_cnt_1ms_ini; |
| 3475 | |
| 3476 | swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK; |
| 3477 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG); |
| 3478 | ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini; |
| 3479 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data); |
| 3480 | } |
| 3481 | |
| 3482 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 3483 | ocp_data |= PFM_PWM_SWITCH; |
| 3484 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 3485 | |
| 3486 | /* Advnace EEE */ |
| 3487 | if (!r8153_patch_request(tp, true)) { |
| 3488 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 3489 | data |= EEE_CLKDIV_EN; |
| 3490 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 3491 | |
| 3492 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 3493 | data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV; |
| 3494 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
| 3495 | |
| 3496 | ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); |
| 3497 | ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5)); |
| 3498 | |
| 3499 | ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV | |
| 3500 | UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN | |
| 3501 | UPS_FLAGS_EEE_PLLOFF_GIGA; |
| 3502 | |
| 3503 | r8153_patch_request(tp, false); |
| 3504 | } |
| 3505 | |
| 3506 | r8153b_ups_flags_w1w0(tp, ups_flags, 0); |
| 3507 | |
| 3508 | r8153b_eee_en(tp, true); |
| 3509 | ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX); |
| 3510 | |
| 3511 | r8153b_aldps_en(tp, true); |
| 3512 | r8153b_enable_fc(tp); |
| 3513 | r8153_u2p3en(tp, true); |
| 3514 | |
| 3515 | set_bit(PHY_RESET, &tp->flags); |
| 3516 | } |
| 3517 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3518 | static void r8153_first_init(struct r8152 *tp) |
| 3519 | { |
| 3520 | u32 ocp_data; |
| 3521 | int i; |
| 3522 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3523 | r8153_mac_clk_spd(tp, false); |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3524 | rxdy_gated_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3525 | r8153_teredo_off(tp); |
| 3526 | |
| 3527 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 3528 | ocp_data &= ~RCR_ACPT_ALL; |
| 3529 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 3530 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3531 | rtl8152_nic_reset(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3532 | rtl_reset_bmu(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3533 | |
| 3534 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3535 | ocp_data &= ~NOW_IS_OOB; |
| 3536 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3537 | |
| 3538 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3539 | ocp_data &= ~MCU_BORW_EN; |
| 3540 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3541 | |
| 3542 | for (i = 0; i < 1000; i++) { |
| 3543 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3544 | if (ocp_data & LINK_LIST_READY) |
| 3545 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3546 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3550 | ocp_data |= RE_INIT_LL; |
| 3551 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3552 | |
| 3553 | for (i = 0; i < 1000; i++) { |
| 3554 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3555 | if (ocp_data & LINK_LIST_READY) |
| 3556 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3557 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3558 | } |
| 3559 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 3560 | rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3561 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 3562 | ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 3563 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 3564 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3565 | |
| 3566 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 3567 | ocp_data |= TCR0_AUTO_FIFO; |
| 3568 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 3569 | |
| 3570 | rtl8152_nic_reset(tp); |
| 3571 | |
| 3572 | /* rx share fifo credit full threshold */ |
| 3573 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 3574 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); |
| 3575 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); |
| 3576 | /* TX share fifo free credit full threshold */ |
| 3577 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | static void r8153_enter_oob(struct r8152 *tp) |
| 3581 | { |
| 3582 | u32 ocp_data; |
| 3583 | int i; |
| 3584 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3585 | r8153_mac_clk_spd(tp, true); |
| 3586 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3587 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3588 | ocp_data &= ~NOW_IS_OOB; |
| 3589 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3590 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3591 | rtl_disable(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3592 | rtl_reset_bmu(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3593 | |
| 3594 | for (i = 0; i < 1000; i++) { |
| 3595 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3596 | if (ocp_data & LINK_LIST_READY) |
| 3597 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3598 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3602 | ocp_data |= RE_INIT_LL; |
| 3603 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3604 | |
| 3605 | for (i = 0; i < 1000; i++) { |
| 3606 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3607 | if (ocp_data & LINK_LIST_READY) |
| 3608 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3609 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3610 | } |
| 3611 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 3612 | ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 3613 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3614 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3615 | switch (tp->version) { |
| 3616 | case RTL_VER_03: |
| 3617 | case RTL_VER_04: |
| 3618 | case RTL_VER_05: |
| 3619 | case RTL_VER_06: |
| 3620 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 3621 | ocp_data &= ~TEREDO_WAKE_MASK; |
| 3622 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 3623 | break; |
| 3624 | |
| 3625 | case RTL_VER_08: |
| 3626 | case RTL_VER_09: |
| 3627 | /* Clear teredo wake event. bit[15:8] is the teredo wakeup |
| 3628 | * type. Set it to zero. bits[7:0] are the W1C bits about |
| 3629 | * the events. Set them to all 1 to clear them. |
| 3630 | */ |
| 3631 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); |
| 3632 | break; |
| 3633 | |
| 3634 | default: |
| 3635 | break; |
| 3636 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3637 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 3638 | rtl_rx_vlan_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3639 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 3640 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3641 | ocp_data |= ALDPS_PROXY_MODE; |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 3642 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3643 | |
| 3644 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3645 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 3646 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3647 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3648 | rxdy_gated_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3649 | |
| 3650 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 3651 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 3652 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 3653 | } |
| 3654 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3655 | static void rtl8153_disable(struct r8152 *tp) |
| 3656 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3657 | r8153_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3658 | rtl_disable(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3659 | rtl_reset_bmu(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3660 | r8153_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3661 | } |
| 3662 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3663 | static void rtl8153b_disable(struct r8152 *tp) |
| 3664 | { |
| 3665 | r8153b_aldps_en(tp, false); |
| 3666 | rtl_disable(tp); |
| 3667 | rtl_reset_bmu(tp); |
| 3668 | r8153b_aldps_en(tp, true); |
| 3669 | } |
| 3670 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3671 | static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex) |
| 3672 | { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3673 | u16 bmcr, anar, gbcr; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3674 | enum spd_duplex speed_duplex; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3675 | int ret = 0; |
| 3676 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3677 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 3678 | anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL | |
| 3679 | ADVERTISE_100HALF | ADVERTISE_100FULL); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3680 | if (tp->mii.supports_gmii) { |
| 3681 | gbcr = r8152_mdio_read(tp, MII_CTRL1000); |
| 3682 | gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
| 3683 | } else { |
| 3684 | gbcr = 0; |
| 3685 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3686 | |
| 3687 | if (autoneg == AUTONEG_DISABLE) { |
| 3688 | if (speed == SPEED_10) { |
| 3689 | bmcr = 0; |
| 3690 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3691 | speed_duplex = FORCE_10M_HALF; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3692 | } else if (speed == SPEED_100) { |
| 3693 | bmcr = BMCR_SPEED100; |
| 3694 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3695 | speed_duplex = FORCE_100M_HALF; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3696 | } else if (speed == SPEED_1000 && tp->mii.supports_gmii) { |
| 3697 | bmcr = BMCR_SPEED1000; |
| 3698 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3699 | speed_duplex = NWAY_1000M_FULL; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3700 | } else { |
| 3701 | ret = -EINVAL; |
| 3702 | goto out; |
| 3703 | } |
| 3704 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3705 | if (duplex == DUPLEX_FULL) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3706 | bmcr |= BMCR_FULLDPLX; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3707 | if (speed != SPEED_1000) |
| 3708 | speed_duplex++; |
| 3709 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3710 | } else { |
| 3711 | if (speed == SPEED_10) { |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3712 | if (duplex == DUPLEX_FULL) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3713 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3714 | speed_duplex = NWAY_10M_FULL; |
| 3715 | } else { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3716 | anar |= ADVERTISE_10HALF; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3717 | speed_duplex = NWAY_10M_HALF; |
| 3718 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3719 | } else if (speed == SPEED_100) { |
| 3720 | if (duplex == DUPLEX_FULL) { |
| 3721 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 3722 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3723 | speed_duplex = NWAY_100M_FULL; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3724 | } else { |
| 3725 | anar |= ADVERTISE_10HALF; |
| 3726 | anar |= ADVERTISE_100HALF; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3727 | speed_duplex = NWAY_100M_HALF; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3728 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3729 | } else if (speed == SPEED_1000 && tp->mii.supports_gmii) { |
| 3730 | if (duplex == DUPLEX_FULL) { |
| 3731 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 3732 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 3733 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
| 3734 | } else { |
| 3735 | anar |= ADVERTISE_10HALF; |
| 3736 | anar |= ADVERTISE_100HALF; |
| 3737 | gbcr |= ADVERTISE_1000HALF; |
| 3738 | } |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3739 | speed_duplex = NWAY_1000M_FULL; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3740 | } else { |
| 3741 | ret = -EINVAL; |
| 3742 | goto out; |
| 3743 | } |
| 3744 | |
| 3745 | bmcr = BMCR_ANENABLE | BMCR_ANRESTART; |
| 3746 | } |
| 3747 | |
hayeswang | fae5617 | 2016-06-16 14:08:29 +0800 | [diff] [blame] | 3748 | if (test_and_clear_bit(PHY_RESET, &tp->flags)) |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3749 | bmcr |= BMCR_RESET; |
| 3750 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3751 | if (tp->mii.supports_gmii) |
| 3752 | r8152_mdio_write(tp, MII_CTRL1000, gbcr); |
| 3753 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3754 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 3755 | r8152_mdio_write(tp, MII_BMCR, bmcr); |
| 3756 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3757 | switch (tp->version) { |
| 3758 | case RTL_VER_08: |
| 3759 | case RTL_VER_09: |
| 3760 | r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex), |
| 3761 | UPS_FLAGS_SPEED_MASK); |
| 3762 | break; |
| 3763 | |
| 3764 | default: |
| 3765 | break; |
| 3766 | } |
| 3767 | |
hayeswang | fae5617 | 2016-06-16 14:08:29 +0800 | [diff] [blame] | 3768 | if (bmcr & BMCR_RESET) { |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3769 | int i; |
| 3770 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3771 | for (i = 0; i < 50; i++) { |
| 3772 | msleep(20); |
| 3773 | if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) |
| 3774 | break; |
| 3775 | } |
| 3776 | } |
| 3777 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3778 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3779 | return ret; |
| 3780 | } |
| 3781 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3782 | static void rtl8152_up(struct r8152 *tp) |
| 3783 | { |
| 3784 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3785 | return; |
| 3786 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3787 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3788 | r8152b_exit_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3789 | r8152_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3790 | } |
| 3791 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3792 | static void rtl8152_down(struct r8152 *tp) |
| 3793 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 3794 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 3795 | rtl_drop_queued_tx(tp); |
| 3796 | return; |
| 3797 | } |
| 3798 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3799 | r8152_power_cut_en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3800 | r8152_aldps_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3801 | r8152b_enter_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3802 | r8152_aldps_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3805 | static void rtl8153_up(struct r8152 *tp) |
| 3806 | { |
| 3807 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3808 | return; |
| 3809 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3810 | r8153_u1u2en(tp, false); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3811 | r8153_u2p3en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3812 | r8153_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3813 | r8153_first_init(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3814 | r8153_aldps_en(tp, true); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3815 | |
| 3816 | switch (tp->version) { |
| 3817 | case RTL_VER_03: |
| 3818 | case RTL_VER_04: |
| 3819 | break; |
| 3820 | case RTL_VER_05: |
| 3821 | case RTL_VER_06: |
| 3822 | default: |
| 3823 | r8153_u2p3en(tp, true); |
| 3824 | break; |
| 3825 | } |
| 3826 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3827 | r8153_u1u2en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3828 | } |
| 3829 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3830 | static void rtl8153_down(struct r8152 *tp) |
| 3831 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 3832 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 3833 | rtl_drop_queued_tx(tp); |
| 3834 | return; |
| 3835 | } |
| 3836 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 3837 | r8153_u1u2en(tp, false); |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3838 | r8153_u2p3en(tp, false); |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 3839 | r8153_power_cut_en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3840 | r8153_aldps_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3841 | r8153_enter_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3842 | r8153_aldps_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3843 | } |
| 3844 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3845 | static void rtl8153b_up(struct r8152 *tp) |
| 3846 | { |
| 3847 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3848 | return; |
| 3849 | |
| 3850 | r8153b_u1u2en(tp, false); |
| 3851 | r8153_u2p3en(tp, false); |
| 3852 | r8153b_aldps_en(tp, false); |
| 3853 | |
| 3854 | r8153_first_init(tp); |
| 3855 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); |
| 3856 | |
| 3857 | r8153b_aldps_en(tp, true); |
| 3858 | r8153_u2p3en(tp, true); |
| 3859 | r8153b_u1u2en(tp, true); |
| 3860 | } |
| 3861 | |
| 3862 | static void rtl8153b_down(struct r8152 *tp) |
| 3863 | { |
| 3864 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 3865 | rtl_drop_queued_tx(tp); |
| 3866 | return; |
| 3867 | } |
| 3868 | |
| 3869 | r8153b_u1u2en(tp, false); |
| 3870 | r8153_u2p3en(tp, false); |
| 3871 | r8153b_power_cut_en(tp, false); |
| 3872 | r8153b_aldps_en(tp, false); |
| 3873 | r8153_enter_oob(tp); |
| 3874 | r8153b_aldps_en(tp, true); |
| 3875 | } |
| 3876 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 3877 | static bool rtl8152_in_nway(struct r8152 *tp) |
| 3878 | { |
| 3879 | u16 nway_state; |
| 3880 | |
| 3881 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000); |
| 3882 | tp->ocp_base = 0x2000; |
| 3883 | ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */ |
| 3884 | nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a); |
| 3885 | |
| 3886 | /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */ |
| 3887 | if (nway_state & 0xc000) |
| 3888 | return false; |
| 3889 | else |
| 3890 | return true; |
| 3891 | } |
| 3892 | |
| 3893 | static bool rtl8153_in_nway(struct r8152 *tp) |
| 3894 | { |
| 3895 | u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff; |
| 3896 | |
| 3897 | if (phy_state == TXDIS_STATE || phy_state == ABD_STATE) |
| 3898 | return false; |
| 3899 | else |
| 3900 | return true; |
| 3901 | } |
| 3902 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3903 | static void set_carrier(struct r8152 *tp) |
| 3904 | { |
| 3905 | struct net_device *netdev = tp->netdev; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 3906 | struct napi_struct *napi = &tp->napi; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3907 | u8 speed; |
| 3908 | |
| 3909 | speed = rtl8152_get_speed(tp); |
| 3910 | |
| 3911 | if (speed & LINK_STATUS) { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 3912 | if (!netif_carrier_ok(netdev)) { |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 3913 | tp->rtl_ops.enable(tp); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 3914 | netif_stop_queue(netdev); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 3915 | napi_disable(napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3916 | netif_carrier_on(netdev); |
hayeswang | aa2e092 | 2015-01-09 10:26:35 +0800 | [diff] [blame] | 3917 | rtl_start_rx(tp); |
Hayes Wang | aece477 | 2018-02-02 16:43:36 +0800 | [diff] [blame] | 3918 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 3919 | _rtl8152_set_rx_mode(netdev); |
hayeswang | 41cec84 | 2015-07-24 13:54:25 +0800 | [diff] [blame] | 3920 | napi_enable(&tp->napi); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 3921 | netif_wake_queue(netdev); |
| 3922 | netif_info(tp, link, netdev, "carrier on\n"); |
hayeswang | 2f25abe | 2017-03-23 19:14:19 +0800 | [diff] [blame] | 3923 | } else if (netif_queue_stopped(netdev) && |
| 3924 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) { |
| 3925 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3926 | } |
| 3927 | } else { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 3928 | if (netif_carrier_ok(netdev)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3929 | netif_carrier_off(netdev); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 3930 | napi_disable(napi); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 3931 | tp->rtl_ops.disable(tp); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 3932 | napi_enable(napi); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 3933 | netif_info(tp, link, netdev, "carrier off\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3934 | } |
| 3935 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | static void rtl_work_func_t(struct work_struct *work) |
| 3939 | { |
| 3940 | struct r8152 *tp = container_of(work, struct r8152, schedule.work); |
| 3941 | |
hayeswang | a1f83fe | 2014-11-12 10:05:05 +0800 | [diff] [blame] | 3942 | /* If the device is unplugged or !netif_running(), the workqueue |
| 3943 | * doesn't need to wake the device, and could return directly. |
| 3944 | */ |
| 3945 | if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev)) |
| 3946 | return; |
| 3947 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3948 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 3949 | return; |
| 3950 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3951 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 3952 | goto out1; |
| 3953 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3954 | if (!mutex_trylock(&tp->control)) { |
| 3955 | schedule_delayed_work(&tp->schedule, 0); |
| 3956 | goto out1; |
| 3957 | } |
| 3958 | |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 3959 | if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags)) |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 3960 | set_carrier(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3961 | |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 3962 | if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3963 | _rtl8152_set_rx_mode(tp->netdev); |
| 3964 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 3965 | /* don't schedule napi before linking */ |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 3966 | if (test_and_clear_bit(SCHEDULE_NAPI, &tp->flags) && |
| 3967 | netif_carrier_ok(tp->netdev)) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 3968 | napi_schedule(&tp->napi); |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3969 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3970 | mutex_unlock(&tp->control); |
| 3971 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3972 | out1: |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3973 | usb_autopm_put_interface(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 3976 | static void rtl_hw_phy_work_func_t(struct work_struct *work) |
| 3977 | { |
| 3978 | struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work); |
| 3979 | |
| 3980 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3981 | return; |
| 3982 | |
| 3983 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 3984 | return; |
| 3985 | |
| 3986 | mutex_lock(&tp->control); |
| 3987 | |
| 3988 | tp->rtl_ops.hw_phy_cfg(tp); |
| 3989 | |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 3990 | rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex); |
hayeswang | 9d21c0d | 2016-06-13 17:49:37 +0800 | [diff] [blame] | 3991 | |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 3992 | mutex_unlock(&tp->control); |
| 3993 | |
| 3994 | usb_autopm_put_interface(tp->intf); |
| 3995 | } |
| 3996 | |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 3997 | #ifdef CONFIG_PM_SLEEP |
| 3998 | static int rtl_notifier(struct notifier_block *nb, unsigned long action, |
| 3999 | void *data) |
| 4000 | { |
| 4001 | struct r8152 *tp = container_of(nb, struct r8152, pm_notifier); |
| 4002 | |
| 4003 | switch (action) { |
| 4004 | case PM_HIBERNATION_PREPARE: |
| 4005 | case PM_SUSPEND_PREPARE: |
| 4006 | usb_autopm_get_interface(tp->intf); |
| 4007 | break; |
| 4008 | |
| 4009 | case PM_POST_HIBERNATION: |
| 4010 | case PM_POST_SUSPEND: |
| 4011 | usb_autopm_put_interface(tp->intf); |
| 4012 | break; |
| 4013 | |
| 4014 | case PM_POST_RESTORE: |
| 4015 | case PM_RESTORE_PREPARE: |
| 4016 | default: |
| 4017 | break; |
| 4018 | } |
| 4019 | |
| 4020 | return NOTIFY_DONE; |
| 4021 | } |
| 4022 | #endif |
| 4023 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4024 | static int rtl8152_open(struct net_device *netdev) |
| 4025 | { |
| 4026 | struct r8152 *tp = netdev_priv(netdev); |
| 4027 | int res = 0; |
| 4028 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 4029 | res = alloc_all_mem(tp); |
| 4030 | if (res) |
| 4031 | goto out; |
| 4032 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4033 | res = usb_autopm_get_interface(tp->intf); |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4034 | if (res < 0) |
| 4035 | goto out_free; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4036 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4037 | mutex_lock(&tp->control); |
| 4038 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 4039 | tp->rtl_ops.up(tp); |
| 4040 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 4041 | netif_carrier_off(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4042 | netif_start_queue(netdev); |
| 4043 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 4044 | |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 4045 | res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
| 4046 | if (res) { |
| 4047 | if (res == -ENODEV) |
| 4048 | netif_device_detach(tp->netdev); |
| 4049 | netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", |
| 4050 | res); |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4051 | goto out_unlock; |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 4052 | } |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4053 | napi_enable(&tp->napi); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 4054 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4055 | mutex_unlock(&tp->control); |
| 4056 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4057 | usb_autopm_put_interface(tp->intf); |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 4058 | #ifdef CONFIG_PM_SLEEP |
| 4059 | tp->pm_notifier.notifier_call = rtl_notifier; |
| 4060 | register_pm_notifier(&tp->pm_notifier); |
| 4061 | #endif |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4062 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4063 | |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4064 | out_unlock: |
| 4065 | mutex_unlock(&tp->control); |
| 4066 | usb_autopm_put_interface(tp->intf); |
| 4067 | out_free: |
| 4068 | free_all_mem(tp); |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 4069 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4070 | return res; |
| 4071 | } |
| 4072 | |
| 4073 | static int rtl8152_close(struct net_device *netdev) |
| 4074 | { |
| 4075 | struct r8152 *tp = netdev_priv(netdev); |
| 4076 | int res = 0; |
| 4077 | |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 4078 | #ifdef CONFIG_PM_SLEEP |
| 4079 | unregister_pm_notifier(&tp->pm_notifier); |
| 4080 | #endif |
Jiri Slaby | 0ee1f47 | 2018-06-25 09:26:27 +0200 | [diff] [blame] | 4081 | if (!test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4082 | napi_disable(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4083 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 4084 | usb_kill_urb(tp->intr_urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4085 | cancel_delayed_work_sync(&tp->schedule); |
| 4086 | netif_stop_queue(netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4087 | |
| 4088 | res = usb_autopm_get_interface(tp->intf); |
hayeswang | 53543db | 2015-02-06 11:30:48 +0800 | [diff] [blame] | 4089 | if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) { |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4090 | rtl_drop_queued_tx(tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 4091 | rtl_stop_rx(tp); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4092 | } else { |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4093 | mutex_lock(&tp->control); |
| 4094 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4095 | tp->rtl_ops.down(tp); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4096 | |
| 4097 | mutex_unlock(&tp->control); |
| 4098 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4099 | usb_autopm_put_interface(tp->intf); |
| 4100 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4101 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 4102 | free_all_mem(tp); |
| 4103 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4104 | return res; |
| 4105 | } |
| 4106 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4107 | static void rtl_tally_reset(struct r8152 *tp) |
| 4108 | { |
| 4109 | u32 ocp_data; |
| 4110 | |
| 4111 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY); |
| 4112 | ocp_data |= TALLY_RESET; |
| 4113 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data); |
| 4114 | } |
| 4115 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4116 | static void r8152b_init(struct r8152 *tp) |
| 4117 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4118 | u32 ocp_data; |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4119 | u16 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4120 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4121 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4122 | return; |
| 4123 | |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4124 | data = r8152_mdio_read(tp, MII_BMCR); |
| 4125 | if (data & BMCR_PDOWN) { |
| 4126 | data &= ~BMCR_PDOWN; |
| 4127 | r8152_mdio_write(tp, MII_BMCR, data); |
| 4128 | } |
| 4129 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4130 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4131 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4132 | if (tp->version == RTL_VER_01) { |
| 4133 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 4134 | ocp_data &= ~LED_MODE_MASK; |
| 4135 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 4136 | } |
| 4137 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4138 | r8152_power_cut_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4139 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4140 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 4141 | ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; |
| 4142 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 4143 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); |
| 4144 | ocp_data &= ~MCU_CLK_RATIO_MASK; |
| 4145 | ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; |
| 4146 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); |
| 4147 | ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | |
| 4148 | SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; |
| 4149 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); |
| 4150 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4151 | rtl_tally_reset(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4152 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4153 | /* enable rx aggregation */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4154 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 4155 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4156 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 4157 | } |
| 4158 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4159 | static void r8153_init(struct r8152 *tp) |
| 4160 | { |
| 4161 | u32 ocp_data; |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4162 | u16 data; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4163 | int i; |
| 4164 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4165 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4166 | return; |
| 4167 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 4168 | r8153_u1u2en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4169 | |
| 4170 | for (i = 0; i < 500; i++) { |
| 4171 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 4172 | AUTOLOAD_DONE) |
| 4173 | break; |
| 4174 | msleep(20); |
| 4175 | } |
| 4176 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 4177 | data = r8153_phy_status(tp, 0); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4178 | |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4179 | if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || |
| 4180 | tp->version == RTL_VER_05) |
| 4181 | ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); |
| 4182 | |
| 4183 | data = r8152_mdio_read(tp, MII_BMCR); |
| 4184 | if (data & BMCR_PDOWN) { |
| 4185 | data &= ~BMCR_PDOWN; |
| 4186 | r8152_mdio_write(tp, MII_BMCR, data); |
| 4187 | } |
| 4188 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 4189 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4190 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 4191 | r8153_u2p3en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4192 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 4193 | if (tp->version == RTL_VER_04) { |
| 4194 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2); |
| 4195 | ocp_data &= ~pwd_dn_scale_mask; |
| 4196 | ocp_data |= pwd_dn_scale(96); |
| 4197 | ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data); |
| 4198 | |
| 4199 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); |
| 4200 | ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; |
| 4201 | ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); |
| 4202 | } else if (tp->version == RTL_VER_05) { |
| 4203 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0); |
| 4204 | ocp_data &= ~ECM_ALDPS; |
| 4205 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data); |
| 4206 | |
| 4207 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 4208 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 4209 | ocp_data &= ~DYNAMIC_BURST; |
| 4210 | else |
| 4211 | ocp_data |= DYNAMIC_BURST; |
| 4212 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 4213 | } else if (tp->version == RTL_VER_06) { |
| 4214 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 4215 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 4216 | ocp_data &= ~DYNAMIC_BURST; |
| 4217 | else |
| 4218 | ocp_data |= DYNAMIC_BURST; |
| 4219 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2); |
| 4223 | ocp_data |= EP4_FULL_FC; |
| 4224 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data); |
| 4225 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4226 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); |
| 4227 | ocp_data &= ~TIMER11_EN; |
| 4228 | ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); |
| 4229 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4230 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 4231 | ocp_data &= ~LED_MODE_MASK; |
| 4232 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 4233 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 4234 | ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; |
Oliver Neukum | 2b84af94a | 2016-05-02 13:06:14 +0200 | [diff] [blame] | 4235 | if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4236 | ocp_data |= LPM_TIMER_500MS; |
hayeswang | 34203e2 | 2015-02-06 11:30:46 +0800 | [diff] [blame] | 4237 | else |
| 4238 | ocp_data |= LPM_TIMER_500US; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4239 | ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); |
| 4240 | |
| 4241 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); |
| 4242 | ocp_data &= ~SEN_VAL_MASK; |
| 4243 | ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; |
| 4244 | ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); |
| 4245 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 4246 | ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); |
| 4247 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 4248 | r8153_power_cut_en(tp, false); |
| 4249 | r8153_u1u2en(tp, true); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 4250 | r8153_mac_clk_spd(tp, false); |
hayeswang | ee4761c | 2017-06-09 17:11:39 +0800 | [diff] [blame] | 4251 | usb_enable_lpm(tp->udev); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4252 | |
hayeswang | e31f636 | 2017-06-09 17:11:41 +0800 | [diff] [blame] | 4253 | /* rx aggregation */ |
| 4254 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 4255 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 4256 | if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags)) |
| 4257 | ocp_data |= RX_AGG_DISABLE; |
| 4258 | |
hayeswang | e31f636 | 2017-06-09 17:11:41 +0800 | [diff] [blame] | 4259 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4260 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4261 | rtl_tally_reset(tp); |
hayeswang | 49d1034 | 2017-06-09 17:11:44 +0800 | [diff] [blame] | 4262 | |
| 4263 | switch (tp->udev->speed) { |
| 4264 | case USB_SPEED_SUPER: |
| 4265 | case USB_SPEED_SUPER_PLUS: |
| 4266 | tp->coalesce = COALESCE_SUPER; |
| 4267 | break; |
| 4268 | case USB_SPEED_HIGH: |
| 4269 | tp->coalesce = COALESCE_HIGH; |
| 4270 | break; |
| 4271 | default: |
| 4272 | tp->coalesce = COALESCE_SLOW; |
| 4273 | break; |
| 4274 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4275 | } |
| 4276 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4277 | static void r8153b_init(struct r8152 *tp) |
| 4278 | { |
| 4279 | u32 ocp_data; |
| 4280 | u16 data; |
| 4281 | int i; |
| 4282 | |
| 4283 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4284 | return; |
| 4285 | |
| 4286 | r8153b_u1u2en(tp, false); |
| 4287 | |
| 4288 | for (i = 0; i < 500; i++) { |
| 4289 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 4290 | AUTOLOAD_DONE) |
| 4291 | break; |
| 4292 | msleep(20); |
| 4293 | } |
| 4294 | |
| 4295 | data = r8153_phy_status(tp, 0); |
| 4296 | |
| 4297 | data = r8152_mdio_read(tp, MII_BMCR); |
| 4298 | if (data & BMCR_PDOWN) { |
| 4299 | data &= ~BMCR_PDOWN; |
| 4300 | r8152_mdio_write(tp, MII_BMCR, data); |
| 4301 | } |
| 4302 | |
| 4303 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
| 4304 | |
| 4305 | r8153_u2p3en(tp, false); |
| 4306 | |
| 4307 | /* MSC timer = 0xfff * 8ms = 32760 ms */ |
| 4308 | ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); |
| 4309 | |
| 4310 | /* U1/U2/L1 idle timer. 500 us */ |
| 4311 | ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); |
| 4312 | |
| 4313 | r8153b_power_cut_en(tp, false); |
| 4314 | r8153b_ups_en(tp, false); |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 4315 | r8153_queue_wake(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4316 | rtl_runtime_suspend_enable(tp, false); |
| 4317 | r8153b_u1u2en(tp, true); |
| 4318 | usb_enable_lpm(tp->udev); |
| 4319 | |
| 4320 | /* MAC clock speed down */ |
| 4321 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); |
| 4322 | ocp_data |= MAC_CLK_SPDWN_EN; |
| 4323 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); |
| 4324 | |
| 4325 | set_bit(GREEN_ETHERNET, &tp->flags); |
| 4326 | |
| 4327 | /* rx aggregation */ |
| 4328 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 4329 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
| 4330 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 4331 | |
| 4332 | rtl_tally_reset(tp); |
| 4333 | |
| 4334 | tp->coalesce = 15000; /* 15 us */ |
| 4335 | } |
| 4336 | |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4337 | static int rtl8152_pre_reset(struct usb_interface *intf) |
| 4338 | { |
| 4339 | struct r8152 *tp = usb_get_intfdata(intf); |
| 4340 | struct net_device *netdev; |
| 4341 | |
| 4342 | if (!tp) |
| 4343 | return 0; |
| 4344 | |
| 4345 | netdev = tp->netdev; |
| 4346 | if (!netif_running(netdev)) |
| 4347 | return 0; |
| 4348 | |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 4349 | netif_stop_queue(netdev); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4350 | napi_disable(&tp->napi); |
| 4351 | clear_bit(WORK_ENABLE, &tp->flags); |
| 4352 | usb_kill_urb(tp->intr_urb); |
| 4353 | cancel_delayed_work_sync(&tp->schedule); |
| 4354 | if (netif_carrier_ok(netdev)) { |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4355 | mutex_lock(&tp->control); |
| 4356 | tp->rtl_ops.disable(tp); |
| 4357 | mutex_unlock(&tp->control); |
| 4358 | } |
| 4359 | |
| 4360 | return 0; |
| 4361 | } |
| 4362 | |
| 4363 | static int rtl8152_post_reset(struct usb_interface *intf) |
| 4364 | { |
| 4365 | struct r8152 *tp = usb_get_intfdata(intf); |
| 4366 | struct net_device *netdev; |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 4367 | struct sockaddr sa; |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4368 | |
| 4369 | if (!tp) |
| 4370 | return 0; |
| 4371 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 4372 | /* reset the MAC adddress in case of policy change */ |
| 4373 | if (determine_ethernet_addr(tp, &sa) >= 0) { |
| 4374 | rtnl_lock(); |
| 4375 | dev_set_mac_address (tp->netdev, &sa, NULL); |
| 4376 | rtnl_unlock(); |
| 4377 | } |
| 4378 | |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4379 | netdev = tp->netdev; |
| 4380 | if (!netif_running(netdev)) |
| 4381 | return 0; |
| 4382 | |
| 4383 | set_bit(WORK_ENABLE, &tp->flags); |
| 4384 | if (netif_carrier_ok(netdev)) { |
| 4385 | mutex_lock(&tp->control); |
| 4386 | tp->rtl_ops.enable(tp); |
hayeswang | 2c561b2 | 2017-01-20 14:33:55 +0800 | [diff] [blame] | 4387 | rtl_start_rx(tp); |
Hayes Wang | aece477 | 2018-02-02 16:43:36 +0800 | [diff] [blame] | 4388 | _rtl8152_set_rx_mode(netdev); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4389 | mutex_unlock(&tp->control); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | napi_enable(&tp->napi); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 4393 | netif_wake_queue(netdev); |
hayeswang | 2c561b2 | 2017-01-20 14:33:55 +0800 | [diff] [blame] | 4394 | usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4395 | |
hayeswang | 7489bda | 2017-01-26 09:38:34 +0800 | [diff] [blame] | 4396 | if (!list_empty(&tp->rx_done)) |
| 4397 | napi_schedule(&tp->napi); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4398 | |
| 4399 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 4402 | static bool delay_autosuspend(struct r8152 *tp) |
| 4403 | { |
| 4404 | bool sw_linking = !!netif_carrier_ok(tp->netdev); |
| 4405 | bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS); |
| 4406 | |
| 4407 | /* This means a linking change occurs and the driver doesn't detect it, |
| 4408 | * yet. If the driver has disabled tx/rx and hw is linking on, the |
| 4409 | * device wouldn't wake up by receiving any packet. |
| 4410 | */ |
| 4411 | if (work_busy(&tp->schedule.work) || sw_linking != hw_linking) |
| 4412 | return true; |
| 4413 | |
| 4414 | /* If the linking down is occurred by nway, the device may miss the |
| 4415 | * linking change event. And it wouldn't wake when linking on. |
| 4416 | */ |
| 4417 | if (!sw_linking && tp->rtl_ops.in_nway(tp)) |
| 4418 | return true; |
hayeswang | 6a0b76c | 2017-01-23 14:18:43 +0800 | [diff] [blame] | 4419 | else if (!skb_queue_empty(&tp->tx_queue)) |
| 4420 | return true; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 4421 | else |
| 4422 | return false; |
| 4423 | } |
| 4424 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 4425 | static int rtl8152_runtime_resume(struct r8152 *tp) |
| 4426 | { |
| 4427 | struct net_device *netdev = tp->netdev; |
| 4428 | |
| 4429 | if (netif_running(netdev) && netdev->flags & IFF_UP) { |
| 4430 | struct napi_struct *napi = &tp->napi; |
| 4431 | |
| 4432 | tp->rtl_ops.autosuspend_en(tp, false); |
| 4433 | napi_disable(napi); |
| 4434 | set_bit(WORK_ENABLE, &tp->flags); |
| 4435 | |
| 4436 | if (netif_carrier_ok(netdev)) { |
| 4437 | if (rtl8152_get_speed(tp) & LINK_STATUS) { |
| 4438 | rtl_start_rx(tp); |
| 4439 | } else { |
| 4440 | netif_carrier_off(netdev); |
| 4441 | tp->rtl_ops.disable(tp); |
| 4442 | netif_info(tp, link, netdev, "linking down\n"); |
| 4443 | } |
| 4444 | } |
| 4445 | |
| 4446 | napi_enable(napi); |
| 4447 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 4448 | smp_mb__after_atomic(); |
| 4449 | |
| 4450 | if (!list_empty(&tp->rx_done)) |
| 4451 | napi_schedule(&tp->napi); |
| 4452 | |
| 4453 | usb_submit_urb(tp->intr_urb, GFP_NOIO); |
| 4454 | } else { |
| 4455 | if (netdev->flags & IFF_UP) |
| 4456 | tp->rtl_ops.autosuspend_en(tp, false); |
| 4457 | |
| 4458 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 4459 | } |
| 4460 | |
| 4461 | return 0; |
| 4462 | } |
| 4463 | |
| 4464 | static int rtl8152_system_resume(struct r8152 *tp) |
| 4465 | { |
| 4466 | struct net_device *netdev = tp->netdev; |
| 4467 | |
| 4468 | netif_device_attach(netdev); |
| 4469 | |
| 4470 | if (netif_running(netdev) && netdev->flags & IFF_UP) { |
| 4471 | tp->rtl_ops.up(tp); |
| 4472 | netif_carrier_off(netdev); |
| 4473 | set_bit(WORK_ENABLE, &tp->flags); |
| 4474 | usb_submit_urb(tp->intr_urb, GFP_NOIO); |
| 4475 | } |
| 4476 | |
| 4477 | return 0; |
| 4478 | } |
| 4479 | |
hayeswang | a9c54ad | 2017-01-25 13:41:45 +0800 | [diff] [blame] | 4480 | static int rtl8152_runtime_suspend(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4481 | { |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 4482 | struct net_device *netdev = tp->netdev; |
| 4483 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4484 | |
hayeswang | 26afec3 | 2017-01-26 09:38:31 +0800 | [diff] [blame] | 4485 | set_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 4486 | smp_mb__after_atomic(); |
| 4487 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4488 | if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4489 | u32 rcr = 0; |
| 4490 | |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4491 | if (netif_carrier_ok(netdev)) { |
| 4492 | u32 ocp_data; |
| 4493 | |
| 4494 | rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4495 | ocp_data = rcr & ~RCR_ACPT_ALL; |
| 4496 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4497 | rxdy_gated_en(tp, true); |
| 4498 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, |
| 4499 | PLA_OOB_CTRL); |
| 4500 | if (!(ocp_data & RXFIFO_EMPTY)) { |
| 4501 | rxdy_gated_en(tp, false); |
| 4502 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); |
hayeswang | 26afec3 | 2017-01-26 09:38:31 +0800 | [diff] [blame] | 4503 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 4504 | smp_mb__after_atomic(); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4505 | ret = -EBUSY; |
| 4506 | goto out1; |
| 4507 | } |
| 4508 | } |
| 4509 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4510 | clear_bit(WORK_ENABLE, &tp->flags); |
| 4511 | usb_kill_urb(tp->intr_urb); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4512 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4513 | tp->rtl_ops.autosuspend_en(tp, true); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4514 | |
| 4515 | if (netif_carrier_ok(netdev)) { |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4516 | struct napi_struct *napi = &tp->napi; |
| 4517 | |
| 4518 | napi_disable(napi); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4519 | rtl_stop_rx(tp); |
| 4520 | rxdy_gated_en(tp, false); |
| 4521 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4522 | napi_enable(napi); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4523 | } |
hayeswang | bd88298 | 2017-06-13 15:14:40 +0800 | [diff] [blame] | 4524 | |
| 4525 | if (delay_autosuspend(tp)) { |
| 4526 | rtl8152_runtime_resume(tp); |
| 4527 | ret = -EBUSY; |
| 4528 | } |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 4529 | } |
| 4530 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4531 | out1: |
| 4532 | return ret; |
| 4533 | } |
| 4534 | |
| 4535 | static int rtl8152_system_suspend(struct r8152 *tp) |
| 4536 | { |
| 4537 | struct net_device *netdev = tp->netdev; |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4538 | |
| 4539 | netif_device_detach(netdev); |
| 4540 | |
hayeswang | e3bd1a8 | 2014-10-29 11:12:17 +0800 | [diff] [blame] | 4541 | if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4542 | struct napi_struct *napi = &tp->napi; |
| 4543 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4544 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 4545 | usb_kill_urb(tp->intr_urb); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4546 | napi_disable(napi); |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4547 | cancel_delayed_work_sync(&tp->schedule); |
| 4548 | tp->rtl_ops.down(tp); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4549 | napi_enable(napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4550 | } |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4551 | |
zhong jiang | f741917 | 2018-08-09 09:39:13 +0800 | [diff] [blame] | 4552 | return 0; |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4553 | } |
| 4554 | |
| 4555 | static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) |
| 4556 | { |
| 4557 | struct r8152 *tp = usb_get_intfdata(intf); |
| 4558 | int ret; |
| 4559 | |
| 4560 | mutex_lock(&tp->control); |
| 4561 | |
| 4562 | if (PMSG_IS_AUTO(message)) |
hayeswang | a9c54ad | 2017-01-25 13:41:45 +0800 | [diff] [blame] | 4563 | ret = rtl8152_runtime_suspend(tp); |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4564 | else |
| 4565 | ret = rtl8152_system_suspend(tp); |
| 4566 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4567 | mutex_unlock(&tp->control); |
| 4568 | |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 4569 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
| 4572 | static int rtl8152_resume(struct usb_interface *intf) |
| 4573 | { |
| 4574 | struct r8152 *tp = usb_get_intfdata(intf); |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 4575 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4576 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4577 | mutex_lock(&tp->control); |
| 4578 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 4579 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) |
| 4580 | ret = rtl8152_runtime_resume(tp); |
| 4581 | else |
| 4582 | ret = rtl8152_system_resume(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4583 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4584 | mutex_unlock(&tp->control); |
| 4585 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 4586 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 4589 | static int rtl8152_reset_resume(struct usb_interface *intf) |
| 4590 | { |
| 4591 | struct r8152 *tp = usb_get_intfdata(intf); |
| 4592 | |
| 4593 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
hayeswang | befb2de | 2017-06-09 17:11:45 +0800 | [diff] [blame] | 4594 | mutex_lock(&tp->control); |
| 4595 | tp->rtl_ops.init(tp); |
| 4596 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); |
| 4597 | mutex_unlock(&tp->control); |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 4598 | return rtl8152_resume(intf); |
| 4599 | } |
| 4600 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4601 | static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 4602 | { |
| 4603 | struct r8152 *tp = netdev_priv(dev); |
| 4604 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4605 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 4606 | return; |
| 4607 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 4608 | if (!rtl_can_wakeup(tp)) { |
| 4609 | wol->supported = 0; |
| 4610 | wol->wolopts = 0; |
| 4611 | } else { |
| 4612 | mutex_lock(&tp->control); |
| 4613 | wol->supported = WAKE_ANY; |
| 4614 | wol->wolopts = __rtl_get_wol(tp); |
| 4615 | mutex_unlock(&tp->control); |
| 4616 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4617 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4618 | usb_autopm_put_interface(tp->intf); |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 4622 | { |
| 4623 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4624 | int ret; |
| 4625 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 4626 | if (!rtl_can_wakeup(tp)) |
| 4627 | return -EOPNOTSUPP; |
| 4628 | |
Florian Fainelli | f2750df | 2018-09-28 16:18:54 -0700 | [diff] [blame] | 4629 | if (wol->wolopts & ~WAKE_ANY) |
| 4630 | return -EINVAL; |
| 4631 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4632 | ret = usb_autopm_get_interface(tp->intf); |
| 4633 | if (ret < 0) |
| 4634 | goto out_set_wol; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4635 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4636 | mutex_lock(&tp->control); |
| 4637 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4638 | __rtl_set_wol(tp, wol->wolopts); |
| 4639 | tp->saved_wolopts = wol->wolopts & WAKE_ANY; |
| 4640 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4641 | mutex_unlock(&tp->control); |
| 4642 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4643 | usb_autopm_put_interface(tp->intf); |
| 4644 | |
| 4645 | out_set_wol: |
| 4646 | return ret; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4647 | } |
| 4648 | |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 4649 | static u32 rtl8152_get_msglevel(struct net_device *dev) |
| 4650 | { |
| 4651 | struct r8152 *tp = netdev_priv(dev); |
| 4652 | |
| 4653 | return tp->msg_enable; |
| 4654 | } |
| 4655 | |
| 4656 | static void rtl8152_set_msglevel(struct net_device *dev, u32 value) |
| 4657 | { |
| 4658 | struct r8152 *tp = netdev_priv(dev); |
| 4659 | |
| 4660 | tp->msg_enable = value; |
| 4661 | } |
| 4662 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4663 | static void rtl8152_get_drvinfo(struct net_device *netdev, |
| 4664 | struct ethtool_drvinfo *info) |
| 4665 | { |
| 4666 | struct r8152 *tp = netdev_priv(netdev); |
| 4667 | |
hayeswang | b0b46c7 | 2014-08-26 10:08:23 +0800 | [diff] [blame] | 4668 | strlcpy(info->driver, MODULENAME, sizeof(info->driver)); |
| 4669 | strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4670 | usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); |
| 4671 | } |
| 4672 | |
| 4673 | static |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 4674 | int rtl8152_get_link_ksettings(struct net_device *netdev, |
| 4675 | struct ethtool_link_ksettings *cmd) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4676 | { |
| 4677 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 4678 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4679 | |
| 4680 | if (!tp->mii.mdio_read) |
| 4681 | return -EOPNOTSUPP; |
| 4682 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 4683 | ret = usb_autopm_get_interface(tp->intf); |
| 4684 | if (ret < 0) |
| 4685 | goto out; |
| 4686 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4687 | mutex_lock(&tp->control); |
| 4688 | |
yuval.shaia@oracle.com | 82c01a8 | 2017-06-04 20:22:00 +0300 | [diff] [blame] | 4689 | mii_ethtool_get_link_ksettings(&tp->mii, cmd); |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 4690 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4691 | mutex_unlock(&tp->control); |
| 4692 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 4693 | usb_autopm_put_interface(tp->intf); |
| 4694 | |
| 4695 | out: |
| 4696 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 4699 | static int rtl8152_set_link_ksettings(struct net_device *dev, |
| 4700 | const struct ethtool_link_ksettings *cmd) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4701 | { |
| 4702 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4703 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4704 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4705 | ret = usb_autopm_get_interface(tp->intf); |
| 4706 | if (ret < 0) |
| 4707 | goto out; |
| 4708 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4709 | mutex_lock(&tp->control); |
| 4710 | |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 4711 | ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed, |
| 4712 | cmd->base.duplex); |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 4713 | if (!ret) { |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 4714 | tp->autoneg = cmd->base.autoneg; |
| 4715 | tp->speed = cmd->base.speed; |
| 4716 | tp->duplex = cmd->base.duplex; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 4717 | } |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4718 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4719 | mutex_unlock(&tp->control); |
| 4720 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4721 | usb_autopm_put_interface(tp->intf); |
| 4722 | |
| 4723 | out: |
| 4724 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4725 | } |
| 4726 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4727 | static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = { |
| 4728 | "tx_packets", |
| 4729 | "rx_packets", |
| 4730 | "tx_errors", |
| 4731 | "rx_errors", |
| 4732 | "rx_missed", |
| 4733 | "align_errors", |
| 4734 | "tx_single_collisions", |
| 4735 | "tx_multi_collisions", |
| 4736 | "rx_unicast", |
| 4737 | "rx_broadcast", |
| 4738 | "rx_multicast", |
| 4739 | "tx_aborted", |
| 4740 | "tx_underrun", |
| 4741 | }; |
| 4742 | |
| 4743 | static int rtl8152_get_sset_count(struct net_device *dev, int sset) |
| 4744 | { |
| 4745 | switch (sset) { |
| 4746 | case ETH_SS_STATS: |
| 4747 | return ARRAY_SIZE(rtl8152_gstrings); |
| 4748 | default: |
| 4749 | return -EOPNOTSUPP; |
| 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | static void rtl8152_get_ethtool_stats(struct net_device *dev, |
| 4754 | struct ethtool_stats *stats, u64 *data) |
| 4755 | { |
| 4756 | struct r8152 *tp = netdev_priv(dev); |
| 4757 | struct tally_counter tally; |
| 4758 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 4759 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 4760 | return; |
| 4761 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4762 | generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA); |
| 4763 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 4764 | usb_autopm_put_interface(tp->intf); |
| 4765 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4766 | data[0] = le64_to_cpu(tally.tx_packets); |
| 4767 | data[1] = le64_to_cpu(tally.rx_packets); |
| 4768 | data[2] = le64_to_cpu(tally.tx_errors); |
| 4769 | data[3] = le32_to_cpu(tally.rx_errors); |
| 4770 | data[4] = le16_to_cpu(tally.rx_missed); |
| 4771 | data[5] = le16_to_cpu(tally.align_errors); |
| 4772 | data[6] = le32_to_cpu(tally.tx_one_collision); |
| 4773 | data[7] = le32_to_cpu(tally.tx_multi_collision); |
| 4774 | data[8] = le64_to_cpu(tally.rx_unicast); |
| 4775 | data[9] = le64_to_cpu(tally.rx_broadcast); |
| 4776 | data[10] = le32_to_cpu(tally.rx_multicast); |
| 4777 | data[11] = le16_to_cpu(tally.tx_aborted); |
hayeswang | f37119c | 2014-10-28 14:05:51 +0800 | [diff] [blame] | 4778 | data[12] = le16_to_cpu(tally.tx_underrun); |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data) |
| 4782 | { |
| 4783 | switch (stringset) { |
| 4784 | case ETH_SS_STATS: |
| 4785 | memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings)); |
| 4786 | break; |
| 4787 | } |
| 4788 | } |
| 4789 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4790 | static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4791 | { |
| 4792 | u32 ocp_data, lp, adv, supported = 0; |
| 4793 | u16 val; |
| 4794 | |
| 4795 | val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); |
| 4796 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 4797 | |
| 4798 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV); |
| 4799 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 4800 | |
| 4801 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); |
| 4802 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 4803 | |
| 4804 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 4805 | ocp_data &= EEE_RX_EN | EEE_TX_EN; |
| 4806 | |
| 4807 | eee->eee_enabled = !!ocp_data; |
| 4808 | eee->eee_active = !!(supported & adv & lp); |
| 4809 | eee->supported = supported; |
| 4810 | eee->advertised = adv; |
| 4811 | eee->lp_advertised = lp; |
| 4812 | |
| 4813 | return 0; |
| 4814 | } |
| 4815 | |
| 4816 | static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4817 | { |
| 4818 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 4819 | |
| 4820 | r8152_eee_en(tp, eee->eee_enabled); |
| 4821 | |
| 4822 | if (!eee->eee_enabled) |
| 4823 | val = 0; |
| 4824 | |
| 4825 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val); |
| 4826 | |
| 4827 | return 0; |
| 4828 | } |
| 4829 | |
| 4830 | static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4831 | { |
| 4832 | u32 ocp_data, lp, adv, supported = 0; |
| 4833 | u16 val; |
| 4834 | |
| 4835 | val = ocp_reg_read(tp, OCP_EEE_ABLE); |
| 4836 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 4837 | |
| 4838 | val = ocp_reg_read(tp, OCP_EEE_ADV); |
| 4839 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 4840 | |
| 4841 | val = ocp_reg_read(tp, OCP_EEE_LPABLE); |
| 4842 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 4843 | |
| 4844 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 4845 | ocp_data &= EEE_RX_EN | EEE_TX_EN; |
| 4846 | |
| 4847 | eee->eee_enabled = !!ocp_data; |
| 4848 | eee->eee_active = !!(supported & adv & lp); |
| 4849 | eee->supported = supported; |
| 4850 | eee->advertised = adv; |
| 4851 | eee->lp_advertised = lp; |
| 4852 | |
| 4853 | return 0; |
| 4854 | } |
| 4855 | |
| 4856 | static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4857 | { |
| 4858 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 4859 | |
| 4860 | r8153_eee_en(tp, eee->eee_enabled); |
| 4861 | |
| 4862 | if (!eee->eee_enabled) |
| 4863 | val = 0; |
| 4864 | |
| 4865 | ocp_reg_write(tp, OCP_EEE_ADV, val); |
| 4866 | |
| 4867 | return 0; |
| 4868 | } |
| 4869 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4870 | static int r8153b_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4871 | { |
| 4872 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 4873 | |
| 4874 | r8153b_eee_en(tp, eee->eee_enabled); |
| 4875 | |
| 4876 | if (!eee->eee_enabled) |
| 4877 | val = 0; |
| 4878 | |
| 4879 | ocp_reg_write(tp, OCP_EEE_ADV, val); |
| 4880 | |
| 4881 | return 0; |
| 4882 | } |
| 4883 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4884 | static int |
| 4885 | rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata) |
| 4886 | { |
| 4887 | struct r8152 *tp = netdev_priv(net); |
| 4888 | int ret; |
| 4889 | |
| 4890 | ret = usb_autopm_get_interface(tp->intf); |
| 4891 | if (ret < 0) |
| 4892 | goto out; |
| 4893 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4894 | mutex_lock(&tp->control); |
| 4895 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4896 | ret = tp->rtl_ops.eee_get(tp, edata); |
| 4897 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4898 | mutex_unlock(&tp->control); |
| 4899 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4900 | usb_autopm_put_interface(tp->intf); |
| 4901 | |
| 4902 | out: |
| 4903 | return ret; |
| 4904 | } |
| 4905 | |
| 4906 | static int |
| 4907 | rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata) |
| 4908 | { |
| 4909 | struct r8152 *tp = netdev_priv(net); |
| 4910 | int ret; |
| 4911 | |
| 4912 | ret = usb_autopm_get_interface(tp->intf); |
| 4913 | if (ret < 0) |
| 4914 | goto out; |
| 4915 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4916 | mutex_lock(&tp->control); |
| 4917 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4918 | ret = tp->rtl_ops.eee_set(tp, edata); |
hayeswang | 9d31a7b | 2014-10-06 10:36:04 +0800 | [diff] [blame] | 4919 | if (!ret) |
| 4920 | ret = mii_nway_restart(&tp->mii); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4921 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4922 | mutex_unlock(&tp->control); |
| 4923 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4924 | usb_autopm_put_interface(tp->intf); |
| 4925 | |
| 4926 | out: |
| 4927 | return ret; |
| 4928 | } |
| 4929 | |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 4930 | static int rtl8152_nway_reset(struct net_device *dev) |
| 4931 | { |
| 4932 | struct r8152 *tp = netdev_priv(dev); |
| 4933 | int ret; |
| 4934 | |
| 4935 | ret = usb_autopm_get_interface(tp->intf); |
| 4936 | if (ret < 0) |
| 4937 | goto out; |
| 4938 | |
| 4939 | mutex_lock(&tp->control); |
| 4940 | |
| 4941 | ret = mii_nway_restart(&tp->mii); |
| 4942 | |
| 4943 | mutex_unlock(&tp->control); |
| 4944 | |
| 4945 | usb_autopm_put_interface(tp->intf); |
| 4946 | |
| 4947 | out: |
| 4948 | return ret; |
| 4949 | } |
| 4950 | |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 4951 | static int rtl8152_get_coalesce(struct net_device *netdev, |
| 4952 | struct ethtool_coalesce *coalesce) |
| 4953 | { |
| 4954 | struct r8152 *tp = netdev_priv(netdev); |
| 4955 | |
| 4956 | switch (tp->version) { |
| 4957 | case RTL_VER_01: |
| 4958 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 4959 | case RTL_VER_07: |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 4960 | return -EOPNOTSUPP; |
| 4961 | default: |
| 4962 | break; |
| 4963 | } |
| 4964 | |
| 4965 | coalesce->rx_coalesce_usecs = tp->coalesce; |
| 4966 | |
| 4967 | return 0; |
| 4968 | } |
| 4969 | |
| 4970 | static int rtl8152_set_coalesce(struct net_device *netdev, |
| 4971 | struct ethtool_coalesce *coalesce) |
| 4972 | { |
| 4973 | struct r8152 *tp = netdev_priv(netdev); |
| 4974 | int ret; |
| 4975 | |
| 4976 | switch (tp->version) { |
| 4977 | case RTL_VER_01: |
| 4978 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 4979 | case RTL_VER_07: |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 4980 | return -EOPNOTSUPP; |
| 4981 | default: |
| 4982 | break; |
| 4983 | } |
| 4984 | |
| 4985 | if (coalesce->rx_coalesce_usecs > COALESCE_SLOW) |
| 4986 | return -EINVAL; |
| 4987 | |
| 4988 | ret = usb_autopm_get_interface(tp->intf); |
| 4989 | if (ret < 0) |
| 4990 | return ret; |
| 4991 | |
| 4992 | mutex_lock(&tp->control); |
| 4993 | |
| 4994 | if (tp->coalesce != coalesce->rx_coalesce_usecs) { |
| 4995 | tp->coalesce = coalesce->rx_coalesce_usecs; |
| 4996 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 4997 | if (netif_running(netdev) && netif_carrier_ok(netdev)) { |
| 4998 | netif_stop_queue(netdev); |
| 4999 | napi_disable(&tp->napi); |
| 5000 | tp->rtl_ops.disable(tp); |
| 5001 | tp->rtl_ops.enable(tp); |
| 5002 | rtl_start_rx(tp); |
| 5003 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 5004 | _rtl8152_set_rx_mode(netdev); |
| 5005 | napi_enable(&tp->napi); |
| 5006 | netif_wake_queue(netdev); |
| 5007 | } |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 5008 | } |
| 5009 | |
| 5010 | mutex_unlock(&tp->control); |
| 5011 | |
| 5012 | usb_autopm_put_interface(tp->intf); |
| 5013 | |
| 5014 | return ret; |
| 5015 | } |
| 5016 | |
Julia Lawall | 407a471 | 2016-09-01 00:21:22 +0200 | [diff] [blame] | 5017 | static const struct ethtool_ops ops = { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5018 | .get_drvinfo = rtl8152_get_drvinfo, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5019 | .get_link = ethtool_op_get_link, |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 5020 | .nway_reset = rtl8152_nway_reset, |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 5021 | .get_msglevel = rtl8152_get_msglevel, |
| 5022 | .set_msglevel = rtl8152_set_msglevel, |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5023 | .get_wol = rtl8152_get_wol, |
| 5024 | .set_wol = rtl8152_set_wol, |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5025 | .get_strings = rtl8152_get_strings, |
| 5026 | .get_sset_count = rtl8152_get_sset_count, |
| 5027 | .get_ethtool_stats = rtl8152_get_ethtool_stats, |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 5028 | .get_coalesce = rtl8152_get_coalesce, |
| 5029 | .set_coalesce = rtl8152_set_coalesce, |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 5030 | .get_eee = rtl_ethtool_get_eee, |
| 5031 | .set_eee = rtl_ethtool_set_eee, |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5032 | .get_link_ksettings = rtl8152_get_link_ksettings, |
| 5033 | .set_link_ksettings = rtl8152_set_link_ksettings, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5034 | }; |
| 5035 | |
| 5036 | static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) |
| 5037 | { |
| 5038 | struct r8152 *tp = netdev_priv(netdev); |
| 5039 | struct mii_ioctl_data *data = if_mii(rq); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5040 | int res; |
| 5041 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5042 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5043 | return -ENODEV; |
| 5044 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5045 | res = usb_autopm_get_interface(tp->intf); |
| 5046 | if (res < 0) |
| 5047 | goto out; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5048 | |
| 5049 | switch (cmd) { |
| 5050 | case SIOCGMIIPHY: |
| 5051 | data->phy_id = R8152_PHY_ID; /* Internal PHY */ |
| 5052 | break; |
| 5053 | |
| 5054 | case SIOCGMIIREG: |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5055 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5056 | data->val_out = r8152_mdio_read(tp, data->reg_num); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5057 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5058 | break; |
| 5059 | |
| 5060 | case SIOCSMIIREG: |
| 5061 | if (!capable(CAP_NET_ADMIN)) { |
| 5062 | res = -EPERM; |
| 5063 | break; |
| 5064 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5065 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5066 | r8152_mdio_write(tp, data->reg_num, data->val_in); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5067 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5068 | break; |
| 5069 | |
| 5070 | default: |
| 5071 | res = -EOPNOTSUPP; |
| 5072 | } |
| 5073 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5074 | usb_autopm_put_interface(tp->intf); |
| 5075 | |
| 5076 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5077 | return res; |
| 5078 | } |
| 5079 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5080 | static int rtl8152_change_mtu(struct net_device *dev, int new_mtu) |
| 5081 | { |
| 5082 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 5083 | int ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5084 | |
| 5085 | switch (tp->version) { |
| 5086 | case RTL_VER_01: |
| 5087 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5088 | case RTL_VER_07: |
Jarod Wilson | a52ad51 | 2016-10-07 22:04:34 -0400 | [diff] [blame] | 5089 | dev->mtu = new_mtu; |
| 5090 | return 0; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5091 | default: |
| 5092 | break; |
| 5093 | } |
| 5094 | |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 5095 | ret = usb_autopm_get_interface(tp->intf); |
| 5096 | if (ret < 0) |
| 5097 | return ret; |
| 5098 | |
| 5099 | mutex_lock(&tp->control); |
| 5100 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5101 | dev->mtu = new_mtu; |
| 5102 | |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 5103 | if (netif_running(dev)) { |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 5104 | u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 5105 | |
| 5106 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms); |
| 5107 | |
| 5108 | if (netif_carrier_ok(dev)) |
| 5109 | r8153_set_rx_early_size(tp); |
| 5110 | } |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 5111 | |
| 5112 | mutex_unlock(&tp->control); |
| 5113 | |
| 5114 | usb_autopm_put_interface(tp->intf); |
| 5115 | |
| 5116 | return ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5117 | } |
| 5118 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5119 | static const struct net_device_ops rtl8152_netdev_ops = { |
| 5120 | .ndo_open = rtl8152_open, |
| 5121 | .ndo_stop = rtl8152_close, |
| 5122 | .ndo_do_ioctl = rtl8152_ioctl, |
| 5123 | .ndo_start_xmit = rtl8152_start_xmit, |
| 5124 | .ndo_tx_timeout = rtl8152_tx_timeout, |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 5125 | .ndo_set_features = rtl8152_set_features, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5126 | .ndo_set_rx_mode = rtl8152_set_rx_mode, |
| 5127 | .ndo_set_mac_address = rtl8152_set_mac_address, |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5128 | .ndo_change_mtu = rtl8152_change_mtu, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5129 | .ndo_validate_addr = eth_validate_addr, |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 5130 | .ndo_features_check = rtl8152_features_check, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5131 | }; |
| 5132 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 5133 | static void rtl8152_unload(struct r8152 *tp) |
| 5134 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5135 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5136 | return; |
| 5137 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 5138 | if (tp->version != RTL_VER_01) |
| 5139 | r8152_power_cut_en(tp, true); |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 5140 | } |
| 5141 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5142 | static void rtl8153_unload(struct r8152 *tp) |
| 5143 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5144 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5145 | return; |
| 5146 | |
hayeswang | 49be172 | 2014-10-01 13:25:11 +0800 | [diff] [blame] | 5147 | r8153_power_cut_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5148 | } |
| 5149 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5150 | static void rtl8153b_unload(struct r8152 *tp) |
| 5151 | { |
| 5152 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5153 | return; |
| 5154 | |
| 5155 | r8153b_power_cut_en(tp, false); |
| 5156 | } |
| 5157 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5158 | static int rtl_ops_init(struct r8152 *tp) |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5159 | { |
| 5160 | struct rtl_ops *ops = &tp->rtl_ops; |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5161 | int ret = 0; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5162 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5163 | switch (tp->version) { |
| 5164 | case RTL_VER_01: |
| 5165 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5166 | case RTL_VER_07: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5167 | ops->init = r8152b_init; |
| 5168 | ops->enable = rtl8152_enable; |
| 5169 | ops->disable = rtl8152_disable; |
| 5170 | ops->up = rtl8152_up; |
| 5171 | ops->down = rtl8152_down; |
| 5172 | ops->unload = rtl8152_unload; |
| 5173 | ops->eee_get = r8152_get_eee; |
| 5174 | ops->eee_set = r8152_set_eee; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5175 | ops->in_nway = rtl8152_in_nway; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5176 | ops->hw_phy_cfg = r8152b_hw_phy_cfg; |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 5177 | ops->autosuspend_en = rtl_runtime_suspend_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 5178 | tp->rx_buf_sz = 16 * 1024; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5179 | break; |
| 5180 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5181 | case RTL_VER_03: |
| 5182 | case RTL_VER_04: |
| 5183 | case RTL_VER_05: |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 5184 | case RTL_VER_06: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5185 | ops->init = r8153_init; |
| 5186 | ops->enable = rtl8153_enable; |
| 5187 | ops->disable = rtl8153_disable; |
| 5188 | ops->up = rtl8153_up; |
| 5189 | ops->down = rtl8153_down; |
| 5190 | ops->unload = rtl8153_unload; |
| 5191 | ops->eee_get = r8153_get_eee; |
| 5192 | ops->eee_set = r8153_set_eee; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5193 | ops->in_nway = rtl8153_in_nway; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5194 | ops->hw_phy_cfg = r8153_hw_phy_cfg; |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 5195 | ops->autosuspend_en = rtl8153_runtime_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 5196 | tp->rx_buf_sz = 32 * 1024; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5197 | break; |
| 5198 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5199 | case RTL_VER_08: |
| 5200 | case RTL_VER_09: |
| 5201 | ops->init = r8153b_init; |
| 5202 | ops->enable = rtl8153_enable; |
| 5203 | ops->disable = rtl8153b_disable; |
| 5204 | ops->up = rtl8153b_up; |
| 5205 | ops->down = rtl8153b_down; |
| 5206 | ops->unload = rtl8153b_unload; |
| 5207 | ops->eee_get = r8153_get_eee; |
| 5208 | ops->eee_set = r8153b_set_eee; |
| 5209 | ops->in_nway = rtl8153_in_nway; |
| 5210 | ops->hw_phy_cfg = r8153b_hw_phy_cfg; |
| 5211 | ops->autosuspend_en = rtl8153b_runtime_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 5212 | tp->rx_buf_sz = 32 * 1024; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5213 | break; |
| 5214 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5215 | default: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5216 | ret = -ENODEV; |
| 5217 | netif_err(tp, probe, tp->netdev, "Unknown Device\n"); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5218 | break; |
| 5219 | } |
| 5220 | |
| 5221 | return ret; |
| 5222 | } |
| 5223 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5224 | static u8 rtl_get_version(struct usb_interface *intf) |
| 5225 | { |
| 5226 | struct usb_device *udev = interface_to_usbdev(intf); |
| 5227 | u32 ocp_data = 0; |
| 5228 | __le32 *tmp; |
| 5229 | u8 version; |
| 5230 | int ret; |
| 5231 | |
| 5232 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); |
| 5233 | if (!tmp) |
| 5234 | return 0; |
| 5235 | |
| 5236 | ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 5237 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
| 5238 | PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500); |
| 5239 | if (ret > 0) |
| 5240 | ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK; |
| 5241 | |
| 5242 | kfree(tmp); |
| 5243 | |
| 5244 | switch (ocp_data) { |
| 5245 | case 0x4c00: |
| 5246 | version = RTL_VER_01; |
| 5247 | break; |
| 5248 | case 0x4c10: |
| 5249 | version = RTL_VER_02; |
| 5250 | break; |
| 5251 | case 0x5c00: |
| 5252 | version = RTL_VER_03; |
| 5253 | break; |
| 5254 | case 0x5c10: |
| 5255 | version = RTL_VER_04; |
| 5256 | break; |
| 5257 | case 0x5c20: |
| 5258 | version = RTL_VER_05; |
| 5259 | break; |
| 5260 | case 0x5c30: |
| 5261 | version = RTL_VER_06; |
| 5262 | break; |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5263 | case 0x4800: |
| 5264 | version = RTL_VER_07; |
| 5265 | break; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5266 | case 0x6000: |
| 5267 | version = RTL_VER_08; |
| 5268 | break; |
| 5269 | case 0x6010: |
| 5270 | version = RTL_VER_09; |
| 5271 | break; |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5272 | default: |
| 5273 | version = RTL_VER_UNKNOWN; |
| 5274 | dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data); |
| 5275 | break; |
| 5276 | } |
| 5277 | |
Oliver Neukum | eb3c28c | 2017-06-12 13:56:51 +0200 | [diff] [blame] | 5278 | dev_dbg(&intf->dev, "Detected version 0x%04x\n", version); |
| 5279 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5280 | return version; |
| 5281 | } |
| 5282 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5283 | static int rtl8152_probe(struct usb_interface *intf, |
| 5284 | const struct usb_device_id *id) |
| 5285 | { |
| 5286 | struct usb_device *udev = interface_to_usbdev(intf); |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5287 | u8 version = rtl_get_version(intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5288 | struct r8152 *tp; |
| 5289 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5290 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5291 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5292 | if (version == RTL_VER_UNKNOWN) |
| 5293 | return -ENODEV; |
| 5294 | |
hayeswang | 10c3271 | 2014-03-04 20:47:48 +0800 | [diff] [blame] | 5295 | if (udev->actconfig->desc.bConfigurationValue != 1) { |
| 5296 | usb_driver_set_configuration(udev, 1); |
| 5297 | return -ENODEV; |
| 5298 | } |
| 5299 | |
| 5300 | usb_reset_device(udev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5301 | netdev = alloc_etherdev(sizeof(struct r8152)); |
| 5302 | if (!netdev) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 5303 | dev_err(&intf->dev, "Out of memory\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5304 | return -ENOMEM; |
| 5305 | } |
| 5306 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5307 | SET_NETDEV_DEV(netdev, &intf->dev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5308 | tp = netdev_priv(netdev); |
| 5309 | tp->msg_enable = 0x7FFF; |
| 5310 | |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 5311 | tp->udev = udev; |
| 5312 | tp->netdev = netdev; |
| 5313 | tp->intf = intf; |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5314 | tp->version = version; |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 5315 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5316 | switch (version) { |
| 5317 | case RTL_VER_01: |
| 5318 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5319 | case RTL_VER_07: |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5320 | tp->mii.supports_gmii = 0; |
| 5321 | break; |
| 5322 | default: |
| 5323 | tp->mii.supports_gmii = 1; |
| 5324 | break; |
| 5325 | } |
| 5326 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5327 | ret = rtl_ops_init(tp); |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 5328 | if (ret) |
| 5329 | goto out; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5330 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5331 | mutex_init(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5332 | INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5333 | INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5334 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5335 | netdev->netdev_ops = &rtl8152_netdev_ops; |
| 5336 | netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 5337 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 5338 | netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 5339 | NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 5340 | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX | |
| 5341 | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 5342 | netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 5343 | NETIF_F_TSO | NETIF_F_FRAGLIST | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 5344 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | |
hayeswang | ccc39fa | 2015-02-06 11:30:49 +0800 | [diff] [blame] | 5345 | NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 5346 | netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | |
| 5347 | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | |
| 5348 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6; |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 5349 | |
hayeswang | 19c0f40 | 2017-01-11 16:25:34 +0800 | [diff] [blame] | 5350 | if (tp->version == RTL_VER_01) { |
| 5351 | netdev->features &= ~NETIF_F_RXCSUM; |
| 5352 | netdev->hw_features &= ~NETIF_F_RXCSUM; |
| 5353 | } |
| 5354 | |
Kai-Heng Feng | 176eb61 | 2018-08-20 12:43:51 +0800 | [diff] [blame] | 5355 | if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial && |
| 5356 | (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) { |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 5357 | dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation"); |
| 5358 | set_bit(DELL_TB_RX_AGG_BUG, &tp->flags); |
| 5359 | } |
| 5360 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 5361 | netdev->ethtool_ops = &ops; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 5362 | netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5363 | |
Jarod Wilson | f77f0ae | 2016-10-20 13:55:17 -0400 | [diff] [blame] | 5364 | /* MTU range: 68 - 1500 or 9194 */ |
| 5365 | netdev->min_mtu = ETH_MIN_MTU; |
| 5366 | switch (tp->version) { |
| 5367 | case RTL_VER_01: |
| 5368 | case RTL_VER_02: |
| 5369 | netdev->max_mtu = ETH_DATA_LEN; |
| 5370 | break; |
| 5371 | default: |
| 5372 | netdev->max_mtu = RTL8153_MAX_MTU; |
| 5373 | break; |
| 5374 | } |
| 5375 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5376 | tp->mii.dev = netdev; |
| 5377 | tp->mii.mdio_read = read_mii_word; |
| 5378 | tp->mii.mdio_write = write_mii_word; |
| 5379 | tp->mii.phy_id_mask = 0x3f; |
| 5380 | tp->mii.reg_num_mask = 0x1f; |
| 5381 | tp->mii.phy_id = R8152_PHY_ID; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5382 | |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 5383 | tp->autoneg = AUTONEG_ENABLE; |
| 5384 | tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100; |
| 5385 | tp->duplex = DUPLEX_FULL; |
| 5386 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5387 | intf->needs_remote_wakeup = 1; |
| 5388 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5389 | tp->rtl_ops.init(tp); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5390 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5391 | set_ethernet_addr(tp); |
| 5392 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5393 | usb_set_intfdata(intf, tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 5394 | netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5395 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5396 | ret = register_netdev(netdev); |
| 5397 | if (ret != 0) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 5398 | netif_err(tp, probe, netdev, "couldn't register the device\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5399 | goto out1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 5402 | if (!rtl_can_wakeup(tp)) |
| 5403 | __rtl_set_wol(tp, 0); |
| 5404 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5405 | tp->saved_wolopts = __rtl_get_wol(tp); |
| 5406 | if (tp->saved_wolopts) |
| 5407 | device_set_wakeup_enable(&udev->dev, true); |
| 5408 | else |
| 5409 | device_set_wakeup_enable(&udev->dev, false); |
| 5410 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 5411 | netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5412 | |
| 5413 | return 0; |
| 5414 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5415 | out1: |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 5416 | netif_napi_del(&tp->napi); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5417 | usb_set_intfdata(intf, NULL); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5418 | out: |
| 5419 | free_netdev(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5420 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5423 | static void rtl8152_disconnect(struct usb_interface *intf) |
| 5424 | { |
| 5425 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5426 | |
| 5427 | usb_set_intfdata(intf, NULL); |
| 5428 | if (tp) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 5429 | rtl_set_unplug(tp); |
hayeswang | f561de3 | 2014-09-30 16:48:01 +0800 | [diff] [blame] | 5430 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 5431 | netif_napi_del(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5432 | unregister_netdev(tp->netdev); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5433 | cancel_delayed_work_sync(&tp->hw_phy_work); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5434 | tp->rtl_ops.unload(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5435 | free_netdev(tp->netdev); |
| 5436 | } |
| 5437 | } |
| 5438 | |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 5439 | #define REALTEK_USB_DEVICE(vend, prod) \ |
| 5440 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ |
| 5441 | USB_DEVICE_ID_MATCH_INT_CLASS, \ |
| 5442 | .idVendor = (vend), \ |
| 5443 | .idProduct = (prod), \ |
| 5444 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC \ |
| 5445 | }, \ |
| 5446 | { \ |
| 5447 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \ |
| 5448 | USB_DEVICE_ID_MATCH_DEVICE, \ |
| 5449 | .idVendor = (vend), \ |
| 5450 | .idProduct = (prod), \ |
| 5451 | .bInterfaceClass = USB_CLASS_COMM, \ |
| 5452 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \ |
| 5453 | .bInterfaceProtocol = USB_CDC_PROTO_NONE |
| 5454 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5455 | /* table of devices that work with this driver */ |
Arvind Yadav | 9b4355f | 2017-08-08 21:28:05 +0530 | [diff] [blame] | 5456 | static const struct usb_device_id rtl8152_table[] = { |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5457 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)}, |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 5458 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)}, |
| 5459 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)}, |
René Rebe | d5b07cc | 2017-03-28 07:56:51 +0200 | [diff] [blame] | 5460 | {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)}, |
| 5461 | {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)}, |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 5462 | {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)}, |
Vasily Titskiy | 1006da1 | 2015-05-06 10:31:21 -0400 | [diff] [blame] | 5463 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)}, |
hayeswang | d248caf | 2016-10-18 11:41:48 +0800 | [diff] [blame] | 5464 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)}, |
| 5465 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)}, |
| 5466 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)}, |
| 5467 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)}, |
| 5468 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)}, |
Grant Grundler | 9084104 | 2017-09-28 11:35:00 -0700 | [diff] [blame] | 5469 | {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)}, |
Zheng Liu | d065c3c1 | 2015-07-07 13:54:12 -0700 | [diff] [blame] | 5470 | {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)}, |
Ran Wang | 9d11b06 | 2017-10-23 18:10:23 +0800 | [diff] [blame] | 5471 | {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)}, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5472 | {} |
| 5473 | }; |
| 5474 | |
| 5475 | MODULE_DEVICE_TABLE(usb, rtl8152_table); |
| 5476 | |
| 5477 | static struct usb_driver rtl8152_driver = { |
| 5478 | .name = MODULENAME, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5479 | .id_table = rtl8152_table, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5480 | .probe = rtl8152_probe, |
| 5481 | .disconnect = rtl8152_disconnect, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5482 | .suspend = rtl8152_suspend, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5483 | .resume = rtl8152_resume, |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 5484 | .reset_resume = rtl8152_reset_resume, |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5485 | .pre_reset = rtl8152_pre_reset, |
| 5486 | .post_reset = rtl8152_post_reset, |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5487 | .supports_autosuspend = 1, |
hayeswang | a634782 | 2014-02-18 21:49:10 +0800 | [diff] [blame] | 5488 | .disable_hub_initiated_lpm = 1, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5489 | }; |
| 5490 | |
Sachin Kamat | b4236daa | 2013-05-16 17:48:08 +0000 | [diff] [blame] | 5491 | module_usb_driver(rtl8152_driver); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5492 | |
| 5493 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 5494 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 5495 | MODULE_LICENSE("GPL"); |
Grant Grundler | c961e87 | 2016-07-14 11:27:16 -0700 | [diff] [blame] | 5496 | MODULE_VERSION(DRIVER_VERSION); |