blob: 2e2244221cbe6dcc8a6a26cb5eeed1bda4e2f27c [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
43#define PLA_FMC 0xc0b4
44#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080045#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000046#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080047#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000048#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080049#define PLA_TEREDO_TIMER 0xd2cc
50#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000051#define PLA_LEDSEL 0xdd90
52#define PLA_LED_FEATURE 0xdd92
53#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080054#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000055#define PLA_GPHY_INTR_IMR 0xe022
56#define PLA_EEE_CR 0xe040
57#define PLA_EEEP_CR 0xe080
58#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080059#define PLA_MAC_PWR_CTRL2 0xe0ca
60#define PLA_MAC_PWR_CTRL3 0xe0cc
61#define PLA_MAC_PWR_CTRL4 0xe0ce
62#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000063#define PLA_TCR0 0xe610
64#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080065#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000066#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080067#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000068#define PLA_CR 0xe813
69#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080070#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
71#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000072#define PLA_CONFIG5 0xe822
73#define PLA_PHY_PWR 0xe84c
74#define PLA_OOB_CTRL 0xe84f
75#define PLA_CPCR 0xe854
76#define PLA_MISC_0 0xe858
77#define PLA_MISC_1 0xe85a
78#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +080079#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000080#define PLA_SFF_STS_7 0xe8de
81#define PLA_PHYSTATUS 0xe908
82#define PLA_BP_BA 0xfc26
83#define PLA_BP_0 0xfc28
84#define PLA_BP_1 0xfc2a
85#define PLA_BP_2 0xfc2c
86#define PLA_BP_3 0xfc2e
87#define PLA_BP_4 0xfc30
88#define PLA_BP_5 0xfc32
89#define PLA_BP_6 0xfc34
90#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080091#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000092
hayeswang43779f82014-01-02 11:25:10 +080093#define USB_U2P3_CTRL 0xb460
hayeswangac718b62013-05-02 16:01:25 +000094#define USB_DEV_STAT 0xb808
95#define USB_USB_CTRL 0xd406
96#define USB_PHY_CTRL 0xd408
97#define USB_TX_AGG 0xd40a
98#define USB_RX_BUF_TH 0xd40c
99#define USB_USB_TIMER 0xd428
hayeswang43779f82014-01-02 11:25:10 +0800100#define USB_RX_EARLY_AGG 0xd42c
hayeswangac718b62013-05-02 16:01:25 +0000101#define USB_PM_CTRL_STATUS 0xd432
102#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +0800103#define USB_TOLERANCE 0xd490
104#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +0000105#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800106#define USB_MISC_0 0xd81a
107#define USB_POWER_CUT 0xd80a
108#define USB_AFE_CTRL2 0xd824
109#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000110#define USB_BP_BA 0xfc26
111#define USB_BP_0 0xfc28
112#define USB_BP_1 0xfc2a
113#define USB_BP_2 0xfc2c
114#define USB_BP_3 0xfc2e
115#define USB_BP_4 0xfc30
116#define USB_BP_5 0xfc32
117#define USB_BP_6 0xfc34
118#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800119#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000120
121/* OCP Registers */
122#define OCP_ALDPS_CONFIG 0x2010
123#define OCP_EEE_CONFIG1 0x2080
124#define OCP_EEE_CONFIG2 0x2092
125#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800126#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000127#define OCP_EEE_AR 0xa41a
128#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800129#define OCP_PHY_STATUS 0xa420
130#define OCP_POWER_CFG 0xa430
131#define OCP_EEE_CFG 0xa432
132#define OCP_SRAM_ADDR 0xa436
133#define OCP_SRAM_DATA 0xa438
134#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800135#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800136#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800137#define OCP_EEE_LPABLE 0xa5d2
hayeswang43779f82014-01-02 11:25:10 +0800138#define OCP_ADC_CFG 0xbc06
139
140/* SRAM Register */
141#define SRAM_LPF_CFG 0x8012
142#define SRAM_10M_AMP1 0x8080
143#define SRAM_10M_AMP2 0x8082
144#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000145
146/* PLA_RCR */
147#define RCR_AAP 0x00000001
148#define RCR_APM 0x00000002
149#define RCR_AM 0x00000004
150#define RCR_AB 0x00000008
151#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
152
153/* PLA_RXFIFO_CTRL0 */
154#define RXFIFO_THR1_NORMAL 0x00080002
155#define RXFIFO_THR1_OOB 0x01800003
156
157/* PLA_RXFIFO_CTRL1 */
158#define RXFIFO_THR2_FULL 0x00000060
159#define RXFIFO_THR2_HIGH 0x00000038
160#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800161#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000162
163/* PLA_RXFIFO_CTRL2 */
164#define RXFIFO_THR3_FULL 0x00000078
165#define RXFIFO_THR3_HIGH 0x00000048
166#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800167#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000168
169/* PLA_TXFIFO_CTRL */
170#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800171#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000172
173/* PLA_FMC */
174#define FMC_FCR_MCU_EN 0x0001
175
176/* PLA_EEEP_CR */
177#define EEEP_CR_EEEP_TX 0x0002
178
hayeswang43779f82014-01-02 11:25:10 +0800179/* PLA_WDT6_CTRL */
180#define WDT6_SET_MODE 0x0010
181
hayeswangac718b62013-05-02 16:01:25 +0000182/* PLA_TCR0 */
183#define TCR0_TX_EMPTY 0x0800
184#define TCR0_AUTO_FIFO 0x0080
185
186/* PLA_TCR1 */
187#define VERSION_MASK 0x7cf0
188
hayeswang69b4b7a2014-07-10 10:58:54 +0800189/* PLA_MTPS */
190#define MTPS_JUMBO (12 * 1024 / 64)
191#define MTPS_DEFAULT (6 * 1024 / 64)
192
hayeswang4f1d4d52014-03-11 16:24:19 +0800193/* PLA_RSTTALLY */
194#define TALLY_RESET 0x0001
195
hayeswangac718b62013-05-02 16:01:25 +0000196/* PLA_CR */
197#define CR_RST 0x10
198#define CR_RE 0x08
199#define CR_TE 0x04
200
201/* PLA_CRWECR */
202#define CRWECR_NORAML 0x00
203#define CRWECR_CONFIG 0xc0
204
205/* PLA_OOB_CTRL */
206#define NOW_IS_OOB 0x80
207#define TXFIFO_EMPTY 0x20
208#define RXFIFO_EMPTY 0x10
209#define LINK_LIST_READY 0x02
210#define DIS_MCU_CLROOB 0x01
211#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
212
213/* PLA_MISC_1 */
214#define RXDY_GATED_EN 0x0008
215
216/* PLA_SFF_STS_7 */
217#define RE_INIT_LL 0x8000
218#define MCU_BORW_EN 0x4000
219
220/* PLA_CPCR */
221#define CPCR_RX_VLAN 0x0040
222
223/* PLA_CFG_WOL */
224#define MAGIC_EN 0x0001
225
hayeswang43779f82014-01-02 11:25:10 +0800226/* PLA_TEREDO_CFG */
227#define TEREDO_SEL 0x8000
228#define TEREDO_WAKE_MASK 0x7f00
229#define TEREDO_RS_EVENT_MASK 0x00fe
230#define OOB_TEREDO_EN 0x0001
231
hayeswangac718b62013-05-02 16:01:25 +0000232/* PAL_BDC_CR */
233#define ALDPS_PROXY_MODE 0x0001
234
hayeswang21ff2e82014-02-18 21:49:06 +0800235/* PLA_CONFIG34 */
236#define LINK_ON_WAKE_EN 0x0010
237#define LINK_OFF_WAKE_EN 0x0008
238
hayeswangac718b62013-05-02 16:01:25 +0000239/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800240#define BWF_EN 0x0040
241#define MWF_EN 0x0020
242#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000243#define LAN_WAKE_EN 0x0002
244
245/* PLA_LED_FEATURE */
246#define LED_MODE_MASK 0x0700
247
248/* PLA_PHY_PWR */
249#define TX_10M_IDLE_EN 0x0080
250#define PFM_PWM_SWITCH 0x0040
251
252/* PLA_MAC_PWR_CTRL */
253#define D3_CLK_GATED_EN 0x00004000
254#define MCU_CLK_RATIO 0x07010f07
255#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800256#define ALDPS_SPDWN_RATIO 0x0f87
257
258/* PLA_MAC_PWR_CTRL2 */
259#define EEE_SPDWN_RATIO 0x8007
260
261/* PLA_MAC_PWR_CTRL3 */
262#define PKT_AVAIL_SPDWN_EN 0x0100
263#define SUSPEND_SPDWN_EN 0x0004
264#define U1U2_SPDWN_EN 0x0002
265#define L1_SPDWN_EN 0x0001
266
267/* PLA_MAC_PWR_CTRL4 */
268#define PWRSAVE_SPDWN_EN 0x1000
269#define RXDV_SPDWN_EN 0x0800
270#define TX10MIDLE_EN 0x0100
271#define TP100_SPDWN_EN 0x0020
272#define TP500_SPDWN_EN 0x0010
273#define TP1000_SPDWN_EN 0x0008
274#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000275
276/* PLA_GPHY_INTR_IMR */
277#define GPHY_STS_MSK 0x0001
278#define SPEED_DOWN_MSK 0x0002
279#define SPDWN_RXDV_MSK 0x0004
280#define SPDWN_LINKCHG_MSK 0x0008
281
282/* PLA_PHYAR */
283#define PHYAR_FLAG 0x80000000
284
285/* PLA_EEE_CR */
286#define EEE_RX_EN 0x0001
287#define EEE_TX_EN 0x0002
288
hayeswang43779f82014-01-02 11:25:10 +0800289/* PLA_BOOT_CTRL */
290#define AUTOLOAD_DONE 0x0002
291
hayeswangac718b62013-05-02 16:01:25 +0000292/* USB_DEV_STAT */
293#define STAT_SPEED_MASK 0x0006
294#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800295#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000296
297/* USB_TX_AGG */
298#define TX_AGG_MAX_THRESHOLD 0x03
299
300/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800301#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800302#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800303#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000304
305/* USB_TX_DMA */
306#define TEST_MODE_DISABLE 0x00000001
307#define TX_SIZE_ADJUST1 0x00000100
308
309/* USB_UPS_CTRL */
310#define POWER_CUT 0x0100
311
312/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800313#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000314
315/* USB_USB_CTRL */
316#define RX_AGG_DISABLE 0x0010
317
hayeswang43779f82014-01-02 11:25:10 +0800318/* USB_U2P3_CTRL */
319#define U2P3_ENABLE 0x0001
320
321/* USB_POWER_CUT */
322#define PWR_EN 0x0001
323#define PHASE2_EN 0x0008
324
325/* USB_MISC_0 */
326#define PCUT_STATUS 0x0001
327
328/* USB_RX_EARLY_AGG */
329#define EARLY_AGG_SUPPER 0x0e832981
330#define EARLY_AGG_HIGH 0x0e837a12
331#define EARLY_AGG_SLOW 0x0e83ffff
332
333/* USB_WDT11_CTRL */
334#define TIMER11_EN 0x0001
335
336/* USB_LPM_CTRL */
337#define LPM_TIMER_MASK 0x0c
338#define LPM_TIMER_500MS 0x04 /* 500 ms */
339#define LPM_TIMER_500US 0x0c /* 500 us */
340
341/* USB_AFE_CTRL2 */
342#define SEN_VAL_MASK 0xf800
343#define SEN_VAL_NORMAL 0xa000
344#define SEL_RXIDLE 0x0100
345
hayeswangac718b62013-05-02 16:01:25 +0000346/* OCP_ALDPS_CONFIG */
347#define ENPWRSAVE 0x8000
348#define ENPDNPS 0x0200
349#define LINKENA 0x0100
350#define DIS_SDSAVE 0x0010
351
hayeswang43779f82014-01-02 11:25:10 +0800352/* OCP_PHY_STATUS */
353#define PHY_STAT_MASK 0x0007
354#define PHY_STAT_LAN_ON 3
355#define PHY_STAT_PWRDN 5
356
357/* OCP_POWER_CFG */
358#define EEE_CLKDIV_EN 0x8000
359#define EN_ALDPS 0x0004
360#define EN_10M_PLLOFF 0x0001
361
hayeswangac718b62013-05-02 16:01:25 +0000362/* OCP_EEE_CONFIG1 */
363#define RG_TXLPI_MSK_HFDUP 0x8000
364#define RG_MATCLR_EN 0x4000
365#define EEE_10_CAP 0x2000
366#define EEE_NWAY_EN 0x1000
367#define TX_QUIET_EN 0x0200
368#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800369#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800370#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000371#define RG_RXLPI_MSK_HFDUP 0x0008
372#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
373
374/* OCP_EEE_CONFIG2 */
375#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
376#define RG_DACQUIET_EN 0x0400
377#define RG_LDVQUIET_EN 0x0200
378#define RG_CKRSEL 0x0020
379#define RG_EEEPRG_EN 0x0010
380
381/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800382#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800383#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000384#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
385#define MSK_PH 0x0006 /* bit 0 ~ 3 */
386
387/* OCP_EEE_AR */
388/* bit[15:14] function */
389#define FUN_ADDR 0x0000
390#define FUN_DATA 0x4000
391/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000392
hayeswang43779f82014-01-02 11:25:10 +0800393/* OCP_EEE_CFG */
394#define CTAP_SHORT_EN 0x0040
395#define EEE10_EN 0x0010
396
397/* OCP_DOWN_SPEED */
398#define EN_10M_BGOFF 0x0080
399
hayeswang43779f82014-01-02 11:25:10 +0800400/* OCP_ADC_CFG */
401#define CKADSEL_L 0x0100
402#define ADC_EN 0x0080
403#define EN_EMI_L 0x0040
404
405/* SRAM_LPF_CFG */
406#define LPF_AUTO_TUNE 0x8000
407
408/* SRAM_10M_AMP1 */
409#define GDAC_IB_UPALL 0x0008
410
411/* SRAM_10M_AMP2 */
412#define AMP_DN 0x0200
413
414/* SRAM_IMPEDANCE */
415#define RX_DRIVING_MASK 0x6000
416
hayeswangac718b62013-05-02 16:01:25 +0000417enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800418 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000419 _100bps = 0x08,
420 _10bps = 0x04,
421 LINK_STATUS = 0x02,
422 FULL_DUP = 0x01,
423};
424
hayeswang1764bcd2014-08-28 10:24:18 +0800425#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800426#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800427#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800428#define CRC_SIZE 4
429#define TX_ALIGN 4
430#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800431
432#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800433
hayeswangac718b62013-05-02 16:01:25 +0000434#define RTL8152_REQT_READ 0xc0
435#define RTL8152_REQT_WRITE 0x40
436#define RTL8152_REQ_GET_REGS 0x05
437#define RTL8152_REQ_SET_REGS 0x05
438
439#define BYTE_EN_DWORD 0xff
440#define BYTE_EN_WORD 0x33
441#define BYTE_EN_BYTE 0x11
442#define BYTE_EN_SIX_BYTES 0x3f
443#define BYTE_EN_START_MASK 0x0f
444#define BYTE_EN_END_MASK 0xf0
445
hayeswang69b4b7a2014-07-10 10:58:54 +0800446#define RTL8153_MAX_PACKET 9216 /* 9K */
447#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
hayeswangac718b62013-05-02 16:01:25 +0000448#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800449#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800450#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800451#define RTL8152_NAPI_WEIGHT 64
hayeswangac718b62013-05-02 16:01:25 +0000452
453/* rtl8152 flags */
454enum rtl8152_flags {
455 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000456 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800457 WORK_ENABLE,
458 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800459 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800460 PHY_RESET,
hayeswangd823ab62015-01-12 12:06:23 +0800461 SCHEDULE_NAPI,
hayeswangac718b62013-05-02 16:01:25 +0000462};
463
464/* Define these values to match your device */
465#define VENDOR_ID_REALTEK 0x0bda
hayeswang43779f82014-01-02 11:25:10 +0800466#define VENDOR_ID_SAMSUNG 0x04e8
hayeswangac718b62013-05-02 16:01:25 +0000467
468#define MCU_TYPE_PLA 0x0100
469#define MCU_TYPE_USB 0x0000
470
hayeswang4f1d4d52014-03-11 16:24:19 +0800471struct tally_counter {
472 __le64 tx_packets;
473 __le64 rx_packets;
474 __le64 tx_errors;
475 __le32 rx_errors;
476 __le16 rx_missed;
477 __le16 align_errors;
478 __le32 tx_one_collision;
479 __le32 tx_multi_collision;
480 __le64 rx_unicast;
481 __le64 rx_broadcast;
482 __le32 rx_multicast;
483 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800484 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800485};
486
hayeswangac718b62013-05-02 16:01:25 +0000487struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800488 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000489#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800490
hayeswang500b6d72013-11-20 17:30:57 +0800491 __le32 opts2;
hayeswang565cab02014-03-07 11:04:38 +0800492#define RD_UDP_CS (1 << 23)
493#define RD_TCP_CS (1 << 22)
hayeswang6128d1bb2014-03-07 11:04:40 +0800494#define RD_IPV6_CS (1 << 20)
hayeswang565cab02014-03-07 11:04:38 +0800495#define RD_IPV4_CS (1 << 19)
496
hayeswang500b6d72013-11-20 17:30:57 +0800497 __le32 opts3;
hayeswang565cab02014-03-07 11:04:38 +0800498#define IPF (1 << 23) /* IP checksum fail */
499#define UDPF (1 << 22) /* UDP checksum fail */
500#define TCPF (1 << 21) /* TCP checksum fail */
hayeswangc5554292014-09-12 10:43:11 +0800501#define RX_VLAN_TAG (1 << 16)
hayeswang565cab02014-03-07 11:04:38 +0800502
hayeswang500b6d72013-11-20 17:30:57 +0800503 __le32 opts4;
504 __le32 opts5;
505 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000506};
507
508struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800509 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000510#define TX_FS (1 << 31) /* First segment of a packet */
511#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang60c89072014-03-07 11:04:39 +0800512#define GTSENDV4 (1 << 28)
hayeswang6128d1bb2014-03-07 11:04:40 +0800513#define GTSENDV6 (1 << 27)
hayeswang60c89072014-03-07 11:04:39 +0800514#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800515#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800516#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800517
hayeswang500b6d72013-11-20 17:30:57 +0800518 __le32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800519#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
520#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
521#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
522#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800523#define MSS_SHIFT 17
524#define MSS_MAX 0x7ffU
525#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800526#define TCPHO_MAX 0x7ffU
hayeswangc5554292014-09-12 10:43:11 +0800527#define TX_VLAN_TAG (1 << 16)
hayeswangac718b62013-05-02 16:01:25 +0000528};
529
hayeswangdff4e8a2013-08-16 16:09:33 +0800530struct r8152;
531
hayeswangebc2ec482013-08-14 20:54:38 +0800532struct rx_agg {
533 struct list_head list;
534 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800535 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800536 void *buffer;
537 void *head;
538};
539
540struct tx_agg {
541 struct list_head list;
542 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800543 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800544 void *buffer;
545 void *head;
546 u32 skb_num;
547 u32 skb_len;
548};
549
hayeswangac718b62013-05-02 16:01:25 +0000550struct r8152 {
551 unsigned long flags;
552 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800553 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800554 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000555 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800556 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800557 struct tx_agg tx_info[RTL8152_MAX_TX];
558 struct rx_agg rx_info[RTL8152_MAX_RX];
559 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800560 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800561 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000562 struct delayed_work schedule;
563 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800564 struct mutex control; /* use for hw setting */
hayeswangc81229c2014-01-02 11:22:42 +0800565
566 struct rtl_ops {
567 void (*init)(struct r8152 *);
568 int (*enable)(struct r8152 *);
569 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800570 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800571 void (*down)(struct r8152 *);
572 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800573 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
574 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswangc81229c2014-01-02 11:22:42 +0800575 } rtl_ops;
576
hayeswang40a82912013-08-14 20:54:40 +0800577 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800578 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000579 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800580 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000581 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800582 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000583 u8 version;
584 u8 speed;
585};
586
587enum rtl_version {
588 RTL_VER_UNKNOWN = 0,
589 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800590 RTL_VER_02,
591 RTL_VER_03,
592 RTL_VER_04,
593 RTL_VER_05,
594 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000595};
596
hayeswang60c89072014-03-07 11:04:39 +0800597enum tx_csum_stat {
598 TX_CSUM_SUCCESS = 0,
599 TX_CSUM_TSO,
600 TX_CSUM_NONE
601};
602
hayeswangac718b62013-05-02 16:01:25 +0000603/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
604 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
605 */
606static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800607static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000608
hayeswang52aec122014-09-02 10:27:52 +0800609#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswang60c89072014-03-07 11:04:39 +0800610 VLAN_ETH_HLEN - VLAN_HLEN)
611
hayeswangac718b62013-05-02 16:01:25 +0000612static
613int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
614{
hayeswang31787f52013-07-31 17:21:25 +0800615 int ret;
616 void *tmp;
617
618 tmp = kmalloc(size, GFP_KERNEL);
619 if (!tmp)
620 return -ENOMEM;
621
622 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800623 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
624 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800625
626 memcpy(data, tmp, size);
627 kfree(tmp);
628
629 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000630}
631
632static
633int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
634{
hayeswang31787f52013-07-31 17:21:25 +0800635 int ret;
636 void *tmp;
637
Benoit Tainec4438f02014-05-26 17:21:23 +0200638 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800639 if (!tmp)
640 return -ENOMEM;
641
hayeswang31787f52013-07-31 17:21:25 +0800642 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800643 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
644 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800645
646 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800647
hayeswang31787f52013-07-31 17:21:25 +0800648 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000649}
650
651static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800652 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000653{
hayeswang45f4a192014-01-06 17:08:41 +0800654 u16 limit = 64;
655 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000656
657 if (test_bit(RTL8152_UNPLUG, &tp->flags))
658 return -ENODEV;
659
660 /* both size and indix must be 4 bytes align */
661 if ((size & 3) || !size || (index & 3) || !data)
662 return -EPERM;
663
664 if ((u32)index + (u32)size > 0xffff)
665 return -EPERM;
666
667 while (size) {
668 if (size > limit) {
669 ret = get_registers(tp, index, type, limit, data);
670 if (ret < 0)
671 break;
672
673 index += limit;
674 data += limit;
675 size -= limit;
676 } else {
677 ret = get_registers(tp, index, type, size, data);
678 if (ret < 0)
679 break;
680
681 index += size;
682 data += size;
683 size = 0;
684 break;
685 }
686 }
687
hayeswang67610492014-10-30 11:46:40 +0800688 if (ret == -ENODEV)
689 set_bit(RTL8152_UNPLUG, &tp->flags);
690
hayeswangac718b62013-05-02 16:01:25 +0000691 return ret;
692}
693
694static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800695 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000696{
hayeswang45f4a192014-01-06 17:08:41 +0800697 int ret;
698 u16 byteen_start, byteen_end, byen;
699 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000700
701 if (test_bit(RTL8152_UNPLUG, &tp->flags))
702 return -ENODEV;
703
704 /* both size and indix must be 4 bytes align */
705 if ((size & 3) || !size || (index & 3) || !data)
706 return -EPERM;
707
708 if ((u32)index + (u32)size > 0xffff)
709 return -EPERM;
710
711 byteen_start = byteen & BYTE_EN_START_MASK;
712 byteen_end = byteen & BYTE_EN_END_MASK;
713
714 byen = byteen_start | (byteen_start << 4);
715 ret = set_registers(tp, index, type | byen, 4, data);
716 if (ret < 0)
717 goto error1;
718
719 index += 4;
720 data += 4;
721 size -= 4;
722
723 if (size) {
724 size -= 4;
725
726 while (size) {
727 if (size > limit) {
728 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800729 type | BYTE_EN_DWORD,
730 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000731 if (ret < 0)
732 goto error1;
733
734 index += limit;
735 data += limit;
736 size -= limit;
737 } else {
738 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800739 type | BYTE_EN_DWORD,
740 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000741 if (ret < 0)
742 goto error1;
743
744 index += size;
745 data += size;
746 size = 0;
747 break;
748 }
749 }
750
751 byen = byteen_end | (byteen_end >> 4);
752 ret = set_registers(tp, index, type | byen, 4, data);
753 if (ret < 0)
754 goto error1;
755 }
756
757error1:
hayeswang67610492014-10-30 11:46:40 +0800758 if (ret == -ENODEV)
759 set_bit(RTL8152_UNPLUG, &tp->flags);
760
hayeswangac718b62013-05-02 16:01:25 +0000761 return ret;
762}
763
764static inline
765int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
766{
767 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
768}
769
770static inline
771int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
772{
773 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
774}
775
776static inline
777int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
778{
779 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
780}
781
782static inline
783int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
784{
785 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
786}
787
788static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
789{
hayeswangc8826de2013-07-31 17:21:26 +0800790 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000791
hayeswangc8826de2013-07-31 17:21:26 +0800792 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000793
794 return __le32_to_cpu(data);
795}
796
797static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
798{
hayeswangc8826de2013-07-31 17:21:26 +0800799 __le32 tmp = __cpu_to_le32(data);
800
801 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000802}
803
804static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
805{
806 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800807 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000808 u8 shift = index & 2;
809
810 index &= ~3;
811
hayeswangc8826de2013-07-31 17:21:26 +0800812 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000813
hayeswangc8826de2013-07-31 17:21:26 +0800814 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000815 data >>= (shift * 8);
816 data &= 0xffff;
817
818 return (u16)data;
819}
820
821static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
822{
hayeswangc8826de2013-07-31 17:21:26 +0800823 u32 mask = 0xffff;
824 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000825 u16 byen = BYTE_EN_WORD;
826 u8 shift = index & 2;
827
828 data &= mask;
829
830 if (index & 2) {
831 byen <<= shift;
832 mask <<= (shift * 8);
833 data <<= (shift * 8);
834 index &= ~3;
835 }
836
hayeswangc8826de2013-07-31 17:21:26 +0800837 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000838
hayeswangc8826de2013-07-31 17:21:26 +0800839 data |= __le32_to_cpu(tmp) & ~mask;
840 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000841
hayeswangc8826de2013-07-31 17:21:26 +0800842 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000843}
844
845static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
846{
847 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800848 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000849 u8 shift = index & 3;
850
851 index &= ~3;
852
hayeswangc8826de2013-07-31 17:21:26 +0800853 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000854
hayeswangc8826de2013-07-31 17:21:26 +0800855 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000856 data >>= (shift * 8);
857 data &= 0xff;
858
859 return (u8)data;
860}
861
862static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
863{
hayeswangc8826de2013-07-31 17:21:26 +0800864 u32 mask = 0xff;
865 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000866 u16 byen = BYTE_EN_BYTE;
867 u8 shift = index & 3;
868
869 data &= mask;
870
871 if (index & 3) {
872 byen <<= shift;
873 mask <<= (shift * 8);
874 data <<= (shift * 8);
875 index &= ~3;
876 }
877
hayeswangc8826de2013-07-31 17:21:26 +0800878 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000879
hayeswangc8826de2013-07-31 17:21:26 +0800880 data |= __le32_to_cpu(tmp) & ~mask;
881 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000882
hayeswangc8826de2013-07-31 17:21:26 +0800883 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000884}
885
hayeswangac244d32014-01-02 11:22:40 +0800886static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
887{
888 u16 ocp_base, ocp_index;
889
890 ocp_base = addr & 0xf000;
891 if (ocp_base != tp->ocp_base) {
892 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
893 tp->ocp_base = ocp_base;
894 }
895
896 ocp_index = (addr & 0x0fff) | 0xb000;
897 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
898}
899
hayeswange3fe0b12014-01-02 11:22:39 +0800900static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
901{
902 u16 ocp_base, ocp_index;
903
904 ocp_base = addr & 0xf000;
905 if (ocp_base != tp->ocp_base) {
906 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
907 tp->ocp_base = ocp_base;
908 }
909
910 ocp_index = (addr & 0x0fff) | 0xb000;
911 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
912}
913
hayeswangac244d32014-01-02 11:22:40 +0800914static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000915{
hayeswangac244d32014-01-02 11:22:40 +0800916 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000917}
918
hayeswangac244d32014-01-02 11:22:40 +0800919static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000920{
hayeswangac244d32014-01-02 11:22:40 +0800921 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000922}
923
hayeswang43779f82014-01-02 11:25:10 +0800924static void sram_write(struct r8152 *tp, u16 addr, u16 data)
925{
926 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
927 ocp_reg_write(tp, OCP_SRAM_DATA, data);
928}
929
930static u16 sram_read(struct r8152 *tp, u16 addr)
931{
932 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
933 return ocp_reg_read(tp, OCP_SRAM_DATA);
934}
935
hayeswangac718b62013-05-02 16:01:25 +0000936static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
937{
938 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +0800939 int ret;
hayeswangac718b62013-05-02 16:01:25 +0000940
hayeswang68714382014-04-11 17:54:31 +0800941 if (test_bit(RTL8152_UNPLUG, &tp->flags))
942 return -ENODEV;
943
hayeswangac718b62013-05-02 16:01:25 +0000944 if (phy_id != R8152_PHY_ID)
945 return -EINVAL;
946
hayeswang9a4be1b2014-02-18 21:49:07 +0800947 ret = r8152_mdio_read(tp, reg);
948
hayeswang9a4be1b2014-02-18 21:49:07 +0800949 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000950}
951
952static
953void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
954{
955 struct r8152 *tp = netdev_priv(netdev);
956
hayeswang68714382014-04-11 17:54:31 +0800957 if (test_bit(RTL8152_UNPLUG, &tp->flags))
958 return;
959
hayeswangac718b62013-05-02 16:01:25 +0000960 if (phy_id != R8152_PHY_ID)
961 return;
962
963 r8152_mdio_write(tp, reg, val);
964}
965
hayeswangb209af92014-08-25 15:53:00 +0800966static int
967r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800968
hayeswang8ba789a2014-09-04 16:15:41 +0800969static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
970{
971 struct r8152 *tp = netdev_priv(netdev);
972 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +0800973 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +0800974
975 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +0800976 goto out1;
977
978 ret = usb_autopm_get_interface(tp->intf);
979 if (ret < 0)
980 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +0800981
hayeswangb5403272014-10-09 18:00:26 +0800982 mutex_lock(&tp->control);
983
hayeswang8ba789a2014-09-04 16:15:41 +0800984 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
985
986 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
987 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
988 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
989
hayeswangb5403272014-10-09 18:00:26 +0800990 mutex_unlock(&tp->control);
991
hayeswangea6a7112014-10-02 17:03:12 +0800992 usb_autopm_put_interface(tp->intf);
993out1:
994 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +0800995}
996
hayeswang179bb6d2014-09-04 16:15:42 +0800997static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +0000998{
999 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001000 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001001 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001002
hayeswang8a91c822014-02-18 21:49:01 +08001003 if (tp->version == RTL_VER_01)
hayeswang179bb6d2014-09-04 16:15:42 +08001004 ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001005 else
hayeswang179bb6d2014-09-04 16:15:42 +08001006 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001007
1008 if (ret < 0) {
hayeswang179bb6d2014-09-04 16:15:42 +08001009 netif_err(tp, probe, dev, "Get ether addr fail\n");
1010 } else if (!is_valid_ether_addr(sa.sa_data)) {
1011 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1012 sa.sa_data);
1013 eth_hw_addr_random(dev);
1014 ether_addr_copy(sa.sa_data, dev->dev_addr);
1015 ret = rtl8152_set_mac_address(dev, &sa);
1016 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1017 sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001018 } else {
hayeswang179bb6d2014-09-04 16:15:42 +08001019 if (tp->version == RTL_VER_01)
1020 ether_addr_copy(dev->dev_addr, sa.sa_data);
1021 else
1022 ret = rtl8152_set_mac_address(dev, &sa);
hayeswangac718b62013-05-02 16:01:25 +00001023 }
hayeswang179bb6d2014-09-04 16:15:42 +08001024
1025 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001026}
1027
hayeswangac718b62013-05-02 16:01:25 +00001028static void read_bulk_callback(struct urb *urb)
1029{
hayeswangac718b62013-05-02 16:01:25 +00001030 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001031 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001032 struct rx_agg *agg;
1033 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +00001034
hayeswangebc2ec482013-08-14 20:54:38 +08001035 agg = urb->context;
1036 if (!agg)
1037 return;
1038
1039 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001040 if (!tp)
1041 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001042
hayeswangac718b62013-05-02 16:01:25 +00001043 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1044 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001045
1046 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001047 return;
1048
hayeswangebc2ec482013-08-14 20:54:38 +08001049 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001050
1051 /* When link down, the driver would cancel all bulks. */
1052 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001053 if (!netif_carrier_ok(netdev))
1054 return;
1055
hayeswang9a4be1b2014-02-18 21:49:07 +08001056 usb_mark_last_busy(tp->udev);
1057
hayeswangac718b62013-05-02 16:01:25 +00001058 switch (status) {
1059 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001060 if (urb->actual_length < ETH_ZLEN)
1061 break;
1062
hayeswang2685d412014-03-07 11:04:34 +08001063 spin_lock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001064 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001065 spin_unlock(&tp->rx_lock);
hayeswangd823ab62015-01-12 12:06:23 +08001066 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001067 return;
hayeswangac718b62013-05-02 16:01:25 +00001068 case -ESHUTDOWN:
1069 set_bit(RTL8152_UNPLUG, &tp->flags);
1070 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001071 return;
hayeswangac718b62013-05-02 16:01:25 +00001072 case -ENOENT:
1073 return; /* the urb is in unlink state */
1074 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001075 if (net_ratelimit())
1076 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001077 break;
hayeswangac718b62013-05-02 16:01:25 +00001078 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001079 if (net_ratelimit())
1080 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001081 break;
hayeswangac718b62013-05-02 16:01:25 +00001082 }
1083
hayeswanga0fccd42014-11-20 10:29:05 +08001084 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001085}
1086
1087static void write_bulk_callback(struct urb *urb)
1088{
hayeswangebc2ec482013-08-14 20:54:38 +08001089 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001090 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001091 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001092 struct r8152 *tp;
1093 int status = urb->status;
1094
hayeswangebc2ec482013-08-14 20:54:38 +08001095 agg = urb->context;
1096 if (!agg)
1097 return;
1098
1099 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001100 if (!tp)
1101 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001102
hayeswangd104eaf2014-03-06 15:07:17 +08001103 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001104 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001105 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001106 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001107 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001108 stats->tx_errors += agg->skb_num;
1109 } else {
1110 stats->tx_packets += agg->skb_num;
1111 stats->tx_bytes += agg->skb_len;
1112 }
1113
hayeswang2685d412014-03-07 11:04:34 +08001114 spin_lock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001115 list_add_tail(&agg->list, &tp->tx_free);
hayeswang2685d412014-03-07 11:04:34 +08001116 spin_unlock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001117
hayeswang9a4be1b2014-02-18 21:49:07 +08001118 usb_autopm_put_interface_async(tp->intf);
1119
hayeswangd104eaf2014-03-06 15:07:17 +08001120 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001121 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001122
1123 if (!test_bit(WORK_ENABLE, &tp->flags))
1124 return;
1125
1126 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1127 return;
1128
1129 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001130 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001131}
1132
hayeswang40a82912013-08-14 20:54:40 +08001133static void intr_callback(struct urb *urb)
1134{
1135 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001136 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001137 int status = urb->status;
1138 int res;
1139
1140 tp = urb->context;
1141 if (!tp)
1142 return;
1143
1144 if (!test_bit(WORK_ENABLE, &tp->flags))
1145 return;
1146
1147 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1148 return;
1149
1150 switch (status) {
1151 case 0: /* success */
1152 break;
1153 case -ECONNRESET: /* unlink */
1154 case -ESHUTDOWN:
1155 netif_device_detach(tp->netdev);
1156 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001157 case -EPROTO:
1158 netif_info(tp, intr, tp->netdev,
1159 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001160 return;
1161 case -EOVERFLOW:
1162 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1163 goto resubmit;
1164 /* -EPIPE: should clear the halt */
1165 default:
1166 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1167 goto resubmit;
1168 }
1169
1170 d = urb->transfer_buffer;
1171 if (INTR_LINK & __le16_to_cpu(d[0])) {
1172 if (!(tp->speed & LINK_STATUS)) {
1173 set_bit(RTL8152_LINK_CHG, &tp->flags);
1174 schedule_delayed_work(&tp->schedule, 0);
1175 }
1176 } else {
1177 if (tp->speed & LINK_STATUS) {
1178 set_bit(RTL8152_LINK_CHG, &tp->flags);
1179 schedule_delayed_work(&tp->schedule, 0);
1180 }
1181 }
1182
1183resubmit:
1184 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001185 if (res == -ENODEV) {
1186 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001187 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001188 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001189 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001190 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001191 }
hayeswang40a82912013-08-14 20:54:40 +08001192}
1193
hayeswangebc2ec482013-08-14 20:54:38 +08001194static inline void *rx_agg_align(void *data)
1195{
hayeswang8e1f51b2014-01-02 11:22:41 +08001196 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001197}
1198
1199static inline void *tx_agg_align(void *data)
1200{
hayeswang8e1f51b2014-01-02 11:22:41 +08001201 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001202}
1203
1204static void free_all_mem(struct r8152 *tp)
1205{
1206 int i;
1207
1208 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001209 usb_free_urb(tp->rx_info[i].urb);
1210 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001211
hayeswang9629e3c2014-01-15 10:42:15 +08001212 kfree(tp->rx_info[i].buffer);
1213 tp->rx_info[i].buffer = NULL;
1214 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001215 }
1216
1217 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001218 usb_free_urb(tp->tx_info[i].urb);
1219 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001220
hayeswang9629e3c2014-01-15 10:42:15 +08001221 kfree(tp->tx_info[i].buffer);
1222 tp->tx_info[i].buffer = NULL;
1223 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001224 }
hayeswang40a82912013-08-14 20:54:40 +08001225
hayeswang9629e3c2014-01-15 10:42:15 +08001226 usb_free_urb(tp->intr_urb);
1227 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001228
hayeswang9629e3c2014-01-15 10:42:15 +08001229 kfree(tp->intr_buff);
1230 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001231}
1232
1233static int alloc_all_mem(struct r8152 *tp)
1234{
1235 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001236 struct usb_interface *intf = tp->intf;
1237 struct usb_host_interface *alt = intf->cur_altsetting;
1238 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001239 struct urb *urb;
1240 int node, i;
1241 u8 *buf;
1242
1243 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1244
1245 spin_lock_init(&tp->rx_lock);
1246 spin_lock_init(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001247 INIT_LIST_HEAD(&tp->tx_free);
1248 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001249 skb_queue_head_init(&tp->rx_queue);
hayeswangebc2ec482013-08-14 20:54:38 +08001250
1251 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001252 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001253 if (!buf)
1254 goto err1;
1255
1256 if (buf != rx_agg_align(buf)) {
1257 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001258 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001259 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001260 if (!buf)
1261 goto err1;
1262 }
1263
1264 urb = usb_alloc_urb(0, GFP_KERNEL);
1265 if (!urb) {
1266 kfree(buf);
1267 goto err1;
1268 }
1269
1270 INIT_LIST_HEAD(&tp->rx_info[i].list);
1271 tp->rx_info[i].context = tp;
1272 tp->rx_info[i].urb = urb;
1273 tp->rx_info[i].buffer = buf;
1274 tp->rx_info[i].head = rx_agg_align(buf);
1275 }
1276
1277 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001278 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001279 if (!buf)
1280 goto err1;
1281
1282 if (buf != tx_agg_align(buf)) {
1283 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001284 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001285 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001286 if (!buf)
1287 goto err1;
1288 }
1289
1290 urb = usb_alloc_urb(0, GFP_KERNEL);
1291 if (!urb) {
1292 kfree(buf);
1293 goto err1;
1294 }
1295
1296 INIT_LIST_HEAD(&tp->tx_info[i].list);
1297 tp->tx_info[i].context = tp;
1298 tp->tx_info[i].urb = urb;
1299 tp->tx_info[i].buffer = buf;
1300 tp->tx_info[i].head = tx_agg_align(buf);
1301
1302 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1303 }
1304
hayeswang40a82912013-08-14 20:54:40 +08001305 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1306 if (!tp->intr_urb)
1307 goto err1;
1308
1309 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1310 if (!tp->intr_buff)
1311 goto err1;
1312
1313 tp->intr_interval = (int)ep_intr->desc.bInterval;
1314 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001315 tp->intr_buff, INTBUFSIZE, intr_callback,
1316 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001317
hayeswangebc2ec482013-08-14 20:54:38 +08001318 return 0;
1319
1320err1:
1321 free_all_mem(tp);
1322 return -ENOMEM;
1323}
1324
hayeswang0de98f62013-08-16 16:09:35 +08001325static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1326{
1327 struct tx_agg *agg = NULL;
1328 unsigned long flags;
1329
hayeswang21949ab2014-03-07 11:04:35 +08001330 if (list_empty(&tp->tx_free))
1331 return NULL;
1332
hayeswang0de98f62013-08-16 16:09:35 +08001333 spin_lock_irqsave(&tp->tx_lock, flags);
1334 if (!list_empty(&tp->tx_free)) {
1335 struct list_head *cursor;
1336
1337 cursor = tp->tx_free.next;
1338 list_del_init(cursor);
1339 agg = list_entry(cursor, struct tx_agg, list);
1340 }
1341 spin_unlock_irqrestore(&tp->tx_lock, flags);
1342
1343 return agg;
1344}
1345
hayeswang60c89072014-03-07 11:04:39 +08001346static inline __be16 get_protocol(struct sk_buff *skb)
hayeswang5bd23882013-08-14 20:54:39 +08001347{
hayeswang60c89072014-03-07 11:04:39 +08001348 __be16 protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001349
hayeswang60c89072014-03-07 11:04:39 +08001350 if (skb->protocol == htons(ETH_P_8021Q))
1351 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1352 else
1353 protocol = skb->protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001354
hayeswang60c89072014-03-07 11:04:39 +08001355 return protocol;
1356}
1357
hayeswangb209af92014-08-25 15:53:00 +08001358/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001359 * The hw limites the value the transport offset. When the offset is out of the
1360 * range, calculate the checksum by sw.
1361 */
1362static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1363 struct sk_buff_head *list)
1364{
1365 if (skb_shinfo(skb)->gso_size) {
1366 netdev_features_t features = tp->netdev->features;
1367 struct sk_buff_head seg_list;
1368 struct sk_buff *segs, *nskb;
1369
hayeswanga91d45f2014-07-11 16:48:27 +08001370 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001371 segs = skb_gso_segment(skb, features);
1372 if (IS_ERR(segs) || !segs)
1373 goto drop;
1374
1375 __skb_queue_head_init(&seg_list);
1376
1377 do {
1378 nskb = segs;
1379 segs = segs->next;
1380 nskb->next = NULL;
1381 __skb_queue_tail(&seg_list, nskb);
1382 } while (segs);
1383
1384 skb_queue_splice(&seg_list, list);
1385 dev_kfree_skb(skb);
1386 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1387 if (skb_checksum_help(skb) < 0)
1388 goto drop;
1389
1390 __skb_queue_head(list, skb);
1391 } else {
1392 struct net_device_stats *stats;
1393
1394drop:
1395 stats = &tp->netdev->stats;
1396 stats->tx_dropped++;
1397 dev_kfree_skb(skb);
1398 }
1399}
1400
hayeswangb209af92014-08-25 15:53:00 +08001401/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001402 * According to the document of microsoft, the TCP Pseudo Header excludes the
1403 * packet length for IPv6 TCP large packets.
1404 */
1405static int msdn_giant_send_check(struct sk_buff *skb)
1406{
1407 const struct ipv6hdr *ipv6h;
1408 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001409 int ret;
1410
1411 ret = skb_cow_head(skb, 0);
1412 if (ret)
1413 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001414
1415 ipv6h = ipv6_hdr(skb);
1416 th = tcp_hdr(skb);
1417
1418 th->check = 0;
1419 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1420
hayeswangfcb308d2014-03-11 10:20:32 +08001421 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001422}
1423
hayeswangc5554292014-09-12 10:43:11 +08001424static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1425{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001426 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001427 u32 opts2;
1428
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001429 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001430 desc->opts2 |= cpu_to_le32(opts2);
1431 }
1432}
1433
1434static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1435{
1436 u32 opts2 = le32_to_cpu(desc->opts2);
1437
1438 if (opts2 & RX_VLAN_TAG)
1439 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1440 swab16(opts2 & 0xffff));
1441}
1442
hayeswang60c89072014-03-07 11:04:39 +08001443static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1444 struct sk_buff *skb, u32 len, u32 transport_offset)
1445{
1446 u32 mss = skb_shinfo(skb)->gso_size;
1447 u32 opts1, opts2 = 0;
1448 int ret = TX_CSUM_SUCCESS;
1449
1450 WARN_ON_ONCE(len > TX_LEN_MAX);
1451
1452 opts1 = len | TX_FS | TX_LS;
1453
1454 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001455 if (transport_offset > GTTCPHO_MAX) {
1456 netif_warn(tp, tx_err, tp->netdev,
1457 "Invalid transport offset 0x%x for TSO\n",
1458 transport_offset);
1459 ret = TX_CSUM_TSO;
1460 goto unavailable;
1461 }
1462
hayeswang60c89072014-03-07 11:04:39 +08001463 switch (get_protocol(skb)) {
1464 case htons(ETH_P_IP):
1465 opts1 |= GTSENDV4;
1466 break;
1467
hayeswang6128d1bb2014-03-07 11:04:40 +08001468 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001469 if (msdn_giant_send_check(skb)) {
1470 ret = TX_CSUM_TSO;
1471 goto unavailable;
1472 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001473 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001474 break;
1475
hayeswang60c89072014-03-07 11:04:39 +08001476 default:
1477 WARN_ON_ONCE(1);
1478 break;
1479 }
1480
1481 opts1 |= transport_offset << GTTCPHO_SHIFT;
1482 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1483 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001484 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001485
hayeswang6128d1bb2014-03-07 11:04:40 +08001486 if (transport_offset > TCPHO_MAX) {
1487 netif_warn(tp, tx_err, tp->netdev,
1488 "Invalid transport offset 0x%x\n",
1489 transport_offset);
1490 ret = TX_CSUM_NONE;
1491 goto unavailable;
1492 }
1493
hayeswang60c89072014-03-07 11:04:39 +08001494 switch (get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001495 case htons(ETH_P_IP):
1496 opts2 |= IPV4_CS;
1497 ip_protocol = ip_hdr(skb)->protocol;
1498 break;
1499
1500 case htons(ETH_P_IPV6):
1501 opts2 |= IPV6_CS;
1502 ip_protocol = ipv6_hdr(skb)->nexthdr;
1503 break;
1504
1505 default:
1506 ip_protocol = IPPROTO_RAW;
1507 break;
1508 }
1509
hayeswang60c89072014-03-07 11:04:39 +08001510 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001511 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001512 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001513 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001514 else
hayeswang5bd23882013-08-14 20:54:39 +08001515 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001516
hayeswang60c89072014-03-07 11:04:39 +08001517 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001518 }
hayeswang60c89072014-03-07 11:04:39 +08001519
1520 desc->opts2 = cpu_to_le32(opts2);
1521 desc->opts1 = cpu_to_le32(opts1);
1522
hayeswang6128d1bb2014-03-07 11:04:40 +08001523unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001524 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001525}
1526
hayeswangb1379d92013-08-16 16:09:37 +08001527static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1528{
hayeswangd84130a2014-02-18 21:49:02 +08001529 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001530 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001531 u8 *tx_data;
1532
hayeswangd84130a2014-02-18 21:49:02 +08001533 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001534 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001535 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001536 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001537
hayeswangb1379d92013-08-16 16:09:37 +08001538 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001539 agg->skb_num = 0;
1540 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001541 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001542
hayeswang7937f9e2013-11-20 17:30:54 +08001543 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001544 struct tx_desc *tx_desc;
1545 struct sk_buff *skb;
1546 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001547 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001548
hayeswangd84130a2014-02-18 21:49:02 +08001549 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001550 if (!skb)
1551 break;
1552
hayeswang60c89072014-03-07 11:04:39 +08001553 len = skb->len + sizeof(*tx_desc);
1554
1555 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001556 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001557 break;
1558 }
1559
hayeswang7937f9e2013-11-20 17:30:54 +08001560 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001561 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001562
1563 offset = (u32)skb_transport_offset(skb);
1564
hayeswang6128d1bb2014-03-07 11:04:40 +08001565 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1566 r8152_csum_workaround(tp, skb, &skb_head);
1567 continue;
1568 }
hayeswang60c89072014-03-07 11:04:39 +08001569
hayeswangc5554292014-09-12 10:43:11 +08001570 rtl_tx_vlan_tag(tx_desc, skb);
1571
hayeswangb1379d92013-08-16 16:09:37 +08001572 tx_data += sizeof(*tx_desc);
1573
hayeswang60c89072014-03-07 11:04:39 +08001574 len = skb->len;
1575 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1576 struct net_device_stats *stats = &tp->netdev->stats;
1577
1578 stats->tx_dropped++;
1579 dev_kfree_skb_any(skb);
1580 tx_data -= sizeof(*tx_desc);
1581 continue;
1582 }
hayeswangb1379d92013-08-16 16:09:37 +08001583
hayeswang7937f9e2013-11-20 17:30:54 +08001584 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001585 agg->skb_len += len;
1586 agg->skb_num++;
1587
1588 dev_kfree_skb_any(skb);
1589
hayeswang52aec122014-09-02 10:27:52 +08001590 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001591 }
1592
hayeswangd84130a2014-02-18 21:49:02 +08001593 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001594 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001595 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001596 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001597 }
1598
hayeswang0c3121f2014-03-07 11:04:36 +08001599 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001600
1601 if (netif_queue_stopped(tp->netdev) &&
1602 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1603 netif_wake_queue(tp->netdev);
1604
hayeswang0c3121f2014-03-07 11:04:36 +08001605 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001606
hayeswang0c3121f2014-03-07 11:04:36 +08001607 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001608 if (ret < 0)
1609 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001610
hayeswangb1379d92013-08-16 16:09:37 +08001611 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1612 agg->head, (int)(tx_data - (u8 *)agg->head),
1613 (usb_complete_t)write_bulk_callback, agg);
1614
hayeswang0c3121f2014-03-07 11:04:36 +08001615 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001616 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001617 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001618
1619out_tx_fill:
1620 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001621}
1622
hayeswang565cab02014-03-07 11:04:38 +08001623static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1624{
1625 u8 checksum = CHECKSUM_NONE;
1626 u32 opts2, opts3;
1627
1628 if (tp->version == RTL_VER_01)
1629 goto return_result;
1630
1631 opts2 = le32_to_cpu(rx_desc->opts2);
1632 opts3 = le32_to_cpu(rx_desc->opts3);
1633
1634 if (opts2 & RD_IPV4_CS) {
1635 if (opts3 & IPF)
1636 checksum = CHECKSUM_NONE;
1637 else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
1638 checksum = CHECKSUM_NONE;
1639 else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
1640 checksum = CHECKSUM_NONE;
1641 else
1642 checksum = CHECKSUM_UNNECESSARY;
hayeswang6128d1bb2014-03-07 11:04:40 +08001643 } else if (RD_IPV6_CS) {
1644 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1645 checksum = CHECKSUM_UNNECESSARY;
1646 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1647 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001648 }
1649
1650return_result:
1651 return checksum;
1652}
1653
hayeswangd823ab62015-01-12 12:06:23 +08001654static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08001655{
hayeswanga5a4f462013-08-16 16:09:34 +08001656 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001657 struct list_head *cursor, *next, rx_queue;
hayeswangd823ab62015-01-12 12:06:23 +08001658 int work_done = 0;
1659
1660 if (!skb_queue_empty(&tp->rx_queue)) {
1661 while (work_done < budget) {
1662 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1663 struct net_device *netdev = tp->netdev;
1664 struct net_device_stats *stats = &netdev->stats;
1665 unsigned int pkt_len;
1666
1667 if (!skb)
1668 break;
1669
1670 pkt_len = skb->len;
1671 napi_gro_receive(&tp->napi, skb);
1672 work_done++;
1673 stats->rx_packets++;
1674 stats->rx_bytes += pkt_len;
1675 }
1676 }
hayeswangebc2ec482013-08-14 20:54:38 +08001677
hayeswangd84130a2014-02-18 21:49:02 +08001678 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08001679 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08001680
1681 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001682 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001683 list_splice_init(&tp->rx_done, &rx_queue);
1684 spin_unlock_irqrestore(&tp->rx_lock, flags);
1685
1686 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001687 struct rx_desc *rx_desc;
1688 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001689 int len_used = 0;
1690 struct urb *urb;
1691 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08001692
hayeswangebc2ec482013-08-14 20:54:38 +08001693 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001694
1695 agg = list_entry(cursor, struct rx_agg, list);
1696 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001697 if (urb->actual_length < ETH_ZLEN)
1698 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001699
hayeswangebc2ec482013-08-14 20:54:38 +08001700 rx_desc = agg->head;
1701 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001702 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001703
hayeswang7937f9e2013-11-20 17:30:54 +08001704 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001705 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001706 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001707 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001708 struct sk_buff *skb;
1709
hayeswang7937f9e2013-11-20 17:30:54 +08001710 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001711 if (pkt_len < ETH_ZLEN)
1712 break;
1713
hayeswang7937f9e2013-11-20 17:30:54 +08001714 len_used += pkt_len;
1715 if (urb->actual_length < len_used)
1716 break;
1717
hayeswang8e1f51b2014-01-02 11:22:41 +08001718 pkt_len -= CRC_SIZE;
hayeswangebc2ec482013-08-14 20:54:38 +08001719 rx_data += sizeof(struct rx_desc);
1720
1721 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1722 if (!skb) {
1723 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001724 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08001725 }
hayeswang565cab02014-03-07 11:04:38 +08001726
1727 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001728 memcpy(skb->data, rx_data, pkt_len);
1729 skb_put(skb, pkt_len);
1730 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08001731 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001732 if (work_done < budget) {
1733 napi_gro_receive(&tp->napi, skb);
1734 work_done++;
1735 stats->rx_packets++;
1736 stats->rx_bytes += pkt_len;
1737 } else {
1738 __skb_queue_tail(&tp->rx_queue, skb);
1739 }
hayeswangebc2ec482013-08-14 20:54:38 +08001740
hayeswang5e2f7482014-03-07 11:04:37 +08001741find_next_rx:
hayeswang8e1f51b2014-01-02 11:22:41 +08001742 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec482013-08-14 20:54:38 +08001743 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001744 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001745 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001746 }
1747
hayeswang0de98f62013-08-16 16:09:35 +08001748submit:
hayeswanga0fccd42014-11-20 10:29:05 +08001749 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangebc2ec482013-08-14 20:54:38 +08001750 }
hayeswangd823ab62015-01-12 12:06:23 +08001751
1752out1:
1753 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08001754}
1755
1756static void tx_bottom(struct r8152 *tp)
1757{
hayeswangebc2ec482013-08-14 20:54:38 +08001758 int res;
1759
hayeswangb1379d92013-08-16 16:09:37 +08001760 do {
1761 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001762
hayeswangb1379d92013-08-16 16:09:37 +08001763 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001764 break;
1765
hayeswangb1379d92013-08-16 16:09:37 +08001766 agg = r8152_get_tx_agg(tp);
1767 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001768 break;
hayeswangb1379d92013-08-16 16:09:37 +08001769
1770 res = r8152_tx_agg_fill(tp, agg);
1771 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08001772 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08001773
1774 if (res == -ENODEV) {
hayeswang67610492014-10-30 11:46:40 +08001775 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswangb1379d92013-08-16 16:09:37 +08001776 netif_device_detach(netdev);
1777 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08001778 struct net_device_stats *stats = &netdev->stats;
1779 unsigned long flags;
1780
hayeswangb1379d92013-08-16 16:09:37 +08001781 netif_warn(tp, tx_err, netdev,
1782 "failed tx_urb %d\n", res);
1783 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08001784
hayeswangb1379d92013-08-16 16:09:37 +08001785 spin_lock_irqsave(&tp->tx_lock, flags);
1786 list_add_tail(&agg->list, &tp->tx_free);
1787 spin_unlock_irqrestore(&tp->tx_lock, flags);
1788 }
hayeswangebc2ec482013-08-14 20:54:38 +08001789 }
hayeswangb1379d92013-08-16 16:09:37 +08001790 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001791}
1792
hayeswangd823ab62015-01-12 12:06:23 +08001793static void bottom_half(struct r8152 *tp)
hayeswangebc2ec482013-08-14 20:54:38 +08001794{
hayeswangebc2ec482013-08-14 20:54:38 +08001795 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1796 return;
1797
1798 if (!test_bit(WORK_ENABLE, &tp->flags))
1799 return;
1800
hayeswang7559fb2f2013-08-16 16:09:38 +08001801 /* When link down, the driver would cancel all bulks. */
1802 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001803 if (!netif_carrier_ok(tp->netdev))
1804 return;
1805
hayeswangd823ab62015-01-12 12:06:23 +08001806 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08001807
hayeswang0c3121f2014-03-07 11:04:36 +08001808 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001809}
1810
hayeswangd823ab62015-01-12 12:06:23 +08001811static int r8152_poll(struct napi_struct *napi, int budget)
1812{
1813 struct r8152 *tp = container_of(napi, struct r8152, napi);
1814 int work_done;
1815
1816 work_done = rx_bottom(tp, budget);
1817 bottom_half(tp);
1818
1819 if (work_done < budget) {
1820 napi_complete(napi);
1821 if (!list_empty(&tp->rx_done))
1822 napi_schedule(napi);
1823 }
1824
1825 return work_done;
1826}
1827
hayeswangebc2ec482013-08-14 20:54:38 +08001828static
1829int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1830{
hayeswanga0fccd42014-11-20 10:29:05 +08001831 int ret;
1832
hayeswangef827a52015-01-09 10:26:36 +08001833 /* The rx would be stopped, so skip submitting */
1834 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
1835 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
1836 return 0;
1837
hayeswangebc2ec482013-08-14 20:54:38 +08001838 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
hayeswang52aec122014-09-02 10:27:52 +08001839 agg->head, agg_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08001840 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08001841
hayeswanga0fccd42014-11-20 10:29:05 +08001842 ret = usb_submit_urb(agg->urb, mem_flags);
1843 if (ret == -ENODEV) {
1844 set_bit(RTL8152_UNPLUG, &tp->flags);
1845 netif_device_detach(tp->netdev);
1846 } else if (ret) {
1847 struct urb *urb = agg->urb;
1848 unsigned long flags;
1849
1850 urb->actual_length = 0;
1851 spin_lock_irqsave(&tp->rx_lock, flags);
1852 list_add_tail(&agg->list, &tp->rx_done);
1853 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001854
1855 netif_err(tp, rx_err, tp->netdev,
1856 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
1857
1858 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08001859 }
1860
1861 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001862}
1863
hayeswang00a5e362014-02-18 21:48:59 +08001864static void rtl_drop_queued_tx(struct r8152 *tp)
1865{
1866 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001867 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08001868 struct sk_buff *skb;
1869
hayeswangd84130a2014-02-18 21:49:02 +08001870 if (skb_queue_empty(tx_queue))
1871 return;
1872
1873 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001874 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001875 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001876 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001877
1878 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001879 dev_kfree_skb(skb);
1880 stats->tx_dropped++;
1881 }
1882}
1883
hayeswangac718b62013-05-02 16:01:25 +00001884static void rtl8152_tx_timeout(struct net_device *netdev)
1885{
1886 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001887 int i;
1888
Hayes Wang4a8deae2014-01-07 11:18:22 +08001889 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001890 for (i = 0; i < RTL8152_MAX_TX; i++)
1891 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001892}
1893
1894static void rtl8152_set_rx_mode(struct net_device *netdev)
1895{
1896 struct r8152 *tp = netdev_priv(netdev);
1897
hayeswang40a82912013-08-14 20:54:40 +08001898 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001899 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001900 schedule_delayed_work(&tp->schedule, 0);
1901 }
hayeswangac718b62013-05-02 16:01:25 +00001902}
1903
1904static void _rtl8152_set_rx_mode(struct net_device *netdev)
1905{
1906 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001907 u32 mc_filter[2]; /* Multicast hash filter */
1908 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001909 u32 ocp_data;
1910
hayeswangac718b62013-05-02 16:01:25 +00001911 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1912 netif_stop_queue(netdev);
1913 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1914 ocp_data &= ~RCR_ACPT_ALL;
1915 ocp_data |= RCR_AB | RCR_APM;
1916
1917 if (netdev->flags & IFF_PROMISC) {
1918 /* Unconditionally log net taps. */
1919 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1920 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08001921 mc_filter[1] = 0xffffffff;
1922 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001923 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1924 (netdev->flags & IFF_ALLMULTI)) {
1925 /* Too many to filter perfectly -- accept all multicasts. */
1926 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08001927 mc_filter[1] = 0xffffffff;
1928 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001929 } else {
1930 struct netdev_hw_addr *ha;
1931
hayeswangb209af92014-08-25 15:53:00 +08001932 mc_filter[1] = 0;
1933 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00001934 netdev_for_each_mc_addr(ha, netdev) {
1935 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08001936
hayeswangac718b62013-05-02 16:01:25 +00001937 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1938 ocp_data |= RCR_AM;
1939 }
1940 }
1941
hayeswang31787f52013-07-31 17:21:25 +08001942 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1943 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001944
hayeswang31787f52013-07-31 17:21:25 +08001945 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001946 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1947 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001948}
1949
hayeswanga5e31252015-01-06 17:41:58 +08001950static netdev_features_t
1951rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
1952 netdev_features_t features)
1953{
1954 u32 mss = skb_shinfo(skb)->gso_size;
1955 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
1956 int offset = skb_transport_offset(skb);
1957
1958 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
1959 features &= ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
1960 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
1961 features &= ~NETIF_F_GSO_MASK;
1962
1963 return features;
1964}
1965
hayeswangac718b62013-05-02 16:01:25 +00001966static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08001967 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00001968{
1969 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001970
hayeswangac718b62013-05-02 16:01:25 +00001971 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001972
hayeswang61598782013-11-20 17:30:55 +08001973 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001974
hayeswang0c3121f2014-03-07 11:04:36 +08001975 if (!list_empty(&tp->tx_free)) {
1976 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08001977 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08001978 schedule_delayed_work(&tp->schedule, 0);
1979 } else {
1980 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08001981 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08001982 }
hayeswangb209af92014-08-25 15:53:00 +08001983 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08001984 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08001985 }
hayeswangdd1b1192013-11-20 17:30:56 +08001986
hayeswangac718b62013-05-02 16:01:25 +00001987 return NETDEV_TX_OK;
1988}
1989
1990static void r8152b_reset_packet_filter(struct r8152 *tp)
1991{
1992 u32 ocp_data;
1993
1994 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1995 ocp_data &= ~FMC_FCR_MCU_EN;
1996 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1997 ocp_data |= FMC_FCR_MCU_EN;
1998 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1999}
2000
2001static void rtl8152_nic_reset(struct r8152 *tp)
2002{
2003 int i;
2004
2005 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2006
2007 for (i = 0; i < 1000; i++) {
2008 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2009 break;
hayeswangb209af92014-08-25 15:53:00 +08002010 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002011 }
2012}
2013
hayeswangdd1b1192013-11-20 17:30:56 +08002014static void set_tx_qlen(struct r8152 *tp)
2015{
2016 struct net_device *netdev = tp->netdev;
2017
hayeswang52aec122014-09-02 10:27:52 +08002018 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
2019 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002020}
2021
hayeswangac718b62013-05-02 16:01:25 +00002022static inline u8 rtl8152_get_speed(struct r8152 *tp)
2023{
2024 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2025}
2026
hayeswang507605a2014-01-02 11:22:43 +08002027static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002028{
hayeswangebc2ec482013-08-14 20:54:38 +08002029 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002030 u8 speed;
2031
2032 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002033 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002034 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002035 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002036 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2037 } else {
2038 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002039 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002040 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2041 }
hayeswang507605a2014-01-02 11:22:43 +08002042}
2043
hayeswang00a5e362014-02-18 21:48:59 +08002044static void rxdy_gated_en(struct r8152 *tp, bool enable)
2045{
2046 u32 ocp_data;
2047
2048 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2049 if (enable)
2050 ocp_data |= RXDY_GATED_EN;
2051 else
2052 ocp_data &= ~RXDY_GATED_EN;
2053 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2054}
2055
hayeswang445f7f42014-09-23 16:31:47 +08002056static int rtl_start_rx(struct r8152 *tp)
2057{
2058 int i, ret = 0;
2059
hayeswangd823ab62015-01-12 12:06:23 +08002060 napi_disable(&tp->napi);
hayeswang445f7f42014-09-23 16:31:47 +08002061 INIT_LIST_HEAD(&tp->rx_done);
2062 for (i = 0; i < RTL8152_MAX_RX; i++) {
2063 INIT_LIST_HEAD(&tp->rx_info[i].list);
2064 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2065 if (ret)
2066 break;
2067 }
hayeswangd823ab62015-01-12 12:06:23 +08002068 napi_enable(&tp->napi);
hayeswang445f7f42014-09-23 16:31:47 +08002069
hayeswang7bcf4f62014-11-20 10:29:06 +08002070 if (ret && ++i < RTL8152_MAX_RX) {
2071 struct list_head rx_queue;
2072 unsigned long flags;
2073
2074 INIT_LIST_HEAD(&rx_queue);
2075
2076 do {
2077 struct rx_agg *agg = &tp->rx_info[i++];
2078 struct urb *urb = agg->urb;
2079
2080 urb->actual_length = 0;
2081 list_add_tail(&agg->list, &rx_queue);
2082 } while (i < RTL8152_MAX_RX);
2083
2084 spin_lock_irqsave(&tp->rx_lock, flags);
2085 list_splice_tail(&rx_queue, &tp->rx_done);
2086 spin_unlock_irqrestore(&tp->rx_lock, flags);
2087 }
2088
hayeswang445f7f42014-09-23 16:31:47 +08002089 return ret;
2090}
2091
2092static int rtl_stop_rx(struct r8152 *tp)
2093{
2094 int i;
2095
2096 for (i = 0; i < RTL8152_MAX_RX; i++)
2097 usb_kill_urb(tp->rx_info[i].urb);
2098
hayeswangd823ab62015-01-12 12:06:23 +08002099 while (!skb_queue_empty(&tp->rx_queue))
2100 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2101
hayeswang445f7f42014-09-23 16:31:47 +08002102 return 0;
2103}
2104
hayeswang507605a2014-01-02 11:22:43 +08002105static int rtl_enable(struct r8152 *tp)
2106{
2107 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002108
2109 r8152b_reset_packet_filter(tp);
2110
2111 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2112 ocp_data |= CR_RE | CR_TE;
2113 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2114
hayeswang00a5e362014-02-18 21:48:59 +08002115 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002116
hayeswangaa2e0922015-01-09 10:26:35 +08002117 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002118}
2119
hayeswang507605a2014-01-02 11:22:43 +08002120static int rtl8152_enable(struct r8152 *tp)
2121{
hayeswang68714382014-04-11 17:54:31 +08002122 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2123 return -ENODEV;
2124
hayeswang507605a2014-01-02 11:22:43 +08002125 set_tx_qlen(tp);
2126 rtl_set_eee_plus(tp);
2127
2128 return rtl_enable(tp);
2129}
2130
hayeswang43779f82014-01-02 11:25:10 +08002131static void r8153_set_rx_agg(struct r8152 *tp)
2132{
2133 u8 speed;
2134
2135 speed = rtl8152_get_speed(tp);
2136 if (speed & _1000bps) {
2137 if (tp->udev->speed == USB_SPEED_SUPER) {
2138 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
2139 RX_THR_SUPPER);
2140 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2141 EARLY_AGG_SUPPER);
2142 } else {
2143 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
2144 RX_THR_HIGH);
2145 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2146 EARLY_AGG_HIGH);
2147 }
2148 } else {
2149 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
2150 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2151 EARLY_AGG_SLOW);
2152 }
2153}
2154
2155static int rtl8153_enable(struct r8152 *tp)
2156{
hayeswang68714382014-04-11 17:54:31 +08002157 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2158 return -ENODEV;
2159
hayeswang43779f82014-01-02 11:25:10 +08002160 set_tx_qlen(tp);
2161 rtl_set_eee_plus(tp);
2162 r8153_set_rx_agg(tp);
2163
2164 return rtl_enable(tp);
2165}
2166
hayeswangd70b1132014-09-19 15:17:18 +08002167static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002168{
hayeswangebc2ec482013-08-14 20:54:38 +08002169 u32 ocp_data;
2170 int i;
hayeswangac718b62013-05-02 16:01:25 +00002171
hayeswang68714382014-04-11 17:54:31 +08002172 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2173 rtl_drop_queued_tx(tp);
2174 return;
2175 }
2176
hayeswangac718b62013-05-02 16:01:25 +00002177 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2178 ocp_data &= ~RCR_ACPT_ALL;
2179 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2180
hayeswang00a5e362014-02-18 21:48:59 +08002181 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002182
2183 for (i = 0; i < RTL8152_MAX_TX; i++)
2184 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002185
hayeswang00a5e362014-02-18 21:48:59 +08002186 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002187
2188 for (i = 0; i < 1000; i++) {
2189 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2190 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2191 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002192 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002193 }
2194
2195 for (i = 0; i < 1000; i++) {
2196 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2197 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002198 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002199 }
2200
hayeswang445f7f42014-09-23 16:31:47 +08002201 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002202
2203 rtl8152_nic_reset(tp);
2204}
2205
hayeswang00a5e362014-02-18 21:48:59 +08002206static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2207{
2208 u32 ocp_data;
2209
2210 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2211 if (enable)
2212 ocp_data |= POWER_CUT;
2213 else
2214 ocp_data &= ~POWER_CUT;
2215 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2216
2217 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2218 ocp_data &= ~RESUME_INDICATE;
2219 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002220}
2221
hayeswangc5554292014-09-12 10:43:11 +08002222static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2223{
2224 u32 ocp_data;
2225
2226 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2227 if (enable)
2228 ocp_data |= CPCR_RX_VLAN;
2229 else
2230 ocp_data &= ~CPCR_RX_VLAN;
2231 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2232}
2233
2234static int rtl8152_set_features(struct net_device *dev,
2235 netdev_features_t features)
2236{
2237 netdev_features_t changed = features ^ dev->features;
2238 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002239 int ret;
2240
2241 ret = usb_autopm_get_interface(tp->intf);
2242 if (ret < 0)
2243 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002244
hayeswangb5403272014-10-09 18:00:26 +08002245 mutex_lock(&tp->control);
2246
hayeswangc5554292014-09-12 10:43:11 +08002247 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2248 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2249 rtl_rx_vlan_en(tp, true);
2250 else
2251 rtl_rx_vlan_en(tp, false);
2252 }
2253
hayeswangb5403272014-10-09 18:00:26 +08002254 mutex_unlock(&tp->control);
2255
hayeswang405f8a02014-10-09 18:00:24 +08002256 usb_autopm_put_interface(tp->intf);
2257
2258out:
2259 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002260}
2261
hayeswang21ff2e82014-02-18 21:49:06 +08002262#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2263
2264static u32 __rtl_get_wol(struct r8152 *tp)
2265{
2266 u32 ocp_data;
2267 u32 wolopts = 0;
2268
2269 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2270 if (!(ocp_data & LAN_WAKE_EN))
2271 return 0;
2272
2273 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2274 if (ocp_data & LINK_ON_WAKE_EN)
2275 wolopts |= WAKE_PHY;
2276
2277 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2278 if (ocp_data & UWF_EN)
2279 wolopts |= WAKE_UCAST;
2280 if (ocp_data & BWF_EN)
2281 wolopts |= WAKE_BCAST;
2282 if (ocp_data & MWF_EN)
2283 wolopts |= WAKE_MCAST;
2284
2285 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2286 if (ocp_data & MAGIC_EN)
2287 wolopts |= WAKE_MAGIC;
2288
2289 return wolopts;
2290}
2291
2292static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2293{
2294 u32 ocp_data;
2295
2296 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2297
2298 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2299 ocp_data &= ~LINK_ON_WAKE_EN;
2300 if (wolopts & WAKE_PHY)
2301 ocp_data |= LINK_ON_WAKE_EN;
2302 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2303
2304 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2305 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
2306 if (wolopts & WAKE_UCAST)
2307 ocp_data |= UWF_EN;
2308 if (wolopts & WAKE_BCAST)
2309 ocp_data |= BWF_EN;
2310 if (wolopts & WAKE_MCAST)
2311 ocp_data |= MWF_EN;
2312 if (wolopts & WAKE_ANY)
2313 ocp_data |= LAN_WAKE_EN;
2314 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2315
2316 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2317
2318 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2319 ocp_data &= ~MAGIC_EN;
2320 if (wolopts & WAKE_MAGIC)
2321 ocp_data |= MAGIC_EN;
2322 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2323
2324 if (wolopts & WAKE_ANY)
2325 device_set_wakeup_enable(&tp->udev->dev, true);
2326 else
2327 device_set_wakeup_enable(&tp->udev->dev, false);
2328}
2329
hayeswang9a4be1b2014-02-18 21:49:07 +08002330static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2331{
2332 if (enable) {
2333 u32 ocp_data;
2334
2335 __rtl_set_wol(tp, WAKE_ANY);
2336
2337 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2338
2339 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2340 ocp_data |= LINK_OFF_WAKE_EN;
2341 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2342
2343 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2344 } else {
2345 __rtl_set_wol(tp, tp->saved_wolopts);
2346 }
2347}
2348
hayeswangaa66a5f2014-02-18 21:49:04 +08002349static void rtl_phy_reset(struct r8152 *tp)
2350{
2351 u16 data;
2352 int i;
2353
2354 clear_bit(PHY_RESET, &tp->flags);
2355
2356 data = r8152_mdio_read(tp, MII_BMCR);
2357
2358 /* don't reset again before the previous one complete */
2359 if (data & BMCR_RESET)
2360 return;
2361
2362 data |= BMCR_RESET;
2363 r8152_mdio_write(tp, MII_BMCR, data);
2364
2365 for (i = 0; i < 50; i++) {
2366 msleep(20);
2367 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2368 break;
2369 }
2370}
2371
hayeswang43499682014-02-18 21:48:58 +08002372static void r8153_teredo_off(struct r8152 *tp)
2373{
2374 u32 ocp_data;
2375
2376 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2377 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
2378 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2379
2380 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2381 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2382 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2383}
2384
2385static void r8152b_disable_aldps(struct r8152 *tp)
2386{
2387 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
2388 msleep(20);
2389}
2390
2391static inline void r8152b_enable_aldps(struct r8152 *tp)
2392{
2393 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2394 LINKENA | DIS_SDSAVE);
2395}
2396
hayeswangd70b1132014-09-19 15:17:18 +08002397static void rtl8152_disable(struct r8152 *tp)
2398{
2399 r8152b_disable_aldps(tp);
2400 rtl_disable(tp);
2401 r8152b_enable_aldps(tp);
2402}
2403
hayeswang43499682014-02-18 21:48:58 +08002404static void r8152b_hw_phy_cfg(struct r8152 *tp)
2405{
hayeswangf0cbe0a2014-02-18 21:49:03 +08002406 u16 data;
2407
2408 data = r8152_mdio_read(tp, MII_BMCR);
2409 if (data & BMCR_PDOWN) {
2410 data &= ~BMCR_PDOWN;
2411 r8152_mdio_write(tp, MII_BMCR, data);
2412 }
2413
hayeswangaa66a5f2014-02-18 21:49:04 +08002414 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002415}
2416
hayeswangac718b62013-05-02 16:01:25 +00002417static void r8152b_exit_oob(struct r8152 *tp)
2418{
hayeswangdb8515e2014-03-06 15:07:16 +08002419 u32 ocp_data;
2420 int i;
hayeswangac718b62013-05-02 16:01:25 +00002421
2422 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2423 ocp_data &= ~RCR_ACPT_ALL;
2424 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2425
hayeswang00a5e362014-02-18 21:48:59 +08002426 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002427 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002428 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002429
2430 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2431 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2432
2433 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2434 ocp_data &= ~NOW_IS_OOB;
2435 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2436
2437 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2438 ocp_data &= ~MCU_BORW_EN;
2439 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2440
2441 for (i = 0; i < 1000; i++) {
2442 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2443 if (ocp_data & LINK_LIST_READY)
2444 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002445 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002446 }
2447
2448 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2449 ocp_data |= RE_INIT_LL;
2450 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2451
2452 for (i = 0; i < 1000; i++) {
2453 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2454 if (ocp_data & LINK_LIST_READY)
2455 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002456 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002457 }
2458
2459 rtl8152_nic_reset(tp);
2460
2461 /* rx share fifo credit full threshold */
2462 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2463
hayeswanga3cc4652014-07-24 16:37:43 +08002464 if (tp->udev->speed == USB_SPEED_FULL ||
2465 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00002466 /* rx share fifo credit near full threshold */
2467 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2468 RXFIFO_THR2_FULL);
2469 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2470 RXFIFO_THR3_FULL);
2471 } else {
2472 /* rx share fifo credit near full threshold */
2473 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2474 RXFIFO_THR2_HIGH);
2475 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2476 RXFIFO_THR3_HIGH);
2477 }
2478
2479 /* TX share fifo free credit full threshold */
2480 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2481
2482 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002483 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002484 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2485 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2486
hayeswangc5554292014-09-12 10:43:11 +08002487 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00002488
2489 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2490
2491 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2492 ocp_data |= TCR0_AUTO_FIFO;
2493 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2494}
2495
2496static void r8152b_enter_oob(struct r8152 *tp)
2497{
hayeswang45f4a192014-01-06 17:08:41 +08002498 u32 ocp_data;
2499 int i;
hayeswangac718b62013-05-02 16:01:25 +00002500
2501 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2502 ocp_data &= ~NOW_IS_OOB;
2503 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2504
2505 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2506 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2507 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2508
hayeswangd70b1132014-09-19 15:17:18 +08002509 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002510
2511 for (i = 0; i < 1000; i++) {
2512 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2513 if (ocp_data & LINK_LIST_READY)
2514 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002515 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002516 }
2517
2518 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2519 ocp_data |= RE_INIT_LL;
2520 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2521
2522 for (i = 0; i < 1000; i++) {
2523 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2524 if (ocp_data & LINK_LIST_READY)
2525 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002526 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002527 }
2528
2529 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2530
hayeswangc5554292014-09-12 10:43:11 +08002531 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002532
2533 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2534 ocp_data |= ALDPS_PROXY_MODE;
2535 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2536
2537 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2538 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2539 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2540
hayeswang00a5e362014-02-18 21:48:59 +08002541 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002542
2543 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2544 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2545 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2546}
2547
hayeswang43779f82014-01-02 11:25:10 +08002548static void r8153_hw_phy_cfg(struct r8152 *tp)
2549{
2550 u32 ocp_data;
2551 u16 data;
2552
2553 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002554 data = r8152_mdio_read(tp, MII_BMCR);
2555 if (data & BMCR_PDOWN) {
2556 data &= ~BMCR_PDOWN;
2557 r8152_mdio_write(tp, MII_BMCR, data);
2558 }
hayeswang43779f82014-01-02 11:25:10 +08002559
2560 if (tp->version == RTL_VER_03) {
2561 data = ocp_reg_read(tp, OCP_EEE_CFG);
2562 data &= ~CTAP_SHORT_EN;
2563 ocp_reg_write(tp, OCP_EEE_CFG, data);
2564 }
2565
2566 data = ocp_reg_read(tp, OCP_POWER_CFG);
2567 data |= EEE_CLKDIV_EN;
2568 ocp_reg_write(tp, OCP_POWER_CFG, data);
2569
2570 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2571 data |= EN_10M_BGOFF;
2572 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2573 data = ocp_reg_read(tp, OCP_POWER_CFG);
2574 data |= EN_10M_PLLOFF;
2575 ocp_reg_write(tp, OCP_POWER_CFG, data);
2576 data = sram_read(tp, SRAM_IMPEDANCE);
2577 data &= ~RX_DRIVING_MASK;
2578 sram_write(tp, SRAM_IMPEDANCE, data);
2579
2580 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2581 ocp_data |= PFM_PWM_SWITCH;
2582 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2583
2584 data = sram_read(tp, SRAM_LPF_CFG);
2585 data |= LPF_AUTO_TUNE;
2586 sram_write(tp, SRAM_LPF_CFG, data);
2587
2588 data = sram_read(tp, SRAM_10M_AMP1);
2589 data |= GDAC_IB_UPALL;
2590 sram_write(tp, SRAM_10M_AMP1, data);
2591 data = sram_read(tp, SRAM_10M_AMP2);
2592 data |= AMP_DN;
2593 sram_write(tp, SRAM_10M_AMP2, data);
hayeswangaa66a5f2014-02-18 21:49:04 +08002594
2595 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002596}
2597
hayeswangb9702722014-02-18 21:49:00 +08002598static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002599{
2600 u8 u1u2[8];
2601
2602 if (enable)
2603 memset(u1u2, 0xff, sizeof(u1u2));
2604 else
2605 memset(u1u2, 0x00, sizeof(u1u2));
2606
2607 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2608}
2609
hayeswangb9702722014-02-18 21:49:00 +08002610static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002611{
2612 u32 ocp_data;
2613
2614 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2615 if (enable)
2616 ocp_data |= U2P3_ENABLE;
2617 else
2618 ocp_data &= ~U2P3_ENABLE;
2619 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2620}
2621
hayeswangb9702722014-02-18 21:49:00 +08002622static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002623{
2624 u32 ocp_data;
2625
2626 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2627 if (enable)
2628 ocp_data |= PWR_EN | PHASE2_EN;
2629 else
2630 ocp_data &= ~(PWR_EN | PHASE2_EN);
2631 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2632
2633 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2634 ocp_data &= ~PCUT_STATUS;
2635 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2636}
2637
hayeswang43779f82014-01-02 11:25:10 +08002638static void r8153_first_init(struct r8152 *tp)
2639{
2640 u32 ocp_data;
2641 int i;
2642
hayeswang00a5e362014-02-18 21:48:59 +08002643 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002644 r8153_teredo_off(tp);
2645
2646 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2647 ocp_data &= ~RCR_ACPT_ALL;
2648 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2649
2650 r8153_hw_phy_cfg(tp);
2651
2652 rtl8152_nic_reset(tp);
2653
2654 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2655 ocp_data &= ~NOW_IS_OOB;
2656 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2657
2658 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2659 ocp_data &= ~MCU_BORW_EN;
2660 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2661
2662 for (i = 0; i < 1000; i++) {
2663 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2664 if (ocp_data & LINK_LIST_READY)
2665 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002666 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002667 }
2668
2669 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2670 ocp_data |= RE_INIT_LL;
2671 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2672
2673 for (i = 0; i < 1000; i++) {
2674 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2675 if (ocp_data & LINK_LIST_READY)
2676 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002677 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002678 }
2679
hayeswangc5554292014-09-12 10:43:11 +08002680 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08002681
hayeswang69b4b7a2014-07-10 10:58:54 +08002682 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
2683 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08002684
2685 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2686 ocp_data |= TCR0_AUTO_FIFO;
2687 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2688
2689 rtl8152_nic_reset(tp);
2690
2691 /* rx share fifo credit full threshold */
2692 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2693 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2694 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2695 /* TX share fifo free credit full threshold */
2696 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2697
hayeswang9629e3c2014-01-15 10:42:15 +08002698 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002699 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2700 ocp_data &= ~RX_AGG_DISABLE;
2701 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2702}
2703
2704static void r8153_enter_oob(struct r8152 *tp)
2705{
2706 u32 ocp_data;
2707 int i;
2708
2709 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2710 ocp_data &= ~NOW_IS_OOB;
2711 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2712
hayeswangd70b1132014-09-19 15:17:18 +08002713 rtl_disable(tp);
hayeswang43779f82014-01-02 11:25:10 +08002714
2715 for (i = 0; i < 1000; i++) {
2716 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2717 if (ocp_data & LINK_LIST_READY)
2718 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002719 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002720 }
2721
2722 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2723 ocp_data |= RE_INIT_LL;
2724 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2725
2726 for (i = 0; i < 1000; i++) {
2727 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2728 if (ocp_data & LINK_LIST_READY)
2729 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002730 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002731 }
2732
hayeswang69b4b7a2014-07-10 10:58:54 +08002733 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
hayeswang43779f82014-01-02 11:25:10 +08002734
hayeswang43779f82014-01-02 11:25:10 +08002735 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2736 ocp_data &= ~TEREDO_WAKE_MASK;
2737 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2738
hayeswangc5554292014-09-12 10:43:11 +08002739 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002740
2741 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2742 ocp_data |= ALDPS_PROXY_MODE;
2743 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2744
2745 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2746 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2747 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2748
hayeswang00a5e362014-02-18 21:48:59 +08002749 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002750
2751 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2752 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2753 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2754}
2755
2756static void r8153_disable_aldps(struct r8152 *tp)
2757{
2758 u16 data;
2759
2760 data = ocp_reg_read(tp, OCP_POWER_CFG);
2761 data &= ~EN_ALDPS;
2762 ocp_reg_write(tp, OCP_POWER_CFG, data);
2763 msleep(20);
2764}
2765
2766static void r8153_enable_aldps(struct r8152 *tp)
2767{
2768 u16 data;
2769
2770 data = ocp_reg_read(tp, OCP_POWER_CFG);
2771 data |= EN_ALDPS;
2772 ocp_reg_write(tp, OCP_POWER_CFG, data);
2773}
2774
hayeswangd70b1132014-09-19 15:17:18 +08002775static void rtl8153_disable(struct r8152 *tp)
2776{
2777 r8153_disable_aldps(tp);
2778 rtl_disable(tp);
2779 r8153_enable_aldps(tp);
2780}
2781
hayeswangac718b62013-05-02 16:01:25 +00002782static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2783{
hayeswang43779f82014-01-02 11:25:10 +08002784 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002785 int ret = 0;
2786
2787 cancel_delayed_work_sync(&tp->schedule);
2788 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2789 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2790 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002791 if (tp->mii.supports_gmii) {
2792 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2793 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2794 } else {
2795 gbcr = 0;
2796 }
hayeswangac718b62013-05-02 16:01:25 +00002797
2798 if (autoneg == AUTONEG_DISABLE) {
2799 if (speed == SPEED_10) {
2800 bmcr = 0;
2801 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2802 } else if (speed == SPEED_100) {
2803 bmcr = BMCR_SPEED100;
2804 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002805 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2806 bmcr = BMCR_SPEED1000;
2807 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002808 } else {
2809 ret = -EINVAL;
2810 goto out;
2811 }
2812
2813 if (duplex == DUPLEX_FULL)
2814 bmcr |= BMCR_FULLDPLX;
2815 } else {
2816 if (speed == SPEED_10) {
2817 if (duplex == DUPLEX_FULL)
2818 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2819 else
2820 anar |= ADVERTISE_10HALF;
2821 } else if (speed == SPEED_100) {
2822 if (duplex == DUPLEX_FULL) {
2823 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2824 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2825 } else {
2826 anar |= ADVERTISE_10HALF;
2827 anar |= ADVERTISE_100HALF;
2828 }
hayeswang43779f82014-01-02 11:25:10 +08002829 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2830 if (duplex == DUPLEX_FULL) {
2831 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2832 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2833 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2834 } else {
2835 anar |= ADVERTISE_10HALF;
2836 anar |= ADVERTISE_100HALF;
2837 gbcr |= ADVERTISE_1000HALF;
2838 }
hayeswangac718b62013-05-02 16:01:25 +00002839 } else {
2840 ret = -EINVAL;
2841 goto out;
2842 }
2843
2844 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2845 }
2846
hayeswangaa66a5f2014-02-18 21:49:04 +08002847 if (test_bit(PHY_RESET, &tp->flags))
2848 bmcr |= BMCR_RESET;
2849
hayeswang43779f82014-01-02 11:25:10 +08002850 if (tp->mii.supports_gmii)
2851 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2852
hayeswangac718b62013-05-02 16:01:25 +00002853 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2854 r8152_mdio_write(tp, MII_BMCR, bmcr);
2855
hayeswangaa66a5f2014-02-18 21:49:04 +08002856 if (test_bit(PHY_RESET, &tp->flags)) {
2857 int i;
2858
2859 clear_bit(PHY_RESET, &tp->flags);
2860 for (i = 0; i < 50; i++) {
2861 msleep(20);
2862 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2863 break;
2864 }
2865 }
2866
hayeswangac718b62013-05-02 16:01:25 +00002867out:
hayeswangac718b62013-05-02 16:01:25 +00002868
2869 return ret;
2870}
2871
hayeswangd70b1132014-09-19 15:17:18 +08002872static void rtl8152_up(struct r8152 *tp)
2873{
2874 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2875 return;
2876
2877 r8152b_disable_aldps(tp);
2878 r8152b_exit_oob(tp);
2879 r8152b_enable_aldps(tp);
2880}
2881
hayeswangac718b62013-05-02 16:01:25 +00002882static void rtl8152_down(struct r8152 *tp)
2883{
hayeswang68714382014-04-11 17:54:31 +08002884 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2885 rtl_drop_queued_tx(tp);
2886 return;
2887 }
2888
hayeswang00a5e362014-02-18 21:48:59 +08002889 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002890 r8152b_disable_aldps(tp);
2891 r8152b_enter_oob(tp);
2892 r8152b_enable_aldps(tp);
2893}
2894
hayeswangd70b1132014-09-19 15:17:18 +08002895static void rtl8153_up(struct r8152 *tp)
2896{
2897 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2898 return;
2899
2900 r8153_disable_aldps(tp);
2901 r8153_first_init(tp);
2902 r8153_enable_aldps(tp);
2903}
2904
hayeswang43779f82014-01-02 11:25:10 +08002905static void rtl8153_down(struct r8152 *tp)
2906{
hayeswang68714382014-04-11 17:54:31 +08002907 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2908 rtl_drop_queued_tx(tp);
2909 return;
2910 }
2911
hayeswangb9702722014-02-18 21:49:00 +08002912 r8153_u1u2en(tp, false);
2913 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002914 r8153_disable_aldps(tp);
2915 r8153_enter_oob(tp);
2916 r8153_enable_aldps(tp);
2917}
2918
hayeswangac718b62013-05-02 16:01:25 +00002919static void set_carrier(struct r8152 *tp)
2920{
2921 struct net_device *netdev = tp->netdev;
2922 u8 speed;
2923
hayeswang40a82912013-08-14 20:54:40 +08002924 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002925 speed = rtl8152_get_speed(tp);
2926
2927 if (speed & LINK_STATUS) {
2928 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002929 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002930 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2931 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08002932 rtl_start_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002933 }
2934 } else {
2935 if (tp->speed & LINK_STATUS) {
2936 netif_carrier_off(netdev);
hayeswangd823ab62015-01-12 12:06:23 +08002937 napi_disable(&tp->napi);
hayeswangc81229c2014-01-02 11:22:42 +08002938 tp->rtl_ops.disable(tp);
hayeswangd823ab62015-01-12 12:06:23 +08002939 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00002940 }
2941 }
2942 tp->speed = speed;
2943}
2944
2945static void rtl_work_func_t(struct work_struct *work)
2946{
2947 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2948
hayeswanga1f83fe2014-11-12 10:05:05 +08002949 /* If the device is unplugged or !netif_running(), the workqueue
2950 * doesn't need to wake the device, and could return directly.
2951 */
2952 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
2953 return;
2954
hayeswang9a4be1b2014-02-18 21:49:07 +08002955 if (usb_autopm_get_interface(tp->intf) < 0)
2956 return;
2957
hayeswangac718b62013-05-02 16:01:25 +00002958 if (!test_bit(WORK_ENABLE, &tp->flags))
2959 goto out1;
2960
hayeswangb5403272014-10-09 18:00:26 +08002961 if (!mutex_trylock(&tp->control)) {
2962 schedule_delayed_work(&tp->schedule, 0);
2963 goto out1;
2964 }
2965
hayeswang40a82912013-08-14 20:54:40 +08002966 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2967 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002968
2969 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2970 _rtl8152_set_rx_mode(tp->netdev);
2971
hayeswangd823ab62015-01-12 12:06:23 +08002972 /* don't schedule napi before linking */
2973 if (test_bit(SCHEDULE_NAPI, &tp->flags) &&
hayeswang0c3121f2014-03-07 11:04:36 +08002974 (tp->speed & LINK_STATUS)) {
hayeswangd823ab62015-01-12 12:06:23 +08002975 clear_bit(SCHEDULE_NAPI, &tp->flags);
2976 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002977 }
hayeswangaa66a5f2014-02-18 21:49:04 +08002978
2979 if (test_bit(PHY_RESET, &tp->flags))
2980 rtl_phy_reset(tp);
2981
hayeswangb5403272014-10-09 18:00:26 +08002982 mutex_unlock(&tp->control);
2983
hayeswangac718b62013-05-02 16:01:25 +00002984out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08002985 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002986}
2987
2988static int rtl8152_open(struct net_device *netdev)
2989{
2990 struct r8152 *tp = netdev_priv(netdev);
2991 int res = 0;
2992
hayeswang7e9da482014-02-18 21:49:05 +08002993 res = alloc_all_mem(tp);
2994 if (res)
2995 goto out;
2996
hayeswangf4c74762014-10-29 11:12:16 +08002997 /* set speed to 0 to avoid autoresume try to submit rx */
2998 tp->speed = 0;
2999
hayeswang9a4be1b2014-02-18 21:49:07 +08003000 res = usb_autopm_get_interface(tp->intf);
3001 if (res < 0) {
3002 free_all_mem(tp);
3003 goto out;
3004 }
3005
hayeswangb5403272014-10-09 18:00:26 +08003006 mutex_lock(&tp->control);
3007
hayeswang9a4be1b2014-02-18 21:49:07 +08003008 /* The WORK_ENABLE may be set when autoresume occurs */
3009 if (test_bit(WORK_ENABLE, &tp->flags)) {
3010 clear_bit(WORK_ENABLE, &tp->flags);
3011 usb_kill_urb(tp->intr_urb);
3012 cancel_delayed_work_sync(&tp->schedule);
hayeswangf4c74762014-10-29 11:12:16 +08003013
3014 /* disable the tx/rx, if the workqueue has enabled them. */
hayeswang9a4be1b2014-02-18 21:49:07 +08003015 if (tp->speed & LINK_STATUS)
3016 tp->rtl_ops.disable(tp);
3017 }
3018
hayeswang7e9da482014-02-18 21:49:05 +08003019 tp->rtl_ops.up(tp);
3020
hayeswang43779f82014-01-02 11:25:10 +08003021 rtl8152_set_speed(tp, AUTONEG_ENABLE,
3022 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
3023 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08003024 tp->speed = 0;
3025 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003026 netif_start_queue(netdev);
3027 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08003028
hayeswang3d55f442014-02-06 11:55:48 +08003029 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3030 if (res) {
3031 if (res == -ENODEV)
3032 netif_device_detach(tp->netdev);
3033 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3034 res);
hayeswang7e9da482014-02-18 21:49:05 +08003035 free_all_mem(tp);
hayeswang93ffbea2014-11-05 10:17:02 +08003036 } else {
hayeswangd823ab62015-01-12 12:06:23 +08003037 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08003038 }
3039
hayeswangb5403272014-10-09 18:00:26 +08003040 mutex_unlock(&tp->control);
3041
hayeswang9a4be1b2014-02-18 21:49:07 +08003042 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003043
hayeswang7e9da482014-02-18 21:49:05 +08003044out:
hayeswangac718b62013-05-02 16:01:25 +00003045 return res;
3046}
3047
3048static int rtl8152_close(struct net_device *netdev)
3049{
3050 struct r8152 *tp = netdev_priv(netdev);
3051 int res = 0;
3052
hayeswangd823ab62015-01-12 12:06:23 +08003053 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003054 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08003055 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00003056 cancel_delayed_work_sync(&tp->schedule);
3057 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003058
3059 res = usb_autopm_get_interface(tp->intf);
3060 if (res < 0) {
3061 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08003062 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003063 } else {
hayeswangb5403272014-10-09 18:00:26 +08003064 mutex_lock(&tp->control);
3065
hayeswangb209af92014-08-25 15:53:00 +08003066 /* The autosuspend may have been enabled and wouldn't
hayeswang9a4be1b2014-02-18 21:49:07 +08003067 * be disable when autoresume occurs, because the
3068 * netif_running() would be false.
3069 */
hayeswang923e1ee2014-10-29 11:12:15 +08003070 rtl_runtime_suspend_enable(tp, false);
hayeswang9a4be1b2014-02-18 21:49:07 +08003071
hayeswang9a4be1b2014-02-18 21:49:07 +08003072 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08003073
3074 mutex_unlock(&tp->control);
3075
hayeswang9a4be1b2014-02-18 21:49:07 +08003076 usb_autopm_put_interface(tp->intf);
3077 }
hayeswangac718b62013-05-02 16:01:25 +00003078
hayeswang7e9da482014-02-18 21:49:05 +08003079 free_all_mem(tp);
3080
hayeswangac718b62013-05-02 16:01:25 +00003081 return res;
3082}
3083
hayeswangd24f6132014-09-25 20:54:01 +08003084static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
hayeswangac718b62013-05-02 16:01:25 +00003085{
hayeswangd24f6132014-09-25 20:54:01 +08003086 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3087 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3088 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3089}
3090
3091static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3092{
3093 u16 data;
3094
3095 r8152_mmd_indirect(tp, dev, reg);
3096 data = ocp_reg_read(tp, OCP_EEE_DATA);
3097 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3098
3099 return data;
3100}
3101
3102static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3103{
3104 r8152_mmd_indirect(tp, dev, reg);
3105 ocp_reg_write(tp, OCP_EEE_DATA, data);
3106 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3107}
3108
3109static void r8152_eee_en(struct r8152 *tp, bool enable)
3110{
3111 u16 config1, config2, config3;
hayeswang45f4a192014-01-06 17:08:41 +08003112 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003113
3114 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
hayeswangd24f6132014-09-25 20:54:01 +08003115 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3116 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3117 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3118
3119 if (enable) {
3120 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3121 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3122 config1 |= sd_rise_time(1);
3123 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3124 config3 |= fast_snr(42);
3125 } else {
3126 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3127 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3128 RX_QUIET_EN);
3129 config1 |= sd_rise_time(7);
3130 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3131 config3 |= fast_snr(511);
3132 }
3133
hayeswangac718b62013-05-02 16:01:25 +00003134 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
hayeswangd24f6132014-09-25 20:54:01 +08003135 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3136 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3137 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3138}
3139
3140static void r8152b_enable_eee(struct r8152 *tp)
3141{
3142 r8152_eee_en(tp, true);
3143 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3144}
3145
3146static void r8153_eee_en(struct r8152 *tp, bool enable)
3147{
3148 u32 ocp_data;
3149 u16 config;
3150
3151 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3152 config = ocp_reg_read(tp, OCP_EEE_CFG);
3153
3154 if (enable) {
3155 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3156 config |= EEE10_EN;
3157 } else {
3158 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3159 config &= ~EEE10_EN;
3160 }
3161
3162 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3163 ocp_reg_write(tp, OCP_EEE_CFG, config);
hayeswangac718b62013-05-02 16:01:25 +00003164}
3165
hayeswang43779f82014-01-02 11:25:10 +08003166static void r8153_enable_eee(struct r8152 *tp)
3167{
hayeswangd24f6132014-09-25 20:54:01 +08003168 r8153_eee_en(tp, true);
3169 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
hayeswang43779f82014-01-02 11:25:10 +08003170}
3171
hayeswangac718b62013-05-02 16:01:25 +00003172static void r8152b_enable_fc(struct r8152 *tp)
3173{
3174 u16 anar;
3175
3176 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3177 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3178 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3179}
3180
hayeswang4f1d4d52014-03-11 16:24:19 +08003181static void rtl_tally_reset(struct r8152 *tp)
3182{
3183 u32 ocp_data;
3184
3185 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
3186 ocp_data |= TALLY_RESET;
3187 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
3188}
3189
hayeswangac718b62013-05-02 16:01:25 +00003190static void r8152b_init(struct r8152 *tp)
3191{
hayeswangebc2ec482013-08-14 20:54:38 +08003192 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003193
hayeswang68714382014-04-11 17:54:31 +08003194 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3195 return;
3196
hayeswangd70b1132014-09-19 15:17:18 +08003197 r8152b_disable_aldps(tp);
3198
hayeswangac718b62013-05-02 16:01:25 +00003199 if (tp->version == RTL_VER_01) {
3200 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3201 ocp_data &= ~LED_MODE_MASK;
3202 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3203 }
3204
hayeswang00a5e362014-02-18 21:48:59 +08003205 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003206
hayeswangac718b62013-05-02 16:01:25 +00003207 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3208 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
3209 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3210 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
3211 ocp_data &= ~MCU_CLK_RATIO_MASK;
3212 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
3213 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
3214 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
3215 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
3216 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
3217
3218 r8152b_enable_eee(tp);
3219 r8152b_enable_aldps(tp);
3220 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003221 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00003222
hayeswangebc2ec482013-08-14 20:54:38 +08003223 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00003224 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08003225 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00003226 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
3227}
3228
hayeswang43779f82014-01-02 11:25:10 +08003229static void r8153_init(struct r8152 *tp)
3230{
3231 u32 ocp_data;
3232 int i;
3233
hayeswang68714382014-04-11 17:54:31 +08003234 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3235 return;
3236
hayeswangd70b1132014-09-19 15:17:18 +08003237 r8153_disable_aldps(tp);
hayeswangb9702722014-02-18 21:49:00 +08003238 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003239
3240 for (i = 0; i < 500; i++) {
3241 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3242 AUTOLOAD_DONE)
3243 break;
3244 msleep(20);
3245 }
3246
3247 for (i = 0; i < 500; i++) {
3248 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
3249 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
3250 break;
3251 msleep(20);
3252 }
3253
hayeswangb9702722014-02-18 21:49:00 +08003254 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003255
3256 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
3257 ocp_data &= ~TIMER11_EN;
3258 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
3259
hayeswang43779f82014-01-02 11:25:10 +08003260 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3261 ocp_data &= ~LED_MODE_MASK;
3262 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3263
3264 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
3265 ocp_data &= ~LPM_TIMER_MASK;
3266 if (tp->udev->speed == USB_SPEED_SUPER)
3267 ocp_data |= LPM_TIMER_500US;
3268 else
3269 ocp_data |= LPM_TIMER_500MS;
3270 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
3271
3272 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
3273 ocp_data &= ~SEN_VAL_MASK;
3274 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
3275 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
3276
hayeswangb9702722014-02-18 21:49:00 +08003277 r8153_power_cut_en(tp, false);
3278 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003279
hayeswang43779f82014-01-02 11:25:10 +08003280 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
3281 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
3282 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3283 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3284 U1U2_SPDWN_EN | L1_SPDWN_EN);
3285 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3286 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3287 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
3288 EEE_SPDWN_EN);
3289
3290 r8153_enable_eee(tp);
3291 r8153_enable_aldps(tp);
3292 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003293 rtl_tally_reset(tp);
hayeswang43779f82014-01-02 11:25:10 +08003294}
3295
hayeswangac718b62013-05-02 16:01:25 +00003296static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
3297{
3298 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang6cc69f22014-10-17 16:55:08 +08003299 struct net_device *netdev = tp->netdev;
3300 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00003301
hayeswangb5403272014-10-09 18:00:26 +08003302 mutex_lock(&tp->control);
3303
hayeswang6cc69f22014-10-17 16:55:08 +08003304 if (PMSG_IS_AUTO(message)) {
3305 if (netif_running(netdev) && work_busy(&tp->schedule.work)) {
3306 ret = -EBUSY;
3307 goto out1;
3308 }
hayeswangac718b62013-05-02 16:01:25 +00003309
hayeswang6cc69f22014-10-17 16:55:08 +08003310 set_bit(SELECTIVE_SUSPEND, &tp->flags);
3311 } else {
3312 netif_device_detach(netdev);
3313 }
3314
hayeswange3bd1a82014-10-29 11:12:17 +08003315 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangac718b62013-05-02 16:01:25 +00003316 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08003317 usb_kill_urb(tp->intr_urb);
hayeswangd823ab62015-01-12 12:06:23 +08003318 napi_disable(&tp->napi);
hayeswang9a4be1b2014-02-18 21:49:07 +08003319 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswang445f7f42014-09-23 16:31:47 +08003320 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003321 rtl_runtime_suspend_enable(tp, true);
3322 } else {
hayeswang6cc69f22014-10-17 16:55:08 +08003323 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08003324 tp->rtl_ops.down(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003325 }
hayeswangd823ab62015-01-12 12:06:23 +08003326 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003327 }
hayeswang6cc69f22014-10-17 16:55:08 +08003328out1:
hayeswangb5403272014-10-09 18:00:26 +08003329 mutex_unlock(&tp->control);
3330
hayeswang6cc69f22014-10-17 16:55:08 +08003331 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003332}
3333
3334static int rtl8152_resume(struct usb_interface *intf)
3335{
3336 struct r8152 *tp = usb_get_intfdata(intf);
3337
hayeswangb5403272014-10-09 18:00:26 +08003338 mutex_lock(&tp->control);
3339
hayeswang9a4be1b2014-02-18 21:49:07 +08003340 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3341 tp->rtl_ops.init(tp);
3342 netif_device_attach(tp->netdev);
3343 }
3344
hayeswangac718b62013-05-02 16:01:25 +00003345 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003346 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3347 rtl_runtime_suspend_enable(tp, false);
3348 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswang445f7f42014-09-23 16:31:47 +08003349 set_bit(WORK_ENABLE, &tp->flags);
hayeswang9a4be1b2014-02-18 21:49:07 +08003350 if (tp->speed & LINK_STATUS)
hayeswang445f7f42014-09-23 16:31:47 +08003351 rtl_start_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003352 } else {
3353 tp->rtl_ops.up(tp);
3354 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswangb209af92014-08-25 15:53:00 +08003355 tp->mii.supports_gmii ?
3356 SPEED_1000 : SPEED_100,
3357 DUPLEX_FULL);
hayeswang445f7f42014-09-23 16:31:47 +08003358 tp->speed = 0;
3359 netif_carrier_off(tp->netdev);
3360 set_bit(WORK_ENABLE, &tp->flags);
hayeswang9a4be1b2014-02-18 21:49:07 +08003361 }
hayeswang40a82912013-08-14 20:54:40 +08003362 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswang923e1ee2014-10-29 11:12:15 +08003363 } else if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3364 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00003365 }
3366
hayeswangb5403272014-10-09 18:00:26 +08003367 mutex_unlock(&tp->control);
3368
hayeswangac718b62013-05-02 16:01:25 +00003369 return 0;
3370}
3371
hayeswang21ff2e82014-02-18 21:49:06 +08003372static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3373{
3374 struct r8152 *tp = netdev_priv(dev);
3375
hayeswang9a4be1b2014-02-18 21:49:07 +08003376 if (usb_autopm_get_interface(tp->intf) < 0)
3377 return;
3378
hayeswangb5403272014-10-09 18:00:26 +08003379 mutex_lock(&tp->control);
3380
hayeswang21ff2e82014-02-18 21:49:06 +08003381 wol->supported = WAKE_ANY;
3382 wol->wolopts = __rtl_get_wol(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003383
hayeswangb5403272014-10-09 18:00:26 +08003384 mutex_unlock(&tp->control);
3385
hayeswang9a4be1b2014-02-18 21:49:07 +08003386 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08003387}
3388
3389static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3390{
3391 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003392 int ret;
3393
3394 ret = usb_autopm_get_interface(tp->intf);
3395 if (ret < 0)
3396 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08003397
hayeswangb5403272014-10-09 18:00:26 +08003398 mutex_lock(&tp->control);
3399
hayeswang21ff2e82014-02-18 21:49:06 +08003400 __rtl_set_wol(tp, wol->wolopts);
3401 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
3402
hayeswangb5403272014-10-09 18:00:26 +08003403 mutex_unlock(&tp->control);
3404
hayeswang9a4be1b2014-02-18 21:49:07 +08003405 usb_autopm_put_interface(tp->intf);
3406
3407out_set_wol:
3408 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08003409}
3410
hayeswanga5ec27c2014-02-18 21:49:11 +08003411static u32 rtl8152_get_msglevel(struct net_device *dev)
3412{
3413 struct r8152 *tp = netdev_priv(dev);
3414
3415 return tp->msg_enable;
3416}
3417
3418static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
3419{
3420 struct r8152 *tp = netdev_priv(dev);
3421
3422 tp->msg_enable = value;
3423}
3424
hayeswangac718b62013-05-02 16:01:25 +00003425static void rtl8152_get_drvinfo(struct net_device *netdev,
3426 struct ethtool_drvinfo *info)
3427{
3428 struct r8152 *tp = netdev_priv(netdev);
3429
hayeswangb0b46c72014-08-26 10:08:23 +08003430 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
3431 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00003432 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
3433}
3434
3435static
3436int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
3437{
3438 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08003439 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003440
3441 if (!tp->mii.mdio_read)
3442 return -EOPNOTSUPP;
3443
hayeswang8d4a4d72014-10-09 18:00:25 +08003444 ret = usb_autopm_get_interface(tp->intf);
3445 if (ret < 0)
3446 goto out;
3447
hayeswangb5403272014-10-09 18:00:26 +08003448 mutex_lock(&tp->control);
3449
hayeswang8d4a4d72014-10-09 18:00:25 +08003450 ret = mii_ethtool_gset(&tp->mii, cmd);
3451
hayeswangb5403272014-10-09 18:00:26 +08003452 mutex_unlock(&tp->control);
3453
hayeswang8d4a4d72014-10-09 18:00:25 +08003454 usb_autopm_put_interface(tp->intf);
3455
3456out:
3457 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003458}
3459
3460static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3461{
3462 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003463 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003464
hayeswang9a4be1b2014-02-18 21:49:07 +08003465 ret = usb_autopm_get_interface(tp->intf);
3466 if (ret < 0)
3467 goto out;
3468
hayeswangb5403272014-10-09 18:00:26 +08003469 mutex_lock(&tp->control);
3470
hayeswang9a4be1b2014-02-18 21:49:07 +08003471 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
3472
hayeswangb5403272014-10-09 18:00:26 +08003473 mutex_unlock(&tp->control);
3474
hayeswang9a4be1b2014-02-18 21:49:07 +08003475 usb_autopm_put_interface(tp->intf);
3476
3477out:
3478 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003479}
3480
hayeswang4f1d4d52014-03-11 16:24:19 +08003481static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
3482 "tx_packets",
3483 "rx_packets",
3484 "tx_errors",
3485 "rx_errors",
3486 "rx_missed",
3487 "align_errors",
3488 "tx_single_collisions",
3489 "tx_multi_collisions",
3490 "rx_unicast",
3491 "rx_broadcast",
3492 "rx_multicast",
3493 "tx_aborted",
3494 "tx_underrun",
3495};
3496
3497static int rtl8152_get_sset_count(struct net_device *dev, int sset)
3498{
3499 switch (sset) {
3500 case ETH_SS_STATS:
3501 return ARRAY_SIZE(rtl8152_gstrings);
3502 default:
3503 return -EOPNOTSUPP;
3504 }
3505}
3506
3507static void rtl8152_get_ethtool_stats(struct net_device *dev,
3508 struct ethtool_stats *stats, u64 *data)
3509{
3510 struct r8152 *tp = netdev_priv(dev);
3511 struct tally_counter tally;
3512
hayeswang0b030242014-07-08 14:49:28 +08003513 if (usb_autopm_get_interface(tp->intf) < 0)
3514 return;
3515
hayeswang4f1d4d52014-03-11 16:24:19 +08003516 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
3517
hayeswang0b030242014-07-08 14:49:28 +08003518 usb_autopm_put_interface(tp->intf);
3519
hayeswang4f1d4d52014-03-11 16:24:19 +08003520 data[0] = le64_to_cpu(tally.tx_packets);
3521 data[1] = le64_to_cpu(tally.rx_packets);
3522 data[2] = le64_to_cpu(tally.tx_errors);
3523 data[3] = le32_to_cpu(tally.rx_errors);
3524 data[4] = le16_to_cpu(tally.rx_missed);
3525 data[5] = le16_to_cpu(tally.align_errors);
3526 data[6] = le32_to_cpu(tally.tx_one_collision);
3527 data[7] = le32_to_cpu(tally.tx_multi_collision);
3528 data[8] = le64_to_cpu(tally.rx_unicast);
3529 data[9] = le64_to_cpu(tally.rx_broadcast);
3530 data[10] = le32_to_cpu(tally.rx_multicast);
3531 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08003532 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08003533}
3534
3535static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3536{
3537 switch (stringset) {
3538 case ETH_SS_STATS:
3539 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
3540 break;
3541 }
3542}
3543
hayeswangdf35d282014-09-25 20:54:02 +08003544static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3545{
3546 u32 ocp_data, lp, adv, supported = 0;
3547 u16 val;
3548
3549 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
3550 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3551
3552 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
3553 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3554
3555 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
3556 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3557
3558 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3559 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3560
3561 eee->eee_enabled = !!ocp_data;
3562 eee->eee_active = !!(supported & adv & lp);
3563 eee->supported = supported;
3564 eee->advertised = adv;
3565 eee->lp_advertised = lp;
3566
3567 return 0;
3568}
3569
3570static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3571{
3572 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3573
3574 r8152_eee_en(tp, eee->eee_enabled);
3575
3576 if (!eee->eee_enabled)
3577 val = 0;
3578
3579 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
3580
3581 return 0;
3582}
3583
3584static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3585{
3586 u32 ocp_data, lp, adv, supported = 0;
3587 u16 val;
3588
3589 val = ocp_reg_read(tp, OCP_EEE_ABLE);
3590 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3591
3592 val = ocp_reg_read(tp, OCP_EEE_ADV);
3593 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3594
3595 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
3596 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3597
3598 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3599 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3600
3601 eee->eee_enabled = !!ocp_data;
3602 eee->eee_active = !!(supported & adv & lp);
3603 eee->supported = supported;
3604 eee->advertised = adv;
3605 eee->lp_advertised = lp;
3606
3607 return 0;
3608}
3609
3610static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3611{
3612 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3613
3614 r8153_eee_en(tp, eee->eee_enabled);
3615
3616 if (!eee->eee_enabled)
3617 val = 0;
3618
3619 ocp_reg_write(tp, OCP_EEE_ADV, val);
3620
3621 return 0;
3622}
3623
3624static int
3625rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
3626{
3627 struct r8152 *tp = netdev_priv(net);
3628 int ret;
3629
3630 ret = usb_autopm_get_interface(tp->intf);
3631 if (ret < 0)
3632 goto out;
3633
hayeswangb5403272014-10-09 18:00:26 +08003634 mutex_lock(&tp->control);
3635
hayeswangdf35d282014-09-25 20:54:02 +08003636 ret = tp->rtl_ops.eee_get(tp, edata);
3637
hayeswangb5403272014-10-09 18:00:26 +08003638 mutex_unlock(&tp->control);
3639
hayeswangdf35d282014-09-25 20:54:02 +08003640 usb_autopm_put_interface(tp->intf);
3641
3642out:
3643 return ret;
3644}
3645
3646static int
3647rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
3648{
3649 struct r8152 *tp = netdev_priv(net);
3650 int ret;
3651
3652 ret = usb_autopm_get_interface(tp->intf);
3653 if (ret < 0)
3654 goto out;
3655
hayeswangb5403272014-10-09 18:00:26 +08003656 mutex_lock(&tp->control);
3657
hayeswangdf35d282014-09-25 20:54:02 +08003658 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08003659 if (!ret)
3660 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08003661
hayeswangb5403272014-10-09 18:00:26 +08003662 mutex_unlock(&tp->control);
3663
hayeswangdf35d282014-09-25 20:54:02 +08003664 usb_autopm_put_interface(tp->intf);
3665
3666out:
3667 return ret;
3668}
3669
hayeswang8884f502014-10-28 14:05:52 +08003670static int rtl8152_nway_reset(struct net_device *dev)
3671{
3672 struct r8152 *tp = netdev_priv(dev);
3673 int ret;
3674
3675 ret = usb_autopm_get_interface(tp->intf);
3676 if (ret < 0)
3677 goto out;
3678
3679 mutex_lock(&tp->control);
3680
3681 ret = mii_nway_restart(&tp->mii);
3682
3683 mutex_unlock(&tp->control);
3684
3685 usb_autopm_put_interface(tp->intf);
3686
3687out:
3688 return ret;
3689}
3690
hayeswangac718b62013-05-02 16:01:25 +00003691static struct ethtool_ops ops = {
3692 .get_drvinfo = rtl8152_get_drvinfo,
3693 .get_settings = rtl8152_get_settings,
3694 .set_settings = rtl8152_set_settings,
3695 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08003696 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08003697 .get_msglevel = rtl8152_get_msglevel,
3698 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08003699 .get_wol = rtl8152_get_wol,
3700 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08003701 .get_strings = rtl8152_get_strings,
3702 .get_sset_count = rtl8152_get_sset_count,
3703 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangdf35d282014-09-25 20:54:02 +08003704 .get_eee = rtl_ethtool_get_eee,
3705 .set_eee = rtl_ethtool_set_eee,
hayeswangac718b62013-05-02 16:01:25 +00003706};
3707
3708static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
3709{
3710 struct r8152 *tp = netdev_priv(netdev);
3711 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08003712 int res;
3713
hayeswang68714382014-04-11 17:54:31 +08003714 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3715 return -ENODEV;
3716
hayeswang9a4be1b2014-02-18 21:49:07 +08003717 res = usb_autopm_get_interface(tp->intf);
3718 if (res < 0)
3719 goto out;
hayeswangac718b62013-05-02 16:01:25 +00003720
3721 switch (cmd) {
3722 case SIOCGMIIPHY:
3723 data->phy_id = R8152_PHY_ID; /* Internal PHY */
3724 break;
3725
3726 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08003727 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003728 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08003729 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003730 break;
3731
3732 case SIOCSMIIREG:
3733 if (!capable(CAP_NET_ADMIN)) {
3734 res = -EPERM;
3735 break;
3736 }
hayeswangb5403272014-10-09 18:00:26 +08003737 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003738 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08003739 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003740 break;
3741
3742 default:
3743 res = -EOPNOTSUPP;
3744 }
3745
hayeswang9a4be1b2014-02-18 21:49:07 +08003746 usb_autopm_put_interface(tp->intf);
3747
3748out:
hayeswangac718b62013-05-02 16:01:25 +00003749 return res;
3750}
3751
hayeswang69b4b7a2014-07-10 10:58:54 +08003752static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
3753{
3754 struct r8152 *tp = netdev_priv(dev);
3755
3756 switch (tp->version) {
3757 case RTL_VER_01:
3758 case RTL_VER_02:
3759 return eth_change_mtu(dev, new_mtu);
3760 default:
3761 break;
3762 }
3763
3764 if (new_mtu < 68 || new_mtu > RTL8153_MAX_MTU)
3765 return -EINVAL;
3766
3767 dev->mtu = new_mtu;
3768
3769 return 0;
3770}
3771
hayeswangac718b62013-05-02 16:01:25 +00003772static const struct net_device_ops rtl8152_netdev_ops = {
3773 .ndo_open = rtl8152_open,
3774 .ndo_stop = rtl8152_close,
3775 .ndo_do_ioctl = rtl8152_ioctl,
3776 .ndo_start_xmit = rtl8152_start_xmit,
3777 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08003778 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00003779 .ndo_set_rx_mode = rtl8152_set_rx_mode,
3780 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08003781 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00003782 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08003783 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00003784};
3785
3786static void r8152b_get_version(struct r8152 *tp)
3787{
3788 u32 ocp_data;
3789 u16 version;
3790
3791 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3792 version = (u16)(ocp_data & VERSION_MASK);
3793
3794 switch (version) {
3795 case 0x4c00:
3796 tp->version = RTL_VER_01;
3797 break;
3798 case 0x4c10:
3799 tp->version = RTL_VER_02;
3800 break;
hayeswang43779f82014-01-02 11:25:10 +08003801 case 0x5c00:
3802 tp->version = RTL_VER_03;
3803 tp->mii.supports_gmii = 1;
3804 break;
3805 case 0x5c10:
3806 tp->version = RTL_VER_04;
3807 tp->mii.supports_gmii = 1;
3808 break;
3809 case 0x5c20:
3810 tp->version = RTL_VER_05;
3811 tp->mii.supports_gmii = 1;
3812 break;
hayeswangac718b62013-05-02 16:01:25 +00003813 default:
3814 netif_info(tp, probe, tp->netdev,
3815 "Unknown version 0x%04x\n", version);
3816 break;
3817 }
3818}
3819
hayeswange3fe0b12014-01-02 11:22:39 +08003820static void rtl8152_unload(struct r8152 *tp)
3821{
hayeswang68714382014-04-11 17:54:31 +08003822 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3823 return;
3824
hayeswang00a5e362014-02-18 21:48:59 +08003825 if (tp->version != RTL_VER_01)
3826 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08003827}
3828
hayeswang43779f82014-01-02 11:25:10 +08003829static void rtl8153_unload(struct r8152 *tp)
3830{
hayeswang68714382014-04-11 17:54:31 +08003831 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3832 return;
3833
hayeswang49be1722014-10-01 13:25:11 +08003834 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003835}
3836
hayeswang55b65472014-11-06 12:47:39 +08003837static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08003838{
3839 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08003840 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08003841
hayeswang55b65472014-11-06 12:47:39 +08003842 switch (tp->version) {
3843 case RTL_VER_01:
3844 case RTL_VER_02:
3845 ops->init = r8152b_init;
3846 ops->enable = rtl8152_enable;
3847 ops->disable = rtl8152_disable;
3848 ops->up = rtl8152_up;
3849 ops->down = rtl8152_down;
3850 ops->unload = rtl8152_unload;
3851 ops->eee_get = r8152_get_eee;
3852 ops->eee_set = r8152_set_eee;
hayeswang43779f82014-01-02 11:25:10 +08003853 break;
3854
hayeswang55b65472014-11-06 12:47:39 +08003855 case RTL_VER_03:
3856 case RTL_VER_04:
3857 case RTL_VER_05:
3858 ops->init = r8153_init;
3859 ops->enable = rtl8153_enable;
3860 ops->disable = rtl8153_disable;
3861 ops->up = rtl8153_up;
3862 ops->down = rtl8153_down;
3863 ops->unload = rtl8153_unload;
3864 ops->eee_get = r8153_get_eee;
3865 ops->eee_set = r8153_set_eee;
hayeswangc81229c2014-01-02 11:22:42 +08003866 break;
3867
3868 default:
hayeswang55b65472014-11-06 12:47:39 +08003869 ret = -ENODEV;
3870 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08003871 break;
3872 }
3873
3874 return ret;
3875}
3876
hayeswangac718b62013-05-02 16:01:25 +00003877static int rtl8152_probe(struct usb_interface *intf,
3878 const struct usb_device_id *id)
3879{
3880 struct usb_device *udev = interface_to_usbdev(intf);
3881 struct r8152 *tp;
3882 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08003883 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003884
hayeswang10c32712014-03-04 20:47:48 +08003885 if (udev->actconfig->desc.bConfigurationValue != 1) {
3886 usb_driver_set_configuration(udev, 1);
3887 return -ENODEV;
3888 }
3889
3890 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00003891 netdev = alloc_etherdev(sizeof(struct r8152));
3892 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003893 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00003894 return -ENOMEM;
3895 }
3896
hayeswangebc2ec482013-08-14 20:54:38 +08003897 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00003898 tp = netdev_priv(netdev);
3899 tp->msg_enable = 0x7FFF;
3900
hayeswange3ad4122014-01-06 17:08:42 +08003901 tp->udev = udev;
3902 tp->netdev = netdev;
3903 tp->intf = intf;
3904
hayeswang82cf94c2014-11-06 12:47:38 +08003905 r8152b_get_version(tp);
hayeswang55b65472014-11-06 12:47:39 +08003906 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08003907 if (ret)
3908 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08003909
hayeswangb5403272014-10-09 18:00:26 +08003910 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003911 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3912
hayeswangac718b62013-05-02 16:01:25 +00003913 netdev->netdev_ops = &rtl8152_netdev_ops;
3914 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08003915
hayeswang60c89072014-03-07 11:04:39 +08003916 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08003917 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08003918 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
3919 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08003920 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08003921 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08003922 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
3923 NETIF_F_HW_VLAN_CTAG_RX |
3924 NETIF_F_HW_VLAN_CTAG_TX;
3925 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3926 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
3927 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08003928
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003929 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08003930 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00003931
3932 tp->mii.dev = netdev;
3933 tp->mii.mdio_read = read_mii_word;
3934 tp->mii.mdio_write = write_mii_word;
3935 tp->mii.phy_id_mask = 0x3f;
3936 tp->mii.reg_num_mask = 0x1f;
3937 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00003938
hayeswang9a4be1b2014-02-18 21:49:07 +08003939 intf->needs_remote_wakeup = 1;
3940
hayeswangc81229c2014-01-02 11:22:42 +08003941 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00003942 set_ethernet_addr(tp);
3943
hayeswangac718b62013-05-02 16:01:25 +00003944 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08003945 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00003946
hayeswangebc2ec482013-08-14 20:54:38 +08003947 ret = register_netdev(netdev);
3948 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003949 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08003950 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00003951 }
3952
hayeswang21ff2e82014-02-18 21:49:06 +08003953 tp->saved_wolopts = __rtl_get_wol(tp);
3954 if (tp->saved_wolopts)
3955 device_set_wakeup_enable(&udev->dev, true);
3956 else
3957 device_set_wakeup_enable(&udev->dev, false);
3958
Hayes Wang4a8deae2014-01-07 11:18:22 +08003959 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00003960
3961 return 0;
3962
hayeswangac718b62013-05-02 16:01:25 +00003963out1:
hayeswangd823ab62015-01-12 12:06:23 +08003964 netif_napi_del(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08003965 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00003966out:
3967 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08003968 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003969}
3970
hayeswangac718b62013-05-02 16:01:25 +00003971static void rtl8152_disconnect(struct usb_interface *intf)
3972{
3973 struct r8152 *tp = usb_get_intfdata(intf);
3974
3975 usb_set_intfdata(intf, NULL);
3976 if (tp) {
hayeswangf561de32014-09-30 16:48:01 +08003977 struct usb_device *udev = tp->udev;
3978
3979 if (udev->state == USB_STATE_NOTATTACHED)
3980 set_bit(RTL8152_UNPLUG, &tp->flags);
3981
hayeswangd823ab62015-01-12 12:06:23 +08003982 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003983 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08003984 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00003985 free_netdev(tp->netdev);
3986 }
3987}
3988
hayeswangd9a28c52014-12-04 10:43:11 +08003989#define REALTEK_USB_DEVICE(vend, prod) \
3990 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
3991 USB_DEVICE_ID_MATCH_INT_CLASS, \
3992 .idVendor = (vend), \
3993 .idProduct = (prod), \
3994 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
3995}, \
3996{ \
3997 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
3998 USB_DEVICE_ID_MATCH_DEVICE, \
3999 .idVendor = (vend), \
4000 .idProduct = (prod), \
4001 .bInterfaceClass = USB_CLASS_COMM, \
4002 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
4003 .bInterfaceProtocol = USB_CDC_PROTO_NONE
4004
hayeswangac718b62013-05-02 16:01:25 +00004005/* table of devices that work with this driver */
4006static struct usb_device_id rtl8152_table[] = {
hayeswangd9a28c52014-12-04 10:43:11 +08004007 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
4008 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
4009 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
hayeswangac718b62013-05-02 16:01:25 +00004010 {}
4011};
4012
4013MODULE_DEVICE_TABLE(usb, rtl8152_table);
4014
4015static struct usb_driver rtl8152_driver = {
4016 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08004017 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00004018 .probe = rtl8152_probe,
4019 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00004020 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08004021 .resume = rtl8152_resume,
4022 .reset_resume = rtl8152_resume,
hayeswang9a4be1b2014-02-18 21:49:07 +08004023 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08004024 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00004025};
4026
Sachin Kamatb4236daa2013-05-16 17:48:08 +00004027module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00004028
4029MODULE_AUTHOR(DRIVER_AUTHOR);
4030MODULE_DESCRIPTION(DRIVER_DESC);
4031MODULE_LICENSE("GPL");