hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1 | /* |
hayeswang | c7de7de | 2014-01-15 10:42:16 +0800 | [diff] [blame] | 2 | * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved. |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | */ |
| 9 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 10 | #include <linux/signal.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/module.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 13 | #include <linux/netdevice.h> |
| 14 | #include <linux/etherdevice.h> |
| 15 | #include <linux/mii.h> |
| 16 | #include <linux/ethtool.h> |
| 17 | #include <linux/usb.h> |
| 18 | #include <linux/crc32.h> |
| 19 | #include <linux/if_vlan.h> |
| 20 | #include <linux/uaccess.h> |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 21 | #include <linux/list.h> |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 22 | #include <linux/ip.h> |
| 23 | #include <linux/ipv6.h> |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 24 | #include <net/ip6_checksum.h> |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 25 | #include <uapi/linux/mdio.h> |
| 26 | #include <linux/mdio.h> |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 27 | #include <linux/usb/cdc.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 28 | |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame^] | 29 | /* Information for net-next */ |
| 30 | #define NETNEXT_VERSION "08" |
| 31 | |
| 32 | /* Information for net */ |
| 33 | #define NET_VERSION "1" |
| 34 | |
| 35 | #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 36 | #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" |
hayeswang | 44d942a | 2014-01-15 10:42:14 +0800 | [diff] [blame] | 37 | #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters" |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 38 | #define MODULENAME "r8152" |
| 39 | |
| 40 | #define R8152_PHY_ID 32 |
| 41 | |
| 42 | #define PLA_IDR 0xc000 |
| 43 | #define PLA_RCR 0xc010 |
| 44 | #define PLA_RMS 0xc016 |
| 45 | #define PLA_RXFIFO_CTRL0 0xc0a0 |
| 46 | #define PLA_RXFIFO_CTRL1 0xc0a4 |
| 47 | #define PLA_RXFIFO_CTRL2 0xc0a8 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 48 | #define PLA_DMY_REG0 0xc0b0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 49 | #define PLA_FMC 0xc0b4 |
| 50 | #define PLA_CFG_WOL 0xc0b6 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 51 | #define PLA_TEREDO_CFG 0xc0bc |
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 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 54 | #define PAL_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 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 57 | #define PLA_LEDSEL 0xdd90 |
| 58 | #define PLA_LED_FEATURE 0xdd92 |
| 59 | #define PLA_PHYAR 0xde00 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 60 | #define PLA_BOOT_CTRL 0xe004 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 61 | #define PLA_GPHY_INTR_IMR 0xe022 |
| 62 | #define PLA_EEE_CR 0xe040 |
| 63 | #define PLA_EEEP_CR 0xe080 |
| 64 | #define PLA_MAC_PWR_CTRL 0xe0c0 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 65 | #define PLA_MAC_PWR_CTRL2 0xe0ca |
| 66 | #define PLA_MAC_PWR_CTRL3 0xe0cc |
| 67 | #define PLA_MAC_PWR_CTRL4 0xe0ce |
| 68 | #define PLA_WDT6_CTRL 0xe428 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 69 | #define PLA_TCR0 0xe610 |
| 70 | #define PLA_TCR1 0xe612 |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 71 | #define PLA_MTPS 0xe615 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 72 | #define PLA_TXFIFO_CTRL 0xe618 |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 73 | #define PLA_RSTTALLY 0xe800 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 74 | #define PLA_CR 0xe813 |
| 75 | #define PLA_CRWECR 0xe81c |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 76 | #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */ |
| 77 | #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 78 | #define PLA_CONFIG5 0xe822 |
| 79 | #define PLA_PHY_PWR 0xe84c |
| 80 | #define PLA_OOB_CTRL 0xe84f |
| 81 | #define PLA_CPCR 0xe854 |
| 82 | #define PLA_MISC_0 0xe858 |
| 83 | #define PLA_MISC_1 0xe85a |
| 84 | #define PLA_OCP_GPHY_BASE 0xe86c |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 85 | #define PLA_TALLYCNT 0xe890 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 86 | #define PLA_SFF_STS_7 0xe8de |
| 87 | #define PLA_PHYSTATUS 0xe908 |
| 88 | #define PLA_BP_BA 0xfc26 |
| 89 | #define PLA_BP_0 0xfc28 |
| 90 | #define PLA_BP_1 0xfc2a |
| 91 | #define PLA_BP_2 0xfc2c |
| 92 | #define PLA_BP_3 0xfc2e |
| 93 | #define PLA_BP_4 0xfc30 |
| 94 | #define PLA_BP_5 0xfc32 |
| 95 | #define PLA_BP_6 0xfc34 |
| 96 | #define PLA_BP_7 0xfc36 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 97 | #define PLA_BP_EN 0xfc38 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 98 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 99 | #define USB_USB2PHY 0xb41e |
| 100 | #define USB_SSPHYLINK2 0xb428 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 101 | #define USB_U2P3_CTRL 0xb460 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 102 | #define USB_CSR_DUMMY1 0xb464 |
| 103 | #define USB_CSR_DUMMY2 0xb466 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 104 | #define USB_DEV_STAT 0xb808 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 105 | #define USB_CONNECT_TIMER 0xcbf8 |
| 106 | #define USB_BURST_SIZE 0xcfc0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 107 | #define USB_USB_CTRL 0xd406 |
| 108 | #define USB_PHY_CTRL 0xd408 |
| 109 | #define USB_TX_AGG 0xd40a |
| 110 | #define USB_RX_BUF_TH 0xd40c |
| 111 | #define USB_USB_TIMER 0xd428 |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 112 | #define USB_RX_EARLY_TIMEOUT 0xd42c |
| 113 | #define USB_RX_EARLY_SIZE 0xd42e |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 114 | #define USB_PM_CTRL_STATUS 0xd432 |
| 115 | #define USB_TX_DMA 0xd434 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 116 | #define USB_TOLERANCE 0xd490 |
| 117 | #define USB_LPM_CTRL 0xd41a |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 118 | #define USB_UPS_CTRL 0xd800 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 119 | #define USB_MISC_0 0xd81a |
| 120 | #define USB_POWER_CUT 0xd80a |
| 121 | #define USB_AFE_CTRL2 0xd824 |
| 122 | #define USB_WDT11_CTRL 0xe43c |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 123 | #define USB_BP_BA 0xfc26 |
| 124 | #define USB_BP_0 0xfc28 |
| 125 | #define USB_BP_1 0xfc2a |
| 126 | #define USB_BP_2 0xfc2c |
| 127 | #define USB_BP_3 0xfc2e |
| 128 | #define USB_BP_4 0xfc30 |
| 129 | #define USB_BP_5 0xfc32 |
| 130 | #define USB_BP_6 0xfc34 |
| 131 | #define USB_BP_7 0xfc36 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 132 | #define USB_BP_EN 0xfc38 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 133 | |
| 134 | /* OCP Registers */ |
| 135 | #define OCP_ALDPS_CONFIG 0x2010 |
| 136 | #define OCP_EEE_CONFIG1 0x2080 |
| 137 | #define OCP_EEE_CONFIG2 0x2092 |
| 138 | #define OCP_EEE_CONFIG3 0x2094 |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 139 | #define OCP_BASE_MII 0xa400 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 140 | #define OCP_EEE_AR 0xa41a |
| 141 | #define OCP_EEE_DATA 0xa41c |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 142 | #define OCP_PHY_STATUS 0xa420 |
| 143 | #define OCP_POWER_CFG 0xa430 |
| 144 | #define OCP_EEE_CFG 0xa432 |
| 145 | #define OCP_SRAM_ADDR 0xa436 |
| 146 | #define OCP_SRAM_DATA 0xa438 |
| 147 | #define OCP_DOWN_SPEED 0xa442 |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 148 | #define OCP_EEE_ABLE 0xa5c4 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 149 | #define OCP_EEE_ADV 0xa5d0 |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 150 | #define OCP_EEE_LPABLE 0xa5d2 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 151 | #define OCP_ADC_CFG 0xbc06 |
| 152 | |
| 153 | /* SRAM Register */ |
| 154 | #define SRAM_LPF_CFG 0x8012 |
| 155 | #define SRAM_10M_AMP1 0x8080 |
| 156 | #define SRAM_10M_AMP2 0x8082 |
| 157 | #define SRAM_IMPEDANCE 0x8084 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 158 | |
| 159 | /* PLA_RCR */ |
| 160 | #define RCR_AAP 0x00000001 |
| 161 | #define RCR_APM 0x00000002 |
| 162 | #define RCR_AM 0x00000004 |
| 163 | #define RCR_AB 0x00000008 |
| 164 | #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB) |
| 165 | |
| 166 | /* PLA_RXFIFO_CTRL0 */ |
| 167 | #define RXFIFO_THR1_NORMAL 0x00080002 |
| 168 | #define RXFIFO_THR1_OOB 0x01800003 |
| 169 | |
| 170 | /* PLA_RXFIFO_CTRL1 */ |
| 171 | #define RXFIFO_THR2_FULL 0x00000060 |
| 172 | #define RXFIFO_THR2_HIGH 0x00000038 |
| 173 | #define RXFIFO_THR2_OOB 0x0000004a |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 174 | #define RXFIFO_THR2_NORMAL 0x00a0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 175 | |
| 176 | /* PLA_RXFIFO_CTRL2 */ |
| 177 | #define RXFIFO_THR3_FULL 0x00000078 |
| 178 | #define RXFIFO_THR3_HIGH 0x00000048 |
| 179 | #define RXFIFO_THR3_OOB 0x0000005a |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 180 | #define RXFIFO_THR3_NORMAL 0x0110 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 181 | |
| 182 | /* PLA_TXFIFO_CTRL */ |
| 183 | #define TXFIFO_THR_NORMAL 0x00400008 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 184 | #define TXFIFO_THR_NORMAL2 0x01000008 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 185 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 186 | /* PLA_DMY_REG0 */ |
| 187 | #define ECM_ALDPS 0x0002 |
| 188 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 189 | /* PLA_FMC */ |
| 190 | #define FMC_FCR_MCU_EN 0x0001 |
| 191 | |
| 192 | /* PLA_EEEP_CR */ |
| 193 | #define EEEP_CR_EEEP_TX 0x0002 |
| 194 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 195 | /* PLA_WDT6_CTRL */ |
| 196 | #define WDT6_SET_MODE 0x0010 |
| 197 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 198 | /* PLA_TCR0 */ |
| 199 | #define TCR0_TX_EMPTY 0x0800 |
| 200 | #define TCR0_AUTO_FIFO 0x0080 |
| 201 | |
| 202 | /* PLA_TCR1 */ |
| 203 | #define VERSION_MASK 0x7cf0 |
| 204 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 205 | /* PLA_MTPS */ |
| 206 | #define MTPS_JUMBO (12 * 1024 / 64) |
| 207 | #define MTPS_DEFAULT (6 * 1024 / 64) |
| 208 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 209 | /* PLA_RSTTALLY */ |
| 210 | #define TALLY_RESET 0x0001 |
| 211 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 212 | /* PLA_CR */ |
| 213 | #define CR_RST 0x10 |
| 214 | #define CR_RE 0x08 |
| 215 | #define CR_TE 0x04 |
| 216 | |
| 217 | /* PLA_CRWECR */ |
| 218 | #define CRWECR_NORAML 0x00 |
| 219 | #define CRWECR_CONFIG 0xc0 |
| 220 | |
| 221 | /* PLA_OOB_CTRL */ |
| 222 | #define NOW_IS_OOB 0x80 |
| 223 | #define TXFIFO_EMPTY 0x20 |
| 224 | #define RXFIFO_EMPTY 0x10 |
| 225 | #define LINK_LIST_READY 0x02 |
| 226 | #define DIS_MCU_CLROOB 0x01 |
| 227 | #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY) |
| 228 | |
| 229 | /* PLA_MISC_1 */ |
| 230 | #define RXDY_GATED_EN 0x0008 |
| 231 | |
| 232 | /* PLA_SFF_STS_7 */ |
| 233 | #define RE_INIT_LL 0x8000 |
| 234 | #define MCU_BORW_EN 0x4000 |
| 235 | |
| 236 | /* PLA_CPCR */ |
| 237 | #define CPCR_RX_VLAN 0x0040 |
| 238 | |
| 239 | /* PLA_CFG_WOL */ |
| 240 | #define MAGIC_EN 0x0001 |
| 241 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 242 | /* PLA_TEREDO_CFG */ |
| 243 | #define TEREDO_SEL 0x8000 |
| 244 | #define TEREDO_WAKE_MASK 0x7f00 |
| 245 | #define TEREDO_RS_EVENT_MASK 0x00fe |
| 246 | #define OOB_TEREDO_EN 0x0001 |
| 247 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 248 | /* PAL_BDC_CR */ |
| 249 | #define ALDPS_PROXY_MODE 0x0001 |
| 250 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 251 | /* PLA_CONFIG34 */ |
| 252 | #define LINK_ON_WAKE_EN 0x0010 |
| 253 | #define LINK_OFF_WAKE_EN 0x0008 |
| 254 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 255 | /* PLA_CONFIG5 */ |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 256 | #define BWF_EN 0x0040 |
| 257 | #define MWF_EN 0x0020 |
| 258 | #define UWF_EN 0x0010 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 259 | #define LAN_WAKE_EN 0x0002 |
| 260 | |
| 261 | /* PLA_LED_FEATURE */ |
| 262 | #define LED_MODE_MASK 0x0700 |
| 263 | |
| 264 | /* PLA_PHY_PWR */ |
| 265 | #define TX_10M_IDLE_EN 0x0080 |
| 266 | #define PFM_PWM_SWITCH 0x0040 |
| 267 | |
| 268 | /* PLA_MAC_PWR_CTRL */ |
| 269 | #define D3_CLK_GATED_EN 0x00004000 |
| 270 | #define MCU_CLK_RATIO 0x07010f07 |
| 271 | #define MCU_CLK_RATIO_MASK 0x0f0f0f0f |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 272 | #define ALDPS_SPDWN_RATIO 0x0f87 |
| 273 | |
| 274 | /* PLA_MAC_PWR_CTRL2 */ |
| 275 | #define EEE_SPDWN_RATIO 0x8007 |
| 276 | |
| 277 | /* PLA_MAC_PWR_CTRL3 */ |
| 278 | #define PKT_AVAIL_SPDWN_EN 0x0100 |
| 279 | #define SUSPEND_SPDWN_EN 0x0004 |
| 280 | #define U1U2_SPDWN_EN 0x0002 |
| 281 | #define L1_SPDWN_EN 0x0001 |
| 282 | |
| 283 | /* PLA_MAC_PWR_CTRL4 */ |
| 284 | #define PWRSAVE_SPDWN_EN 0x1000 |
| 285 | #define RXDV_SPDWN_EN 0x0800 |
| 286 | #define TX10MIDLE_EN 0x0100 |
| 287 | #define TP100_SPDWN_EN 0x0020 |
| 288 | #define TP500_SPDWN_EN 0x0010 |
| 289 | #define TP1000_SPDWN_EN 0x0008 |
| 290 | #define EEE_SPDWN_EN 0x0001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 291 | |
| 292 | /* PLA_GPHY_INTR_IMR */ |
| 293 | #define GPHY_STS_MSK 0x0001 |
| 294 | #define SPEED_DOWN_MSK 0x0002 |
| 295 | #define SPDWN_RXDV_MSK 0x0004 |
| 296 | #define SPDWN_LINKCHG_MSK 0x0008 |
| 297 | |
| 298 | /* PLA_PHYAR */ |
| 299 | #define PHYAR_FLAG 0x80000000 |
| 300 | |
| 301 | /* PLA_EEE_CR */ |
| 302 | #define EEE_RX_EN 0x0001 |
| 303 | #define EEE_TX_EN 0x0002 |
| 304 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 305 | /* PLA_BOOT_CTRL */ |
| 306 | #define AUTOLOAD_DONE 0x0002 |
| 307 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 308 | /* USB_USB2PHY */ |
| 309 | #define USB2PHY_SUSPEND 0x0001 |
| 310 | #define USB2PHY_L1 0x0002 |
| 311 | |
| 312 | /* USB_SSPHYLINK2 */ |
| 313 | #define pwd_dn_scale_mask 0x3ffe |
| 314 | #define pwd_dn_scale(x) ((x) << 1) |
| 315 | |
| 316 | /* USB_CSR_DUMMY1 */ |
| 317 | #define DYNAMIC_BURST 0x0001 |
| 318 | |
| 319 | /* USB_CSR_DUMMY2 */ |
| 320 | #define EP4_FULL_FC 0x0001 |
| 321 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 322 | /* USB_DEV_STAT */ |
| 323 | #define STAT_SPEED_MASK 0x0006 |
| 324 | #define STAT_SPEED_HIGH 0x0000 |
hayeswang | a3cc465 | 2014-07-24 16:37:43 +0800 | [diff] [blame] | 325 | #define STAT_SPEED_FULL 0x0002 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 326 | |
| 327 | /* USB_TX_AGG */ |
| 328 | #define TX_AGG_MAX_THRESHOLD 0x03 |
| 329 | |
| 330 | /* USB_RX_BUF_TH */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 331 | #define RX_THR_SUPPER 0x0c350180 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 332 | #define RX_THR_HIGH 0x7a120180 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 333 | #define RX_THR_SLOW 0xffff0180 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 334 | |
| 335 | /* USB_TX_DMA */ |
| 336 | #define TEST_MODE_DISABLE 0x00000001 |
| 337 | #define TX_SIZE_ADJUST1 0x00000100 |
| 338 | |
| 339 | /* USB_UPS_CTRL */ |
| 340 | #define POWER_CUT 0x0100 |
| 341 | |
| 342 | /* USB_PM_CTRL_STATUS */ |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 343 | #define RESUME_INDICATE 0x0001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 344 | |
| 345 | /* USB_USB_CTRL */ |
| 346 | #define RX_AGG_DISABLE 0x0010 |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 347 | #define RX_ZERO_EN 0x0080 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 348 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 349 | /* USB_U2P3_CTRL */ |
| 350 | #define U2P3_ENABLE 0x0001 |
| 351 | |
| 352 | /* USB_POWER_CUT */ |
| 353 | #define PWR_EN 0x0001 |
| 354 | #define PHASE2_EN 0x0008 |
| 355 | |
| 356 | /* USB_MISC_0 */ |
| 357 | #define PCUT_STATUS 0x0001 |
| 358 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 359 | /* USB_RX_EARLY_TIMEOUT */ |
| 360 | #define COALESCE_SUPER 85000U |
| 361 | #define COALESCE_HIGH 250000U |
| 362 | #define COALESCE_SLOW 524280U |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 363 | |
| 364 | /* USB_WDT11_CTRL */ |
| 365 | #define TIMER11_EN 0x0001 |
| 366 | |
| 367 | /* USB_LPM_CTRL */ |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 368 | /* bit 4 ~ 5: fifo empty boundary */ |
| 369 | #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */ |
| 370 | /* bit 2 ~ 3: LMP timer */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 371 | #define LPM_TIMER_MASK 0x0c |
| 372 | #define LPM_TIMER_500MS 0x04 /* 500 ms */ |
| 373 | #define LPM_TIMER_500US 0x0c /* 500 us */ |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 374 | #define ROK_EXIT_LPM 0x02 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 375 | |
| 376 | /* USB_AFE_CTRL2 */ |
| 377 | #define SEN_VAL_MASK 0xf800 |
| 378 | #define SEN_VAL_NORMAL 0xa000 |
| 379 | #define SEL_RXIDLE 0x0100 |
| 380 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 381 | /* OCP_ALDPS_CONFIG */ |
| 382 | #define ENPWRSAVE 0x8000 |
| 383 | #define ENPDNPS 0x0200 |
| 384 | #define LINKENA 0x0100 |
| 385 | #define DIS_SDSAVE 0x0010 |
| 386 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 387 | /* OCP_PHY_STATUS */ |
| 388 | #define PHY_STAT_MASK 0x0007 |
| 389 | #define PHY_STAT_LAN_ON 3 |
| 390 | #define PHY_STAT_PWRDN 5 |
| 391 | |
| 392 | /* OCP_POWER_CFG */ |
| 393 | #define EEE_CLKDIV_EN 0x8000 |
| 394 | #define EN_ALDPS 0x0004 |
| 395 | #define EN_10M_PLLOFF 0x0001 |
| 396 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 397 | /* OCP_EEE_CONFIG1 */ |
| 398 | #define RG_TXLPI_MSK_HFDUP 0x8000 |
| 399 | #define RG_MATCLR_EN 0x4000 |
| 400 | #define EEE_10_CAP 0x2000 |
| 401 | #define EEE_NWAY_EN 0x1000 |
| 402 | #define TX_QUIET_EN 0x0200 |
| 403 | #define RX_QUIET_EN 0x0100 |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 404 | #define sd_rise_time_mask 0x0070 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 405 | #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 406 | #define RG_RXLPI_MSK_HFDUP 0x0008 |
| 407 | #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ |
| 408 | |
| 409 | /* OCP_EEE_CONFIG2 */ |
| 410 | #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ |
| 411 | #define RG_DACQUIET_EN 0x0400 |
| 412 | #define RG_LDVQUIET_EN 0x0200 |
| 413 | #define RG_CKRSEL 0x0020 |
| 414 | #define RG_EEEPRG_EN 0x0010 |
| 415 | |
| 416 | /* OCP_EEE_CONFIG3 */ |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 417 | #define fast_snr_mask 0xff80 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 418 | #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 419 | #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ |
| 420 | #define MSK_PH 0x0006 /* bit 0 ~ 3 */ |
| 421 | |
| 422 | /* OCP_EEE_AR */ |
| 423 | /* bit[15:14] function */ |
| 424 | #define FUN_ADDR 0x0000 |
| 425 | #define FUN_DATA 0x4000 |
| 426 | /* bit[4:0] device addr */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 427 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 428 | /* OCP_EEE_CFG */ |
| 429 | #define CTAP_SHORT_EN 0x0040 |
| 430 | #define EEE10_EN 0x0010 |
| 431 | |
| 432 | /* OCP_DOWN_SPEED */ |
| 433 | #define EN_10M_BGOFF 0x0080 |
| 434 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 435 | /* OCP_ADC_CFG */ |
| 436 | #define CKADSEL_L 0x0100 |
| 437 | #define ADC_EN 0x0080 |
| 438 | #define EN_EMI_L 0x0040 |
| 439 | |
| 440 | /* SRAM_LPF_CFG */ |
| 441 | #define LPF_AUTO_TUNE 0x8000 |
| 442 | |
| 443 | /* SRAM_10M_AMP1 */ |
| 444 | #define GDAC_IB_UPALL 0x0008 |
| 445 | |
| 446 | /* SRAM_10M_AMP2 */ |
| 447 | #define AMP_DN 0x0200 |
| 448 | |
| 449 | /* SRAM_IMPEDANCE */ |
| 450 | #define RX_DRIVING_MASK 0x6000 |
| 451 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 452 | enum rtl_register_content { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 453 | _1000bps = 0x10, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 454 | _100bps = 0x08, |
| 455 | _10bps = 0x04, |
| 456 | LINK_STATUS = 0x02, |
| 457 | FULL_DUP = 0x01, |
| 458 | }; |
| 459 | |
hayeswang | 1764bcd | 2014-08-28 10:24:18 +0800 | [diff] [blame] | 460 | #define RTL8152_MAX_TX 4 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 461 | #define RTL8152_MAX_RX 10 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 462 | #define INTBUFSIZE 2 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 463 | #define CRC_SIZE 4 |
| 464 | #define TX_ALIGN 4 |
| 465 | #define RX_ALIGN 8 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 466 | |
| 467 | #define INTR_LINK 0x0004 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 468 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 469 | #define RTL8152_REQT_READ 0xc0 |
| 470 | #define RTL8152_REQT_WRITE 0x40 |
| 471 | #define RTL8152_REQ_GET_REGS 0x05 |
| 472 | #define RTL8152_REQ_SET_REGS 0x05 |
| 473 | |
| 474 | #define BYTE_EN_DWORD 0xff |
| 475 | #define BYTE_EN_WORD 0x33 |
| 476 | #define BYTE_EN_BYTE 0x11 |
| 477 | #define BYTE_EN_SIX_BYTES 0x3f |
| 478 | #define BYTE_EN_START_MASK 0x0f |
| 479 | #define BYTE_EN_END_MASK 0xf0 |
| 480 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 481 | #define RTL8153_MAX_PACKET 9216 /* 9K */ |
| 482 | #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 483 | #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN) |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 484 | #define RTL8153_RMS RTL8153_MAX_PACKET |
hayeswang | b812540 | 2014-07-03 11:55:48 +0800 | [diff] [blame] | 485 | #define RTL8152_TX_TIMEOUT (5 * HZ) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 486 | #define RTL8152_NAPI_WEIGHT 64 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 487 | |
| 488 | /* rtl8152 flags */ |
| 489 | enum rtl8152_flags { |
| 490 | RTL8152_UNPLUG = 0, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 491 | RTL8152_SET_RX_MODE, |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 492 | WORK_ENABLE, |
| 493 | RTL8152_LINK_CHG, |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 494 | SELECTIVE_SUSPEND, |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 495 | PHY_RESET, |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 496 | SCHEDULE_NAPI, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | /* Define these values to match your device */ |
| 500 | #define VENDOR_ID_REALTEK 0x0bda |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 501 | #define VENDOR_ID_SAMSUNG 0x04e8 |
Christian Hesse | 347eec3 | 2015-03-31 14:10:07 +0200 | [diff] [blame] | 502 | #define VENDOR_ID_LENOVO 0x17ef |
Zheng Liu | d065c3c1 | 2015-07-07 13:54:12 -0700 | [diff] [blame] | 503 | #define VENDOR_ID_NVIDIA 0x0955 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 504 | |
| 505 | #define MCU_TYPE_PLA 0x0100 |
| 506 | #define MCU_TYPE_USB 0x0000 |
| 507 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 508 | struct tally_counter { |
| 509 | __le64 tx_packets; |
| 510 | __le64 rx_packets; |
| 511 | __le64 tx_errors; |
| 512 | __le32 rx_errors; |
| 513 | __le16 rx_missed; |
| 514 | __le16 align_errors; |
| 515 | __le32 tx_one_collision; |
| 516 | __le32 tx_multi_collision; |
| 517 | __le64 rx_unicast; |
| 518 | __le64 rx_broadcast; |
| 519 | __le32 rx_multicast; |
| 520 | __le16 tx_aborted; |
hayeswang | f37119c | 2014-10-28 14:05:51 +0800 | [diff] [blame] | 521 | __le16 tx_underrun; |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 522 | }; |
| 523 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 524 | struct rx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 525 | __le32 opts1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 526 | #define RX_LEN_MASK 0x7fff |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 527 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 528 | __le32 opts2; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 529 | #define RD_UDP_CS BIT(23) |
| 530 | #define RD_TCP_CS BIT(22) |
| 531 | #define RD_IPV6_CS BIT(20) |
| 532 | #define RD_IPV4_CS BIT(19) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 533 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 534 | __le32 opts3; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 535 | #define IPF BIT(23) /* IP checksum fail */ |
| 536 | #define UDPF BIT(22) /* UDP checksum fail */ |
| 537 | #define TCPF BIT(21) /* TCP checksum fail */ |
| 538 | #define RX_VLAN_TAG BIT(16) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 539 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 540 | __le32 opts4; |
| 541 | __le32 opts5; |
| 542 | __le32 opts6; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
| 545 | struct tx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 546 | __le32 opts1; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 547 | #define TX_FS BIT(31) /* First segment of a packet */ |
| 548 | #define TX_LS BIT(30) /* Final segment of a packet */ |
| 549 | #define GTSENDV4 BIT(28) |
| 550 | #define GTSENDV6 BIT(27) |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 551 | #define GTTCPHO_SHIFT 18 |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 552 | #define GTTCPHO_MAX 0x7fU |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 553 | #define TX_LEN_MAX 0x3ffffU |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 554 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 555 | __le32 opts2; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 556 | #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */ |
| 557 | #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */ |
| 558 | #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */ |
| 559 | #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */ |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 560 | #define MSS_SHIFT 17 |
| 561 | #define MSS_MAX 0x7ffU |
| 562 | #define TCPHO_SHIFT 17 |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 563 | #define TCPHO_MAX 0x7ffU |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 564 | #define TX_VLAN_TAG BIT(16) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 565 | }; |
| 566 | |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 567 | struct r8152; |
| 568 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 569 | struct rx_agg { |
| 570 | struct list_head list; |
| 571 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 572 | struct r8152 *context; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 573 | void *buffer; |
| 574 | void *head; |
| 575 | }; |
| 576 | |
| 577 | struct tx_agg { |
| 578 | struct list_head list; |
| 579 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 580 | struct r8152 *context; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 581 | void *buffer; |
| 582 | void *head; |
| 583 | u32 skb_num; |
| 584 | u32 skb_len; |
| 585 | }; |
| 586 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 587 | struct r8152 { |
| 588 | unsigned long flags; |
| 589 | struct usb_device *udev; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 590 | struct napi_struct napi; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 591 | struct usb_interface *intf; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 592 | struct net_device *netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 593 | struct urb *intr_urb; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 594 | struct tx_agg tx_info[RTL8152_MAX_TX]; |
| 595 | struct rx_agg rx_info[RTL8152_MAX_RX]; |
| 596 | struct list_head rx_done, tx_free; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 597 | struct sk_buff_head tx_queue, rx_queue; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 598 | spinlock_t rx_lock, tx_lock; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 599 | struct delayed_work schedule; |
| 600 | struct mii_if_info mii; |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 601 | struct mutex control; /* use for hw setting */ |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 602 | |
| 603 | struct rtl_ops { |
| 604 | void (*init)(struct r8152 *); |
| 605 | int (*enable)(struct r8152 *); |
| 606 | void (*disable)(struct r8152 *); |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 607 | void (*up)(struct r8152 *); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 608 | void (*down)(struct r8152 *); |
| 609 | void (*unload)(struct r8152 *); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 610 | int (*eee_get)(struct r8152 *, struct ethtool_eee *); |
| 611 | int (*eee_set)(struct r8152 *, struct ethtool_eee *); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 612 | } rtl_ops; |
| 613 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 614 | int intr_interval; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 615 | u32 saved_wolopts; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 616 | u32 msg_enable; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 617 | u32 tx_qlen; |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 618 | u32 coalesce; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 619 | u16 ocp_base; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 620 | u8 *intr_buff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 621 | u8 version; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 622 | }; |
| 623 | |
| 624 | enum rtl_version { |
| 625 | RTL_VER_UNKNOWN = 0, |
| 626 | RTL_VER_01, |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 627 | RTL_VER_02, |
| 628 | RTL_VER_03, |
| 629 | RTL_VER_04, |
| 630 | RTL_VER_05, |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 631 | RTL_VER_06, |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 632 | RTL_VER_MAX |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 633 | }; |
| 634 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 635 | enum tx_csum_stat { |
| 636 | TX_CSUM_SUCCESS = 0, |
| 637 | TX_CSUM_TSO, |
| 638 | TX_CSUM_NONE |
| 639 | }; |
| 640 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 641 | /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). |
| 642 | * The RTL chips use a 64 element hash table based on the Ethernet CRC. |
| 643 | */ |
| 644 | static const int multicast_filter_limit = 32; |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 645 | static unsigned int agg_buf_sz = 16384; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 646 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 647 | #define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \ |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 648 | VLAN_ETH_HLEN - VLAN_HLEN) |
| 649 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 650 | static |
| 651 | int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 652 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 653 | int ret; |
| 654 | void *tmp; |
| 655 | |
| 656 | tmp = kmalloc(size, GFP_KERNEL); |
| 657 | if (!tmp) |
| 658 | return -ENOMEM; |
| 659 | |
| 660 | ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 661 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
| 662 | value, index, tmp, size, 500); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 663 | |
| 664 | memcpy(data, tmp, size); |
| 665 | kfree(tmp); |
| 666 | |
| 667 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static |
| 671 | int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 672 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 673 | int ret; |
| 674 | void *tmp; |
| 675 | |
Benoit Taine | c4438f0 | 2014-05-26 17:21:23 +0200 | [diff] [blame] | 676 | tmp = kmemdup(data, size, GFP_KERNEL); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 677 | if (!tmp) |
| 678 | return -ENOMEM; |
| 679 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 680 | ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 681 | RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, |
| 682 | value, index, tmp, size, 500); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 683 | |
| 684 | kfree(tmp); |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 685 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 686 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 690 | void *data, u16 type) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 691 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 692 | u16 limit = 64; |
| 693 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 694 | |
| 695 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 696 | return -ENODEV; |
| 697 | |
| 698 | /* both size and indix must be 4 bytes align */ |
| 699 | if ((size & 3) || !size || (index & 3) || !data) |
| 700 | return -EPERM; |
| 701 | |
| 702 | if ((u32)index + (u32)size > 0xffff) |
| 703 | return -EPERM; |
| 704 | |
| 705 | while (size) { |
| 706 | if (size > limit) { |
| 707 | ret = get_registers(tp, index, type, limit, data); |
| 708 | if (ret < 0) |
| 709 | break; |
| 710 | |
| 711 | index += limit; |
| 712 | data += limit; |
| 713 | size -= limit; |
| 714 | } else { |
| 715 | ret = get_registers(tp, index, type, size, data); |
| 716 | if (ret < 0) |
| 717 | break; |
| 718 | |
| 719 | index += size; |
| 720 | data += size; |
| 721 | size = 0; |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 726 | if (ret == -ENODEV) |
| 727 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 728 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 733 | u16 size, void *data, u16 type) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 734 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 735 | int ret; |
| 736 | u16 byteen_start, byteen_end, byen; |
| 737 | u16 limit = 512; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 738 | |
| 739 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 740 | return -ENODEV; |
| 741 | |
| 742 | /* both size and indix must be 4 bytes align */ |
| 743 | if ((size & 3) || !size || (index & 3) || !data) |
| 744 | return -EPERM; |
| 745 | |
| 746 | if ((u32)index + (u32)size > 0xffff) |
| 747 | return -EPERM; |
| 748 | |
| 749 | byteen_start = byteen & BYTE_EN_START_MASK; |
| 750 | byteen_end = byteen & BYTE_EN_END_MASK; |
| 751 | |
| 752 | byen = byteen_start | (byteen_start << 4); |
| 753 | ret = set_registers(tp, index, type | byen, 4, data); |
| 754 | if (ret < 0) |
| 755 | goto error1; |
| 756 | |
| 757 | index += 4; |
| 758 | data += 4; |
| 759 | size -= 4; |
| 760 | |
| 761 | if (size) { |
| 762 | size -= 4; |
| 763 | |
| 764 | while (size) { |
| 765 | if (size > limit) { |
| 766 | ret = set_registers(tp, index, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 767 | type | BYTE_EN_DWORD, |
| 768 | limit, data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 769 | if (ret < 0) |
| 770 | goto error1; |
| 771 | |
| 772 | index += limit; |
| 773 | data += limit; |
| 774 | size -= limit; |
| 775 | } else { |
| 776 | ret = set_registers(tp, index, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 777 | type | BYTE_EN_DWORD, |
| 778 | size, data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 779 | if (ret < 0) |
| 780 | goto error1; |
| 781 | |
| 782 | index += size; |
| 783 | data += size; |
| 784 | size = 0; |
| 785 | break; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | byen = byteen_end | (byteen_end >> 4); |
| 790 | ret = set_registers(tp, index, type | byen, 4, data); |
| 791 | if (ret < 0) |
| 792 | goto error1; |
| 793 | } |
| 794 | |
| 795 | error1: |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 796 | if (ret == -ENODEV) |
| 797 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 798 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | static inline |
| 803 | int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 804 | { |
| 805 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); |
| 806 | } |
| 807 | |
| 808 | static inline |
| 809 | int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 810 | { |
| 811 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); |
| 812 | } |
| 813 | |
| 814 | static inline |
| 815 | int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 816 | { |
| 817 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB); |
| 818 | } |
| 819 | |
| 820 | static inline |
| 821 | int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 822 | { |
| 823 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); |
| 824 | } |
| 825 | |
| 826 | static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) |
| 827 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 828 | __le32 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 829 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 830 | generic_ocp_read(tp, index, sizeof(data), &data, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 831 | |
| 832 | return __le32_to_cpu(data); |
| 833 | } |
| 834 | |
| 835 | static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 836 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 837 | __le32 tmp = __cpu_to_le32(data); |
| 838 | |
| 839 | generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) |
| 843 | { |
| 844 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 845 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 846 | u8 shift = index & 2; |
| 847 | |
| 848 | index &= ~3; |
| 849 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 850 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 851 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 852 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 853 | data >>= (shift * 8); |
| 854 | data &= 0xffff; |
| 855 | |
| 856 | return (u16)data; |
| 857 | } |
| 858 | |
| 859 | static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 860 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 861 | u32 mask = 0xffff; |
| 862 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 863 | u16 byen = BYTE_EN_WORD; |
| 864 | u8 shift = index & 2; |
| 865 | |
| 866 | data &= mask; |
| 867 | |
| 868 | if (index & 2) { |
| 869 | byen <<= shift; |
| 870 | mask <<= (shift * 8); |
| 871 | data <<= (shift * 8); |
| 872 | index &= ~3; |
| 873 | } |
| 874 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 875 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 876 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 877 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) |
| 881 | { |
| 882 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 883 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 884 | u8 shift = index & 3; |
| 885 | |
| 886 | index &= ~3; |
| 887 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 888 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 889 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 890 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 891 | data >>= (shift * 8); |
| 892 | data &= 0xff; |
| 893 | |
| 894 | return (u8)data; |
| 895 | } |
| 896 | |
| 897 | static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 898 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 899 | u32 mask = 0xff; |
| 900 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 901 | u16 byen = BYTE_EN_BYTE; |
| 902 | u8 shift = index & 3; |
| 903 | |
| 904 | data &= mask; |
| 905 | |
| 906 | if (index & 3) { |
| 907 | byen <<= shift; |
| 908 | mask <<= (shift * 8); |
| 909 | data <<= (shift * 8); |
| 910 | index &= ~3; |
| 911 | } |
| 912 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 913 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 914 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 915 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 916 | } |
| 917 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 918 | static u16 ocp_reg_read(struct r8152 *tp, u16 addr) |
| 919 | { |
| 920 | u16 ocp_base, ocp_index; |
| 921 | |
| 922 | ocp_base = addr & 0xf000; |
| 923 | if (ocp_base != tp->ocp_base) { |
| 924 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 925 | tp->ocp_base = ocp_base; |
| 926 | } |
| 927 | |
| 928 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 929 | return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); |
| 930 | } |
| 931 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 932 | static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) |
| 933 | { |
| 934 | u16 ocp_base, ocp_index; |
| 935 | |
| 936 | ocp_base = addr & 0xf000; |
| 937 | if (ocp_base != tp->ocp_base) { |
| 938 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 939 | tp->ocp_base = ocp_base; |
| 940 | } |
| 941 | |
| 942 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 943 | ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); |
| 944 | } |
| 945 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 946 | 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] | 947 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 948 | ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 949 | } |
| 950 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 951 | static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 952 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 953 | return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 954 | } |
| 955 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 956 | static void sram_write(struct r8152 *tp, u16 addr, u16 data) |
| 957 | { |
| 958 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 959 | ocp_reg_write(tp, OCP_SRAM_DATA, data); |
| 960 | } |
| 961 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 962 | static int read_mii_word(struct net_device *netdev, int phy_id, int reg) |
| 963 | { |
| 964 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 965 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 966 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 967 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 968 | return -ENODEV; |
| 969 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 970 | if (phy_id != R8152_PHY_ID) |
| 971 | return -EINVAL; |
| 972 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 973 | ret = r8152_mdio_read(tp, reg); |
| 974 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 975 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | static |
| 979 | void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) |
| 980 | { |
| 981 | struct r8152 *tp = netdev_priv(netdev); |
| 982 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 983 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 984 | return; |
| 985 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 986 | if (phy_id != R8152_PHY_ID) |
| 987 | return; |
| 988 | |
| 989 | r8152_mdio_write(tp, reg, val); |
| 990 | } |
| 991 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 992 | static int |
| 993 | 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] | 994 | |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 995 | static int rtl8152_set_mac_address(struct net_device *netdev, void *p) |
| 996 | { |
| 997 | struct r8152 *tp = netdev_priv(netdev); |
| 998 | struct sockaddr *addr = p; |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 999 | int ret = -EADDRNOTAVAIL; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1000 | |
| 1001 | if (!is_valid_ether_addr(addr->sa_data)) |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1002 | goto out1; |
| 1003 | |
| 1004 | ret = usb_autopm_get_interface(tp->intf); |
| 1005 | if (ret < 0) |
| 1006 | goto out1; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1007 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 1008 | mutex_lock(&tp->control); |
| 1009 | |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1010 | memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); |
| 1011 | |
| 1012 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 1013 | pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data); |
| 1014 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 1015 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 1016 | mutex_unlock(&tp->control); |
| 1017 | |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1018 | usb_autopm_put_interface(tp->intf); |
| 1019 | out1: |
| 1020 | return ret; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1021 | } |
| 1022 | |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1023 | static int set_ethernet_addr(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1024 | { |
| 1025 | struct net_device *dev = tp->netdev; |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1026 | struct sockaddr sa; |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1027 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1028 | |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1029 | if (tp->version == RTL_VER_01) |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1030 | ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data); |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1031 | else |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1032 | ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data); |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1033 | |
| 1034 | if (ret < 0) { |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1035 | netif_err(tp, probe, dev, "Get ether addr fail\n"); |
| 1036 | } else if (!is_valid_ether_addr(sa.sa_data)) { |
| 1037 | netif_err(tp, probe, dev, "Invalid ether addr %pM\n", |
| 1038 | sa.sa_data); |
| 1039 | eth_hw_addr_random(dev); |
| 1040 | ether_addr_copy(sa.sa_data, dev->dev_addr); |
| 1041 | ret = rtl8152_set_mac_address(dev, &sa); |
| 1042 | netif_info(tp, probe, dev, "Random ether addr %pM\n", |
| 1043 | sa.sa_data); |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1044 | } else { |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1045 | if (tp->version == RTL_VER_01) |
| 1046 | ether_addr_copy(dev->dev_addr, sa.sa_data); |
| 1047 | else |
| 1048 | ret = rtl8152_set_mac_address(dev, &sa); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1049 | } |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1050 | |
| 1051 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1054 | static void read_bulk_callback(struct urb *urb) |
| 1055 | { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1056 | struct net_device *netdev; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1057 | int status = urb->status; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1058 | struct rx_agg *agg; |
| 1059 | struct r8152 *tp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1060 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1061 | agg = urb->context; |
| 1062 | if (!agg) |
| 1063 | return; |
| 1064 | |
| 1065 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1066 | if (!tp) |
| 1067 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1068 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1069 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1070 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1071 | |
| 1072 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1073 | return; |
| 1074 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1075 | netdev = tp->netdev; |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 1076 | |
| 1077 | /* When link down, the driver would cancel all bulks. */ |
| 1078 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1079 | if (!netif_carrier_ok(netdev)) |
| 1080 | return; |
| 1081 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1082 | usb_mark_last_busy(tp->udev); |
| 1083 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1084 | switch (status) { |
| 1085 | case 0: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1086 | if (urb->actual_length < ETH_ZLEN) |
| 1087 | break; |
| 1088 | |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 1089 | spin_lock(&tp->rx_lock); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1090 | list_add_tail(&agg->list, &tp->rx_done); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 1091 | spin_unlock(&tp->rx_lock); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1092 | napi_schedule(&tp->napi); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1093 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1094 | case -ESHUTDOWN: |
| 1095 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 1096 | netif_device_detach(tp->netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1097 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1098 | case -ENOENT: |
| 1099 | return; /* the urb is in unlink state */ |
| 1100 | case -ETIME: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1101 | if (net_ratelimit()) |
| 1102 | netdev_warn(netdev, "maybe reset is needed?\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1103 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1104 | default: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1105 | if (net_ratelimit()) |
| 1106 | netdev_warn(netdev, "Rx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1107 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 1110 | r8152_submit_rx(tp, agg, GFP_ATOMIC); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static void write_bulk_callback(struct urb *urb) |
| 1114 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1115 | struct net_device_stats *stats; |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1116 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1117 | struct tx_agg *agg; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1118 | struct r8152 *tp; |
| 1119 | int status = urb->status; |
| 1120 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1121 | agg = urb->context; |
| 1122 | if (!agg) |
| 1123 | return; |
| 1124 | |
| 1125 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1126 | if (!tp) |
| 1127 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1128 | |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1129 | netdev = tp->netdev; |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 1130 | stats = &netdev->stats; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1131 | if (status) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1132 | if (net_ratelimit()) |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1133 | netdev_warn(netdev, "Tx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1134 | stats->tx_errors += agg->skb_num; |
| 1135 | } else { |
| 1136 | stats->tx_packets += agg->skb_num; |
| 1137 | stats->tx_bytes += agg->skb_len; |
| 1138 | } |
| 1139 | |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 1140 | spin_lock(&tp->tx_lock); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1141 | list_add_tail(&agg->list, &tp->tx_free); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 1142 | spin_unlock(&tp->tx_lock); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1143 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1144 | usb_autopm_put_interface_async(tp->intf); |
| 1145 | |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1146 | if (!netif_carrier_ok(netdev)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1147 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1148 | |
| 1149 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1150 | return; |
| 1151 | |
| 1152 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1153 | return; |
| 1154 | |
| 1155 | if (!skb_queue_empty(&tp->tx_queue)) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1156 | napi_schedule(&tp->napi); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1157 | } |
| 1158 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1159 | static void intr_callback(struct urb *urb) |
| 1160 | { |
| 1161 | struct r8152 *tp; |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 1162 | __le16 *d; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1163 | int status = urb->status; |
| 1164 | int res; |
| 1165 | |
| 1166 | tp = urb->context; |
| 1167 | if (!tp) |
| 1168 | return; |
| 1169 | |
| 1170 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1171 | return; |
| 1172 | |
| 1173 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1174 | return; |
| 1175 | |
| 1176 | switch (status) { |
| 1177 | case 0: /* success */ |
| 1178 | break; |
| 1179 | case -ECONNRESET: /* unlink */ |
| 1180 | case -ESHUTDOWN: |
| 1181 | netif_device_detach(tp->netdev); |
| 1182 | case -ENOENT: |
hayeswang | d59c876 | 2014-10-31 13:35:57 +0800 | [diff] [blame] | 1183 | case -EPROTO: |
| 1184 | netif_info(tp, intr, tp->netdev, |
| 1185 | "Stop submitting intr, status %d\n", status); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1186 | return; |
| 1187 | case -EOVERFLOW: |
| 1188 | netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n"); |
| 1189 | goto resubmit; |
| 1190 | /* -EPIPE: should clear the halt */ |
| 1191 | default: |
| 1192 | netif_info(tp, intr, tp->netdev, "intr status %d\n", status); |
| 1193 | goto resubmit; |
| 1194 | } |
| 1195 | |
| 1196 | d = urb->transfer_buffer; |
| 1197 | if (INTR_LINK & __le16_to_cpu(d[0])) { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 1198 | if (!netif_carrier_ok(tp->netdev)) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1199 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1200 | schedule_delayed_work(&tp->schedule, 0); |
| 1201 | } |
| 1202 | } else { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 1203 | if (netif_carrier_ok(tp->netdev)) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1204 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1205 | schedule_delayed_work(&tp->schedule, 0); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | resubmit: |
| 1210 | res = usb_submit_urb(urb, GFP_ATOMIC); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1211 | if (res == -ENODEV) { |
| 1212 | set_bit(RTL8152_UNPLUG, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1213 | netif_device_detach(tp->netdev); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1214 | } else if (res) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1215 | netif_err(tp, intr, tp->netdev, |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1216 | "can't resubmit intr, status %d\n", res); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1217 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1218 | } |
| 1219 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1220 | static inline void *rx_agg_align(void *data) |
| 1221 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1222 | return (void *)ALIGN((uintptr_t)data, RX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | static inline void *tx_agg_align(void *data) |
| 1226 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1227 | return (void *)ALIGN((uintptr_t)data, TX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | static void free_all_mem(struct r8152 *tp) |
| 1231 | { |
| 1232 | int i; |
| 1233 | |
| 1234 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1235 | usb_free_urb(tp->rx_info[i].urb); |
| 1236 | tp->rx_info[i].urb = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1237 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1238 | kfree(tp->rx_info[i].buffer); |
| 1239 | tp->rx_info[i].buffer = NULL; |
| 1240 | tp->rx_info[i].head = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1244 | usb_free_urb(tp->tx_info[i].urb); |
| 1245 | tp->tx_info[i].urb = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1246 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1247 | kfree(tp->tx_info[i].buffer); |
| 1248 | tp->tx_info[i].buffer = NULL; |
| 1249 | tp->tx_info[i].head = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1250 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1251 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1252 | usb_free_urb(tp->intr_urb); |
| 1253 | tp->intr_urb = NULL; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1254 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1255 | kfree(tp->intr_buff); |
| 1256 | tp->intr_buff = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | static int alloc_all_mem(struct r8152 *tp) |
| 1260 | { |
| 1261 | struct net_device *netdev = tp->netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1262 | struct usb_interface *intf = tp->intf; |
| 1263 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1264 | struct usb_host_endpoint *ep_intr = alt->endpoint + 2; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1265 | struct urb *urb; |
| 1266 | int node, i; |
| 1267 | u8 *buf; |
| 1268 | |
| 1269 | node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; |
| 1270 | |
| 1271 | spin_lock_init(&tp->rx_lock); |
| 1272 | spin_lock_init(&tp->tx_lock); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1273 | INIT_LIST_HEAD(&tp->tx_free); |
| 1274 | skb_queue_head_init(&tp->tx_queue); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1275 | skb_queue_head_init(&tp->rx_queue); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1276 | |
| 1277 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1278 | buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1279 | if (!buf) |
| 1280 | goto err1; |
| 1281 | |
| 1282 | if (buf != rx_agg_align(buf)) { |
| 1283 | kfree(buf); |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1284 | buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL, |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1285 | node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1286 | if (!buf) |
| 1287 | goto err1; |
| 1288 | } |
| 1289 | |
| 1290 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1291 | if (!urb) { |
| 1292 | kfree(buf); |
| 1293 | goto err1; |
| 1294 | } |
| 1295 | |
| 1296 | INIT_LIST_HEAD(&tp->rx_info[i].list); |
| 1297 | tp->rx_info[i].context = tp; |
| 1298 | tp->rx_info[i].urb = urb; |
| 1299 | tp->rx_info[i].buffer = buf; |
| 1300 | tp->rx_info[i].head = rx_agg_align(buf); |
| 1301 | } |
| 1302 | |
| 1303 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1304 | buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1305 | if (!buf) |
| 1306 | goto err1; |
| 1307 | |
| 1308 | if (buf != tx_agg_align(buf)) { |
| 1309 | kfree(buf); |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1310 | buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL, |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1311 | node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1312 | if (!buf) |
| 1313 | goto err1; |
| 1314 | } |
| 1315 | |
| 1316 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1317 | if (!urb) { |
| 1318 | kfree(buf); |
| 1319 | goto err1; |
| 1320 | } |
| 1321 | |
| 1322 | INIT_LIST_HEAD(&tp->tx_info[i].list); |
| 1323 | tp->tx_info[i].context = tp; |
| 1324 | tp->tx_info[i].urb = urb; |
| 1325 | tp->tx_info[i].buffer = buf; |
| 1326 | tp->tx_info[i].head = tx_agg_align(buf); |
| 1327 | |
| 1328 | list_add_tail(&tp->tx_info[i].list, &tp->tx_free); |
| 1329 | } |
| 1330 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1331 | tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1332 | if (!tp->intr_urb) |
| 1333 | goto err1; |
| 1334 | |
| 1335 | tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); |
| 1336 | if (!tp->intr_buff) |
| 1337 | goto err1; |
| 1338 | |
| 1339 | tp->intr_interval = (int)ep_intr->desc.bInterval; |
| 1340 | 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] | 1341 | tp->intr_buff, INTBUFSIZE, intr_callback, |
| 1342 | tp, tp->intr_interval); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1343 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1344 | return 0; |
| 1345 | |
| 1346 | err1: |
| 1347 | free_all_mem(tp); |
| 1348 | return -ENOMEM; |
| 1349 | } |
| 1350 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1351 | static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp) |
| 1352 | { |
| 1353 | struct tx_agg *agg = NULL; |
| 1354 | unsigned long flags; |
| 1355 | |
hayeswang | 21949ab | 2014-03-07 11:04:35 +0800 | [diff] [blame] | 1356 | if (list_empty(&tp->tx_free)) |
| 1357 | return NULL; |
| 1358 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1359 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 1360 | if (!list_empty(&tp->tx_free)) { |
| 1361 | struct list_head *cursor; |
| 1362 | |
| 1363 | cursor = tp->tx_free.next; |
| 1364 | list_del_init(cursor); |
| 1365 | agg = list_entry(cursor, struct tx_agg, list); |
| 1366 | } |
| 1367 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 1368 | |
| 1369 | return agg; |
| 1370 | } |
| 1371 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1372 | /* r8152_csum_workaround() |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1373 | * The hw limites the value the transport offset. When the offset is out of the |
| 1374 | * range, calculate the checksum by sw. |
| 1375 | */ |
| 1376 | static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb, |
| 1377 | struct sk_buff_head *list) |
| 1378 | { |
| 1379 | if (skb_shinfo(skb)->gso_size) { |
| 1380 | netdev_features_t features = tp->netdev->features; |
| 1381 | struct sk_buff_head seg_list; |
| 1382 | struct sk_buff *segs, *nskb; |
| 1383 | |
hayeswang | a91d45f | 2014-07-11 16:48:27 +0800 | [diff] [blame] | 1384 | features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6); |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1385 | segs = skb_gso_segment(skb, features); |
| 1386 | if (IS_ERR(segs) || !segs) |
| 1387 | goto drop; |
| 1388 | |
| 1389 | __skb_queue_head_init(&seg_list); |
| 1390 | |
| 1391 | do { |
| 1392 | nskb = segs; |
| 1393 | segs = segs->next; |
| 1394 | nskb->next = NULL; |
| 1395 | __skb_queue_tail(&seg_list, nskb); |
| 1396 | } while (segs); |
| 1397 | |
| 1398 | skb_queue_splice(&seg_list, list); |
| 1399 | dev_kfree_skb(skb); |
| 1400 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1401 | if (skb_checksum_help(skb) < 0) |
| 1402 | goto drop; |
| 1403 | |
| 1404 | __skb_queue_head(list, skb); |
| 1405 | } else { |
| 1406 | struct net_device_stats *stats; |
| 1407 | |
| 1408 | drop: |
| 1409 | stats = &tp->netdev->stats; |
| 1410 | stats->tx_dropped++; |
| 1411 | dev_kfree_skb(skb); |
| 1412 | } |
| 1413 | } |
| 1414 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1415 | /* msdn_giant_send_check() |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1416 | * According to the document of microsoft, the TCP Pseudo Header excludes the |
| 1417 | * packet length for IPv6 TCP large packets. |
| 1418 | */ |
| 1419 | static int msdn_giant_send_check(struct sk_buff *skb) |
| 1420 | { |
| 1421 | const struct ipv6hdr *ipv6h; |
| 1422 | struct tcphdr *th; |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1423 | int ret; |
| 1424 | |
| 1425 | ret = skb_cow_head(skb, 0); |
| 1426 | if (ret) |
| 1427 | return ret; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1428 | |
| 1429 | ipv6h = ipv6_hdr(skb); |
| 1430 | th = tcp_hdr(skb); |
| 1431 | |
| 1432 | th->check = 0; |
| 1433 | th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0); |
| 1434 | |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1435 | return ret; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1436 | } |
| 1437 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1438 | static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb) |
| 1439 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1440 | if (skb_vlan_tag_present(skb)) { |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1441 | u32 opts2; |
| 1442 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1443 | opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb)); |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1444 | desc->opts2 |= cpu_to_le32(opts2); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb) |
| 1449 | { |
| 1450 | u32 opts2 = le32_to_cpu(desc->opts2); |
| 1451 | |
| 1452 | if (opts2 & RX_VLAN_TAG) |
| 1453 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
| 1454 | swab16(opts2 & 0xffff)); |
| 1455 | } |
| 1456 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1457 | static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, |
| 1458 | struct sk_buff *skb, u32 len, u32 transport_offset) |
| 1459 | { |
| 1460 | u32 mss = skb_shinfo(skb)->gso_size; |
| 1461 | u32 opts1, opts2 = 0; |
| 1462 | int ret = TX_CSUM_SUCCESS; |
| 1463 | |
| 1464 | WARN_ON_ONCE(len > TX_LEN_MAX); |
| 1465 | |
| 1466 | opts1 = len | TX_FS | TX_LS; |
| 1467 | |
| 1468 | if (mss) { |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1469 | if (transport_offset > GTTCPHO_MAX) { |
| 1470 | netif_warn(tp, tx_err, tp->netdev, |
| 1471 | "Invalid transport offset 0x%x for TSO\n", |
| 1472 | transport_offset); |
| 1473 | ret = TX_CSUM_TSO; |
| 1474 | goto unavailable; |
| 1475 | } |
| 1476 | |
hayeswang | 6e74d17 | 2015-02-06 11:30:50 +0800 | [diff] [blame] | 1477 | switch (vlan_get_protocol(skb)) { |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1478 | case htons(ETH_P_IP): |
| 1479 | opts1 |= GTSENDV4; |
| 1480 | break; |
| 1481 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1482 | case htons(ETH_P_IPV6): |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1483 | if (msdn_giant_send_check(skb)) { |
| 1484 | ret = TX_CSUM_TSO; |
| 1485 | goto unavailable; |
| 1486 | } |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1487 | opts1 |= GTSENDV6; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1488 | break; |
| 1489 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1490 | default: |
| 1491 | WARN_ON_ONCE(1); |
| 1492 | break; |
| 1493 | } |
| 1494 | |
| 1495 | opts1 |= transport_offset << GTTCPHO_SHIFT; |
| 1496 | opts2 |= min(mss, MSS_MAX) << MSS_SHIFT; |
| 1497 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1498 | u8 ip_protocol; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1499 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1500 | if (transport_offset > TCPHO_MAX) { |
| 1501 | netif_warn(tp, tx_err, tp->netdev, |
| 1502 | "Invalid transport offset 0x%x\n", |
| 1503 | transport_offset); |
| 1504 | ret = TX_CSUM_NONE; |
| 1505 | goto unavailable; |
| 1506 | } |
| 1507 | |
hayeswang | 6e74d17 | 2015-02-06 11:30:50 +0800 | [diff] [blame] | 1508 | switch (vlan_get_protocol(skb)) { |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1509 | case htons(ETH_P_IP): |
| 1510 | opts2 |= IPV4_CS; |
| 1511 | ip_protocol = ip_hdr(skb)->protocol; |
| 1512 | break; |
| 1513 | |
| 1514 | case htons(ETH_P_IPV6): |
| 1515 | opts2 |= IPV6_CS; |
| 1516 | ip_protocol = ipv6_hdr(skb)->nexthdr; |
| 1517 | break; |
| 1518 | |
| 1519 | default: |
| 1520 | ip_protocol = IPPROTO_RAW; |
| 1521 | break; |
| 1522 | } |
| 1523 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1524 | if (ip_protocol == IPPROTO_TCP) |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1525 | opts2 |= TCP_CS; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1526 | else if (ip_protocol == IPPROTO_UDP) |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1527 | opts2 |= UDP_CS; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1528 | else |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1529 | WARN_ON_ONCE(1); |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1530 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1531 | opts2 |= transport_offset << TCPHO_SHIFT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1532 | } |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1533 | |
| 1534 | desc->opts2 = cpu_to_le32(opts2); |
| 1535 | desc->opts1 = cpu_to_le32(opts1); |
| 1536 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1537 | unavailable: |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1538 | return ret; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1539 | } |
| 1540 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1541 | static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg) |
| 1542 | { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1543 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1544 | int remain, ret; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1545 | u8 *tx_data; |
| 1546 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1547 | __skb_queue_head_init(&skb_head); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1548 | spin_lock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1549 | skb_queue_splice_init(tx_queue, &skb_head); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1550 | spin_unlock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1551 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1552 | tx_data = agg->head; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1553 | agg->skb_num = 0; |
| 1554 | agg->skb_len = 0; |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1555 | remain = agg_buf_sz; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1556 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1557 | while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) { |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1558 | struct tx_desc *tx_desc; |
| 1559 | struct sk_buff *skb; |
| 1560 | unsigned int len; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1561 | u32 offset; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1562 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1563 | skb = __skb_dequeue(&skb_head); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1564 | if (!skb) |
| 1565 | break; |
| 1566 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1567 | len = skb->len + sizeof(*tx_desc); |
| 1568 | |
| 1569 | if (len > remain) { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1570 | __skb_queue_head(&skb_head, skb); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1571 | break; |
| 1572 | } |
| 1573 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1574 | tx_data = tx_agg_align(tx_data); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1575 | tx_desc = (struct tx_desc *)tx_data; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1576 | |
| 1577 | offset = (u32)skb_transport_offset(skb); |
| 1578 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1579 | if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) { |
| 1580 | r8152_csum_workaround(tp, skb, &skb_head); |
| 1581 | continue; |
| 1582 | } |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1583 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1584 | rtl_tx_vlan_tag(tx_desc, skb); |
| 1585 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1586 | tx_data += sizeof(*tx_desc); |
| 1587 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1588 | len = skb->len; |
| 1589 | if (skb_copy_bits(skb, 0, tx_data, len) < 0) { |
| 1590 | struct net_device_stats *stats = &tp->netdev->stats; |
| 1591 | |
| 1592 | stats->tx_dropped++; |
| 1593 | dev_kfree_skb_any(skb); |
| 1594 | tx_data -= sizeof(*tx_desc); |
| 1595 | continue; |
| 1596 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1597 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1598 | tx_data += len; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1599 | agg->skb_len += len; |
| 1600 | agg->skb_num++; |
| 1601 | |
| 1602 | dev_kfree_skb_any(skb); |
| 1603 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1604 | remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1605 | } |
| 1606 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1607 | if (!skb_queue_empty(&skb_head)) { |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1608 | spin_lock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1609 | skb_queue_splice(&skb_head, tx_queue); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1610 | spin_unlock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1611 | } |
| 1612 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1613 | netif_tx_lock(tp->netdev); |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1614 | |
| 1615 | if (netif_queue_stopped(tp->netdev) && |
| 1616 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) |
| 1617 | netif_wake_queue(tp->netdev); |
| 1618 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1619 | netif_tx_unlock(tp->netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1620 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1621 | ret = usb_autopm_get_interface_async(tp->intf); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1622 | if (ret < 0) |
| 1623 | goto out_tx_fill; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1624 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1625 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2), |
| 1626 | agg->head, (int)(tx_data - (u8 *)agg->head), |
| 1627 | (usb_complete_t)write_bulk_callback, agg); |
| 1628 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1629 | ret = usb_submit_urb(agg->urb, GFP_ATOMIC); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1630 | if (ret < 0) |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1631 | usb_autopm_put_interface_async(tp->intf); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1632 | |
| 1633 | out_tx_fill: |
| 1634 | return ret; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1635 | } |
| 1636 | |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1637 | static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc) |
| 1638 | { |
| 1639 | u8 checksum = CHECKSUM_NONE; |
| 1640 | u32 opts2, opts3; |
| 1641 | |
| 1642 | if (tp->version == RTL_VER_01) |
| 1643 | goto return_result; |
| 1644 | |
| 1645 | opts2 = le32_to_cpu(rx_desc->opts2); |
| 1646 | opts3 = le32_to_cpu(rx_desc->opts3); |
| 1647 | |
| 1648 | if (opts2 & RD_IPV4_CS) { |
| 1649 | if (opts3 & IPF) |
| 1650 | checksum = CHECKSUM_NONE; |
| 1651 | else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF)) |
| 1652 | checksum = CHECKSUM_NONE; |
| 1653 | else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF)) |
| 1654 | checksum = CHECKSUM_NONE; |
| 1655 | else |
| 1656 | checksum = CHECKSUM_UNNECESSARY; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1657 | } else if (RD_IPV6_CS) { |
| 1658 | if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) |
| 1659 | checksum = CHECKSUM_UNNECESSARY; |
| 1660 | else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) |
| 1661 | checksum = CHECKSUM_UNNECESSARY; |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | return_result: |
| 1665 | return checksum; |
| 1666 | } |
| 1667 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1668 | static int rx_bottom(struct r8152 *tp, int budget) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1669 | { |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1670 | unsigned long flags; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1671 | struct list_head *cursor, *next, rx_queue; |
hayeswang | e1a2ca9 | 2015-02-06 11:30:45 +0800 | [diff] [blame] | 1672 | int ret = 0, work_done = 0; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1673 | |
| 1674 | if (!skb_queue_empty(&tp->rx_queue)) { |
| 1675 | while (work_done < budget) { |
| 1676 | struct sk_buff *skb = __skb_dequeue(&tp->rx_queue); |
| 1677 | struct net_device *netdev = tp->netdev; |
| 1678 | struct net_device_stats *stats = &netdev->stats; |
| 1679 | unsigned int pkt_len; |
| 1680 | |
| 1681 | if (!skb) |
| 1682 | break; |
| 1683 | |
| 1684 | pkt_len = skb->len; |
| 1685 | napi_gro_receive(&tp->napi, skb); |
| 1686 | work_done++; |
| 1687 | stats->rx_packets++; |
| 1688 | stats->rx_bytes += pkt_len; |
| 1689 | } |
| 1690 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1691 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1692 | if (list_empty(&tp->rx_done)) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1693 | goto out1; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1694 | |
| 1695 | INIT_LIST_HEAD(&rx_queue); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1696 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1697 | list_splice_init(&tp->rx_done, &rx_queue); |
| 1698 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1699 | |
| 1700 | list_for_each_safe(cursor, next, &rx_queue) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1701 | struct rx_desc *rx_desc; |
| 1702 | struct rx_agg *agg; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1703 | int len_used = 0; |
| 1704 | struct urb *urb; |
| 1705 | u8 *rx_data; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1706 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1707 | list_del_init(cursor); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1708 | |
| 1709 | agg = list_entry(cursor, struct rx_agg, list); |
| 1710 | urb = agg->urb; |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1711 | if (urb->actual_length < ETH_ZLEN) |
| 1712 | goto submit; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1713 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1714 | rx_desc = agg->head; |
| 1715 | rx_data = agg->head; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1716 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1717 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1718 | while (urb->actual_length > len_used) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1719 | struct net_device *netdev = tp->netdev; |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 1720 | struct net_device_stats *stats = &netdev->stats; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1721 | unsigned int pkt_len; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1722 | struct sk_buff *skb; |
| 1723 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1724 | pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1725 | if (pkt_len < ETH_ZLEN) |
| 1726 | break; |
| 1727 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1728 | len_used += pkt_len; |
| 1729 | if (urb->actual_length < len_used) |
| 1730 | break; |
| 1731 | |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1732 | pkt_len -= CRC_SIZE; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1733 | rx_data += sizeof(struct rx_desc); |
| 1734 | |
| 1735 | skb = netdev_alloc_skb_ip_align(netdev, pkt_len); |
| 1736 | if (!skb) { |
| 1737 | stats->rx_dropped++; |
hayeswang | 5e2f748 | 2014-03-07 11:04:37 +0800 | [diff] [blame] | 1738 | goto find_next_rx; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1739 | } |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1740 | |
| 1741 | skb->ip_summed = r8152_rx_csum(tp, rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1742 | memcpy(skb->data, rx_data, pkt_len); |
| 1743 | skb_put(skb, pkt_len); |
| 1744 | skb->protocol = eth_type_trans(skb, netdev); |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1745 | rtl_rx_vlan_tag(rx_desc, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1746 | if (work_done < budget) { |
| 1747 | napi_gro_receive(&tp->napi, skb); |
| 1748 | work_done++; |
| 1749 | stats->rx_packets++; |
| 1750 | stats->rx_bytes += pkt_len; |
| 1751 | } else { |
| 1752 | __skb_queue_tail(&tp->rx_queue, skb); |
| 1753 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1754 | |
hayeswang | 5e2f748 | 2014-03-07 11:04:37 +0800 | [diff] [blame] | 1755 | find_next_rx: |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1756 | rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1757 | rx_desc = (struct rx_desc *)rx_data; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1758 | len_used = (int)(rx_data - (u8 *)agg->head); |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1759 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1760 | } |
| 1761 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1762 | submit: |
hayeswang | e1a2ca9 | 2015-02-06 11:30:45 +0800 | [diff] [blame] | 1763 | if (!ret) { |
| 1764 | ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); |
| 1765 | } else { |
| 1766 | urb->actual_length = 0; |
| 1767 | list_add_tail(&agg->list, next); |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | if (!list_empty(&rx_queue)) { |
| 1772 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 1773 | list_splice_tail(&rx_queue, &tp->rx_done); |
| 1774 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1775 | } |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1776 | |
| 1777 | out1: |
| 1778 | return work_done; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | static void tx_bottom(struct r8152 *tp) |
| 1782 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1783 | int res; |
| 1784 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1785 | do { |
| 1786 | struct tx_agg *agg; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1787 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1788 | if (skb_queue_empty(&tp->tx_queue)) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1789 | break; |
| 1790 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1791 | agg = r8152_get_tx_agg(tp); |
| 1792 | if (!agg) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1793 | break; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1794 | |
| 1795 | res = r8152_tx_agg_fill(tp, agg); |
| 1796 | if (res) { |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 1797 | struct net_device *netdev = tp->netdev; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1798 | |
| 1799 | if (res == -ENODEV) { |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1800 | set_bit(RTL8152_UNPLUG, &tp->flags); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1801 | netif_device_detach(netdev); |
| 1802 | } else { |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 1803 | struct net_device_stats *stats = &netdev->stats; |
| 1804 | unsigned long flags; |
| 1805 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1806 | netif_warn(tp, tx_err, netdev, |
| 1807 | "failed tx_urb %d\n", res); |
| 1808 | stats->tx_dropped += agg->skb_num; |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 1809 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1810 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 1811 | list_add_tail(&agg->list, &tp->tx_free); |
| 1812 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 1813 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1814 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1815 | } while (res == 0); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1816 | } |
| 1817 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1818 | static void bottom_half(struct r8152 *tp) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1819 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1820 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1821 | return; |
| 1822 | |
| 1823 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1824 | return; |
| 1825 | |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 1826 | /* When link down, the driver would cancel all bulks. */ |
| 1827 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1828 | if (!netif_carrier_ok(tp->netdev)) |
| 1829 | return; |
| 1830 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1831 | clear_bit(SCHEDULE_NAPI, &tp->flags); |
hayeswang | 9451a11 | 2014-11-12 10:05:04 +0800 | [diff] [blame] | 1832 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1833 | tx_bottom(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1834 | } |
| 1835 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1836 | static int r8152_poll(struct napi_struct *napi, int budget) |
| 1837 | { |
| 1838 | struct r8152 *tp = container_of(napi, struct r8152, napi); |
| 1839 | int work_done; |
| 1840 | |
| 1841 | work_done = rx_bottom(tp, budget); |
| 1842 | bottom_half(tp); |
| 1843 | |
| 1844 | if (work_done < budget) { |
| 1845 | napi_complete(napi); |
| 1846 | if (!list_empty(&tp->rx_done)) |
| 1847 | napi_schedule(napi); |
| 1848 | } |
| 1849 | |
| 1850 | return work_done; |
| 1851 | } |
| 1852 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1853 | static |
| 1854 | int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) |
| 1855 | { |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 1856 | int ret; |
| 1857 | |
hayeswang | ef827a5 | 2015-01-09 10:26:36 +0800 | [diff] [blame] | 1858 | /* The rx would be stopped, so skip submitting */ |
| 1859 | if (test_bit(RTL8152_UNPLUG, &tp->flags) || |
| 1860 | !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev)) |
| 1861 | return 0; |
| 1862 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1863 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1), |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1864 | agg->head, agg_buf_sz, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1865 | (usb_complete_t)read_bulk_callback, agg); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1866 | |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 1867 | ret = usb_submit_urb(agg->urb, mem_flags); |
| 1868 | if (ret == -ENODEV) { |
| 1869 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 1870 | netif_device_detach(tp->netdev); |
| 1871 | } else if (ret) { |
| 1872 | struct urb *urb = agg->urb; |
| 1873 | unsigned long flags; |
| 1874 | |
| 1875 | urb->actual_length = 0; |
| 1876 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 1877 | list_add_tail(&agg->list, &tp->rx_done); |
| 1878 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1879 | |
| 1880 | netif_err(tp, rx_err, tp->netdev, |
| 1881 | "Couldn't submit rx[%p], ret = %d\n", agg, ret); |
| 1882 | |
| 1883 | napi_schedule(&tp->napi); |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1889 | static void rtl_drop_queued_tx(struct r8152 *tp) |
| 1890 | { |
| 1891 | struct net_device_stats *stats = &tp->netdev->stats; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1892 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1893 | struct sk_buff *skb; |
| 1894 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1895 | if (skb_queue_empty(tx_queue)) |
| 1896 | return; |
| 1897 | |
| 1898 | __skb_queue_head_init(&skb_head); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 1899 | spin_lock_bh(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1900 | skb_queue_splice_init(tx_queue, &skb_head); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 1901 | spin_unlock_bh(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1902 | |
| 1903 | while ((skb = __skb_dequeue(&skb_head))) { |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1904 | dev_kfree_skb(skb); |
| 1905 | stats->tx_dropped++; |
| 1906 | } |
| 1907 | } |
| 1908 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1909 | static void rtl8152_tx_timeout(struct net_device *netdev) |
| 1910 | { |
| 1911 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1912 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1913 | netif_warn(tp, tx_err, netdev, "Tx timeout\n"); |
hayeswang | 37608f3 | 2015-07-29 20:39:09 +0800 | [diff] [blame] | 1914 | |
| 1915 | usb_queue_reset_device(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | static void rtl8152_set_rx_mode(struct net_device *netdev) |
| 1919 | { |
| 1920 | struct r8152 *tp = netdev_priv(netdev); |
| 1921 | |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 1922 | if (netif_carrier_ok(netdev)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1923 | set_bit(RTL8152_SET_RX_MODE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1924 | schedule_delayed_work(&tp->schedule, 0); |
| 1925 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | static void _rtl8152_set_rx_mode(struct net_device *netdev) |
| 1929 | { |
| 1930 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1931 | u32 mc_filter[2]; /* Multicast hash filter */ |
| 1932 | __le32 tmp[2]; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1933 | u32 ocp_data; |
| 1934 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1935 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 1936 | netif_stop_queue(netdev); |
| 1937 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 1938 | ocp_data &= ~RCR_ACPT_ALL; |
| 1939 | ocp_data |= RCR_AB | RCR_APM; |
| 1940 | |
| 1941 | if (netdev->flags & IFF_PROMISC) { |
| 1942 | /* Unconditionally log net taps. */ |
| 1943 | netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); |
| 1944 | ocp_data |= RCR_AM | RCR_AAP; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1945 | mc_filter[1] = 0xffffffff; |
| 1946 | mc_filter[0] = 0xffffffff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1947 | } else if ((netdev_mc_count(netdev) > multicast_filter_limit) || |
| 1948 | (netdev->flags & IFF_ALLMULTI)) { |
| 1949 | /* Too many to filter perfectly -- accept all multicasts. */ |
| 1950 | ocp_data |= RCR_AM; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1951 | mc_filter[1] = 0xffffffff; |
| 1952 | mc_filter[0] = 0xffffffff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1953 | } else { |
| 1954 | struct netdev_hw_addr *ha; |
| 1955 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1956 | mc_filter[1] = 0; |
| 1957 | mc_filter[0] = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1958 | netdev_for_each_mc_addr(ha, netdev) { |
| 1959 | int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1960 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1961 | mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); |
| 1962 | ocp_data |= RCR_AM; |
| 1963 | } |
| 1964 | } |
| 1965 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1966 | tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); |
| 1967 | tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1968 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1969 | pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1970 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 1971 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 1974 | static netdev_features_t |
| 1975 | rtl8152_features_check(struct sk_buff *skb, struct net_device *dev, |
| 1976 | netdev_features_t features) |
| 1977 | { |
| 1978 | u32 mss = skb_shinfo(skb)->gso_size; |
| 1979 | int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX; |
| 1980 | int offset = skb_transport_offset(skb); |
| 1981 | |
| 1982 | if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset) |
| 1983 | features &= ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK); |
| 1984 | else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz) |
| 1985 | features &= ~NETIF_F_GSO_MASK; |
| 1986 | |
| 1987 | return features; |
| 1988 | } |
| 1989 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1990 | static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1991 | struct net_device *netdev) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1992 | { |
| 1993 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1994 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1995 | skb_tx_timestamp(skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1996 | |
hayeswang | 6159878 | 2013-11-20 17:30:55 +0800 | [diff] [blame] | 1997 | skb_queue_tail(&tp->tx_queue, skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1998 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1999 | if (!list_empty(&tp->tx_free)) { |
| 2000 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2001 | set_bit(SCHEDULE_NAPI, &tp->flags); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2002 | schedule_delayed_work(&tp->schedule, 0); |
| 2003 | } else { |
| 2004 | usb_mark_last_busy(tp->udev); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2005 | napi_schedule(&tp->napi); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2006 | } |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2007 | } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) { |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2008 | netif_stop_queue(netdev); |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2009 | } |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2010 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2011 | return NETDEV_TX_OK; |
| 2012 | } |
| 2013 | |
| 2014 | static void r8152b_reset_packet_filter(struct r8152 *tp) |
| 2015 | { |
| 2016 | u32 ocp_data; |
| 2017 | |
| 2018 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); |
| 2019 | ocp_data &= ~FMC_FCR_MCU_EN; |
| 2020 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 2021 | ocp_data |= FMC_FCR_MCU_EN; |
| 2022 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 2023 | } |
| 2024 | |
| 2025 | static void rtl8152_nic_reset(struct r8152 *tp) |
| 2026 | { |
| 2027 | int i; |
| 2028 | |
| 2029 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); |
| 2030 | |
| 2031 | for (i = 0; i < 1000; i++) { |
| 2032 | if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) |
| 2033 | break; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2034 | usleep_range(100, 400); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2038 | static void set_tx_qlen(struct r8152 *tp) |
| 2039 | { |
| 2040 | struct net_device *netdev = tp->netdev; |
| 2041 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 2042 | tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN + |
| 2043 | sizeof(struct tx_desc)); |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2044 | } |
| 2045 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2046 | static inline u8 rtl8152_get_speed(struct r8152 *tp) |
| 2047 | { |
| 2048 | return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); |
| 2049 | } |
| 2050 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2051 | static void rtl_set_eee_plus(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2052 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2053 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2054 | u8 speed; |
| 2055 | |
| 2056 | speed = rtl8152_get_speed(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2057 | if (speed & _10bps) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2058 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2059 | ocp_data |= EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2060 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 2061 | } else { |
| 2062 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2063 | ocp_data &= ~EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2064 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 2065 | } |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2066 | } |
| 2067 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2068 | static void rxdy_gated_en(struct r8152 *tp, bool enable) |
| 2069 | { |
| 2070 | u32 ocp_data; |
| 2071 | |
| 2072 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 2073 | if (enable) |
| 2074 | ocp_data |= RXDY_GATED_EN; |
| 2075 | else |
| 2076 | ocp_data &= ~RXDY_GATED_EN; |
| 2077 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 2078 | } |
| 2079 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2080 | static int rtl_start_rx(struct r8152 *tp) |
| 2081 | { |
| 2082 | int i, ret = 0; |
| 2083 | |
| 2084 | INIT_LIST_HEAD(&tp->rx_done); |
| 2085 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
| 2086 | INIT_LIST_HEAD(&tp->rx_info[i].list); |
| 2087 | ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL); |
| 2088 | if (ret) |
| 2089 | break; |
| 2090 | } |
| 2091 | |
hayeswang | 7bcf4f6 | 2014-11-20 10:29:06 +0800 | [diff] [blame] | 2092 | if (ret && ++i < RTL8152_MAX_RX) { |
| 2093 | struct list_head rx_queue; |
| 2094 | unsigned long flags; |
| 2095 | |
| 2096 | INIT_LIST_HEAD(&rx_queue); |
| 2097 | |
| 2098 | do { |
| 2099 | struct rx_agg *agg = &tp->rx_info[i++]; |
| 2100 | struct urb *urb = agg->urb; |
| 2101 | |
| 2102 | urb->actual_length = 0; |
| 2103 | list_add_tail(&agg->list, &rx_queue); |
| 2104 | } while (i < RTL8152_MAX_RX); |
| 2105 | |
| 2106 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2107 | list_splice_tail(&rx_queue, &tp->rx_done); |
| 2108 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2109 | } |
| 2110 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2111 | return ret; |
| 2112 | } |
| 2113 | |
| 2114 | static int rtl_stop_rx(struct r8152 *tp) |
| 2115 | { |
| 2116 | int i; |
| 2117 | |
| 2118 | for (i = 0; i < RTL8152_MAX_RX; i++) |
| 2119 | usb_kill_urb(tp->rx_info[i].urb); |
| 2120 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2121 | while (!skb_queue_empty(&tp->rx_queue)) |
| 2122 | dev_kfree_skb(__skb_dequeue(&tp->rx_queue)); |
| 2123 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2124 | return 0; |
| 2125 | } |
| 2126 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2127 | static int rtl_enable(struct r8152 *tp) |
| 2128 | { |
| 2129 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2130 | |
| 2131 | r8152b_reset_packet_filter(tp); |
| 2132 | |
| 2133 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); |
| 2134 | ocp_data |= CR_RE | CR_TE; |
| 2135 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); |
| 2136 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2137 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2138 | |
hayeswang | aa2e092 | 2015-01-09 10:26:35 +0800 | [diff] [blame] | 2139 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2142 | static int rtl8152_enable(struct r8152 *tp) |
| 2143 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2144 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2145 | return -ENODEV; |
| 2146 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2147 | set_tx_qlen(tp); |
| 2148 | rtl_set_eee_plus(tp); |
| 2149 | |
| 2150 | return rtl_enable(tp); |
| 2151 | } |
| 2152 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2153 | static void r8153_set_rx_early_timeout(struct r8152 *tp) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2154 | { |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2155 | u32 ocp_data = tp->coalesce / 8; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2156 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2157 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, ocp_data); |
| 2158 | } |
| 2159 | |
| 2160 | static void r8153_set_rx_early_size(struct r8152 *tp) |
| 2161 | { |
| 2162 | u32 mtu = tp->netdev->mtu; |
| 2163 | u32 ocp_data = (agg_buf_sz - mtu - VLAN_ETH_HLEN - VLAN_HLEN) / 4; |
| 2164 | |
| 2165 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | static int rtl8153_enable(struct r8152 *tp) |
| 2169 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2170 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2171 | return -ENODEV; |
| 2172 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2173 | usb_disable_lpm(tp->udev); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2174 | set_tx_qlen(tp); |
| 2175 | rtl_set_eee_plus(tp); |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2176 | r8153_set_rx_early_timeout(tp); |
| 2177 | r8153_set_rx_early_size(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2178 | |
| 2179 | return rtl_enable(tp); |
| 2180 | } |
| 2181 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2182 | static void rtl_disable(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2183 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2184 | u32 ocp_data; |
| 2185 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2186 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2187 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 2188 | rtl_drop_queued_tx(tp); |
| 2189 | return; |
| 2190 | } |
| 2191 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2192 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2193 | ocp_data &= ~RCR_ACPT_ALL; |
| 2194 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2195 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2196 | rtl_drop_queued_tx(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2197 | |
| 2198 | for (i = 0; i < RTL8152_MAX_TX; i++) |
| 2199 | usb_kill_urb(tp->tx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2200 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2201 | rxdy_gated_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2202 | |
| 2203 | for (i = 0; i < 1000; i++) { |
| 2204 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2205 | if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) |
| 2206 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2207 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | for (i = 0; i < 1000; i++) { |
| 2211 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) |
| 2212 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2213 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2216 | rtl_stop_rx(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2217 | |
| 2218 | rtl8152_nic_reset(tp); |
| 2219 | } |
| 2220 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2221 | static void r8152_power_cut_en(struct r8152 *tp, bool enable) |
| 2222 | { |
| 2223 | u32 ocp_data; |
| 2224 | |
| 2225 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 2226 | if (enable) |
| 2227 | ocp_data |= POWER_CUT; |
| 2228 | else |
| 2229 | ocp_data &= ~POWER_CUT; |
| 2230 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 2231 | |
| 2232 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); |
| 2233 | ocp_data &= ~RESUME_INDICATE; |
| 2234 | ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2235 | } |
| 2236 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2237 | static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) |
| 2238 | { |
| 2239 | u32 ocp_data; |
| 2240 | |
| 2241 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 2242 | if (enable) |
| 2243 | ocp_data |= CPCR_RX_VLAN; |
| 2244 | else |
| 2245 | ocp_data &= ~CPCR_RX_VLAN; |
| 2246 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 2247 | } |
| 2248 | |
| 2249 | static int rtl8152_set_features(struct net_device *dev, |
| 2250 | netdev_features_t features) |
| 2251 | { |
| 2252 | netdev_features_t changed = features ^ dev->features; |
| 2253 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2254 | int ret; |
| 2255 | |
| 2256 | ret = usb_autopm_get_interface(tp->intf); |
| 2257 | if (ret < 0) |
| 2258 | goto out; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2259 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2260 | mutex_lock(&tp->control); |
| 2261 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2262 | if (changed & NETIF_F_HW_VLAN_CTAG_RX) { |
| 2263 | if (features & NETIF_F_HW_VLAN_CTAG_RX) |
| 2264 | rtl_rx_vlan_en(tp, true); |
| 2265 | else |
| 2266 | rtl_rx_vlan_en(tp, false); |
| 2267 | } |
| 2268 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2269 | mutex_unlock(&tp->control); |
| 2270 | |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2271 | usb_autopm_put_interface(tp->intf); |
| 2272 | |
| 2273 | out: |
| 2274 | return ret; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2275 | } |
| 2276 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2277 | #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) |
| 2278 | |
| 2279 | static u32 __rtl_get_wol(struct r8152 *tp) |
| 2280 | { |
| 2281 | u32 ocp_data; |
| 2282 | u32 wolopts = 0; |
| 2283 | |
| 2284 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
| 2285 | if (!(ocp_data & LAN_WAKE_EN)) |
| 2286 | return 0; |
| 2287 | |
| 2288 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2289 | if (ocp_data & LINK_ON_WAKE_EN) |
| 2290 | wolopts |= WAKE_PHY; |
| 2291 | |
| 2292 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
| 2293 | if (ocp_data & UWF_EN) |
| 2294 | wolopts |= WAKE_UCAST; |
| 2295 | if (ocp_data & BWF_EN) |
| 2296 | wolopts |= WAKE_BCAST; |
| 2297 | if (ocp_data & MWF_EN) |
| 2298 | wolopts |= WAKE_MCAST; |
| 2299 | |
| 2300 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 2301 | if (ocp_data & MAGIC_EN) |
| 2302 | wolopts |= WAKE_MAGIC; |
| 2303 | |
| 2304 | return wolopts; |
| 2305 | } |
| 2306 | |
| 2307 | static void __rtl_set_wol(struct r8152 *tp, u32 wolopts) |
| 2308 | { |
| 2309 | u32 ocp_data; |
| 2310 | |
| 2311 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 2312 | |
| 2313 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2314 | ocp_data &= ~LINK_ON_WAKE_EN; |
| 2315 | if (wolopts & WAKE_PHY) |
| 2316 | ocp_data |= LINK_ON_WAKE_EN; |
| 2317 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 2318 | |
| 2319 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
| 2320 | ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN); |
| 2321 | if (wolopts & WAKE_UCAST) |
| 2322 | ocp_data |= UWF_EN; |
| 2323 | if (wolopts & WAKE_BCAST) |
| 2324 | ocp_data |= BWF_EN; |
| 2325 | if (wolopts & WAKE_MCAST) |
| 2326 | ocp_data |= MWF_EN; |
| 2327 | if (wolopts & WAKE_ANY) |
| 2328 | ocp_data |= LAN_WAKE_EN; |
| 2329 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); |
| 2330 | |
| 2331 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 2332 | |
| 2333 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 2334 | ocp_data &= ~MAGIC_EN; |
| 2335 | if (wolopts & WAKE_MAGIC) |
| 2336 | ocp_data |= MAGIC_EN; |
| 2337 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); |
| 2338 | |
| 2339 | if (wolopts & WAKE_ANY) |
| 2340 | device_set_wakeup_enable(&tp->udev->dev, true); |
| 2341 | else |
| 2342 | device_set_wakeup_enable(&tp->udev->dev, false); |
| 2343 | } |
| 2344 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2345 | static void r8153_u1u2en(struct r8152 *tp, bool enable) |
| 2346 | { |
| 2347 | u8 u1u2[8]; |
| 2348 | |
| 2349 | if (enable) |
| 2350 | memset(u1u2, 0xff, sizeof(u1u2)); |
| 2351 | else |
| 2352 | memset(u1u2, 0x00, sizeof(u1u2)); |
| 2353 | |
| 2354 | usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); |
| 2355 | } |
| 2356 | |
| 2357 | static void r8153_u2p3en(struct r8152 *tp, bool enable) |
| 2358 | { |
| 2359 | u32 ocp_data; |
| 2360 | |
| 2361 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); |
| 2362 | if (enable && tp->version != RTL_VER_03 && tp->version != RTL_VER_04) |
| 2363 | ocp_data |= U2P3_ENABLE; |
| 2364 | else |
| 2365 | ocp_data &= ~U2P3_ENABLE; |
| 2366 | ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); |
| 2367 | } |
| 2368 | |
| 2369 | static void r8153_power_cut_en(struct r8152 *tp, bool enable) |
| 2370 | { |
| 2371 | u32 ocp_data; |
| 2372 | |
| 2373 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 2374 | if (enable) |
| 2375 | ocp_data |= PWR_EN | PHASE2_EN; |
| 2376 | else |
| 2377 | ocp_data &= ~(PWR_EN | PHASE2_EN); |
| 2378 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2379 | |
| 2380 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 2381 | ocp_data &= ~PCUT_STATUS; |
| 2382 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 2383 | } |
| 2384 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 2385 | static bool rtl_can_wakeup(struct r8152 *tp) |
| 2386 | { |
| 2387 | struct usb_device *udev = tp->udev; |
| 2388 | |
| 2389 | return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP); |
| 2390 | } |
| 2391 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2392 | static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable) |
| 2393 | { |
| 2394 | if (enable) { |
| 2395 | u32 ocp_data; |
| 2396 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2397 | r8153_u1u2en(tp, false); |
| 2398 | r8153_u2p3en(tp, false); |
| 2399 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2400 | __rtl_set_wol(tp, WAKE_ANY); |
| 2401 | |
| 2402 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 2403 | |
| 2404 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2405 | ocp_data |= LINK_OFF_WAKE_EN; |
| 2406 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 2407 | |
| 2408 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 2409 | } else { |
| 2410 | __rtl_set_wol(tp, tp->saved_wolopts); |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2411 | r8153_u2p3en(tp, true); |
| 2412 | r8153_u1u2en(tp, true); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 2416 | static void rtl_phy_reset(struct r8152 *tp) |
| 2417 | { |
| 2418 | u16 data; |
| 2419 | int i; |
| 2420 | |
| 2421 | clear_bit(PHY_RESET, &tp->flags); |
| 2422 | |
| 2423 | data = r8152_mdio_read(tp, MII_BMCR); |
| 2424 | |
| 2425 | /* don't reset again before the previous one complete */ |
| 2426 | if (data & BMCR_RESET) |
| 2427 | return; |
| 2428 | |
| 2429 | data |= BMCR_RESET; |
| 2430 | r8152_mdio_write(tp, MII_BMCR, data); |
| 2431 | |
| 2432 | for (i = 0; i < 50; i++) { |
| 2433 | msleep(20); |
| 2434 | if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) |
| 2435 | break; |
| 2436 | } |
| 2437 | } |
| 2438 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 2439 | static void r8153_teredo_off(struct r8152 *tp) |
| 2440 | { |
| 2441 | u32 ocp_data; |
| 2442 | |
| 2443 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 2444 | ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN); |
| 2445 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 2446 | |
| 2447 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); |
| 2448 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); |
| 2449 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); |
| 2450 | } |
| 2451 | |
| 2452 | static void r8152b_disable_aldps(struct r8152 *tp) |
| 2453 | { |
| 2454 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE); |
| 2455 | msleep(20); |
| 2456 | } |
| 2457 | |
| 2458 | static inline void r8152b_enable_aldps(struct r8152 *tp) |
| 2459 | { |
| 2460 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | |
| 2461 | LINKENA | DIS_SDSAVE); |
| 2462 | } |
| 2463 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2464 | static void rtl8152_disable(struct r8152 *tp) |
| 2465 | { |
| 2466 | r8152b_disable_aldps(tp); |
| 2467 | rtl_disable(tp); |
| 2468 | r8152b_enable_aldps(tp); |
| 2469 | } |
| 2470 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 2471 | static void r8152b_hw_phy_cfg(struct r8152 *tp) |
| 2472 | { |
hayeswang | f0cbe0a | 2014-02-18 21:49:03 +0800 | [diff] [blame] | 2473 | u16 data; |
| 2474 | |
| 2475 | data = r8152_mdio_read(tp, MII_BMCR); |
| 2476 | if (data & BMCR_PDOWN) { |
| 2477 | data &= ~BMCR_PDOWN; |
| 2478 | r8152_mdio_write(tp, MII_BMCR, data); |
| 2479 | } |
| 2480 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 2481 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 2482 | } |
| 2483 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2484 | static void r8152b_exit_oob(struct r8152 *tp) |
| 2485 | { |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 2486 | u32 ocp_data; |
| 2487 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2488 | |
| 2489 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2490 | ocp_data &= ~RCR_ACPT_ALL; |
| 2491 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2492 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2493 | rxdy_gated_en(tp, true); |
hayeswang | da9bd11 | 2014-02-18 21:49:08 +0800 | [diff] [blame] | 2494 | r8153_teredo_off(tp); |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 2495 | r8152b_hw_phy_cfg(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2496 | |
| 2497 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 2498 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); |
| 2499 | |
| 2500 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2501 | ocp_data &= ~NOW_IS_OOB; |
| 2502 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 2503 | |
| 2504 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 2505 | ocp_data &= ~MCU_BORW_EN; |
| 2506 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 2507 | |
| 2508 | for (i = 0; i < 1000; i++) { |
| 2509 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2510 | if (ocp_data & LINK_LIST_READY) |
| 2511 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2512 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 2516 | ocp_data |= RE_INIT_LL; |
| 2517 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 2518 | |
| 2519 | for (i = 0; i < 1000; i++) { |
| 2520 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2521 | if (ocp_data & LINK_LIST_READY) |
| 2522 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2523 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | rtl8152_nic_reset(tp); |
| 2527 | |
| 2528 | /* rx share fifo credit full threshold */ |
| 2529 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 2530 | |
hayeswang | a3cc465 | 2014-07-24 16:37:43 +0800 | [diff] [blame] | 2531 | if (tp->udev->speed == USB_SPEED_FULL || |
| 2532 | tp->udev->speed == USB_SPEED_LOW) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2533 | /* rx share fifo credit near full threshold */ |
| 2534 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 2535 | RXFIFO_THR2_FULL); |
| 2536 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 2537 | RXFIFO_THR3_FULL); |
| 2538 | } else { |
| 2539 | /* rx share fifo credit near full threshold */ |
| 2540 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 2541 | RXFIFO_THR2_HIGH); |
| 2542 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 2543 | RXFIFO_THR3_HIGH); |
| 2544 | } |
| 2545 | |
| 2546 | /* TX share fifo free credit full threshold */ |
| 2547 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); |
| 2548 | |
| 2549 | 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] | 2550 | 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] | 2551 | ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, |
| 2552 | TEST_MODE_DISABLE | TX_SIZE_ADJUST1); |
| 2553 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2554 | 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] | 2555 | |
| 2556 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 2557 | |
| 2558 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 2559 | ocp_data |= TCR0_AUTO_FIFO; |
| 2560 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 2561 | } |
| 2562 | |
| 2563 | static void r8152b_enter_oob(struct r8152 *tp) |
| 2564 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 2565 | u32 ocp_data; |
| 2566 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2567 | |
| 2568 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2569 | ocp_data &= ~NOW_IS_OOB; |
| 2570 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 2571 | |
| 2572 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); |
| 2573 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); |
| 2574 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); |
| 2575 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2576 | rtl_disable(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2577 | |
| 2578 | for (i = 0; i < 1000; i++) { |
| 2579 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2580 | if (ocp_data & LINK_LIST_READY) |
| 2581 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2582 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 2586 | ocp_data |= RE_INIT_LL; |
| 2587 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 2588 | |
| 2589 | for (i = 0; i < 1000; i++) { |
| 2590 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2591 | if (ocp_data & LINK_LIST_READY) |
| 2592 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2593 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 2597 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2598 | rtl_rx_vlan_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2599 | |
| 2600 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR); |
| 2601 | ocp_data |= ALDPS_PROXY_MODE; |
| 2602 | ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data); |
| 2603 | |
| 2604 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2605 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 2606 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 2607 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2608 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2609 | |
| 2610 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2611 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 2612 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2613 | } |
| 2614 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2615 | static void r8153_hw_phy_cfg(struct r8152 *tp) |
| 2616 | { |
| 2617 | u32 ocp_data; |
| 2618 | u16 data; |
| 2619 | |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 2620 | if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || |
| 2621 | tp->version == RTL_VER_05) |
| 2622 | ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); |
| 2623 | |
hayeswang | f0cbe0a | 2014-02-18 21:49:03 +0800 | [diff] [blame] | 2624 | data = r8152_mdio_read(tp, MII_BMCR); |
| 2625 | if (data & BMCR_PDOWN) { |
| 2626 | data &= ~BMCR_PDOWN; |
| 2627 | r8152_mdio_write(tp, MII_BMCR, data); |
| 2628 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2629 | |
| 2630 | if (tp->version == RTL_VER_03) { |
| 2631 | data = ocp_reg_read(tp, OCP_EEE_CFG); |
| 2632 | data &= ~CTAP_SHORT_EN; |
| 2633 | ocp_reg_write(tp, OCP_EEE_CFG, data); |
| 2634 | } |
| 2635 | |
| 2636 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 2637 | data |= EEE_CLKDIV_EN; |
| 2638 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 2639 | |
| 2640 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 2641 | data |= EN_10M_BGOFF; |
| 2642 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
| 2643 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 2644 | data |= EN_10M_PLLOFF; |
| 2645 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 2646 | sram_write(tp, SRAM_IMPEDANCE, 0x0b13); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2647 | |
| 2648 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 2649 | ocp_data |= PFM_PWM_SWITCH; |
| 2650 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 2651 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 2652 | /* Enable LPF corner auto tune */ |
| 2653 | sram_write(tp, SRAM_LPF_CFG, 0xf70f); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2654 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 2655 | /* Adjust 10M Amplitude */ |
| 2656 | sram_write(tp, SRAM_10M_AMP1, 0x00af); |
| 2657 | sram_write(tp, SRAM_10M_AMP2, 0x0208); |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 2658 | |
| 2659 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2660 | } |
| 2661 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2662 | static void r8153_first_init(struct r8152 *tp) |
| 2663 | { |
| 2664 | u32 ocp_data; |
| 2665 | int i; |
| 2666 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2667 | rxdy_gated_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2668 | r8153_teredo_off(tp); |
| 2669 | |
| 2670 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2671 | ocp_data &= ~RCR_ACPT_ALL; |
| 2672 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2673 | |
| 2674 | r8153_hw_phy_cfg(tp); |
| 2675 | |
| 2676 | rtl8152_nic_reset(tp); |
| 2677 | |
| 2678 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2679 | ocp_data &= ~NOW_IS_OOB; |
| 2680 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 2681 | |
| 2682 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 2683 | ocp_data &= ~MCU_BORW_EN; |
| 2684 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 2685 | |
| 2686 | for (i = 0; i < 1000; i++) { |
| 2687 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2688 | if (ocp_data & LINK_LIST_READY) |
| 2689 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2690 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 2694 | ocp_data |= RE_INIT_LL; |
| 2695 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 2696 | |
| 2697 | for (i = 0; i < 1000; i++) { |
| 2698 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2699 | if (ocp_data & LINK_LIST_READY) |
| 2700 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2701 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2702 | } |
| 2703 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2704 | 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] | 2705 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 2706 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS); |
| 2707 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2708 | |
| 2709 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 2710 | ocp_data |= TCR0_AUTO_FIFO; |
| 2711 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 2712 | |
| 2713 | rtl8152_nic_reset(tp); |
| 2714 | |
| 2715 | /* rx share fifo credit full threshold */ |
| 2716 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 2717 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); |
| 2718 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); |
| 2719 | /* TX share fifo free credit full threshold */ |
| 2720 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); |
| 2721 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 2722 | /* rx aggregation */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2723 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 2724 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2725 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 2726 | } |
| 2727 | |
| 2728 | static void r8153_enter_oob(struct r8152 *tp) |
| 2729 | { |
| 2730 | u32 ocp_data; |
| 2731 | int i; |
| 2732 | |
| 2733 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2734 | ocp_data &= ~NOW_IS_OOB; |
| 2735 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 2736 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2737 | rtl_disable(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2738 | |
| 2739 | for (i = 0; i < 1000; i++) { |
| 2740 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2741 | if (ocp_data & LINK_LIST_READY) |
| 2742 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2743 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 2747 | ocp_data |= RE_INIT_LL; |
| 2748 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 2749 | |
| 2750 | for (i = 0; i < 1000; i++) { |
| 2751 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2752 | if (ocp_data & LINK_LIST_READY) |
| 2753 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2754 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2755 | } |
| 2756 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 2757 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2758 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2759 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 2760 | ocp_data &= ~TEREDO_WAKE_MASK; |
| 2761 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 2762 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2763 | rtl_rx_vlan_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2764 | |
| 2765 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR); |
| 2766 | ocp_data |= ALDPS_PROXY_MODE; |
| 2767 | ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data); |
| 2768 | |
| 2769 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2770 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 2771 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 2772 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2773 | rxdy_gated_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2774 | |
| 2775 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2776 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 2777 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2778 | } |
| 2779 | |
| 2780 | static void r8153_disable_aldps(struct r8152 *tp) |
| 2781 | { |
| 2782 | u16 data; |
| 2783 | |
| 2784 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 2785 | data &= ~EN_ALDPS; |
| 2786 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 2787 | msleep(20); |
| 2788 | } |
| 2789 | |
| 2790 | static void r8153_enable_aldps(struct r8152 *tp) |
| 2791 | { |
| 2792 | u16 data; |
| 2793 | |
| 2794 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 2795 | data |= EN_ALDPS; |
| 2796 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 2797 | } |
| 2798 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2799 | static void rtl8153_disable(struct r8152 *tp) |
| 2800 | { |
| 2801 | r8153_disable_aldps(tp); |
| 2802 | rtl_disable(tp); |
| 2803 | r8153_enable_aldps(tp); |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2804 | usb_enable_lpm(tp->udev); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2805 | } |
| 2806 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2807 | static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex) |
| 2808 | { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2809 | u16 bmcr, anar, gbcr; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2810 | int ret = 0; |
| 2811 | |
| 2812 | cancel_delayed_work_sync(&tp->schedule); |
| 2813 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 2814 | anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL | |
| 2815 | ADVERTISE_100HALF | ADVERTISE_100FULL); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2816 | if (tp->mii.supports_gmii) { |
| 2817 | gbcr = r8152_mdio_read(tp, MII_CTRL1000); |
| 2818 | gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
| 2819 | } else { |
| 2820 | gbcr = 0; |
| 2821 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2822 | |
| 2823 | if (autoneg == AUTONEG_DISABLE) { |
| 2824 | if (speed == SPEED_10) { |
| 2825 | bmcr = 0; |
| 2826 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 2827 | } else if (speed == SPEED_100) { |
| 2828 | bmcr = BMCR_SPEED100; |
| 2829 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2830 | } else if (speed == SPEED_1000 && tp->mii.supports_gmii) { |
| 2831 | bmcr = BMCR_SPEED1000; |
| 2832 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2833 | } else { |
| 2834 | ret = -EINVAL; |
| 2835 | goto out; |
| 2836 | } |
| 2837 | |
| 2838 | if (duplex == DUPLEX_FULL) |
| 2839 | bmcr |= BMCR_FULLDPLX; |
| 2840 | } else { |
| 2841 | if (speed == SPEED_10) { |
| 2842 | if (duplex == DUPLEX_FULL) |
| 2843 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 2844 | else |
| 2845 | anar |= ADVERTISE_10HALF; |
| 2846 | } else if (speed == SPEED_100) { |
| 2847 | if (duplex == DUPLEX_FULL) { |
| 2848 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 2849 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 2850 | } else { |
| 2851 | anar |= ADVERTISE_10HALF; |
| 2852 | anar |= ADVERTISE_100HALF; |
| 2853 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2854 | } else if (speed == SPEED_1000 && tp->mii.supports_gmii) { |
| 2855 | if (duplex == DUPLEX_FULL) { |
| 2856 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 2857 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 2858 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
| 2859 | } else { |
| 2860 | anar |= ADVERTISE_10HALF; |
| 2861 | anar |= ADVERTISE_100HALF; |
| 2862 | gbcr |= ADVERTISE_1000HALF; |
| 2863 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2864 | } else { |
| 2865 | ret = -EINVAL; |
| 2866 | goto out; |
| 2867 | } |
| 2868 | |
| 2869 | bmcr = BMCR_ANENABLE | BMCR_ANRESTART; |
| 2870 | } |
| 2871 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 2872 | if (test_bit(PHY_RESET, &tp->flags)) |
| 2873 | bmcr |= BMCR_RESET; |
| 2874 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2875 | if (tp->mii.supports_gmii) |
| 2876 | r8152_mdio_write(tp, MII_CTRL1000, gbcr); |
| 2877 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2878 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 2879 | r8152_mdio_write(tp, MII_BMCR, bmcr); |
| 2880 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 2881 | if (test_bit(PHY_RESET, &tp->flags)) { |
| 2882 | int i; |
| 2883 | |
| 2884 | clear_bit(PHY_RESET, &tp->flags); |
| 2885 | for (i = 0; i < 50; i++) { |
| 2886 | msleep(20); |
| 2887 | if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) |
| 2888 | break; |
| 2889 | } |
| 2890 | } |
| 2891 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2892 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2893 | |
| 2894 | return ret; |
| 2895 | } |
| 2896 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2897 | static void rtl8152_up(struct r8152 *tp) |
| 2898 | { |
| 2899 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2900 | return; |
| 2901 | |
| 2902 | r8152b_disable_aldps(tp); |
| 2903 | r8152b_exit_oob(tp); |
| 2904 | r8152b_enable_aldps(tp); |
| 2905 | } |
| 2906 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2907 | static void rtl8152_down(struct r8152 *tp) |
| 2908 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2909 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 2910 | rtl_drop_queued_tx(tp); |
| 2911 | return; |
| 2912 | } |
| 2913 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2914 | r8152_power_cut_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2915 | r8152b_disable_aldps(tp); |
| 2916 | r8152b_enter_oob(tp); |
| 2917 | r8152b_enable_aldps(tp); |
| 2918 | } |
| 2919 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2920 | static void rtl8153_up(struct r8152 *tp) |
| 2921 | { |
| 2922 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2923 | return; |
| 2924 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2925 | r8153_u1u2en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2926 | r8153_disable_aldps(tp); |
| 2927 | r8153_first_init(tp); |
| 2928 | r8153_enable_aldps(tp); |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2929 | r8153_u2p3en(tp, true); |
| 2930 | r8153_u1u2en(tp, true); |
| 2931 | usb_enable_lpm(tp->udev); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2932 | } |
| 2933 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2934 | static void rtl8153_down(struct r8152 *tp) |
| 2935 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2936 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 2937 | rtl_drop_queued_tx(tp); |
| 2938 | return; |
| 2939 | } |
| 2940 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2941 | r8153_u1u2en(tp, false); |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2942 | r8153_u2p3en(tp, false); |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2943 | r8153_power_cut_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2944 | r8153_disable_aldps(tp); |
| 2945 | r8153_enter_oob(tp); |
| 2946 | r8153_enable_aldps(tp); |
| 2947 | } |
| 2948 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2949 | static void set_carrier(struct r8152 *tp) |
| 2950 | { |
| 2951 | struct net_device *netdev = tp->netdev; |
| 2952 | u8 speed; |
| 2953 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2954 | clear_bit(RTL8152_LINK_CHG, &tp->flags); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2955 | speed = rtl8152_get_speed(tp); |
| 2956 | |
| 2957 | if (speed & LINK_STATUS) { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 2958 | if (!netif_carrier_ok(netdev)) { |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2959 | tp->rtl_ops.enable(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2960 | set_bit(RTL8152_SET_RX_MODE, &tp->flags); |
hayeswang | 41cec84 | 2015-07-24 13:54:25 +0800 | [diff] [blame] | 2961 | napi_disable(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2962 | netif_carrier_on(netdev); |
hayeswang | aa2e092 | 2015-01-09 10:26:35 +0800 | [diff] [blame] | 2963 | rtl_start_rx(tp); |
hayeswang | 41cec84 | 2015-07-24 13:54:25 +0800 | [diff] [blame] | 2964 | napi_enable(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2965 | } |
| 2966 | } else { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 2967 | if (netif_carrier_ok(netdev)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2968 | netif_carrier_off(netdev); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2969 | napi_disable(&tp->napi); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2970 | tp->rtl_ops.disable(tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2971 | napi_enable(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2972 | } |
| 2973 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | static void rtl_work_func_t(struct work_struct *work) |
| 2977 | { |
| 2978 | struct r8152 *tp = container_of(work, struct r8152, schedule.work); |
| 2979 | |
hayeswang | a1f83fe | 2014-11-12 10:05:05 +0800 | [diff] [blame] | 2980 | /* If the device is unplugged or !netif_running(), the workqueue |
| 2981 | * doesn't need to wake the device, and could return directly. |
| 2982 | */ |
| 2983 | if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev)) |
| 2984 | return; |
| 2985 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2986 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 2987 | return; |
| 2988 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2989 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 2990 | goto out1; |
| 2991 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2992 | if (!mutex_trylock(&tp->control)) { |
| 2993 | schedule_delayed_work(&tp->schedule, 0); |
| 2994 | goto out1; |
| 2995 | } |
| 2996 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2997 | if (test_bit(RTL8152_LINK_CHG, &tp->flags)) |
| 2998 | set_carrier(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2999 | |
| 3000 | if (test_bit(RTL8152_SET_RX_MODE, &tp->flags)) |
| 3001 | _rtl8152_set_rx_mode(tp->netdev); |
| 3002 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 3003 | /* don't schedule napi before linking */ |
| 3004 | if (test_bit(SCHEDULE_NAPI, &tp->flags) && |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 3005 | netif_carrier_ok(tp->netdev)) { |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 3006 | clear_bit(SCHEDULE_NAPI, &tp->flags); |
| 3007 | napi_schedule(&tp->napi); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 3008 | } |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3009 | |
| 3010 | if (test_bit(PHY_RESET, &tp->flags)) |
| 3011 | rtl_phy_reset(tp); |
| 3012 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3013 | mutex_unlock(&tp->control); |
| 3014 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3015 | out1: |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3016 | usb_autopm_put_interface(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | static int rtl8152_open(struct net_device *netdev) |
| 3020 | { |
| 3021 | struct r8152 *tp = netdev_priv(netdev); |
| 3022 | int res = 0; |
| 3023 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 3024 | res = alloc_all_mem(tp); |
| 3025 | if (res) |
| 3026 | goto out; |
| 3027 | |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 3028 | netif_carrier_off(netdev); |
hayeswang | f4c7476 | 2014-10-29 11:12:16 +0800 | [diff] [blame] | 3029 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3030 | res = usb_autopm_get_interface(tp->intf); |
| 3031 | if (res < 0) { |
| 3032 | free_all_mem(tp); |
| 3033 | goto out; |
| 3034 | } |
| 3035 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3036 | mutex_lock(&tp->control); |
| 3037 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3038 | /* The WORK_ENABLE may be set when autoresume occurs */ |
| 3039 | if (test_bit(WORK_ENABLE, &tp->flags)) { |
| 3040 | clear_bit(WORK_ENABLE, &tp->flags); |
| 3041 | usb_kill_urb(tp->intr_urb); |
| 3042 | cancel_delayed_work_sync(&tp->schedule); |
hayeswang | f4c7476 | 2014-10-29 11:12:16 +0800 | [diff] [blame] | 3043 | |
| 3044 | /* disable the tx/rx, if the workqueue has enabled them. */ |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 3045 | if (netif_carrier_ok(netdev)) |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3046 | tp->rtl_ops.disable(tp); |
| 3047 | } |
| 3048 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 3049 | tp->rtl_ops.up(tp); |
| 3050 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3051 | rtl8152_set_speed(tp, AUTONEG_ENABLE, |
| 3052 | tp->mii.supports_gmii ? SPEED_1000 : SPEED_100, |
| 3053 | DUPLEX_FULL); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 3054 | netif_carrier_off(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3055 | netif_start_queue(netdev); |
| 3056 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 3057 | |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 3058 | res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
| 3059 | if (res) { |
| 3060 | if (res == -ENODEV) |
| 3061 | netif_device_detach(tp->netdev); |
| 3062 | netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", |
| 3063 | res); |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 3064 | free_all_mem(tp); |
hayeswang | 93ffbea | 2014-11-05 10:17:02 +0800 | [diff] [blame] | 3065 | } else { |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 3066 | napi_enable(&tp->napi); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 3067 | } |
| 3068 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3069 | mutex_unlock(&tp->control); |
| 3070 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3071 | usb_autopm_put_interface(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3072 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 3073 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3074 | return res; |
| 3075 | } |
| 3076 | |
| 3077 | static int rtl8152_close(struct net_device *netdev) |
| 3078 | { |
| 3079 | struct r8152 *tp = netdev_priv(netdev); |
| 3080 | int res = 0; |
| 3081 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 3082 | napi_disable(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3083 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 3084 | usb_kill_urb(tp->intr_urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3085 | cancel_delayed_work_sync(&tp->schedule); |
| 3086 | netif_stop_queue(netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3087 | |
| 3088 | res = usb_autopm_get_interface(tp->intf); |
hayeswang | 53543db | 2015-02-06 11:30:48 +0800 | [diff] [blame] | 3089 | if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) { |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3090 | rtl_drop_queued_tx(tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 3091 | rtl_stop_rx(tp); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3092 | } else { |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3093 | mutex_lock(&tp->control); |
| 3094 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 3095 | /* The autosuspend may have been enabled and wouldn't |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3096 | * be disable when autoresume occurs, because the |
| 3097 | * netif_running() would be false. |
| 3098 | */ |
hayeswang | 923e1ee | 2014-10-29 11:12:15 +0800 | [diff] [blame] | 3099 | rtl_runtime_suspend_enable(tp, false); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3100 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3101 | tp->rtl_ops.down(tp); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3102 | |
| 3103 | mutex_unlock(&tp->control); |
| 3104 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3105 | usb_autopm_put_interface(tp->intf); |
| 3106 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3107 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 3108 | free_all_mem(tp); |
| 3109 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3110 | return res; |
| 3111 | } |
| 3112 | |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 3113 | static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3114 | { |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 3115 | ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev); |
| 3116 | ocp_reg_write(tp, OCP_EEE_DATA, reg); |
| 3117 | ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev); |
| 3118 | } |
| 3119 | |
| 3120 | static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg) |
| 3121 | { |
| 3122 | u16 data; |
| 3123 | |
| 3124 | r8152_mmd_indirect(tp, dev, reg); |
| 3125 | data = ocp_reg_read(tp, OCP_EEE_DATA); |
| 3126 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 3127 | |
| 3128 | return data; |
| 3129 | } |
| 3130 | |
| 3131 | static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data) |
| 3132 | { |
| 3133 | r8152_mmd_indirect(tp, dev, reg); |
| 3134 | ocp_reg_write(tp, OCP_EEE_DATA, data); |
| 3135 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 3136 | } |
| 3137 | |
| 3138 | static void r8152_eee_en(struct r8152 *tp, bool enable) |
| 3139 | { |
| 3140 | u16 config1, config2, config3; |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 3141 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3142 | |
| 3143 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 3144 | config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask; |
| 3145 | config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2); |
| 3146 | config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask; |
| 3147 | |
| 3148 | if (enable) { |
| 3149 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 3150 | config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN; |
| 3151 | config1 |= sd_rise_time(1); |
| 3152 | config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN; |
| 3153 | config3 |= fast_snr(42); |
| 3154 | } else { |
| 3155 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 3156 | config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | |
| 3157 | RX_QUIET_EN); |
| 3158 | config1 |= sd_rise_time(7); |
| 3159 | config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN); |
| 3160 | config3 |= fast_snr(511); |
| 3161 | } |
| 3162 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3163 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 3164 | ocp_reg_write(tp, OCP_EEE_CONFIG1, config1); |
| 3165 | ocp_reg_write(tp, OCP_EEE_CONFIG2, config2); |
| 3166 | ocp_reg_write(tp, OCP_EEE_CONFIG3, config3); |
| 3167 | } |
| 3168 | |
| 3169 | static void r8152b_enable_eee(struct r8152 *tp) |
| 3170 | { |
| 3171 | r8152_eee_en(tp, true); |
| 3172 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX); |
| 3173 | } |
| 3174 | |
| 3175 | static void r8153_eee_en(struct r8152 *tp, bool enable) |
| 3176 | { |
| 3177 | u32 ocp_data; |
| 3178 | u16 config; |
| 3179 | |
| 3180 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 3181 | config = ocp_reg_read(tp, OCP_EEE_CFG); |
| 3182 | |
| 3183 | if (enable) { |
| 3184 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 3185 | config |= EEE10_EN; |
| 3186 | } else { |
| 3187 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 3188 | config &= ~EEE10_EN; |
| 3189 | } |
| 3190 | |
| 3191 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 3192 | ocp_reg_write(tp, OCP_EEE_CFG, config); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3195 | static void r8153_enable_eee(struct r8152 *tp) |
| 3196 | { |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 3197 | r8153_eee_en(tp, true); |
| 3198 | ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3199 | } |
| 3200 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3201 | static void r8152b_enable_fc(struct r8152 *tp) |
| 3202 | { |
| 3203 | u16 anar; |
| 3204 | |
| 3205 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 3206 | anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; |
| 3207 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 3208 | } |
| 3209 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 3210 | static void rtl_tally_reset(struct r8152 *tp) |
| 3211 | { |
| 3212 | u32 ocp_data; |
| 3213 | |
| 3214 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY); |
| 3215 | ocp_data |= TALLY_RESET; |
| 3216 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data); |
| 3217 | } |
| 3218 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3219 | static void r8152b_init(struct r8152 *tp) |
| 3220 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 3221 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3222 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 3223 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3224 | return; |
| 3225 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3226 | r8152b_disable_aldps(tp); |
| 3227 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3228 | if (tp->version == RTL_VER_01) { |
| 3229 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 3230 | ocp_data &= ~LED_MODE_MASK; |
| 3231 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 3232 | } |
| 3233 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3234 | r8152_power_cut_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3235 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3236 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 3237 | ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; |
| 3238 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 3239 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); |
| 3240 | ocp_data &= ~MCU_CLK_RATIO_MASK; |
| 3241 | ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; |
| 3242 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); |
| 3243 | ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | |
| 3244 | SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; |
| 3245 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); |
| 3246 | |
| 3247 | r8152b_enable_eee(tp); |
| 3248 | r8152b_enable_aldps(tp); |
| 3249 | r8152b_enable_fc(tp); |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 3250 | rtl_tally_reset(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3251 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 3252 | /* enable rx aggregation */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3253 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 3254 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3255 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 3256 | } |
| 3257 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3258 | static void r8153_init(struct r8152 *tp) |
| 3259 | { |
| 3260 | u32 ocp_data; |
| 3261 | int i; |
| 3262 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 3263 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3264 | return; |
| 3265 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3266 | r8153_disable_aldps(tp); |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 3267 | r8153_u1u2en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3268 | |
| 3269 | for (i = 0; i < 500; i++) { |
| 3270 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 3271 | AUTOLOAD_DONE) |
| 3272 | break; |
| 3273 | msleep(20); |
| 3274 | } |
| 3275 | |
| 3276 | for (i = 0; i < 500; i++) { |
| 3277 | ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK; |
| 3278 | if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN) |
| 3279 | break; |
| 3280 | msleep(20); |
| 3281 | } |
| 3282 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3283 | usb_disable_lpm(tp->udev); |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 3284 | r8153_u2p3en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3285 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 3286 | if (tp->version == RTL_VER_04) { |
| 3287 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2); |
| 3288 | ocp_data &= ~pwd_dn_scale_mask; |
| 3289 | ocp_data |= pwd_dn_scale(96); |
| 3290 | ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data); |
| 3291 | |
| 3292 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); |
| 3293 | ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; |
| 3294 | ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); |
| 3295 | } else if (tp->version == RTL_VER_05) { |
| 3296 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0); |
| 3297 | ocp_data &= ~ECM_ALDPS; |
| 3298 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data); |
| 3299 | |
| 3300 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 3301 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 3302 | ocp_data &= ~DYNAMIC_BURST; |
| 3303 | else |
| 3304 | ocp_data |= DYNAMIC_BURST; |
| 3305 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 3306 | } else if (tp->version == RTL_VER_06) { |
| 3307 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 3308 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 3309 | ocp_data &= ~DYNAMIC_BURST; |
| 3310 | else |
| 3311 | ocp_data |= DYNAMIC_BURST; |
| 3312 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2); |
| 3316 | ocp_data |= EP4_FULL_FC; |
| 3317 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data); |
| 3318 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3319 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); |
| 3320 | ocp_data &= ~TIMER11_EN; |
| 3321 | ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); |
| 3322 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3323 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 3324 | ocp_data &= ~LED_MODE_MASK; |
| 3325 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 3326 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 3327 | ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; |
hayeswang | 34203e2 | 2015-02-06 11:30:46 +0800 | [diff] [blame] | 3328 | if (tp->version == RTL_VER_04 && tp->udev->speed != USB_SPEED_SUPER) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3329 | ocp_data |= LPM_TIMER_500MS; |
hayeswang | 34203e2 | 2015-02-06 11:30:46 +0800 | [diff] [blame] | 3330 | else |
| 3331 | ocp_data |= LPM_TIMER_500US; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3332 | ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); |
| 3333 | |
| 3334 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); |
| 3335 | ocp_data &= ~SEN_VAL_MASK; |
| 3336 | ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; |
| 3337 | ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); |
| 3338 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 3339 | ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); |
| 3340 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 3341 | r8153_power_cut_en(tp, false); |
| 3342 | r8153_u1u2en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3343 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3344 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO); |
| 3345 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO); |
| 3346 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, |
| 3347 | PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN | |
| 3348 | U1U2_SPDWN_EN | L1_SPDWN_EN); |
| 3349 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, |
| 3350 | PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN | |
| 3351 | TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN | |
| 3352 | EEE_SPDWN_EN); |
| 3353 | |
| 3354 | r8153_enable_eee(tp); |
| 3355 | r8153_enable_aldps(tp); |
| 3356 | r8152b_enable_fc(tp); |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 3357 | rtl_tally_reset(tp); |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3358 | r8153_u2p3en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3359 | } |
| 3360 | |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 3361 | static int rtl8152_pre_reset(struct usb_interface *intf) |
| 3362 | { |
| 3363 | struct r8152 *tp = usb_get_intfdata(intf); |
| 3364 | struct net_device *netdev; |
| 3365 | |
| 3366 | if (!tp) |
| 3367 | return 0; |
| 3368 | |
| 3369 | netdev = tp->netdev; |
| 3370 | if (!netif_running(netdev)) |
| 3371 | return 0; |
| 3372 | |
| 3373 | napi_disable(&tp->napi); |
| 3374 | clear_bit(WORK_ENABLE, &tp->flags); |
| 3375 | usb_kill_urb(tp->intr_urb); |
| 3376 | cancel_delayed_work_sync(&tp->schedule); |
| 3377 | if (netif_carrier_ok(netdev)) { |
| 3378 | netif_stop_queue(netdev); |
| 3379 | mutex_lock(&tp->control); |
| 3380 | tp->rtl_ops.disable(tp); |
| 3381 | mutex_unlock(&tp->control); |
| 3382 | } |
| 3383 | |
| 3384 | return 0; |
| 3385 | } |
| 3386 | |
| 3387 | static int rtl8152_post_reset(struct usb_interface *intf) |
| 3388 | { |
| 3389 | struct r8152 *tp = usb_get_intfdata(intf); |
| 3390 | struct net_device *netdev; |
| 3391 | |
| 3392 | if (!tp) |
| 3393 | return 0; |
| 3394 | |
| 3395 | netdev = tp->netdev; |
| 3396 | if (!netif_running(netdev)) |
| 3397 | return 0; |
| 3398 | |
| 3399 | set_bit(WORK_ENABLE, &tp->flags); |
| 3400 | if (netif_carrier_ok(netdev)) { |
| 3401 | mutex_lock(&tp->control); |
| 3402 | tp->rtl_ops.enable(tp); |
| 3403 | rtl8152_set_rx_mode(netdev); |
| 3404 | mutex_unlock(&tp->control); |
| 3405 | netif_wake_queue(netdev); |
| 3406 | } |
| 3407 | |
| 3408 | napi_enable(&tp->napi); |
| 3409 | |
| 3410 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) |
| 3414 | { |
| 3415 | struct r8152 *tp = usb_get_intfdata(intf); |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 3416 | struct net_device *netdev = tp->netdev; |
| 3417 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3418 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3419 | mutex_lock(&tp->control); |
| 3420 | |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 3421 | if (PMSG_IS_AUTO(message)) { |
| 3422 | if (netif_running(netdev) && work_busy(&tp->schedule.work)) { |
| 3423 | ret = -EBUSY; |
| 3424 | goto out1; |
| 3425 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3426 | |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 3427 | set_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 3428 | } else { |
| 3429 | netif_device_detach(netdev); |
| 3430 | } |
| 3431 | |
hayeswang | e3bd1a8 | 2014-10-29 11:12:17 +0800 | [diff] [blame] | 3432 | if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3433 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 3434 | usb_kill_urb(tp->intr_urb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 3435 | napi_disable(&tp->napi); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3436 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 3437 | rtl_stop_rx(tp); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3438 | rtl_runtime_suspend_enable(tp, true); |
| 3439 | } else { |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 3440 | cancel_delayed_work_sync(&tp->schedule); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3441 | tp->rtl_ops.down(tp); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3442 | } |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 3443 | napi_enable(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3444 | } |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 3445 | out1: |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3446 | mutex_unlock(&tp->control); |
| 3447 | |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 3448 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | static int rtl8152_resume(struct usb_interface *intf) |
| 3452 | { |
| 3453 | struct r8152 *tp = usb_get_intfdata(intf); |
| 3454 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3455 | mutex_lock(&tp->control); |
| 3456 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3457 | if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) { |
| 3458 | tp->rtl_ops.init(tp); |
| 3459 | netif_device_attach(tp->netdev); |
| 3460 | } |
| 3461 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3462 | if (netif_running(tp->netdev)) { |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3463 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { |
| 3464 | rtl_runtime_suspend_enable(tp, false); |
| 3465 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
hayeswang | 41cec84 | 2015-07-24 13:54:25 +0800 | [diff] [blame] | 3466 | napi_disable(&tp->napi); |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 3467 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 3468 | if (netif_carrier_ok(tp->netdev)) |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 3469 | rtl_start_rx(tp); |
hayeswang | 41cec84 | 2015-07-24 13:54:25 +0800 | [diff] [blame] | 3470 | napi_enable(&tp->napi); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3471 | } else { |
| 3472 | tp->rtl_ops.up(tp); |
| 3473 | rtl8152_set_speed(tp, AUTONEG_ENABLE, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 3474 | tp->mii.supports_gmii ? |
| 3475 | SPEED_1000 : SPEED_100, |
| 3476 | DUPLEX_FULL); |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 3477 | netif_carrier_off(tp->netdev); |
| 3478 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3479 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 3480 | usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
hayeswang | 923e1ee | 2014-10-29 11:12:15 +0800 | [diff] [blame] | 3481 | } else if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { |
| 3482 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3485 | mutex_unlock(&tp->control); |
| 3486 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3487 | return 0; |
| 3488 | } |
| 3489 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3490 | static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 3491 | { |
| 3492 | struct r8152 *tp = netdev_priv(dev); |
| 3493 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3494 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 3495 | return; |
| 3496 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 3497 | if (!rtl_can_wakeup(tp)) { |
| 3498 | wol->supported = 0; |
| 3499 | wol->wolopts = 0; |
| 3500 | } else { |
| 3501 | mutex_lock(&tp->control); |
| 3502 | wol->supported = WAKE_ANY; |
| 3503 | wol->wolopts = __rtl_get_wol(tp); |
| 3504 | mutex_unlock(&tp->control); |
| 3505 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3506 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3507 | usb_autopm_put_interface(tp->intf); |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 3511 | { |
| 3512 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3513 | int ret; |
| 3514 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 3515 | if (!rtl_can_wakeup(tp)) |
| 3516 | return -EOPNOTSUPP; |
| 3517 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3518 | ret = usb_autopm_get_interface(tp->intf); |
| 3519 | if (ret < 0) |
| 3520 | goto out_set_wol; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3521 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3522 | mutex_lock(&tp->control); |
| 3523 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3524 | __rtl_set_wol(tp, wol->wolopts); |
| 3525 | tp->saved_wolopts = wol->wolopts & WAKE_ANY; |
| 3526 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3527 | mutex_unlock(&tp->control); |
| 3528 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3529 | usb_autopm_put_interface(tp->intf); |
| 3530 | |
| 3531 | out_set_wol: |
| 3532 | return ret; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3533 | } |
| 3534 | |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 3535 | static u32 rtl8152_get_msglevel(struct net_device *dev) |
| 3536 | { |
| 3537 | struct r8152 *tp = netdev_priv(dev); |
| 3538 | |
| 3539 | return tp->msg_enable; |
| 3540 | } |
| 3541 | |
| 3542 | static void rtl8152_set_msglevel(struct net_device *dev, u32 value) |
| 3543 | { |
| 3544 | struct r8152 *tp = netdev_priv(dev); |
| 3545 | |
| 3546 | tp->msg_enable = value; |
| 3547 | } |
| 3548 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3549 | static void rtl8152_get_drvinfo(struct net_device *netdev, |
| 3550 | struct ethtool_drvinfo *info) |
| 3551 | { |
| 3552 | struct r8152 *tp = netdev_priv(netdev); |
| 3553 | |
hayeswang | b0b46c7 | 2014-08-26 10:08:23 +0800 | [diff] [blame] | 3554 | strlcpy(info->driver, MODULENAME, sizeof(info->driver)); |
| 3555 | strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3556 | usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); |
| 3557 | } |
| 3558 | |
| 3559 | static |
| 3560 | int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd) |
| 3561 | { |
| 3562 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 3563 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3564 | |
| 3565 | if (!tp->mii.mdio_read) |
| 3566 | return -EOPNOTSUPP; |
| 3567 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 3568 | ret = usb_autopm_get_interface(tp->intf); |
| 3569 | if (ret < 0) |
| 3570 | goto out; |
| 3571 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3572 | mutex_lock(&tp->control); |
| 3573 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 3574 | ret = mii_ethtool_gset(&tp->mii, cmd); |
| 3575 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3576 | mutex_unlock(&tp->control); |
| 3577 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 3578 | usb_autopm_put_interface(tp->intf); |
| 3579 | |
| 3580 | out: |
| 3581 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3582 | } |
| 3583 | |
| 3584 | static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 3585 | { |
| 3586 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3587 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3588 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3589 | ret = usb_autopm_get_interface(tp->intf); |
| 3590 | if (ret < 0) |
| 3591 | goto out; |
| 3592 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3593 | mutex_lock(&tp->control); |
| 3594 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3595 | ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex); |
| 3596 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3597 | mutex_unlock(&tp->control); |
| 3598 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3599 | usb_autopm_put_interface(tp->intf); |
| 3600 | |
| 3601 | out: |
| 3602 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 3605 | static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = { |
| 3606 | "tx_packets", |
| 3607 | "rx_packets", |
| 3608 | "tx_errors", |
| 3609 | "rx_errors", |
| 3610 | "rx_missed", |
| 3611 | "align_errors", |
| 3612 | "tx_single_collisions", |
| 3613 | "tx_multi_collisions", |
| 3614 | "rx_unicast", |
| 3615 | "rx_broadcast", |
| 3616 | "rx_multicast", |
| 3617 | "tx_aborted", |
| 3618 | "tx_underrun", |
| 3619 | }; |
| 3620 | |
| 3621 | static int rtl8152_get_sset_count(struct net_device *dev, int sset) |
| 3622 | { |
| 3623 | switch (sset) { |
| 3624 | case ETH_SS_STATS: |
| 3625 | return ARRAY_SIZE(rtl8152_gstrings); |
| 3626 | default: |
| 3627 | return -EOPNOTSUPP; |
| 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | static void rtl8152_get_ethtool_stats(struct net_device *dev, |
| 3632 | struct ethtool_stats *stats, u64 *data) |
| 3633 | { |
| 3634 | struct r8152 *tp = netdev_priv(dev); |
| 3635 | struct tally_counter tally; |
| 3636 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 3637 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 3638 | return; |
| 3639 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 3640 | generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA); |
| 3641 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 3642 | usb_autopm_put_interface(tp->intf); |
| 3643 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 3644 | data[0] = le64_to_cpu(tally.tx_packets); |
| 3645 | data[1] = le64_to_cpu(tally.rx_packets); |
| 3646 | data[2] = le64_to_cpu(tally.tx_errors); |
| 3647 | data[3] = le32_to_cpu(tally.rx_errors); |
| 3648 | data[4] = le16_to_cpu(tally.rx_missed); |
| 3649 | data[5] = le16_to_cpu(tally.align_errors); |
| 3650 | data[6] = le32_to_cpu(tally.tx_one_collision); |
| 3651 | data[7] = le32_to_cpu(tally.tx_multi_collision); |
| 3652 | data[8] = le64_to_cpu(tally.rx_unicast); |
| 3653 | data[9] = le64_to_cpu(tally.rx_broadcast); |
| 3654 | data[10] = le32_to_cpu(tally.rx_multicast); |
| 3655 | data[11] = le16_to_cpu(tally.tx_aborted); |
hayeswang | f37119c | 2014-10-28 14:05:51 +0800 | [diff] [blame] | 3656 | data[12] = le16_to_cpu(tally.tx_underrun); |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data) |
| 3660 | { |
| 3661 | switch (stringset) { |
| 3662 | case ETH_SS_STATS: |
| 3663 | memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings)); |
| 3664 | break; |
| 3665 | } |
| 3666 | } |
| 3667 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 3668 | static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 3669 | { |
| 3670 | u32 ocp_data, lp, adv, supported = 0; |
| 3671 | u16 val; |
| 3672 | |
| 3673 | val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); |
| 3674 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 3675 | |
| 3676 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV); |
| 3677 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 3678 | |
| 3679 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); |
| 3680 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 3681 | |
| 3682 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 3683 | ocp_data &= EEE_RX_EN | EEE_TX_EN; |
| 3684 | |
| 3685 | eee->eee_enabled = !!ocp_data; |
| 3686 | eee->eee_active = !!(supported & adv & lp); |
| 3687 | eee->supported = supported; |
| 3688 | eee->advertised = adv; |
| 3689 | eee->lp_advertised = lp; |
| 3690 | |
| 3691 | return 0; |
| 3692 | } |
| 3693 | |
| 3694 | static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 3695 | { |
| 3696 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 3697 | |
| 3698 | r8152_eee_en(tp, eee->eee_enabled); |
| 3699 | |
| 3700 | if (!eee->eee_enabled) |
| 3701 | val = 0; |
| 3702 | |
| 3703 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val); |
| 3704 | |
| 3705 | return 0; |
| 3706 | } |
| 3707 | |
| 3708 | static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 3709 | { |
| 3710 | u32 ocp_data, lp, adv, supported = 0; |
| 3711 | u16 val; |
| 3712 | |
| 3713 | val = ocp_reg_read(tp, OCP_EEE_ABLE); |
| 3714 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 3715 | |
| 3716 | val = ocp_reg_read(tp, OCP_EEE_ADV); |
| 3717 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 3718 | |
| 3719 | val = ocp_reg_read(tp, OCP_EEE_LPABLE); |
| 3720 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 3721 | |
| 3722 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 3723 | ocp_data &= EEE_RX_EN | EEE_TX_EN; |
| 3724 | |
| 3725 | eee->eee_enabled = !!ocp_data; |
| 3726 | eee->eee_active = !!(supported & adv & lp); |
| 3727 | eee->supported = supported; |
| 3728 | eee->advertised = adv; |
| 3729 | eee->lp_advertised = lp; |
| 3730 | |
| 3731 | return 0; |
| 3732 | } |
| 3733 | |
| 3734 | static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 3735 | { |
| 3736 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 3737 | |
| 3738 | r8153_eee_en(tp, eee->eee_enabled); |
| 3739 | |
| 3740 | if (!eee->eee_enabled) |
| 3741 | val = 0; |
| 3742 | |
| 3743 | ocp_reg_write(tp, OCP_EEE_ADV, val); |
| 3744 | |
| 3745 | return 0; |
| 3746 | } |
| 3747 | |
| 3748 | static int |
| 3749 | rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata) |
| 3750 | { |
| 3751 | struct r8152 *tp = netdev_priv(net); |
| 3752 | int ret; |
| 3753 | |
| 3754 | ret = usb_autopm_get_interface(tp->intf); |
| 3755 | if (ret < 0) |
| 3756 | goto out; |
| 3757 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3758 | mutex_lock(&tp->control); |
| 3759 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 3760 | ret = tp->rtl_ops.eee_get(tp, edata); |
| 3761 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3762 | mutex_unlock(&tp->control); |
| 3763 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 3764 | usb_autopm_put_interface(tp->intf); |
| 3765 | |
| 3766 | out: |
| 3767 | return ret; |
| 3768 | } |
| 3769 | |
| 3770 | static int |
| 3771 | rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata) |
| 3772 | { |
| 3773 | struct r8152 *tp = netdev_priv(net); |
| 3774 | int ret; |
| 3775 | |
| 3776 | ret = usb_autopm_get_interface(tp->intf); |
| 3777 | if (ret < 0) |
| 3778 | goto out; |
| 3779 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3780 | mutex_lock(&tp->control); |
| 3781 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 3782 | ret = tp->rtl_ops.eee_set(tp, edata); |
hayeswang | 9d31a7b | 2014-10-06 10:36:04 +0800 | [diff] [blame] | 3783 | if (!ret) |
| 3784 | ret = mii_nway_restart(&tp->mii); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 3785 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3786 | mutex_unlock(&tp->control); |
| 3787 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 3788 | usb_autopm_put_interface(tp->intf); |
| 3789 | |
| 3790 | out: |
| 3791 | return ret; |
| 3792 | } |
| 3793 | |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 3794 | static int rtl8152_nway_reset(struct net_device *dev) |
| 3795 | { |
| 3796 | struct r8152 *tp = netdev_priv(dev); |
| 3797 | int ret; |
| 3798 | |
| 3799 | ret = usb_autopm_get_interface(tp->intf); |
| 3800 | if (ret < 0) |
| 3801 | goto out; |
| 3802 | |
| 3803 | mutex_lock(&tp->control); |
| 3804 | |
| 3805 | ret = mii_nway_restart(&tp->mii); |
| 3806 | |
| 3807 | mutex_unlock(&tp->control); |
| 3808 | |
| 3809 | usb_autopm_put_interface(tp->intf); |
| 3810 | |
| 3811 | out: |
| 3812 | return ret; |
| 3813 | } |
| 3814 | |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 3815 | static int rtl8152_get_coalesce(struct net_device *netdev, |
| 3816 | struct ethtool_coalesce *coalesce) |
| 3817 | { |
| 3818 | struct r8152 *tp = netdev_priv(netdev); |
| 3819 | |
| 3820 | switch (tp->version) { |
| 3821 | case RTL_VER_01: |
| 3822 | case RTL_VER_02: |
| 3823 | return -EOPNOTSUPP; |
| 3824 | default: |
| 3825 | break; |
| 3826 | } |
| 3827 | |
| 3828 | coalesce->rx_coalesce_usecs = tp->coalesce; |
| 3829 | |
| 3830 | return 0; |
| 3831 | } |
| 3832 | |
| 3833 | static int rtl8152_set_coalesce(struct net_device *netdev, |
| 3834 | struct ethtool_coalesce *coalesce) |
| 3835 | { |
| 3836 | struct r8152 *tp = netdev_priv(netdev); |
| 3837 | int ret; |
| 3838 | |
| 3839 | switch (tp->version) { |
| 3840 | case RTL_VER_01: |
| 3841 | case RTL_VER_02: |
| 3842 | return -EOPNOTSUPP; |
| 3843 | default: |
| 3844 | break; |
| 3845 | } |
| 3846 | |
| 3847 | if (coalesce->rx_coalesce_usecs > COALESCE_SLOW) |
| 3848 | return -EINVAL; |
| 3849 | |
| 3850 | ret = usb_autopm_get_interface(tp->intf); |
| 3851 | if (ret < 0) |
| 3852 | return ret; |
| 3853 | |
| 3854 | mutex_lock(&tp->control); |
| 3855 | |
| 3856 | if (tp->coalesce != coalesce->rx_coalesce_usecs) { |
| 3857 | tp->coalesce = coalesce->rx_coalesce_usecs; |
| 3858 | |
| 3859 | if (netif_running(tp->netdev) && netif_carrier_ok(netdev)) |
| 3860 | r8153_set_rx_early_timeout(tp); |
| 3861 | } |
| 3862 | |
| 3863 | mutex_unlock(&tp->control); |
| 3864 | |
| 3865 | usb_autopm_put_interface(tp->intf); |
| 3866 | |
| 3867 | return ret; |
| 3868 | } |
| 3869 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3870 | static struct ethtool_ops ops = { |
| 3871 | .get_drvinfo = rtl8152_get_drvinfo, |
| 3872 | .get_settings = rtl8152_get_settings, |
| 3873 | .set_settings = rtl8152_set_settings, |
| 3874 | .get_link = ethtool_op_get_link, |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 3875 | .nway_reset = rtl8152_nway_reset, |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 3876 | .get_msglevel = rtl8152_get_msglevel, |
| 3877 | .set_msglevel = rtl8152_set_msglevel, |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3878 | .get_wol = rtl8152_get_wol, |
| 3879 | .set_wol = rtl8152_set_wol, |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 3880 | .get_strings = rtl8152_get_strings, |
| 3881 | .get_sset_count = rtl8152_get_sset_count, |
| 3882 | .get_ethtool_stats = rtl8152_get_ethtool_stats, |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 3883 | .get_coalesce = rtl8152_get_coalesce, |
| 3884 | .set_coalesce = rtl8152_set_coalesce, |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 3885 | .get_eee = rtl_ethtool_get_eee, |
| 3886 | .set_eee = rtl_ethtool_set_eee, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3887 | }; |
| 3888 | |
| 3889 | static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) |
| 3890 | { |
| 3891 | struct r8152 *tp = netdev_priv(netdev); |
| 3892 | struct mii_ioctl_data *data = if_mii(rq); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3893 | int res; |
| 3894 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 3895 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3896 | return -ENODEV; |
| 3897 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3898 | res = usb_autopm_get_interface(tp->intf); |
| 3899 | if (res < 0) |
| 3900 | goto out; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3901 | |
| 3902 | switch (cmd) { |
| 3903 | case SIOCGMIIPHY: |
| 3904 | data->phy_id = R8152_PHY_ID; /* Internal PHY */ |
| 3905 | break; |
| 3906 | |
| 3907 | case SIOCGMIIREG: |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3908 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3909 | data->val_out = r8152_mdio_read(tp, data->reg_num); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3910 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3911 | break; |
| 3912 | |
| 3913 | case SIOCSMIIREG: |
| 3914 | if (!capable(CAP_NET_ADMIN)) { |
| 3915 | res = -EPERM; |
| 3916 | break; |
| 3917 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3918 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3919 | r8152_mdio_write(tp, data->reg_num, data->val_in); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 3920 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3921 | break; |
| 3922 | |
| 3923 | default: |
| 3924 | res = -EOPNOTSUPP; |
| 3925 | } |
| 3926 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3927 | usb_autopm_put_interface(tp->intf); |
| 3928 | |
| 3929 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3930 | return res; |
| 3931 | } |
| 3932 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 3933 | static int rtl8152_change_mtu(struct net_device *dev, int new_mtu) |
| 3934 | { |
| 3935 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 3936 | int ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 3937 | |
| 3938 | switch (tp->version) { |
| 3939 | case RTL_VER_01: |
| 3940 | case RTL_VER_02: |
| 3941 | return eth_change_mtu(dev, new_mtu); |
| 3942 | default: |
| 3943 | break; |
| 3944 | } |
| 3945 | |
| 3946 | if (new_mtu < 68 || new_mtu > RTL8153_MAX_MTU) |
| 3947 | return -EINVAL; |
| 3948 | |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 3949 | ret = usb_autopm_get_interface(tp->intf); |
| 3950 | if (ret < 0) |
| 3951 | return ret; |
| 3952 | |
| 3953 | mutex_lock(&tp->control); |
| 3954 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 3955 | dev->mtu = new_mtu; |
| 3956 | |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 3957 | if (netif_running(dev) && netif_carrier_ok(dev)) |
| 3958 | r8153_set_rx_early_size(tp); |
| 3959 | |
| 3960 | mutex_unlock(&tp->control); |
| 3961 | |
| 3962 | usb_autopm_put_interface(tp->intf); |
| 3963 | |
| 3964 | return ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 3965 | } |
| 3966 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3967 | static const struct net_device_ops rtl8152_netdev_ops = { |
| 3968 | .ndo_open = rtl8152_open, |
| 3969 | .ndo_stop = rtl8152_close, |
| 3970 | .ndo_do_ioctl = rtl8152_ioctl, |
| 3971 | .ndo_start_xmit = rtl8152_start_xmit, |
| 3972 | .ndo_tx_timeout = rtl8152_tx_timeout, |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 3973 | .ndo_set_features = rtl8152_set_features, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3974 | .ndo_set_rx_mode = rtl8152_set_rx_mode, |
| 3975 | .ndo_set_mac_address = rtl8152_set_mac_address, |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 3976 | .ndo_change_mtu = rtl8152_change_mtu, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3977 | .ndo_validate_addr = eth_validate_addr, |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 3978 | .ndo_features_check = rtl8152_features_check, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3979 | }; |
| 3980 | |
| 3981 | static void r8152b_get_version(struct r8152 *tp) |
| 3982 | { |
| 3983 | u32 ocp_data; |
| 3984 | u16 version; |
| 3985 | |
| 3986 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1); |
| 3987 | version = (u16)(ocp_data & VERSION_MASK); |
| 3988 | |
| 3989 | switch (version) { |
| 3990 | case 0x4c00: |
| 3991 | tp->version = RTL_VER_01; |
| 3992 | break; |
| 3993 | case 0x4c10: |
| 3994 | tp->version = RTL_VER_02; |
| 3995 | break; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3996 | case 0x5c00: |
| 3997 | tp->version = RTL_VER_03; |
| 3998 | tp->mii.supports_gmii = 1; |
| 3999 | break; |
| 4000 | case 0x5c10: |
| 4001 | tp->version = RTL_VER_04; |
| 4002 | tp->mii.supports_gmii = 1; |
| 4003 | break; |
| 4004 | case 0x5c20: |
| 4005 | tp->version = RTL_VER_05; |
| 4006 | tp->mii.supports_gmii = 1; |
| 4007 | break; |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 4008 | case 0x5c30: |
| 4009 | tp->version = RTL_VER_06; |
| 4010 | tp->mii.supports_gmii = 1; |
| 4011 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4012 | default: |
| 4013 | netif_info(tp, probe, tp->netdev, |
| 4014 | "Unknown version 0x%04x\n", version); |
| 4015 | break; |
| 4016 | } |
| 4017 | } |
| 4018 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 4019 | static void rtl8152_unload(struct r8152 *tp) |
| 4020 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4021 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4022 | return; |
| 4023 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4024 | if (tp->version != RTL_VER_01) |
| 4025 | r8152_power_cut_en(tp, true); |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 4026 | } |
| 4027 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4028 | static void rtl8153_unload(struct r8152 *tp) |
| 4029 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4030 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4031 | return; |
| 4032 | |
hayeswang | 49be172 | 2014-10-01 13:25:11 +0800 | [diff] [blame] | 4033 | r8153_power_cut_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4034 | } |
| 4035 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 4036 | static int rtl_ops_init(struct r8152 *tp) |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 4037 | { |
| 4038 | struct rtl_ops *ops = &tp->rtl_ops; |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 4039 | int ret = 0; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 4040 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 4041 | switch (tp->version) { |
| 4042 | case RTL_VER_01: |
| 4043 | case RTL_VER_02: |
| 4044 | ops->init = r8152b_init; |
| 4045 | ops->enable = rtl8152_enable; |
| 4046 | ops->disable = rtl8152_disable; |
| 4047 | ops->up = rtl8152_up; |
| 4048 | ops->down = rtl8152_down; |
| 4049 | ops->unload = rtl8152_unload; |
| 4050 | ops->eee_get = r8152_get_eee; |
| 4051 | ops->eee_set = r8152_set_eee; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4052 | break; |
| 4053 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 4054 | case RTL_VER_03: |
| 4055 | case RTL_VER_04: |
| 4056 | case RTL_VER_05: |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 4057 | case RTL_VER_06: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 4058 | ops->init = r8153_init; |
| 4059 | ops->enable = rtl8153_enable; |
| 4060 | ops->disable = rtl8153_disable; |
| 4061 | ops->up = rtl8153_up; |
| 4062 | ops->down = rtl8153_down; |
| 4063 | ops->unload = rtl8153_unload; |
| 4064 | ops->eee_get = r8153_get_eee; |
| 4065 | ops->eee_set = r8153_set_eee; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 4066 | break; |
| 4067 | |
| 4068 | default: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 4069 | ret = -ENODEV; |
| 4070 | netif_err(tp, probe, tp->netdev, "Unknown Device\n"); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 4071 | break; |
| 4072 | } |
| 4073 | |
| 4074 | return ret; |
| 4075 | } |
| 4076 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4077 | static int rtl8152_probe(struct usb_interface *intf, |
| 4078 | const struct usb_device_id *id) |
| 4079 | { |
| 4080 | struct usb_device *udev = interface_to_usbdev(intf); |
| 4081 | struct r8152 *tp; |
| 4082 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4083 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4084 | |
hayeswang | 10c3271 | 2014-03-04 20:47:48 +0800 | [diff] [blame] | 4085 | if (udev->actconfig->desc.bConfigurationValue != 1) { |
| 4086 | usb_driver_set_configuration(udev, 1); |
| 4087 | return -ENODEV; |
| 4088 | } |
| 4089 | |
| 4090 | usb_reset_device(udev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4091 | netdev = alloc_etherdev(sizeof(struct r8152)); |
| 4092 | if (!netdev) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 4093 | dev_err(&intf->dev, "Out of memory\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4094 | return -ENOMEM; |
| 4095 | } |
| 4096 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4097 | SET_NETDEV_DEV(netdev, &intf->dev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4098 | tp = netdev_priv(netdev); |
| 4099 | tp->msg_enable = 0x7FFF; |
| 4100 | |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 4101 | tp->udev = udev; |
| 4102 | tp->netdev = netdev; |
| 4103 | tp->intf = intf; |
| 4104 | |
hayeswang | 82cf94c | 2014-11-06 12:47:38 +0800 | [diff] [blame] | 4105 | r8152b_get_version(tp); |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 4106 | ret = rtl_ops_init(tp); |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 4107 | if (ret) |
| 4108 | goto out; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 4109 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4110 | mutex_init(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4111 | INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); |
| 4112 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4113 | netdev->netdev_ops = &rtl8152_netdev_ops; |
| 4114 | netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 4115 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 4116 | netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 4117 | NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4118 | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX | |
| 4119 | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 4120 | netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 4121 | NETIF_F_TSO | NETIF_F_FRAGLIST | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4122 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | |
hayeswang | ccc39fa | 2015-02-06 11:30:49 +0800 | [diff] [blame] | 4123 | NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4124 | netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | |
| 4125 | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | |
| 4126 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6; |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 4127 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 4128 | netdev->ethtool_ops = &ops; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 4129 | netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4130 | |
| 4131 | tp->mii.dev = netdev; |
| 4132 | tp->mii.mdio_read = read_mii_word; |
| 4133 | tp->mii.mdio_write = write_mii_word; |
| 4134 | tp->mii.phy_id_mask = 0x3f; |
| 4135 | tp->mii.reg_num_mask = 0x1f; |
| 4136 | tp->mii.phy_id = R8152_PHY_ID; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4137 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 4138 | switch (udev->speed) { |
| 4139 | case USB_SPEED_SUPER: |
| 4140 | tp->coalesce = COALESCE_SUPER; |
| 4141 | break; |
| 4142 | case USB_SPEED_HIGH: |
| 4143 | tp->coalesce = COALESCE_HIGH; |
| 4144 | break; |
| 4145 | default: |
| 4146 | tp->coalesce = COALESCE_SLOW; |
| 4147 | break; |
| 4148 | } |
| 4149 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4150 | intf->needs_remote_wakeup = 1; |
| 4151 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 4152 | tp->rtl_ops.init(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4153 | set_ethernet_addr(tp); |
| 4154 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4155 | usb_set_intfdata(intf, tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 4156 | netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4157 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4158 | ret = register_netdev(netdev); |
| 4159 | if (ret != 0) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 4160 | netif_err(tp, probe, netdev, "couldn't register the device\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4161 | goto out1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 4164 | if (!rtl_can_wakeup(tp)) |
| 4165 | __rtl_set_wol(tp, 0); |
| 4166 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4167 | tp->saved_wolopts = __rtl_get_wol(tp); |
| 4168 | if (tp->saved_wolopts) |
| 4169 | device_set_wakeup_enable(&udev->dev, true); |
| 4170 | else |
| 4171 | device_set_wakeup_enable(&udev->dev, false); |
| 4172 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 4173 | netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4174 | |
| 4175 | return 0; |
| 4176 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4177 | out1: |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 4178 | netif_napi_del(&tp->napi); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4179 | usb_set_intfdata(intf, NULL); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4180 | out: |
| 4181 | free_netdev(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4182 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4185 | static void rtl8152_disconnect(struct usb_interface *intf) |
| 4186 | { |
| 4187 | struct r8152 *tp = usb_get_intfdata(intf); |
| 4188 | |
| 4189 | usb_set_intfdata(intf, NULL); |
| 4190 | if (tp) { |
hayeswang | f561de3 | 2014-09-30 16:48:01 +0800 | [diff] [blame] | 4191 | struct usb_device *udev = tp->udev; |
| 4192 | |
| 4193 | if (udev->state == USB_STATE_NOTATTACHED) |
| 4194 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 4195 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 4196 | netif_napi_del(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4197 | unregister_netdev(tp->netdev); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 4198 | tp->rtl_ops.unload(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4199 | free_netdev(tp->netdev); |
| 4200 | } |
| 4201 | } |
| 4202 | |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 4203 | #define REALTEK_USB_DEVICE(vend, prod) \ |
| 4204 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ |
| 4205 | USB_DEVICE_ID_MATCH_INT_CLASS, \ |
| 4206 | .idVendor = (vend), \ |
| 4207 | .idProduct = (prod), \ |
| 4208 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC \ |
| 4209 | }, \ |
| 4210 | { \ |
| 4211 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \ |
| 4212 | USB_DEVICE_ID_MATCH_DEVICE, \ |
| 4213 | .idVendor = (vend), \ |
| 4214 | .idProduct = (prod), \ |
| 4215 | .bInterfaceClass = USB_CLASS_COMM, \ |
| 4216 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \ |
| 4217 | .bInterfaceProtocol = USB_CDC_PROTO_NONE |
| 4218 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4219 | /* table of devices that work with this driver */ |
| 4220 | static struct usb_device_id rtl8152_table[] = { |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 4221 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)}, |
| 4222 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)}, |
| 4223 | {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)}, |
Christian Hesse | 347eec3 | 2015-03-31 14:10:07 +0200 | [diff] [blame] | 4224 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)}, |
Vasily Titskiy | 1006da1 | 2015-05-06 10:31:21 -0400 | [diff] [blame] | 4225 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)}, |
Zheng Liu | d065c3c1 | 2015-07-07 13:54:12 -0700 | [diff] [blame] | 4226 | {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)}, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4227 | {} |
| 4228 | }; |
| 4229 | |
| 4230 | MODULE_DEVICE_TABLE(usb, rtl8152_table); |
| 4231 | |
| 4232 | static struct usb_driver rtl8152_driver = { |
| 4233 | .name = MODULENAME, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4234 | .id_table = rtl8152_table, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4235 | .probe = rtl8152_probe, |
| 4236 | .disconnect = rtl8152_disconnect, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4237 | .suspend = rtl8152_suspend, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4238 | .resume = rtl8152_resume, |
| 4239 | .reset_resume = rtl8152_resume, |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4240 | .pre_reset = rtl8152_pre_reset, |
| 4241 | .post_reset = rtl8152_post_reset, |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4242 | .supports_autosuspend = 1, |
hayeswang | a634782 | 2014-02-18 21:49:10 +0800 | [diff] [blame] | 4243 | .disable_hub_initiated_lpm = 1, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4244 | }; |
| 4245 | |
Sachin Kamat | b4236daa | 2013-05-16 17:48:08 +0000 | [diff] [blame] | 4246 | module_usb_driver(rtl8152_driver); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4247 | |
| 4248 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 4249 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 4250 | MODULE_LICENSE("GPL"); |