hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Realtek Semiconductor Corp. All rights reserved. |
| 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 | |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/signal.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/module.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/etherdevice.h> |
| 16 | #include <linux/mii.h> |
| 17 | #include <linux/ethtool.h> |
| 18 | #include <linux/usb.h> |
| 19 | #include <linux/crc32.h> |
| 20 | #include <linux/if_vlan.h> |
| 21 | #include <linux/uaccess.h> |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 22 | #include <linux/list.h> |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 23 | #include <linux/ip.h> |
| 24 | #include <linux/ipv6.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 25 | |
| 26 | /* Version Information */ |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 27 | #define DRIVER_VERSION "v1.02.0 (2013/10/28)" |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 28 | #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" |
| 29 | #define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters" |
| 30 | #define MODULENAME "r8152" |
| 31 | |
| 32 | #define R8152_PHY_ID 32 |
| 33 | |
| 34 | #define PLA_IDR 0xc000 |
| 35 | #define PLA_RCR 0xc010 |
| 36 | #define PLA_RMS 0xc016 |
| 37 | #define PLA_RXFIFO_CTRL0 0xc0a0 |
| 38 | #define PLA_RXFIFO_CTRL1 0xc0a4 |
| 39 | #define PLA_RXFIFO_CTRL2 0xc0a8 |
| 40 | #define PLA_FMC 0xc0b4 |
| 41 | #define PLA_CFG_WOL 0xc0b6 |
| 42 | #define PLA_MAR 0xcd00 |
| 43 | #define PAL_BDC_CR 0xd1a0 |
| 44 | #define PLA_LEDSEL 0xdd90 |
| 45 | #define PLA_LED_FEATURE 0xdd92 |
| 46 | #define PLA_PHYAR 0xde00 |
| 47 | #define PLA_GPHY_INTR_IMR 0xe022 |
| 48 | #define PLA_EEE_CR 0xe040 |
| 49 | #define PLA_EEEP_CR 0xe080 |
| 50 | #define PLA_MAC_PWR_CTRL 0xe0c0 |
| 51 | #define PLA_TCR0 0xe610 |
| 52 | #define PLA_TCR1 0xe612 |
| 53 | #define PLA_TXFIFO_CTRL 0xe618 |
| 54 | #define PLA_RSTTELLY 0xe800 |
| 55 | #define PLA_CR 0xe813 |
| 56 | #define PLA_CRWECR 0xe81c |
| 57 | #define PLA_CONFIG5 0xe822 |
| 58 | #define PLA_PHY_PWR 0xe84c |
| 59 | #define PLA_OOB_CTRL 0xe84f |
| 60 | #define PLA_CPCR 0xe854 |
| 61 | #define PLA_MISC_0 0xe858 |
| 62 | #define PLA_MISC_1 0xe85a |
| 63 | #define PLA_OCP_GPHY_BASE 0xe86c |
| 64 | #define PLA_TELLYCNT 0xe890 |
| 65 | #define PLA_SFF_STS_7 0xe8de |
| 66 | #define PLA_PHYSTATUS 0xe908 |
| 67 | #define PLA_BP_BA 0xfc26 |
| 68 | #define PLA_BP_0 0xfc28 |
| 69 | #define PLA_BP_1 0xfc2a |
| 70 | #define PLA_BP_2 0xfc2c |
| 71 | #define PLA_BP_3 0xfc2e |
| 72 | #define PLA_BP_4 0xfc30 |
| 73 | #define PLA_BP_5 0xfc32 |
| 74 | #define PLA_BP_6 0xfc34 |
| 75 | #define PLA_BP_7 0xfc36 |
| 76 | |
| 77 | #define USB_DEV_STAT 0xb808 |
| 78 | #define USB_USB_CTRL 0xd406 |
| 79 | #define USB_PHY_CTRL 0xd408 |
| 80 | #define USB_TX_AGG 0xd40a |
| 81 | #define USB_RX_BUF_TH 0xd40c |
| 82 | #define USB_USB_TIMER 0xd428 |
| 83 | #define USB_PM_CTRL_STATUS 0xd432 |
| 84 | #define USB_TX_DMA 0xd434 |
| 85 | #define USB_UPS_CTRL 0xd800 |
| 86 | #define USB_BP_BA 0xfc26 |
| 87 | #define USB_BP_0 0xfc28 |
| 88 | #define USB_BP_1 0xfc2a |
| 89 | #define USB_BP_2 0xfc2c |
| 90 | #define USB_BP_3 0xfc2e |
| 91 | #define USB_BP_4 0xfc30 |
| 92 | #define USB_BP_5 0xfc32 |
| 93 | #define USB_BP_6 0xfc34 |
| 94 | #define USB_BP_7 0xfc36 |
| 95 | |
| 96 | /* OCP Registers */ |
| 97 | #define OCP_ALDPS_CONFIG 0x2010 |
| 98 | #define OCP_EEE_CONFIG1 0x2080 |
| 99 | #define OCP_EEE_CONFIG2 0x2092 |
| 100 | #define OCP_EEE_CONFIG3 0x2094 |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame^] | 101 | #define OCP_BASE_MII 0xa400 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 102 | #define OCP_EEE_AR 0xa41a |
| 103 | #define OCP_EEE_DATA 0xa41c |
| 104 | |
| 105 | /* PLA_RCR */ |
| 106 | #define RCR_AAP 0x00000001 |
| 107 | #define RCR_APM 0x00000002 |
| 108 | #define RCR_AM 0x00000004 |
| 109 | #define RCR_AB 0x00000008 |
| 110 | #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB) |
| 111 | |
| 112 | /* PLA_RXFIFO_CTRL0 */ |
| 113 | #define RXFIFO_THR1_NORMAL 0x00080002 |
| 114 | #define RXFIFO_THR1_OOB 0x01800003 |
| 115 | |
| 116 | /* PLA_RXFIFO_CTRL1 */ |
| 117 | #define RXFIFO_THR2_FULL 0x00000060 |
| 118 | #define RXFIFO_THR2_HIGH 0x00000038 |
| 119 | #define RXFIFO_THR2_OOB 0x0000004a |
| 120 | |
| 121 | /* PLA_RXFIFO_CTRL2 */ |
| 122 | #define RXFIFO_THR3_FULL 0x00000078 |
| 123 | #define RXFIFO_THR3_HIGH 0x00000048 |
| 124 | #define RXFIFO_THR3_OOB 0x0000005a |
| 125 | |
| 126 | /* PLA_TXFIFO_CTRL */ |
| 127 | #define TXFIFO_THR_NORMAL 0x00400008 |
| 128 | |
| 129 | /* PLA_FMC */ |
| 130 | #define FMC_FCR_MCU_EN 0x0001 |
| 131 | |
| 132 | /* PLA_EEEP_CR */ |
| 133 | #define EEEP_CR_EEEP_TX 0x0002 |
| 134 | |
| 135 | /* PLA_TCR0 */ |
| 136 | #define TCR0_TX_EMPTY 0x0800 |
| 137 | #define TCR0_AUTO_FIFO 0x0080 |
| 138 | |
| 139 | /* PLA_TCR1 */ |
| 140 | #define VERSION_MASK 0x7cf0 |
| 141 | |
| 142 | /* PLA_CR */ |
| 143 | #define CR_RST 0x10 |
| 144 | #define CR_RE 0x08 |
| 145 | #define CR_TE 0x04 |
| 146 | |
| 147 | /* PLA_CRWECR */ |
| 148 | #define CRWECR_NORAML 0x00 |
| 149 | #define CRWECR_CONFIG 0xc0 |
| 150 | |
| 151 | /* PLA_OOB_CTRL */ |
| 152 | #define NOW_IS_OOB 0x80 |
| 153 | #define TXFIFO_EMPTY 0x20 |
| 154 | #define RXFIFO_EMPTY 0x10 |
| 155 | #define LINK_LIST_READY 0x02 |
| 156 | #define DIS_MCU_CLROOB 0x01 |
| 157 | #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY) |
| 158 | |
| 159 | /* PLA_MISC_1 */ |
| 160 | #define RXDY_GATED_EN 0x0008 |
| 161 | |
| 162 | /* PLA_SFF_STS_7 */ |
| 163 | #define RE_INIT_LL 0x8000 |
| 164 | #define MCU_BORW_EN 0x4000 |
| 165 | |
| 166 | /* PLA_CPCR */ |
| 167 | #define CPCR_RX_VLAN 0x0040 |
| 168 | |
| 169 | /* PLA_CFG_WOL */ |
| 170 | #define MAGIC_EN 0x0001 |
| 171 | |
| 172 | /* PAL_BDC_CR */ |
| 173 | #define ALDPS_PROXY_MODE 0x0001 |
| 174 | |
| 175 | /* PLA_CONFIG5 */ |
| 176 | #define LAN_WAKE_EN 0x0002 |
| 177 | |
| 178 | /* PLA_LED_FEATURE */ |
| 179 | #define LED_MODE_MASK 0x0700 |
| 180 | |
| 181 | /* PLA_PHY_PWR */ |
| 182 | #define TX_10M_IDLE_EN 0x0080 |
| 183 | #define PFM_PWM_SWITCH 0x0040 |
| 184 | |
| 185 | /* PLA_MAC_PWR_CTRL */ |
| 186 | #define D3_CLK_GATED_EN 0x00004000 |
| 187 | #define MCU_CLK_RATIO 0x07010f07 |
| 188 | #define MCU_CLK_RATIO_MASK 0x0f0f0f0f |
| 189 | |
| 190 | /* PLA_GPHY_INTR_IMR */ |
| 191 | #define GPHY_STS_MSK 0x0001 |
| 192 | #define SPEED_DOWN_MSK 0x0002 |
| 193 | #define SPDWN_RXDV_MSK 0x0004 |
| 194 | #define SPDWN_LINKCHG_MSK 0x0008 |
| 195 | |
| 196 | /* PLA_PHYAR */ |
| 197 | #define PHYAR_FLAG 0x80000000 |
| 198 | |
| 199 | /* PLA_EEE_CR */ |
| 200 | #define EEE_RX_EN 0x0001 |
| 201 | #define EEE_TX_EN 0x0002 |
| 202 | |
| 203 | /* USB_DEV_STAT */ |
| 204 | #define STAT_SPEED_MASK 0x0006 |
| 205 | #define STAT_SPEED_HIGH 0x0000 |
| 206 | #define STAT_SPEED_FULL 0x0001 |
| 207 | |
| 208 | /* USB_TX_AGG */ |
| 209 | #define TX_AGG_MAX_THRESHOLD 0x03 |
| 210 | |
| 211 | /* USB_RX_BUF_TH */ |
| 212 | #define RX_BUF_THR 0x7a120180 |
| 213 | |
| 214 | /* USB_TX_DMA */ |
| 215 | #define TEST_MODE_DISABLE 0x00000001 |
| 216 | #define TX_SIZE_ADJUST1 0x00000100 |
| 217 | |
| 218 | /* USB_UPS_CTRL */ |
| 219 | #define POWER_CUT 0x0100 |
| 220 | |
| 221 | /* USB_PM_CTRL_STATUS */ |
| 222 | #define RWSUME_INDICATE 0x0001 |
| 223 | |
| 224 | /* USB_USB_CTRL */ |
| 225 | #define RX_AGG_DISABLE 0x0010 |
| 226 | |
| 227 | /* OCP_ALDPS_CONFIG */ |
| 228 | #define ENPWRSAVE 0x8000 |
| 229 | #define ENPDNPS 0x0200 |
| 230 | #define LINKENA 0x0100 |
| 231 | #define DIS_SDSAVE 0x0010 |
| 232 | |
| 233 | /* OCP_EEE_CONFIG1 */ |
| 234 | #define RG_TXLPI_MSK_HFDUP 0x8000 |
| 235 | #define RG_MATCLR_EN 0x4000 |
| 236 | #define EEE_10_CAP 0x2000 |
| 237 | #define EEE_NWAY_EN 0x1000 |
| 238 | #define TX_QUIET_EN 0x0200 |
| 239 | #define RX_QUIET_EN 0x0100 |
| 240 | #define SDRISETIME 0x0010 /* bit 4 ~ 6 */ |
| 241 | #define RG_RXLPI_MSK_HFDUP 0x0008 |
| 242 | #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ |
| 243 | |
| 244 | /* OCP_EEE_CONFIG2 */ |
| 245 | #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ |
| 246 | #define RG_DACQUIET_EN 0x0400 |
| 247 | #define RG_LDVQUIET_EN 0x0200 |
| 248 | #define RG_CKRSEL 0x0020 |
| 249 | #define RG_EEEPRG_EN 0x0010 |
| 250 | |
| 251 | /* OCP_EEE_CONFIG3 */ |
| 252 | #define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */ |
| 253 | #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ |
| 254 | #define MSK_PH 0x0006 /* bit 0 ~ 3 */ |
| 255 | |
| 256 | /* OCP_EEE_AR */ |
| 257 | /* bit[15:14] function */ |
| 258 | #define FUN_ADDR 0x0000 |
| 259 | #define FUN_DATA 0x4000 |
| 260 | /* bit[4:0] device addr */ |
| 261 | #define DEVICE_ADDR 0x0007 |
| 262 | |
| 263 | /* OCP_EEE_DATA */ |
| 264 | #define EEE_ADDR 0x003C |
| 265 | #define EEE_DATA 0x0002 |
| 266 | |
| 267 | enum rtl_register_content { |
| 268 | _100bps = 0x08, |
| 269 | _10bps = 0x04, |
| 270 | LINK_STATUS = 0x02, |
| 271 | FULL_DUP = 0x01, |
| 272 | }; |
| 273 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 274 | #define RTL8152_MAX_TX 10 |
| 275 | #define RTL8152_MAX_RX 10 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 276 | #define INTBUFSIZE 2 |
| 277 | |
| 278 | #define INTR_LINK 0x0004 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 279 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 280 | #define RTL8152_REQT_READ 0xc0 |
| 281 | #define RTL8152_REQT_WRITE 0x40 |
| 282 | #define RTL8152_REQ_GET_REGS 0x05 |
| 283 | #define RTL8152_REQ_SET_REGS 0x05 |
| 284 | |
| 285 | #define BYTE_EN_DWORD 0xff |
| 286 | #define BYTE_EN_WORD 0x33 |
| 287 | #define BYTE_EN_BYTE 0x11 |
| 288 | #define BYTE_EN_SIX_BYTES 0x3f |
| 289 | #define BYTE_EN_START_MASK 0x0f |
| 290 | #define BYTE_EN_END_MASK 0xf0 |
| 291 | |
| 292 | #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN) |
| 293 | #define RTL8152_TX_TIMEOUT (HZ) |
| 294 | |
| 295 | /* rtl8152 flags */ |
| 296 | enum rtl8152_flags { |
| 297 | RTL8152_UNPLUG = 0, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 298 | RTL8152_SET_RX_MODE, |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 299 | WORK_ENABLE, |
| 300 | RTL8152_LINK_CHG, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | /* Define these values to match your device */ |
| 304 | #define VENDOR_ID_REALTEK 0x0bda |
| 305 | #define PRODUCT_ID_RTL8152 0x8152 |
| 306 | |
| 307 | #define MCU_TYPE_PLA 0x0100 |
| 308 | #define MCU_TYPE_USB 0x0000 |
| 309 | |
| 310 | struct rx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 311 | __le32 opts1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 312 | #define RX_LEN_MASK 0x7fff |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 313 | __le32 opts2; |
| 314 | __le32 opts3; |
| 315 | __le32 opts4; |
| 316 | __le32 opts5; |
| 317 | __le32 opts6; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | struct tx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 321 | __le32 opts1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 322 | #define TX_FS (1 << 31) /* First segment of a packet */ |
| 323 | #define TX_LS (1 << 30) /* Final segment of a packet */ |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 324 | #define TX_LEN_MASK 0x3ffff |
| 325 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 326 | __le32 opts2; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 327 | #define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */ |
| 328 | #define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */ |
| 329 | #define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */ |
| 330 | #define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 333 | struct r8152; |
| 334 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 335 | struct rx_agg { |
| 336 | struct list_head list; |
| 337 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 338 | struct r8152 *context; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 339 | void *buffer; |
| 340 | void *head; |
| 341 | }; |
| 342 | |
| 343 | struct tx_agg { |
| 344 | struct list_head list; |
| 345 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 346 | struct r8152 *context; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 347 | void *buffer; |
| 348 | void *head; |
| 349 | u32 skb_num; |
| 350 | u32 skb_len; |
| 351 | }; |
| 352 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 353 | struct r8152 { |
| 354 | unsigned long flags; |
| 355 | struct usb_device *udev; |
| 356 | struct tasklet_struct tl; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 357 | struct usb_interface *intf; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 358 | struct net_device *netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 359 | struct urb *intr_urb; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 360 | struct tx_agg tx_info[RTL8152_MAX_TX]; |
| 361 | struct rx_agg rx_info[RTL8152_MAX_RX]; |
| 362 | struct list_head rx_done, tx_free; |
| 363 | struct sk_buff_head tx_queue; |
| 364 | spinlock_t rx_lock, tx_lock; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 365 | struct delayed_work schedule; |
| 366 | struct mii_if_info mii; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 367 | int intr_interval; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 368 | u32 msg_enable; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 369 | u32 tx_qlen; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 370 | u16 ocp_base; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 371 | u8 *intr_buff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 372 | u8 version; |
| 373 | u8 speed; |
| 374 | }; |
| 375 | |
| 376 | enum rtl_version { |
| 377 | RTL_VER_UNKNOWN = 0, |
| 378 | RTL_VER_01, |
| 379 | RTL_VER_02 |
| 380 | }; |
| 381 | |
| 382 | /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). |
| 383 | * The RTL chips use a 64 element hash table based on the Ethernet CRC. |
| 384 | */ |
| 385 | static const int multicast_filter_limit = 32; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 386 | static unsigned int rx_buf_sz = 16384; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 387 | |
| 388 | static |
| 389 | int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 390 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 391 | int ret; |
| 392 | void *tmp; |
| 393 | |
| 394 | tmp = kmalloc(size, GFP_KERNEL); |
| 395 | if (!tmp) |
| 396 | return -ENOMEM; |
| 397 | |
| 398 | ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0), |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 399 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 400 | value, index, tmp, size, 500); |
| 401 | |
| 402 | memcpy(data, tmp, size); |
| 403 | kfree(tmp); |
| 404 | |
| 405 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static |
| 409 | int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 410 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 411 | int ret; |
| 412 | void *tmp; |
| 413 | |
| 414 | tmp = kmalloc(size, GFP_KERNEL); |
| 415 | if (!tmp) |
| 416 | return -ENOMEM; |
| 417 | |
| 418 | memcpy(tmp, data, size); |
| 419 | |
| 420 | ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0), |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 421 | RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 422 | value, index, tmp, size, 500); |
| 423 | |
| 424 | kfree(tmp); |
| 425 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, |
| 429 | void *data, u16 type) |
| 430 | { |
| 431 | u16 limit = 64; |
| 432 | int ret = 0; |
| 433 | |
| 434 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 435 | return -ENODEV; |
| 436 | |
| 437 | /* both size and indix must be 4 bytes align */ |
| 438 | if ((size & 3) || !size || (index & 3) || !data) |
| 439 | return -EPERM; |
| 440 | |
| 441 | if ((u32)index + (u32)size > 0xffff) |
| 442 | return -EPERM; |
| 443 | |
| 444 | while (size) { |
| 445 | if (size > limit) { |
| 446 | ret = get_registers(tp, index, type, limit, data); |
| 447 | if (ret < 0) |
| 448 | break; |
| 449 | |
| 450 | index += limit; |
| 451 | data += limit; |
| 452 | size -= limit; |
| 453 | } else { |
| 454 | ret = get_registers(tp, index, type, size, data); |
| 455 | if (ret < 0) |
| 456 | break; |
| 457 | |
| 458 | index += size; |
| 459 | data += size; |
| 460 | size = 0; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, |
| 469 | u16 size, void *data, u16 type) |
| 470 | { |
| 471 | int ret; |
| 472 | u16 byteen_start, byteen_end, byen; |
| 473 | u16 limit = 512; |
| 474 | |
| 475 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 476 | return -ENODEV; |
| 477 | |
| 478 | /* both size and indix must be 4 bytes align */ |
| 479 | if ((size & 3) || !size || (index & 3) || !data) |
| 480 | return -EPERM; |
| 481 | |
| 482 | if ((u32)index + (u32)size > 0xffff) |
| 483 | return -EPERM; |
| 484 | |
| 485 | byteen_start = byteen & BYTE_EN_START_MASK; |
| 486 | byteen_end = byteen & BYTE_EN_END_MASK; |
| 487 | |
| 488 | byen = byteen_start | (byteen_start << 4); |
| 489 | ret = set_registers(tp, index, type | byen, 4, data); |
| 490 | if (ret < 0) |
| 491 | goto error1; |
| 492 | |
| 493 | index += 4; |
| 494 | data += 4; |
| 495 | size -= 4; |
| 496 | |
| 497 | if (size) { |
| 498 | size -= 4; |
| 499 | |
| 500 | while (size) { |
| 501 | if (size > limit) { |
| 502 | ret = set_registers(tp, index, |
| 503 | type | BYTE_EN_DWORD, |
| 504 | limit, data); |
| 505 | if (ret < 0) |
| 506 | goto error1; |
| 507 | |
| 508 | index += limit; |
| 509 | data += limit; |
| 510 | size -= limit; |
| 511 | } else { |
| 512 | ret = set_registers(tp, index, |
| 513 | type | BYTE_EN_DWORD, |
| 514 | size, data); |
| 515 | if (ret < 0) |
| 516 | goto error1; |
| 517 | |
| 518 | index += size; |
| 519 | data += size; |
| 520 | size = 0; |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | byen = byteen_end | (byteen_end >> 4); |
| 526 | ret = set_registers(tp, index, type | byen, 4, data); |
| 527 | if (ret < 0) |
| 528 | goto error1; |
| 529 | } |
| 530 | |
| 531 | error1: |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | static inline |
| 536 | int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 537 | { |
| 538 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); |
| 539 | } |
| 540 | |
| 541 | static inline |
| 542 | int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 543 | { |
| 544 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); |
| 545 | } |
| 546 | |
| 547 | static inline |
| 548 | int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 549 | { |
| 550 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB); |
| 551 | } |
| 552 | |
| 553 | static inline |
| 554 | int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 555 | { |
| 556 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); |
| 557 | } |
| 558 | |
| 559 | static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) |
| 560 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 561 | __le32 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 562 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 563 | generic_ocp_read(tp, index, sizeof(data), &data, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 564 | |
| 565 | return __le32_to_cpu(data); |
| 566 | } |
| 567 | |
| 568 | static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 569 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 570 | __le32 tmp = __cpu_to_le32(data); |
| 571 | |
| 572 | generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) |
| 576 | { |
| 577 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 578 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 579 | u8 shift = index & 2; |
| 580 | |
| 581 | index &= ~3; |
| 582 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 583 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 584 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 585 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 586 | data >>= (shift * 8); |
| 587 | data &= 0xffff; |
| 588 | |
| 589 | return (u16)data; |
| 590 | } |
| 591 | |
| 592 | static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 593 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 594 | u32 mask = 0xffff; |
| 595 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 596 | u16 byen = BYTE_EN_WORD; |
| 597 | u8 shift = index & 2; |
| 598 | |
| 599 | data &= mask; |
| 600 | |
| 601 | if (index & 2) { |
| 602 | byen <<= shift; |
| 603 | mask <<= (shift * 8); |
| 604 | data <<= (shift * 8); |
| 605 | index &= ~3; |
| 606 | } |
| 607 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 608 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 609 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 610 | data |= __le32_to_cpu(tmp) & ~mask; |
| 611 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 612 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 613 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) |
| 617 | { |
| 618 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 619 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 620 | u8 shift = index & 3; |
| 621 | |
| 622 | index &= ~3; |
| 623 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 624 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 625 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 626 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 627 | data >>= (shift * 8); |
| 628 | data &= 0xff; |
| 629 | |
| 630 | return (u8)data; |
| 631 | } |
| 632 | |
| 633 | static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 634 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 635 | u32 mask = 0xff; |
| 636 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 637 | u16 byen = BYTE_EN_BYTE; |
| 638 | u8 shift = index & 3; |
| 639 | |
| 640 | data &= mask; |
| 641 | |
| 642 | if (index & 3) { |
| 643 | byen <<= shift; |
| 644 | mask <<= (shift * 8); |
| 645 | data <<= (shift * 8); |
| 646 | index &= ~3; |
| 647 | } |
| 648 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 649 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 650 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 651 | data |= __le32_to_cpu(tmp) & ~mask; |
| 652 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 653 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 654 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 655 | } |
| 656 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame^] | 657 | static u16 ocp_reg_read(struct r8152 *tp, u16 addr) |
| 658 | { |
| 659 | u16 ocp_base, ocp_index; |
| 660 | |
| 661 | ocp_base = addr & 0xf000; |
| 662 | if (ocp_base != tp->ocp_base) { |
| 663 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 664 | tp->ocp_base = ocp_base; |
| 665 | } |
| 666 | |
| 667 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 668 | return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); |
| 669 | } |
| 670 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 671 | static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) |
| 672 | { |
| 673 | u16 ocp_base, ocp_index; |
| 674 | |
| 675 | ocp_base = addr & 0xf000; |
| 676 | if (ocp_base != tp->ocp_base) { |
| 677 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 678 | tp->ocp_base = ocp_base; |
| 679 | } |
| 680 | |
| 681 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 682 | ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); |
| 683 | } |
| 684 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame^] | 685 | 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] | 686 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame^] | 687 | ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 688 | } |
| 689 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame^] | 690 | static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 691 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame^] | 692 | return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | static int read_mii_word(struct net_device *netdev, int phy_id, int reg) |
| 696 | { |
| 697 | struct r8152 *tp = netdev_priv(netdev); |
| 698 | |
| 699 | if (phy_id != R8152_PHY_ID) |
| 700 | return -EINVAL; |
| 701 | |
| 702 | return r8152_mdio_read(tp, reg); |
| 703 | } |
| 704 | |
| 705 | static |
| 706 | void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) |
| 707 | { |
| 708 | struct r8152 *tp = netdev_priv(netdev); |
| 709 | |
| 710 | if (phy_id != R8152_PHY_ID) |
| 711 | return; |
| 712 | |
| 713 | r8152_mdio_write(tp, reg, val); |
| 714 | } |
| 715 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 716 | static |
| 717 | int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags); |
| 718 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 719 | static inline void set_ethernet_addr(struct r8152 *tp) |
| 720 | { |
| 721 | struct net_device *dev = tp->netdev; |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 722 | u8 node_id[8] = {0}; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 723 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 724 | if (pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id) < 0) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 725 | netif_notice(tp, probe, dev, "inet addr fail\n"); |
| 726 | else { |
| 727 | memcpy(dev->dev_addr, node_id, dev->addr_len); |
| 728 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); |
| 729 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static int rtl8152_set_mac_address(struct net_device *netdev, void *p) |
| 733 | { |
| 734 | struct r8152 *tp = netdev_priv(netdev); |
| 735 | struct sockaddr *addr = p; |
| 736 | |
| 737 | if (!is_valid_ether_addr(addr->sa_data)) |
| 738 | return -EADDRNOTAVAIL; |
| 739 | |
| 740 | memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); |
| 741 | |
| 742 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 743 | pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data); |
| 744 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 749 | static struct net_device_stats *rtl8152_get_stats(struct net_device *dev) |
| 750 | { |
| 751 | return &dev->stats; |
| 752 | } |
| 753 | |
| 754 | static void read_bulk_callback(struct urb *urb) |
| 755 | { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 756 | struct net_device *netdev; |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 757 | unsigned long flags; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 758 | int status = urb->status; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 759 | struct rx_agg *agg; |
| 760 | struct r8152 *tp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 761 | int result; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 762 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 763 | agg = urb->context; |
| 764 | if (!agg) |
| 765 | return; |
| 766 | |
| 767 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 768 | if (!tp) |
| 769 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 770 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 771 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 772 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 773 | |
| 774 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 775 | return; |
| 776 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 777 | netdev = tp->netdev; |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 778 | |
| 779 | /* When link down, the driver would cancel all bulks. */ |
| 780 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 781 | if (!netif_carrier_ok(netdev)) |
| 782 | return; |
| 783 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 784 | switch (status) { |
| 785 | case 0: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 786 | if (urb->actual_length < ETH_ZLEN) |
| 787 | break; |
| 788 | |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 789 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 790 | list_add_tail(&agg->list, &tp->rx_done); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 791 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 792 | tasklet_schedule(&tp->tl); |
| 793 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 794 | case -ESHUTDOWN: |
| 795 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 796 | netif_device_detach(tp->netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 797 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 798 | case -ENOENT: |
| 799 | return; /* the urb is in unlink state */ |
| 800 | case -ETIME: |
| 801 | pr_warn_ratelimited("may be reset is needed?..\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 802 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 803 | default: |
| 804 | pr_warn_ratelimited("Rx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 805 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 806 | } |
| 807 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 808 | result = r8152_submit_rx(tp, agg, GFP_ATOMIC); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 809 | if (result == -ENODEV) { |
| 810 | netif_device_detach(tp->netdev); |
| 811 | } else if (result) { |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 812 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 813 | list_add_tail(&agg->list, &tp->rx_done); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 814 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 815 | tasklet_schedule(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 816 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | static void write_bulk_callback(struct urb *urb) |
| 820 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 821 | struct net_device_stats *stats; |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 822 | unsigned long flags; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 823 | struct tx_agg *agg; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 824 | struct r8152 *tp; |
| 825 | int status = urb->status; |
| 826 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 827 | agg = urb->context; |
| 828 | if (!agg) |
| 829 | return; |
| 830 | |
| 831 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 832 | if (!tp) |
| 833 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 834 | |
| 835 | stats = rtl8152_get_stats(tp->netdev); |
| 836 | if (status) { |
| 837 | pr_warn_ratelimited("Tx status %d\n", status); |
| 838 | stats->tx_errors += agg->skb_num; |
| 839 | } else { |
| 840 | stats->tx_packets += agg->skb_num; |
| 841 | stats->tx_bytes += agg->skb_len; |
| 842 | } |
| 843 | |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 844 | spin_lock_irqsave(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 845 | list_add_tail(&agg->list, &tp->tx_free); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 846 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 847 | |
| 848 | if (!netif_carrier_ok(tp->netdev)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 849 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 850 | |
| 851 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 852 | return; |
| 853 | |
| 854 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 855 | return; |
| 856 | |
| 857 | if (!skb_queue_empty(&tp->tx_queue)) |
| 858 | tasklet_schedule(&tp->tl); |
| 859 | } |
| 860 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 861 | static void intr_callback(struct urb *urb) |
| 862 | { |
| 863 | struct r8152 *tp; |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 864 | __le16 *d; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 865 | int status = urb->status; |
| 866 | int res; |
| 867 | |
| 868 | tp = urb->context; |
| 869 | if (!tp) |
| 870 | return; |
| 871 | |
| 872 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 873 | return; |
| 874 | |
| 875 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 876 | return; |
| 877 | |
| 878 | switch (status) { |
| 879 | case 0: /* success */ |
| 880 | break; |
| 881 | case -ECONNRESET: /* unlink */ |
| 882 | case -ESHUTDOWN: |
| 883 | netif_device_detach(tp->netdev); |
| 884 | case -ENOENT: |
| 885 | return; |
| 886 | case -EOVERFLOW: |
| 887 | netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n"); |
| 888 | goto resubmit; |
| 889 | /* -EPIPE: should clear the halt */ |
| 890 | default: |
| 891 | netif_info(tp, intr, tp->netdev, "intr status %d\n", status); |
| 892 | goto resubmit; |
| 893 | } |
| 894 | |
| 895 | d = urb->transfer_buffer; |
| 896 | if (INTR_LINK & __le16_to_cpu(d[0])) { |
| 897 | if (!(tp->speed & LINK_STATUS)) { |
| 898 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 899 | schedule_delayed_work(&tp->schedule, 0); |
| 900 | } |
| 901 | } else { |
| 902 | if (tp->speed & LINK_STATUS) { |
| 903 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 904 | schedule_delayed_work(&tp->schedule, 0); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | resubmit: |
| 909 | res = usb_submit_urb(urb, GFP_ATOMIC); |
| 910 | if (res == -ENODEV) |
| 911 | netif_device_detach(tp->netdev); |
| 912 | else if (res) |
| 913 | netif_err(tp, intr, tp->netdev, |
| 914 | "can't resubmit intr, status %d\n", res); |
| 915 | } |
| 916 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 917 | static inline void *rx_agg_align(void *data) |
| 918 | { |
| 919 | return (void *)ALIGN((uintptr_t)data, 8); |
| 920 | } |
| 921 | |
| 922 | static inline void *tx_agg_align(void *data) |
| 923 | { |
| 924 | return (void *)ALIGN((uintptr_t)data, 4); |
| 925 | } |
| 926 | |
| 927 | static void free_all_mem(struct r8152 *tp) |
| 928 | { |
| 929 | int i; |
| 930 | |
| 931 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
| 932 | if (tp->rx_info[i].urb) { |
| 933 | usb_free_urb(tp->rx_info[i].urb); |
| 934 | tp->rx_info[i].urb = NULL; |
| 935 | } |
| 936 | |
| 937 | if (tp->rx_info[i].buffer) { |
| 938 | kfree(tp->rx_info[i].buffer); |
| 939 | tp->rx_info[i].buffer = NULL; |
| 940 | tp->rx_info[i].head = NULL; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
| 945 | if (tp->tx_info[i].urb) { |
| 946 | usb_free_urb(tp->tx_info[i].urb); |
| 947 | tp->tx_info[i].urb = NULL; |
| 948 | } |
| 949 | |
| 950 | if (tp->tx_info[i].buffer) { |
| 951 | kfree(tp->tx_info[i].buffer); |
| 952 | tp->tx_info[i].buffer = NULL; |
| 953 | tp->tx_info[i].head = NULL; |
| 954 | } |
| 955 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 956 | |
| 957 | if (tp->intr_urb) { |
| 958 | usb_free_urb(tp->intr_urb); |
| 959 | tp->intr_urb = NULL; |
| 960 | } |
| 961 | |
| 962 | if (tp->intr_buff) { |
| 963 | kfree(tp->intr_buff); |
| 964 | tp->intr_buff = NULL; |
| 965 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | static int alloc_all_mem(struct r8152 *tp) |
| 969 | { |
| 970 | struct net_device *netdev = tp->netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 971 | struct usb_interface *intf = tp->intf; |
| 972 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 973 | struct usb_host_endpoint *ep_intr = alt->endpoint + 2; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 974 | struct urb *urb; |
| 975 | int node, i; |
| 976 | u8 *buf; |
| 977 | |
| 978 | node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; |
| 979 | |
| 980 | spin_lock_init(&tp->rx_lock); |
| 981 | spin_lock_init(&tp->tx_lock); |
| 982 | INIT_LIST_HEAD(&tp->rx_done); |
| 983 | INIT_LIST_HEAD(&tp->tx_free); |
| 984 | skb_queue_head_init(&tp->tx_queue); |
| 985 | |
| 986 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
| 987 | buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node); |
| 988 | if (!buf) |
| 989 | goto err1; |
| 990 | |
| 991 | if (buf != rx_agg_align(buf)) { |
| 992 | kfree(buf); |
| 993 | buf = kmalloc_node(rx_buf_sz + 8, GFP_KERNEL, node); |
| 994 | if (!buf) |
| 995 | goto err1; |
| 996 | } |
| 997 | |
| 998 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 999 | if (!urb) { |
| 1000 | kfree(buf); |
| 1001 | goto err1; |
| 1002 | } |
| 1003 | |
| 1004 | INIT_LIST_HEAD(&tp->rx_info[i].list); |
| 1005 | tp->rx_info[i].context = tp; |
| 1006 | tp->rx_info[i].urb = urb; |
| 1007 | tp->rx_info[i].buffer = buf; |
| 1008 | tp->rx_info[i].head = rx_agg_align(buf); |
| 1009 | } |
| 1010 | |
| 1011 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
| 1012 | buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node); |
| 1013 | if (!buf) |
| 1014 | goto err1; |
| 1015 | |
| 1016 | if (buf != tx_agg_align(buf)) { |
| 1017 | kfree(buf); |
| 1018 | buf = kmalloc_node(rx_buf_sz + 4, GFP_KERNEL, node); |
| 1019 | if (!buf) |
| 1020 | goto err1; |
| 1021 | } |
| 1022 | |
| 1023 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1024 | if (!urb) { |
| 1025 | kfree(buf); |
| 1026 | goto err1; |
| 1027 | } |
| 1028 | |
| 1029 | INIT_LIST_HEAD(&tp->tx_info[i].list); |
| 1030 | tp->tx_info[i].context = tp; |
| 1031 | tp->tx_info[i].urb = urb; |
| 1032 | tp->tx_info[i].buffer = buf; |
| 1033 | tp->tx_info[i].head = tx_agg_align(buf); |
| 1034 | |
| 1035 | list_add_tail(&tp->tx_info[i].list, &tp->tx_free); |
| 1036 | } |
| 1037 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1038 | tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1039 | if (!tp->intr_urb) |
| 1040 | goto err1; |
| 1041 | |
| 1042 | tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); |
| 1043 | if (!tp->intr_buff) |
| 1044 | goto err1; |
| 1045 | |
| 1046 | tp->intr_interval = (int)ep_intr->desc.bInterval; |
| 1047 | usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3), |
| 1048 | tp->intr_buff, INTBUFSIZE, intr_callback, |
| 1049 | tp, tp->intr_interval); |
| 1050 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1051 | return 0; |
| 1052 | |
| 1053 | err1: |
| 1054 | free_all_mem(tp); |
| 1055 | return -ENOMEM; |
| 1056 | } |
| 1057 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1058 | static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp) |
| 1059 | { |
| 1060 | struct tx_agg *agg = NULL; |
| 1061 | unsigned long flags; |
| 1062 | |
| 1063 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 1064 | if (!list_empty(&tp->tx_free)) { |
| 1065 | struct list_head *cursor; |
| 1066 | |
| 1067 | cursor = tp->tx_free.next; |
| 1068 | list_del_init(cursor); |
| 1069 | agg = list_entry(cursor, struct tx_agg, list); |
| 1070 | } |
| 1071 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 1072 | |
| 1073 | return agg; |
| 1074 | } |
| 1075 | |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1076 | static void |
| 1077 | r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb) |
| 1078 | { |
| 1079 | memset(desc, 0, sizeof(*desc)); |
| 1080 | |
| 1081 | desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS); |
| 1082 | |
| 1083 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1084 | __be16 protocol; |
| 1085 | u8 ip_protocol; |
| 1086 | u32 opts2 = 0; |
| 1087 | |
| 1088 | if (skb->protocol == htons(ETH_P_8021Q)) |
| 1089 | protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto; |
| 1090 | else |
| 1091 | protocol = skb->protocol; |
| 1092 | |
| 1093 | switch (protocol) { |
| 1094 | case htons(ETH_P_IP): |
| 1095 | opts2 |= IPV4_CS; |
| 1096 | ip_protocol = ip_hdr(skb)->protocol; |
| 1097 | break; |
| 1098 | |
| 1099 | case htons(ETH_P_IPV6): |
| 1100 | opts2 |= IPV6_CS; |
| 1101 | ip_protocol = ipv6_hdr(skb)->nexthdr; |
| 1102 | break; |
| 1103 | |
| 1104 | default: |
| 1105 | ip_protocol = IPPROTO_RAW; |
| 1106 | break; |
| 1107 | } |
| 1108 | |
| 1109 | if (ip_protocol == IPPROTO_TCP) { |
| 1110 | opts2 |= TCP_CS; |
| 1111 | opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17; |
| 1112 | } else if (ip_protocol == IPPROTO_UDP) { |
| 1113 | opts2 |= UDP_CS; |
| 1114 | } else { |
| 1115 | WARN_ON_ONCE(1); |
| 1116 | } |
| 1117 | |
| 1118 | desc->opts2 = cpu_to_le32(opts2); |
| 1119 | } |
| 1120 | } |
| 1121 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1122 | static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg) |
| 1123 | { |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1124 | int remain; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1125 | u8 *tx_data; |
| 1126 | |
| 1127 | tx_data = agg->head; |
| 1128 | agg->skb_num = agg->skb_len = 0; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1129 | remain = rx_buf_sz; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1130 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1131 | while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) { |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1132 | struct tx_desc *tx_desc; |
| 1133 | struct sk_buff *skb; |
| 1134 | unsigned int len; |
| 1135 | |
| 1136 | skb = skb_dequeue(&tp->tx_queue); |
| 1137 | if (!skb) |
| 1138 | break; |
| 1139 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1140 | remain -= sizeof(*tx_desc); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1141 | len = skb->len; |
| 1142 | if (remain < len) { |
| 1143 | skb_queue_head(&tp->tx_queue, skb); |
| 1144 | break; |
| 1145 | } |
| 1146 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1147 | tx_data = tx_agg_align(tx_data); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1148 | tx_desc = (struct tx_desc *)tx_data; |
| 1149 | tx_data += sizeof(*tx_desc); |
| 1150 | |
| 1151 | r8152_tx_csum(tp, tx_desc, skb); |
| 1152 | memcpy(tx_data, skb->data, len); |
| 1153 | agg->skb_num++; |
| 1154 | agg->skb_len += len; |
| 1155 | dev_kfree_skb_any(skb); |
| 1156 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1157 | tx_data += len; |
| 1158 | remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1159 | } |
| 1160 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1161 | netif_tx_lock(tp->netdev); |
| 1162 | |
| 1163 | if (netif_queue_stopped(tp->netdev) && |
| 1164 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) |
| 1165 | netif_wake_queue(tp->netdev); |
| 1166 | |
| 1167 | netif_tx_unlock(tp->netdev); |
| 1168 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1169 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2), |
| 1170 | agg->head, (int)(tx_data - (u8 *)agg->head), |
| 1171 | (usb_complete_t)write_bulk_callback, agg); |
| 1172 | |
| 1173 | return usb_submit_urb(agg->urb, GFP_ATOMIC); |
| 1174 | } |
| 1175 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1176 | static void rx_bottom(struct r8152 *tp) |
| 1177 | { |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1178 | unsigned long flags; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1179 | struct list_head *cursor, *next; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1180 | |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1181 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1182 | list_for_each_safe(cursor, next, &tp->rx_done) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1183 | struct rx_desc *rx_desc; |
| 1184 | struct rx_agg *agg; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1185 | int len_used = 0; |
| 1186 | struct urb *urb; |
| 1187 | u8 *rx_data; |
| 1188 | int ret; |
| 1189 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1190 | list_del_init(cursor); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1191 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1192 | |
| 1193 | agg = list_entry(cursor, struct rx_agg, list); |
| 1194 | urb = agg->urb; |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1195 | if (urb->actual_length < ETH_ZLEN) |
| 1196 | goto submit; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1197 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1198 | rx_desc = agg->head; |
| 1199 | rx_data = agg->head; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1200 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1201 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1202 | while (urb->actual_length > len_used) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1203 | struct net_device *netdev = tp->netdev; |
| 1204 | struct net_device_stats *stats; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1205 | unsigned int pkt_len; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1206 | struct sk_buff *skb; |
| 1207 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1208 | pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1209 | if (pkt_len < ETH_ZLEN) |
| 1210 | break; |
| 1211 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1212 | len_used += pkt_len; |
| 1213 | if (urb->actual_length < len_used) |
| 1214 | break; |
| 1215 | |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1216 | stats = rtl8152_get_stats(netdev); |
| 1217 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1218 | pkt_len -= 4; /* CRC */ |
| 1219 | rx_data += sizeof(struct rx_desc); |
| 1220 | |
| 1221 | skb = netdev_alloc_skb_ip_align(netdev, pkt_len); |
| 1222 | if (!skb) { |
| 1223 | stats->rx_dropped++; |
| 1224 | break; |
| 1225 | } |
| 1226 | memcpy(skb->data, rx_data, pkt_len); |
| 1227 | skb_put(skb, pkt_len); |
| 1228 | skb->protocol = eth_type_trans(skb, netdev); |
| 1229 | netif_rx(skb); |
| 1230 | stats->rx_packets++; |
| 1231 | stats->rx_bytes += pkt_len; |
| 1232 | |
| 1233 | rx_data = rx_agg_align(rx_data + pkt_len + 4); |
| 1234 | rx_desc = (struct rx_desc *)rx_data; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1235 | len_used = (int)(rx_data - (u8 *)agg->head); |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1236 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1237 | } |
| 1238 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1239 | submit: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1240 | ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1241 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1242 | if (ret && ret != -ENODEV) { |
| 1243 | list_add_tail(&agg->list, next); |
| 1244 | tasklet_schedule(&tp->tl); |
| 1245 | } |
| 1246 | } |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1247 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | static void tx_bottom(struct r8152 *tp) |
| 1251 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1252 | int res; |
| 1253 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1254 | do { |
| 1255 | struct tx_agg *agg; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1256 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1257 | if (skb_queue_empty(&tp->tx_queue)) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1258 | break; |
| 1259 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1260 | agg = r8152_get_tx_agg(tp); |
| 1261 | if (!agg) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1262 | break; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1263 | |
| 1264 | res = r8152_tx_agg_fill(tp, agg); |
| 1265 | if (res) { |
| 1266 | struct net_device_stats *stats; |
| 1267 | struct net_device *netdev; |
| 1268 | unsigned long flags; |
| 1269 | |
| 1270 | netdev = tp->netdev; |
| 1271 | stats = rtl8152_get_stats(netdev); |
| 1272 | |
| 1273 | if (res == -ENODEV) { |
| 1274 | netif_device_detach(netdev); |
| 1275 | } else { |
| 1276 | netif_warn(tp, tx_err, netdev, |
| 1277 | "failed tx_urb %d\n", res); |
| 1278 | stats->tx_dropped += agg->skb_num; |
| 1279 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 1280 | list_add_tail(&agg->list, &tp->tx_free); |
| 1281 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 1282 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1283 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1284 | } while (res == 0); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | static void bottom_half(unsigned long data) |
| 1288 | { |
| 1289 | struct r8152 *tp; |
| 1290 | |
| 1291 | tp = (struct r8152 *)data; |
| 1292 | |
| 1293 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1294 | return; |
| 1295 | |
| 1296 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1297 | return; |
| 1298 | |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 1299 | /* When link down, the driver would cancel all bulks. */ |
| 1300 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1301 | if (!netif_carrier_ok(tp->netdev)) |
| 1302 | return; |
| 1303 | |
| 1304 | rx_bottom(tp); |
| 1305 | tx_bottom(tp); |
| 1306 | } |
| 1307 | |
| 1308 | static |
| 1309 | int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) |
| 1310 | { |
| 1311 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1), |
| 1312 | agg->head, rx_buf_sz, |
| 1313 | (usb_complete_t)read_bulk_callback, agg); |
| 1314 | |
| 1315 | return usb_submit_urb(agg->urb, mem_flags); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static void rtl8152_tx_timeout(struct net_device *netdev) |
| 1319 | { |
| 1320 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1321 | int i; |
| 1322 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1323 | netif_warn(tp, tx_err, netdev, "Tx timeout.\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1324 | for (i = 0; i < RTL8152_MAX_TX; i++) |
| 1325 | usb_unlink_urb(tp->tx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | static void rtl8152_set_rx_mode(struct net_device *netdev) |
| 1329 | { |
| 1330 | struct r8152 *tp = netdev_priv(netdev); |
| 1331 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1332 | if (tp->speed & LINK_STATUS) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1333 | set_bit(RTL8152_SET_RX_MODE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1334 | schedule_delayed_work(&tp->schedule, 0); |
| 1335 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | static void _rtl8152_set_rx_mode(struct net_device *netdev) |
| 1339 | { |
| 1340 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1341 | u32 mc_filter[2]; /* Multicast hash filter */ |
| 1342 | __le32 tmp[2]; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1343 | u32 ocp_data; |
| 1344 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1345 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 1346 | netif_stop_queue(netdev); |
| 1347 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 1348 | ocp_data &= ~RCR_ACPT_ALL; |
| 1349 | ocp_data |= RCR_AB | RCR_APM; |
| 1350 | |
| 1351 | if (netdev->flags & IFF_PROMISC) { |
| 1352 | /* Unconditionally log net taps. */ |
| 1353 | netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); |
| 1354 | ocp_data |= RCR_AM | RCR_AAP; |
| 1355 | mc_filter[1] = mc_filter[0] = 0xffffffff; |
| 1356 | } else if ((netdev_mc_count(netdev) > multicast_filter_limit) || |
| 1357 | (netdev->flags & IFF_ALLMULTI)) { |
| 1358 | /* Too many to filter perfectly -- accept all multicasts. */ |
| 1359 | ocp_data |= RCR_AM; |
| 1360 | mc_filter[1] = mc_filter[0] = 0xffffffff; |
| 1361 | } else { |
| 1362 | struct netdev_hw_addr *ha; |
| 1363 | |
| 1364 | mc_filter[1] = mc_filter[0] = 0; |
| 1365 | netdev_for_each_mc_addr(ha, netdev) { |
| 1366 | int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; |
| 1367 | mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); |
| 1368 | ocp_data |= RCR_AM; |
| 1369 | } |
| 1370 | } |
| 1371 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1372 | tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); |
| 1373 | tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1374 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1375 | pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1376 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 1377 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, |
| 1381 | struct net_device *netdev) |
| 1382 | { |
| 1383 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1384 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1385 | skb_tx_timestamp(skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1386 | |
hayeswang | 6159878 | 2013-11-20 17:30:55 +0800 | [diff] [blame] | 1387 | skb_queue_tail(&tp->tx_queue, skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1388 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1389 | if (list_empty(&tp->tx_free) && |
| 1390 | skb_queue_len(&tp->tx_queue) > tp->tx_qlen) |
| 1391 | netif_stop_queue(netdev); |
| 1392 | |
hayeswang | 6159878 | 2013-11-20 17:30:55 +0800 | [diff] [blame] | 1393 | if (!list_empty(&tp->tx_free)) |
| 1394 | tasklet_schedule(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1395 | |
| 1396 | return NETDEV_TX_OK; |
| 1397 | } |
| 1398 | |
| 1399 | static void r8152b_reset_packet_filter(struct r8152 *tp) |
| 1400 | { |
| 1401 | u32 ocp_data; |
| 1402 | |
| 1403 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); |
| 1404 | ocp_data &= ~FMC_FCR_MCU_EN; |
| 1405 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 1406 | ocp_data |= FMC_FCR_MCU_EN; |
| 1407 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 1408 | } |
| 1409 | |
| 1410 | static void rtl8152_nic_reset(struct r8152 *tp) |
| 1411 | { |
| 1412 | int i; |
| 1413 | |
| 1414 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); |
| 1415 | |
| 1416 | for (i = 0; i < 1000; i++) { |
| 1417 | if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) |
| 1418 | break; |
| 1419 | udelay(100); |
| 1420 | } |
| 1421 | } |
| 1422 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1423 | static void set_tx_qlen(struct r8152 *tp) |
| 1424 | { |
| 1425 | struct net_device *netdev = tp->netdev; |
| 1426 | |
| 1427 | tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN + |
| 1428 | sizeof(struct tx_desc)); |
| 1429 | } |
| 1430 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1431 | static inline u8 rtl8152_get_speed(struct r8152 *tp) |
| 1432 | { |
| 1433 | return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); |
| 1434 | } |
| 1435 | |
| 1436 | static int rtl8152_enable(struct r8152 *tp) |
| 1437 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1438 | u32 ocp_data; |
| 1439 | int i, ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1440 | u8 speed; |
| 1441 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1442 | set_tx_qlen(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1443 | speed = rtl8152_get_speed(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1444 | if (speed & _10bps) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1445 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1446 | ocp_data |= EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1447 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 1448 | } else { |
| 1449 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1450 | ocp_data &= ~EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1451 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 1452 | } |
| 1453 | |
| 1454 | r8152b_reset_packet_filter(tp); |
| 1455 | |
| 1456 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); |
| 1457 | ocp_data |= CR_RE | CR_TE; |
| 1458 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); |
| 1459 | |
| 1460 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 1461 | ocp_data &= ~RXDY_GATED_EN; |
| 1462 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 1463 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1464 | INIT_LIST_HEAD(&tp->rx_done); |
| 1465 | ret = 0; |
| 1466 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
| 1467 | INIT_LIST_HEAD(&tp->rx_info[i].list); |
| 1468 | ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL); |
| 1469 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1470 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1471 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | static void rtl8152_disable(struct r8152 *tp) |
| 1475 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1476 | struct net_device_stats *stats = rtl8152_get_stats(tp->netdev); |
| 1477 | struct sk_buff *skb; |
| 1478 | u32 ocp_data; |
| 1479 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1480 | |
| 1481 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 1482 | ocp_data &= ~RCR_ACPT_ALL; |
| 1483 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 1484 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1485 | while ((skb = skb_dequeue(&tp->tx_queue))) { |
| 1486 | dev_kfree_skb(skb); |
| 1487 | stats->tx_dropped++; |
| 1488 | } |
| 1489 | |
| 1490 | for (i = 0; i < RTL8152_MAX_TX; i++) |
| 1491 | usb_kill_urb(tp->tx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1492 | |
| 1493 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 1494 | ocp_data |= RXDY_GATED_EN; |
| 1495 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 1496 | |
| 1497 | for (i = 0; i < 1000; i++) { |
| 1498 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1499 | if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) |
| 1500 | break; |
| 1501 | mdelay(1); |
| 1502 | } |
| 1503 | |
| 1504 | for (i = 0; i < 1000; i++) { |
| 1505 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) |
| 1506 | break; |
| 1507 | mdelay(1); |
| 1508 | } |
| 1509 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1510 | for (i = 0; i < RTL8152_MAX_RX; i++) |
| 1511 | usb_kill_urb(tp->rx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1512 | |
| 1513 | rtl8152_nic_reset(tp); |
| 1514 | } |
| 1515 | |
| 1516 | static void r8152b_exit_oob(struct r8152 *tp) |
| 1517 | { |
| 1518 | u32 ocp_data; |
| 1519 | int i; |
| 1520 | |
| 1521 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 1522 | ocp_data &= ~RCR_ACPT_ALL; |
| 1523 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 1524 | |
| 1525 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 1526 | ocp_data |= RXDY_GATED_EN; |
| 1527 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 1528 | |
| 1529 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 1530 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); |
| 1531 | |
| 1532 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1533 | ocp_data &= ~NOW_IS_OOB; |
| 1534 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 1535 | |
| 1536 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 1537 | ocp_data &= ~MCU_BORW_EN; |
| 1538 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 1539 | |
| 1540 | for (i = 0; i < 1000; i++) { |
| 1541 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1542 | if (ocp_data & LINK_LIST_READY) |
| 1543 | break; |
| 1544 | mdelay(1); |
| 1545 | } |
| 1546 | |
| 1547 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 1548 | ocp_data |= RE_INIT_LL; |
| 1549 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 1550 | |
| 1551 | for (i = 0; i < 1000; i++) { |
| 1552 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1553 | if (ocp_data & LINK_LIST_READY) |
| 1554 | break; |
| 1555 | mdelay(1); |
| 1556 | } |
| 1557 | |
| 1558 | rtl8152_nic_reset(tp); |
| 1559 | |
| 1560 | /* rx share fifo credit full threshold */ |
| 1561 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 1562 | |
| 1563 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT); |
| 1564 | ocp_data &= STAT_SPEED_MASK; |
| 1565 | if (ocp_data == STAT_SPEED_FULL) { |
| 1566 | /* rx share fifo credit near full threshold */ |
| 1567 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 1568 | RXFIFO_THR2_FULL); |
| 1569 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 1570 | RXFIFO_THR3_FULL); |
| 1571 | } else { |
| 1572 | /* rx share fifo credit near full threshold */ |
| 1573 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 1574 | RXFIFO_THR2_HIGH); |
| 1575 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 1576 | RXFIFO_THR3_HIGH); |
| 1577 | } |
| 1578 | |
| 1579 | /* TX share fifo free credit full threshold */ |
| 1580 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); |
| 1581 | |
| 1582 | ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); |
| 1583 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_BUF_THR); |
| 1584 | ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, |
| 1585 | TEST_MODE_DISABLE | TX_SIZE_ADJUST1); |
| 1586 | |
| 1587 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 1588 | ocp_data &= ~CPCR_RX_VLAN; |
| 1589 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 1590 | |
| 1591 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 1592 | |
| 1593 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 1594 | ocp_data |= TCR0_AUTO_FIFO; |
| 1595 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 1596 | } |
| 1597 | |
| 1598 | static void r8152b_enter_oob(struct r8152 *tp) |
| 1599 | { |
| 1600 | u32 ocp_data; |
| 1601 | int i; |
| 1602 | |
| 1603 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1604 | ocp_data &= ~NOW_IS_OOB; |
| 1605 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 1606 | |
| 1607 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); |
| 1608 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); |
| 1609 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); |
| 1610 | |
| 1611 | rtl8152_disable(tp); |
| 1612 | |
| 1613 | for (i = 0; i < 1000; i++) { |
| 1614 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1615 | if (ocp_data & LINK_LIST_READY) |
| 1616 | break; |
| 1617 | mdelay(1); |
| 1618 | } |
| 1619 | |
| 1620 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 1621 | ocp_data |= RE_INIT_LL; |
| 1622 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 1623 | |
| 1624 | for (i = 0; i < 1000; i++) { |
| 1625 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1626 | if (ocp_data & LINK_LIST_READY) |
| 1627 | break; |
| 1628 | mdelay(1); |
| 1629 | } |
| 1630 | |
| 1631 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 1632 | |
| 1633 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 1634 | ocp_data |= MAGIC_EN; |
| 1635 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); |
| 1636 | |
| 1637 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 1638 | ocp_data |= CPCR_RX_VLAN; |
| 1639 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 1640 | |
| 1641 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR); |
| 1642 | ocp_data |= ALDPS_PROXY_MODE; |
| 1643 | ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data); |
| 1644 | |
| 1645 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1646 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 1647 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 1648 | |
| 1649 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN); |
| 1650 | |
| 1651 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 1652 | ocp_data &= ~RXDY_GATED_EN; |
| 1653 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 1654 | |
| 1655 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 1656 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 1657 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 1658 | } |
| 1659 | |
| 1660 | static void r8152b_disable_aldps(struct r8152 *tp) |
| 1661 | { |
| 1662 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE); |
| 1663 | msleep(20); |
| 1664 | } |
| 1665 | |
| 1666 | static inline void r8152b_enable_aldps(struct r8152 *tp) |
| 1667 | { |
| 1668 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | |
| 1669 | LINKENA | DIS_SDSAVE); |
| 1670 | } |
| 1671 | |
| 1672 | static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex) |
| 1673 | { |
| 1674 | u16 bmcr, anar; |
| 1675 | int ret = 0; |
| 1676 | |
| 1677 | cancel_delayed_work_sync(&tp->schedule); |
| 1678 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 1679 | anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL | |
| 1680 | ADVERTISE_100HALF | ADVERTISE_100FULL); |
| 1681 | |
| 1682 | if (autoneg == AUTONEG_DISABLE) { |
| 1683 | if (speed == SPEED_10) { |
| 1684 | bmcr = 0; |
| 1685 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 1686 | } else if (speed == SPEED_100) { |
| 1687 | bmcr = BMCR_SPEED100; |
| 1688 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 1689 | } else { |
| 1690 | ret = -EINVAL; |
| 1691 | goto out; |
| 1692 | } |
| 1693 | |
| 1694 | if (duplex == DUPLEX_FULL) |
| 1695 | bmcr |= BMCR_FULLDPLX; |
| 1696 | } else { |
| 1697 | if (speed == SPEED_10) { |
| 1698 | if (duplex == DUPLEX_FULL) |
| 1699 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 1700 | else |
| 1701 | anar |= ADVERTISE_10HALF; |
| 1702 | } else if (speed == SPEED_100) { |
| 1703 | if (duplex == DUPLEX_FULL) { |
| 1704 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 1705 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 1706 | } else { |
| 1707 | anar |= ADVERTISE_10HALF; |
| 1708 | anar |= ADVERTISE_100HALF; |
| 1709 | } |
| 1710 | } else { |
| 1711 | ret = -EINVAL; |
| 1712 | goto out; |
| 1713 | } |
| 1714 | |
| 1715 | bmcr = BMCR_ANENABLE | BMCR_ANRESTART; |
| 1716 | } |
| 1717 | |
| 1718 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 1719 | r8152_mdio_write(tp, MII_BMCR, bmcr); |
| 1720 | |
| 1721 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1722 | |
| 1723 | return ret; |
| 1724 | } |
| 1725 | |
| 1726 | static void rtl8152_down(struct r8152 *tp) |
| 1727 | { |
| 1728 | u32 ocp_data; |
| 1729 | |
| 1730 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 1731 | ocp_data &= ~POWER_CUT; |
| 1732 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 1733 | |
| 1734 | r8152b_disable_aldps(tp); |
| 1735 | r8152b_enter_oob(tp); |
| 1736 | r8152b_enable_aldps(tp); |
| 1737 | } |
| 1738 | |
| 1739 | static void set_carrier(struct r8152 *tp) |
| 1740 | { |
| 1741 | struct net_device *netdev = tp->netdev; |
| 1742 | u8 speed; |
| 1743 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1744 | clear_bit(RTL8152_LINK_CHG, &tp->flags); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1745 | speed = rtl8152_get_speed(tp); |
| 1746 | |
| 1747 | if (speed & LINK_STATUS) { |
| 1748 | if (!(tp->speed & LINK_STATUS)) { |
| 1749 | rtl8152_enable(tp); |
| 1750 | set_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 1751 | netif_carrier_on(netdev); |
| 1752 | } |
| 1753 | } else { |
| 1754 | if (tp->speed & LINK_STATUS) { |
| 1755 | netif_carrier_off(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1756 | tasklet_disable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1757 | rtl8152_disable(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1758 | tasklet_enable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | tp->speed = speed; |
| 1762 | } |
| 1763 | |
| 1764 | static void rtl_work_func_t(struct work_struct *work) |
| 1765 | { |
| 1766 | struct r8152 *tp = container_of(work, struct r8152, schedule.work); |
| 1767 | |
| 1768 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1769 | goto out1; |
| 1770 | |
| 1771 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1772 | goto out1; |
| 1773 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1774 | if (test_bit(RTL8152_LINK_CHG, &tp->flags)) |
| 1775 | set_carrier(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1776 | |
| 1777 | if (test_bit(RTL8152_SET_RX_MODE, &tp->flags)) |
| 1778 | _rtl8152_set_rx_mode(tp->netdev); |
| 1779 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1780 | out1: |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | static int rtl8152_open(struct net_device *netdev) |
| 1785 | { |
| 1786 | struct r8152 *tp = netdev_priv(netdev); |
| 1787 | int res = 0; |
| 1788 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1789 | res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
| 1790 | if (res) { |
| 1791 | if (res == -ENODEV) |
| 1792 | netif_device_detach(tp->netdev); |
| 1793 | netif_warn(tp, ifup, netdev, |
| 1794 | "intr_urb submit failed: %d\n", res); |
| 1795 | return res; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1799 | tp->speed = 0; |
| 1800 | netif_carrier_off(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1801 | netif_start_queue(netdev); |
| 1802 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1803 | |
| 1804 | return res; |
| 1805 | } |
| 1806 | |
| 1807 | static int rtl8152_close(struct net_device *netdev) |
| 1808 | { |
| 1809 | struct r8152 *tp = netdev_priv(netdev); |
| 1810 | int res = 0; |
| 1811 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1812 | usb_kill_urb(tp->intr_urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1813 | clear_bit(WORK_ENABLE, &tp->flags); |
| 1814 | cancel_delayed_work_sync(&tp->schedule); |
| 1815 | netif_stop_queue(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1816 | tasklet_disable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1817 | rtl8152_disable(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1818 | tasklet_enable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1819 | |
| 1820 | return res; |
| 1821 | } |
| 1822 | |
| 1823 | static void rtl_clear_bp(struct r8152 *tp) |
| 1824 | { |
| 1825 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0); |
| 1826 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0); |
| 1827 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0); |
| 1828 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0); |
| 1829 | ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0); |
| 1830 | ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0); |
| 1831 | ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0); |
| 1832 | ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0); |
| 1833 | mdelay(3); |
| 1834 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0); |
| 1835 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0); |
| 1836 | } |
| 1837 | |
| 1838 | static void r8152b_enable_eee(struct r8152 *tp) |
| 1839 | { |
| 1840 | u32 ocp_data; |
| 1841 | |
| 1842 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 1843 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 1844 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 1845 | ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN | |
| 1846 | EEE_10_CAP | EEE_NWAY_EN | |
| 1847 | TX_QUIET_EN | RX_QUIET_EN | |
| 1848 | SDRISETIME | RG_RXLPI_MSK_HFDUP | |
| 1849 | SDFALLTIME); |
| 1850 | ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN | |
| 1851 | RG_LDVQUIET_EN | RG_CKRSEL | |
| 1852 | RG_EEEPRG_EN); |
| 1853 | ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH); |
| 1854 | ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR); |
| 1855 | ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR); |
| 1856 | ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR); |
| 1857 | ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA); |
| 1858 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 1859 | } |
| 1860 | |
| 1861 | static void r8152b_enable_fc(struct r8152 *tp) |
| 1862 | { |
| 1863 | u16 anar; |
| 1864 | |
| 1865 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 1866 | anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; |
| 1867 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 1868 | } |
| 1869 | |
| 1870 | static void r8152b_hw_phy_cfg(struct r8152 *tp) |
| 1871 | { |
| 1872 | r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE); |
| 1873 | r8152b_disable_aldps(tp); |
| 1874 | } |
| 1875 | |
| 1876 | static void r8152b_init(struct r8152 *tp) |
| 1877 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1878 | u32 ocp_data; |
| 1879 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1880 | |
| 1881 | rtl_clear_bp(tp); |
| 1882 | |
| 1883 | if (tp->version == RTL_VER_01) { |
| 1884 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 1885 | ocp_data &= ~LED_MODE_MASK; |
| 1886 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 1887 | } |
| 1888 | |
| 1889 | r8152b_hw_phy_cfg(tp); |
| 1890 | |
| 1891 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 1892 | ocp_data &= ~POWER_CUT; |
| 1893 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 1894 | |
| 1895 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); |
| 1896 | ocp_data &= ~RWSUME_INDICATE; |
| 1897 | ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); |
| 1898 | |
| 1899 | r8152b_exit_oob(tp); |
| 1900 | |
| 1901 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 1902 | ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; |
| 1903 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 1904 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); |
| 1905 | ocp_data &= ~MCU_CLK_RATIO_MASK; |
| 1906 | ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; |
| 1907 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); |
| 1908 | ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | |
| 1909 | SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; |
| 1910 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); |
| 1911 | |
| 1912 | r8152b_enable_eee(tp); |
| 1913 | r8152b_enable_aldps(tp); |
| 1914 | r8152b_enable_fc(tp); |
| 1915 | |
| 1916 | r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE | |
| 1917 | BMCR_ANRESTART); |
| 1918 | for (i = 0; i < 100; i++) { |
| 1919 | udelay(100); |
| 1920 | if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET)) |
| 1921 | break; |
| 1922 | } |
| 1923 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1924 | /* enable rx aggregation */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1925 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1926 | ocp_data &= ~RX_AGG_DISABLE; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1927 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 1928 | } |
| 1929 | |
| 1930 | static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) |
| 1931 | { |
| 1932 | struct r8152 *tp = usb_get_intfdata(intf); |
| 1933 | |
| 1934 | netif_device_detach(tp->netdev); |
| 1935 | |
| 1936 | if (netif_running(tp->netdev)) { |
| 1937 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1938 | usb_kill_urb(tp->intr_urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1939 | cancel_delayed_work_sync(&tp->schedule); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1940 | tasklet_disable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | rtl8152_down(tp); |
| 1944 | |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
| 1948 | static int rtl8152_resume(struct usb_interface *intf) |
| 1949 | { |
| 1950 | struct r8152 *tp = usb_get_intfdata(intf); |
| 1951 | |
| 1952 | r8152b_init(tp); |
| 1953 | netif_device_attach(tp->netdev); |
| 1954 | if (netif_running(tp->netdev)) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1955 | rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL); |
| 1956 | tp->speed = 0; |
| 1957 | netif_carrier_off(tp->netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1958 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1959 | usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1960 | tasklet_enable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | return 0; |
| 1964 | } |
| 1965 | |
| 1966 | static void rtl8152_get_drvinfo(struct net_device *netdev, |
| 1967 | struct ethtool_drvinfo *info) |
| 1968 | { |
| 1969 | struct r8152 *tp = netdev_priv(netdev); |
| 1970 | |
| 1971 | strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN); |
| 1972 | strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN); |
| 1973 | usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); |
| 1974 | } |
| 1975 | |
| 1976 | static |
| 1977 | int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd) |
| 1978 | { |
| 1979 | struct r8152 *tp = netdev_priv(netdev); |
| 1980 | |
| 1981 | if (!tp->mii.mdio_read) |
| 1982 | return -EOPNOTSUPP; |
| 1983 | |
| 1984 | return mii_ethtool_gset(&tp->mii, cmd); |
| 1985 | } |
| 1986 | |
| 1987 | static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1988 | { |
| 1989 | struct r8152 *tp = netdev_priv(dev); |
| 1990 | |
| 1991 | return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex); |
| 1992 | } |
| 1993 | |
| 1994 | static struct ethtool_ops ops = { |
| 1995 | .get_drvinfo = rtl8152_get_drvinfo, |
| 1996 | .get_settings = rtl8152_get_settings, |
| 1997 | .set_settings = rtl8152_set_settings, |
| 1998 | .get_link = ethtool_op_get_link, |
| 1999 | }; |
| 2000 | |
| 2001 | static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) |
| 2002 | { |
| 2003 | struct r8152 *tp = netdev_priv(netdev); |
| 2004 | struct mii_ioctl_data *data = if_mii(rq); |
| 2005 | int res = 0; |
| 2006 | |
| 2007 | switch (cmd) { |
| 2008 | case SIOCGMIIPHY: |
| 2009 | data->phy_id = R8152_PHY_ID; /* Internal PHY */ |
| 2010 | break; |
| 2011 | |
| 2012 | case SIOCGMIIREG: |
| 2013 | data->val_out = r8152_mdio_read(tp, data->reg_num); |
| 2014 | break; |
| 2015 | |
| 2016 | case SIOCSMIIREG: |
| 2017 | if (!capable(CAP_NET_ADMIN)) { |
| 2018 | res = -EPERM; |
| 2019 | break; |
| 2020 | } |
| 2021 | r8152_mdio_write(tp, data->reg_num, data->val_in); |
| 2022 | break; |
| 2023 | |
| 2024 | default: |
| 2025 | res = -EOPNOTSUPP; |
| 2026 | } |
| 2027 | |
| 2028 | return res; |
| 2029 | } |
| 2030 | |
| 2031 | static const struct net_device_ops rtl8152_netdev_ops = { |
| 2032 | .ndo_open = rtl8152_open, |
| 2033 | .ndo_stop = rtl8152_close, |
| 2034 | .ndo_do_ioctl = rtl8152_ioctl, |
| 2035 | .ndo_start_xmit = rtl8152_start_xmit, |
| 2036 | .ndo_tx_timeout = rtl8152_tx_timeout, |
| 2037 | .ndo_set_rx_mode = rtl8152_set_rx_mode, |
| 2038 | .ndo_set_mac_address = rtl8152_set_mac_address, |
| 2039 | |
| 2040 | .ndo_change_mtu = eth_change_mtu, |
| 2041 | .ndo_validate_addr = eth_validate_addr, |
| 2042 | }; |
| 2043 | |
| 2044 | static void r8152b_get_version(struct r8152 *tp) |
| 2045 | { |
| 2046 | u32 ocp_data; |
| 2047 | u16 version; |
| 2048 | |
| 2049 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1); |
| 2050 | version = (u16)(ocp_data & VERSION_MASK); |
| 2051 | |
| 2052 | switch (version) { |
| 2053 | case 0x4c00: |
| 2054 | tp->version = RTL_VER_01; |
| 2055 | break; |
| 2056 | case 0x4c10: |
| 2057 | tp->version = RTL_VER_02; |
| 2058 | break; |
| 2059 | default: |
| 2060 | netif_info(tp, probe, tp->netdev, |
| 2061 | "Unknown version 0x%04x\n", version); |
| 2062 | break; |
| 2063 | } |
| 2064 | } |
| 2065 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 2066 | static void rtl8152_unload(struct r8152 *tp) |
| 2067 | { |
| 2068 | u32 ocp_data; |
| 2069 | |
| 2070 | if (tp->version != RTL_VER_01) { |
| 2071 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 2072 | ocp_data |= POWER_CUT; |
| 2073 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 2074 | } |
| 2075 | |
| 2076 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); |
| 2077 | ocp_data &= ~RWSUME_INDICATE; |
| 2078 | ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); |
| 2079 | } |
| 2080 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2081 | static int rtl8152_probe(struct usb_interface *intf, |
| 2082 | const struct usb_device_id *id) |
| 2083 | { |
| 2084 | struct usb_device *udev = interface_to_usbdev(intf); |
| 2085 | struct r8152 *tp; |
| 2086 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2087 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2088 | |
| 2089 | if (udev->actconfig->desc.bConfigurationValue != 1) { |
| 2090 | usb_driver_set_configuration(udev, 1); |
| 2091 | return -ENODEV; |
| 2092 | } |
| 2093 | |
| 2094 | netdev = alloc_etherdev(sizeof(struct r8152)); |
| 2095 | if (!netdev) { |
| 2096 | dev_err(&intf->dev, "Out of memory"); |
| 2097 | return -ENOMEM; |
| 2098 | } |
| 2099 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2100 | SET_NETDEV_DEV(netdev, &intf->dev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2101 | tp = netdev_priv(netdev); |
| 2102 | tp->msg_enable = 0x7FFF; |
| 2103 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2104 | tasklet_init(&tp->tl, bottom_half, (unsigned long)tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2105 | INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); |
| 2106 | |
| 2107 | tp->udev = udev; |
| 2108 | tp->netdev = netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2109 | tp->intf = intf; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2110 | netdev->netdev_ops = &rtl8152_netdev_ops; |
| 2111 | netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2112 | |
| 2113 | netdev->features |= NETIF_F_IP_CSUM; |
| 2114 | netdev->hw_features = NETIF_F_IP_CSUM; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2115 | SET_ETHTOOL_OPS(netdev, &ops); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2116 | |
| 2117 | tp->mii.dev = netdev; |
| 2118 | tp->mii.mdio_read = read_mii_word; |
| 2119 | tp->mii.mdio_write = write_mii_word; |
| 2120 | tp->mii.phy_id_mask = 0x3f; |
| 2121 | tp->mii.reg_num_mask = 0x1f; |
| 2122 | tp->mii.phy_id = R8152_PHY_ID; |
| 2123 | tp->mii.supports_gmii = 0; |
| 2124 | |
| 2125 | r8152b_get_version(tp); |
| 2126 | r8152b_init(tp); |
| 2127 | set_ethernet_addr(tp); |
| 2128 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2129 | ret = alloc_all_mem(tp); |
| 2130 | if (ret) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2131 | goto out; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2132 | |
| 2133 | usb_set_intfdata(intf, tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2134 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2135 | ret = register_netdev(netdev); |
| 2136 | if (ret != 0) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2137 | netif_err(tp, probe, netdev, "couldn't register the device"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2138 | goto out1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | netif_info(tp, probe, netdev, "%s", DRIVER_VERSION); |
| 2142 | |
| 2143 | return 0; |
| 2144 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2145 | out1: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2146 | usb_set_intfdata(intf, NULL); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2147 | out: |
| 2148 | free_netdev(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2149 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2152 | static void rtl8152_disconnect(struct usb_interface *intf) |
| 2153 | { |
| 2154 | struct r8152 *tp = usb_get_intfdata(intf); |
| 2155 | |
| 2156 | usb_set_intfdata(intf, NULL); |
| 2157 | if (tp) { |
| 2158 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 2159 | tasklet_kill(&tp->tl); |
| 2160 | unregister_netdev(tp->netdev); |
| 2161 | rtl8152_unload(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2162 | free_all_mem(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2163 | free_netdev(tp->netdev); |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | /* table of devices that work with this driver */ |
| 2168 | static struct usb_device_id rtl8152_table[] = { |
| 2169 | {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)}, |
| 2170 | {} |
| 2171 | }; |
| 2172 | |
| 2173 | MODULE_DEVICE_TABLE(usb, rtl8152_table); |
| 2174 | |
| 2175 | static struct usb_driver rtl8152_driver = { |
| 2176 | .name = MODULENAME, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2177 | .id_table = rtl8152_table, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2178 | .probe = rtl8152_probe, |
| 2179 | .disconnect = rtl8152_disconnect, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2180 | .suspend = rtl8152_suspend, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2181 | .resume = rtl8152_resume, |
| 2182 | .reset_resume = rtl8152_resume, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2183 | }; |
| 2184 | |
Sachin Kamat | b4236daa | 2013-05-16 17:48:08 +0000 | [diff] [blame] | 2185 | module_usb_driver(rtl8152_driver); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2186 | |
| 2187 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 2188 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 2189 | MODULE_LICENSE("GPL"); |