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