blob: 438fc6bcaef15538e9c151c46fce08dc67cec1a5 [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>
hayeswang4c4a6b12014-09-25 20:54:00 +080025#include <uapi/linux/mdio.h>
26#include <linux/mdio.h>
hayeswangd9a28c52014-12-04 10:43:11 +080027#include <linux/usb/cdc.h>
hayeswangac718b62013-05-02 16:01:25 +000028
29/* Version Information */
hayeswangd823ab62015-01-12 12:06:23 +080030#define DRIVER_VERSION "v1.08.0 (2015/01/13)"
hayeswangac718b62013-05-02 16:01:25 +000031#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080032#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000033#define MODULENAME "r8152"
34
35#define R8152_PHY_ID 32
36
37#define PLA_IDR 0xc000
38#define PLA_RCR 0xc010
39#define PLA_RMS 0xc016
40#define PLA_RXFIFO_CTRL0 0xc0a0
41#define PLA_RXFIFO_CTRL1 0xc0a4
42#define PLA_RXFIFO_CTRL2 0xc0a8
hayeswang65bab842015-02-12 16:20:46 +080043#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000044#define PLA_FMC 0xc0b4
45#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080046#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000047#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080048#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000049#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080050#define PLA_TEREDO_TIMER 0xd2cc
51#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000052#define PLA_LEDSEL 0xdd90
53#define PLA_LED_FEATURE 0xdd92
54#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000056#define PLA_GPHY_INTR_IMR 0xe022
57#define PLA_EEE_CR 0xe040
58#define PLA_EEEP_CR 0xe080
59#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080060#define PLA_MAC_PWR_CTRL2 0xe0ca
61#define PLA_MAC_PWR_CTRL3 0xe0cc
62#define PLA_MAC_PWR_CTRL4 0xe0ce
63#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000064#define PLA_TCR0 0xe610
65#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080066#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000067#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080068#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000069#define PLA_CR 0xe813
70#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080071#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
72#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000073#define PLA_CONFIG5 0xe822
74#define PLA_PHY_PWR 0xe84c
75#define PLA_OOB_CTRL 0xe84f
76#define PLA_CPCR 0xe854
77#define PLA_MISC_0 0xe858
78#define PLA_MISC_1 0xe85a
79#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +080080#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000081#define PLA_SFF_STS_7 0xe8de
82#define PLA_PHYSTATUS 0xe908
83#define PLA_BP_BA 0xfc26
84#define PLA_BP_0 0xfc28
85#define PLA_BP_1 0xfc2a
86#define PLA_BP_2 0xfc2c
87#define PLA_BP_3 0xfc2e
88#define PLA_BP_4 0xfc30
89#define PLA_BP_5 0xfc32
90#define PLA_BP_6 0xfc34
91#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080092#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000093
hayeswang65bab842015-02-12 16:20:46 +080094#define USB_USB2PHY 0xb41e
95#define USB_SSPHYLINK2 0xb428
hayeswang43779f82014-01-02 11:25:10 +080096#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +080097#define USB_CSR_DUMMY1 0xb464
98#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +000099#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800100#define USB_CONNECT_TIMER 0xcbf8
101#define USB_BURST_SIZE 0xcfc0
hayeswangac718b62013-05-02 16:01:25 +0000102#define USB_USB_CTRL 0xd406
103#define USB_PHY_CTRL 0xd408
104#define USB_TX_AGG 0xd40a
105#define USB_RX_BUF_TH 0xd40c
106#define USB_USB_TIMER 0xd428
hayeswang43779f82014-01-02 11:25:10 +0800107#define USB_RX_EARLY_AGG 0xd42c
hayeswangac718b62013-05-02 16:01:25 +0000108#define USB_PM_CTRL_STATUS 0xd432
109#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +0800110#define USB_TOLERANCE 0xd490
111#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +0000112#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800113#define USB_MISC_0 0xd81a
114#define USB_POWER_CUT 0xd80a
115#define USB_AFE_CTRL2 0xd824
116#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000117#define USB_BP_BA 0xfc26
118#define USB_BP_0 0xfc28
119#define USB_BP_1 0xfc2a
120#define USB_BP_2 0xfc2c
121#define USB_BP_3 0xfc2e
122#define USB_BP_4 0xfc30
123#define USB_BP_5 0xfc32
124#define USB_BP_6 0xfc34
125#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800126#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000127
128/* OCP Registers */
129#define OCP_ALDPS_CONFIG 0x2010
130#define OCP_EEE_CONFIG1 0x2080
131#define OCP_EEE_CONFIG2 0x2092
132#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800133#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000134#define OCP_EEE_AR 0xa41a
135#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800136#define OCP_PHY_STATUS 0xa420
137#define OCP_POWER_CFG 0xa430
138#define OCP_EEE_CFG 0xa432
139#define OCP_SRAM_ADDR 0xa436
140#define OCP_SRAM_DATA 0xa438
141#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800142#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800143#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800144#define OCP_EEE_LPABLE 0xa5d2
hayeswang43779f82014-01-02 11:25:10 +0800145#define OCP_ADC_CFG 0xbc06
146
147/* SRAM Register */
148#define SRAM_LPF_CFG 0x8012
149#define SRAM_10M_AMP1 0x8080
150#define SRAM_10M_AMP2 0x8082
151#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000152
153/* PLA_RCR */
154#define RCR_AAP 0x00000001
155#define RCR_APM 0x00000002
156#define RCR_AM 0x00000004
157#define RCR_AB 0x00000008
158#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
159
160/* PLA_RXFIFO_CTRL0 */
161#define RXFIFO_THR1_NORMAL 0x00080002
162#define RXFIFO_THR1_OOB 0x01800003
163
164/* PLA_RXFIFO_CTRL1 */
165#define RXFIFO_THR2_FULL 0x00000060
166#define RXFIFO_THR2_HIGH 0x00000038
167#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800168#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000169
170/* PLA_RXFIFO_CTRL2 */
171#define RXFIFO_THR3_FULL 0x00000078
172#define RXFIFO_THR3_HIGH 0x00000048
173#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800174#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000175
176/* PLA_TXFIFO_CTRL */
177#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800178#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000179
hayeswang65bab842015-02-12 16:20:46 +0800180/* PLA_DMY_REG0 */
181#define ECM_ALDPS 0x0002
182
hayeswangac718b62013-05-02 16:01:25 +0000183/* PLA_FMC */
184#define FMC_FCR_MCU_EN 0x0001
185
186/* PLA_EEEP_CR */
187#define EEEP_CR_EEEP_TX 0x0002
188
hayeswang43779f82014-01-02 11:25:10 +0800189/* PLA_WDT6_CTRL */
190#define WDT6_SET_MODE 0x0010
191
hayeswangac718b62013-05-02 16:01:25 +0000192/* PLA_TCR0 */
193#define TCR0_TX_EMPTY 0x0800
194#define TCR0_AUTO_FIFO 0x0080
195
196/* PLA_TCR1 */
197#define VERSION_MASK 0x7cf0
198
hayeswang69b4b7a2014-07-10 10:58:54 +0800199/* PLA_MTPS */
200#define MTPS_JUMBO (12 * 1024 / 64)
201#define MTPS_DEFAULT (6 * 1024 / 64)
202
hayeswang4f1d4d52014-03-11 16:24:19 +0800203/* PLA_RSTTALLY */
204#define TALLY_RESET 0x0001
205
hayeswangac718b62013-05-02 16:01:25 +0000206/* PLA_CR */
207#define CR_RST 0x10
208#define CR_RE 0x08
209#define CR_TE 0x04
210
211/* PLA_CRWECR */
212#define CRWECR_NORAML 0x00
213#define CRWECR_CONFIG 0xc0
214
215/* PLA_OOB_CTRL */
216#define NOW_IS_OOB 0x80
217#define TXFIFO_EMPTY 0x20
218#define RXFIFO_EMPTY 0x10
219#define LINK_LIST_READY 0x02
220#define DIS_MCU_CLROOB 0x01
221#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
222
223/* PLA_MISC_1 */
224#define RXDY_GATED_EN 0x0008
225
226/* PLA_SFF_STS_7 */
227#define RE_INIT_LL 0x8000
228#define MCU_BORW_EN 0x4000
229
230/* PLA_CPCR */
231#define CPCR_RX_VLAN 0x0040
232
233/* PLA_CFG_WOL */
234#define MAGIC_EN 0x0001
235
hayeswang43779f82014-01-02 11:25:10 +0800236/* PLA_TEREDO_CFG */
237#define TEREDO_SEL 0x8000
238#define TEREDO_WAKE_MASK 0x7f00
239#define TEREDO_RS_EVENT_MASK 0x00fe
240#define OOB_TEREDO_EN 0x0001
241
hayeswangac718b62013-05-02 16:01:25 +0000242/* PAL_BDC_CR */
243#define ALDPS_PROXY_MODE 0x0001
244
hayeswang21ff2e82014-02-18 21:49:06 +0800245/* PLA_CONFIG34 */
246#define LINK_ON_WAKE_EN 0x0010
247#define LINK_OFF_WAKE_EN 0x0008
248
hayeswangac718b62013-05-02 16:01:25 +0000249/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800250#define BWF_EN 0x0040
251#define MWF_EN 0x0020
252#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000253#define LAN_WAKE_EN 0x0002
254
255/* PLA_LED_FEATURE */
256#define LED_MODE_MASK 0x0700
257
258/* PLA_PHY_PWR */
259#define TX_10M_IDLE_EN 0x0080
260#define PFM_PWM_SWITCH 0x0040
261
262/* PLA_MAC_PWR_CTRL */
263#define D3_CLK_GATED_EN 0x00004000
264#define MCU_CLK_RATIO 0x07010f07
265#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800266#define ALDPS_SPDWN_RATIO 0x0f87
267
268/* PLA_MAC_PWR_CTRL2 */
269#define EEE_SPDWN_RATIO 0x8007
270
271/* PLA_MAC_PWR_CTRL3 */
272#define PKT_AVAIL_SPDWN_EN 0x0100
273#define SUSPEND_SPDWN_EN 0x0004
274#define U1U2_SPDWN_EN 0x0002
275#define L1_SPDWN_EN 0x0001
276
277/* PLA_MAC_PWR_CTRL4 */
278#define PWRSAVE_SPDWN_EN 0x1000
279#define RXDV_SPDWN_EN 0x0800
280#define TX10MIDLE_EN 0x0100
281#define TP100_SPDWN_EN 0x0020
282#define TP500_SPDWN_EN 0x0010
283#define TP1000_SPDWN_EN 0x0008
284#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000285
286/* PLA_GPHY_INTR_IMR */
287#define GPHY_STS_MSK 0x0001
288#define SPEED_DOWN_MSK 0x0002
289#define SPDWN_RXDV_MSK 0x0004
290#define SPDWN_LINKCHG_MSK 0x0008
291
292/* PLA_PHYAR */
293#define PHYAR_FLAG 0x80000000
294
295/* PLA_EEE_CR */
296#define EEE_RX_EN 0x0001
297#define EEE_TX_EN 0x0002
298
hayeswang43779f82014-01-02 11:25:10 +0800299/* PLA_BOOT_CTRL */
300#define AUTOLOAD_DONE 0x0002
301
hayeswang65bab842015-02-12 16:20:46 +0800302/* USB_USB2PHY */
303#define USB2PHY_SUSPEND 0x0001
304#define USB2PHY_L1 0x0002
305
306/* USB_SSPHYLINK2 */
307#define pwd_dn_scale_mask 0x3ffe
308#define pwd_dn_scale(x) ((x) << 1)
309
310/* USB_CSR_DUMMY1 */
311#define DYNAMIC_BURST 0x0001
312
313/* USB_CSR_DUMMY2 */
314#define EP4_FULL_FC 0x0001
315
hayeswangac718b62013-05-02 16:01:25 +0000316/* USB_DEV_STAT */
317#define STAT_SPEED_MASK 0x0006
318#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800319#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000320
321/* USB_TX_AGG */
322#define TX_AGG_MAX_THRESHOLD 0x03
323
324/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800325#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800326#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800327#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000328
329/* USB_TX_DMA */
330#define TEST_MODE_DISABLE 0x00000001
331#define TX_SIZE_ADJUST1 0x00000100
332
333/* USB_UPS_CTRL */
334#define POWER_CUT 0x0100
335
336/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800337#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000338
339/* USB_USB_CTRL */
340#define RX_AGG_DISABLE 0x0010
341
hayeswang43779f82014-01-02 11:25:10 +0800342/* USB_U2P3_CTRL */
343#define U2P3_ENABLE 0x0001
344
345/* USB_POWER_CUT */
346#define PWR_EN 0x0001
347#define PHASE2_EN 0x0008
348
349/* USB_MISC_0 */
350#define PCUT_STATUS 0x0001
351
352/* USB_RX_EARLY_AGG */
353#define EARLY_AGG_SUPPER 0x0e832981
354#define EARLY_AGG_HIGH 0x0e837a12
355#define EARLY_AGG_SLOW 0x0e83ffff
356
357/* USB_WDT11_CTRL */
358#define TIMER11_EN 0x0001
359
360/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800361/* bit 4 ~ 5: fifo empty boundary */
362#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
363/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800364#define LPM_TIMER_MASK 0x0c
365#define LPM_TIMER_500MS 0x04 /* 500 ms */
366#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800367#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800368
369/* USB_AFE_CTRL2 */
370#define SEN_VAL_MASK 0xf800
371#define SEN_VAL_NORMAL 0xa000
372#define SEL_RXIDLE 0x0100
373
hayeswangac718b62013-05-02 16:01:25 +0000374/* OCP_ALDPS_CONFIG */
375#define ENPWRSAVE 0x8000
376#define ENPDNPS 0x0200
377#define LINKENA 0x0100
378#define DIS_SDSAVE 0x0010
379
hayeswang43779f82014-01-02 11:25:10 +0800380/* OCP_PHY_STATUS */
381#define PHY_STAT_MASK 0x0007
382#define PHY_STAT_LAN_ON 3
383#define PHY_STAT_PWRDN 5
384
385/* OCP_POWER_CFG */
386#define EEE_CLKDIV_EN 0x8000
387#define EN_ALDPS 0x0004
388#define EN_10M_PLLOFF 0x0001
389
hayeswangac718b62013-05-02 16:01:25 +0000390/* OCP_EEE_CONFIG1 */
391#define RG_TXLPI_MSK_HFDUP 0x8000
392#define RG_MATCLR_EN 0x4000
393#define EEE_10_CAP 0x2000
394#define EEE_NWAY_EN 0x1000
395#define TX_QUIET_EN 0x0200
396#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800397#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800398#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000399#define RG_RXLPI_MSK_HFDUP 0x0008
400#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
401
402/* OCP_EEE_CONFIG2 */
403#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
404#define RG_DACQUIET_EN 0x0400
405#define RG_LDVQUIET_EN 0x0200
406#define RG_CKRSEL 0x0020
407#define RG_EEEPRG_EN 0x0010
408
409/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800410#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800411#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000412#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
413#define MSK_PH 0x0006 /* bit 0 ~ 3 */
414
415/* OCP_EEE_AR */
416/* bit[15:14] function */
417#define FUN_ADDR 0x0000
418#define FUN_DATA 0x4000
419/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000420
hayeswang43779f82014-01-02 11:25:10 +0800421/* OCP_EEE_CFG */
422#define CTAP_SHORT_EN 0x0040
423#define EEE10_EN 0x0010
424
425/* OCP_DOWN_SPEED */
426#define EN_10M_BGOFF 0x0080
427
hayeswang43779f82014-01-02 11:25:10 +0800428/* OCP_ADC_CFG */
429#define CKADSEL_L 0x0100
430#define ADC_EN 0x0080
431#define EN_EMI_L 0x0040
432
433/* SRAM_LPF_CFG */
434#define LPF_AUTO_TUNE 0x8000
435
436/* SRAM_10M_AMP1 */
437#define GDAC_IB_UPALL 0x0008
438
439/* SRAM_10M_AMP2 */
440#define AMP_DN 0x0200
441
442/* SRAM_IMPEDANCE */
443#define RX_DRIVING_MASK 0x6000
444
hayeswangac718b62013-05-02 16:01:25 +0000445enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800446 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000447 _100bps = 0x08,
448 _10bps = 0x04,
449 LINK_STATUS = 0x02,
450 FULL_DUP = 0x01,
451};
452
hayeswang1764bcd2014-08-28 10:24:18 +0800453#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800454#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800455#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800456#define CRC_SIZE 4
457#define TX_ALIGN 4
458#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800459
460#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800461
hayeswangac718b62013-05-02 16:01:25 +0000462#define RTL8152_REQT_READ 0xc0
463#define RTL8152_REQT_WRITE 0x40
464#define RTL8152_REQ_GET_REGS 0x05
465#define RTL8152_REQ_SET_REGS 0x05
466
467#define BYTE_EN_DWORD 0xff
468#define BYTE_EN_WORD 0x33
469#define BYTE_EN_BYTE 0x11
470#define BYTE_EN_SIX_BYTES 0x3f
471#define BYTE_EN_START_MASK 0x0f
472#define BYTE_EN_END_MASK 0xf0
473
hayeswang69b4b7a2014-07-10 10:58:54 +0800474#define RTL8153_MAX_PACKET 9216 /* 9K */
475#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
hayeswangac718b62013-05-02 16:01:25 +0000476#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800477#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800478#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800479#define RTL8152_NAPI_WEIGHT 64
hayeswangac718b62013-05-02 16:01:25 +0000480
481/* rtl8152 flags */
482enum rtl8152_flags {
483 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000484 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800485 WORK_ENABLE,
486 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800487 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800488 PHY_RESET,
hayeswangd823ab62015-01-12 12:06:23 +0800489 SCHEDULE_NAPI,
hayeswangac718b62013-05-02 16:01:25 +0000490};
491
492/* Define these values to match your device */
493#define VENDOR_ID_REALTEK 0x0bda
hayeswang43779f82014-01-02 11:25:10 +0800494#define VENDOR_ID_SAMSUNG 0x04e8
hayeswangac718b62013-05-02 16:01:25 +0000495
496#define MCU_TYPE_PLA 0x0100
497#define MCU_TYPE_USB 0x0000
498
hayeswang4f1d4d52014-03-11 16:24:19 +0800499struct tally_counter {
500 __le64 tx_packets;
501 __le64 rx_packets;
502 __le64 tx_errors;
503 __le32 rx_errors;
504 __le16 rx_missed;
505 __le16 align_errors;
506 __le32 tx_one_collision;
507 __le32 tx_multi_collision;
508 __le64 rx_unicast;
509 __le64 rx_broadcast;
510 __le32 rx_multicast;
511 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800512 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800513};
514
hayeswangac718b62013-05-02 16:01:25 +0000515struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800516 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000517#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800518
hayeswang500b6d72013-11-20 17:30:57 +0800519 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800520#define RD_UDP_CS BIT(23)
521#define RD_TCP_CS BIT(22)
522#define RD_IPV6_CS BIT(20)
523#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800524
hayeswang500b6d72013-11-20 17:30:57 +0800525 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800526#define IPF BIT(23) /* IP checksum fail */
527#define UDPF BIT(22) /* UDP checksum fail */
528#define TCPF BIT(21) /* TCP checksum fail */
529#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800530
hayeswang500b6d72013-11-20 17:30:57 +0800531 __le32 opts4;
532 __le32 opts5;
533 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000534};
535
536struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800537 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800538#define TX_FS BIT(31) /* First segment of a packet */
539#define TX_LS BIT(30) /* Final segment of a packet */
540#define GTSENDV4 BIT(28)
541#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800542#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800543#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800544#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800545
hayeswang500b6d72013-11-20 17:30:57 +0800546 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800547#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
548#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
549#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
550#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800551#define MSS_SHIFT 17
552#define MSS_MAX 0x7ffU
553#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800554#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800555#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000556};
557
hayeswangdff4e8a2013-08-16 16:09:33 +0800558struct r8152;
559
hayeswangebc2ec482013-08-14 20:54:38 +0800560struct rx_agg {
561 struct list_head list;
562 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800563 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800564 void *buffer;
565 void *head;
566};
567
568struct tx_agg {
569 struct list_head list;
570 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800571 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800572 void *buffer;
573 void *head;
574 u32 skb_num;
575 u32 skb_len;
576};
577
hayeswangac718b62013-05-02 16:01:25 +0000578struct r8152 {
579 unsigned long flags;
580 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800581 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800582 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000583 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800584 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800585 struct tx_agg tx_info[RTL8152_MAX_TX];
586 struct rx_agg rx_info[RTL8152_MAX_RX];
587 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800588 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800589 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000590 struct delayed_work schedule;
591 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800592 struct mutex control; /* use for hw setting */
hayeswangc81229c2014-01-02 11:22:42 +0800593
594 struct rtl_ops {
595 void (*init)(struct r8152 *);
596 int (*enable)(struct r8152 *);
597 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800598 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800599 void (*down)(struct r8152 *);
600 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800601 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
602 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswangc81229c2014-01-02 11:22:42 +0800603 } rtl_ops;
604
hayeswang40a82912013-08-14 20:54:40 +0800605 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800606 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000607 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800608 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000609 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800610 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000611 u8 version;
hayeswangac718b62013-05-02 16:01:25 +0000612};
613
614enum rtl_version {
615 RTL_VER_UNKNOWN = 0,
616 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800617 RTL_VER_02,
618 RTL_VER_03,
619 RTL_VER_04,
620 RTL_VER_05,
621 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000622};
623
hayeswang60c89072014-03-07 11:04:39 +0800624enum tx_csum_stat {
625 TX_CSUM_SUCCESS = 0,
626 TX_CSUM_TSO,
627 TX_CSUM_NONE
628};
629
hayeswangac718b62013-05-02 16:01:25 +0000630/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
631 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
632 */
633static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800634static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000635
hayeswang52aec122014-09-02 10:27:52 +0800636#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswang60c89072014-03-07 11:04:39 +0800637 VLAN_ETH_HLEN - VLAN_HLEN)
638
hayeswangac718b62013-05-02 16:01:25 +0000639static
640int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
641{
hayeswang31787f52013-07-31 17:21:25 +0800642 int ret;
643 void *tmp;
644
645 tmp = kmalloc(size, GFP_KERNEL);
646 if (!tmp)
647 return -ENOMEM;
648
649 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800650 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
651 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800652
653 memcpy(data, tmp, size);
654 kfree(tmp);
655
656 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000657}
658
659static
660int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
661{
hayeswang31787f52013-07-31 17:21:25 +0800662 int ret;
663 void *tmp;
664
Benoit Tainec4438f02014-05-26 17:21:23 +0200665 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800666 if (!tmp)
667 return -ENOMEM;
668
hayeswang31787f52013-07-31 17:21:25 +0800669 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800670 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
671 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800672
673 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800674
hayeswang31787f52013-07-31 17:21:25 +0800675 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000676}
677
678static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800679 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000680{
hayeswang45f4a192014-01-06 17:08:41 +0800681 u16 limit = 64;
682 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000683
684 if (test_bit(RTL8152_UNPLUG, &tp->flags))
685 return -ENODEV;
686
687 /* both size and indix must be 4 bytes align */
688 if ((size & 3) || !size || (index & 3) || !data)
689 return -EPERM;
690
691 if ((u32)index + (u32)size > 0xffff)
692 return -EPERM;
693
694 while (size) {
695 if (size > limit) {
696 ret = get_registers(tp, index, type, limit, data);
697 if (ret < 0)
698 break;
699
700 index += limit;
701 data += limit;
702 size -= limit;
703 } else {
704 ret = get_registers(tp, index, type, size, data);
705 if (ret < 0)
706 break;
707
708 index += size;
709 data += size;
710 size = 0;
711 break;
712 }
713 }
714
hayeswang67610492014-10-30 11:46:40 +0800715 if (ret == -ENODEV)
716 set_bit(RTL8152_UNPLUG, &tp->flags);
717
hayeswangac718b62013-05-02 16:01:25 +0000718 return ret;
719}
720
721static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800722 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000723{
hayeswang45f4a192014-01-06 17:08:41 +0800724 int ret;
725 u16 byteen_start, byteen_end, byen;
726 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000727
728 if (test_bit(RTL8152_UNPLUG, &tp->flags))
729 return -ENODEV;
730
731 /* both size and indix must be 4 bytes align */
732 if ((size & 3) || !size || (index & 3) || !data)
733 return -EPERM;
734
735 if ((u32)index + (u32)size > 0xffff)
736 return -EPERM;
737
738 byteen_start = byteen & BYTE_EN_START_MASK;
739 byteen_end = byteen & BYTE_EN_END_MASK;
740
741 byen = byteen_start | (byteen_start << 4);
742 ret = set_registers(tp, index, type | byen, 4, data);
743 if (ret < 0)
744 goto error1;
745
746 index += 4;
747 data += 4;
748 size -= 4;
749
750 if (size) {
751 size -= 4;
752
753 while (size) {
754 if (size > limit) {
755 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800756 type | BYTE_EN_DWORD,
757 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000758 if (ret < 0)
759 goto error1;
760
761 index += limit;
762 data += limit;
763 size -= limit;
764 } else {
765 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800766 type | BYTE_EN_DWORD,
767 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000768 if (ret < 0)
769 goto error1;
770
771 index += size;
772 data += size;
773 size = 0;
774 break;
775 }
776 }
777
778 byen = byteen_end | (byteen_end >> 4);
779 ret = set_registers(tp, index, type | byen, 4, data);
780 if (ret < 0)
781 goto error1;
782 }
783
784error1:
hayeswang67610492014-10-30 11:46:40 +0800785 if (ret == -ENODEV)
786 set_bit(RTL8152_UNPLUG, &tp->flags);
787
hayeswangac718b62013-05-02 16:01:25 +0000788 return ret;
789}
790
791static inline
792int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
793{
794 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
795}
796
797static inline
798int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
799{
800 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
801}
802
803static inline
804int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
805{
806 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
807}
808
809static inline
810int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
811{
812 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
813}
814
815static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
816{
hayeswangc8826de2013-07-31 17:21:26 +0800817 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000818
hayeswangc8826de2013-07-31 17:21:26 +0800819 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000820
821 return __le32_to_cpu(data);
822}
823
824static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
825{
hayeswangc8826de2013-07-31 17:21:26 +0800826 __le32 tmp = __cpu_to_le32(data);
827
828 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000829}
830
831static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
832{
833 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800834 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000835 u8 shift = index & 2;
836
837 index &= ~3;
838
hayeswangc8826de2013-07-31 17:21:26 +0800839 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000840
hayeswangc8826de2013-07-31 17:21:26 +0800841 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000842 data >>= (shift * 8);
843 data &= 0xffff;
844
845 return (u16)data;
846}
847
848static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
849{
hayeswangc8826de2013-07-31 17:21:26 +0800850 u32 mask = 0xffff;
851 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000852 u16 byen = BYTE_EN_WORD;
853 u8 shift = index & 2;
854
855 data &= mask;
856
857 if (index & 2) {
858 byen <<= shift;
859 mask <<= (shift * 8);
860 data <<= (shift * 8);
861 index &= ~3;
862 }
863
hayeswangc8826de2013-07-31 17:21:26 +0800864 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000865
hayeswangc8826de2013-07-31 17:21:26 +0800866 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000867}
868
869static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
870{
871 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800872 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000873 u8 shift = index & 3;
874
875 index &= ~3;
876
hayeswangc8826de2013-07-31 17:21:26 +0800877 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000878
hayeswangc8826de2013-07-31 17:21:26 +0800879 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000880 data >>= (shift * 8);
881 data &= 0xff;
882
883 return (u8)data;
884}
885
886static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
887{
hayeswangc8826de2013-07-31 17:21:26 +0800888 u32 mask = 0xff;
889 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000890 u16 byen = BYTE_EN_BYTE;
891 u8 shift = index & 3;
892
893 data &= mask;
894
895 if (index & 3) {
896 byen <<= shift;
897 mask <<= (shift * 8);
898 data <<= (shift * 8);
899 index &= ~3;
900 }
901
hayeswangc8826de2013-07-31 17:21:26 +0800902 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000903
hayeswangc8826de2013-07-31 17:21:26 +0800904 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000905}
906
hayeswangac244d32014-01-02 11:22:40 +0800907static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
908{
909 u16 ocp_base, ocp_index;
910
911 ocp_base = addr & 0xf000;
912 if (ocp_base != tp->ocp_base) {
913 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
914 tp->ocp_base = ocp_base;
915 }
916
917 ocp_index = (addr & 0x0fff) | 0xb000;
918 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
919}
920
hayeswange3fe0b12014-01-02 11:22:39 +0800921static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
922{
923 u16 ocp_base, ocp_index;
924
925 ocp_base = addr & 0xf000;
926 if (ocp_base != tp->ocp_base) {
927 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
928 tp->ocp_base = ocp_base;
929 }
930
931 ocp_index = (addr & 0x0fff) | 0xb000;
932 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
933}
934
hayeswangac244d32014-01-02 11:22:40 +0800935static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000936{
hayeswangac244d32014-01-02 11:22:40 +0800937 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000938}
939
hayeswangac244d32014-01-02 11:22:40 +0800940static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000941{
hayeswangac244d32014-01-02 11:22:40 +0800942 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000943}
944
hayeswang43779f82014-01-02 11:25:10 +0800945static void sram_write(struct r8152 *tp, u16 addr, u16 data)
946{
947 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
948 ocp_reg_write(tp, OCP_SRAM_DATA, data);
949}
950
hayeswangac718b62013-05-02 16:01:25 +0000951static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
952{
953 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +0800954 int ret;
hayeswangac718b62013-05-02 16:01:25 +0000955
hayeswang68714382014-04-11 17:54:31 +0800956 if (test_bit(RTL8152_UNPLUG, &tp->flags))
957 return -ENODEV;
958
hayeswangac718b62013-05-02 16:01:25 +0000959 if (phy_id != R8152_PHY_ID)
960 return -EINVAL;
961
hayeswang9a4be1b2014-02-18 21:49:07 +0800962 ret = r8152_mdio_read(tp, reg);
963
hayeswang9a4be1b2014-02-18 21:49:07 +0800964 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000965}
966
967static
968void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
969{
970 struct r8152 *tp = netdev_priv(netdev);
971
hayeswang68714382014-04-11 17:54:31 +0800972 if (test_bit(RTL8152_UNPLUG, &tp->flags))
973 return;
974
hayeswangac718b62013-05-02 16:01:25 +0000975 if (phy_id != R8152_PHY_ID)
976 return;
977
978 r8152_mdio_write(tp, reg, val);
979}
980
hayeswangb209af92014-08-25 15:53:00 +0800981static int
982r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800983
hayeswang8ba789a2014-09-04 16:15:41 +0800984static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
985{
986 struct r8152 *tp = netdev_priv(netdev);
987 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +0800988 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +0800989
990 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +0800991 goto out1;
992
993 ret = usb_autopm_get_interface(tp->intf);
994 if (ret < 0)
995 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +0800996
hayeswangb5403272014-10-09 18:00:26 +0800997 mutex_lock(&tp->control);
998
hayeswang8ba789a2014-09-04 16:15:41 +0800999 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1000
1001 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1002 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1003 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1004
hayeswangb5403272014-10-09 18:00:26 +08001005 mutex_unlock(&tp->control);
1006
hayeswangea6a7112014-10-02 17:03:12 +08001007 usb_autopm_put_interface(tp->intf);
1008out1:
1009 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001010}
1011
hayeswang179bb6d2014-09-04 16:15:42 +08001012static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001013{
1014 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001015 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001016 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001017
hayeswang8a91c822014-02-18 21:49:01 +08001018 if (tp->version == RTL_VER_01)
hayeswang179bb6d2014-09-04 16:15:42 +08001019 ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001020 else
hayeswang179bb6d2014-09-04 16:15:42 +08001021 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001022
1023 if (ret < 0) {
hayeswang179bb6d2014-09-04 16:15:42 +08001024 netif_err(tp, probe, dev, "Get ether addr fail\n");
1025 } else if (!is_valid_ether_addr(sa.sa_data)) {
1026 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1027 sa.sa_data);
1028 eth_hw_addr_random(dev);
1029 ether_addr_copy(sa.sa_data, dev->dev_addr);
1030 ret = rtl8152_set_mac_address(dev, &sa);
1031 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1032 sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001033 } else {
hayeswang179bb6d2014-09-04 16:15:42 +08001034 if (tp->version == RTL_VER_01)
1035 ether_addr_copy(dev->dev_addr, sa.sa_data);
1036 else
1037 ret = rtl8152_set_mac_address(dev, &sa);
hayeswangac718b62013-05-02 16:01:25 +00001038 }
hayeswang179bb6d2014-09-04 16:15:42 +08001039
1040 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001041}
1042
hayeswangac718b62013-05-02 16:01:25 +00001043static void read_bulk_callback(struct urb *urb)
1044{
hayeswangac718b62013-05-02 16:01:25 +00001045 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001046 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001047 struct rx_agg *agg;
1048 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +00001049
hayeswangebc2ec482013-08-14 20:54:38 +08001050 agg = urb->context;
1051 if (!agg)
1052 return;
1053
1054 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001055 if (!tp)
1056 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001057
hayeswangac718b62013-05-02 16:01:25 +00001058 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1059 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001060
1061 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001062 return;
1063
hayeswangebc2ec482013-08-14 20:54:38 +08001064 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001065
1066 /* When link down, the driver would cancel all bulks. */
1067 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001068 if (!netif_carrier_ok(netdev))
1069 return;
1070
hayeswang9a4be1b2014-02-18 21:49:07 +08001071 usb_mark_last_busy(tp->udev);
1072
hayeswangac718b62013-05-02 16:01:25 +00001073 switch (status) {
1074 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001075 if (urb->actual_length < ETH_ZLEN)
1076 break;
1077
hayeswang2685d412014-03-07 11:04:34 +08001078 spin_lock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001079 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001080 spin_unlock(&tp->rx_lock);
hayeswangd823ab62015-01-12 12:06:23 +08001081 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001082 return;
hayeswangac718b62013-05-02 16:01:25 +00001083 case -ESHUTDOWN:
1084 set_bit(RTL8152_UNPLUG, &tp->flags);
1085 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001086 return;
hayeswangac718b62013-05-02 16:01:25 +00001087 case -ENOENT:
1088 return; /* the urb is in unlink state */
1089 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001090 if (net_ratelimit())
1091 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001092 break;
hayeswangac718b62013-05-02 16:01:25 +00001093 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001094 if (net_ratelimit())
1095 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001096 break;
hayeswangac718b62013-05-02 16:01:25 +00001097 }
1098
hayeswanga0fccd42014-11-20 10:29:05 +08001099 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001100}
1101
1102static void write_bulk_callback(struct urb *urb)
1103{
hayeswangebc2ec482013-08-14 20:54:38 +08001104 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001105 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001106 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001107 struct r8152 *tp;
1108 int status = urb->status;
1109
hayeswangebc2ec482013-08-14 20:54:38 +08001110 agg = urb->context;
1111 if (!agg)
1112 return;
1113
1114 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001115 if (!tp)
1116 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001117
hayeswangd104eaf2014-03-06 15:07:17 +08001118 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001119 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001120 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001121 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001122 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001123 stats->tx_errors += agg->skb_num;
1124 } else {
1125 stats->tx_packets += agg->skb_num;
1126 stats->tx_bytes += agg->skb_len;
1127 }
1128
hayeswang2685d412014-03-07 11:04:34 +08001129 spin_lock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001130 list_add_tail(&agg->list, &tp->tx_free);
hayeswang2685d412014-03-07 11:04:34 +08001131 spin_unlock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001132
hayeswang9a4be1b2014-02-18 21:49:07 +08001133 usb_autopm_put_interface_async(tp->intf);
1134
hayeswangd104eaf2014-03-06 15:07:17 +08001135 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001136 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001137
1138 if (!test_bit(WORK_ENABLE, &tp->flags))
1139 return;
1140
1141 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1142 return;
1143
1144 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001145 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001146}
1147
hayeswang40a82912013-08-14 20:54:40 +08001148static void intr_callback(struct urb *urb)
1149{
1150 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001151 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001152 int status = urb->status;
1153 int res;
1154
1155 tp = urb->context;
1156 if (!tp)
1157 return;
1158
1159 if (!test_bit(WORK_ENABLE, &tp->flags))
1160 return;
1161
1162 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1163 return;
1164
1165 switch (status) {
1166 case 0: /* success */
1167 break;
1168 case -ECONNRESET: /* unlink */
1169 case -ESHUTDOWN:
1170 netif_device_detach(tp->netdev);
1171 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001172 case -EPROTO:
1173 netif_info(tp, intr, tp->netdev,
1174 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001175 return;
1176 case -EOVERFLOW:
1177 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1178 goto resubmit;
1179 /* -EPIPE: should clear the halt */
1180 default:
1181 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1182 goto resubmit;
1183 }
1184
1185 d = urb->transfer_buffer;
1186 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001187 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001188 set_bit(RTL8152_LINK_CHG, &tp->flags);
1189 schedule_delayed_work(&tp->schedule, 0);
1190 }
1191 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001192 if (netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001193 set_bit(RTL8152_LINK_CHG, &tp->flags);
1194 schedule_delayed_work(&tp->schedule, 0);
1195 }
1196 }
1197
1198resubmit:
1199 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001200 if (res == -ENODEV) {
1201 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001202 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001203 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001204 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001205 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001206 }
hayeswang40a82912013-08-14 20:54:40 +08001207}
1208
hayeswangebc2ec482013-08-14 20:54:38 +08001209static inline void *rx_agg_align(void *data)
1210{
hayeswang8e1f51b2014-01-02 11:22:41 +08001211 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001212}
1213
1214static inline void *tx_agg_align(void *data)
1215{
hayeswang8e1f51b2014-01-02 11:22:41 +08001216 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001217}
1218
1219static void free_all_mem(struct r8152 *tp)
1220{
1221 int i;
1222
1223 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001224 usb_free_urb(tp->rx_info[i].urb);
1225 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001226
hayeswang9629e3c2014-01-15 10:42:15 +08001227 kfree(tp->rx_info[i].buffer);
1228 tp->rx_info[i].buffer = NULL;
1229 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001230 }
1231
1232 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001233 usb_free_urb(tp->tx_info[i].urb);
1234 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001235
hayeswang9629e3c2014-01-15 10:42:15 +08001236 kfree(tp->tx_info[i].buffer);
1237 tp->tx_info[i].buffer = NULL;
1238 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001239 }
hayeswang40a82912013-08-14 20:54:40 +08001240
hayeswang9629e3c2014-01-15 10:42:15 +08001241 usb_free_urb(tp->intr_urb);
1242 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001243
hayeswang9629e3c2014-01-15 10:42:15 +08001244 kfree(tp->intr_buff);
1245 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001246}
1247
1248static int alloc_all_mem(struct r8152 *tp)
1249{
1250 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001251 struct usb_interface *intf = tp->intf;
1252 struct usb_host_interface *alt = intf->cur_altsetting;
1253 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001254 struct urb *urb;
1255 int node, i;
1256 u8 *buf;
1257
1258 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1259
1260 spin_lock_init(&tp->rx_lock);
1261 spin_lock_init(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001262 INIT_LIST_HEAD(&tp->tx_free);
1263 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001264 skb_queue_head_init(&tp->rx_queue);
hayeswangebc2ec482013-08-14 20:54:38 +08001265
1266 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001267 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001268 if (!buf)
1269 goto err1;
1270
1271 if (buf != rx_agg_align(buf)) {
1272 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001273 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001274 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001275 if (!buf)
1276 goto err1;
1277 }
1278
1279 urb = usb_alloc_urb(0, GFP_KERNEL);
1280 if (!urb) {
1281 kfree(buf);
1282 goto err1;
1283 }
1284
1285 INIT_LIST_HEAD(&tp->rx_info[i].list);
1286 tp->rx_info[i].context = tp;
1287 tp->rx_info[i].urb = urb;
1288 tp->rx_info[i].buffer = buf;
1289 tp->rx_info[i].head = rx_agg_align(buf);
1290 }
1291
1292 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001293 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001294 if (!buf)
1295 goto err1;
1296
1297 if (buf != tx_agg_align(buf)) {
1298 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001299 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001300 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001301 if (!buf)
1302 goto err1;
1303 }
1304
1305 urb = usb_alloc_urb(0, GFP_KERNEL);
1306 if (!urb) {
1307 kfree(buf);
1308 goto err1;
1309 }
1310
1311 INIT_LIST_HEAD(&tp->tx_info[i].list);
1312 tp->tx_info[i].context = tp;
1313 tp->tx_info[i].urb = urb;
1314 tp->tx_info[i].buffer = buf;
1315 tp->tx_info[i].head = tx_agg_align(buf);
1316
1317 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1318 }
1319
hayeswang40a82912013-08-14 20:54:40 +08001320 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1321 if (!tp->intr_urb)
1322 goto err1;
1323
1324 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1325 if (!tp->intr_buff)
1326 goto err1;
1327
1328 tp->intr_interval = (int)ep_intr->desc.bInterval;
1329 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001330 tp->intr_buff, INTBUFSIZE, intr_callback,
1331 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001332
hayeswangebc2ec482013-08-14 20:54:38 +08001333 return 0;
1334
1335err1:
1336 free_all_mem(tp);
1337 return -ENOMEM;
1338}
1339
hayeswang0de98f62013-08-16 16:09:35 +08001340static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1341{
1342 struct tx_agg *agg = NULL;
1343 unsigned long flags;
1344
hayeswang21949ab2014-03-07 11:04:35 +08001345 if (list_empty(&tp->tx_free))
1346 return NULL;
1347
hayeswang0de98f62013-08-16 16:09:35 +08001348 spin_lock_irqsave(&tp->tx_lock, flags);
1349 if (!list_empty(&tp->tx_free)) {
1350 struct list_head *cursor;
1351
1352 cursor = tp->tx_free.next;
1353 list_del_init(cursor);
1354 agg = list_entry(cursor, struct tx_agg, list);
1355 }
1356 spin_unlock_irqrestore(&tp->tx_lock, flags);
1357
1358 return agg;
1359}
1360
hayeswangb209af92014-08-25 15:53:00 +08001361/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001362 * The hw limites the value the transport offset. When the offset is out of the
1363 * range, calculate the checksum by sw.
1364 */
1365static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1366 struct sk_buff_head *list)
1367{
1368 if (skb_shinfo(skb)->gso_size) {
1369 netdev_features_t features = tp->netdev->features;
1370 struct sk_buff_head seg_list;
1371 struct sk_buff *segs, *nskb;
1372
hayeswanga91d45f2014-07-11 16:48:27 +08001373 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001374 segs = skb_gso_segment(skb, features);
1375 if (IS_ERR(segs) || !segs)
1376 goto drop;
1377
1378 __skb_queue_head_init(&seg_list);
1379
1380 do {
1381 nskb = segs;
1382 segs = segs->next;
1383 nskb->next = NULL;
1384 __skb_queue_tail(&seg_list, nskb);
1385 } while (segs);
1386
1387 skb_queue_splice(&seg_list, list);
1388 dev_kfree_skb(skb);
1389 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1390 if (skb_checksum_help(skb) < 0)
1391 goto drop;
1392
1393 __skb_queue_head(list, skb);
1394 } else {
1395 struct net_device_stats *stats;
1396
1397drop:
1398 stats = &tp->netdev->stats;
1399 stats->tx_dropped++;
1400 dev_kfree_skb(skb);
1401 }
1402}
1403
hayeswangb209af92014-08-25 15:53:00 +08001404/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001405 * According to the document of microsoft, the TCP Pseudo Header excludes the
1406 * packet length for IPv6 TCP large packets.
1407 */
1408static int msdn_giant_send_check(struct sk_buff *skb)
1409{
1410 const struct ipv6hdr *ipv6h;
1411 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001412 int ret;
1413
1414 ret = skb_cow_head(skb, 0);
1415 if (ret)
1416 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001417
1418 ipv6h = ipv6_hdr(skb);
1419 th = tcp_hdr(skb);
1420
1421 th->check = 0;
1422 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1423
hayeswangfcb308d2014-03-11 10:20:32 +08001424 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001425}
1426
hayeswangc5554292014-09-12 10:43:11 +08001427static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1428{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001429 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001430 u32 opts2;
1431
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001432 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001433 desc->opts2 |= cpu_to_le32(opts2);
1434 }
1435}
1436
1437static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1438{
1439 u32 opts2 = le32_to_cpu(desc->opts2);
1440
1441 if (opts2 & RX_VLAN_TAG)
1442 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1443 swab16(opts2 & 0xffff));
1444}
1445
hayeswang60c89072014-03-07 11:04:39 +08001446static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1447 struct sk_buff *skb, u32 len, u32 transport_offset)
1448{
1449 u32 mss = skb_shinfo(skb)->gso_size;
1450 u32 opts1, opts2 = 0;
1451 int ret = TX_CSUM_SUCCESS;
1452
1453 WARN_ON_ONCE(len > TX_LEN_MAX);
1454
1455 opts1 = len | TX_FS | TX_LS;
1456
1457 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001458 if (transport_offset > GTTCPHO_MAX) {
1459 netif_warn(tp, tx_err, tp->netdev,
1460 "Invalid transport offset 0x%x for TSO\n",
1461 transport_offset);
1462 ret = TX_CSUM_TSO;
1463 goto unavailable;
1464 }
1465
hayeswang6e74d172015-02-06 11:30:50 +08001466 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001467 case htons(ETH_P_IP):
1468 opts1 |= GTSENDV4;
1469 break;
1470
hayeswang6128d1bb2014-03-07 11:04:40 +08001471 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001472 if (msdn_giant_send_check(skb)) {
1473 ret = TX_CSUM_TSO;
1474 goto unavailable;
1475 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001476 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001477 break;
1478
hayeswang60c89072014-03-07 11:04:39 +08001479 default:
1480 WARN_ON_ONCE(1);
1481 break;
1482 }
1483
1484 opts1 |= transport_offset << GTTCPHO_SHIFT;
1485 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1486 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001487 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001488
hayeswang6128d1bb2014-03-07 11:04:40 +08001489 if (transport_offset > TCPHO_MAX) {
1490 netif_warn(tp, tx_err, tp->netdev,
1491 "Invalid transport offset 0x%x\n",
1492 transport_offset);
1493 ret = TX_CSUM_NONE;
1494 goto unavailable;
1495 }
1496
hayeswang6e74d172015-02-06 11:30:50 +08001497 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001498 case htons(ETH_P_IP):
1499 opts2 |= IPV4_CS;
1500 ip_protocol = ip_hdr(skb)->protocol;
1501 break;
1502
1503 case htons(ETH_P_IPV6):
1504 opts2 |= IPV6_CS;
1505 ip_protocol = ipv6_hdr(skb)->nexthdr;
1506 break;
1507
1508 default:
1509 ip_protocol = IPPROTO_RAW;
1510 break;
1511 }
1512
hayeswang60c89072014-03-07 11:04:39 +08001513 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001514 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001515 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001516 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001517 else
hayeswang5bd23882013-08-14 20:54:39 +08001518 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001519
hayeswang60c89072014-03-07 11:04:39 +08001520 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001521 }
hayeswang60c89072014-03-07 11:04:39 +08001522
1523 desc->opts2 = cpu_to_le32(opts2);
1524 desc->opts1 = cpu_to_le32(opts1);
1525
hayeswang6128d1bb2014-03-07 11:04:40 +08001526unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001527 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001528}
1529
hayeswangb1379d92013-08-16 16:09:37 +08001530static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1531{
hayeswangd84130a2014-02-18 21:49:02 +08001532 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001533 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001534 u8 *tx_data;
1535
hayeswangd84130a2014-02-18 21:49:02 +08001536 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001537 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001538 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001539 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001540
hayeswangb1379d92013-08-16 16:09:37 +08001541 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001542 agg->skb_num = 0;
1543 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001544 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001545
hayeswang7937f9e2013-11-20 17:30:54 +08001546 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001547 struct tx_desc *tx_desc;
1548 struct sk_buff *skb;
1549 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001550 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001551
hayeswangd84130a2014-02-18 21:49:02 +08001552 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001553 if (!skb)
1554 break;
1555
hayeswang60c89072014-03-07 11:04:39 +08001556 len = skb->len + sizeof(*tx_desc);
1557
1558 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001559 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001560 break;
1561 }
1562
hayeswang7937f9e2013-11-20 17:30:54 +08001563 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001564 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001565
1566 offset = (u32)skb_transport_offset(skb);
1567
hayeswang6128d1bb2014-03-07 11:04:40 +08001568 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1569 r8152_csum_workaround(tp, skb, &skb_head);
1570 continue;
1571 }
hayeswang60c89072014-03-07 11:04:39 +08001572
hayeswangc5554292014-09-12 10:43:11 +08001573 rtl_tx_vlan_tag(tx_desc, skb);
1574
hayeswangb1379d92013-08-16 16:09:37 +08001575 tx_data += sizeof(*tx_desc);
1576
hayeswang60c89072014-03-07 11:04:39 +08001577 len = skb->len;
1578 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1579 struct net_device_stats *stats = &tp->netdev->stats;
1580
1581 stats->tx_dropped++;
1582 dev_kfree_skb_any(skb);
1583 tx_data -= sizeof(*tx_desc);
1584 continue;
1585 }
hayeswangb1379d92013-08-16 16:09:37 +08001586
hayeswang7937f9e2013-11-20 17:30:54 +08001587 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001588 agg->skb_len += len;
1589 agg->skb_num++;
1590
1591 dev_kfree_skb_any(skb);
1592
hayeswang52aec122014-09-02 10:27:52 +08001593 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001594 }
1595
hayeswangd84130a2014-02-18 21:49:02 +08001596 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001597 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001598 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001599 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001600 }
1601
hayeswang0c3121f2014-03-07 11:04:36 +08001602 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001603
1604 if (netif_queue_stopped(tp->netdev) &&
1605 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1606 netif_wake_queue(tp->netdev);
1607
hayeswang0c3121f2014-03-07 11:04:36 +08001608 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001609
hayeswang0c3121f2014-03-07 11:04:36 +08001610 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001611 if (ret < 0)
1612 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001613
hayeswangb1379d92013-08-16 16:09:37 +08001614 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1615 agg->head, (int)(tx_data - (u8 *)agg->head),
1616 (usb_complete_t)write_bulk_callback, agg);
1617
hayeswang0c3121f2014-03-07 11:04:36 +08001618 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001619 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001620 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001621
1622out_tx_fill:
1623 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001624}
1625
hayeswang565cab02014-03-07 11:04:38 +08001626static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1627{
1628 u8 checksum = CHECKSUM_NONE;
1629 u32 opts2, opts3;
1630
1631 if (tp->version == RTL_VER_01)
1632 goto return_result;
1633
1634 opts2 = le32_to_cpu(rx_desc->opts2);
1635 opts3 = le32_to_cpu(rx_desc->opts3);
1636
1637 if (opts2 & RD_IPV4_CS) {
1638 if (opts3 & IPF)
1639 checksum = CHECKSUM_NONE;
1640 else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
1641 checksum = CHECKSUM_NONE;
1642 else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
1643 checksum = CHECKSUM_NONE;
1644 else
1645 checksum = CHECKSUM_UNNECESSARY;
hayeswang6128d1bb2014-03-07 11:04:40 +08001646 } else if (RD_IPV6_CS) {
1647 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1648 checksum = CHECKSUM_UNNECESSARY;
1649 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1650 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001651 }
1652
1653return_result:
1654 return checksum;
1655}
1656
hayeswangd823ab62015-01-12 12:06:23 +08001657static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08001658{
hayeswanga5a4f462013-08-16 16:09:34 +08001659 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001660 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08001661 int ret = 0, work_done = 0;
hayeswangd823ab62015-01-12 12:06:23 +08001662
1663 if (!skb_queue_empty(&tp->rx_queue)) {
1664 while (work_done < budget) {
1665 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1666 struct net_device *netdev = tp->netdev;
1667 struct net_device_stats *stats = &netdev->stats;
1668 unsigned int pkt_len;
1669
1670 if (!skb)
1671 break;
1672
1673 pkt_len = skb->len;
1674 napi_gro_receive(&tp->napi, skb);
1675 work_done++;
1676 stats->rx_packets++;
1677 stats->rx_bytes += pkt_len;
1678 }
1679 }
hayeswangebc2ec482013-08-14 20:54:38 +08001680
hayeswangd84130a2014-02-18 21:49:02 +08001681 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08001682 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08001683
1684 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001685 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001686 list_splice_init(&tp->rx_done, &rx_queue);
1687 spin_unlock_irqrestore(&tp->rx_lock, flags);
1688
1689 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001690 struct rx_desc *rx_desc;
1691 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001692 int len_used = 0;
1693 struct urb *urb;
1694 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08001695
hayeswangebc2ec482013-08-14 20:54:38 +08001696 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001697
1698 agg = list_entry(cursor, struct rx_agg, list);
1699 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001700 if (urb->actual_length < ETH_ZLEN)
1701 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001702
hayeswangebc2ec482013-08-14 20:54:38 +08001703 rx_desc = agg->head;
1704 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001705 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001706
hayeswang7937f9e2013-11-20 17:30:54 +08001707 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001708 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001709 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001710 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001711 struct sk_buff *skb;
1712
hayeswang7937f9e2013-11-20 17:30:54 +08001713 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001714 if (pkt_len < ETH_ZLEN)
1715 break;
1716
hayeswang7937f9e2013-11-20 17:30:54 +08001717 len_used += pkt_len;
1718 if (urb->actual_length < len_used)
1719 break;
1720
hayeswang8e1f51b2014-01-02 11:22:41 +08001721 pkt_len -= CRC_SIZE;
hayeswangebc2ec482013-08-14 20:54:38 +08001722 rx_data += sizeof(struct rx_desc);
1723
1724 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1725 if (!skb) {
1726 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001727 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08001728 }
hayeswang565cab02014-03-07 11:04:38 +08001729
1730 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001731 memcpy(skb->data, rx_data, pkt_len);
1732 skb_put(skb, pkt_len);
1733 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08001734 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001735 if (work_done < budget) {
1736 napi_gro_receive(&tp->napi, skb);
1737 work_done++;
1738 stats->rx_packets++;
1739 stats->rx_bytes += pkt_len;
1740 } else {
1741 __skb_queue_tail(&tp->rx_queue, skb);
1742 }
hayeswangebc2ec482013-08-14 20:54:38 +08001743
hayeswang5e2f7482014-03-07 11:04:37 +08001744find_next_rx:
hayeswang8e1f51b2014-01-02 11:22:41 +08001745 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec482013-08-14 20:54:38 +08001746 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001747 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001748 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001749 }
1750
hayeswang0de98f62013-08-16 16:09:35 +08001751submit:
hayeswange1a2ca92015-02-06 11:30:45 +08001752 if (!ret) {
1753 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1754 } else {
1755 urb->actual_length = 0;
1756 list_add_tail(&agg->list, next);
1757 }
1758 }
1759
1760 if (!list_empty(&rx_queue)) {
1761 spin_lock_irqsave(&tp->rx_lock, flags);
1762 list_splice_tail(&rx_queue, &tp->rx_done);
1763 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001764 }
hayeswangd823ab62015-01-12 12:06:23 +08001765
1766out1:
1767 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08001768}
1769
1770static void tx_bottom(struct r8152 *tp)
1771{
hayeswangebc2ec482013-08-14 20:54:38 +08001772 int res;
1773
hayeswangb1379d92013-08-16 16:09:37 +08001774 do {
1775 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001776
hayeswangb1379d92013-08-16 16:09:37 +08001777 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001778 break;
1779
hayeswangb1379d92013-08-16 16:09:37 +08001780 agg = r8152_get_tx_agg(tp);
1781 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001782 break;
hayeswangb1379d92013-08-16 16:09:37 +08001783
1784 res = r8152_tx_agg_fill(tp, agg);
1785 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08001786 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08001787
1788 if (res == -ENODEV) {
hayeswang67610492014-10-30 11:46:40 +08001789 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswangb1379d92013-08-16 16:09:37 +08001790 netif_device_detach(netdev);
1791 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08001792 struct net_device_stats *stats = &netdev->stats;
1793 unsigned long flags;
1794
hayeswangb1379d92013-08-16 16:09:37 +08001795 netif_warn(tp, tx_err, netdev,
1796 "failed tx_urb %d\n", res);
1797 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08001798
hayeswangb1379d92013-08-16 16:09:37 +08001799 spin_lock_irqsave(&tp->tx_lock, flags);
1800 list_add_tail(&agg->list, &tp->tx_free);
1801 spin_unlock_irqrestore(&tp->tx_lock, flags);
1802 }
hayeswangebc2ec482013-08-14 20:54:38 +08001803 }
hayeswangb1379d92013-08-16 16:09:37 +08001804 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001805}
1806
hayeswangd823ab62015-01-12 12:06:23 +08001807static void bottom_half(struct r8152 *tp)
hayeswangebc2ec482013-08-14 20:54:38 +08001808{
hayeswangebc2ec482013-08-14 20:54:38 +08001809 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1810 return;
1811
1812 if (!test_bit(WORK_ENABLE, &tp->flags))
1813 return;
1814
hayeswang7559fb2f2013-08-16 16:09:38 +08001815 /* When link down, the driver would cancel all bulks. */
1816 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001817 if (!netif_carrier_ok(tp->netdev))
1818 return;
1819
hayeswangd823ab62015-01-12 12:06:23 +08001820 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08001821
hayeswang0c3121f2014-03-07 11:04:36 +08001822 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001823}
1824
hayeswangd823ab62015-01-12 12:06:23 +08001825static int r8152_poll(struct napi_struct *napi, int budget)
1826{
1827 struct r8152 *tp = container_of(napi, struct r8152, napi);
1828 int work_done;
1829
1830 work_done = rx_bottom(tp, budget);
1831 bottom_half(tp);
1832
1833 if (work_done < budget) {
1834 napi_complete(napi);
1835 if (!list_empty(&tp->rx_done))
1836 napi_schedule(napi);
1837 }
1838
1839 return work_done;
1840}
1841
hayeswangebc2ec482013-08-14 20:54:38 +08001842static
1843int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1844{
hayeswanga0fccd42014-11-20 10:29:05 +08001845 int ret;
1846
hayeswangef827a52015-01-09 10:26:36 +08001847 /* The rx would be stopped, so skip submitting */
1848 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
1849 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
1850 return 0;
1851
hayeswangebc2ec482013-08-14 20:54:38 +08001852 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
hayeswang52aec122014-09-02 10:27:52 +08001853 agg->head, agg_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08001854 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08001855
hayeswanga0fccd42014-11-20 10:29:05 +08001856 ret = usb_submit_urb(agg->urb, mem_flags);
1857 if (ret == -ENODEV) {
1858 set_bit(RTL8152_UNPLUG, &tp->flags);
1859 netif_device_detach(tp->netdev);
1860 } else if (ret) {
1861 struct urb *urb = agg->urb;
1862 unsigned long flags;
1863
1864 urb->actual_length = 0;
1865 spin_lock_irqsave(&tp->rx_lock, flags);
1866 list_add_tail(&agg->list, &tp->rx_done);
1867 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001868
1869 netif_err(tp, rx_err, tp->netdev,
1870 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
1871
1872 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08001873 }
1874
1875 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001876}
1877
hayeswang00a5e362014-02-18 21:48:59 +08001878static void rtl_drop_queued_tx(struct r8152 *tp)
1879{
1880 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001881 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08001882 struct sk_buff *skb;
1883
hayeswangd84130a2014-02-18 21:49:02 +08001884 if (skb_queue_empty(tx_queue))
1885 return;
1886
1887 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001888 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001889 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001890 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001891
1892 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001893 dev_kfree_skb(skb);
1894 stats->tx_dropped++;
1895 }
1896}
1897
hayeswangac718b62013-05-02 16:01:25 +00001898static void rtl8152_tx_timeout(struct net_device *netdev)
1899{
1900 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001901 int i;
1902
Hayes Wang4a8deae2014-01-07 11:18:22 +08001903 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001904 for (i = 0; i < RTL8152_MAX_TX; i++)
1905 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001906}
1907
1908static void rtl8152_set_rx_mode(struct net_device *netdev)
1909{
1910 struct r8152 *tp = netdev_priv(netdev);
1911
hayeswang51d979f2015-02-06 11:30:47 +08001912 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00001913 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001914 schedule_delayed_work(&tp->schedule, 0);
1915 }
hayeswangac718b62013-05-02 16:01:25 +00001916}
1917
1918static void _rtl8152_set_rx_mode(struct net_device *netdev)
1919{
1920 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001921 u32 mc_filter[2]; /* Multicast hash filter */
1922 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001923 u32 ocp_data;
1924
hayeswangac718b62013-05-02 16:01:25 +00001925 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1926 netif_stop_queue(netdev);
1927 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1928 ocp_data &= ~RCR_ACPT_ALL;
1929 ocp_data |= RCR_AB | RCR_APM;
1930
1931 if (netdev->flags & IFF_PROMISC) {
1932 /* Unconditionally log net taps. */
1933 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1934 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08001935 mc_filter[1] = 0xffffffff;
1936 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001937 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1938 (netdev->flags & IFF_ALLMULTI)) {
1939 /* Too many to filter perfectly -- accept all multicasts. */
1940 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08001941 mc_filter[1] = 0xffffffff;
1942 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001943 } else {
1944 struct netdev_hw_addr *ha;
1945
hayeswangb209af92014-08-25 15:53:00 +08001946 mc_filter[1] = 0;
1947 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00001948 netdev_for_each_mc_addr(ha, netdev) {
1949 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08001950
hayeswangac718b62013-05-02 16:01:25 +00001951 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1952 ocp_data |= RCR_AM;
1953 }
1954 }
1955
hayeswang31787f52013-07-31 17:21:25 +08001956 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1957 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001958
hayeswang31787f52013-07-31 17:21:25 +08001959 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001960 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1961 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001962}
1963
hayeswanga5e31252015-01-06 17:41:58 +08001964static netdev_features_t
1965rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
1966 netdev_features_t features)
1967{
1968 u32 mss = skb_shinfo(skb)->gso_size;
1969 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
1970 int offset = skb_transport_offset(skb);
1971
1972 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
1973 features &= ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
1974 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
1975 features &= ~NETIF_F_GSO_MASK;
1976
1977 return features;
1978}
1979
hayeswangac718b62013-05-02 16:01:25 +00001980static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08001981 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00001982{
1983 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001984
hayeswangac718b62013-05-02 16:01:25 +00001985 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001986
hayeswang61598782013-11-20 17:30:55 +08001987 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001988
hayeswang0c3121f2014-03-07 11:04:36 +08001989 if (!list_empty(&tp->tx_free)) {
1990 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08001991 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08001992 schedule_delayed_work(&tp->schedule, 0);
1993 } else {
1994 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08001995 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08001996 }
hayeswangb209af92014-08-25 15:53:00 +08001997 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08001998 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08001999 }
hayeswangdd1b1192013-11-20 17:30:56 +08002000
hayeswangac718b62013-05-02 16:01:25 +00002001 return NETDEV_TX_OK;
2002}
2003
2004static void r8152b_reset_packet_filter(struct r8152 *tp)
2005{
2006 u32 ocp_data;
2007
2008 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2009 ocp_data &= ~FMC_FCR_MCU_EN;
2010 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2011 ocp_data |= FMC_FCR_MCU_EN;
2012 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2013}
2014
2015static void rtl8152_nic_reset(struct r8152 *tp)
2016{
2017 int i;
2018
2019 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2020
2021 for (i = 0; i < 1000; i++) {
2022 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2023 break;
hayeswangb209af92014-08-25 15:53:00 +08002024 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002025 }
2026}
2027
hayeswangdd1b1192013-11-20 17:30:56 +08002028static void set_tx_qlen(struct r8152 *tp)
2029{
2030 struct net_device *netdev = tp->netdev;
2031
hayeswang52aec122014-09-02 10:27:52 +08002032 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
2033 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002034}
2035
hayeswangac718b62013-05-02 16:01:25 +00002036static inline u8 rtl8152_get_speed(struct r8152 *tp)
2037{
2038 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2039}
2040
hayeswang507605a2014-01-02 11:22:43 +08002041static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002042{
hayeswangebc2ec482013-08-14 20:54:38 +08002043 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002044 u8 speed;
2045
2046 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002047 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002048 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002049 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002050 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2051 } else {
2052 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002053 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002054 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2055 }
hayeswang507605a2014-01-02 11:22:43 +08002056}
2057
hayeswang00a5e362014-02-18 21:48:59 +08002058static void rxdy_gated_en(struct r8152 *tp, bool enable)
2059{
2060 u32 ocp_data;
2061
2062 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2063 if (enable)
2064 ocp_data |= RXDY_GATED_EN;
2065 else
2066 ocp_data &= ~RXDY_GATED_EN;
2067 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2068}
2069
hayeswang445f7f42014-09-23 16:31:47 +08002070static int rtl_start_rx(struct r8152 *tp)
2071{
2072 int i, ret = 0;
2073
hayeswangd823ab62015-01-12 12:06:23 +08002074 napi_disable(&tp->napi);
hayeswang445f7f42014-09-23 16:31:47 +08002075 INIT_LIST_HEAD(&tp->rx_done);
2076 for (i = 0; i < RTL8152_MAX_RX; i++) {
2077 INIT_LIST_HEAD(&tp->rx_info[i].list);
2078 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2079 if (ret)
2080 break;
2081 }
hayeswangd823ab62015-01-12 12:06:23 +08002082 napi_enable(&tp->napi);
hayeswang445f7f42014-09-23 16:31:47 +08002083
hayeswang7bcf4f62014-11-20 10:29:06 +08002084 if (ret && ++i < RTL8152_MAX_RX) {
2085 struct list_head rx_queue;
2086 unsigned long flags;
2087
2088 INIT_LIST_HEAD(&rx_queue);
2089
2090 do {
2091 struct rx_agg *agg = &tp->rx_info[i++];
2092 struct urb *urb = agg->urb;
2093
2094 urb->actual_length = 0;
2095 list_add_tail(&agg->list, &rx_queue);
2096 } while (i < RTL8152_MAX_RX);
2097
2098 spin_lock_irqsave(&tp->rx_lock, flags);
2099 list_splice_tail(&rx_queue, &tp->rx_done);
2100 spin_unlock_irqrestore(&tp->rx_lock, flags);
2101 }
2102
hayeswang445f7f42014-09-23 16:31:47 +08002103 return ret;
2104}
2105
2106static int rtl_stop_rx(struct r8152 *tp)
2107{
2108 int i;
2109
2110 for (i = 0; i < RTL8152_MAX_RX; i++)
2111 usb_kill_urb(tp->rx_info[i].urb);
2112
hayeswangd823ab62015-01-12 12:06:23 +08002113 while (!skb_queue_empty(&tp->rx_queue))
2114 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2115
hayeswang445f7f42014-09-23 16:31:47 +08002116 return 0;
2117}
2118
hayeswang507605a2014-01-02 11:22:43 +08002119static int rtl_enable(struct r8152 *tp)
2120{
2121 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002122
2123 r8152b_reset_packet_filter(tp);
2124
2125 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2126 ocp_data |= CR_RE | CR_TE;
2127 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2128
hayeswang00a5e362014-02-18 21:48:59 +08002129 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002130
hayeswangaa2e0922015-01-09 10:26:35 +08002131 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002132}
2133
hayeswang507605a2014-01-02 11:22:43 +08002134static int rtl8152_enable(struct r8152 *tp)
2135{
hayeswang68714382014-04-11 17:54:31 +08002136 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2137 return -ENODEV;
2138
hayeswang507605a2014-01-02 11:22:43 +08002139 set_tx_qlen(tp);
2140 rtl_set_eee_plus(tp);
2141
2142 return rtl_enable(tp);
2143}
2144
hayeswang43779f82014-01-02 11:25:10 +08002145static void r8153_set_rx_agg(struct r8152 *tp)
2146{
2147 u8 speed;
2148
2149 speed = rtl8152_get_speed(tp);
2150 if (speed & _1000bps) {
2151 if (tp->udev->speed == USB_SPEED_SUPER) {
2152 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
2153 RX_THR_SUPPER);
2154 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2155 EARLY_AGG_SUPPER);
2156 } else {
2157 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
2158 RX_THR_HIGH);
2159 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2160 EARLY_AGG_HIGH);
2161 }
2162 } else {
2163 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
2164 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2165 EARLY_AGG_SLOW);
2166 }
2167}
2168
2169static int rtl8153_enable(struct r8152 *tp)
2170{
hayeswang68714382014-04-11 17:54:31 +08002171 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2172 return -ENODEV;
2173
hayeswang43779f82014-01-02 11:25:10 +08002174 set_tx_qlen(tp);
2175 rtl_set_eee_plus(tp);
2176 r8153_set_rx_agg(tp);
2177
2178 return rtl_enable(tp);
2179}
2180
hayeswangd70b1132014-09-19 15:17:18 +08002181static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002182{
hayeswangebc2ec482013-08-14 20:54:38 +08002183 u32 ocp_data;
2184 int i;
hayeswangac718b62013-05-02 16:01:25 +00002185
hayeswang68714382014-04-11 17:54:31 +08002186 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2187 rtl_drop_queued_tx(tp);
2188 return;
2189 }
2190
hayeswangac718b62013-05-02 16:01:25 +00002191 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2192 ocp_data &= ~RCR_ACPT_ALL;
2193 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2194
hayeswang00a5e362014-02-18 21:48:59 +08002195 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002196
2197 for (i = 0; i < RTL8152_MAX_TX; i++)
2198 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002199
hayeswang00a5e362014-02-18 21:48:59 +08002200 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002201
2202 for (i = 0; i < 1000; i++) {
2203 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2204 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2205 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002206 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002207 }
2208
2209 for (i = 0; i < 1000; i++) {
2210 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2211 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002212 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002213 }
2214
hayeswang445f7f42014-09-23 16:31:47 +08002215 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002216
2217 rtl8152_nic_reset(tp);
2218}
2219
hayeswang00a5e362014-02-18 21:48:59 +08002220static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2221{
2222 u32 ocp_data;
2223
2224 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2225 if (enable)
2226 ocp_data |= POWER_CUT;
2227 else
2228 ocp_data &= ~POWER_CUT;
2229 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2230
2231 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2232 ocp_data &= ~RESUME_INDICATE;
2233 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002234}
2235
hayeswangc5554292014-09-12 10:43:11 +08002236static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2237{
2238 u32 ocp_data;
2239
2240 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2241 if (enable)
2242 ocp_data |= CPCR_RX_VLAN;
2243 else
2244 ocp_data &= ~CPCR_RX_VLAN;
2245 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2246}
2247
2248static int rtl8152_set_features(struct net_device *dev,
2249 netdev_features_t features)
2250{
2251 netdev_features_t changed = features ^ dev->features;
2252 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002253 int ret;
2254
2255 ret = usb_autopm_get_interface(tp->intf);
2256 if (ret < 0)
2257 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002258
hayeswangb5403272014-10-09 18:00:26 +08002259 mutex_lock(&tp->control);
2260
hayeswangc5554292014-09-12 10:43:11 +08002261 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2262 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2263 rtl_rx_vlan_en(tp, true);
2264 else
2265 rtl_rx_vlan_en(tp, false);
2266 }
2267
hayeswangb5403272014-10-09 18:00:26 +08002268 mutex_unlock(&tp->control);
2269
hayeswang405f8a02014-10-09 18:00:24 +08002270 usb_autopm_put_interface(tp->intf);
2271
2272out:
2273 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002274}
2275
hayeswang21ff2e82014-02-18 21:49:06 +08002276#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2277
2278static u32 __rtl_get_wol(struct r8152 *tp)
2279{
2280 u32 ocp_data;
2281 u32 wolopts = 0;
2282
2283 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2284 if (!(ocp_data & LAN_WAKE_EN))
2285 return 0;
2286
2287 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2288 if (ocp_data & LINK_ON_WAKE_EN)
2289 wolopts |= WAKE_PHY;
2290
2291 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2292 if (ocp_data & UWF_EN)
2293 wolopts |= WAKE_UCAST;
2294 if (ocp_data & BWF_EN)
2295 wolopts |= WAKE_BCAST;
2296 if (ocp_data & MWF_EN)
2297 wolopts |= WAKE_MCAST;
2298
2299 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2300 if (ocp_data & MAGIC_EN)
2301 wolopts |= WAKE_MAGIC;
2302
2303 return wolopts;
2304}
2305
2306static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2307{
2308 u32 ocp_data;
2309
2310 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2311
2312 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2313 ocp_data &= ~LINK_ON_WAKE_EN;
2314 if (wolopts & WAKE_PHY)
2315 ocp_data |= LINK_ON_WAKE_EN;
2316 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2317
2318 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2319 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
2320 if (wolopts & WAKE_UCAST)
2321 ocp_data |= UWF_EN;
2322 if (wolopts & WAKE_BCAST)
2323 ocp_data |= BWF_EN;
2324 if (wolopts & WAKE_MCAST)
2325 ocp_data |= MWF_EN;
2326 if (wolopts & WAKE_ANY)
2327 ocp_data |= LAN_WAKE_EN;
2328 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2329
2330 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2331
2332 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2333 ocp_data &= ~MAGIC_EN;
2334 if (wolopts & WAKE_MAGIC)
2335 ocp_data |= MAGIC_EN;
2336 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2337
2338 if (wolopts & WAKE_ANY)
2339 device_set_wakeup_enable(&tp->udev->dev, true);
2340 else
2341 device_set_wakeup_enable(&tp->udev->dev, false);
2342}
2343
hayeswang9a4be1b2014-02-18 21:49:07 +08002344static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2345{
2346 if (enable) {
2347 u32 ocp_data;
2348
2349 __rtl_set_wol(tp, WAKE_ANY);
2350
2351 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2352
2353 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2354 ocp_data |= LINK_OFF_WAKE_EN;
2355 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2356
2357 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2358 } else {
2359 __rtl_set_wol(tp, tp->saved_wolopts);
2360 }
2361}
2362
hayeswangaa66a5f2014-02-18 21:49:04 +08002363static void rtl_phy_reset(struct r8152 *tp)
2364{
2365 u16 data;
2366 int i;
2367
2368 clear_bit(PHY_RESET, &tp->flags);
2369
2370 data = r8152_mdio_read(tp, MII_BMCR);
2371
2372 /* don't reset again before the previous one complete */
2373 if (data & BMCR_RESET)
2374 return;
2375
2376 data |= BMCR_RESET;
2377 r8152_mdio_write(tp, MII_BMCR, data);
2378
2379 for (i = 0; i < 50; i++) {
2380 msleep(20);
2381 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2382 break;
2383 }
2384}
2385
hayeswang43499682014-02-18 21:48:58 +08002386static void r8153_teredo_off(struct r8152 *tp)
2387{
2388 u32 ocp_data;
2389
2390 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2391 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
2392 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2393
2394 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2395 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2396 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2397}
2398
2399static void r8152b_disable_aldps(struct r8152 *tp)
2400{
2401 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
2402 msleep(20);
2403}
2404
2405static inline void r8152b_enable_aldps(struct r8152 *tp)
2406{
2407 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2408 LINKENA | DIS_SDSAVE);
2409}
2410
hayeswangd70b1132014-09-19 15:17:18 +08002411static void rtl8152_disable(struct r8152 *tp)
2412{
2413 r8152b_disable_aldps(tp);
2414 rtl_disable(tp);
2415 r8152b_enable_aldps(tp);
2416}
2417
hayeswang43499682014-02-18 21:48:58 +08002418static void r8152b_hw_phy_cfg(struct r8152 *tp)
2419{
hayeswangf0cbe0a2014-02-18 21:49:03 +08002420 u16 data;
2421
2422 data = r8152_mdio_read(tp, MII_BMCR);
2423 if (data & BMCR_PDOWN) {
2424 data &= ~BMCR_PDOWN;
2425 r8152_mdio_write(tp, MII_BMCR, data);
2426 }
2427
hayeswangaa66a5f2014-02-18 21:49:04 +08002428 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002429}
2430
hayeswangac718b62013-05-02 16:01:25 +00002431static void r8152b_exit_oob(struct r8152 *tp)
2432{
hayeswangdb8515e2014-03-06 15:07:16 +08002433 u32 ocp_data;
2434 int i;
hayeswangac718b62013-05-02 16:01:25 +00002435
2436 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2437 ocp_data &= ~RCR_ACPT_ALL;
2438 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2439
hayeswang00a5e362014-02-18 21:48:59 +08002440 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002441 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002442 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002443
2444 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2445 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2446
2447 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2448 ocp_data &= ~NOW_IS_OOB;
2449 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2450
2451 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2452 ocp_data &= ~MCU_BORW_EN;
2453 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2454
2455 for (i = 0; i < 1000; i++) {
2456 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2457 if (ocp_data & LINK_LIST_READY)
2458 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002459 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002460 }
2461
2462 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2463 ocp_data |= RE_INIT_LL;
2464 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2465
2466 for (i = 0; i < 1000; i++) {
2467 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2468 if (ocp_data & LINK_LIST_READY)
2469 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002470 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002471 }
2472
2473 rtl8152_nic_reset(tp);
2474
2475 /* rx share fifo credit full threshold */
2476 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2477
hayeswanga3cc4652014-07-24 16:37:43 +08002478 if (tp->udev->speed == USB_SPEED_FULL ||
2479 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00002480 /* rx share fifo credit near full threshold */
2481 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2482 RXFIFO_THR2_FULL);
2483 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2484 RXFIFO_THR3_FULL);
2485 } else {
2486 /* rx share fifo credit near full threshold */
2487 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2488 RXFIFO_THR2_HIGH);
2489 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2490 RXFIFO_THR3_HIGH);
2491 }
2492
2493 /* TX share fifo free credit full threshold */
2494 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2495
2496 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002497 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002498 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2499 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2500
hayeswangc5554292014-09-12 10:43:11 +08002501 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00002502
2503 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2504
2505 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2506 ocp_data |= TCR0_AUTO_FIFO;
2507 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2508}
2509
2510static void r8152b_enter_oob(struct r8152 *tp)
2511{
hayeswang45f4a192014-01-06 17:08:41 +08002512 u32 ocp_data;
2513 int i;
hayeswangac718b62013-05-02 16:01:25 +00002514
2515 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2516 ocp_data &= ~NOW_IS_OOB;
2517 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2518
2519 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2520 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2521 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2522
hayeswangd70b1132014-09-19 15:17:18 +08002523 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002524
2525 for (i = 0; i < 1000; i++) {
2526 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2527 if (ocp_data & LINK_LIST_READY)
2528 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002529 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002530 }
2531
2532 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2533 ocp_data |= RE_INIT_LL;
2534 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2535
2536 for (i = 0; i < 1000; i++) {
2537 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2538 if (ocp_data & LINK_LIST_READY)
2539 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002540 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002541 }
2542
2543 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2544
hayeswangc5554292014-09-12 10:43:11 +08002545 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002546
2547 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2548 ocp_data |= ALDPS_PROXY_MODE;
2549 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2550
2551 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2552 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2553 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2554
hayeswang00a5e362014-02-18 21:48:59 +08002555 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002556
2557 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2558 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2559 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2560}
2561
hayeswang43779f82014-01-02 11:25:10 +08002562static void r8153_hw_phy_cfg(struct r8152 *tp)
2563{
2564 u32 ocp_data;
2565 u16 data;
2566
2567 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002568 data = r8152_mdio_read(tp, MII_BMCR);
2569 if (data & BMCR_PDOWN) {
2570 data &= ~BMCR_PDOWN;
2571 r8152_mdio_write(tp, MII_BMCR, data);
2572 }
hayeswang43779f82014-01-02 11:25:10 +08002573
2574 if (tp->version == RTL_VER_03) {
2575 data = ocp_reg_read(tp, OCP_EEE_CFG);
2576 data &= ~CTAP_SHORT_EN;
2577 ocp_reg_write(tp, OCP_EEE_CFG, data);
2578 }
2579
2580 data = ocp_reg_read(tp, OCP_POWER_CFG);
2581 data |= EEE_CLKDIV_EN;
2582 ocp_reg_write(tp, OCP_POWER_CFG, data);
2583
2584 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2585 data |= EN_10M_BGOFF;
2586 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2587 data = ocp_reg_read(tp, OCP_POWER_CFG);
2588 data |= EN_10M_PLLOFF;
2589 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08002590 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08002591
2592 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2593 ocp_data |= PFM_PWM_SWITCH;
2594 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2595
hayeswangb4d99de2015-01-19 17:02:46 +08002596 /* Enable LPF corner auto tune */
2597 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08002598
hayeswangb4d99de2015-01-19 17:02:46 +08002599 /* Adjust 10M Amplitude */
2600 sram_write(tp, SRAM_10M_AMP1, 0x00af);
2601 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08002602
2603 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002604}
2605
hayeswangb9702722014-02-18 21:49:00 +08002606static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002607{
2608 u8 u1u2[8];
2609
2610 if (enable)
2611 memset(u1u2, 0xff, sizeof(u1u2));
2612 else
2613 memset(u1u2, 0x00, sizeof(u1u2));
2614
2615 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2616}
2617
hayeswangb9702722014-02-18 21:49:00 +08002618static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002619{
2620 u32 ocp_data;
2621
2622 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2623 if (enable)
2624 ocp_data |= U2P3_ENABLE;
2625 else
2626 ocp_data &= ~U2P3_ENABLE;
2627 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2628}
2629
hayeswangb9702722014-02-18 21:49:00 +08002630static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002631{
2632 u32 ocp_data;
2633
2634 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2635 if (enable)
2636 ocp_data |= PWR_EN | PHASE2_EN;
2637 else
2638 ocp_data &= ~(PWR_EN | PHASE2_EN);
2639 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2640
2641 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2642 ocp_data &= ~PCUT_STATUS;
2643 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2644}
2645
hayeswang43779f82014-01-02 11:25:10 +08002646static void r8153_first_init(struct r8152 *tp)
2647{
2648 u32 ocp_data;
2649 int i;
2650
hayeswang00a5e362014-02-18 21:48:59 +08002651 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002652 r8153_teredo_off(tp);
2653
2654 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2655 ocp_data &= ~RCR_ACPT_ALL;
2656 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2657
2658 r8153_hw_phy_cfg(tp);
2659
2660 rtl8152_nic_reset(tp);
2661
2662 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2663 ocp_data &= ~NOW_IS_OOB;
2664 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2665
2666 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2667 ocp_data &= ~MCU_BORW_EN;
2668 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2669
2670 for (i = 0; i < 1000; i++) {
2671 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2672 if (ocp_data & LINK_LIST_READY)
2673 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002674 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002675 }
2676
2677 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2678 ocp_data |= RE_INIT_LL;
2679 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2680
2681 for (i = 0; i < 1000; i++) {
2682 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2683 if (ocp_data & LINK_LIST_READY)
2684 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002685 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002686 }
2687
hayeswangc5554292014-09-12 10:43:11 +08002688 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08002689
hayeswang69b4b7a2014-07-10 10:58:54 +08002690 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
2691 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08002692
2693 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2694 ocp_data |= TCR0_AUTO_FIFO;
2695 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2696
2697 rtl8152_nic_reset(tp);
2698
2699 /* rx share fifo credit full threshold */
2700 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2701 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2702 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2703 /* TX share fifo free credit full threshold */
2704 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2705
hayeswang9629e3c2014-01-15 10:42:15 +08002706 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002707 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2708 ocp_data &= ~RX_AGG_DISABLE;
2709 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2710}
2711
2712static void r8153_enter_oob(struct r8152 *tp)
2713{
2714 u32 ocp_data;
2715 int i;
2716
2717 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2718 ocp_data &= ~NOW_IS_OOB;
2719 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2720
hayeswangd70b1132014-09-19 15:17:18 +08002721 rtl_disable(tp);
hayeswang43779f82014-01-02 11:25:10 +08002722
2723 for (i = 0; i < 1000; i++) {
2724 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2725 if (ocp_data & LINK_LIST_READY)
2726 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002727 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002728 }
2729
2730 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2731 ocp_data |= RE_INIT_LL;
2732 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2733
2734 for (i = 0; i < 1000; i++) {
2735 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2736 if (ocp_data & LINK_LIST_READY)
2737 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002738 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002739 }
2740
hayeswang69b4b7a2014-07-10 10:58:54 +08002741 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
hayeswang43779f82014-01-02 11:25:10 +08002742
hayeswang43779f82014-01-02 11:25:10 +08002743 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2744 ocp_data &= ~TEREDO_WAKE_MASK;
2745 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2746
hayeswangc5554292014-09-12 10:43:11 +08002747 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002748
2749 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2750 ocp_data |= ALDPS_PROXY_MODE;
2751 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2752
2753 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2754 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2755 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2756
hayeswang00a5e362014-02-18 21:48:59 +08002757 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002758
2759 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2760 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2761 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2762}
2763
2764static void r8153_disable_aldps(struct r8152 *tp)
2765{
2766 u16 data;
2767
2768 data = ocp_reg_read(tp, OCP_POWER_CFG);
2769 data &= ~EN_ALDPS;
2770 ocp_reg_write(tp, OCP_POWER_CFG, data);
2771 msleep(20);
2772}
2773
2774static void r8153_enable_aldps(struct r8152 *tp)
2775{
2776 u16 data;
2777
2778 data = ocp_reg_read(tp, OCP_POWER_CFG);
2779 data |= EN_ALDPS;
2780 ocp_reg_write(tp, OCP_POWER_CFG, data);
2781}
2782
hayeswangd70b1132014-09-19 15:17:18 +08002783static void rtl8153_disable(struct r8152 *tp)
2784{
2785 r8153_disable_aldps(tp);
2786 rtl_disable(tp);
2787 r8153_enable_aldps(tp);
2788}
2789
hayeswangac718b62013-05-02 16:01:25 +00002790static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2791{
hayeswang43779f82014-01-02 11:25:10 +08002792 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002793 int ret = 0;
2794
2795 cancel_delayed_work_sync(&tp->schedule);
2796 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2797 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2798 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002799 if (tp->mii.supports_gmii) {
2800 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2801 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2802 } else {
2803 gbcr = 0;
2804 }
hayeswangac718b62013-05-02 16:01:25 +00002805
2806 if (autoneg == AUTONEG_DISABLE) {
2807 if (speed == SPEED_10) {
2808 bmcr = 0;
2809 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2810 } else if (speed == SPEED_100) {
2811 bmcr = BMCR_SPEED100;
2812 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002813 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2814 bmcr = BMCR_SPEED1000;
2815 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002816 } else {
2817 ret = -EINVAL;
2818 goto out;
2819 }
2820
2821 if (duplex == DUPLEX_FULL)
2822 bmcr |= BMCR_FULLDPLX;
2823 } else {
2824 if (speed == SPEED_10) {
2825 if (duplex == DUPLEX_FULL)
2826 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2827 else
2828 anar |= ADVERTISE_10HALF;
2829 } else if (speed == SPEED_100) {
2830 if (duplex == DUPLEX_FULL) {
2831 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2832 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2833 } else {
2834 anar |= ADVERTISE_10HALF;
2835 anar |= ADVERTISE_100HALF;
2836 }
hayeswang43779f82014-01-02 11:25:10 +08002837 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2838 if (duplex == DUPLEX_FULL) {
2839 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2840 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2841 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2842 } else {
2843 anar |= ADVERTISE_10HALF;
2844 anar |= ADVERTISE_100HALF;
2845 gbcr |= ADVERTISE_1000HALF;
2846 }
hayeswangac718b62013-05-02 16:01:25 +00002847 } else {
2848 ret = -EINVAL;
2849 goto out;
2850 }
2851
2852 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2853 }
2854
hayeswangaa66a5f2014-02-18 21:49:04 +08002855 if (test_bit(PHY_RESET, &tp->flags))
2856 bmcr |= BMCR_RESET;
2857
hayeswang43779f82014-01-02 11:25:10 +08002858 if (tp->mii.supports_gmii)
2859 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2860
hayeswangac718b62013-05-02 16:01:25 +00002861 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2862 r8152_mdio_write(tp, MII_BMCR, bmcr);
2863
hayeswangaa66a5f2014-02-18 21:49:04 +08002864 if (test_bit(PHY_RESET, &tp->flags)) {
2865 int i;
2866
2867 clear_bit(PHY_RESET, &tp->flags);
2868 for (i = 0; i < 50; i++) {
2869 msleep(20);
2870 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2871 break;
2872 }
2873 }
2874
hayeswangac718b62013-05-02 16:01:25 +00002875out:
hayeswangac718b62013-05-02 16:01:25 +00002876
2877 return ret;
2878}
2879
hayeswangd70b1132014-09-19 15:17:18 +08002880static void rtl8152_up(struct r8152 *tp)
2881{
2882 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2883 return;
2884
2885 r8152b_disable_aldps(tp);
2886 r8152b_exit_oob(tp);
2887 r8152b_enable_aldps(tp);
2888}
2889
hayeswangac718b62013-05-02 16:01:25 +00002890static void rtl8152_down(struct r8152 *tp)
2891{
hayeswang68714382014-04-11 17:54:31 +08002892 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2893 rtl_drop_queued_tx(tp);
2894 return;
2895 }
2896
hayeswang00a5e362014-02-18 21:48:59 +08002897 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002898 r8152b_disable_aldps(tp);
2899 r8152b_enter_oob(tp);
2900 r8152b_enable_aldps(tp);
2901}
2902
hayeswangd70b1132014-09-19 15:17:18 +08002903static void rtl8153_up(struct r8152 *tp)
2904{
2905 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2906 return;
2907
2908 r8153_disable_aldps(tp);
2909 r8153_first_init(tp);
2910 r8153_enable_aldps(tp);
2911}
2912
hayeswang43779f82014-01-02 11:25:10 +08002913static void rtl8153_down(struct r8152 *tp)
2914{
hayeswang68714382014-04-11 17:54:31 +08002915 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2916 rtl_drop_queued_tx(tp);
2917 return;
2918 }
2919
hayeswangb9702722014-02-18 21:49:00 +08002920 r8153_u1u2en(tp, false);
2921 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002922 r8153_disable_aldps(tp);
2923 r8153_enter_oob(tp);
2924 r8153_enable_aldps(tp);
2925}
2926
hayeswangac718b62013-05-02 16:01:25 +00002927static void set_carrier(struct r8152 *tp)
2928{
2929 struct net_device *netdev = tp->netdev;
2930 u8 speed;
2931
hayeswang40a82912013-08-14 20:54:40 +08002932 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002933 speed = rtl8152_get_speed(tp);
2934
2935 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08002936 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08002937 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002938 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2939 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08002940 rtl_start_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002941 }
2942 } else {
hayeswang51d979f2015-02-06 11:30:47 +08002943 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002944 netif_carrier_off(netdev);
hayeswangd823ab62015-01-12 12:06:23 +08002945 napi_disable(&tp->napi);
hayeswangc81229c2014-01-02 11:22:42 +08002946 tp->rtl_ops.disable(tp);
hayeswangd823ab62015-01-12 12:06:23 +08002947 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00002948 }
2949 }
hayeswangac718b62013-05-02 16:01:25 +00002950}
2951
2952static void rtl_work_func_t(struct work_struct *work)
2953{
2954 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2955
hayeswanga1f83fe2014-11-12 10:05:05 +08002956 /* If the device is unplugged or !netif_running(), the workqueue
2957 * doesn't need to wake the device, and could return directly.
2958 */
2959 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
2960 return;
2961
hayeswang9a4be1b2014-02-18 21:49:07 +08002962 if (usb_autopm_get_interface(tp->intf) < 0)
2963 return;
2964
hayeswangac718b62013-05-02 16:01:25 +00002965 if (!test_bit(WORK_ENABLE, &tp->flags))
2966 goto out1;
2967
hayeswangb5403272014-10-09 18:00:26 +08002968 if (!mutex_trylock(&tp->control)) {
2969 schedule_delayed_work(&tp->schedule, 0);
2970 goto out1;
2971 }
2972
hayeswang40a82912013-08-14 20:54:40 +08002973 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2974 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002975
2976 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2977 _rtl8152_set_rx_mode(tp->netdev);
2978
hayeswangd823ab62015-01-12 12:06:23 +08002979 /* don't schedule napi before linking */
2980 if (test_bit(SCHEDULE_NAPI, &tp->flags) &&
hayeswang51d979f2015-02-06 11:30:47 +08002981 netif_carrier_ok(tp->netdev)) {
hayeswangd823ab62015-01-12 12:06:23 +08002982 clear_bit(SCHEDULE_NAPI, &tp->flags);
2983 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002984 }
hayeswangaa66a5f2014-02-18 21:49:04 +08002985
2986 if (test_bit(PHY_RESET, &tp->flags))
2987 rtl_phy_reset(tp);
2988
hayeswangb5403272014-10-09 18:00:26 +08002989 mutex_unlock(&tp->control);
2990
hayeswangac718b62013-05-02 16:01:25 +00002991out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08002992 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002993}
2994
2995static int rtl8152_open(struct net_device *netdev)
2996{
2997 struct r8152 *tp = netdev_priv(netdev);
2998 int res = 0;
2999
hayeswang7e9da482014-02-18 21:49:05 +08003000 res = alloc_all_mem(tp);
3001 if (res)
3002 goto out;
3003
hayeswang51d979f2015-02-06 11:30:47 +08003004 netif_carrier_off(netdev);
hayeswangf4c74762014-10-29 11:12:16 +08003005
hayeswang9a4be1b2014-02-18 21:49:07 +08003006 res = usb_autopm_get_interface(tp->intf);
3007 if (res < 0) {
3008 free_all_mem(tp);
3009 goto out;
3010 }
3011
hayeswangb5403272014-10-09 18:00:26 +08003012 mutex_lock(&tp->control);
3013
hayeswang9a4be1b2014-02-18 21:49:07 +08003014 /* The WORK_ENABLE may be set when autoresume occurs */
3015 if (test_bit(WORK_ENABLE, &tp->flags)) {
3016 clear_bit(WORK_ENABLE, &tp->flags);
3017 usb_kill_urb(tp->intr_urb);
3018 cancel_delayed_work_sync(&tp->schedule);
hayeswangf4c74762014-10-29 11:12:16 +08003019
3020 /* disable the tx/rx, if the workqueue has enabled them. */
hayeswang51d979f2015-02-06 11:30:47 +08003021 if (netif_carrier_ok(netdev))
hayeswang9a4be1b2014-02-18 21:49:07 +08003022 tp->rtl_ops.disable(tp);
3023 }
3024
hayeswang7e9da482014-02-18 21:49:05 +08003025 tp->rtl_ops.up(tp);
3026
hayeswang43779f82014-01-02 11:25:10 +08003027 rtl8152_set_speed(tp, AUTONEG_ENABLE,
3028 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
3029 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08003030 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003031 netif_start_queue(netdev);
3032 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08003033
hayeswang3d55f442014-02-06 11:55:48 +08003034 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3035 if (res) {
3036 if (res == -ENODEV)
3037 netif_device_detach(tp->netdev);
3038 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3039 res);
hayeswang7e9da482014-02-18 21:49:05 +08003040 free_all_mem(tp);
hayeswang93ffbea2014-11-05 10:17:02 +08003041 } else {
hayeswangd823ab62015-01-12 12:06:23 +08003042 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08003043 }
3044
hayeswangb5403272014-10-09 18:00:26 +08003045 mutex_unlock(&tp->control);
3046
hayeswang9a4be1b2014-02-18 21:49:07 +08003047 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003048
hayeswang7e9da482014-02-18 21:49:05 +08003049out:
hayeswangac718b62013-05-02 16:01:25 +00003050 return res;
3051}
3052
3053static int rtl8152_close(struct net_device *netdev)
3054{
3055 struct r8152 *tp = netdev_priv(netdev);
3056 int res = 0;
3057
hayeswangd823ab62015-01-12 12:06:23 +08003058 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003059 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08003060 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00003061 cancel_delayed_work_sync(&tp->schedule);
3062 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003063
3064 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08003065 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003066 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08003067 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003068 } else {
hayeswangb5403272014-10-09 18:00:26 +08003069 mutex_lock(&tp->control);
3070
hayeswangb209af92014-08-25 15:53:00 +08003071 /* The autosuspend may have been enabled and wouldn't
hayeswang9a4be1b2014-02-18 21:49:07 +08003072 * be disable when autoresume occurs, because the
3073 * netif_running() would be false.
3074 */
hayeswang923e1ee2014-10-29 11:12:15 +08003075 rtl_runtime_suspend_enable(tp, false);
hayeswang9a4be1b2014-02-18 21:49:07 +08003076
hayeswang9a4be1b2014-02-18 21:49:07 +08003077 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08003078
3079 mutex_unlock(&tp->control);
3080
hayeswang9a4be1b2014-02-18 21:49:07 +08003081 usb_autopm_put_interface(tp->intf);
3082 }
hayeswangac718b62013-05-02 16:01:25 +00003083
hayeswang7e9da482014-02-18 21:49:05 +08003084 free_all_mem(tp);
3085
hayeswangac718b62013-05-02 16:01:25 +00003086 return res;
3087}
3088
hayeswangd24f6132014-09-25 20:54:01 +08003089static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
hayeswangac718b62013-05-02 16:01:25 +00003090{
hayeswangd24f6132014-09-25 20:54:01 +08003091 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3092 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3093 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3094}
3095
3096static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3097{
3098 u16 data;
3099
3100 r8152_mmd_indirect(tp, dev, reg);
3101 data = ocp_reg_read(tp, OCP_EEE_DATA);
3102 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3103
3104 return data;
3105}
3106
3107static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3108{
3109 r8152_mmd_indirect(tp, dev, reg);
3110 ocp_reg_write(tp, OCP_EEE_DATA, data);
3111 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3112}
3113
3114static void r8152_eee_en(struct r8152 *tp, bool enable)
3115{
3116 u16 config1, config2, config3;
hayeswang45f4a192014-01-06 17:08:41 +08003117 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003118
3119 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
hayeswangd24f6132014-09-25 20:54:01 +08003120 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3121 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3122 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3123
3124 if (enable) {
3125 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3126 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3127 config1 |= sd_rise_time(1);
3128 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3129 config3 |= fast_snr(42);
3130 } else {
3131 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3132 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3133 RX_QUIET_EN);
3134 config1 |= sd_rise_time(7);
3135 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3136 config3 |= fast_snr(511);
3137 }
3138
hayeswangac718b62013-05-02 16:01:25 +00003139 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
hayeswangd24f6132014-09-25 20:54:01 +08003140 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3141 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3142 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3143}
3144
3145static void r8152b_enable_eee(struct r8152 *tp)
3146{
3147 r8152_eee_en(tp, true);
3148 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3149}
3150
3151static void r8153_eee_en(struct r8152 *tp, bool enable)
3152{
3153 u32 ocp_data;
3154 u16 config;
3155
3156 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3157 config = ocp_reg_read(tp, OCP_EEE_CFG);
3158
3159 if (enable) {
3160 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3161 config |= EEE10_EN;
3162 } else {
3163 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3164 config &= ~EEE10_EN;
3165 }
3166
3167 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3168 ocp_reg_write(tp, OCP_EEE_CFG, config);
hayeswangac718b62013-05-02 16:01:25 +00003169}
3170
hayeswang43779f82014-01-02 11:25:10 +08003171static void r8153_enable_eee(struct r8152 *tp)
3172{
hayeswangd24f6132014-09-25 20:54:01 +08003173 r8153_eee_en(tp, true);
3174 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
hayeswang43779f82014-01-02 11:25:10 +08003175}
3176
hayeswangac718b62013-05-02 16:01:25 +00003177static void r8152b_enable_fc(struct r8152 *tp)
3178{
3179 u16 anar;
3180
3181 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3182 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3183 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3184}
3185
hayeswang4f1d4d52014-03-11 16:24:19 +08003186static void rtl_tally_reset(struct r8152 *tp)
3187{
3188 u32 ocp_data;
3189
3190 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
3191 ocp_data |= TALLY_RESET;
3192 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
3193}
3194
hayeswangac718b62013-05-02 16:01:25 +00003195static void r8152b_init(struct r8152 *tp)
3196{
hayeswangebc2ec482013-08-14 20:54:38 +08003197 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003198
hayeswang68714382014-04-11 17:54:31 +08003199 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3200 return;
3201
hayeswangd70b1132014-09-19 15:17:18 +08003202 r8152b_disable_aldps(tp);
3203
hayeswangac718b62013-05-02 16:01:25 +00003204 if (tp->version == RTL_VER_01) {
3205 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3206 ocp_data &= ~LED_MODE_MASK;
3207 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3208 }
3209
hayeswang00a5e362014-02-18 21:48:59 +08003210 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003211
hayeswangac718b62013-05-02 16:01:25 +00003212 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3213 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
3214 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3215 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
3216 ocp_data &= ~MCU_CLK_RATIO_MASK;
3217 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
3218 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
3219 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
3220 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
3221 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
3222
3223 r8152b_enable_eee(tp);
3224 r8152b_enable_aldps(tp);
3225 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003226 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00003227
hayeswangebc2ec482013-08-14 20:54:38 +08003228 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00003229 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08003230 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00003231 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
3232}
3233
hayeswang43779f82014-01-02 11:25:10 +08003234static void r8153_init(struct r8152 *tp)
3235{
3236 u32 ocp_data;
3237 int i;
3238
hayeswang68714382014-04-11 17:54:31 +08003239 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3240 return;
3241
hayeswangd70b1132014-09-19 15:17:18 +08003242 r8153_disable_aldps(tp);
hayeswangb9702722014-02-18 21:49:00 +08003243 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003244
3245 for (i = 0; i < 500; i++) {
3246 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3247 AUTOLOAD_DONE)
3248 break;
3249 msleep(20);
3250 }
3251
3252 for (i = 0; i < 500; i++) {
3253 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
3254 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
3255 break;
3256 msleep(20);
3257 }
3258
hayeswangb9702722014-02-18 21:49:00 +08003259 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003260
hayeswang65bab842015-02-12 16:20:46 +08003261 if (tp->version == RTL_VER_04) {
3262 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
3263 ocp_data &= ~pwd_dn_scale_mask;
3264 ocp_data |= pwd_dn_scale(96);
3265 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
3266
3267 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
3268 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
3269 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
3270 } else if (tp->version == RTL_VER_05) {
3271 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
3272 ocp_data &= ~ECM_ALDPS;
3273 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
3274
3275 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
3276 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
3277 ocp_data &= ~DYNAMIC_BURST;
3278 else
3279 ocp_data |= DYNAMIC_BURST;
3280 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
3281 }
3282
3283 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
3284 ocp_data |= EP4_FULL_FC;
3285 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
3286
hayeswang43779f82014-01-02 11:25:10 +08003287 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
3288 ocp_data &= ~TIMER11_EN;
3289 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
3290
hayeswang43779f82014-01-02 11:25:10 +08003291 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3292 ocp_data &= ~LED_MODE_MASK;
3293 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3294
hayeswang65bab842015-02-12 16:20:46 +08003295 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
hayeswang34203e22015-02-06 11:30:46 +08003296 if (tp->version == RTL_VER_04 && tp->udev->speed != USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08003297 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08003298 else
3299 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08003300 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
3301
3302 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
3303 ocp_data &= ~SEN_VAL_MASK;
3304 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
3305 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
3306
hayeswang65bab842015-02-12 16:20:46 +08003307 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
3308
hayeswangb9702722014-02-18 21:49:00 +08003309 r8153_power_cut_en(tp, false);
3310 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003311
hayeswang43779f82014-01-02 11:25:10 +08003312 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
3313 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
3314 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3315 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3316 U1U2_SPDWN_EN | L1_SPDWN_EN);
3317 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3318 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3319 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
3320 EEE_SPDWN_EN);
3321
3322 r8153_enable_eee(tp);
3323 r8153_enable_aldps(tp);
3324 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003325 rtl_tally_reset(tp);
hayeswang43779f82014-01-02 11:25:10 +08003326}
3327
hayeswangac718b62013-05-02 16:01:25 +00003328static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
3329{
3330 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang6cc69f22014-10-17 16:55:08 +08003331 struct net_device *netdev = tp->netdev;
3332 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00003333
hayeswangb5403272014-10-09 18:00:26 +08003334 mutex_lock(&tp->control);
3335
hayeswang6cc69f22014-10-17 16:55:08 +08003336 if (PMSG_IS_AUTO(message)) {
3337 if (netif_running(netdev) && work_busy(&tp->schedule.work)) {
3338 ret = -EBUSY;
3339 goto out1;
3340 }
hayeswangac718b62013-05-02 16:01:25 +00003341
hayeswang6cc69f22014-10-17 16:55:08 +08003342 set_bit(SELECTIVE_SUSPEND, &tp->flags);
3343 } else {
3344 netif_device_detach(netdev);
3345 }
3346
hayeswange3bd1a82014-10-29 11:12:17 +08003347 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangac718b62013-05-02 16:01:25 +00003348 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08003349 usb_kill_urb(tp->intr_urb);
hayeswangd823ab62015-01-12 12:06:23 +08003350 napi_disable(&tp->napi);
hayeswang9a4be1b2014-02-18 21:49:07 +08003351 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswang445f7f42014-09-23 16:31:47 +08003352 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003353 rtl_runtime_suspend_enable(tp, true);
3354 } else {
hayeswang6cc69f22014-10-17 16:55:08 +08003355 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08003356 tp->rtl_ops.down(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003357 }
hayeswangd823ab62015-01-12 12:06:23 +08003358 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003359 }
hayeswang6cc69f22014-10-17 16:55:08 +08003360out1:
hayeswangb5403272014-10-09 18:00:26 +08003361 mutex_unlock(&tp->control);
3362
hayeswang6cc69f22014-10-17 16:55:08 +08003363 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003364}
3365
3366static int rtl8152_resume(struct usb_interface *intf)
3367{
3368 struct r8152 *tp = usb_get_intfdata(intf);
3369
hayeswangb5403272014-10-09 18:00:26 +08003370 mutex_lock(&tp->control);
3371
hayeswang9a4be1b2014-02-18 21:49:07 +08003372 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3373 tp->rtl_ops.init(tp);
3374 netif_device_attach(tp->netdev);
3375 }
3376
hayeswangac718b62013-05-02 16:01:25 +00003377 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003378 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3379 rtl_runtime_suspend_enable(tp, false);
3380 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswang445f7f42014-09-23 16:31:47 +08003381 set_bit(WORK_ENABLE, &tp->flags);
hayeswang51d979f2015-02-06 11:30:47 +08003382 if (netif_carrier_ok(tp->netdev))
hayeswang445f7f42014-09-23 16:31:47 +08003383 rtl_start_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003384 } else {
3385 tp->rtl_ops.up(tp);
3386 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswangb209af92014-08-25 15:53:00 +08003387 tp->mii.supports_gmii ?
3388 SPEED_1000 : SPEED_100,
3389 DUPLEX_FULL);
hayeswang445f7f42014-09-23 16:31:47 +08003390 netif_carrier_off(tp->netdev);
3391 set_bit(WORK_ENABLE, &tp->flags);
hayeswang9a4be1b2014-02-18 21:49:07 +08003392 }
hayeswang40a82912013-08-14 20:54:40 +08003393 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswang923e1ee2014-10-29 11:12:15 +08003394 } else if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3395 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00003396 }
3397
hayeswangb5403272014-10-09 18:00:26 +08003398 mutex_unlock(&tp->control);
3399
hayeswangac718b62013-05-02 16:01:25 +00003400 return 0;
3401}
3402
hayeswang21ff2e82014-02-18 21:49:06 +08003403static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3404{
3405 struct r8152 *tp = netdev_priv(dev);
3406
hayeswang9a4be1b2014-02-18 21:49:07 +08003407 if (usb_autopm_get_interface(tp->intf) < 0)
3408 return;
3409
hayeswangb5403272014-10-09 18:00:26 +08003410 mutex_lock(&tp->control);
3411
hayeswang21ff2e82014-02-18 21:49:06 +08003412 wol->supported = WAKE_ANY;
3413 wol->wolopts = __rtl_get_wol(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003414
hayeswangb5403272014-10-09 18:00:26 +08003415 mutex_unlock(&tp->control);
3416
hayeswang9a4be1b2014-02-18 21:49:07 +08003417 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08003418}
3419
3420static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3421{
3422 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003423 int ret;
3424
3425 ret = usb_autopm_get_interface(tp->intf);
3426 if (ret < 0)
3427 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08003428
hayeswangb5403272014-10-09 18:00:26 +08003429 mutex_lock(&tp->control);
3430
hayeswang21ff2e82014-02-18 21:49:06 +08003431 __rtl_set_wol(tp, wol->wolopts);
3432 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
3433
hayeswangb5403272014-10-09 18:00:26 +08003434 mutex_unlock(&tp->control);
3435
hayeswang9a4be1b2014-02-18 21:49:07 +08003436 usb_autopm_put_interface(tp->intf);
3437
3438out_set_wol:
3439 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08003440}
3441
hayeswanga5ec27c2014-02-18 21:49:11 +08003442static u32 rtl8152_get_msglevel(struct net_device *dev)
3443{
3444 struct r8152 *tp = netdev_priv(dev);
3445
3446 return tp->msg_enable;
3447}
3448
3449static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
3450{
3451 struct r8152 *tp = netdev_priv(dev);
3452
3453 tp->msg_enable = value;
3454}
3455
hayeswangac718b62013-05-02 16:01:25 +00003456static void rtl8152_get_drvinfo(struct net_device *netdev,
3457 struct ethtool_drvinfo *info)
3458{
3459 struct r8152 *tp = netdev_priv(netdev);
3460
hayeswangb0b46c72014-08-26 10:08:23 +08003461 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
3462 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00003463 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
3464}
3465
3466static
3467int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
3468{
3469 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08003470 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003471
3472 if (!tp->mii.mdio_read)
3473 return -EOPNOTSUPP;
3474
hayeswang8d4a4d72014-10-09 18:00:25 +08003475 ret = usb_autopm_get_interface(tp->intf);
3476 if (ret < 0)
3477 goto out;
3478
hayeswangb5403272014-10-09 18:00:26 +08003479 mutex_lock(&tp->control);
3480
hayeswang8d4a4d72014-10-09 18:00:25 +08003481 ret = mii_ethtool_gset(&tp->mii, cmd);
3482
hayeswangb5403272014-10-09 18:00:26 +08003483 mutex_unlock(&tp->control);
3484
hayeswang8d4a4d72014-10-09 18:00:25 +08003485 usb_autopm_put_interface(tp->intf);
3486
3487out:
3488 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003489}
3490
3491static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3492{
3493 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003494 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003495
hayeswang9a4be1b2014-02-18 21:49:07 +08003496 ret = usb_autopm_get_interface(tp->intf);
3497 if (ret < 0)
3498 goto out;
3499
hayeswangb5403272014-10-09 18:00:26 +08003500 mutex_lock(&tp->control);
3501
hayeswang9a4be1b2014-02-18 21:49:07 +08003502 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
3503
hayeswangb5403272014-10-09 18:00:26 +08003504 mutex_unlock(&tp->control);
3505
hayeswang9a4be1b2014-02-18 21:49:07 +08003506 usb_autopm_put_interface(tp->intf);
3507
3508out:
3509 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003510}
3511
hayeswang4f1d4d52014-03-11 16:24:19 +08003512static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
3513 "tx_packets",
3514 "rx_packets",
3515 "tx_errors",
3516 "rx_errors",
3517 "rx_missed",
3518 "align_errors",
3519 "tx_single_collisions",
3520 "tx_multi_collisions",
3521 "rx_unicast",
3522 "rx_broadcast",
3523 "rx_multicast",
3524 "tx_aborted",
3525 "tx_underrun",
3526};
3527
3528static int rtl8152_get_sset_count(struct net_device *dev, int sset)
3529{
3530 switch (sset) {
3531 case ETH_SS_STATS:
3532 return ARRAY_SIZE(rtl8152_gstrings);
3533 default:
3534 return -EOPNOTSUPP;
3535 }
3536}
3537
3538static void rtl8152_get_ethtool_stats(struct net_device *dev,
3539 struct ethtool_stats *stats, u64 *data)
3540{
3541 struct r8152 *tp = netdev_priv(dev);
3542 struct tally_counter tally;
3543
hayeswang0b030242014-07-08 14:49:28 +08003544 if (usb_autopm_get_interface(tp->intf) < 0)
3545 return;
3546
hayeswang4f1d4d52014-03-11 16:24:19 +08003547 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
3548
hayeswang0b030242014-07-08 14:49:28 +08003549 usb_autopm_put_interface(tp->intf);
3550
hayeswang4f1d4d52014-03-11 16:24:19 +08003551 data[0] = le64_to_cpu(tally.tx_packets);
3552 data[1] = le64_to_cpu(tally.rx_packets);
3553 data[2] = le64_to_cpu(tally.tx_errors);
3554 data[3] = le32_to_cpu(tally.rx_errors);
3555 data[4] = le16_to_cpu(tally.rx_missed);
3556 data[5] = le16_to_cpu(tally.align_errors);
3557 data[6] = le32_to_cpu(tally.tx_one_collision);
3558 data[7] = le32_to_cpu(tally.tx_multi_collision);
3559 data[8] = le64_to_cpu(tally.rx_unicast);
3560 data[9] = le64_to_cpu(tally.rx_broadcast);
3561 data[10] = le32_to_cpu(tally.rx_multicast);
3562 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08003563 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08003564}
3565
3566static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3567{
3568 switch (stringset) {
3569 case ETH_SS_STATS:
3570 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
3571 break;
3572 }
3573}
3574
hayeswangdf35d282014-09-25 20:54:02 +08003575static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3576{
3577 u32 ocp_data, lp, adv, supported = 0;
3578 u16 val;
3579
3580 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
3581 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3582
3583 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
3584 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3585
3586 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
3587 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3588
3589 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3590 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3591
3592 eee->eee_enabled = !!ocp_data;
3593 eee->eee_active = !!(supported & adv & lp);
3594 eee->supported = supported;
3595 eee->advertised = adv;
3596 eee->lp_advertised = lp;
3597
3598 return 0;
3599}
3600
3601static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3602{
3603 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3604
3605 r8152_eee_en(tp, eee->eee_enabled);
3606
3607 if (!eee->eee_enabled)
3608 val = 0;
3609
3610 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
3611
3612 return 0;
3613}
3614
3615static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3616{
3617 u32 ocp_data, lp, adv, supported = 0;
3618 u16 val;
3619
3620 val = ocp_reg_read(tp, OCP_EEE_ABLE);
3621 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3622
3623 val = ocp_reg_read(tp, OCP_EEE_ADV);
3624 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3625
3626 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
3627 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3628
3629 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3630 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3631
3632 eee->eee_enabled = !!ocp_data;
3633 eee->eee_active = !!(supported & adv & lp);
3634 eee->supported = supported;
3635 eee->advertised = adv;
3636 eee->lp_advertised = lp;
3637
3638 return 0;
3639}
3640
3641static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3642{
3643 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3644
3645 r8153_eee_en(tp, eee->eee_enabled);
3646
3647 if (!eee->eee_enabled)
3648 val = 0;
3649
3650 ocp_reg_write(tp, OCP_EEE_ADV, val);
3651
3652 return 0;
3653}
3654
3655static int
3656rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
3657{
3658 struct r8152 *tp = netdev_priv(net);
3659 int ret;
3660
3661 ret = usb_autopm_get_interface(tp->intf);
3662 if (ret < 0)
3663 goto out;
3664
hayeswangb5403272014-10-09 18:00:26 +08003665 mutex_lock(&tp->control);
3666
hayeswangdf35d282014-09-25 20:54:02 +08003667 ret = tp->rtl_ops.eee_get(tp, edata);
3668
hayeswangb5403272014-10-09 18:00:26 +08003669 mutex_unlock(&tp->control);
3670
hayeswangdf35d282014-09-25 20:54:02 +08003671 usb_autopm_put_interface(tp->intf);
3672
3673out:
3674 return ret;
3675}
3676
3677static int
3678rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
3679{
3680 struct r8152 *tp = netdev_priv(net);
3681 int ret;
3682
3683 ret = usb_autopm_get_interface(tp->intf);
3684 if (ret < 0)
3685 goto out;
3686
hayeswangb5403272014-10-09 18:00:26 +08003687 mutex_lock(&tp->control);
3688
hayeswangdf35d282014-09-25 20:54:02 +08003689 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08003690 if (!ret)
3691 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08003692
hayeswangb5403272014-10-09 18:00:26 +08003693 mutex_unlock(&tp->control);
3694
hayeswangdf35d282014-09-25 20:54:02 +08003695 usb_autopm_put_interface(tp->intf);
3696
3697out:
3698 return ret;
3699}
3700
hayeswang8884f502014-10-28 14:05:52 +08003701static int rtl8152_nway_reset(struct net_device *dev)
3702{
3703 struct r8152 *tp = netdev_priv(dev);
3704 int ret;
3705
3706 ret = usb_autopm_get_interface(tp->intf);
3707 if (ret < 0)
3708 goto out;
3709
3710 mutex_lock(&tp->control);
3711
3712 ret = mii_nway_restart(&tp->mii);
3713
3714 mutex_unlock(&tp->control);
3715
3716 usb_autopm_put_interface(tp->intf);
3717
3718out:
3719 return ret;
3720}
3721
hayeswangac718b62013-05-02 16:01:25 +00003722static struct ethtool_ops ops = {
3723 .get_drvinfo = rtl8152_get_drvinfo,
3724 .get_settings = rtl8152_get_settings,
3725 .set_settings = rtl8152_set_settings,
3726 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08003727 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08003728 .get_msglevel = rtl8152_get_msglevel,
3729 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08003730 .get_wol = rtl8152_get_wol,
3731 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08003732 .get_strings = rtl8152_get_strings,
3733 .get_sset_count = rtl8152_get_sset_count,
3734 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangdf35d282014-09-25 20:54:02 +08003735 .get_eee = rtl_ethtool_get_eee,
3736 .set_eee = rtl_ethtool_set_eee,
hayeswangac718b62013-05-02 16:01:25 +00003737};
3738
3739static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
3740{
3741 struct r8152 *tp = netdev_priv(netdev);
3742 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08003743 int res;
3744
hayeswang68714382014-04-11 17:54:31 +08003745 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3746 return -ENODEV;
3747
hayeswang9a4be1b2014-02-18 21:49:07 +08003748 res = usb_autopm_get_interface(tp->intf);
3749 if (res < 0)
3750 goto out;
hayeswangac718b62013-05-02 16:01:25 +00003751
3752 switch (cmd) {
3753 case SIOCGMIIPHY:
3754 data->phy_id = R8152_PHY_ID; /* Internal PHY */
3755 break;
3756
3757 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08003758 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003759 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08003760 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003761 break;
3762
3763 case SIOCSMIIREG:
3764 if (!capable(CAP_NET_ADMIN)) {
3765 res = -EPERM;
3766 break;
3767 }
hayeswangb5403272014-10-09 18:00:26 +08003768 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003769 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08003770 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003771 break;
3772
3773 default:
3774 res = -EOPNOTSUPP;
3775 }
3776
hayeswang9a4be1b2014-02-18 21:49:07 +08003777 usb_autopm_put_interface(tp->intf);
3778
3779out:
hayeswangac718b62013-05-02 16:01:25 +00003780 return res;
3781}
3782
hayeswang69b4b7a2014-07-10 10:58:54 +08003783static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
3784{
3785 struct r8152 *tp = netdev_priv(dev);
3786
3787 switch (tp->version) {
3788 case RTL_VER_01:
3789 case RTL_VER_02:
3790 return eth_change_mtu(dev, new_mtu);
3791 default:
3792 break;
3793 }
3794
3795 if (new_mtu < 68 || new_mtu > RTL8153_MAX_MTU)
3796 return -EINVAL;
3797
3798 dev->mtu = new_mtu;
3799
3800 return 0;
3801}
3802
hayeswangac718b62013-05-02 16:01:25 +00003803static const struct net_device_ops rtl8152_netdev_ops = {
3804 .ndo_open = rtl8152_open,
3805 .ndo_stop = rtl8152_close,
3806 .ndo_do_ioctl = rtl8152_ioctl,
3807 .ndo_start_xmit = rtl8152_start_xmit,
3808 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08003809 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00003810 .ndo_set_rx_mode = rtl8152_set_rx_mode,
3811 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08003812 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00003813 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08003814 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00003815};
3816
3817static void r8152b_get_version(struct r8152 *tp)
3818{
3819 u32 ocp_data;
3820 u16 version;
3821
3822 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3823 version = (u16)(ocp_data & VERSION_MASK);
3824
3825 switch (version) {
3826 case 0x4c00:
3827 tp->version = RTL_VER_01;
3828 break;
3829 case 0x4c10:
3830 tp->version = RTL_VER_02;
3831 break;
hayeswang43779f82014-01-02 11:25:10 +08003832 case 0x5c00:
3833 tp->version = RTL_VER_03;
3834 tp->mii.supports_gmii = 1;
3835 break;
3836 case 0x5c10:
3837 tp->version = RTL_VER_04;
3838 tp->mii.supports_gmii = 1;
3839 break;
3840 case 0x5c20:
3841 tp->version = RTL_VER_05;
3842 tp->mii.supports_gmii = 1;
3843 break;
hayeswangac718b62013-05-02 16:01:25 +00003844 default:
3845 netif_info(tp, probe, tp->netdev,
3846 "Unknown version 0x%04x\n", version);
3847 break;
3848 }
3849}
3850
hayeswange3fe0b12014-01-02 11:22:39 +08003851static void rtl8152_unload(struct r8152 *tp)
3852{
hayeswang68714382014-04-11 17:54:31 +08003853 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3854 return;
3855
hayeswang00a5e362014-02-18 21:48:59 +08003856 if (tp->version != RTL_VER_01)
3857 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08003858}
3859
hayeswang43779f82014-01-02 11:25:10 +08003860static void rtl8153_unload(struct r8152 *tp)
3861{
hayeswang68714382014-04-11 17:54:31 +08003862 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3863 return;
3864
hayeswang49be1722014-10-01 13:25:11 +08003865 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003866}
3867
hayeswang55b65472014-11-06 12:47:39 +08003868static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08003869{
3870 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08003871 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08003872
hayeswang55b65472014-11-06 12:47:39 +08003873 switch (tp->version) {
3874 case RTL_VER_01:
3875 case RTL_VER_02:
3876 ops->init = r8152b_init;
3877 ops->enable = rtl8152_enable;
3878 ops->disable = rtl8152_disable;
3879 ops->up = rtl8152_up;
3880 ops->down = rtl8152_down;
3881 ops->unload = rtl8152_unload;
3882 ops->eee_get = r8152_get_eee;
3883 ops->eee_set = r8152_set_eee;
hayeswang43779f82014-01-02 11:25:10 +08003884 break;
3885
hayeswang55b65472014-11-06 12:47:39 +08003886 case RTL_VER_03:
3887 case RTL_VER_04:
3888 case RTL_VER_05:
3889 ops->init = r8153_init;
3890 ops->enable = rtl8153_enable;
3891 ops->disable = rtl8153_disable;
3892 ops->up = rtl8153_up;
3893 ops->down = rtl8153_down;
3894 ops->unload = rtl8153_unload;
3895 ops->eee_get = r8153_get_eee;
3896 ops->eee_set = r8153_set_eee;
hayeswangc81229c2014-01-02 11:22:42 +08003897 break;
3898
3899 default:
hayeswang55b65472014-11-06 12:47:39 +08003900 ret = -ENODEV;
3901 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08003902 break;
3903 }
3904
3905 return ret;
3906}
3907
hayeswangac718b62013-05-02 16:01:25 +00003908static int rtl8152_probe(struct usb_interface *intf,
3909 const struct usb_device_id *id)
3910{
3911 struct usb_device *udev = interface_to_usbdev(intf);
3912 struct r8152 *tp;
3913 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08003914 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003915
hayeswang10c32712014-03-04 20:47:48 +08003916 if (udev->actconfig->desc.bConfigurationValue != 1) {
3917 usb_driver_set_configuration(udev, 1);
3918 return -ENODEV;
3919 }
3920
3921 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00003922 netdev = alloc_etherdev(sizeof(struct r8152));
3923 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003924 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00003925 return -ENOMEM;
3926 }
3927
hayeswangebc2ec482013-08-14 20:54:38 +08003928 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00003929 tp = netdev_priv(netdev);
3930 tp->msg_enable = 0x7FFF;
3931
hayeswange3ad4122014-01-06 17:08:42 +08003932 tp->udev = udev;
3933 tp->netdev = netdev;
3934 tp->intf = intf;
3935
hayeswang82cf94c2014-11-06 12:47:38 +08003936 r8152b_get_version(tp);
hayeswang55b65472014-11-06 12:47:39 +08003937 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08003938 if (ret)
3939 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08003940
hayeswangb5403272014-10-09 18:00:26 +08003941 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003942 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3943
hayeswangac718b62013-05-02 16:01:25 +00003944 netdev->netdev_ops = &rtl8152_netdev_ops;
3945 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08003946
hayeswang60c89072014-03-07 11:04:39 +08003947 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08003948 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08003949 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
3950 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08003951 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08003952 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08003953 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08003954 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08003955 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3956 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
3957 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08003958
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003959 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08003960 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00003961
3962 tp->mii.dev = netdev;
3963 tp->mii.mdio_read = read_mii_word;
3964 tp->mii.mdio_write = write_mii_word;
3965 tp->mii.phy_id_mask = 0x3f;
3966 tp->mii.reg_num_mask = 0x1f;
3967 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00003968
hayeswang9a4be1b2014-02-18 21:49:07 +08003969 intf->needs_remote_wakeup = 1;
3970
hayeswangc81229c2014-01-02 11:22:42 +08003971 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00003972 set_ethernet_addr(tp);
3973
hayeswangac718b62013-05-02 16:01:25 +00003974 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08003975 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00003976
hayeswangebc2ec482013-08-14 20:54:38 +08003977 ret = register_netdev(netdev);
3978 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003979 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08003980 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00003981 }
3982
hayeswang21ff2e82014-02-18 21:49:06 +08003983 tp->saved_wolopts = __rtl_get_wol(tp);
3984 if (tp->saved_wolopts)
3985 device_set_wakeup_enable(&udev->dev, true);
3986 else
3987 device_set_wakeup_enable(&udev->dev, false);
3988
Hayes Wang4a8deae2014-01-07 11:18:22 +08003989 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00003990
3991 return 0;
3992
hayeswangac718b62013-05-02 16:01:25 +00003993out1:
hayeswangd823ab62015-01-12 12:06:23 +08003994 netif_napi_del(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08003995 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00003996out:
3997 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08003998 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003999}
4000
hayeswangac718b62013-05-02 16:01:25 +00004001static void rtl8152_disconnect(struct usb_interface *intf)
4002{
4003 struct r8152 *tp = usb_get_intfdata(intf);
4004
4005 usb_set_intfdata(intf, NULL);
4006 if (tp) {
hayeswangf561de32014-09-30 16:48:01 +08004007 struct usb_device *udev = tp->udev;
4008
4009 if (udev->state == USB_STATE_NOTATTACHED)
4010 set_bit(RTL8152_UNPLUG, &tp->flags);
4011
hayeswangd823ab62015-01-12 12:06:23 +08004012 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004013 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08004014 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00004015 free_netdev(tp->netdev);
4016 }
4017}
4018
hayeswangd9a28c52014-12-04 10:43:11 +08004019#define REALTEK_USB_DEVICE(vend, prod) \
4020 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
4021 USB_DEVICE_ID_MATCH_INT_CLASS, \
4022 .idVendor = (vend), \
4023 .idProduct = (prod), \
4024 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
4025}, \
4026{ \
4027 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
4028 USB_DEVICE_ID_MATCH_DEVICE, \
4029 .idVendor = (vend), \
4030 .idProduct = (prod), \
4031 .bInterfaceClass = USB_CLASS_COMM, \
4032 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
4033 .bInterfaceProtocol = USB_CDC_PROTO_NONE
4034
hayeswangac718b62013-05-02 16:01:25 +00004035/* table of devices that work with this driver */
4036static struct usb_device_id rtl8152_table[] = {
hayeswangd9a28c52014-12-04 10:43:11 +08004037 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
4038 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
4039 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
hayeswangac718b62013-05-02 16:01:25 +00004040 {}
4041};
4042
4043MODULE_DEVICE_TABLE(usb, rtl8152_table);
4044
4045static struct usb_driver rtl8152_driver = {
4046 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08004047 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00004048 .probe = rtl8152_probe,
4049 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00004050 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08004051 .resume = rtl8152_resume,
4052 .reset_resume = rtl8152_resume,
hayeswang9a4be1b2014-02-18 21:49:07 +08004053 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08004054 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00004055};
4056
Sachin Kamatb4236daa2013-05-16 17:48:08 +00004057module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00004058
4059MODULE_AUTHOR(DRIVER_AUTHOR);
4060MODULE_DESCRIPTION(DRIVER_DESC);
4061MODULE_LICENSE("GPL");