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