blob: a90a7eb91f1ccd83829c6e3af204c552e0b42f37 [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
hayeswangc7de7de2014-01-15 10:42:16 +08002 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00003 *
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
hayeswangac718b62013-05-02 16:01:25 +000010#include <linux/signal.h>
11#include <linux/slab.h>
12#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000013#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/mii.h>
16#include <linux/ethtool.h>
17#include <linux/usb.h>
18#include <linux/crc32.h>
19#include <linux/if_vlan.h>
20#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080021#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080022#include <linux/ip.h>
23#include <linux/ipv6.h>
hayeswang6128d1bb2014-03-07 11:04:40 +080024#include <net/ip6_checksum.h>
hayeswangac718b62013-05-02 16:01:25 +000025
26/* Version Information */
hayeswang60c89072014-03-07 11:04:39 +080027#define DRIVER_VERSION "v1.06.0 (2014/03/03)"
hayeswangac718b62013-05-02 16:01:25 +000028#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080029#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000030#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
hayeswang43779f82014-01-02 11:25:10 +080042#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000043#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080044#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000045#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080046#define PLA_TEREDO_TIMER 0xd2cc
47#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000048#define PLA_LEDSEL 0xdd90
49#define PLA_LED_FEATURE 0xdd92
50#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080051#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000052#define PLA_GPHY_INTR_IMR 0xe022
53#define PLA_EEE_CR 0xe040
54#define PLA_EEEP_CR 0xe080
55#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080056#define PLA_MAC_PWR_CTRL2 0xe0ca
57#define PLA_MAC_PWR_CTRL3 0xe0cc
58#define PLA_MAC_PWR_CTRL4 0xe0ce
59#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000060#define PLA_TCR0 0xe610
61#define PLA_TCR1 0xe612
62#define PLA_TXFIFO_CTRL 0xe618
63#define PLA_RSTTELLY 0xe800
64#define PLA_CR 0xe813
65#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080066#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
67#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000068#define PLA_CONFIG5 0xe822
69#define PLA_PHY_PWR 0xe84c
70#define PLA_OOB_CTRL 0xe84f
71#define PLA_CPCR 0xe854
72#define PLA_MISC_0 0xe858
73#define PLA_MISC_1 0xe85a
74#define PLA_OCP_GPHY_BASE 0xe86c
75#define PLA_TELLYCNT 0xe890
76#define PLA_SFF_STS_7 0xe8de
77#define PLA_PHYSTATUS 0xe908
78#define PLA_BP_BA 0xfc26
79#define PLA_BP_0 0xfc28
80#define PLA_BP_1 0xfc2a
81#define PLA_BP_2 0xfc2c
82#define PLA_BP_3 0xfc2e
83#define PLA_BP_4 0xfc30
84#define PLA_BP_5 0xfc32
85#define PLA_BP_6 0xfc34
86#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080087#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000088
hayeswang43779f82014-01-02 11:25:10 +080089#define USB_U2P3_CTRL 0xb460
hayeswangac718b62013-05-02 16:01:25 +000090#define USB_DEV_STAT 0xb808
91#define USB_USB_CTRL 0xd406
92#define USB_PHY_CTRL 0xd408
93#define USB_TX_AGG 0xd40a
94#define USB_RX_BUF_TH 0xd40c
95#define USB_USB_TIMER 0xd428
hayeswang43779f82014-01-02 11:25:10 +080096#define USB_RX_EARLY_AGG 0xd42c
hayeswangac718b62013-05-02 16:01:25 +000097#define USB_PM_CTRL_STATUS 0xd432
98#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +080099#define USB_TOLERANCE 0xd490
100#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +0000101#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800102#define USB_MISC_0 0xd81a
103#define USB_POWER_CUT 0xd80a
104#define USB_AFE_CTRL2 0xd824
105#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000106#define USB_BP_BA 0xfc26
107#define USB_BP_0 0xfc28
108#define USB_BP_1 0xfc2a
109#define USB_BP_2 0xfc2c
110#define USB_BP_3 0xfc2e
111#define USB_BP_4 0xfc30
112#define USB_BP_5 0xfc32
113#define USB_BP_6 0xfc34
114#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800115#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000116
117/* OCP Registers */
118#define OCP_ALDPS_CONFIG 0x2010
119#define OCP_EEE_CONFIG1 0x2080
120#define OCP_EEE_CONFIG2 0x2092
121#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800122#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000123#define OCP_EEE_AR 0xa41a
124#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800125#define OCP_PHY_STATUS 0xa420
126#define OCP_POWER_CFG 0xa430
127#define OCP_EEE_CFG 0xa432
128#define OCP_SRAM_ADDR 0xa436
129#define OCP_SRAM_DATA 0xa438
130#define OCP_DOWN_SPEED 0xa442
131#define OCP_EEE_CFG2 0xa5d0
132#define OCP_ADC_CFG 0xbc06
133
134/* SRAM Register */
135#define SRAM_LPF_CFG 0x8012
136#define SRAM_10M_AMP1 0x8080
137#define SRAM_10M_AMP2 0x8082
138#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000139
140/* PLA_RCR */
141#define RCR_AAP 0x00000001
142#define RCR_APM 0x00000002
143#define RCR_AM 0x00000004
144#define RCR_AB 0x00000008
145#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
146
147/* PLA_RXFIFO_CTRL0 */
148#define RXFIFO_THR1_NORMAL 0x00080002
149#define RXFIFO_THR1_OOB 0x01800003
150
151/* PLA_RXFIFO_CTRL1 */
152#define RXFIFO_THR2_FULL 0x00000060
153#define RXFIFO_THR2_HIGH 0x00000038
154#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800155#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000156
157/* PLA_RXFIFO_CTRL2 */
158#define RXFIFO_THR3_FULL 0x00000078
159#define RXFIFO_THR3_HIGH 0x00000048
160#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800161#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000162
163/* PLA_TXFIFO_CTRL */
164#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800165#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000166
167/* PLA_FMC */
168#define FMC_FCR_MCU_EN 0x0001
169
170/* PLA_EEEP_CR */
171#define EEEP_CR_EEEP_TX 0x0002
172
hayeswang43779f82014-01-02 11:25:10 +0800173/* PLA_WDT6_CTRL */
174#define WDT6_SET_MODE 0x0010
175
hayeswangac718b62013-05-02 16:01:25 +0000176/* PLA_TCR0 */
177#define TCR0_TX_EMPTY 0x0800
178#define TCR0_AUTO_FIFO 0x0080
179
180/* PLA_TCR1 */
181#define VERSION_MASK 0x7cf0
182
183/* PLA_CR */
184#define CR_RST 0x10
185#define CR_RE 0x08
186#define CR_TE 0x04
187
188/* PLA_CRWECR */
189#define CRWECR_NORAML 0x00
190#define CRWECR_CONFIG 0xc0
191
192/* PLA_OOB_CTRL */
193#define NOW_IS_OOB 0x80
194#define TXFIFO_EMPTY 0x20
195#define RXFIFO_EMPTY 0x10
196#define LINK_LIST_READY 0x02
197#define DIS_MCU_CLROOB 0x01
198#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
199
200/* PLA_MISC_1 */
201#define RXDY_GATED_EN 0x0008
202
203/* PLA_SFF_STS_7 */
204#define RE_INIT_LL 0x8000
205#define MCU_BORW_EN 0x4000
206
207/* PLA_CPCR */
208#define CPCR_RX_VLAN 0x0040
209
210/* PLA_CFG_WOL */
211#define MAGIC_EN 0x0001
212
hayeswang43779f82014-01-02 11:25:10 +0800213/* PLA_TEREDO_CFG */
214#define TEREDO_SEL 0x8000
215#define TEREDO_WAKE_MASK 0x7f00
216#define TEREDO_RS_EVENT_MASK 0x00fe
217#define OOB_TEREDO_EN 0x0001
218
hayeswangac718b62013-05-02 16:01:25 +0000219/* PAL_BDC_CR */
220#define ALDPS_PROXY_MODE 0x0001
221
hayeswang21ff2e82014-02-18 21:49:06 +0800222/* PLA_CONFIG34 */
223#define LINK_ON_WAKE_EN 0x0010
224#define LINK_OFF_WAKE_EN 0x0008
225
hayeswangac718b62013-05-02 16:01:25 +0000226/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800227#define BWF_EN 0x0040
228#define MWF_EN 0x0020
229#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000230#define LAN_WAKE_EN 0x0002
231
232/* PLA_LED_FEATURE */
233#define LED_MODE_MASK 0x0700
234
235/* PLA_PHY_PWR */
236#define TX_10M_IDLE_EN 0x0080
237#define PFM_PWM_SWITCH 0x0040
238
239/* PLA_MAC_PWR_CTRL */
240#define D3_CLK_GATED_EN 0x00004000
241#define MCU_CLK_RATIO 0x07010f07
242#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800243#define ALDPS_SPDWN_RATIO 0x0f87
244
245/* PLA_MAC_PWR_CTRL2 */
246#define EEE_SPDWN_RATIO 0x8007
247
248/* PLA_MAC_PWR_CTRL3 */
249#define PKT_AVAIL_SPDWN_EN 0x0100
250#define SUSPEND_SPDWN_EN 0x0004
251#define U1U2_SPDWN_EN 0x0002
252#define L1_SPDWN_EN 0x0001
253
254/* PLA_MAC_PWR_CTRL4 */
255#define PWRSAVE_SPDWN_EN 0x1000
256#define RXDV_SPDWN_EN 0x0800
257#define TX10MIDLE_EN 0x0100
258#define TP100_SPDWN_EN 0x0020
259#define TP500_SPDWN_EN 0x0010
260#define TP1000_SPDWN_EN 0x0008
261#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000262
263/* PLA_GPHY_INTR_IMR */
264#define GPHY_STS_MSK 0x0001
265#define SPEED_DOWN_MSK 0x0002
266#define SPDWN_RXDV_MSK 0x0004
267#define SPDWN_LINKCHG_MSK 0x0008
268
269/* PLA_PHYAR */
270#define PHYAR_FLAG 0x80000000
271
272/* PLA_EEE_CR */
273#define EEE_RX_EN 0x0001
274#define EEE_TX_EN 0x0002
275
hayeswang43779f82014-01-02 11:25:10 +0800276/* PLA_BOOT_CTRL */
277#define AUTOLOAD_DONE 0x0002
278
hayeswangac718b62013-05-02 16:01:25 +0000279/* USB_DEV_STAT */
280#define STAT_SPEED_MASK 0x0006
281#define STAT_SPEED_HIGH 0x0000
282#define STAT_SPEED_FULL 0x0001
283
284/* USB_TX_AGG */
285#define TX_AGG_MAX_THRESHOLD 0x03
286
287/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800288#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800289#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800290#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000291
292/* USB_TX_DMA */
293#define TEST_MODE_DISABLE 0x00000001
294#define TX_SIZE_ADJUST1 0x00000100
295
296/* USB_UPS_CTRL */
297#define POWER_CUT 0x0100
298
299/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800300#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000301
302/* USB_USB_CTRL */
303#define RX_AGG_DISABLE 0x0010
304
hayeswang43779f82014-01-02 11:25:10 +0800305/* USB_U2P3_CTRL */
306#define U2P3_ENABLE 0x0001
307
308/* USB_POWER_CUT */
309#define PWR_EN 0x0001
310#define PHASE2_EN 0x0008
311
312/* USB_MISC_0 */
313#define PCUT_STATUS 0x0001
314
315/* USB_RX_EARLY_AGG */
316#define EARLY_AGG_SUPPER 0x0e832981
317#define EARLY_AGG_HIGH 0x0e837a12
318#define EARLY_AGG_SLOW 0x0e83ffff
319
320/* USB_WDT11_CTRL */
321#define TIMER11_EN 0x0001
322
323/* USB_LPM_CTRL */
324#define LPM_TIMER_MASK 0x0c
325#define LPM_TIMER_500MS 0x04 /* 500 ms */
326#define LPM_TIMER_500US 0x0c /* 500 us */
327
328/* USB_AFE_CTRL2 */
329#define SEN_VAL_MASK 0xf800
330#define SEN_VAL_NORMAL 0xa000
331#define SEL_RXIDLE 0x0100
332
hayeswangac718b62013-05-02 16:01:25 +0000333/* OCP_ALDPS_CONFIG */
334#define ENPWRSAVE 0x8000
335#define ENPDNPS 0x0200
336#define LINKENA 0x0100
337#define DIS_SDSAVE 0x0010
338
hayeswang43779f82014-01-02 11:25:10 +0800339/* OCP_PHY_STATUS */
340#define PHY_STAT_MASK 0x0007
341#define PHY_STAT_LAN_ON 3
342#define PHY_STAT_PWRDN 5
343
344/* OCP_POWER_CFG */
345#define EEE_CLKDIV_EN 0x8000
346#define EN_ALDPS 0x0004
347#define EN_10M_PLLOFF 0x0001
348
hayeswangac718b62013-05-02 16:01:25 +0000349/* OCP_EEE_CONFIG1 */
350#define RG_TXLPI_MSK_HFDUP 0x8000
351#define RG_MATCLR_EN 0x4000
352#define EEE_10_CAP 0x2000
353#define EEE_NWAY_EN 0x1000
354#define TX_QUIET_EN 0x0200
355#define RX_QUIET_EN 0x0100
356#define SDRISETIME 0x0010 /* bit 4 ~ 6 */
357#define RG_RXLPI_MSK_HFDUP 0x0008
358#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
359
360/* OCP_EEE_CONFIG2 */
361#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
362#define RG_DACQUIET_EN 0x0400
363#define RG_LDVQUIET_EN 0x0200
364#define RG_CKRSEL 0x0020
365#define RG_EEEPRG_EN 0x0010
366
367/* OCP_EEE_CONFIG3 */
368#define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
369#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
370#define MSK_PH 0x0006 /* bit 0 ~ 3 */
371
372/* OCP_EEE_AR */
373/* bit[15:14] function */
374#define FUN_ADDR 0x0000
375#define FUN_DATA 0x4000
376/* bit[4:0] device addr */
377#define DEVICE_ADDR 0x0007
378
379/* OCP_EEE_DATA */
380#define EEE_ADDR 0x003C
381#define EEE_DATA 0x0002
382
hayeswang43779f82014-01-02 11:25:10 +0800383/* OCP_EEE_CFG */
384#define CTAP_SHORT_EN 0x0040
385#define EEE10_EN 0x0010
386
387/* OCP_DOWN_SPEED */
388#define EN_10M_BGOFF 0x0080
389
390/* OCP_EEE_CFG2 */
391#define MY1000_EEE 0x0004
392#define MY100_EEE 0x0002
393
394/* OCP_ADC_CFG */
395#define CKADSEL_L 0x0100
396#define ADC_EN 0x0080
397#define EN_EMI_L 0x0040
398
399/* SRAM_LPF_CFG */
400#define LPF_AUTO_TUNE 0x8000
401
402/* SRAM_10M_AMP1 */
403#define GDAC_IB_UPALL 0x0008
404
405/* SRAM_10M_AMP2 */
406#define AMP_DN 0x0200
407
408/* SRAM_IMPEDANCE */
409#define RX_DRIVING_MASK 0x6000
410
hayeswangac718b62013-05-02 16:01:25 +0000411enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800412 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000413 _100bps = 0x08,
414 _10bps = 0x04,
415 LINK_STATUS = 0x02,
416 FULL_DUP = 0x01,
417};
418
hayeswangebc2ec482013-08-14 20:54:38 +0800419#define RTL8152_MAX_TX 10
420#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800421#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800422#define CRC_SIZE 4
423#define TX_ALIGN 4
424#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800425
426#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800427
hayeswangac718b62013-05-02 16:01:25 +0000428#define RTL8152_REQT_READ 0xc0
429#define RTL8152_REQT_WRITE 0x40
430#define RTL8152_REQ_GET_REGS 0x05
431#define RTL8152_REQ_SET_REGS 0x05
432
433#define BYTE_EN_DWORD 0xff
434#define BYTE_EN_WORD 0x33
435#define BYTE_EN_BYTE 0x11
436#define BYTE_EN_SIX_BYTES 0x3f
437#define BYTE_EN_START_MASK 0x0f
438#define BYTE_EN_END_MASK 0xf0
439
440#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
441#define RTL8152_TX_TIMEOUT (HZ)
442
443/* rtl8152 flags */
444enum rtl8152_flags {
445 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000446 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800447 WORK_ENABLE,
448 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800449 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800450 PHY_RESET,
hayeswang0c3121f2014-03-07 11:04:36 +0800451 SCHEDULE_TASKLET,
hayeswangac718b62013-05-02 16:01:25 +0000452};
453
454/* Define these values to match your device */
455#define VENDOR_ID_REALTEK 0x0bda
456#define PRODUCT_ID_RTL8152 0x8152
hayeswang43779f82014-01-02 11:25:10 +0800457#define PRODUCT_ID_RTL8153 0x8153
458
459#define VENDOR_ID_SAMSUNG 0x04e8
460#define PRODUCT_ID_SAMSUNG 0xa101
hayeswangac718b62013-05-02 16:01:25 +0000461
462#define MCU_TYPE_PLA 0x0100
463#define MCU_TYPE_USB 0x0000
464
hayeswangc7de7de2014-01-15 10:42:16 +0800465#define REALTEK_USB_DEVICE(vend, prod) \
466 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)
467
hayeswangac718b62013-05-02 16:01:25 +0000468struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800469 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000470#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800471
hayeswang500b6d72013-11-20 17:30:57 +0800472 __le32 opts2;
hayeswang565cab02014-03-07 11:04:38 +0800473#define RD_UDP_CS (1 << 23)
474#define RD_TCP_CS (1 << 22)
hayeswang6128d1bb2014-03-07 11:04:40 +0800475#define RD_IPV6_CS (1 << 20)
hayeswang565cab02014-03-07 11:04:38 +0800476#define RD_IPV4_CS (1 << 19)
477
hayeswang500b6d72013-11-20 17:30:57 +0800478 __le32 opts3;
hayeswang565cab02014-03-07 11:04:38 +0800479#define IPF (1 << 23) /* IP checksum fail */
480#define UDPF (1 << 22) /* UDP checksum fail */
481#define TCPF (1 << 21) /* TCP checksum fail */
482
hayeswang500b6d72013-11-20 17:30:57 +0800483 __le32 opts4;
484 __le32 opts5;
485 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000486};
487
488struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800489 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000490#define TX_FS (1 << 31) /* First segment of a packet */
491#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang60c89072014-03-07 11:04:39 +0800492#define GTSENDV4 (1 << 28)
hayeswang6128d1bb2014-03-07 11:04:40 +0800493#define GTSENDV6 (1 << 27)
hayeswang60c89072014-03-07 11:04:39 +0800494#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800495#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800496#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800497
hayeswang500b6d72013-11-20 17:30:57 +0800498 __le32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800499#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
500#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
501#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
502#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800503#define MSS_SHIFT 17
504#define MSS_MAX 0x7ffU
505#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800506#define TCPHO_MAX 0x7ffU
hayeswangac718b62013-05-02 16:01:25 +0000507};
508
hayeswangdff4e8a2013-08-16 16:09:33 +0800509struct r8152;
510
hayeswangebc2ec482013-08-14 20:54:38 +0800511struct rx_agg {
512 struct list_head list;
513 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800514 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800515 void *buffer;
516 void *head;
517};
518
519struct tx_agg {
520 struct list_head list;
521 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800522 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800523 void *buffer;
524 void *head;
525 u32 skb_num;
526 u32 skb_len;
527};
528
hayeswangac718b62013-05-02 16:01:25 +0000529struct r8152 {
530 unsigned long flags;
531 struct usb_device *udev;
532 struct tasklet_struct tl;
hayeswang40a82912013-08-14 20:54:40 +0800533 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000534 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800535 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800536 struct tx_agg tx_info[RTL8152_MAX_TX];
537 struct rx_agg rx_info[RTL8152_MAX_RX];
538 struct list_head rx_done, tx_free;
539 struct sk_buff_head tx_queue;
540 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000541 struct delayed_work schedule;
542 struct mii_if_info mii;
hayeswangc81229c2014-01-02 11:22:42 +0800543
544 struct rtl_ops {
545 void (*init)(struct r8152 *);
546 int (*enable)(struct r8152 *);
547 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800548 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800549 void (*down)(struct r8152 *);
550 void (*unload)(struct r8152 *);
551 } rtl_ops;
552
hayeswang40a82912013-08-14 20:54:40 +0800553 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800554 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000555 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800556 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000557 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800558 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000559 u8 version;
560 u8 speed;
561};
562
563enum rtl_version {
564 RTL_VER_UNKNOWN = 0,
565 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800566 RTL_VER_02,
567 RTL_VER_03,
568 RTL_VER_04,
569 RTL_VER_05,
570 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000571};
572
hayeswang60c89072014-03-07 11:04:39 +0800573enum tx_csum_stat {
574 TX_CSUM_SUCCESS = 0,
575 TX_CSUM_TSO,
576 TX_CSUM_NONE
577};
578
hayeswangac718b62013-05-02 16:01:25 +0000579/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
580 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
581 */
582static const int multicast_filter_limit = 32;
hayeswangebc2ec482013-08-14 20:54:38 +0800583static unsigned int rx_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000584
hayeswang60c89072014-03-07 11:04:39 +0800585#define RTL_LIMITED_TSO_SIZE (rx_buf_sz - sizeof(struct tx_desc) - \
586 VLAN_ETH_HLEN - VLAN_HLEN)
587
hayeswangac718b62013-05-02 16:01:25 +0000588static
589int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
590{
hayeswang31787f52013-07-31 17:21:25 +0800591 int ret;
592 void *tmp;
593
594 tmp = kmalloc(size, GFP_KERNEL);
595 if (!tmp)
596 return -ENOMEM;
597
598 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000599 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
hayeswang31787f52013-07-31 17:21:25 +0800600 value, index, tmp, size, 500);
601
602 memcpy(data, tmp, size);
603 kfree(tmp);
604
605 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000606}
607
608static
609int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
610{
hayeswang31787f52013-07-31 17:21:25 +0800611 int ret;
612 void *tmp;
613
614 tmp = kmalloc(size, GFP_KERNEL);
615 if (!tmp)
616 return -ENOMEM;
617
618 memcpy(tmp, data, size);
619
620 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000621 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
hayeswang31787f52013-07-31 17:21:25 +0800622 value, index, tmp, size, 500);
623
624 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800625
hayeswang31787f52013-07-31 17:21:25 +0800626 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000627}
628
629static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
630 void *data, u16 type)
631{
hayeswang45f4a192014-01-06 17:08:41 +0800632 u16 limit = 64;
633 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000634
635 if (test_bit(RTL8152_UNPLUG, &tp->flags))
636 return -ENODEV;
637
638 /* both size and indix must be 4 bytes align */
639 if ((size & 3) || !size || (index & 3) || !data)
640 return -EPERM;
641
642 if ((u32)index + (u32)size > 0xffff)
643 return -EPERM;
644
645 while (size) {
646 if (size > limit) {
647 ret = get_registers(tp, index, type, limit, data);
648 if (ret < 0)
649 break;
650
651 index += limit;
652 data += limit;
653 size -= limit;
654 } else {
655 ret = get_registers(tp, index, type, size, data);
656 if (ret < 0)
657 break;
658
659 index += size;
660 data += size;
661 size = 0;
662 break;
663 }
664 }
665
666 return ret;
667}
668
669static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
670 u16 size, void *data, u16 type)
671{
hayeswang45f4a192014-01-06 17:08:41 +0800672 int ret;
673 u16 byteen_start, byteen_end, byen;
674 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000675
676 if (test_bit(RTL8152_UNPLUG, &tp->flags))
677 return -ENODEV;
678
679 /* both size and indix must be 4 bytes align */
680 if ((size & 3) || !size || (index & 3) || !data)
681 return -EPERM;
682
683 if ((u32)index + (u32)size > 0xffff)
684 return -EPERM;
685
686 byteen_start = byteen & BYTE_EN_START_MASK;
687 byteen_end = byteen & BYTE_EN_END_MASK;
688
689 byen = byteen_start | (byteen_start << 4);
690 ret = set_registers(tp, index, type | byen, 4, data);
691 if (ret < 0)
692 goto error1;
693
694 index += 4;
695 data += 4;
696 size -= 4;
697
698 if (size) {
699 size -= 4;
700
701 while (size) {
702 if (size > limit) {
703 ret = set_registers(tp, index,
704 type | BYTE_EN_DWORD,
705 limit, data);
706 if (ret < 0)
707 goto error1;
708
709 index += limit;
710 data += limit;
711 size -= limit;
712 } else {
713 ret = set_registers(tp, index,
714 type | BYTE_EN_DWORD,
715 size, data);
716 if (ret < 0)
717 goto error1;
718
719 index += size;
720 data += size;
721 size = 0;
722 break;
723 }
724 }
725
726 byen = byteen_end | (byteen_end >> 4);
727 ret = set_registers(tp, index, type | byen, 4, data);
728 if (ret < 0)
729 goto error1;
730 }
731
732error1:
733 return ret;
734}
735
736static inline
737int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
738{
739 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
740}
741
742static inline
743int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
744{
745 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
746}
747
748static inline
749int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
750{
751 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
752}
753
754static inline
755int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
756{
757 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
758}
759
760static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
761{
hayeswangc8826de2013-07-31 17:21:26 +0800762 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000763
hayeswangc8826de2013-07-31 17:21:26 +0800764 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000765
766 return __le32_to_cpu(data);
767}
768
769static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
770{
hayeswangc8826de2013-07-31 17:21:26 +0800771 __le32 tmp = __cpu_to_le32(data);
772
773 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000774}
775
776static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
777{
778 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800779 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000780 u8 shift = index & 2;
781
782 index &= ~3;
783
hayeswangc8826de2013-07-31 17:21:26 +0800784 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000785
hayeswangc8826de2013-07-31 17:21:26 +0800786 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000787 data >>= (shift * 8);
788 data &= 0xffff;
789
790 return (u16)data;
791}
792
793static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
794{
hayeswangc8826de2013-07-31 17:21:26 +0800795 u32 mask = 0xffff;
796 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000797 u16 byen = BYTE_EN_WORD;
798 u8 shift = index & 2;
799
800 data &= mask;
801
802 if (index & 2) {
803 byen <<= shift;
804 mask <<= (shift * 8);
805 data <<= (shift * 8);
806 index &= ~3;
807 }
808
hayeswangc8826de2013-07-31 17:21:26 +0800809 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000810
hayeswangc8826de2013-07-31 17:21:26 +0800811 data |= __le32_to_cpu(tmp) & ~mask;
812 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000813
hayeswangc8826de2013-07-31 17:21:26 +0800814 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000815}
816
817static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
818{
819 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800820 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000821 u8 shift = index & 3;
822
823 index &= ~3;
824
hayeswangc8826de2013-07-31 17:21:26 +0800825 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000826
hayeswangc8826de2013-07-31 17:21:26 +0800827 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000828 data >>= (shift * 8);
829 data &= 0xff;
830
831 return (u8)data;
832}
833
834static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
835{
hayeswangc8826de2013-07-31 17:21:26 +0800836 u32 mask = 0xff;
837 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000838 u16 byen = BYTE_EN_BYTE;
839 u8 shift = index & 3;
840
841 data &= mask;
842
843 if (index & 3) {
844 byen <<= shift;
845 mask <<= (shift * 8);
846 data <<= (shift * 8);
847 index &= ~3;
848 }
849
hayeswangc8826de2013-07-31 17:21:26 +0800850 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000851
hayeswangc8826de2013-07-31 17:21:26 +0800852 data |= __le32_to_cpu(tmp) & ~mask;
853 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000854
hayeswangc8826de2013-07-31 17:21:26 +0800855 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000856}
857
hayeswangac244d32014-01-02 11:22:40 +0800858static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
859{
860 u16 ocp_base, ocp_index;
861
862 ocp_base = addr & 0xf000;
863 if (ocp_base != tp->ocp_base) {
864 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
865 tp->ocp_base = ocp_base;
866 }
867
868 ocp_index = (addr & 0x0fff) | 0xb000;
869 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
870}
871
hayeswange3fe0b12014-01-02 11:22:39 +0800872static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
873{
874 u16 ocp_base, ocp_index;
875
876 ocp_base = addr & 0xf000;
877 if (ocp_base != tp->ocp_base) {
878 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
879 tp->ocp_base = ocp_base;
880 }
881
882 ocp_index = (addr & 0x0fff) | 0xb000;
883 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
884}
885
hayeswangac244d32014-01-02 11:22:40 +0800886static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000887{
hayeswangac244d32014-01-02 11:22:40 +0800888 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000889}
890
hayeswangac244d32014-01-02 11:22:40 +0800891static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000892{
hayeswangac244d32014-01-02 11:22:40 +0800893 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000894}
895
hayeswang43779f82014-01-02 11:25:10 +0800896static void sram_write(struct r8152 *tp, u16 addr, u16 data)
897{
898 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
899 ocp_reg_write(tp, OCP_SRAM_DATA, data);
900}
901
902static u16 sram_read(struct r8152 *tp, u16 addr)
903{
904 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
905 return ocp_reg_read(tp, OCP_SRAM_DATA);
906}
907
hayeswangac718b62013-05-02 16:01:25 +0000908static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
909{
910 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +0800911 int ret;
hayeswangac718b62013-05-02 16:01:25 +0000912
913 if (phy_id != R8152_PHY_ID)
914 return -EINVAL;
915
hayeswang9a4be1b2014-02-18 21:49:07 +0800916 ret = usb_autopm_get_interface(tp->intf);
917 if (ret < 0)
918 goto out;
919
920 ret = r8152_mdio_read(tp, reg);
921
922 usb_autopm_put_interface(tp->intf);
923
924out:
925 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000926}
927
928static
929void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
930{
931 struct r8152 *tp = netdev_priv(netdev);
932
933 if (phy_id != R8152_PHY_ID)
934 return;
935
hayeswang9a4be1b2014-02-18 21:49:07 +0800936 if (usb_autopm_get_interface(tp->intf) < 0)
937 return;
938
hayeswangac718b62013-05-02 16:01:25 +0000939 r8152_mdio_write(tp, reg, val);
hayeswang9a4be1b2014-02-18 21:49:07 +0800940
941 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +0000942}
943
hayeswangebc2ec482013-08-14 20:54:38 +0800944static
945int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
946
hayeswangac718b62013-05-02 16:01:25 +0000947static inline void set_ethernet_addr(struct r8152 *tp)
948{
949 struct net_device *dev = tp->netdev;
hayeswang8a91c822014-02-18 21:49:01 +0800950 int ret;
hayeswang31787f52013-07-31 17:21:25 +0800951 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000952
hayeswang8a91c822014-02-18 21:49:01 +0800953 if (tp->version == RTL_VER_01)
954 ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id);
955 else
956 ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id);
957
958 if (ret < 0) {
hayeswangac718b62013-05-02 16:01:25 +0000959 netif_notice(tp, probe, dev, "inet addr fail\n");
hayeswang8a91c822014-02-18 21:49:01 +0800960 } else {
961 if (tp->version != RTL_VER_01) {
962 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
963 CRWECR_CONFIG);
964 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES,
965 sizeof(node_id), node_id);
966 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
967 CRWECR_NORAML);
968 }
969
hayeswangac718b62013-05-02 16:01:25 +0000970 memcpy(dev->dev_addr, node_id, dev->addr_len);
971 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
972 }
hayeswangac718b62013-05-02 16:01:25 +0000973}
974
975static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
976{
977 struct r8152 *tp = netdev_priv(netdev);
978 struct sockaddr *addr = p;
979
980 if (!is_valid_ether_addr(addr->sa_data))
981 return -EADDRNOTAVAIL;
982
983 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
984
985 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
986 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
987 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
988
989 return 0;
990}
991
hayeswangac718b62013-05-02 16:01:25 +0000992static void read_bulk_callback(struct urb *urb)
993{
hayeswangac718b62013-05-02 16:01:25 +0000994 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +0000995 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +0800996 struct rx_agg *agg;
997 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000998 int result;
hayeswangac718b62013-05-02 16:01:25 +0000999
hayeswangebc2ec482013-08-14 20:54:38 +08001000 agg = urb->context;
1001 if (!agg)
1002 return;
1003
1004 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001005 if (!tp)
1006 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001007
hayeswangac718b62013-05-02 16:01:25 +00001008 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1009 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001010
1011 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001012 return;
1013
hayeswangebc2ec482013-08-14 20:54:38 +08001014 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001015
1016 /* When link down, the driver would cancel all bulks. */
1017 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001018 if (!netif_carrier_ok(netdev))
1019 return;
1020
hayeswang9a4be1b2014-02-18 21:49:07 +08001021 usb_mark_last_busy(tp->udev);
1022
hayeswangac718b62013-05-02 16:01:25 +00001023 switch (status) {
1024 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001025 if (urb->actual_length < ETH_ZLEN)
1026 break;
1027
hayeswang2685d412014-03-07 11:04:34 +08001028 spin_lock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001029 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001030 spin_unlock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001031 tasklet_schedule(&tp->tl);
1032 return;
hayeswangac718b62013-05-02 16:01:25 +00001033 case -ESHUTDOWN:
1034 set_bit(RTL8152_UNPLUG, &tp->flags);
1035 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001036 return;
hayeswangac718b62013-05-02 16:01:25 +00001037 case -ENOENT:
1038 return; /* the urb is in unlink state */
1039 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001040 if (net_ratelimit())
1041 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001042 break;
hayeswangac718b62013-05-02 16:01:25 +00001043 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001044 if (net_ratelimit())
1045 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001046 break;
hayeswangac718b62013-05-02 16:01:25 +00001047 }
1048
hayeswangebc2ec482013-08-14 20:54:38 +08001049 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001050 if (result == -ENODEV) {
1051 netif_device_detach(tp->netdev);
1052 } else if (result) {
hayeswang2685d412014-03-07 11:04:34 +08001053 spin_lock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001054 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001055 spin_unlock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001056 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001057 }
hayeswangac718b62013-05-02 16:01:25 +00001058}
1059
1060static void write_bulk_callback(struct urb *urb)
1061{
hayeswangebc2ec482013-08-14 20:54:38 +08001062 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001063 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001064 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001065 struct r8152 *tp;
1066 int status = urb->status;
1067
hayeswangebc2ec482013-08-14 20:54:38 +08001068 agg = urb->context;
1069 if (!agg)
1070 return;
1071
1072 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001073 if (!tp)
1074 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001075
hayeswangd104eaf2014-03-06 15:07:17 +08001076 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001077 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001078 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001079 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001080 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001081 stats->tx_errors += agg->skb_num;
1082 } else {
1083 stats->tx_packets += agg->skb_num;
1084 stats->tx_bytes += agg->skb_len;
1085 }
1086
hayeswang2685d412014-03-07 11:04:34 +08001087 spin_lock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001088 list_add_tail(&agg->list, &tp->tx_free);
hayeswang2685d412014-03-07 11:04:34 +08001089 spin_unlock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001090
hayeswang9a4be1b2014-02-18 21:49:07 +08001091 usb_autopm_put_interface_async(tp->intf);
1092
hayeswangd104eaf2014-03-06 15:07:17 +08001093 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001094 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001095
1096 if (!test_bit(WORK_ENABLE, &tp->flags))
1097 return;
1098
1099 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1100 return;
1101
1102 if (!skb_queue_empty(&tp->tx_queue))
hayeswang0c3121f2014-03-07 11:04:36 +08001103 tasklet_schedule(&tp->tl);
hayeswangebc2ec482013-08-14 20:54:38 +08001104}
1105
hayeswang40a82912013-08-14 20:54:40 +08001106static void intr_callback(struct urb *urb)
1107{
1108 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001109 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001110 int status = urb->status;
1111 int res;
1112
1113 tp = urb->context;
1114 if (!tp)
1115 return;
1116
1117 if (!test_bit(WORK_ENABLE, &tp->flags))
1118 return;
1119
1120 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1121 return;
1122
1123 switch (status) {
1124 case 0: /* success */
1125 break;
1126 case -ECONNRESET: /* unlink */
1127 case -ESHUTDOWN:
1128 netif_device_detach(tp->netdev);
1129 case -ENOENT:
1130 return;
1131 case -EOVERFLOW:
1132 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1133 goto resubmit;
1134 /* -EPIPE: should clear the halt */
1135 default:
1136 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1137 goto resubmit;
1138 }
1139
1140 d = urb->transfer_buffer;
1141 if (INTR_LINK & __le16_to_cpu(d[0])) {
1142 if (!(tp->speed & LINK_STATUS)) {
1143 set_bit(RTL8152_LINK_CHG, &tp->flags);
1144 schedule_delayed_work(&tp->schedule, 0);
1145 }
1146 } else {
1147 if (tp->speed & LINK_STATUS) {
1148 set_bit(RTL8152_LINK_CHG, &tp->flags);
1149 schedule_delayed_work(&tp->schedule, 0);
1150 }
1151 }
1152
1153resubmit:
1154 res = usb_submit_urb(urb, GFP_ATOMIC);
1155 if (res == -ENODEV)
1156 netif_device_detach(tp->netdev);
1157 else if (res)
1158 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001159 "can't resubmit intr, status %d\n", res);
hayeswang40a82912013-08-14 20:54:40 +08001160}
1161
hayeswangebc2ec482013-08-14 20:54:38 +08001162static inline void *rx_agg_align(void *data)
1163{
hayeswang8e1f51b2014-01-02 11:22:41 +08001164 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001165}
1166
1167static inline void *tx_agg_align(void *data)
1168{
hayeswang8e1f51b2014-01-02 11:22:41 +08001169 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001170}
1171
1172static void free_all_mem(struct r8152 *tp)
1173{
1174 int i;
1175
1176 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001177 usb_free_urb(tp->rx_info[i].urb);
1178 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001179
hayeswang9629e3c2014-01-15 10:42:15 +08001180 kfree(tp->rx_info[i].buffer);
1181 tp->rx_info[i].buffer = NULL;
1182 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001183 }
1184
1185 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001186 usb_free_urb(tp->tx_info[i].urb);
1187 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001188
hayeswang9629e3c2014-01-15 10:42:15 +08001189 kfree(tp->tx_info[i].buffer);
1190 tp->tx_info[i].buffer = NULL;
1191 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001192 }
hayeswang40a82912013-08-14 20:54:40 +08001193
hayeswang9629e3c2014-01-15 10:42:15 +08001194 usb_free_urb(tp->intr_urb);
1195 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001196
hayeswang9629e3c2014-01-15 10:42:15 +08001197 kfree(tp->intr_buff);
1198 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001199}
1200
1201static int alloc_all_mem(struct r8152 *tp)
1202{
1203 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001204 struct usb_interface *intf = tp->intf;
1205 struct usb_host_interface *alt = intf->cur_altsetting;
1206 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001207 struct urb *urb;
1208 int node, i;
1209 u8 *buf;
1210
1211 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1212
1213 spin_lock_init(&tp->rx_lock);
1214 spin_lock_init(&tp->tx_lock);
1215 INIT_LIST_HEAD(&tp->rx_done);
1216 INIT_LIST_HEAD(&tp->tx_free);
1217 skb_queue_head_init(&tp->tx_queue);
1218
1219 for (i = 0; i < RTL8152_MAX_RX; i++) {
1220 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1221 if (!buf)
1222 goto err1;
1223
1224 if (buf != rx_agg_align(buf)) {
1225 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001226 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1227 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001228 if (!buf)
1229 goto err1;
1230 }
1231
1232 urb = usb_alloc_urb(0, GFP_KERNEL);
1233 if (!urb) {
1234 kfree(buf);
1235 goto err1;
1236 }
1237
1238 INIT_LIST_HEAD(&tp->rx_info[i].list);
1239 tp->rx_info[i].context = tp;
1240 tp->rx_info[i].urb = urb;
1241 tp->rx_info[i].buffer = buf;
1242 tp->rx_info[i].head = rx_agg_align(buf);
1243 }
1244
1245 for (i = 0; i < RTL8152_MAX_TX; i++) {
1246 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1247 if (!buf)
1248 goto err1;
1249
1250 if (buf != tx_agg_align(buf)) {
1251 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001252 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1253 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001254 if (!buf)
1255 goto err1;
1256 }
1257
1258 urb = usb_alloc_urb(0, GFP_KERNEL);
1259 if (!urb) {
1260 kfree(buf);
1261 goto err1;
1262 }
1263
1264 INIT_LIST_HEAD(&tp->tx_info[i].list);
1265 tp->tx_info[i].context = tp;
1266 tp->tx_info[i].urb = urb;
1267 tp->tx_info[i].buffer = buf;
1268 tp->tx_info[i].head = tx_agg_align(buf);
1269
1270 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1271 }
1272
hayeswang40a82912013-08-14 20:54:40 +08001273 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1274 if (!tp->intr_urb)
1275 goto err1;
1276
1277 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1278 if (!tp->intr_buff)
1279 goto err1;
1280
1281 tp->intr_interval = (int)ep_intr->desc.bInterval;
1282 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1283 tp->intr_buff, INTBUFSIZE, intr_callback,
1284 tp, tp->intr_interval);
1285
hayeswangebc2ec482013-08-14 20:54:38 +08001286 return 0;
1287
1288err1:
1289 free_all_mem(tp);
1290 return -ENOMEM;
1291}
1292
hayeswang0de98f62013-08-16 16:09:35 +08001293static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1294{
1295 struct tx_agg *agg = NULL;
1296 unsigned long flags;
1297
hayeswang21949ab2014-03-07 11:04:35 +08001298 if (list_empty(&tp->tx_free))
1299 return NULL;
1300
hayeswang0de98f62013-08-16 16:09:35 +08001301 spin_lock_irqsave(&tp->tx_lock, flags);
1302 if (!list_empty(&tp->tx_free)) {
1303 struct list_head *cursor;
1304
1305 cursor = tp->tx_free.next;
1306 list_del_init(cursor);
1307 agg = list_entry(cursor, struct tx_agg, list);
1308 }
1309 spin_unlock_irqrestore(&tp->tx_lock, flags);
1310
1311 return agg;
1312}
1313
hayeswang60c89072014-03-07 11:04:39 +08001314static inline __be16 get_protocol(struct sk_buff *skb)
hayeswang5bd23882013-08-14 20:54:39 +08001315{
hayeswang60c89072014-03-07 11:04:39 +08001316 __be16 protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001317
hayeswang60c89072014-03-07 11:04:39 +08001318 if (skb->protocol == htons(ETH_P_8021Q))
1319 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1320 else
1321 protocol = skb->protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001322
hayeswang60c89072014-03-07 11:04:39 +08001323 return protocol;
1324}
1325
hayeswang6128d1bb2014-03-07 11:04:40 +08001326/*
1327 * r8152_csum_workaround()
1328 * The hw limites the value the transport offset. When the offset is out of the
1329 * range, calculate the checksum by sw.
1330 */
1331static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1332 struct sk_buff_head *list)
1333{
1334 if (skb_shinfo(skb)->gso_size) {
1335 netdev_features_t features = tp->netdev->features;
1336 struct sk_buff_head seg_list;
1337 struct sk_buff *segs, *nskb;
1338
1339 features &= ~(NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO);
1340 segs = skb_gso_segment(skb, features);
1341 if (IS_ERR(segs) || !segs)
1342 goto drop;
1343
1344 __skb_queue_head_init(&seg_list);
1345
1346 do {
1347 nskb = segs;
1348 segs = segs->next;
1349 nskb->next = NULL;
1350 __skb_queue_tail(&seg_list, nskb);
1351 } while (segs);
1352
1353 skb_queue_splice(&seg_list, list);
1354 dev_kfree_skb(skb);
1355 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1356 if (skb_checksum_help(skb) < 0)
1357 goto drop;
1358
1359 __skb_queue_head(list, skb);
1360 } else {
1361 struct net_device_stats *stats;
1362
1363drop:
1364 stats = &tp->netdev->stats;
1365 stats->tx_dropped++;
1366 dev_kfree_skb(skb);
1367 }
1368}
1369
1370/*
1371 * msdn_giant_send_check()
1372 * According to the document of microsoft, the TCP Pseudo Header excludes the
1373 * packet length for IPv6 TCP large packets.
1374 */
1375static int msdn_giant_send_check(struct sk_buff *skb)
1376{
1377 const struct ipv6hdr *ipv6h;
1378 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001379 int ret;
1380
1381 ret = skb_cow_head(skb, 0);
1382 if (ret)
1383 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001384
1385 ipv6h = ipv6_hdr(skb);
1386 th = tcp_hdr(skb);
1387
1388 th->check = 0;
1389 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1390
hayeswangfcb308d2014-03-11 10:20:32 +08001391 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001392}
1393
hayeswang60c89072014-03-07 11:04:39 +08001394static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1395 struct sk_buff *skb, u32 len, u32 transport_offset)
1396{
1397 u32 mss = skb_shinfo(skb)->gso_size;
1398 u32 opts1, opts2 = 0;
1399 int ret = TX_CSUM_SUCCESS;
1400
1401 WARN_ON_ONCE(len > TX_LEN_MAX);
1402
1403 opts1 = len | TX_FS | TX_LS;
1404
1405 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001406 if (transport_offset > GTTCPHO_MAX) {
1407 netif_warn(tp, tx_err, tp->netdev,
1408 "Invalid transport offset 0x%x for TSO\n",
1409 transport_offset);
1410 ret = TX_CSUM_TSO;
1411 goto unavailable;
1412 }
1413
hayeswang60c89072014-03-07 11:04:39 +08001414 switch (get_protocol(skb)) {
1415 case htons(ETH_P_IP):
1416 opts1 |= GTSENDV4;
1417 break;
1418
hayeswang6128d1bb2014-03-07 11:04:40 +08001419 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001420 if (msdn_giant_send_check(skb)) {
1421 ret = TX_CSUM_TSO;
1422 goto unavailable;
1423 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001424 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001425 break;
1426
hayeswang60c89072014-03-07 11:04:39 +08001427 default:
1428 WARN_ON_ONCE(1);
1429 break;
1430 }
1431
1432 opts1 |= transport_offset << GTTCPHO_SHIFT;
1433 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1434 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001435 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001436
hayeswang6128d1bb2014-03-07 11:04:40 +08001437 if (transport_offset > TCPHO_MAX) {
1438 netif_warn(tp, tx_err, tp->netdev,
1439 "Invalid transport offset 0x%x\n",
1440 transport_offset);
1441 ret = TX_CSUM_NONE;
1442 goto unavailable;
1443 }
1444
hayeswang60c89072014-03-07 11:04:39 +08001445 switch (get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001446 case htons(ETH_P_IP):
1447 opts2 |= IPV4_CS;
1448 ip_protocol = ip_hdr(skb)->protocol;
1449 break;
1450
1451 case htons(ETH_P_IPV6):
1452 opts2 |= IPV6_CS;
1453 ip_protocol = ipv6_hdr(skb)->nexthdr;
1454 break;
1455
1456 default:
1457 ip_protocol = IPPROTO_RAW;
1458 break;
1459 }
1460
hayeswang60c89072014-03-07 11:04:39 +08001461 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001462 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001463 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001464 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001465 else
hayeswang5bd23882013-08-14 20:54:39 +08001466 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001467
hayeswang60c89072014-03-07 11:04:39 +08001468 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001469 }
hayeswang60c89072014-03-07 11:04:39 +08001470
1471 desc->opts2 = cpu_to_le32(opts2);
1472 desc->opts1 = cpu_to_le32(opts1);
1473
hayeswang6128d1bb2014-03-07 11:04:40 +08001474unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001475 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001476}
1477
hayeswangb1379d92013-08-16 16:09:37 +08001478static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1479{
hayeswangd84130a2014-02-18 21:49:02 +08001480 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001481 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001482 u8 *tx_data;
1483
hayeswangd84130a2014-02-18 21:49:02 +08001484 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001485 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001486 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001487 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001488
hayeswangb1379d92013-08-16 16:09:37 +08001489 tx_data = agg->head;
1490 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001491 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001492
hayeswang7937f9e2013-11-20 17:30:54 +08001493 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001494 struct tx_desc *tx_desc;
1495 struct sk_buff *skb;
1496 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001497 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001498
hayeswangd84130a2014-02-18 21:49:02 +08001499 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001500 if (!skb)
1501 break;
1502
hayeswang60c89072014-03-07 11:04:39 +08001503 len = skb->len + sizeof(*tx_desc);
1504
1505 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001506 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001507 break;
1508 }
1509
hayeswang7937f9e2013-11-20 17:30:54 +08001510 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001511 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001512
1513 offset = (u32)skb_transport_offset(skb);
1514
hayeswang6128d1bb2014-03-07 11:04:40 +08001515 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1516 r8152_csum_workaround(tp, skb, &skb_head);
1517 continue;
1518 }
hayeswang60c89072014-03-07 11:04:39 +08001519
hayeswangb1379d92013-08-16 16:09:37 +08001520 tx_data += sizeof(*tx_desc);
1521
hayeswang60c89072014-03-07 11:04:39 +08001522 len = skb->len;
1523 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1524 struct net_device_stats *stats = &tp->netdev->stats;
1525
1526 stats->tx_dropped++;
1527 dev_kfree_skb_any(skb);
1528 tx_data -= sizeof(*tx_desc);
1529 continue;
1530 }
hayeswangb1379d92013-08-16 16:09:37 +08001531
hayeswang7937f9e2013-11-20 17:30:54 +08001532 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001533 agg->skb_len += len;
1534 agg->skb_num++;
1535
1536 dev_kfree_skb_any(skb);
1537
hayeswang7937f9e2013-11-20 17:30:54 +08001538 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001539 }
1540
hayeswangd84130a2014-02-18 21:49:02 +08001541 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001542 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001543 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001544 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001545 }
1546
hayeswang0c3121f2014-03-07 11:04:36 +08001547 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001548
1549 if (netif_queue_stopped(tp->netdev) &&
1550 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1551 netif_wake_queue(tp->netdev);
1552
hayeswang0c3121f2014-03-07 11:04:36 +08001553 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001554
hayeswang0c3121f2014-03-07 11:04:36 +08001555 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001556 if (ret < 0)
1557 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001558
hayeswangb1379d92013-08-16 16:09:37 +08001559 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1560 agg->head, (int)(tx_data - (u8 *)agg->head),
1561 (usb_complete_t)write_bulk_callback, agg);
1562
hayeswang0c3121f2014-03-07 11:04:36 +08001563 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001564 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001565 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001566
1567out_tx_fill:
1568 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001569}
1570
hayeswang565cab02014-03-07 11:04:38 +08001571static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1572{
1573 u8 checksum = CHECKSUM_NONE;
1574 u32 opts2, opts3;
1575
1576 if (tp->version == RTL_VER_01)
1577 goto return_result;
1578
1579 opts2 = le32_to_cpu(rx_desc->opts2);
1580 opts3 = le32_to_cpu(rx_desc->opts3);
1581
1582 if (opts2 & RD_IPV4_CS) {
1583 if (opts3 & IPF)
1584 checksum = CHECKSUM_NONE;
1585 else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
1586 checksum = CHECKSUM_NONE;
1587 else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
1588 checksum = CHECKSUM_NONE;
1589 else
1590 checksum = CHECKSUM_UNNECESSARY;
hayeswang6128d1bb2014-03-07 11:04:40 +08001591 } else if (RD_IPV6_CS) {
1592 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1593 checksum = CHECKSUM_UNNECESSARY;
1594 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1595 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001596 }
1597
1598return_result:
1599 return checksum;
1600}
1601
hayeswangebc2ec482013-08-14 20:54:38 +08001602static void rx_bottom(struct r8152 *tp)
1603{
hayeswanga5a4f462013-08-16 16:09:34 +08001604 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001605 struct list_head *cursor, *next, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +08001606
hayeswangd84130a2014-02-18 21:49:02 +08001607 if (list_empty(&tp->rx_done))
1608 return;
1609
1610 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001611 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001612 list_splice_init(&tp->rx_done, &rx_queue);
1613 spin_unlock_irqrestore(&tp->rx_lock, flags);
1614
1615 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001616 struct rx_desc *rx_desc;
1617 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001618 int len_used = 0;
1619 struct urb *urb;
1620 u8 *rx_data;
1621 int ret;
1622
hayeswangebc2ec482013-08-14 20:54:38 +08001623 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001624
1625 agg = list_entry(cursor, struct rx_agg, list);
1626 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001627 if (urb->actual_length < ETH_ZLEN)
1628 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001629
hayeswangebc2ec482013-08-14 20:54:38 +08001630 rx_desc = agg->head;
1631 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001632 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001633
hayeswang7937f9e2013-11-20 17:30:54 +08001634 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001635 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001636 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001637 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001638 struct sk_buff *skb;
1639
hayeswang7937f9e2013-11-20 17:30:54 +08001640 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001641 if (pkt_len < ETH_ZLEN)
1642 break;
1643
hayeswang7937f9e2013-11-20 17:30:54 +08001644 len_used += pkt_len;
1645 if (urb->actual_length < len_used)
1646 break;
1647
hayeswang8e1f51b2014-01-02 11:22:41 +08001648 pkt_len -= CRC_SIZE;
hayeswangebc2ec482013-08-14 20:54:38 +08001649 rx_data += sizeof(struct rx_desc);
1650
1651 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1652 if (!skb) {
1653 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001654 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08001655 }
hayeswang565cab02014-03-07 11:04:38 +08001656
1657 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001658 memcpy(skb->data, rx_data, pkt_len);
1659 skb_put(skb, pkt_len);
1660 skb->protocol = eth_type_trans(skb, netdev);
hayeswang9d9aafa2014-02-18 21:49:09 +08001661 netif_receive_skb(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001662 stats->rx_packets++;
1663 stats->rx_bytes += pkt_len;
1664
hayeswang5e2f7482014-03-07 11:04:37 +08001665find_next_rx:
hayeswang8e1f51b2014-01-02 11:22:41 +08001666 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec482013-08-14 20:54:38 +08001667 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001668 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001669 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001670 }
1671
hayeswang0de98f62013-08-16 16:09:35 +08001672submit:
hayeswangebc2ec482013-08-14 20:54:38 +08001673 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangebc2ec482013-08-14 20:54:38 +08001674 if (ret && ret != -ENODEV) {
hayeswangd84130a2014-02-18 21:49:02 +08001675 spin_lock_irqsave(&tp->rx_lock, flags);
1676 list_add_tail(&agg->list, &tp->rx_done);
1677 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001678 tasklet_schedule(&tp->tl);
1679 }
1680 }
hayeswangebc2ec482013-08-14 20:54:38 +08001681}
1682
1683static void tx_bottom(struct r8152 *tp)
1684{
hayeswangebc2ec482013-08-14 20:54:38 +08001685 int res;
1686
hayeswangb1379d92013-08-16 16:09:37 +08001687 do {
1688 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001689
hayeswangb1379d92013-08-16 16:09:37 +08001690 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001691 break;
1692
hayeswangb1379d92013-08-16 16:09:37 +08001693 agg = r8152_get_tx_agg(tp);
1694 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001695 break;
hayeswangb1379d92013-08-16 16:09:37 +08001696
1697 res = r8152_tx_agg_fill(tp, agg);
1698 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08001699 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08001700
1701 if (res == -ENODEV) {
1702 netif_device_detach(netdev);
1703 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08001704 struct net_device_stats *stats = &netdev->stats;
1705 unsigned long flags;
1706
hayeswangb1379d92013-08-16 16:09:37 +08001707 netif_warn(tp, tx_err, netdev,
1708 "failed tx_urb %d\n", res);
1709 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08001710
hayeswangb1379d92013-08-16 16:09:37 +08001711 spin_lock_irqsave(&tp->tx_lock, flags);
1712 list_add_tail(&agg->list, &tp->tx_free);
1713 spin_unlock_irqrestore(&tp->tx_lock, flags);
1714 }
hayeswangebc2ec482013-08-14 20:54:38 +08001715 }
hayeswangb1379d92013-08-16 16:09:37 +08001716 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001717}
1718
1719static void bottom_half(unsigned long data)
1720{
1721 struct r8152 *tp;
1722
1723 tp = (struct r8152 *)data;
1724
1725 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1726 return;
1727
1728 if (!test_bit(WORK_ENABLE, &tp->flags))
1729 return;
1730
hayeswang7559fb2f2013-08-16 16:09:38 +08001731 /* When link down, the driver would cancel all bulks. */
1732 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001733 if (!netif_carrier_ok(tp->netdev))
1734 return;
1735
1736 rx_bottom(tp);
hayeswang0c3121f2014-03-07 11:04:36 +08001737 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001738}
1739
1740static
1741int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1742{
1743 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1744 agg->head, rx_buf_sz,
1745 (usb_complete_t)read_bulk_callback, agg);
1746
1747 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001748}
1749
hayeswang00a5e362014-02-18 21:48:59 +08001750static void rtl_drop_queued_tx(struct r8152 *tp)
1751{
1752 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001753 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08001754 struct sk_buff *skb;
1755
hayeswangd84130a2014-02-18 21:49:02 +08001756 if (skb_queue_empty(tx_queue))
1757 return;
1758
1759 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001760 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001761 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001762 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001763
1764 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001765 dev_kfree_skb(skb);
1766 stats->tx_dropped++;
1767 }
1768}
1769
hayeswangac718b62013-05-02 16:01:25 +00001770static void rtl8152_tx_timeout(struct net_device *netdev)
1771{
1772 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001773 int i;
1774
Hayes Wang4a8deae2014-01-07 11:18:22 +08001775 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001776 for (i = 0; i < RTL8152_MAX_TX; i++)
1777 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001778}
1779
1780static void rtl8152_set_rx_mode(struct net_device *netdev)
1781{
1782 struct r8152 *tp = netdev_priv(netdev);
1783
hayeswang40a82912013-08-14 20:54:40 +08001784 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001785 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001786 schedule_delayed_work(&tp->schedule, 0);
1787 }
hayeswangac718b62013-05-02 16:01:25 +00001788}
1789
1790static void _rtl8152_set_rx_mode(struct net_device *netdev)
1791{
1792 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001793 u32 mc_filter[2]; /* Multicast hash filter */
1794 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001795 u32 ocp_data;
1796
hayeswangac718b62013-05-02 16:01:25 +00001797 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1798 netif_stop_queue(netdev);
1799 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1800 ocp_data &= ~RCR_ACPT_ALL;
1801 ocp_data |= RCR_AB | RCR_APM;
1802
1803 if (netdev->flags & IFF_PROMISC) {
1804 /* Unconditionally log net taps. */
1805 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1806 ocp_data |= RCR_AM | RCR_AAP;
1807 mc_filter[1] = mc_filter[0] = 0xffffffff;
1808 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1809 (netdev->flags & IFF_ALLMULTI)) {
1810 /* Too many to filter perfectly -- accept all multicasts. */
1811 ocp_data |= RCR_AM;
1812 mc_filter[1] = mc_filter[0] = 0xffffffff;
1813 } else {
1814 struct netdev_hw_addr *ha;
1815
1816 mc_filter[1] = mc_filter[0] = 0;
1817 netdev_for_each_mc_addr(ha, netdev) {
1818 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1819 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1820 ocp_data |= RCR_AM;
1821 }
1822 }
1823
hayeswang31787f52013-07-31 17:21:25 +08001824 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1825 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001826
hayeswang31787f52013-07-31 17:21:25 +08001827 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001828 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1829 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001830}
1831
1832static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswang0c3121f2014-03-07 11:04:36 +08001833 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00001834{
1835 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001836
hayeswangac718b62013-05-02 16:01:25 +00001837 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001838
hayeswang61598782013-11-20 17:30:55 +08001839 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001840
hayeswang0c3121f2014-03-07 11:04:36 +08001841 if (!list_empty(&tp->tx_free)) {
1842 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
1843 set_bit(SCHEDULE_TASKLET, &tp->flags);
1844 schedule_delayed_work(&tp->schedule, 0);
1845 } else {
1846 usb_mark_last_busy(tp->udev);
1847 tasklet_schedule(&tp->tl);
1848 }
1849 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
hayeswangdd1b1192013-11-20 17:30:56 +08001850 netif_stop_queue(netdev);
1851
hayeswangac718b62013-05-02 16:01:25 +00001852 return NETDEV_TX_OK;
1853}
1854
1855static void r8152b_reset_packet_filter(struct r8152 *tp)
1856{
1857 u32 ocp_data;
1858
1859 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1860 ocp_data &= ~FMC_FCR_MCU_EN;
1861 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1862 ocp_data |= FMC_FCR_MCU_EN;
1863 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1864}
1865
1866static void rtl8152_nic_reset(struct r8152 *tp)
1867{
1868 int i;
1869
1870 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1871
1872 for (i = 0; i < 1000; i++) {
1873 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1874 break;
1875 udelay(100);
1876 }
1877}
1878
hayeswangdd1b1192013-11-20 17:30:56 +08001879static void set_tx_qlen(struct r8152 *tp)
1880{
1881 struct net_device *netdev = tp->netdev;
1882
1883 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1884 sizeof(struct tx_desc));
1885}
1886
hayeswangac718b62013-05-02 16:01:25 +00001887static inline u8 rtl8152_get_speed(struct r8152 *tp)
1888{
1889 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1890}
1891
hayeswang507605a2014-01-02 11:22:43 +08001892static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001893{
hayeswangebc2ec482013-08-14 20:54:38 +08001894 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001895 u8 speed;
1896
1897 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001898 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001899 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001900 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001901 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1902 } else {
1903 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001904 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001905 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1906 }
hayeswang507605a2014-01-02 11:22:43 +08001907}
1908
hayeswang00a5e362014-02-18 21:48:59 +08001909static void rxdy_gated_en(struct r8152 *tp, bool enable)
1910{
1911 u32 ocp_data;
1912
1913 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1914 if (enable)
1915 ocp_data |= RXDY_GATED_EN;
1916 else
1917 ocp_data &= ~RXDY_GATED_EN;
1918 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1919}
1920
hayeswang507605a2014-01-02 11:22:43 +08001921static int rtl_enable(struct r8152 *tp)
1922{
1923 u32 ocp_data;
1924 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001925
1926 r8152b_reset_packet_filter(tp);
1927
1928 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1929 ocp_data |= CR_RE | CR_TE;
1930 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1931
hayeswang00a5e362014-02-18 21:48:59 +08001932 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001933
hayeswangebc2ec482013-08-14 20:54:38 +08001934 INIT_LIST_HEAD(&tp->rx_done);
1935 ret = 0;
1936 for (i = 0; i < RTL8152_MAX_RX; i++) {
1937 INIT_LIST_HEAD(&tp->rx_info[i].list);
1938 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1939 }
hayeswangac718b62013-05-02 16:01:25 +00001940
hayeswangebc2ec482013-08-14 20:54:38 +08001941 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001942}
1943
hayeswang507605a2014-01-02 11:22:43 +08001944static int rtl8152_enable(struct r8152 *tp)
1945{
1946 set_tx_qlen(tp);
1947 rtl_set_eee_plus(tp);
1948
1949 return rtl_enable(tp);
1950}
1951
hayeswang43779f82014-01-02 11:25:10 +08001952static void r8153_set_rx_agg(struct r8152 *tp)
1953{
1954 u8 speed;
1955
1956 speed = rtl8152_get_speed(tp);
1957 if (speed & _1000bps) {
1958 if (tp->udev->speed == USB_SPEED_SUPER) {
1959 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1960 RX_THR_SUPPER);
1961 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1962 EARLY_AGG_SUPPER);
1963 } else {
1964 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1965 RX_THR_HIGH);
1966 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1967 EARLY_AGG_HIGH);
1968 }
1969 } else {
1970 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1971 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1972 EARLY_AGG_SLOW);
1973 }
1974}
1975
1976static int rtl8153_enable(struct r8152 *tp)
1977{
1978 set_tx_qlen(tp);
1979 rtl_set_eee_plus(tp);
1980 r8153_set_rx_agg(tp);
1981
1982 return rtl_enable(tp);
1983}
1984
hayeswangac718b62013-05-02 16:01:25 +00001985static void rtl8152_disable(struct r8152 *tp)
1986{
hayeswangebc2ec482013-08-14 20:54:38 +08001987 u32 ocp_data;
1988 int i;
hayeswangac718b62013-05-02 16:01:25 +00001989
1990 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1991 ocp_data &= ~RCR_ACPT_ALL;
1992 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1993
hayeswang00a5e362014-02-18 21:48:59 +08001994 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001995
1996 for (i = 0; i < RTL8152_MAX_TX; i++)
1997 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001998
hayeswang00a5e362014-02-18 21:48:59 +08001999 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002000
2001 for (i = 0; i < 1000; i++) {
2002 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2003 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2004 break;
2005 mdelay(1);
2006 }
2007
2008 for (i = 0; i < 1000; i++) {
2009 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2010 break;
2011 mdelay(1);
2012 }
2013
hayeswangebc2ec482013-08-14 20:54:38 +08002014 for (i = 0; i < RTL8152_MAX_RX; i++)
2015 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002016
2017 rtl8152_nic_reset(tp);
2018}
2019
hayeswang00a5e362014-02-18 21:48:59 +08002020static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2021{
2022 u32 ocp_data;
2023
2024 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2025 if (enable)
2026 ocp_data |= POWER_CUT;
2027 else
2028 ocp_data &= ~POWER_CUT;
2029 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2030
2031 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2032 ocp_data &= ~RESUME_INDICATE;
2033 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002034}
2035
hayeswang21ff2e82014-02-18 21:49:06 +08002036#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2037
2038static u32 __rtl_get_wol(struct r8152 *tp)
2039{
2040 u32 ocp_data;
2041 u32 wolopts = 0;
2042
2043 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2044 if (!(ocp_data & LAN_WAKE_EN))
2045 return 0;
2046
2047 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2048 if (ocp_data & LINK_ON_WAKE_EN)
2049 wolopts |= WAKE_PHY;
2050
2051 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2052 if (ocp_data & UWF_EN)
2053 wolopts |= WAKE_UCAST;
2054 if (ocp_data & BWF_EN)
2055 wolopts |= WAKE_BCAST;
2056 if (ocp_data & MWF_EN)
2057 wolopts |= WAKE_MCAST;
2058
2059 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2060 if (ocp_data & MAGIC_EN)
2061 wolopts |= WAKE_MAGIC;
2062
2063 return wolopts;
2064}
2065
2066static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2067{
2068 u32 ocp_data;
2069
2070 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2071
2072 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2073 ocp_data &= ~LINK_ON_WAKE_EN;
2074 if (wolopts & WAKE_PHY)
2075 ocp_data |= LINK_ON_WAKE_EN;
2076 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2077
2078 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2079 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
2080 if (wolopts & WAKE_UCAST)
2081 ocp_data |= UWF_EN;
2082 if (wolopts & WAKE_BCAST)
2083 ocp_data |= BWF_EN;
2084 if (wolopts & WAKE_MCAST)
2085 ocp_data |= MWF_EN;
2086 if (wolopts & WAKE_ANY)
2087 ocp_data |= LAN_WAKE_EN;
2088 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2089
2090 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2091
2092 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2093 ocp_data &= ~MAGIC_EN;
2094 if (wolopts & WAKE_MAGIC)
2095 ocp_data |= MAGIC_EN;
2096 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2097
2098 if (wolopts & WAKE_ANY)
2099 device_set_wakeup_enable(&tp->udev->dev, true);
2100 else
2101 device_set_wakeup_enable(&tp->udev->dev, false);
2102}
2103
hayeswang9a4be1b2014-02-18 21:49:07 +08002104static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2105{
2106 if (enable) {
2107 u32 ocp_data;
2108
2109 __rtl_set_wol(tp, WAKE_ANY);
2110
2111 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2112
2113 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2114 ocp_data |= LINK_OFF_WAKE_EN;
2115 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2116
2117 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2118 } else {
2119 __rtl_set_wol(tp, tp->saved_wolopts);
2120 }
2121}
2122
hayeswangaa66a5f2014-02-18 21:49:04 +08002123static void rtl_phy_reset(struct r8152 *tp)
2124{
2125 u16 data;
2126 int i;
2127
2128 clear_bit(PHY_RESET, &tp->flags);
2129
2130 data = r8152_mdio_read(tp, MII_BMCR);
2131
2132 /* don't reset again before the previous one complete */
2133 if (data & BMCR_RESET)
2134 return;
2135
2136 data |= BMCR_RESET;
2137 r8152_mdio_write(tp, MII_BMCR, data);
2138
2139 for (i = 0; i < 50; i++) {
2140 msleep(20);
2141 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2142 break;
2143 }
2144}
2145
hayeswang43499682014-02-18 21:48:58 +08002146static void rtl_clear_bp(struct r8152 *tp)
2147{
2148 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
2149 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
2150 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
2151 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
2152 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
2153 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
2154 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
2155 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
2156 mdelay(3);
2157 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
2158 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
2159}
2160
2161static void r8153_clear_bp(struct r8152 *tp)
2162{
2163 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
2164 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
2165 rtl_clear_bp(tp);
2166}
2167
2168static void r8153_teredo_off(struct r8152 *tp)
2169{
2170 u32 ocp_data;
2171
2172 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2173 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
2174 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2175
2176 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2177 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2178 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2179}
2180
2181static void r8152b_disable_aldps(struct r8152 *tp)
2182{
2183 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
2184 msleep(20);
2185}
2186
2187static inline void r8152b_enable_aldps(struct r8152 *tp)
2188{
2189 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2190 LINKENA | DIS_SDSAVE);
2191}
2192
2193static void r8152b_hw_phy_cfg(struct r8152 *tp)
2194{
hayeswangf0cbe0a2014-02-18 21:49:03 +08002195 u16 data;
2196
2197 data = r8152_mdio_read(tp, MII_BMCR);
2198 if (data & BMCR_PDOWN) {
2199 data &= ~BMCR_PDOWN;
2200 r8152_mdio_write(tp, MII_BMCR, data);
2201 }
2202
hayeswang43499682014-02-18 21:48:58 +08002203 r8152b_disable_aldps(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002204
2205 rtl_clear_bp(tp);
2206
2207 r8152b_enable_aldps(tp);
hayeswangaa66a5f2014-02-18 21:49:04 +08002208 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002209}
2210
hayeswangac718b62013-05-02 16:01:25 +00002211static void r8152b_exit_oob(struct r8152 *tp)
2212{
hayeswangdb8515e2014-03-06 15:07:16 +08002213 u32 ocp_data;
2214 int i;
hayeswangac718b62013-05-02 16:01:25 +00002215
2216 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2217 ocp_data &= ~RCR_ACPT_ALL;
2218 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2219
hayeswang00a5e362014-02-18 21:48:59 +08002220 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002221 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002222 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002223
2224 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2225 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2226
2227 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2228 ocp_data &= ~NOW_IS_OOB;
2229 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2230
2231 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2232 ocp_data &= ~MCU_BORW_EN;
2233 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2234
2235 for (i = 0; i < 1000; i++) {
2236 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2237 if (ocp_data & LINK_LIST_READY)
2238 break;
2239 mdelay(1);
2240 }
2241
2242 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2243 ocp_data |= RE_INIT_LL;
2244 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2245
2246 for (i = 0; i < 1000; i++) {
2247 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2248 if (ocp_data & LINK_LIST_READY)
2249 break;
2250 mdelay(1);
2251 }
2252
2253 rtl8152_nic_reset(tp);
2254
2255 /* rx share fifo credit full threshold */
2256 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2257
2258 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
2259 ocp_data &= STAT_SPEED_MASK;
2260 if (ocp_data == STAT_SPEED_FULL) {
2261 /* rx share fifo credit near full threshold */
2262 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2263 RXFIFO_THR2_FULL);
2264 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2265 RXFIFO_THR3_FULL);
2266 } else {
2267 /* rx share fifo credit near full threshold */
2268 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2269 RXFIFO_THR2_HIGH);
2270 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2271 RXFIFO_THR3_HIGH);
2272 }
2273
2274 /* TX share fifo free credit full threshold */
2275 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2276
2277 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002278 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002279 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2280 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2281
2282 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2283 ocp_data &= ~CPCR_RX_VLAN;
2284 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2285
2286 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2287
2288 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2289 ocp_data |= TCR0_AUTO_FIFO;
2290 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2291}
2292
2293static void r8152b_enter_oob(struct r8152 *tp)
2294{
hayeswang45f4a192014-01-06 17:08:41 +08002295 u32 ocp_data;
2296 int i;
hayeswangac718b62013-05-02 16:01:25 +00002297
2298 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2299 ocp_data &= ~NOW_IS_OOB;
2300 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2301
2302 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2303 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2304 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2305
2306 rtl8152_disable(tp);
2307
2308 for (i = 0; i < 1000; i++) {
2309 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2310 if (ocp_data & LINK_LIST_READY)
2311 break;
2312 mdelay(1);
2313 }
2314
2315 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2316 ocp_data |= RE_INIT_LL;
2317 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2318
2319 for (i = 0; i < 1000; i++) {
2320 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2321 if (ocp_data & LINK_LIST_READY)
2322 break;
2323 mdelay(1);
2324 }
2325
2326 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2327
hayeswangac718b62013-05-02 16:01:25 +00002328 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2329 ocp_data |= CPCR_RX_VLAN;
2330 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2331
2332 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2333 ocp_data |= ALDPS_PROXY_MODE;
2334 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2335
2336 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2337 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2338 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2339
hayeswang00a5e362014-02-18 21:48:59 +08002340 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002341
2342 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2343 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2344 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2345}
2346
hayeswang43779f82014-01-02 11:25:10 +08002347static void r8153_hw_phy_cfg(struct r8152 *tp)
2348{
2349 u32 ocp_data;
2350 u16 data;
2351
2352 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002353 data = r8152_mdio_read(tp, MII_BMCR);
2354 if (data & BMCR_PDOWN) {
2355 data &= ~BMCR_PDOWN;
2356 r8152_mdio_write(tp, MII_BMCR, data);
2357 }
hayeswang43779f82014-01-02 11:25:10 +08002358
hayeswang7e9da482014-02-18 21:49:05 +08002359 r8153_clear_bp(tp);
2360
hayeswang43779f82014-01-02 11:25:10 +08002361 if (tp->version == RTL_VER_03) {
2362 data = ocp_reg_read(tp, OCP_EEE_CFG);
2363 data &= ~CTAP_SHORT_EN;
2364 ocp_reg_write(tp, OCP_EEE_CFG, data);
2365 }
2366
2367 data = ocp_reg_read(tp, OCP_POWER_CFG);
2368 data |= EEE_CLKDIV_EN;
2369 ocp_reg_write(tp, OCP_POWER_CFG, data);
2370
2371 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2372 data |= EN_10M_BGOFF;
2373 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2374 data = ocp_reg_read(tp, OCP_POWER_CFG);
2375 data |= EN_10M_PLLOFF;
2376 ocp_reg_write(tp, OCP_POWER_CFG, data);
2377 data = sram_read(tp, SRAM_IMPEDANCE);
2378 data &= ~RX_DRIVING_MASK;
2379 sram_write(tp, SRAM_IMPEDANCE, data);
2380
2381 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2382 ocp_data |= PFM_PWM_SWITCH;
2383 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2384
2385 data = sram_read(tp, SRAM_LPF_CFG);
2386 data |= LPF_AUTO_TUNE;
2387 sram_write(tp, SRAM_LPF_CFG, data);
2388
2389 data = sram_read(tp, SRAM_10M_AMP1);
2390 data |= GDAC_IB_UPALL;
2391 sram_write(tp, SRAM_10M_AMP1, data);
2392 data = sram_read(tp, SRAM_10M_AMP2);
2393 data |= AMP_DN;
2394 sram_write(tp, SRAM_10M_AMP2, data);
hayeswangaa66a5f2014-02-18 21:49:04 +08002395
2396 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002397}
2398
hayeswangb9702722014-02-18 21:49:00 +08002399static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002400{
2401 u8 u1u2[8];
2402
2403 if (enable)
2404 memset(u1u2, 0xff, sizeof(u1u2));
2405 else
2406 memset(u1u2, 0x00, sizeof(u1u2));
2407
2408 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2409}
2410
hayeswangb9702722014-02-18 21:49:00 +08002411static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002412{
2413 u32 ocp_data;
2414
2415 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2416 if (enable)
2417 ocp_data |= U2P3_ENABLE;
2418 else
2419 ocp_data &= ~U2P3_ENABLE;
2420 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2421}
2422
hayeswangb9702722014-02-18 21:49:00 +08002423static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002424{
2425 u32 ocp_data;
2426
2427 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2428 if (enable)
2429 ocp_data |= PWR_EN | PHASE2_EN;
2430 else
2431 ocp_data &= ~(PWR_EN | PHASE2_EN);
2432 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2433
2434 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2435 ocp_data &= ~PCUT_STATUS;
2436 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2437}
2438
hayeswang43779f82014-01-02 11:25:10 +08002439static void r8153_first_init(struct r8152 *tp)
2440{
2441 u32 ocp_data;
2442 int i;
2443
hayeswang00a5e362014-02-18 21:48:59 +08002444 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002445 r8153_teredo_off(tp);
2446
2447 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2448 ocp_data &= ~RCR_ACPT_ALL;
2449 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2450
2451 r8153_hw_phy_cfg(tp);
2452
2453 rtl8152_nic_reset(tp);
2454
2455 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2456 ocp_data &= ~NOW_IS_OOB;
2457 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2458
2459 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2460 ocp_data &= ~MCU_BORW_EN;
2461 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2462
2463 for (i = 0; i < 1000; i++) {
2464 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2465 if (ocp_data & LINK_LIST_READY)
2466 break;
2467 mdelay(1);
2468 }
2469
2470 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2471 ocp_data |= RE_INIT_LL;
2472 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2473
2474 for (i = 0; i < 1000; i++) {
2475 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2476 if (ocp_data & LINK_LIST_READY)
2477 break;
2478 mdelay(1);
2479 }
2480
2481 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2482 ocp_data &= ~CPCR_RX_VLAN;
2483 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2484
2485 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2486
2487 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2488 ocp_data |= TCR0_AUTO_FIFO;
2489 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2490
2491 rtl8152_nic_reset(tp);
2492
2493 /* rx share fifo credit full threshold */
2494 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2495 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2496 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2497 /* TX share fifo free credit full threshold */
2498 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2499
hayeswang9629e3c2014-01-15 10:42:15 +08002500 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002501 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2502 ocp_data &= ~RX_AGG_DISABLE;
2503 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2504}
2505
2506static void r8153_enter_oob(struct r8152 *tp)
2507{
2508 u32 ocp_data;
2509 int i;
2510
2511 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2512 ocp_data &= ~NOW_IS_OOB;
2513 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2514
2515 rtl8152_disable(tp);
2516
2517 for (i = 0; i < 1000; i++) {
2518 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2519 if (ocp_data & LINK_LIST_READY)
2520 break;
2521 mdelay(1);
2522 }
2523
2524 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2525 ocp_data |= RE_INIT_LL;
2526 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2527
2528 for (i = 0; i < 1000; i++) {
2529 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2530 if (ocp_data & LINK_LIST_READY)
2531 break;
2532 mdelay(1);
2533 }
2534
2535 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2536
hayeswang43779f82014-01-02 11:25:10 +08002537 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2538 ocp_data &= ~TEREDO_WAKE_MASK;
2539 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2540
2541 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2542 ocp_data |= CPCR_RX_VLAN;
2543 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2544
2545 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2546 ocp_data |= ALDPS_PROXY_MODE;
2547 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2548
2549 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2550 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2551 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2552
hayeswang00a5e362014-02-18 21:48:59 +08002553 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002554
2555 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2556 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2557 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2558}
2559
2560static void r8153_disable_aldps(struct r8152 *tp)
2561{
2562 u16 data;
2563
2564 data = ocp_reg_read(tp, OCP_POWER_CFG);
2565 data &= ~EN_ALDPS;
2566 ocp_reg_write(tp, OCP_POWER_CFG, data);
2567 msleep(20);
2568}
2569
2570static void r8153_enable_aldps(struct r8152 *tp)
2571{
2572 u16 data;
2573
2574 data = ocp_reg_read(tp, OCP_POWER_CFG);
2575 data |= EN_ALDPS;
2576 ocp_reg_write(tp, OCP_POWER_CFG, data);
2577}
2578
hayeswangac718b62013-05-02 16:01:25 +00002579static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2580{
hayeswang43779f82014-01-02 11:25:10 +08002581 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002582 int ret = 0;
2583
2584 cancel_delayed_work_sync(&tp->schedule);
2585 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2586 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2587 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002588 if (tp->mii.supports_gmii) {
2589 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2590 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2591 } else {
2592 gbcr = 0;
2593 }
hayeswangac718b62013-05-02 16:01:25 +00002594
2595 if (autoneg == AUTONEG_DISABLE) {
2596 if (speed == SPEED_10) {
2597 bmcr = 0;
2598 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2599 } else if (speed == SPEED_100) {
2600 bmcr = BMCR_SPEED100;
2601 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002602 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2603 bmcr = BMCR_SPEED1000;
2604 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002605 } else {
2606 ret = -EINVAL;
2607 goto out;
2608 }
2609
2610 if (duplex == DUPLEX_FULL)
2611 bmcr |= BMCR_FULLDPLX;
2612 } else {
2613 if (speed == SPEED_10) {
2614 if (duplex == DUPLEX_FULL)
2615 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2616 else
2617 anar |= ADVERTISE_10HALF;
2618 } else if (speed == SPEED_100) {
2619 if (duplex == DUPLEX_FULL) {
2620 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2621 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2622 } else {
2623 anar |= ADVERTISE_10HALF;
2624 anar |= ADVERTISE_100HALF;
2625 }
hayeswang43779f82014-01-02 11:25:10 +08002626 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2627 if (duplex == DUPLEX_FULL) {
2628 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2629 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2630 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2631 } else {
2632 anar |= ADVERTISE_10HALF;
2633 anar |= ADVERTISE_100HALF;
2634 gbcr |= ADVERTISE_1000HALF;
2635 }
hayeswangac718b62013-05-02 16:01:25 +00002636 } else {
2637 ret = -EINVAL;
2638 goto out;
2639 }
2640
2641 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2642 }
2643
hayeswangaa66a5f2014-02-18 21:49:04 +08002644 if (test_bit(PHY_RESET, &tp->flags))
2645 bmcr |= BMCR_RESET;
2646
hayeswang43779f82014-01-02 11:25:10 +08002647 if (tp->mii.supports_gmii)
2648 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2649
hayeswangac718b62013-05-02 16:01:25 +00002650 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2651 r8152_mdio_write(tp, MII_BMCR, bmcr);
2652
hayeswangaa66a5f2014-02-18 21:49:04 +08002653 if (test_bit(PHY_RESET, &tp->flags)) {
2654 int i;
2655
2656 clear_bit(PHY_RESET, &tp->flags);
2657 for (i = 0; i < 50; i++) {
2658 msleep(20);
2659 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2660 break;
2661 }
2662 }
2663
hayeswangac718b62013-05-02 16:01:25 +00002664out:
hayeswangac718b62013-05-02 16:01:25 +00002665
2666 return ret;
2667}
2668
2669static void rtl8152_down(struct r8152 *tp)
2670{
hayeswang00a5e362014-02-18 21:48:59 +08002671 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002672 r8152b_disable_aldps(tp);
2673 r8152b_enter_oob(tp);
2674 r8152b_enable_aldps(tp);
2675}
2676
hayeswang43779f82014-01-02 11:25:10 +08002677static void rtl8153_down(struct r8152 *tp)
2678{
hayeswangb9702722014-02-18 21:49:00 +08002679 r8153_u1u2en(tp, false);
2680 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002681 r8153_disable_aldps(tp);
2682 r8153_enter_oob(tp);
2683 r8153_enable_aldps(tp);
2684}
2685
hayeswangac718b62013-05-02 16:01:25 +00002686static void set_carrier(struct r8152 *tp)
2687{
2688 struct net_device *netdev = tp->netdev;
2689 u8 speed;
2690
hayeswang40a82912013-08-14 20:54:40 +08002691 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002692 speed = rtl8152_get_speed(tp);
2693
2694 if (speed & LINK_STATUS) {
2695 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002696 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002697 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2698 netif_carrier_on(netdev);
2699 }
2700 } else {
2701 if (tp->speed & LINK_STATUS) {
2702 netif_carrier_off(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002703 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002704 tp->rtl_ops.disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002705 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002706 }
2707 }
2708 tp->speed = speed;
2709}
2710
2711static void rtl_work_func_t(struct work_struct *work)
2712{
2713 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2714
hayeswang9a4be1b2014-02-18 21:49:07 +08002715 if (usb_autopm_get_interface(tp->intf) < 0)
2716 return;
2717
hayeswangac718b62013-05-02 16:01:25 +00002718 if (!test_bit(WORK_ENABLE, &tp->flags))
2719 goto out1;
2720
2721 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2722 goto out1;
2723
hayeswang40a82912013-08-14 20:54:40 +08002724 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2725 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002726
2727 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2728 _rtl8152_set_rx_mode(tp->netdev);
2729
hayeswang0c3121f2014-03-07 11:04:36 +08002730 if (test_bit(SCHEDULE_TASKLET, &tp->flags) &&
2731 (tp->speed & LINK_STATUS)) {
2732 clear_bit(SCHEDULE_TASKLET, &tp->flags);
2733 tasklet_schedule(&tp->tl);
2734 }
hayeswangaa66a5f2014-02-18 21:49:04 +08002735
2736 if (test_bit(PHY_RESET, &tp->flags))
2737 rtl_phy_reset(tp);
2738
hayeswangac718b62013-05-02 16:01:25 +00002739out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08002740 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002741}
2742
2743static int rtl8152_open(struct net_device *netdev)
2744{
2745 struct r8152 *tp = netdev_priv(netdev);
2746 int res = 0;
2747
hayeswang7e9da482014-02-18 21:49:05 +08002748 res = alloc_all_mem(tp);
2749 if (res)
2750 goto out;
2751
hayeswang9a4be1b2014-02-18 21:49:07 +08002752 res = usb_autopm_get_interface(tp->intf);
2753 if (res < 0) {
2754 free_all_mem(tp);
2755 goto out;
2756 }
2757
2758 /* The WORK_ENABLE may be set when autoresume occurs */
2759 if (test_bit(WORK_ENABLE, &tp->flags)) {
2760 clear_bit(WORK_ENABLE, &tp->flags);
2761 usb_kill_urb(tp->intr_urb);
2762 cancel_delayed_work_sync(&tp->schedule);
2763 if (tp->speed & LINK_STATUS)
2764 tp->rtl_ops.disable(tp);
2765 }
2766
hayeswang7e9da482014-02-18 21:49:05 +08002767 tp->rtl_ops.up(tp);
2768
hayeswang43779f82014-01-02 11:25:10 +08002769 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2770 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2771 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002772 tp->speed = 0;
2773 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002774 netif_start_queue(netdev);
2775 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08002776
hayeswang3d55f442014-02-06 11:55:48 +08002777 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2778 if (res) {
2779 if (res == -ENODEV)
2780 netif_device_detach(tp->netdev);
2781 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2782 res);
hayeswang7e9da482014-02-18 21:49:05 +08002783 free_all_mem(tp);
hayeswang3d55f442014-02-06 11:55:48 +08002784 }
2785
hayeswang9a4be1b2014-02-18 21:49:07 +08002786 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002787
hayeswang7e9da482014-02-18 21:49:05 +08002788out:
hayeswangac718b62013-05-02 16:01:25 +00002789 return res;
2790}
2791
2792static int rtl8152_close(struct net_device *netdev)
2793{
2794 struct r8152 *tp = netdev_priv(netdev);
2795 int res = 0;
2796
2797 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002798 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002799 cancel_delayed_work_sync(&tp->schedule);
2800 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002801
2802 res = usb_autopm_get_interface(tp->intf);
2803 if (res < 0) {
2804 rtl_drop_queued_tx(tp);
2805 } else {
2806 /*
2807 * The autosuspend may have been enabled and wouldn't
2808 * be disable when autoresume occurs, because the
2809 * netif_running() would be false.
2810 */
2811 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2812 rtl_runtime_suspend_enable(tp, false);
2813 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2814 }
2815
2816 tasklet_disable(&tp->tl);
2817 tp->rtl_ops.down(tp);
2818 tasklet_enable(&tp->tl);
2819 usb_autopm_put_interface(tp->intf);
2820 }
hayeswangac718b62013-05-02 16:01:25 +00002821
hayeswang7e9da482014-02-18 21:49:05 +08002822 free_all_mem(tp);
2823
hayeswangac718b62013-05-02 16:01:25 +00002824 return res;
2825}
2826
hayeswangac718b62013-05-02 16:01:25 +00002827static void r8152b_enable_eee(struct r8152 *tp)
2828{
hayeswang45f4a192014-01-06 17:08:41 +08002829 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002830
2831 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2832 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2833 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2834 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2835 EEE_10_CAP | EEE_NWAY_EN |
2836 TX_QUIET_EN | RX_QUIET_EN |
2837 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2838 SDFALLTIME);
2839 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2840 RG_LDVQUIET_EN | RG_CKRSEL |
2841 RG_EEEPRG_EN);
2842 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2843 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2844 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2845 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2846 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2847 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2848}
2849
hayeswang43779f82014-01-02 11:25:10 +08002850static void r8153_enable_eee(struct r8152 *tp)
2851{
2852 u32 ocp_data;
2853 u16 data;
2854
2855 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2856 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2857 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2858 data = ocp_reg_read(tp, OCP_EEE_CFG);
2859 data |= EEE10_EN;
2860 ocp_reg_write(tp, OCP_EEE_CFG, data);
2861 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2862 data |= MY1000_EEE | MY100_EEE;
2863 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2864}
2865
hayeswangac718b62013-05-02 16:01:25 +00002866static void r8152b_enable_fc(struct r8152 *tp)
2867{
2868 u16 anar;
2869
2870 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2871 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2872 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2873}
2874
hayeswangac718b62013-05-02 16:01:25 +00002875static void r8152b_init(struct r8152 *tp)
2876{
hayeswangebc2ec482013-08-14 20:54:38 +08002877 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002878
hayeswangac718b62013-05-02 16:01:25 +00002879 if (tp->version == RTL_VER_01) {
2880 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2881 ocp_data &= ~LED_MODE_MASK;
2882 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2883 }
2884
hayeswang00a5e362014-02-18 21:48:59 +08002885 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002886
hayeswangac718b62013-05-02 16:01:25 +00002887 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2888 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2889 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2890 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2891 ocp_data &= ~MCU_CLK_RATIO_MASK;
2892 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2893 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2894 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2895 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2896 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2897
2898 r8152b_enable_eee(tp);
2899 r8152b_enable_aldps(tp);
2900 r8152b_enable_fc(tp);
2901
hayeswangebc2ec482013-08-14 20:54:38 +08002902 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002903 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08002904 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002905 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2906}
2907
hayeswang43779f82014-01-02 11:25:10 +08002908static void r8153_init(struct r8152 *tp)
2909{
2910 u32 ocp_data;
2911 int i;
2912
hayeswangb9702722014-02-18 21:49:00 +08002913 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002914
2915 for (i = 0; i < 500; i++) {
2916 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2917 AUTOLOAD_DONE)
2918 break;
2919 msleep(20);
2920 }
2921
2922 for (i = 0; i < 500; i++) {
2923 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2924 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2925 break;
2926 msleep(20);
2927 }
2928
hayeswangb9702722014-02-18 21:49:00 +08002929 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002930
2931 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2932 ocp_data &= ~TIMER11_EN;
2933 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2934
hayeswang43779f82014-01-02 11:25:10 +08002935 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2936 ocp_data &= ~LED_MODE_MASK;
2937 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2938
2939 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2940 ocp_data &= ~LPM_TIMER_MASK;
2941 if (tp->udev->speed == USB_SPEED_SUPER)
2942 ocp_data |= LPM_TIMER_500US;
2943 else
2944 ocp_data |= LPM_TIMER_500MS;
2945 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2946
2947 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2948 ocp_data &= ~SEN_VAL_MASK;
2949 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2950 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2951
hayeswangb9702722014-02-18 21:49:00 +08002952 r8153_power_cut_en(tp, false);
2953 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002954
hayeswang43779f82014-01-02 11:25:10 +08002955 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2956 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2957 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2958 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2959 U1U2_SPDWN_EN | L1_SPDWN_EN);
2960 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2961 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2962 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2963 EEE_SPDWN_EN);
2964
2965 r8153_enable_eee(tp);
2966 r8153_enable_aldps(tp);
2967 r8152b_enable_fc(tp);
hayeswang43779f82014-01-02 11:25:10 +08002968}
2969
hayeswangac718b62013-05-02 16:01:25 +00002970static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2971{
2972 struct r8152 *tp = usb_get_intfdata(intf);
2973
hayeswang9a4be1b2014-02-18 21:49:07 +08002974 if (PMSG_IS_AUTO(message))
2975 set_bit(SELECTIVE_SUSPEND, &tp->flags);
2976 else
2977 netif_device_detach(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002978
2979 if (netif_running(tp->netdev)) {
2980 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002981 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002982 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08002983 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2984 rtl_runtime_suspend_enable(tp, true);
2985 } else {
2986 tasklet_disable(&tp->tl);
2987 tp->rtl_ops.down(tp);
2988 tasklet_enable(&tp->tl);
2989 }
hayeswangac718b62013-05-02 16:01:25 +00002990 }
2991
hayeswangac718b62013-05-02 16:01:25 +00002992 return 0;
2993}
2994
2995static int rtl8152_resume(struct usb_interface *intf)
2996{
2997 struct r8152 *tp = usb_get_intfdata(intf);
2998
hayeswang9a4be1b2014-02-18 21:49:07 +08002999 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3000 tp->rtl_ops.init(tp);
3001 netif_device_attach(tp->netdev);
3002 }
3003
hayeswangac718b62013-05-02 16:01:25 +00003004 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003005 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3006 rtl_runtime_suspend_enable(tp, false);
3007 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
3008 if (tp->speed & LINK_STATUS)
3009 tp->rtl_ops.disable(tp);
3010 } else {
3011 tp->rtl_ops.up(tp);
3012 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswang43779f82014-01-02 11:25:10 +08003013 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
3014 DUPLEX_FULL);
hayeswang9a4be1b2014-02-18 21:49:07 +08003015 }
hayeswang40a82912013-08-14 20:54:40 +08003016 tp->speed = 0;
3017 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00003018 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08003019 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangac718b62013-05-02 16:01:25 +00003020 }
3021
3022 return 0;
3023}
3024
hayeswang21ff2e82014-02-18 21:49:06 +08003025static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3026{
3027 struct r8152 *tp = netdev_priv(dev);
3028
hayeswang9a4be1b2014-02-18 21:49:07 +08003029 if (usb_autopm_get_interface(tp->intf) < 0)
3030 return;
3031
hayeswang21ff2e82014-02-18 21:49:06 +08003032 wol->supported = WAKE_ANY;
3033 wol->wolopts = __rtl_get_wol(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003034
3035 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08003036}
3037
3038static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3039{
3040 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003041 int ret;
3042
3043 ret = usb_autopm_get_interface(tp->intf);
3044 if (ret < 0)
3045 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08003046
3047 __rtl_set_wol(tp, wol->wolopts);
3048 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
3049
hayeswang9a4be1b2014-02-18 21:49:07 +08003050 usb_autopm_put_interface(tp->intf);
3051
3052out_set_wol:
3053 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08003054}
3055
hayeswanga5ec27c2014-02-18 21:49:11 +08003056static u32 rtl8152_get_msglevel(struct net_device *dev)
3057{
3058 struct r8152 *tp = netdev_priv(dev);
3059
3060 return tp->msg_enable;
3061}
3062
3063static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
3064{
3065 struct r8152 *tp = netdev_priv(dev);
3066
3067 tp->msg_enable = value;
3068}
3069
hayeswangac718b62013-05-02 16:01:25 +00003070static void rtl8152_get_drvinfo(struct net_device *netdev,
3071 struct ethtool_drvinfo *info)
3072{
3073 struct r8152 *tp = netdev_priv(netdev);
3074
3075 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
3076 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
3077 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
3078}
3079
3080static
3081int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
3082{
3083 struct r8152 *tp = netdev_priv(netdev);
3084
3085 if (!tp->mii.mdio_read)
3086 return -EOPNOTSUPP;
3087
3088 return mii_ethtool_gset(&tp->mii, cmd);
3089}
3090
3091static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3092{
3093 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003094 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003095
hayeswang9a4be1b2014-02-18 21:49:07 +08003096 ret = usb_autopm_get_interface(tp->intf);
3097 if (ret < 0)
3098 goto out;
3099
3100 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
3101
3102 usb_autopm_put_interface(tp->intf);
3103
3104out:
3105 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003106}
3107
3108static struct ethtool_ops ops = {
3109 .get_drvinfo = rtl8152_get_drvinfo,
3110 .get_settings = rtl8152_get_settings,
3111 .set_settings = rtl8152_set_settings,
3112 .get_link = ethtool_op_get_link,
hayeswanga5ec27c2014-02-18 21:49:11 +08003113 .get_msglevel = rtl8152_get_msglevel,
3114 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08003115 .get_wol = rtl8152_get_wol,
3116 .set_wol = rtl8152_set_wol,
hayeswangac718b62013-05-02 16:01:25 +00003117};
3118
3119static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
3120{
3121 struct r8152 *tp = netdev_priv(netdev);
3122 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08003123 int res;
3124
3125 res = usb_autopm_get_interface(tp->intf);
3126 if (res < 0)
3127 goto out;
hayeswangac718b62013-05-02 16:01:25 +00003128
3129 switch (cmd) {
3130 case SIOCGMIIPHY:
3131 data->phy_id = R8152_PHY_ID; /* Internal PHY */
3132 break;
3133
3134 case SIOCGMIIREG:
3135 data->val_out = r8152_mdio_read(tp, data->reg_num);
3136 break;
3137
3138 case SIOCSMIIREG:
3139 if (!capable(CAP_NET_ADMIN)) {
3140 res = -EPERM;
3141 break;
3142 }
3143 r8152_mdio_write(tp, data->reg_num, data->val_in);
3144 break;
3145
3146 default:
3147 res = -EOPNOTSUPP;
3148 }
3149
hayeswang9a4be1b2014-02-18 21:49:07 +08003150 usb_autopm_put_interface(tp->intf);
3151
3152out:
hayeswangac718b62013-05-02 16:01:25 +00003153 return res;
3154}
3155
3156static const struct net_device_ops rtl8152_netdev_ops = {
3157 .ndo_open = rtl8152_open,
3158 .ndo_stop = rtl8152_close,
3159 .ndo_do_ioctl = rtl8152_ioctl,
3160 .ndo_start_xmit = rtl8152_start_xmit,
3161 .ndo_tx_timeout = rtl8152_tx_timeout,
3162 .ndo_set_rx_mode = rtl8152_set_rx_mode,
3163 .ndo_set_mac_address = rtl8152_set_mac_address,
3164
3165 .ndo_change_mtu = eth_change_mtu,
3166 .ndo_validate_addr = eth_validate_addr,
3167};
3168
3169static void r8152b_get_version(struct r8152 *tp)
3170{
3171 u32 ocp_data;
3172 u16 version;
3173
3174 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3175 version = (u16)(ocp_data & VERSION_MASK);
3176
3177 switch (version) {
3178 case 0x4c00:
3179 tp->version = RTL_VER_01;
3180 break;
3181 case 0x4c10:
3182 tp->version = RTL_VER_02;
3183 break;
hayeswang43779f82014-01-02 11:25:10 +08003184 case 0x5c00:
3185 tp->version = RTL_VER_03;
3186 tp->mii.supports_gmii = 1;
3187 break;
3188 case 0x5c10:
3189 tp->version = RTL_VER_04;
3190 tp->mii.supports_gmii = 1;
3191 break;
3192 case 0x5c20:
3193 tp->version = RTL_VER_05;
3194 tp->mii.supports_gmii = 1;
3195 break;
hayeswangac718b62013-05-02 16:01:25 +00003196 default:
3197 netif_info(tp, probe, tp->netdev,
3198 "Unknown version 0x%04x\n", version);
3199 break;
3200 }
3201}
3202
hayeswange3fe0b12014-01-02 11:22:39 +08003203static void rtl8152_unload(struct r8152 *tp)
3204{
hayeswang00a5e362014-02-18 21:48:59 +08003205 if (tp->version != RTL_VER_01)
3206 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08003207}
3208
hayeswang43779f82014-01-02 11:25:10 +08003209static void rtl8153_unload(struct r8152 *tp)
3210{
hayeswangb9702722014-02-18 21:49:00 +08003211 r8153_power_cut_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003212}
3213
hayeswang31ca1de2014-01-06 17:08:43 +08003214static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
hayeswangc81229c2014-01-02 11:22:42 +08003215{
3216 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang31ca1de2014-01-06 17:08:43 +08003217 int ret = -ENODEV;
hayeswangc81229c2014-01-02 11:22:42 +08003218
3219 switch (id->idVendor) {
3220 case VENDOR_ID_REALTEK:
3221 switch (id->idProduct) {
3222 case PRODUCT_ID_RTL8152:
3223 ops->init = r8152b_init;
3224 ops->enable = rtl8152_enable;
3225 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003226 ops->up = r8152b_exit_oob;
hayeswangc81229c2014-01-02 11:22:42 +08003227 ops->down = rtl8152_down;
3228 ops->unload = rtl8152_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003229 ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08003230 break;
hayeswang43779f82014-01-02 11:25:10 +08003231 case PRODUCT_ID_RTL8153:
3232 ops->init = r8153_init;
3233 ops->enable = rtl8153_enable;
3234 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003235 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003236 ops->down = rtl8153_down;
3237 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003238 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003239 break;
3240 default:
hayeswang43779f82014-01-02 11:25:10 +08003241 break;
3242 }
3243 break;
3244
3245 case VENDOR_ID_SAMSUNG:
3246 switch (id->idProduct) {
3247 case PRODUCT_ID_SAMSUNG:
3248 ops->init = r8153_init;
3249 ops->enable = rtl8153_enable;
3250 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003251 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003252 ops->down = rtl8153_down;
3253 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003254 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003255 break;
hayeswangc81229c2014-01-02 11:22:42 +08003256 default:
hayeswangc81229c2014-01-02 11:22:42 +08003257 break;
3258 }
3259 break;
3260
3261 default:
hayeswangc81229c2014-01-02 11:22:42 +08003262 break;
3263 }
3264
hayeswang31ca1de2014-01-06 17:08:43 +08003265 if (ret)
3266 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
3267
hayeswangc81229c2014-01-02 11:22:42 +08003268 return ret;
3269}
3270
hayeswangac718b62013-05-02 16:01:25 +00003271static int rtl8152_probe(struct usb_interface *intf,
3272 const struct usb_device_id *id)
3273{
3274 struct usb_device *udev = interface_to_usbdev(intf);
3275 struct r8152 *tp;
3276 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08003277 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003278
hayeswangac718b62013-05-02 16:01:25 +00003279 netdev = alloc_etherdev(sizeof(struct r8152));
3280 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003281 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00003282 return -ENOMEM;
3283 }
3284
hayeswangebc2ec482013-08-14 20:54:38 +08003285 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00003286 tp = netdev_priv(netdev);
3287 tp->msg_enable = 0x7FFF;
3288
hayeswange3ad4122014-01-06 17:08:42 +08003289 tp->udev = udev;
3290 tp->netdev = netdev;
3291 tp->intf = intf;
3292
hayeswang31ca1de2014-01-06 17:08:43 +08003293 ret = rtl_ops_init(tp, id);
3294 if (ret)
3295 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08003296
hayeswangebc2ec482013-08-14 20:54:38 +08003297 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00003298 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3299
hayeswangac718b62013-05-02 16:01:25 +00003300 netdev->netdev_ops = &rtl8152_netdev_ops;
3301 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08003302
hayeswang60c89072014-03-07 11:04:39 +08003303 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08003304 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
3305 NETIF_F_TSO6;
hayeswang60c89072014-03-07 11:04:39 +08003306 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08003307 NETIF_F_TSO | NETIF_F_FRAGLIST |
3308 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08003309
hayeswangac718b62013-05-02 16:01:25 +00003310 SET_ETHTOOL_OPS(netdev, &ops);
hayeswang60c89072014-03-07 11:04:39 +08003311 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00003312
3313 tp->mii.dev = netdev;
3314 tp->mii.mdio_read = read_mii_word;
3315 tp->mii.mdio_write = write_mii_word;
3316 tp->mii.phy_id_mask = 0x3f;
3317 tp->mii.reg_num_mask = 0x1f;
3318 tp->mii.phy_id = R8152_PHY_ID;
3319 tp->mii.supports_gmii = 0;
3320
hayeswang9a4be1b2014-02-18 21:49:07 +08003321 intf->needs_remote_wakeup = 1;
3322
hayeswangac718b62013-05-02 16:01:25 +00003323 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08003324 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00003325 set_ethernet_addr(tp);
3326
hayeswangac718b62013-05-02 16:01:25 +00003327 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00003328
hayeswangebc2ec482013-08-14 20:54:38 +08003329 ret = register_netdev(netdev);
3330 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003331 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08003332 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00003333 }
3334
hayeswang21ff2e82014-02-18 21:49:06 +08003335 tp->saved_wolopts = __rtl_get_wol(tp);
3336 if (tp->saved_wolopts)
3337 device_set_wakeup_enable(&udev->dev, true);
3338 else
3339 device_set_wakeup_enable(&udev->dev, false);
3340
Hayes Wang4a8deae2014-01-07 11:18:22 +08003341 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00003342
3343 return 0;
3344
hayeswangac718b62013-05-02 16:01:25 +00003345out1:
hayeswangebc2ec482013-08-14 20:54:38 +08003346 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00003347out:
3348 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08003349 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003350}
3351
hayeswangac718b62013-05-02 16:01:25 +00003352static void rtl8152_disconnect(struct usb_interface *intf)
3353{
3354 struct r8152 *tp = usb_get_intfdata(intf);
3355
3356 usb_set_intfdata(intf, NULL);
3357 if (tp) {
3358 set_bit(RTL8152_UNPLUG, &tp->flags);
3359 tasklet_kill(&tp->tl);
3360 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08003361 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00003362 free_netdev(tp->netdev);
3363 }
3364}
3365
3366/* table of devices that work with this driver */
3367static struct usb_device_id rtl8152_table[] = {
hayeswangc7de7de2014-01-15 10:42:16 +08003368 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
3369 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
3370 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00003371 {}
3372};
3373
3374MODULE_DEVICE_TABLE(usb, rtl8152_table);
3375
3376static struct usb_driver rtl8152_driver = {
3377 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08003378 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00003379 .probe = rtl8152_probe,
3380 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00003381 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08003382 .resume = rtl8152_resume,
3383 .reset_resume = rtl8152_resume,
hayeswang9a4be1b2014-02-18 21:49:07 +08003384 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08003385 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00003386};
3387
Sachin Kamatb4236daa2013-05-16 17:48:08 +00003388module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00003389
3390MODULE_AUTHOR(DRIVER_AUTHOR);
3391MODULE_DESCRIPTION(DRIVER_DESC);
3392MODULE_LICENSE("GPL");