blob: 8a8d3a3b18bddb917d29888f00ac25e6fae3bc76 [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>
hayeswangebc2ec42013-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>
hayeswang6128d1b2014-03-07 11:04:40 +080024#include <net/ip6_checksum.h>
hayeswang4c4a6b12014-09-25 20:54:00 +080025#include <uapi/linux/mdio.h>
26#include <linux/mdio.h>
hayeswangd9a28c52014-12-04 10:43:11 +080027#include <linux/usb/cdc.h>
hayeswangac718b62013-05-02 16:01:25 +000028
29/* Version Information */
hayeswangd823ab62015-01-12 12:06:23 +080030#define DRIVER_VERSION "v1.08.0 (2015/01/13)"
hayeswangac718b62013-05-02 16:01:25 +000031#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080032#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000033#define MODULENAME "r8152"
34
35#define R8152_PHY_ID 32
36
37#define PLA_IDR 0xc000
38#define PLA_RCR 0xc010
39#define PLA_RMS 0xc016
40#define PLA_RXFIFO_CTRL0 0xc0a0
41#define PLA_RXFIFO_CTRL1 0xc0a4
42#define PLA_RXFIFO_CTRL2 0xc0a8
hayeswang65bab842015-02-12 16:20:46 +080043#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000044#define PLA_FMC 0xc0b4
45#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080046#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000047#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080048#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000049#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080050#define PLA_TEREDO_TIMER 0xd2cc
51#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000052#define PLA_LEDSEL 0xdd90
53#define PLA_LED_FEATURE 0xdd92
54#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000056#define PLA_GPHY_INTR_IMR 0xe022
57#define PLA_EEE_CR 0xe040
58#define PLA_EEEP_CR 0xe080
59#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080060#define PLA_MAC_PWR_CTRL2 0xe0ca
61#define PLA_MAC_PWR_CTRL3 0xe0cc
62#define PLA_MAC_PWR_CTRL4 0xe0ce
63#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000064#define PLA_TCR0 0xe610
65#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080066#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000067#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080068#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000069#define PLA_CR 0xe813
70#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080071#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
72#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000073#define PLA_CONFIG5 0xe822
74#define PLA_PHY_PWR 0xe84c
75#define PLA_OOB_CTRL 0xe84f
76#define PLA_CPCR 0xe854
77#define PLA_MISC_0 0xe858
78#define PLA_MISC_1 0xe85a
79#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +080080#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000081#define PLA_SFF_STS_7 0xe8de
82#define PLA_PHYSTATUS 0xe908
83#define PLA_BP_BA 0xfc26
84#define PLA_BP_0 0xfc28
85#define PLA_BP_1 0xfc2a
86#define PLA_BP_2 0xfc2c
87#define PLA_BP_3 0xfc2e
88#define PLA_BP_4 0xfc30
89#define PLA_BP_5 0xfc32
90#define PLA_BP_6 0xfc34
91#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080092#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000093
hayeswang65bab842015-02-12 16:20:46 +080094#define USB_USB2PHY 0xb41e
95#define USB_SSPHYLINK2 0xb428
hayeswang43779f82014-01-02 11:25:10 +080096#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +080097#define USB_CSR_DUMMY1 0xb464
98#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +000099#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800100#define USB_CONNECT_TIMER 0xcbf8
101#define USB_BURST_SIZE 0xcfc0
hayeswangac718b62013-05-02 16:01:25 +0000102#define USB_USB_CTRL 0xd406
103#define USB_PHY_CTRL 0xd408
104#define USB_TX_AGG 0xd40a
105#define USB_RX_BUF_TH 0xd40c
106#define USB_USB_TIMER 0xd428
hayeswang464ec102015-02-12 14:33:46 +0800107#define USB_RX_EARLY_TIMEOUT 0xd42c
108#define USB_RX_EARLY_SIZE 0xd42e
hayeswangac718b62013-05-02 16:01:25 +0000109#define USB_PM_CTRL_STATUS 0xd432
110#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +0800111#define USB_TOLERANCE 0xd490
112#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +0000113#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800114#define USB_MISC_0 0xd81a
115#define USB_POWER_CUT 0xd80a
116#define USB_AFE_CTRL2 0xd824
117#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000118#define USB_BP_BA 0xfc26
119#define USB_BP_0 0xfc28
120#define USB_BP_1 0xfc2a
121#define USB_BP_2 0xfc2c
122#define USB_BP_3 0xfc2e
123#define USB_BP_4 0xfc30
124#define USB_BP_5 0xfc32
125#define USB_BP_6 0xfc34
126#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800127#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000128
129/* OCP Registers */
130#define OCP_ALDPS_CONFIG 0x2010
131#define OCP_EEE_CONFIG1 0x2080
132#define OCP_EEE_CONFIG2 0x2092
133#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800134#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000135#define OCP_EEE_AR 0xa41a
136#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800137#define OCP_PHY_STATUS 0xa420
138#define OCP_POWER_CFG 0xa430
139#define OCP_EEE_CFG 0xa432
140#define OCP_SRAM_ADDR 0xa436
141#define OCP_SRAM_DATA 0xa438
142#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800143#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800144#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800145#define OCP_EEE_LPABLE 0xa5d2
hayeswang43779f82014-01-02 11:25:10 +0800146#define OCP_ADC_CFG 0xbc06
147
148/* SRAM Register */
149#define SRAM_LPF_CFG 0x8012
150#define SRAM_10M_AMP1 0x8080
151#define SRAM_10M_AMP2 0x8082
152#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000153
154/* PLA_RCR */
155#define RCR_AAP 0x00000001
156#define RCR_APM 0x00000002
157#define RCR_AM 0x00000004
158#define RCR_AB 0x00000008
159#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
160
161/* PLA_RXFIFO_CTRL0 */
162#define RXFIFO_THR1_NORMAL 0x00080002
163#define RXFIFO_THR1_OOB 0x01800003
164
165/* PLA_RXFIFO_CTRL1 */
166#define RXFIFO_THR2_FULL 0x00000060
167#define RXFIFO_THR2_HIGH 0x00000038
168#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800169#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000170
171/* PLA_RXFIFO_CTRL2 */
172#define RXFIFO_THR3_FULL 0x00000078
173#define RXFIFO_THR3_HIGH 0x00000048
174#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800175#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000176
177/* PLA_TXFIFO_CTRL */
178#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800179#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000180
hayeswang65bab842015-02-12 16:20:46 +0800181/* PLA_DMY_REG0 */
182#define ECM_ALDPS 0x0002
183
hayeswangac718b62013-05-02 16:01:25 +0000184/* PLA_FMC */
185#define FMC_FCR_MCU_EN 0x0001
186
187/* PLA_EEEP_CR */
188#define EEEP_CR_EEEP_TX 0x0002
189
hayeswang43779f82014-01-02 11:25:10 +0800190/* PLA_WDT6_CTRL */
191#define WDT6_SET_MODE 0x0010
192
hayeswangac718b62013-05-02 16:01:25 +0000193/* PLA_TCR0 */
194#define TCR0_TX_EMPTY 0x0800
195#define TCR0_AUTO_FIFO 0x0080
196
197/* PLA_TCR1 */
198#define VERSION_MASK 0x7cf0
199
hayeswang69b4b7a2014-07-10 10:58:54 +0800200/* PLA_MTPS */
201#define MTPS_JUMBO (12 * 1024 / 64)
202#define MTPS_DEFAULT (6 * 1024 / 64)
203
hayeswang4f1d4d52014-03-11 16:24:19 +0800204/* PLA_RSTTALLY */
205#define TALLY_RESET 0x0001
206
hayeswangac718b62013-05-02 16:01:25 +0000207/* PLA_CR */
208#define CR_RST 0x10
209#define CR_RE 0x08
210#define CR_TE 0x04
211
212/* PLA_CRWECR */
213#define CRWECR_NORAML 0x00
214#define CRWECR_CONFIG 0xc0
215
216/* PLA_OOB_CTRL */
217#define NOW_IS_OOB 0x80
218#define TXFIFO_EMPTY 0x20
219#define RXFIFO_EMPTY 0x10
220#define LINK_LIST_READY 0x02
221#define DIS_MCU_CLROOB 0x01
222#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
223
224/* PLA_MISC_1 */
225#define RXDY_GATED_EN 0x0008
226
227/* PLA_SFF_STS_7 */
228#define RE_INIT_LL 0x8000
229#define MCU_BORW_EN 0x4000
230
231/* PLA_CPCR */
232#define CPCR_RX_VLAN 0x0040
233
234/* PLA_CFG_WOL */
235#define MAGIC_EN 0x0001
236
hayeswang43779f82014-01-02 11:25:10 +0800237/* PLA_TEREDO_CFG */
238#define TEREDO_SEL 0x8000
239#define TEREDO_WAKE_MASK 0x7f00
240#define TEREDO_RS_EVENT_MASK 0x00fe
241#define OOB_TEREDO_EN 0x0001
242
hayeswangac718b62013-05-02 16:01:25 +0000243/* PAL_BDC_CR */
244#define ALDPS_PROXY_MODE 0x0001
245
hayeswang21ff2e82014-02-18 21:49:06 +0800246/* PLA_CONFIG34 */
247#define LINK_ON_WAKE_EN 0x0010
248#define LINK_OFF_WAKE_EN 0x0008
249
hayeswangac718b62013-05-02 16:01:25 +0000250/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800251#define BWF_EN 0x0040
252#define MWF_EN 0x0020
253#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000254#define LAN_WAKE_EN 0x0002
255
256/* PLA_LED_FEATURE */
257#define LED_MODE_MASK 0x0700
258
259/* PLA_PHY_PWR */
260#define TX_10M_IDLE_EN 0x0080
261#define PFM_PWM_SWITCH 0x0040
262
263/* PLA_MAC_PWR_CTRL */
264#define D3_CLK_GATED_EN 0x00004000
265#define MCU_CLK_RATIO 0x07010f07
266#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800267#define ALDPS_SPDWN_RATIO 0x0f87
268
269/* PLA_MAC_PWR_CTRL2 */
270#define EEE_SPDWN_RATIO 0x8007
271
272/* PLA_MAC_PWR_CTRL3 */
273#define PKT_AVAIL_SPDWN_EN 0x0100
274#define SUSPEND_SPDWN_EN 0x0004
275#define U1U2_SPDWN_EN 0x0002
276#define L1_SPDWN_EN 0x0001
277
278/* PLA_MAC_PWR_CTRL4 */
279#define PWRSAVE_SPDWN_EN 0x1000
280#define RXDV_SPDWN_EN 0x0800
281#define TX10MIDLE_EN 0x0100
282#define TP100_SPDWN_EN 0x0020
283#define TP500_SPDWN_EN 0x0010
284#define TP1000_SPDWN_EN 0x0008
285#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000286
287/* PLA_GPHY_INTR_IMR */
288#define GPHY_STS_MSK 0x0001
289#define SPEED_DOWN_MSK 0x0002
290#define SPDWN_RXDV_MSK 0x0004
291#define SPDWN_LINKCHG_MSK 0x0008
292
293/* PLA_PHYAR */
294#define PHYAR_FLAG 0x80000000
295
296/* PLA_EEE_CR */
297#define EEE_RX_EN 0x0001
298#define EEE_TX_EN 0x0002
299
hayeswang43779f82014-01-02 11:25:10 +0800300/* PLA_BOOT_CTRL */
301#define AUTOLOAD_DONE 0x0002
302
hayeswang65bab842015-02-12 16:20:46 +0800303/* USB_USB2PHY */
304#define USB2PHY_SUSPEND 0x0001
305#define USB2PHY_L1 0x0002
306
307/* USB_SSPHYLINK2 */
308#define pwd_dn_scale_mask 0x3ffe
309#define pwd_dn_scale(x) ((x) << 1)
310
311/* USB_CSR_DUMMY1 */
312#define DYNAMIC_BURST 0x0001
313
314/* USB_CSR_DUMMY2 */
315#define EP4_FULL_FC 0x0001
316
hayeswangac718b62013-05-02 16:01:25 +0000317/* USB_DEV_STAT */
318#define STAT_SPEED_MASK 0x0006
319#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800320#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000321
322/* USB_TX_AGG */
323#define TX_AGG_MAX_THRESHOLD 0x03
324
325/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800326#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800327#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800328#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000329
330/* USB_TX_DMA */
331#define TEST_MODE_DISABLE 0x00000001
332#define TX_SIZE_ADJUST1 0x00000100
333
334/* USB_UPS_CTRL */
335#define POWER_CUT 0x0100
336
337/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800338#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000339
340/* USB_USB_CTRL */
341#define RX_AGG_DISABLE 0x0010
342
hayeswang43779f82014-01-02 11:25:10 +0800343/* USB_U2P3_CTRL */
344#define U2P3_ENABLE 0x0001
345
346/* USB_POWER_CUT */
347#define PWR_EN 0x0001
348#define PHASE2_EN 0x0008
349
350/* USB_MISC_0 */
351#define PCUT_STATUS 0x0001
352
hayeswang464ec102015-02-12 14:33:46 +0800353/* USB_RX_EARLY_TIMEOUT */
354#define COALESCE_SUPER 85000U
355#define COALESCE_HIGH 250000U
356#define COALESCE_SLOW 524280U
hayeswang43779f82014-01-02 11:25:10 +0800357
358/* USB_WDT11_CTRL */
359#define TIMER11_EN 0x0001
360
361/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800362/* bit 4 ~ 5: fifo empty boundary */
363#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
364/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800365#define LPM_TIMER_MASK 0x0c
366#define LPM_TIMER_500MS 0x04 /* 500 ms */
367#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800368#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800369
370/* USB_AFE_CTRL2 */
371#define SEN_VAL_MASK 0xf800
372#define SEN_VAL_NORMAL 0xa000
373#define SEL_RXIDLE 0x0100
374
hayeswangac718b62013-05-02 16:01:25 +0000375/* OCP_ALDPS_CONFIG */
376#define ENPWRSAVE 0x8000
377#define ENPDNPS 0x0200
378#define LINKENA 0x0100
379#define DIS_SDSAVE 0x0010
380
hayeswang43779f82014-01-02 11:25:10 +0800381/* OCP_PHY_STATUS */
382#define PHY_STAT_MASK 0x0007
383#define PHY_STAT_LAN_ON 3
384#define PHY_STAT_PWRDN 5
385
386/* OCP_POWER_CFG */
387#define EEE_CLKDIV_EN 0x8000
388#define EN_ALDPS 0x0004
389#define EN_10M_PLLOFF 0x0001
390
hayeswangac718b62013-05-02 16:01:25 +0000391/* OCP_EEE_CONFIG1 */
392#define RG_TXLPI_MSK_HFDUP 0x8000
393#define RG_MATCLR_EN 0x4000
394#define EEE_10_CAP 0x2000
395#define EEE_NWAY_EN 0x1000
396#define TX_QUIET_EN 0x0200
397#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800398#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800399#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000400#define RG_RXLPI_MSK_HFDUP 0x0008
401#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
402
403/* OCP_EEE_CONFIG2 */
404#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
405#define RG_DACQUIET_EN 0x0400
406#define RG_LDVQUIET_EN 0x0200
407#define RG_CKRSEL 0x0020
408#define RG_EEEPRG_EN 0x0010
409
410/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800411#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800412#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000413#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
414#define MSK_PH 0x0006 /* bit 0 ~ 3 */
415
416/* OCP_EEE_AR */
417/* bit[15:14] function */
418#define FUN_ADDR 0x0000
419#define FUN_DATA 0x4000
420/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000421
hayeswang43779f82014-01-02 11:25:10 +0800422/* OCP_EEE_CFG */
423#define CTAP_SHORT_EN 0x0040
424#define EEE10_EN 0x0010
425
426/* OCP_DOWN_SPEED */
427#define EN_10M_BGOFF 0x0080
428
hayeswang43779f82014-01-02 11:25:10 +0800429/* OCP_ADC_CFG */
430#define CKADSEL_L 0x0100
431#define ADC_EN 0x0080
432#define EN_EMI_L 0x0040
433
434/* SRAM_LPF_CFG */
435#define LPF_AUTO_TUNE 0x8000
436
437/* SRAM_10M_AMP1 */
438#define GDAC_IB_UPALL 0x0008
439
440/* SRAM_10M_AMP2 */
441#define AMP_DN 0x0200
442
443/* SRAM_IMPEDANCE */
444#define RX_DRIVING_MASK 0x6000
445
hayeswangac718b62013-05-02 16:01:25 +0000446enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800447 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000448 _100bps = 0x08,
449 _10bps = 0x04,
450 LINK_STATUS = 0x02,
451 FULL_DUP = 0x01,
452};
453
hayeswang1764bcd2014-08-28 10:24:18 +0800454#define RTL8152_MAX_TX 4
hayeswangebc2ec42013-08-14 20:54:38 +0800455#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800456#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800457#define CRC_SIZE 4
458#define TX_ALIGN 4
459#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800460
461#define INTR_LINK 0x0004
hayeswangebc2ec42013-08-14 20:54:38 +0800462
hayeswangac718b62013-05-02 16:01:25 +0000463#define RTL8152_REQT_READ 0xc0
464#define RTL8152_REQT_WRITE 0x40
465#define RTL8152_REQ_GET_REGS 0x05
466#define RTL8152_REQ_SET_REGS 0x05
467
468#define BYTE_EN_DWORD 0xff
469#define BYTE_EN_WORD 0x33
470#define BYTE_EN_BYTE 0x11
471#define BYTE_EN_SIX_BYTES 0x3f
472#define BYTE_EN_START_MASK 0x0f
473#define BYTE_EN_END_MASK 0xf0
474
hayeswang69b4b7a2014-07-10 10:58:54 +0800475#define RTL8153_MAX_PACKET 9216 /* 9K */
476#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
hayeswangac718b62013-05-02 16:01:25 +0000477#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800478#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800479#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800480#define RTL8152_NAPI_WEIGHT 64
hayeswangac718b62013-05-02 16:01:25 +0000481
482/* rtl8152 flags */
483enum rtl8152_flags {
484 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000485 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800486 WORK_ENABLE,
487 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800488 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800489 PHY_RESET,
hayeswangd823ab62015-01-12 12:06:23 +0800490 SCHEDULE_NAPI,
hayeswangac718b62013-05-02 16:01:25 +0000491};
492
493/* Define these values to match your device */
494#define VENDOR_ID_REALTEK 0x0bda
hayeswang43779f82014-01-02 11:25:10 +0800495#define VENDOR_ID_SAMSUNG 0x04e8
hayeswangac718b62013-05-02 16:01:25 +0000496
497#define MCU_TYPE_PLA 0x0100
498#define MCU_TYPE_USB 0x0000
499
hayeswang4f1d4d52014-03-11 16:24:19 +0800500struct tally_counter {
501 __le64 tx_packets;
502 __le64 rx_packets;
503 __le64 tx_errors;
504 __le32 rx_errors;
505 __le16 rx_missed;
506 __le16 align_errors;
507 __le32 tx_one_collision;
508 __le32 tx_multi_collision;
509 __le64 rx_unicast;
510 __le64 rx_broadcast;
511 __le32 rx_multicast;
512 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800513 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800514};
515
hayeswangac718b62013-05-02 16:01:25 +0000516struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800517 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000518#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800519
hayeswang500b6d72013-11-20 17:30:57 +0800520 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800521#define RD_UDP_CS BIT(23)
522#define RD_TCP_CS BIT(22)
523#define RD_IPV6_CS BIT(20)
524#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800525
hayeswang500b6d72013-11-20 17:30:57 +0800526 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800527#define IPF BIT(23) /* IP checksum fail */
528#define UDPF BIT(22) /* UDP checksum fail */
529#define TCPF BIT(21) /* TCP checksum fail */
530#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800531
hayeswang500b6d72013-11-20 17:30:57 +0800532 __le32 opts4;
533 __le32 opts5;
534 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000535};
536
537struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800538 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800539#define TX_FS BIT(31) /* First segment of a packet */
540#define TX_LS BIT(30) /* Final segment of a packet */
541#define GTSENDV4 BIT(28)
542#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800543#define GTTCPHO_SHIFT 18
hayeswang6128d1b2014-03-07 11:04:40 +0800544#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800545#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800546
hayeswang500b6d72013-11-20 17:30:57 +0800547 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800548#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
549#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
550#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
551#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800552#define MSS_SHIFT 17
553#define MSS_MAX 0x7ffU
554#define TCPHO_SHIFT 17
hayeswang6128d1b2014-03-07 11:04:40 +0800555#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800556#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000557};
558
hayeswangdff4e8a2013-08-16 16:09:33 +0800559struct r8152;
560
hayeswangebc2ec42013-08-14 20:54:38 +0800561struct rx_agg {
562 struct list_head list;
563 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800564 struct r8152 *context;
hayeswangebc2ec42013-08-14 20:54:38 +0800565 void *buffer;
566 void *head;
567};
568
569struct tx_agg {
570 struct list_head list;
571 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800572 struct r8152 *context;
hayeswangebc2ec42013-08-14 20:54:38 +0800573 void *buffer;
574 void *head;
575 u32 skb_num;
576 u32 skb_len;
577};
578
hayeswangac718b62013-05-02 16:01:25 +0000579struct r8152 {
580 unsigned long flags;
581 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800582 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800583 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000584 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800585 struct urb *intr_urb;
hayeswangebc2ec42013-08-14 20:54:38 +0800586 struct tx_agg tx_info[RTL8152_MAX_TX];
587 struct rx_agg rx_info[RTL8152_MAX_RX];
588 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800589 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec42013-08-14 20:54:38 +0800590 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000591 struct delayed_work schedule;
592 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800593 struct mutex control; /* use for hw setting */
hayeswangc81229c2014-01-02 11:22:42 +0800594
595 struct rtl_ops {
596 void (*init)(struct r8152 *);
597 int (*enable)(struct r8152 *);
598 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800599 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800600 void (*down)(struct r8152 *);
601 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800602 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
603 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswangc81229c2014-01-02 11:22:42 +0800604 } rtl_ops;
605
hayeswang40a82912013-08-14 20:54:40 +0800606 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800607 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000608 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800609 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800610 u32 coalesce;
hayeswangac718b62013-05-02 16:01:25 +0000611 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800612 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000613 u8 version;
hayeswangac718b62013-05-02 16:01:25 +0000614};
615
616enum rtl_version {
617 RTL_VER_UNKNOWN = 0,
618 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800619 RTL_VER_02,
620 RTL_VER_03,
621 RTL_VER_04,
622 RTL_VER_05,
623 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000624};
625
hayeswang60c89072014-03-07 11:04:39 +0800626enum tx_csum_stat {
627 TX_CSUM_SUCCESS = 0,
628 TX_CSUM_TSO,
629 TX_CSUM_NONE
630};
631
hayeswangac718b62013-05-02 16:01:25 +0000632/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
633 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
634 */
635static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800636static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000637
hayeswang52aec122014-09-02 10:27:52 +0800638#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswang60c89072014-03-07 11:04:39 +0800639 VLAN_ETH_HLEN - VLAN_HLEN)
640
hayeswangac718b62013-05-02 16:01:25 +0000641static
642int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
643{
hayeswang31787f52013-07-31 17:21:25 +0800644 int ret;
645 void *tmp;
646
647 tmp = kmalloc(size, GFP_KERNEL);
648 if (!tmp)
649 return -ENOMEM;
650
651 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800652 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
653 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800654
655 memcpy(data, tmp, size);
656 kfree(tmp);
657
658 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000659}
660
661static
662int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
663{
hayeswang31787f52013-07-31 17:21:25 +0800664 int ret;
665 void *tmp;
666
Benoit Tainec4438f02014-05-26 17:21:23 +0200667 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800668 if (!tmp)
669 return -ENOMEM;
670
hayeswang31787f52013-07-31 17:21:25 +0800671 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800672 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
673 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800674
675 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800676
hayeswang31787f52013-07-31 17:21:25 +0800677 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000678}
679
680static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800681 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000682{
hayeswang45f4a192014-01-06 17:08:41 +0800683 u16 limit = 64;
684 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000685
686 if (test_bit(RTL8152_UNPLUG, &tp->flags))
687 return -ENODEV;
688
689 /* both size and indix must be 4 bytes align */
690 if ((size & 3) || !size || (index & 3) || !data)
691 return -EPERM;
692
693 if ((u32)index + (u32)size > 0xffff)
694 return -EPERM;
695
696 while (size) {
697 if (size > limit) {
698 ret = get_registers(tp, index, type, limit, data);
699 if (ret < 0)
700 break;
701
702 index += limit;
703 data += limit;
704 size -= limit;
705 } else {
706 ret = get_registers(tp, index, type, size, data);
707 if (ret < 0)
708 break;
709
710 index += size;
711 data += size;
712 size = 0;
713 break;
714 }
715 }
716
hayeswang67610492014-10-30 11:46:40 +0800717 if (ret == -ENODEV)
718 set_bit(RTL8152_UNPLUG, &tp->flags);
719
hayeswangac718b62013-05-02 16:01:25 +0000720 return ret;
721}
722
723static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800724 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000725{
hayeswang45f4a192014-01-06 17:08:41 +0800726 int ret;
727 u16 byteen_start, byteen_end, byen;
728 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000729
730 if (test_bit(RTL8152_UNPLUG, &tp->flags))
731 return -ENODEV;
732
733 /* both size and indix must be 4 bytes align */
734 if ((size & 3) || !size || (index & 3) || !data)
735 return -EPERM;
736
737 if ((u32)index + (u32)size > 0xffff)
738 return -EPERM;
739
740 byteen_start = byteen & BYTE_EN_START_MASK;
741 byteen_end = byteen & BYTE_EN_END_MASK;
742
743 byen = byteen_start | (byteen_start << 4);
744 ret = set_registers(tp, index, type | byen, 4, data);
745 if (ret < 0)
746 goto error1;
747
748 index += 4;
749 data += 4;
750 size -= 4;
751
752 if (size) {
753 size -= 4;
754
755 while (size) {
756 if (size > limit) {
757 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800758 type | BYTE_EN_DWORD,
759 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000760 if (ret < 0)
761 goto error1;
762
763 index += limit;
764 data += limit;
765 size -= limit;
766 } else {
767 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800768 type | BYTE_EN_DWORD,
769 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000770 if (ret < 0)
771 goto error1;
772
773 index += size;
774 data += size;
775 size = 0;
776 break;
777 }
778 }
779
780 byen = byteen_end | (byteen_end >> 4);
781 ret = set_registers(tp, index, type | byen, 4, data);
782 if (ret < 0)
783 goto error1;
784 }
785
786error1:
hayeswang67610492014-10-30 11:46:40 +0800787 if (ret == -ENODEV)
788 set_bit(RTL8152_UNPLUG, &tp->flags);
789
hayeswangac718b62013-05-02 16:01:25 +0000790 return ret;
791}
792
793static inline
794int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
795{
796 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
797}
798
799static inline
800int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
801{
802 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
803}
804
805static inline
806int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
807{
808 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
809}
810
811static inline
812int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
813{
814 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
815}
816
817static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
818{
hayeswangc8826de2013-07-31 17:21:26 +0800819 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000820
hayeswangc8826de2013-07-31 17:21:26 +0800821 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000822
823 return __le32_to_cpu(data);
824}
825
826static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
827{
hayeswangc8826de2013-07-31 17:21:26 +0800828 __le32 tmp = __cpu_to_le32(data);
829
830 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000831}
832
833static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
834{
835 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800836 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000837 u8 shift = index & 2;
838
839 index &= ~3;
840
hayeswangc8826de2013-07-31 17:21:26 +0800841 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000842
hayeswangc8826de2013-07-31 17:21:26 +0800843 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000844 data >>= (shift * 8);
845 data &= 0xffff;
846
847 return (u16)data;
848}
849
850static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
851{
hayeswangc8826de2013-07-31 17:21:26 +0800852 u32 mask = 0xffff;
853 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000854 u16 byen = BYTE_EN_WORD;
855 u8 shift = index & 2;
856
857 data &= mask;
858
859 if (index & 2) {
860 byen <<= shift;
861 mask <<= (shift * 8);
862 data <<= (shift * 8);
863 index &= ~3;
864 }
865
hayeswangc8826de2013-07-31 17:21:26 +0800866 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000867
hayeswangc8826de2013-07-31 17:21:26 +0800868 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000869}
870
871static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
872{
873 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800874 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000875 u8 shift = index & 3;
876
877 index &= ~3;
878
hayeswangc8826de2013-07-31 17:21:26 +0800879 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000880
hayeswangc8826de2013-07-31 17:21:26 +0800881 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000882 data >>= (shift * 8);
883 data &= 0xff;
884
885 return (u8)data;
886}
887
888static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
889{
hayeswangc8826de2013-07-31 17:21:26 +0800890 u32 mask = 0xff;
891 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000892 u16 byen = BYTE_EN_BYTE;
893 u8 shift = index & 3;
894
895 data &= mask;
896
897 if (index & 3) {
898 byen <<= shift;
899 mask <<= (shift * 8);
900 data <<= (shift * 8);
901 index &= ~3;
902 }
903
hayeswangc8826de2013-07-31 17:21:26 +0800904 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000905
hayeswangc8826de2013-07-31 17:21:26 +0800906 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000907}
908
hayeswangac244d32014-01-02 11:22:40 +0800909static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
910{
911 u16 ocp_base, ocp_index;
912
913 ocp_base = addr & 0xf000;
914 if (ocp_base != tp->ocp_base) {
915 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
916 tp->ocp_base = ocp_base;
917 }
918
919 ocp_index = (addr & 0x0fff) | 0xb000;
920 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
921}
922
hayeswange3fe0b12014-01-02 11:22:39 +0800923static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
924{
925 u16 ocp_base, ocp_index;
926
927 ocp_base = addr & 0xf000;
928 if (ocp_base != tp->ocp_base) {
929 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
930 tp->ocp_base = ocp_base;
931 }
932
933 ocp_index = (addr & 0x0fff) | 0xb000;
934 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
935}
936
hayeswangac244d32014-01-02 11:22:40 +0800937static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000938{
hayeswangac244d32014-01-02 11:22:40 +0800939 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000940}
941
hayeswangac244d32014-01-02 11:22:40 +0800942static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000943{
hayeswangac244d32014-01-02 11:22:40 +0800944 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000945}
946
hayeswang43779f82014-01-02 11:25:10 +0800947static void sram_write(struct r8152 *tp, u16 addr, u16 data)
948{
949 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
950 ocp_reg_write(tp, OCP_SRAM_DATA, data);
951}
952
hayeswangac718b62013-05-02 16:01:25 +0000953static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
954{
955 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +0800956 int ret;
hayeswangac718b62013-05-02 16:01:25 +0000957
hayeswang68714382014-04-11 17:54:31 +0800958 if (test_bit(RTL8152_UNPLUG, &tp->flags))
959 return -ENODEV;
960
hayeswangac718b62013-05-02 16:01:25 +0000961 if (phy_id != R8152_PHY_ID)
962 return -EINVAL;
963
hayeswang9a4be1b2014-02-18 21:49:07 +0800964 ret = r8152_mdio_read(tp, reg);
965
hayeswang9a4be1b2014-02-18 21:49:07 +0800966 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000967}
968
969static
970void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
971{
972 struct r8152 *tp = netdev_priv(netdev);
973
hayeswang68714382014-04-11 17:54:31 +0800974 if (test_bit(RTL8152_UNPLUG, &tp->flags))
975 return;
976
hayeswangac718b62013-05-02 16:01:25 +0000977 if (phy_id != R8152_PHY_ID)
978 return;
979
980 r8152_mdio_write(tp, reg, val);
981}
982
hayeswangb209af92014-08-25 15:53:00 +0800983static int
984r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec42013-08-14 20:54:38 +0800985
hayeswang8ba789a2014-09-04 16:15:41 +0800986static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
987{
988 struct r8152 *tp = netdev_priv(netdev);
989 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +0800990 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +0800991
992 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +0800993 goto out1;
994
995 ret = usb_autopm_get_interface(tp->intf);
996 if (ret < 0)
997 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +0800998
hayeswangb5403272014-10-09 18:00:26 +0800999 mutex_lock(&tp->control);
1000
hayeswang8ba789a2014-09-04 16:15:41 +08001001 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1002
1003 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1004 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1005 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1006
hayeswangb5403272014-10-09 18:00:26 +08001007 mutex_unlock(&tp->control);
1008
hayeswangea6a7112014-10-02 17:03:12 +08001009 usb_autopm_put_interface(tp->intf);
1010out1:
1011 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001012}
1013
hayeswang179bb6d2014-09-04 16:15:42 +08001014static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001015{
1016 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001017 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001018 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001019
hayeswang8a91c822014-02-18 21:49:01 +08001020 if (tp->version == RTL_VER_01)
hayeswang179bb6d2014-09-04 16:15:42 +08001021 ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001022 else
hayeswang179bb6d2014-09-04 16:15:42 +08001023 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001024
1025 if (ret < 0) {
hayeswang179bb6d2014-09-04 16:15:42 +08001026 netif_err(tp, probe, dev, "Get ether addr fail\n");
1027 } else if (!is_valid_ether_addr(sa.sa_data)) {
1028 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1029 sa.sa_data);
1030 eth_hw_addr_random(dev);
1031 ether_addr_copy(sa.sa_data, dev->dev_addr);
1032 ret = rtl8152_set_mac_address(dev, &sa);
1033 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1034 sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001035 } else {
hayeswang179bb6d2014-09-04 16:15:42 +08001036 if (tp->version == RTL_VER_01)
1037 ether_addr_copy(dev->dev_addr, sa.sa_data);
1038 else
1039 ret = rtl8152_set_mac_address(dev, &sa);
hayeswangac718b62013-05-02 16:01:25 +00001040 }
hayeswang179bb6d2014-09-04 16:15:42 +08001041
1042 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001043}
1044
hayeswangac718b62013-05-02 16:01:25 +00001045static void read_bulk_callback(struct urb *urb)
1046{
hayeswangac718b62013-05-02 16:01:25 +00001047 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001048 int status = urb->status;
hayeswangebc2ec42013-08-14 20:54:38 +08001049 struct rx_agg *agg;
1050 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +00001051
hayeswangebc2ec42013-08-14 20:54:38 +08001052 agg = urb->context;
1053 if (!agg)
1054 return;
1055
1056 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001057 if (!tp)
1058 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001059
hayeswangac718b62013-05-02 16:01:25 +00001060 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1061 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001062
1063 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001064 return;
1065
hayeswangebc2ec42013-08-14 20:54:38 +08001066 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001067
1068 /* When link down, the driver would cancel all bulks. */
1069 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +08001070 if (!netif_carrier_ok(netdev))
1071 return;
1072
hayeswang9a4be1b2014-02-18 21:49:07 +08001073 usb_mark_last_busy(tp->udev);
1074
hayeswangac718b62013-05-02 16:01:25 +00001075 switch (status) {
1076 case 0:
hayeswangebc2ec42013-08-14 20:54:38 +08001077 if (urb->actual_length < ETH_ZLEN)
1078 break;
1079
hayeswang2685d412014-03-07 11:04:34 +08001080 spin_lock(&tp->rx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001081 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001082 spin_unlock(&tp->rx_lock);
hayeswangd823ab62015-01-12 12:06:23 +08001083 napi_schedule(&tp->napi);
hayeswangebc2ec42013-08-14 20:54:38 +08001084 return;
hayeswangac718b62013-05-02 16:01:25 +00001085 case -ESHUTDOWN:
1086 set_bit(RTL8152_UNPLUG, &tp->flags);
1087 netif_device_detach(tp->netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001088 return;
hayeswangac718b62013-05-02 16:01:25 +00001089 case -ENOENT:
1090 return; /* the urb is in unlink state */
1091 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001092 if (net_ratelimit())
1093 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001094 break;
hayeswangac718b62013-05-02 16:01:25 +00001095 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001096 if (net_ratelimit())
1097 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001098 break;
hayeswangac718b62013-05-02 16:01:25 +00001099 }
1100
hayeswanga0fccd42014-11-20 10:29:05 +08001101 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001102}
1103
1104static void write_bulk_callback(struct urb *urb)
1105{
hayeswangebc2ec42013-08-14 20:54:38 +08001106 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001107 struct net_device *netdev;
hayeswangebc2ec42013-08-14 20:54:38 +08001108 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001109 struct r8152 *tp;
1110 int status = urb->status;
1111
hayeswangebc2ec42013-08-14 20:54:38 +08001112 agg = urb->context;
1113 if (!agg)
1114 return;
1115
1116 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001117 if (!tp)
1118 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001119
hayeswangd104eaf2014-03-06 15:07:17 +08001120 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001121 stats = &netdev->stats;
hayeswangebc2ec42013-08-14 20:54:38 +08001122 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001123 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001124 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001125 stats->tx_errors += agg->skb_num;
1126 } else {
1127 stats->tx_packets += agg->skb_num;
1128 stats->tx_bytes += agg->skb_len;
1129 }
1130
hayeswang2685d412014-03-07 11:04:34 +08001131 spin_lock(&tp->tx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001132 list_add_tail(&agg->list, &tp->tx_free);
hayeswang2685d412014-03-07 11:04:34 +08001133 spin_unlock(&tp->tx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001134
hayeswang9a4be1b2014-02-18 21:49:07 +08001135 usb_autopm_put_interface_async(tp->intf);
1136
hayeswangd104eaf2014-03-06 15:07:17 +08001137 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001138 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001139
1140 if (!test_bit(WORK_ENABLE, &tp->flags))
1141 return;
1142
1143 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1144 return;
1145
1146 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001147 napi_schedule(&tp->napi);
hayeswangebc2ec42013-08-14 20:54:38 +08001148}
1149
hayeswang40a82912013-08-14 20:54:40 +08001150static void intr_callback(struct urb *urb)
1151{
1152 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001153 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001154 int status = urb->status;
1155 int res;
1156
1157 tp = urb->context;
1158 if (!tp)
1159 return;
1160
1161 if (!test_bit(WORK_ENABLE, &tp->flags))
1162 return;
1163
1164 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1165 return;
1166
1167 switch (status) {
1168 case 0: /* success */
1169 break;
1170 case -ECONNRESET: /* unlink */
1171 case -ESHUTDOWN:
1172 netif_device_detach(tp->netdev);
1173 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001174 case -EPROTO:
1175 netif_info(tp, intr, tp->netdev,
1176 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001177 return;
1178 case -EOVERFLOW:
1179 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1180 goto resubmit;
1181 /* -EPIPE: should clear the halt */
1182 default:
1183 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1184 goto resubmit;
1185 }
1186
1187 d = urb->transfer_buffer;
1188 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001189 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001190 set_bit(RTL8152_LINK_CHG, &tp->flags);
1191 schedule_delayed_work(&tp->schedule, 0);
1192 }
1193 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001194 if (netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001195 set_bit(RTL8152_LINK_CHG, &tp->flags);
1196 schedule_delayed_work(&tp->schedule, 0);
1197 }
1198 }
1199
1200resubmit:
1201 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001202 if (res == -ENODEV) {
1203 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001204 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001205 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001206 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001207 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001208 }
hayeswang40a82912013-08-14 20:54:40 +08001209}
1210
hayeswangebc2ec42013-08-14 20:54:38 +08001211static inline void *rx_agg_align(void *data)
1212{
hayeswang8e1f51b2014-01-02 11:22:41 +08001213 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001214}
1215
1216static inline void *tx_agg_align(void *data)
1217{
hayeswang8e1f51b2014-01-02 11:22:41 +08001218 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001219}
1220
1221static void free_all_mem(struct r8152 *tp)
1222{
1223 int i;
1224
1225 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001226 usb_free_urb(tp->rx_info[i].urb);
1227 tp->rx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001228
hayeswang9629e3c2014-01-15 10:42:15 +08001229 kfree(tp->rx_info[i].buffer);
1230 tp->rx_info[i].buffer = NULL;
1231 tp->rx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001232 }
1233
1234 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001235 usb_free_urb(tp->tx_info[i].urb);
1236 tp->tx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001237
hayeswang9629e3c2014-01-15 10:42:15 +08001238 kfree(tp->tx_info[i].buffer);
1239 tp->tx_info[i].buffer = NULL;
1240 tp->tx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001241 }
hayeswang40a82912013-08-14 20:54:40 +08001242
hayeswang9629e3c2014-01-15 10:42:15 +08001243 usb_free_urb(tp->intr_urb);
1244 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001245
hayeswang9629e3c2014-01-15 10:42:15 +08001246 kfree(tp->intr_buff);
1247 tp->intr_buff = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001248}
1249
1250static int alloc_all_mem(struct r8152 *tp)
1251{
1252 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001253 struct usb_interface *intf = tp->intf;
1254 struct usb_host_interface *alt = intf->cur_altsetting;
1255 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec42013-08-14 20:54:38 +08001256 struct urb *urb;
1257 int node, i;
1258 u8 *buf;
1259
1260 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1261
1262 spin_lock_init(&tp->rx_lock);
1263 spin_lock_init(&tp->tx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001264 INIT_LIST_HEAD(&tp->tx_free);
1265 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001266 skb_queue_head_init(&tp->rx_queue);
hayeswangebc2ec42013-08-14 20:54:38 +08001267
1268 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001269 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec42013-08-14 20:54:38 +08001270 if (!buf)
1271 goto err1;
1272
1273 if (buf != rx_agg_align(buf)) {
1274 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001275 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001276 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001277 if (!buf)
1278 goto err1;
1279 }
1280
1281 urb = usb_alloc_urb(0, GFP_KERNEL);
1282 if (!urb) {
1283 kfree(buf);
1284 goto err1;
1285 }
1286
1287 INIT_LIST_HEAD(&tp->rx_info[i].list);
1288 tp->rx_info[i].context = tp;
1289 tp->rx_info[i].urb = urb;
1290 tp->rx_info[i].buffer = buf;
1291 tp->rx_info[i].head = rx_agg_align(buf);
1292 }
1293
1294 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001295 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec42013-08-14 20:54:38 +08001296 if (!buf)
1297 goto err1;
1298
1299 if (buf != tx_agg_align(buf)) {
1300 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001301 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001302 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001303 if (!buf)
1304 goto err1;
1305 }
1306
1307 urb = usb_alloc_urb(0, GFP_KERNEL);
1308 if (!urb) {
1309 kfree(buf);
1310 goto err1;
1311 }
1312
1313 INIT_LIST_HEAD(&tp->tx_info[i].list);
1314 tp->tx_info[i].context = tp;
1315 tp->tx_info[i].urb = urb;
1316 tp->tx_info[i].buffer = buf;
1317 tp->tx_info[i].head = tx_agg_align(buf);
1318
1319 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1320 }
1321
hayeswang40a82912013-08-14 20:54:40 +08001322 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1323 if (!tp->intr_urb)
1324 goto err1;
1325
1326 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1327 if (!tp->intr_buff)
1328 goto err1;
1329
1330 tp->intr_interval = (int)ep_intr->desc.bInterval;
1331 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001332 tp->intr_buff, INTBUFSIZE, intr_callback,
1333 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001334
hayeswangebc2ec42013-08-14 20:54:38 +08001335 return 0;
1336
1337err1:
1338 free_all_mem(tp);
1339 return -ENOMEM;
1340}
1341
hayeswang0de98f62013-08-16 16:09:35 +08001342static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1343{
1344 struct tx_agg *agg = NULL;
1345 unsigned long flags;
1346
hayeswang21949ab2014-03-07 11:04:35 +08001347 if (list_empty(&tp->tx_free))
1348 return NULL;
1349
hayeswang0de98f62013-08-16 16:09:35 +08001350 spin_lock_irqsave(&tp->tx_lock, flags);
1351 if (!list_empty(&tp->tx_free)) {
1352 struct list_head *cursor;
1353
1354 cursor = tp->tx_free.next;
1355 list_del_init(cursor);
1356 agg = list_entry(cursor, struct tx_agg, list);
1357 }
1358 spin_unlock_irqrestore(&tp->tx_lock, flags);
1359
1360 return agg;
1361}
1362
hayeswangb209af92014-08-25 15:53:00 +08001363/* r8152_csum_workaround()
hayeswang6128d1b2014-03-07 11:04:40 +08001364 * The hw limites the value the transport offset. When the offset is out of the
1365 * range, calculate the checksum by sw.
1366 */
1367static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1368 struct sk_buff_head *list)
1369{
1370 if (skb_shinfo(skb)->gso_size) {
1371 netdev_features_t features = tp->netdev->features;
1372 struct sk_buff_head seg_list;
1373 struct sk_buff *segs, *nskb;
1374
hayeswanga91d45f2014-07-11 16:48:27 +08001375 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1b2014-03-07 11:04:40 +08001376 segs = skb_gso_segment(skb, features);
1377 if (IS_ERR(segs) || !segs)
1378 goto drop;
1379
1380 __skb_queue_head_init(&seg_list);
1381
1382 do {
1383 nskb = segs;
1384 segs = segs->next;
1385 nskb->next = NULL;
1386 __skb_queue_tail(&seg_list, nskb);
1387 } while (segs);
1388
1389 skb_queue_splice(&seg_list, list);
1390 dev_kfree_skb(skb);
1391 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1392 if (skb_checksum_help(skb) < 0)
1393 goto drop;
1394
1395 __skb_queue_head(list, skb);
1396 } else {
1397 struct net_device_stats *stats;
1398
1399drop:
1400 stats = &tp->netdev->stats;
1401 stats->tx_dropped++;
1402 dev_kfree_skb(skb);
1403 }
1404}
1405
hayeswangb209af92014-08-25 15:53:00 +08001406/* msdn_giant_send_check()
hayeswang6128d1b2014-03-07 11:04:40 +08001407 * According to the document of microsoft, the TCP Pseudo Header excludes the
1408 * packet length for IPv6 TCP large packets.
1409 */
1410static int msdn_giant_send_check(struct sk_buff *skb)
1411{
1412 const struct ipv6hdr *ipv6h;
1413 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001414 int ret;
1415
1416 ret = skb_cow_head(skb, 0);
1417 if (ret)
1418 return ret;
hayeswang6128d1b2014-03-07 11:04:40 +08001419
1420 ipv6h = ipv6_hdr(skb);
1421 th = tcp_hdr(skb);
1422
1423 th->check = 0;
1424 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1425
hayeswangfcb308d2014-03-11 10:20:32 +08001426 return ret;
hayeswang6128d1b2014-03-07 11:04:40 +08001427}
1428
hayeswangc5554292014-09-12 10:43:11 +08001429static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1430{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001431 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001432 u32 opts2;
1433
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001434 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001435 desc->opts2 |= cpu_to_le32(opts2);
1436 }
1437}
1438
1439static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1440{
1441 u32 opts2 = le32_to_cpu(desc->opts2);
1442
1443 if (opts2 & RX_VLAN_TAG)
1444 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1445 swab16(opts2 & 0xffff));
1446}
1447
hayeswang60c89072014-03-07 11:04:39 +08001448static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1449 struct sk_buff *skb, u32 len, u32 transport_offset)
1450{
1451 u32 mss = skb_shinfo(skb)->gso_size;
1452 u32 opts1, opts2 = 0;
1453 int ret = TX_CSUM_SUCCESS;
1454
1455 WARN_ON_ONCE(len > TX_LEN_MAX);
1456
1457 opts1 = len | TX_FS | TX_LS;
1458
1459 if (mss) {
hayeswang6128d1b2014-03-07 11:04:40 +08001460 if (transport_offset > GTTCPHO_MAX) {
1461 netif_warn(tp, tx_err, tp->netdev,
1462 "Invalid transport offset 0x%x for TSO\n",
1463 transport_offset);
1464 ret = TX_CSUM_TSO;
1465 goto unavailable;
1466 }
1467
hayeswang6e74d172015-02-06 11:30:50 +08001468 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001469 case htons(ETH_P_IP):
1470 opts1 |= GTSENDV4;
1471 break;
1472
hayeswang6128d1b2014-03-07 11:04:40 +08001473 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001474 if (msdn_giant_send_check(skb)) {
1475 ret = TX_CSUM_TSO;
1476 goto unavailable;
1477 }
hayeswang6128d1b2014-03-07 11:04:40 +08001478 opts1 |= GTSENDV6;
hayeswang6128d1b2014-03-07 11:04:40 +08001479 break;
1480
hayeswang60c89072014-03-07 11:04:39 +08001481 default:
1482 WARN_ON_ONCE(1);
1483 break;
1484 }
1485
1486 opts1 |= transport_offset << GTTCPHO_SHIFT;
1487 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1488 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001489 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001490
hayeswang6128d1b2014-03-07 11:04:40 +08001491 if (transport_offset > TCPHO_MAX) {
1492 netif_warn(tp, tx_err, tp->netdev,
1493 "Invalid transport offset 0x%x\n",
1494 transport_offset);
1495 ret = TX_CSUM_NONE;
1496 goto unavailable;
1497 }
1498
hayeswang6e74d172015-02-06 11:30:50 +08001499 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001500 case htons(ETH_P_IP):
1501 opts2 |= IPV4_CS;
1502 ip_protocol = ip_hdr(skb)->protocol;
1503 break;
1504
1505 case htons(ETH_P_IPV6):
1506 opts2 |= IPV6_CS;
1507 ip_protocol = ipv6_hdr(skb)->nexthdr;
1508 break;
1509
1510 default:
1511 ip_protocol = IPPROTO_RAW;
1512 break;
1513 }
1514
hayeswang60c89072014-03-07 11:04:39 +08001515 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001516 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001517 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001518 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001519 else
hayeswang5bd23882013-08-14 20:54:39 +08001520 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001521
hayeswang60c89072014-03-07 11:04:39 +08001522 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001523 }
hayeswang60c89072014-03-07 11:04:39 +08001524
1525 desc->opts2 = cpu_to_le32(opts2);
1526 desc->opts1 = cpu_to_le32(opts1);
1527
hayeswang6128d1b2014-03-07 11:04:40 +08001528unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001529 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001530}
1531
hayeswangb1379d92013-08-16 16:09:37 +08001532static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1533{
hayeswangd84130a2014-02-18 21:49:02 +08001534 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001535 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001536 u8 *tx_data;
1537
hayeswangd84130a2014-02-18 21:49:02 +08001538 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001539 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001540 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001541 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001542
hayeswangb1379d92013-08-16 16:09:37 +08001543 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001544 agg->skb_num = 0;
1545 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001546 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001547
hayeswang7937f9e2013-11-20 17:30:54 +08001548 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001549 struct tx_desc *tx_desc;
1550 struct sk_buff *skb;
1551 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001552 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001553
hayeswangd84130a2014-02-18 21:49:02 +08001554 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001555 if (!skb)
1556 break;
1557
hayeswang60c89072014-03-07 11:04:39 +08001558 len = skb->len + sizeof(*tx_desc);
1559
1560 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001561 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001562 break;
1563 }
1564
hayeswang7937f9e2013-11-20 17:30:54 +08001565 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001566 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001567
1568 offset = (u32)skb_transport_offset(skb);
1569
hayeswang6128d1b2014-03-07 11:04:40 +08001570 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1571 r8152_csum_workaround(tp, skb, &skb_head);
1572 continue;
1573 }
hayeswang60c89072014-03-07 11:04:39 +08001574
hayeswangc5554292014-09-12 10:43:11 +08001575 rtl_tx_vlan_tag(tx_desc, skb);
1576
hayeswangb1379d92013-08-16 16:09:37 +08001577 tx_data += sizeof(*tx_desc);
1578
hayeswang60c89072014-03-07 11:04:39 +08001579 len = skb->len;
1580 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1581 struct net_device_stats *stats = &tp->netdev->stats;
1582
1583 stats->tx_dropped++;
1584 dev_kfree_skb_any(skb);
1585 tx_data -= sizeof(*tx_desc);
1586 continue;
1587 }
hayeswangb1379d92013-08-16 16:09:37 +08001588
hayeswang7937f9e2013-11-20 17:30:54 +08001589 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001590 agg->skb_len += len;
1591 agg->skb_num++;
1592
1593 dev_kfree_skb_any(skb);
1594
hayeswang52aec122014-09-02 10:27:52 +08001595 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001596 }
1597
hayeswangd84130a2014-02-18 21:49:02 +08001598 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001599 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001600 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001601 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001602 }
1603
hayeswang0c3121f2014-03-07 11:04:36 +08001604 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001605
1606 if (netif_queue_stopped(tp->netdev) &&
1607 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1608 netif_wake_queue(tp->netdev);
1609
hayeswang0c3121f2014-03-07 11:04:36 +08001610 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001611
hayeswang0c3121f2014-03-07 11:04:36 +08001612 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001613 if (ret < 0)
1614 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001615
hayeswangb1379d92013-08-16 16:09:37 +08001616 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1617 agg->head, (int)(tx_data - (u8 *)agg->head),
1618 (usb_complete_t)write_bulk_callback, agg);
1619
hayeswang0c3121f2014-03-07 11:04:36 +08001620 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001621 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001622 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001623
1624out_tx_fill:
1625 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001626}
1627
hayeswang565cab02014-03-07 11:04:38 +08001628static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1629{
1630 u8 checksum = CHECKSUM_NONE;
1631 u32 opts2, opts3;
1632
1633 if (tp->version == RTL_VER_01)
1634 goto return_result;
1635
1636 opts2 = le32_to_cpu(rx_desc->opts2);
1637 opts3 = le32_to_cpu(rx_desc->opts3);
1638
1639 if (opts2 & RD_IPV4_CS) {
1640 if (opts3 & IPF)
1641 checksum = CHECKSUM_NONE;
1642 else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
1643 checksum = CHECKSUM_NONE;
1644 else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
1645 checksum = CHECKSUM_NONE;
1646 else
1647 checksum = CHECKSUM_UNNECESSARY;
hayeswang6128d1b2014-03-07 11:04:40 +08001648 } else if (RD_IPV6_CS) {
1649 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1650 checksum = CHECKSUM_UNNECESSARY;
1651 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1652 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001653 }
1654
1655return_result:
1656 return checksum;
1657}
1658
hayeswangd823ab62015-01-12 12:06:23 +08001659static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec42013-08-14 20:54:38 +08001660{
hayeswanga5a4f462013-08-16 16:09:34 +08001661 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001662 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08001663 int ret = 0, work_done = 0;
hayeswangd823ab62015-01-12 12:06:23 +08001664
1665 if (!skb_queue_empty(&tp->rx_queue)) {
1666 while (work_done < budget) {
1667 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1668 struct net_device *netdev = tp->netdev;
1669 struct net_device_stats *stats = &netdev->stats;
1670 unsigned int pkt_len;
1671
1672 if (!skb)
1673 break;
1674
1675 pkt_len = skb->len;
1676 napi_gro_receive(&tp->napi, skb);
1677 work_done++;
1678 stats->rx_packets++;
1679 stats->rx_bytes += pkt_len;
1680 }
1681 }
hayeswangebc2ec42013-08-14 20:54:38 +08001682
hayeswangd84130a2014-02-18 21:49:02 +08001683 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08001684 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08001685
1686 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001687 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001688 list_splice_init(&tp->rx_done, &rx_queue);
1689 spin_unlock_irqrestore(&tp->rx_lock, flags);
1690
1691 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001692 struct rx_desc *rx_desc;
1693 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001694 int len_used = 0;
1695 struct urb *urb;
1696 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08001697
hayeswangebc2ec42013-08-14 20:54:38 +08001698 list_del_init(cursor);
hayeswangebc2ec42013-08-14 20:54:38 +08001699
1700 agg = list_entry(cursor, struct rx_agg, list);
1701 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001702 if (urb->actual_length < ETH_ZLEN)
1703 goto submit;
hayeswangebc2ec42013-08-14 20:54:38 +08001704
hayeswangebc2ec42013-08-14 20:54:38 +08001705 rx_desc = agg->head;
1706 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001707 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001708
hayeswang7937f9e2013-11-20 17:30:54 +08001709 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001710 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001711 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001712 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001713 struct sk_buff *skb;
1714
hayeswang7937f9e2013-11-20 17:30:54 +08001715 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec42013-08-14 20:54:38 +08001716 if (pkt_len < ETH_ZLEN)
1717 break;
1718
hayeswang7937f9e2013-11-20 17:30:54 +08001719 len_used += pkt_len;
1720 if (urb->actual_length < len_used)
1721 break;
1722
hayeswang8e1f51b2014-01-02 11:22:41 +08001723 pkt_len -= CRC_SIZE;
hayeswangebc2ec42013-08-14 20:54:38 +08001724 rx_data += sizeof(struct rx_desc);
1725
1726 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1727 if (!skb) {
1728 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001729 goto find_next_rx;
hayeswangebc2ec42013-08-14 20:54:38 +08001730 }
hayeswang565cab02014-03-07 11:04:38 +08001731
1732 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001733 memcpy(skb->data, rx_data, pkt_len);
1734 skb_put(skb, pkt_len);
1735 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08001736 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001737 if (work_done < budget) {
1738 napi_gro_receive(&tp->napi, skb);
1739 work_done++;
1740 stats->rx_packets++;
1741 stats->rx_bytes += pkt_len;
1742 } else {
1743 __skb_queue_tail(&tp->rx_queue, skb);
1744 }
hayeswangebc2ec42013-08-14 20:54:38 +08001745
hayeswang5e2f7482014-03-07 11:04:37 +08001746find_next_rx:
hayeswang8e1f51b2014-01-02 11:22:41 +08001747 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec42013-08-14 20:54:38 +08001748 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec42013-08-14 20:54:38 +08001749 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001750 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001751 }
1752
hayeswang0de98f62013-08-16 16:09:35 +08001753submit:
hayeswange1a2ca92015-02-06 11:30:45 +08001754 if (!ret) {
1755 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1756 } else {
1757 urb->actual_length = 0;
1758 list_add_tail(&agg->list, next);
1759 }
1760 }
1761
1762 if (!list_empty(&rx_queue)) {
1763 spin_lock_irqsave(&tp->rx_lock, flags);
1764 list_splice_tail(&rx_queue, &tp->rx_done);
1765 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001766 }
hayeswangd823ab62015-01-12 12:06:23 +08001767
1768out1:
1769 return work_done;
hayeswangebc2ec42013-08-14 20:54:38 +08001770}
1771
1772static void tx_bottom(struct r8152 *tp)
1773{
hayeswangebc2ec42013-08-14 20:54:38 +08001774 int res;
1775
hayeswangb1379d92013-08-16 16:09:37 +08001776 do {
1777 struct tx_agg *agg;
hayeswangebc2ec42013-08-14 20:54:38 +08001778
hayeswangb1379d92013-08-16 16:09:37 +08001779 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec42013-08-14 20:54:38 +08001780 break;
1781
hayeswangb1379d92013-08-16 16:09:37 +08001782 agg = r8152_get_tx_agg(tp);
1783 if (!agg)
hayeswangebc2ec42013-08-14 20:54:38 +08001784 break;
hayeswangb1379d92013-08-16 16:09:37 +08001785
1786 res = r8152_tx_agg_fill(tp, agg);
1787 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08001788 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08001789
1790 if (res == -ENODEV) {
hayeswang67610492014-10-30 11:46:40 +08001791 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswangb1379d92013-08-16 16:09:37 +08001792 netif_device_detach(netdev);
1793 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08001794 struct net_device_stats *stats = &netdev->stats;
1795 unsigned long flags;
1796
hayeswangb1379d92013-08-16 16:09:37 +08001797 netif_warn(tp, tx_err, netdev,
1798 "failed tx_urb %d\n", res);
1799 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08001800
hayeswangb1379d92013-08-16 16:09:37 +08001801 spin_lock_irqsave(&tp->tx_lock, flags);
1802 list_add_tail(&agg->list, &tp->tx_free);
1803 spin_unlock_irqrestore(&tp->tx_lock, flags);
1804 }
hayeswangebc2ec42013-08-14 20:54:38 +08001805 }
hayeswangb1379d92013-08-16 16:09:37 +08001806 } while (res == 0);
hayeswangebc2ec42013-08-14 20:54:38 +08001807}
1808
hayeswangd823ab62015-01-12 12:06:23 +08001809static void bottom_half(struct r8152 *tp)
hayeswangebc2ec42013-08-14 20:54:38 +08001810{
hayeswangebc2ec42013-08-14 20:54:38 +08001811 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1812 return;
1813
1814 if (!test_bit(WORK_ENABLE, &tp->flags))
1815 return;
1816
hayeswang7559fb2f2013-08-16 16:09:38 +08001817 /* When link down, the driver would cancel all bulks. */
1818 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +08001819 if (!netif_carrier_ok(tp->netdev))
1820 return;
1821
hayeswangd823ab62015-01-12 12:06:23 +08001822 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08001823
hayeswang0c3121f2014-03-07 11:04:36 +08001824 tx_bottom(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001825}
1826
hayeswangd823ab62015-01-12 12:06:23 +08001827static int r8152_poll(struct napi_struct *napi, int budget)
1828{
1829 struct r8152 *tp = container_of(napi, struct r8152, napi);
1830 int work_done;
1831
1832 work_done = rx_bottom(tp, budget);
1833 bottom_half(tp);
1834
1835 if (work_done < budget) {
1836 napi_complete(napi);
1837 if (!list_empty(&tp->rx_done))
1838 napi_schedule(napi);
1839 }
1840
1841 return work_done;
1842}
1843
hayeswangebc2ec42013-08-14 20:54:38 +08001844static
1845int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1846{
hayeswanga0fccd42014-11-20 10:29:05 +08001847 int ret;
1848
hayeswangef827a52015-01-09 10:26:36 +08001849 /* The rx would be stopped, so skip submitting */
1850 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
1851 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
1852 return 0;
1853
hayeswangebc2ec42013-08-14 20:54:38 +08001854 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
hayeswang52aec122014-09-02 10:27:52 +08001855 agg->head, agg_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08001856 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec42013-08-14 20:54:38 +08001857
hayeswanga0fccd42014-11-20 10:29:05 +08001858 ret = usb_submit_urb(agg->urb, mem_flags);
1859 if (ret == -ENODEV) {
1860 set_bit(RTL8152_UNPLUG, &tp->flags);
1861 netif_device_detach(tp->netdev);
1862 } else if (ret) {
1863 struct urb *urb = agg->urb;
1864 unsigned long flags;
1865
1866 urb->actual_length = 0;
1867 spin_lock_irqsave(&tp->rx_lock, flags);
1868 list_add_tail(&agg->list, &tp->rx_done);
1869 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001870
1871 netif_err(tp, rx_err, tp->netdev,
1872 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
1873
1874 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08001875 }
1876
1877 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001878}
1879
hayeswang00a5e362014-02-18 21:48:59 +08001880static void rtl_drop_queued_tx(struct r8152 *tp)
1881{
1882 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001883 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08001884 struct sk_buff *skb;
1885
hayeswangd84130a2014-02-18 21:49:02 +08001886 if (skb_queue_empty(tx_queue))
1887 return;
1888
1889 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001890 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001891 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001892 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001893
1894 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001895 dev_kfree_skb(skb);
1896 stats->tx_dropped++;
1897 }
1898}
1899
hayeswangac718b62013-05-02 16:01:25 +00001900static void rtl8152_tx_timeout(struct net_device *netdev)
1901{
1902 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001903 int i;
1904
Hayes Wang4a8deae2014-01-07 11:18:22 +08001905 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001906 for (i = 0; i < RTL8152_MAX_TX; i++)
1907 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001908}
1909
1910static void rtl8152_set_rx_mode(struct net_device *netdev)
1911{
1912 struct r8152 *tp = netdev_priv(netdev);
1913
hayeswang51d979f2015-02-06 11:30:47 +08001914 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00001915 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001916 schedule_delayed_work(&tp->schedule, 0);
1917 }
hayeswangac718b62013-05-02 16:01:25 +00001918}
1919
1920static void _rtl8152_set_rx_mode(struct net_device *netdev)
1921{
1922 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001923 u32 mc_filter[2]; /* Multicast hash filter */
1924 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001925 u32 ocp_data;
1926
hayeswangac718b62013-05-02 16:01:25 +00001927 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1928 netif_stop_queue(netdev);
1929 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1930 ocp_data &= ~RCR_ACPT_ALL;
1931 ocp_data |= RCR_AB | RCR_APM;
1932
1933 if (netdev->flags & IFF_PROMISC) {
1934 /* Unconditionally log net taps. */
1935 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1936 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08001937 mc_filter[1] = 0xffffffff;
1938 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001939 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1940 (netdev->flags & IFF_ALLMULTI)) {
1941 /* Too many to filter perfectly -- accept all multicasts. */
1942 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08001943 mc_filter[1] = 0xffffffff;
1944 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001945 } else {
1946 struct netdev_hw_addr *ha;
1947
hayeswangb209af92014-08-25 15:53:00 +08001948 mc_filter[1] = 0;
1949 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00001950 netdev_for_each_mc_addr(ha, netdev) {
1951 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08001952
hayeswangac718b62013-05-02 16:01:25 +00001953 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1954 ocp_data |= RCR_AM;
1955 }
1956 }
1957
hayeswang31787f52013-07-31 17:21:25 +08001958 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1959 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001960
hayeswang31787f52013-07-31 17:21:25 +08001961 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001962 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1963 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001964}
1965
hayeswanga5e31252015-01-06 17:41:58 +08001966static netdev_features_t
1967rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
1968 netdev_features_t features)
1969{
1970 u32 mss = skb_shinfo(skb)->gso_size;
1971 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
1972 int offset = skb_transport_offset(skb);
1973
1974 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
1975 features &= ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
1976 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
1977 features &= ~NETIF_F_GSO_MASK;
1978
1979 return features;
1980}
1981
hayeswangac718b62013-05-02 16:01:25 +00001982static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08001983 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00001984{
1985 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001986
hayeswangac718b62013-05-02 16:01:25 +00001987 skb_tx_timestamp(skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001988
hayeswang61598782013-11-20 17:30:55 +08001989 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001990
hayeswang0c3121f2014-03-07 11:04:36 +08001991 if (!list_empty(&tp->tx_free)) {
1992 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08001993 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08001994 schedule_delayed_work(&tp->schedule, 0);
1995 } else {
1996 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08001997 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08001998 }
hayeswangb209af92014-08-25 15:53:00 +08001999 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002000 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002001 }
hayeswangdd1b1192013-11-20 17:30:56 +08002002
hayeswangac718b62013-05-02 16:01:25 +00002003 return NETDEV_TX_OK;
2004}
2005
2006static void r8152b_reset_packet_filter(struct r8152 *tp)
2007{
2008 u32 ocp_data;
2009
2010 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2011 ocp_data &= ~FMC_FCR_MCU_EN;
2012 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2013 ocp_data |= FMC_FCR_MCU_EN;
2014 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2015}
2016
2017static void rtl8152_nic_reset(struct r8152 *tp)
2018{
2019 int i;
2020
2021 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2022
2023 for (i = 0; i < 1000; i++) {
2024 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2025 break;
hayeswangb209af92014-08-25 15:53:00 +08002026 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002027 }
2028}
2029
hayeswangdd1b1192013-11-20 17:30:56 +08002030static void set_tx_qlen(struct r8152 *tp)
2031{
2032 struct net_device *netdev = tp->netdev;
2033
hayeswang52aec122014-09-02 10:27:52 +08002034 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
2035 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002036}
2037
hayeswangac718b62013-05-02 16:01:25 +00002038static inline u8 rtl8152_get_speed(struct r8152 *tp)
2039{
2040 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2041}
2042
hayeswang507605a2014-01-02 11:22:43 +08002043static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002044{
hayeswangebc2ec42013-08-14 20:54:38 +08002045 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002046 u8 speed;
2047
2048 speed = rtl8152_get_speed(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002049 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002050 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08002051 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002052 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2053 } else {
2054 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08002055 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002056 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2057 }
hayeswang507605a2014-01-02 11:22:43 +08002058}
2059
hayeswang00a5e362014-02-18 21:48:59 +08002060static void rxdy_gated_en(struct r8152 *tp, bool enable)
2061{
2062 u32 ocp_data;
2063
2064 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2065 if (enable)
2066 ocp_data |= RXDY_GATED_EN;
2067 else
2068 ocp_data &= ~RXDY_GATED_EN;
2069 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2070}
2071
hayeswang445f7f42014-09-23 16:31:47 +08002072static int rtl_start_rx(struct r8152 *tp)
2073{
2074 int i, ret = 0;
2075
hayeswangd823ab62015-01-12 12:06:23 +08002076 napi_disable(&tp->napi);
hayeswang445f7f42014-09-23 16:31:47 +08002077 INIT_LIST_HEAD(&tp->rx_done);
2078 for (i = 0; i < RTL8152_MAX_RX; i++) {
2079 INIT_LIST_HEAD(&tp->rx_info[i].list);
2080 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2081 if (ret)
2082 break;
2083 }
hayeswangd823ab62015-01-12 12:06:23 +08002084 napi_enable(&tp->napi);
hayeswang445f7f42014-09-23 16:31:47 +08002085
hayeswang7bcf4f62014-11-20 10:29:06 +08002086 if (ret && ++i < RTL8152_MAX_RX) {
2087 struct list_head rx_queue;
2088 unsigned long flags;
2089
2090 INIT_LIST_HEAD(&rx_queue);
2091
2092 do {
2093 struct rx_agg *agg = &tp->rx_info[i++];
2094 struct urb *urb = agg->urb;
2095
2096 urb->actual_length = 0;
2097 list_add_tail(&agg->list, &rx_queue);
2098 } while (i < RTL8152_MAX_RX);
2099
2100 spin_lock_irqsave(&tp->rx_lock, flags);
2101 list_splice_tail(&rx_queue, &tp->rx_done);
2102 spin_unlock_irqrestore(&tp->rx_lock, flags);
2103 }
2104
hayeswang445f7f42014-09-23 16:31:47 +08002105 return ret;
2106}
2107
2108static int rtl_stop_rx(struct r8152 *tp)
2109{
2110 int i;
2111
2112 for (i = 0; i < RTL8152_MAX_RX; i++)
2113 usb_kill_urb(tp->rx_info[i].urb);
2114
hayeswangd823ab62015-01-12 12:06:23 +08002115 while (!skb_queue_empty(&tp->rx_queue))
2116 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2117
hayeswang445f7f42014-09-23 16:31:47 +08002118 return 0;
2119}
2120
hayeswang507605a2014-01-02 11:22:43 +08002121static int rtl_enable(struct r8152 *tp)
2122{
2123 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002124
2125 r8152b_reset_packet_filter(tp);
2126
2127 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2128 ocp_data |= CR_RE | CR_TE;
2129 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2130
hayeswang00a5e362014-02-18 21:48:59 +08002131 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002132
hayeswangaa2e0922015-01-09 10:26:35 +08002133 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002134}
2135
hayeswang507605a2014-01-02 11:22:43 +08002136static int rtl8152_enable(struct r8152 *tp)
2137{
hayeswang68714382014-04-11 17:54:31 +08002138 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2139 return -ENODEV;
2140
hayeswang507605a2014-01-02 11:22:43 +08002141 set_tx_qlen(tp);
2142 rtl_set_eee_plus(tp);
2143
2144 return rtl_enable(tp);
2145}
2146
hayeswang464ec102015-02-12 14:33:46 +08002147static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002148{
hayeswang464ec102015-02-12 14:33:46 +08002149 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002150
hayeswang464ec102015-02-12 14:33:46 +08002151 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, ocp_data);
2152}
2153
2154static void r8153_set_rx_early_size(struct r8152 *tp)
2155{
2156 u32 mtu = tp->netdev->mtu;
2157 u32 ocp_data = (agg_buf_sz - mtu - VLAN_ETH_HLEN - VLAN_HLEN) / 4;
2158
2159 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08002160}
2161
2162static int rtl8153_enable(struct r8152 *tp)
2163{
hayeswang68714382014-04-11 17:54:31 +08002164 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2165 return -ENODEV;
2166
hayeswang43779f82014-01-02 11:25:10 +08002167 set_tx_qlen(tp);
2168 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002169 r8153_set_rx_early_timeout(tp);
2170 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002171
2172 return rtl_enable(tp);
2173}
2174
hayeswangd70b1132014-09-19 15:17:18 +08002175static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002176{
hayeswangebc2ec42013-08-14 20:54:38 +08002177 u32 ocp_data;
2178 int i;
hayeswangac718b62013-05-02 16:01:25 +00002179
hayeswang68714382014-04-11 17:54:31 +08002180 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2181 rtl_drop_queued_tx(tp);
2182 return;
2183 }
2184
hayeswangac718b62013-05-02 16:01:25 +00002185 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2186 ocp_data &= ~RCR_ACPT_ALL;
2187 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2188
hayeswang00a5e362014-02-18 21:48:59 +08002189 rtl_drop_queued_tx(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002190
2191 for (i = 0; i < RTL8152_MAX_TX; i++)
2192 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002193
hayeswang00a5e362014-02-18 21:48:59 +08002194 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002195
2196 for (i = 0; i < 1000; i++) {
2197 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2198 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2199 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002200 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002201 }
2202
2203 for (i = 0; i < 1000; i++) {
2204 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2205 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002206 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002207 }
2208
hayeswang445f7f42014-09-23 16:31:47 +08002209 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002210
2211 rtl8152_nic_reset(tp);
2212}
2213
hayeswang00a5e362014-02-18 21:48:59 +08002214static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2215{
2216 u32 ocp_data;
2217
2218 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2219 if (enable)
2220 ocp_data |= POWER_CUT;
2221 else
2222 ocp_data &= ~POWER_CUT;
2223 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2224
2225 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2226 ocp_data &= ~RESUME_INDICATE;
2227 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002228}
2229
hayeswangc5554292014-09-12 10:43:11 +08002230static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2231{
2232 u32 ocp_data;
2233
2234 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2235 if (enable)
2236 ocp_data |= CPCR_RX_VLAN;
2237 else
2238 ocp_data &= ~CPCR_RX_VLAN;
2239 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2240}
2241
2242static int rtl8152_set_features(struct net_device *dev,
2243 netdev_features_t features)
2244{
2245 netdev_features_t changed = features ^ dev->features;
2246 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002247 int ret;
2248
2249 ret = usb_autopm_get_interface(tp->intf);
2250 if (ret < 0)
2251 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002252
hayeswangb5403272014-10-09 18:00:26 +08002253 mutex_lock(&tp->control);
2254
hayeswangc5554292014-09-12 10:43:11 +08002255 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2256 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2257 rtl_rx_vlan_en(tp, true);
2258 else
2259 rtl_rx_vlan_en(tp, false);
2260 }
2261
hayeswangb5403272014-10-09 18:00:26 +08002262 mutex_unlock(&tp->control);
2263
hayeswang405f8a02014-10-09 18:00:24 +08002264 usb_autopm_put_interface(tp->intf);
2265
2266out:
2267 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002268}
2269
hayeswang21ff2e82014-02-18 21:49:06 +08002270#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2271
2272static u32 __rtl_get_wol(struct r8152 *tp)
2273{
2274 u32 ocp_data;
2275 u32 wolopts = 0;
2276
2277 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2278 if (!(ocp_data & LAN_WAKE_EN))
2279 return 0;
2280
2281 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2282 if (ocp_data & LINK_ON_WAKE_EN)
2283 wolopts |= WAKE_PHY;
2284
2285 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2286 if (ocp_data & UWF_EN)
2287 wolopts |= WAKE_UCAST;
2288 if (ocp_data & BWF_EN)
2289 wolopts |= WAKE_BCAST;
2290 if (ocp_data & MWF_EN)
2291 wolopts |= WAKE_MCAST;
2292
2293 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2294 if (ocp_data & MAGIC_EN)
2295 wolopts |= WAKE_MAGIC;
2296
2297 return wolopts;
2298}
2299
2300static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2301{
2302 u32 ocp_data;
2303
2304 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2305
2306 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2307 ocp_data &= ~LINK_ON_WAKE_EN;
2308 if (wolopts & WAKE_PHY)
2309 ocp_data |= LINK_ON_WAKE_EN;
2310 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2311
2312 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2313 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
2314 if (wolopts & WAKE_UCAST)
2315 ocp_data |= UWF_EN;
2316 if (wolopts & WAKE_BCAST)
2317 ocp_data |= BWF_EN;
2318 if (wolopts & WAKE_MCAST)
2319 ocp_data |= MWF_EN;
2320 if (wolopts & WAKE_ANY)
2321 ocp_data |= LAN_WAKE_EN;
2322 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2323
2324 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2325
2326 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2327 ocp_data &= ~MAGIC_EN;
2328 if (wolopts & WAKE_MAGIC)
2329 ocp_data |= MAGIC_EN;
2330 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2331
2332 if (wolopts & WAKE_ANY)
2333 device_set_wakeup_enable(&tp->udev->dev, true);
2334 else
2335 device_set_wakeup_enable(&tp->udev->dev, false);
2336}
2337
hayeswang9a4be1b2014-02-18 21:49:07 +08002338static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2339{
2340 if (enable) {
2341 u32 ocp_data;
2342
2343 __rtl_set_wol(tp, WAKE_ANY);
2344
2345 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2346
2347 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2348 ocp_data |= LINK_OFF_WAKE_EN;
2349 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2350
2351 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2352 } else {
2353 __rtl_set_wol(tp, tp->saved_wolopts);
2354 }
2355}
2356
hayeswangaa66a5f2014-02-18 21:49:04 +08002357static void rtl_phy_reset(struct r8152 *tp)
2358{
2359 u16 data;
2360 int i;
2361
2362 clear_bit(PHY_RESET, &tp->flags);
2363
2364 data = r8152_mdio_read(tp, MII_BMCR);
2365
2366 /* don't reset again before the previous one complete */
2367 if (data & BMCR_RESET)
2368 return;
2369
2370 data |= BMCR_RESET;
2371 r8152_mdio_write(tp, MII_BMCR, data);
2372
2373 for (i = 0; i < 50; i++) {
2374 msleep(20);
2375 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2376 break;
2377 }
2378}
2379
hayeswang43499682014-02-18 21:48:58 +08002380static void r8153_teredo_off(struct r8152 *tp)
2381{
2382 u32 ocp_data;
2383
2384 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2385 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
2386 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2387
2388 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2389 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2390 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2391}
2392
2393static void r8152b_disable_aldps(struct r8152 *tp)
2394{
2395 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
2396 msleep(20);
2397}
2398
2399static inline void r8152b_enable_aldps(struct r8152 *tp)
2400{
2401 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2402 LINKENA | DIS_SDSAVE);
2403}
2404
hayeswangd70b1132014-09-19 15:17:18 +08002405static void rtl8152_disable(struct r8152 *tp)
2406{
2407 r8152b_disable_aldps(tp);
2408 rtl_disable(tp);
2409 r8152b_enable_aldps(tp);
2410}
2411
hayeswang43499682014-02-18 21:48:58 +08002412static void r8152b_hw_phy_cfg(struct r8152 *tp)
2413{
hayeswangf0cbe0a2014-02-18 21:49:03 +08002414 u16 data;
2415
2416 data = r8152_mdio_read(tp, MII_BMCR);
2417 if (data & BMCR_PDOWN) {
2418 data &= ~BMCR_PDOWN;
2419 r8152_mdio_write(tp, MII_BMCR, data);
2420 }
2421
hayeswangaa66a5f2014-02-18 21:49:04 +08002422 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002423}
2424
hayeswangac718b62013-05-02 16:01:25 +00002425static void r8152b_exit_oob(struct r8152 *tp)
2426{
hayeswangdb8515e2014-03-06 15:07:16 +08002427 u32 ocp_data;
2428 int i;
hayeswangac718b62013-05-02 16:01:25 +00002429
2430 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2431 ocp_data &= ~RCR_ACPT_ALL;
2432 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2433
hayeswang00a5e362014-02-18 21:48:59 +08002434 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002435 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002436 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002437
2438 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2439 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2440
2441 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2442 ocp_data &= ~NOW_IS_OOB;
2443 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2444
2445 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2446 ocp_data &= ~MCU_BORW_EN;
2447 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2448
2449 for (i = 0; i < 1000; i++) {
2450 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2451 if (ocp_data & LINK_LIST_READY)
2452 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002453 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002454 }
2455
2456 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2457 ocp_data |= RE_INIT_LL;
2458 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2459
2460 for (i = 0; i < 1000; i++) {
2461 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2462 if (ocp_data & LINK_LIST_READY)
2463 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002464 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002465 }
2466
2467 rtl8152_nic_reset(tp);
2468
2469 /* rx share fifo credit full threshold */
2470 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2471
hayeswanga3cc4652014-07-24 16:37:43 +08002472 if (tp->udev->speed == USB_SPEED_FULL ||
2473 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00002474 /* rx share fifo credit near full threshold */
2475 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2476 RXFIFO_THR2_FULL);
2477 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2478 RXFIFO_THR3_FULL);
2479 } else {
2480 /* rx share fifo credit near full threshold */
2481 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2482 RXFIFO_THR2_HIGH);
2483 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2484 RXFIFO_THR3_HIGH);
2485 }
2486
2487 /* TX share fifo free credit full threshold */
2488 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2489
2490 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002491 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002492 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2493 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2494
hayeswangc5554292014-09-12 10:43:11 +08002495 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00002496
2497 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2498
2499 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2500 ocp_data |= TCR0_AUTO_FIFO;
2501 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2502}
2503
2504static void r8152b_enter_oob(struct r8152 *tp)
2505{
hayeswang45f4a192014-01-06 17:08:41 +08002506 u32 ocp_data;
2507 int i;
hayeswangac718b62013-05-02 16:01:25 +00002508
2509 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2510 ocp_data &= ~NOW_IS_OOB;
2511 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2512
2513 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2514 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2515 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2516
hayeswangd70b1132014-09-19 15:17:18 +08002517 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002518
2519 for (i = 0; i < 1000; i++) {
2520 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2521 if (ocp_data & LINK_LIST_READY)
2522 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002523 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002524 }
2525
2526 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2527 ocp_data |= RE_INIT_LL;
2528 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2529
2530 for (i = 0; i < 1000; i++) {
2531 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2532 if (ocp_data & LINK_LIST_READY)
2533 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002534 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002535 }
2536
2537 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2538
hayeswangc5554292014-09-12 10:43:11 +08002539 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002540
2541 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2542 ocp_data |= ALDPS_PROXY_MODE;
2543 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2544
2545 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2546 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2547 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2548
hayeswang00a5e362014-02-18 21:48:59 +08002549 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002550
2551 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2552 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2553 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2554}
2555
hayeswang43779f82014-01-02 11:25:10 +08002556static void r8153_hw_phy_cfg(struct r8152 *tp)
2557{
2558 u32 ocp_data;
2559 u16 data;
2560
2561 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002562 data = r8152_mdio_read(tp, MII_BMCR);
2563 if (data & BMCR_PDOWN) {
2564 data &= ~BMCR_PDOWN;
2565 r8152_mdio_write(tp, MII_BMCR, data);
2566 }
hayeswang43779f82014-01-02 11:25:10 +08002567
2568 if (tp->version == RTL_VER_03) {
2569 data = ocp_reg_read(tp, OCP_EEE_CFG);
2570 data &= ~CTAP_SHORT_EN;
2571 ocp_reg_write(tp, OCP_EEE_CFG, data);
2572 }
2573
2574 data = ocp_reg_read(tp, OCP_POWER_CFG);
2575 data |= EEE_CLKDIV_EN;
2576 ocp_reg_write(tp, OCP_POWER_CFG, data);
2577
2578 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2579 data |= EN_10M_BGOFF;
2580 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2581 data = ocp_reg_read(tp, OCP_POWER_CFG);
2582 data |= EN_10M_PLLOFF;
2583 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08002584 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08002585
2586 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2587 ocp_data |= PFM_PWM_SWITCH;
2588 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2589
hayeswangb4d99de2015-01-19 17:02:46 +08002590 /* Enable LPF corner auto tune */
2591 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08002592
hayeswangb4d99de2015-01-19 17:02:46 +08002593 /* Adjust 10M Amplitude */
2594 sram_write(tp, SRAM_10M_AMP1, 0x00af);
2595 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08002596
2597 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002598}
2599
hayeswangb9702722014-02-18 21:49:00 +08002600static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002601{
2602 u8 u1u2[8];
2603
2604 if (enable)
2605 memset(u1u2, 0xff, sizeof(u1u2));
2606 else
2607 memset(u1u2, 0x00, sizeof(u1u2));
2608
2609 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2610}
2611
hayeswangb9702722014-02-18 21:49:00 +08002612static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002613{
2614 u32 ocp_data;
2615
2616 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2617 if (enable)
2618 ocp_data |= U2P3_ENABLE;
2619 else
2620 ocp_data &= ~U2P3_ENABLE;
2621 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2622}
2623
hayeswangb9702722014-02-18 21:49:00 +08002624static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002625{
2626 u32 ocp_data;
2627
2628 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2629 if (enable)
2630 ocp_data |= PWR_EN | PHASE2_EN;
2631 else
2632 ocp_data &= ~(PWR_EN | PHASE2_EN);
2633 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2634
2635 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2636 ocp_data &= ~PCUT_STATUS;
2637 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2638}
2639
hayeswang43779f82014-01-02 11:25:10 +08002640static void r8153_first_init(struct r8152 *tp)
2641{
2642 u32 ocp_data;
2643 int i;
2644
hayeswang00a5e362014-02-18 21:48:59 +08002645 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002646 r8153_teredo_off(tp);
2647
2648 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2649 ocp_data &= ~RCR_ACPT_ALL;
2650 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2651
2652 r8153_hw_phy_cfg(tp);
2653
2654 rtl8152_nic_reset(tp);
2655
2656 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2657 ocp_data &= ~NOW_IS_OOB;
2658 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2659
2660 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2661 ocp_data &= ~MCU_BORW_EN;
2662 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2663
2664 for (i = 0; i < 1000; i++) {
2665 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2666 if (ocp_data & LINK_LIST_READY)
2667 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002668 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002669 }
2670
2671 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2672 ocp_data |= RE_INIT_LL;
2673 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2674
2675 for (i = 0; i < 1000; i++) {
2676 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2677 if (ocp_data & LINK_LIST_READY)
2678 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002679 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002680 }
2681
hayeswangc5554292014-09-12 10:43:11 +08002682 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08002683
hayeswang69b4b7a2014-07-10 10:58:54 +08002684 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
2685 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08002686
2687 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2688 ocp_data |= TCR0_AUTO_FIFO;
2689 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2690
2691 rtl8152_nic_reset(tp);
2692
2693 /* rx share fifo credit full threshold */
2694 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2695 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2696 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2697 /* TX share fifo free credit full threshold */
2698 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2699
hayeswang9629e3c2014-01-15 10:42:15 +08002700 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002701 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2702 ocp_data &= ~RX_AGG_DISABLE;
2703 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2704}
2705
2706static void r8153_enter_oob(struct r8152 *tp)
2707{
2708 u32 ocp_data;
2709 int i;
2710
2711 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2712 ocp_data &= ~NOW_IS_OOB;
2713 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2714
hayeswangd70b1132014-09-19 15:17:18 +08002715 rtl_disable(tp);
hayeswang43779f82014-01-02 11:25:10 +08002716
2717 for (i = 0; i < 1000; i++) {
2718 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2719 if (ocp_data & LINK_LIST_READY)
2720 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002721 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002722 }
2723
2724 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2725 ocp_data |= RE_INIT_LL;
2726 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2727
2728 for (i = 0; i < 1000; i++) {
2729 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2730 if (ocp_data & LINK_LIST_READY)
2731 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002732 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002733 }
2734
hayeswang69b4b7a2014-07-10 10:58:54 +08002735 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
hayeswang43779f82014-01-02 11:25:10 +08002736
hayeswang43779f82014-01-02 11:25:10 +08002737 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2738 ocp_data &= ~TEREDO_WAKE_MASK;
2739 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2740
hayeswangc5554292014-09-12 10:43:11 +08002741 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002742
2743 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2744 ocp_data |= ALDPS_PROXY_MODE;
2745 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2746
2747 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2748 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2749 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2750
hayeswang00a5e362014-02-18 21:48:59 +08002751 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002752
2753 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2754 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2755 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2756}
2757
2758static void r8153_disable_aldps(struct r8152 *tp)
2759{
2760 u16 data;
2761
2762 data = ocp_reg_read(tp, OCP_POWER_CFG);
2763 data &= ~EN_ALDPS;
2764 ocp_reg_write(tp, OCP_POWER_CFG, data);
2765 msleep(20);
2766}
2767
2768static void r8153_enable_aldps(struct r8152 *tp)
2769{
2770 u16 data;
2771
2772 data = ocp_reg_read(tp, OCP_POWER_CFG);
2773 data |= EN_ALDPS;
2774 ocp_reg_write(tp, OCP_POWER_CFG, data);
2775}
2776
hayeswangd70b1132014-09-19 15:17:18 +08002777static void rtl8153_disable(struct r8152 *tp)
2778{
2779 r8153_disable_aldps(tp);
2780 rtl_disable(tp);
2781 r8153_enable_aldps(tp);
2782}
2783
hayeswangac718b62013-05-02 16:01:25 +00002784static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2785{
hayeswang43779f82014-01-02 11:25:10 +08002786 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002787 int ret = 0;
2788
2789 cancel_delayed_work_sync(&tp->schedule);
2790 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2791 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2792 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002793 if (tp->mii.supports_gmii) {
2794 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2795 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2796 } else {
2797 gbcr = 0;
2798 }
hayeswangac718b62013-05-02 16:01:25 +00002799
2800 if (autoneg == AUTONEG_DISABLE) {
2801 if (speed == SPEED_10) {
2802 bmcr = 0;
2803 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2804 } else if (speed == SPEED_100) {
2805 bmcr = BMCR_SPEED100;
2806 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002807 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2808 bmcr = BMCR_SPEED1000;
2809 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002810 } else {
2811 ret = -EINVAL;
2812 goto out;
2813 }
2814
2815 if (duplex == DUPLEX_FULL)
2816 bmcr |= BMCR_FULLDPLX;
2817 } else {
2818 if (speed == SPEED_10) {
2819 if (duplex == DUPLEX_FULL)
2820 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2821 else
2822 anar |= ADVERTISE_10HALF;
2823 } else if (speed == SPEED_100) {
2824 if (duplex == DUPLEX_FULL) {
2825 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2826 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2827 } else {
2828 anar |= ADVERTISE_10HALF;
2829 anar |= ADVERTISE_100HALF;
2830 }
hayeswang43779f82014-01-02 11:25:10 +08002831 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2832 if (duplex == DUPLEX_FULL) {
2833 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2834 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2835 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2836 } else {
2837 anar |= ADVERTISE_10HALF;
2838 anar |= ADVERTISE_100HALF;
2839 gbcr |= ADVERTISE_1000HALF;
2840 }
hayeswangac718b62013-05-02 16:01:25 +00002841 } else {
2842 ret = -EINVAL;
2843 goto out;
2844 }
2845
2846 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2847 }
2848
hayeswangaa66a5f2014-02-18 21:49:04 +08002849 if (test_bit(PHY_RESET, &tp->flags))
2850 bmcr |= BMCR_RESET;
2851
hayeswang43779f82014-01-02 11:25:10 +08002852 if (tp->mii.supports_gmii)
2853 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2854
hayeswangac718b62013-05-02 16:01:25 +00002855 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2856 r8152_mdio_write(tp, MII_BMCR, bmcr);
2857
hayeswangaa66a5f2014-02-18 21:49:04 +08002858 if (test_bit(PHY_RESET, &tp->flags)) {
2859 int i;
2860
2861 clear_bit(PHY_RESET, &tp->flags);
2862 for (i = 0; i < 50; i++) {
2863 msleep(20);
2864 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2865 break;
2866 }
2867 }
2868
hayeswangac718b62013-05-02 16:01:25 +00002869out:
hayeswangac718b62013-05-02 16:01:25 +00002870
2871 return ret;
2872}
2873
hayeswangd70b1132014-09-19 15:17:18 +08002874static void rtl8152_up(struct r8152 *tp)
2875{
2876 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2877 return;
2878
2879 r8152b_disable_aldps(tp);
2880 r8152b_exit_oob(tp);
2881 r8152b_enable_aldps(tp);
2882}
2883
hayeswangac718b62013-05-02 16:01:25 +00002884static void rtl8152_down(struct r8152 *tp)
2885{
hayeswang68714382014-04-11 17:54:31 +08002886 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2887 rtl_drop_queued_tx(tp);
2888 return;
2889 }
2890
hayeswang00a5e362014-02-18 21:48:59 +08002891 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002892 r8152b_disable_aldps(tp);
2893 r8152b_enter_oob(tp);
2894 r8152b_enable_aldps(tp);
2895}
2896
hayeswangd70b1132014-09-19 15:17:18 +08002897static void rtl8153_up(struct r8152 *tp)
2898{
2899 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2900 return;
2901
2902 r8153_disable_aldps(tp);
2903 r8153_first_init(tp);
2904 r8153_enable_aldps(tp);
2905}
2906
hayeswang43779f82014-01-02 11:25:10 +08002907static void rtl8153_down(struct r8152 *tp)
2908{
hayeswang68714382014-04-11 17:54:31 +08002909 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2910 rtl_drop_queued_tx(tp);
2911 return;
2912 }
2913
hayeswangb9702722014-02-18 21:49:00 +08002914 r8153_u1u2en(tp, false);
2915 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002916 r8153_disable_aldps(tp);
2917 r8153_enter_oob(tp);
2918 r8153_enable_aldps(tp);
2919}
2920
hayeswangac718b62013-05-02 16:01:25 +00002921static void set_carrier(struct r8152 *tp)
2922{
2923 struct net_device *netdev = tp->netdev;
2924 u8 speed;
2925
hayeswang40a82912013-08-14 20:54:40 +08002926 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002927 speed = rtl8152_get_speed(tp);
2928
2929 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08002930 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08002931 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002932 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2933 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08002934 rtl_start_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002935 }
2936 } else {
hayeswang51d979f2015-02-06 11:30:47 +08002937 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002938 netif_carrier_off(netdev);
hayeswangd823ab62015-01-12 12:06:23 +08002939 napi_disable(&tp->napi);
hayeswangc81229c2014-01-02 11:22:42 +08002940 tp->rtl_ops.disable(tp);
hayeswangd823ab62015-01-12 12:06:23 +08002941 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00002942 }
2943 }
hayeswangac718b62013-05-02 16:01:25 +00002944}
2945
2946static void rtl_work_func_t(struct work_struct *work)
2947{
2948 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2949
hayeswanga1f83fe2014-11-12 10:05:05 +08002950 /* If the device is unplugged or !netif_running(), the workqueue
2951 * doesn't need to wake the device, and could return directly.
2952 */
2953 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
2954 return;
2955
hayeswang9a4be1b2014-02-18 21:49:07 +08002956 if (usb_autopm_get_interface(tp->intf) < 0)
2957 return;
2958
hayeswangac718b62013-05-02 16:01:25 +00002959 if (!test_bit(WORK_ENABLE, &tp->flags))
2960 goto out1;
2961
hayeswangb5403272014-10-09 18:00:26 +08002962 if (!mutex_trylock(&tp->control)) {
2963 schedule_delayed_work(&tp->schedule, 0);
2964 goto out1;
2965 }
2966
hayeswang40a82912013-08-14 20:54:40 +08002967 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2968 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002969
2970 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2971 _rtl8152_set_rx_mode(tp->netdev);
2972
hayeswangd823ab62015-01-12 12:06:23 +08002973 /* don't schedule napi before linking */
2974 if (test_bit(SCHEDULE_NAPI, &tp->flags) &&
hayeswang51d979f2015-02-06 11:30:47 +08002975 netif_carrier_ok(tp->netdev)) {
hayeswangd823ab62015-01-12 12:06:23 +08002976 clear_bit(SCHEDULE_NAPI, &tp->flags);
2977 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002978 }
hayeswangaa66a5f2014-02-18 21:49:04 +08002979
2980 if (test_bit(PHY_RESET, &tp->flags))
2981 rtl_phy_reset(tp);
2982
hayeswangb5403272014-10-09 18:00:26 +08002983 mutex_unlock(&tp->control);
2984
hayeswangac718b62013-05-02 16:01:25 +00002985out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08002986 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002987}
2988
2989static int rtl8152_open(struct net_device *netdev)
2990{
2991 struct r8152 *tp = netdev_priv(netdev);
2992 int res = 0;
2993
hayeswang7e9da482014-02-18 21:49:05 +08002994 res = alloc_all_mem(tp);
2995 if (res)
2996 goto out;
2997
hayeswang51d979f2015-02-06 11:30:47 +08002998 netif_carrier_off(netdev);
hayeswangf4c74762014-10-29 11:12:16 +08002999
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. */
hayeswang51d979f2015-02-06 11:30:47 +08003015 if (netif_carrier_ok(netdev))
hayeswang9a4be1b2014-02-18 21:49:07 +08003016 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 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003025 netif_start_queue(netdev);
3026 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08003027
hayeswang3d55f442014-02-06 11:55:48 +08003028 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3029 if (res) {
3030 if (res == -ENODEV)
3031 netif_device_detach(tp->netdev);
3032 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3033 res);
hayeswang7e9da482014-02-18 21:49:05 +08003034 free_all_mem(tp);
hayeswang93ffbea2014-11-05 10:17:02 +08003035 } else {
hayeswangd823ab62015-01-12 12:06:23 +08003036 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08003037 }
3038
hayeswangb5403272014-10-09 18:00:26 +08003039 mutex_unlock(&tp->control);
3040
hayeswang9a4be1b2014-02-18 21:49:07 +08003041 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003042
hayeswang7e9da482014-02-18 21:49:05 +08003043out:
hayeswangac718b62013-05-02 16:01:25 +00003044 return res;
3045}
3046
3047static int rtl8152_close(struct net_device *netdev)
3048{
3049 struct r8152 *tp = netdev_priv(netdev);
3050 int res = 0;
3051
hayeswangd823ab62015-01-12 12:06:23 +08003052 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003053 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08003054 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00003055 cancel_delayed_work_sync(&tp->schedule);
3056 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003057
3058 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08003059 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003060 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08003061 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003062 } else {
hayeswangb5403272014-10-09 18:00:26 +08003063 mutex_lock(&tp->control);
3064
hayeswangb209af92014-08-25 15:53:00 +08003065 /* The autosuspend may have been enabled and wouldn't
hayeswang9a4be1b2014-02-18 21:49:07 +08003066 * be disable when autoresume occurs, because the
3067 * netif_running() would be false.
3068 */
hayeswang923e1ee2014-10-29 11:12:15 +08003069 rtl_runtime_suspend_enable(tp, false);
hayeswang9a4be1b2014-02-18 21:49:07 +08003070
hayeswang9a4be1b2014-02-18 21:49:07 +08003071 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08003072
3073 mutex_unlock(&tp->control);
3074
hayeswang9a4be1b2014-02-18 21:49:07 +08003075 usb_autopm_put_interface(tp->intf);
3076 }
hayeswangac718b62013-05-02 16:01:25 +00003077
hayeswang7e9da482014-02-18 21:49:05 +08003078 free_all_mem(tp);
3079
hayeswangac718b62013-05-02 16:01:25 +00003080 return res;
3081}
3082
hayeswangd24f6132014-09-25 20:54:01 +08003083static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
hayeswangac718b62013-05-02 16:01:25 +00003084{
hayeswangd24f6132014-09-25 20:54:01 +08003085 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3086 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3087 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3088}
3089
3090static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3091{
3092 u16 data;
3093
3094 r8152_mmd_indirect(tp, dev, reg);
3095 data = ocp_reg_read(tp, OCP_EEE_DATA);
3096 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3097
3098 return data;
3099}
3100
3101static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3102{
3103 r8152_mmd_indirect(tp, dev, reg);
3104 ocp_reg_write(tp, OCP_EEE_DATA, data);
3105 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3106}
3107
3108static void r8152_eee_en(struct r8152 *tp, bool enable)
3109{
3110 u16 config1, config2, config3;
hayeswang45f4a192014-01-06 17:08:41 +08003111 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003112
3113 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
hayeswangd24f6132014-09-25 20:54:01 +08003114 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3115 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3116 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3117
3118 if (enable) {
3119 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3120 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3121 config1 |= sd_rise_time(1);
3122 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3123 config3 |= fast_snr(42);
3124 } else {
3125 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3126 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3127 RX_QUIET_EN);
3128 config1 |= sd_rise_time(7);
3129 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3130 config3 |= fast_snr(511);
3131 }
3132
hayeswangac718b62013-05-02 16:01:25 +00003133 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
hayeswangd24f6132014-09-25 20:54:01 +08003134 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3135 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3136 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3137}
3138
3139static void r8152b_enable_eee(struct r8152 *tp)
3140{
3141 r8152_eee_en(tp, true);
3142 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3143}
3144
3145static void r8153_eee_en(struct r8152 *tp, bool enable)
3146{
3147 u32 ocp_data;
3148 u16 config;
3149
3150 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3151 config = ocp_reg_read(tp, OCP_EEE_CFG);
3152
3153 if (enable) {
3154 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3155 config |= EEE10_EN;
3156 } else {
3157 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3158 config &= ~EEE10_EN;
3159 }
3160
3161 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3162 ocp_reg_write(tp, OCP_EEE_CFG, config);
hayeswangac718b62013-05-02 16:01:25 +00003163}
3164
hayeswang43779f82014-01-02 11:25:10 +08003165static void r8153_enable_eee(struct r8152 *tp)
3166{
hayeswangd24f6132014-09-25 20:54:01 +08003167 r8153_eee_en(tp, true);
3168 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
hayeswang43779f82014-01-02 11:25:10 +08003169}
3170
hayeswangac718b62013-05-02 16:01:25 +00003171static void r8152b_enable_fc(struct r8152 *tp)
3172{
3173 u16 anar;
3174
3175 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3176 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3177 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3178}
3179
hayeswang4f1d4d52014-03-11 16:24:19 +08003180static void rtl_tally_reset(struct r8152 *tp)
3181{
3182 u32 ocp_data;
3183
3184 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
3185 ocp_data |= TALLY_RESET;
3186 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
3187}
3188
hayeswangac718b62013-05-02 16:01:25 +00003189static void r8152b_init(struct r8152 *tp)
3190{
hayeswangebc2ec42013-08-14 20:54:38 +08003191 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003192
hayeswang68714382014-04-11 17:54:31 +08003193 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3194 return;
3195
hayeswangd70b1132014-09-19 15:17:18 +08003196 r8152b_disable_aldps(tp);
3197
hayeswangac718b62013-05-02 16:01:25 +00003198 if (tp->version == RTL_VER_01) {
3199 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3200 ocp_data &= ~LED_MODE_MASK;
3201 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3202 }
3203
hayeswang00a5e362014-02-18 21:48:59 +08003204 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003205
hayeswangac718b62013-05-02 16:01:25 +00003206 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3207 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
3208 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3209 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
3210 ocp_data &= ~MCU_CLK_RATIO_MASK;
3211 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
3212 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
3213 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
3214 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
3215 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
3216
3217 r8152b_enable_eee(tp);
3218 r8152b_enable_aldps(tp);
3219 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003220 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00003221
hayeswangebc2ec42013-08-14 20:54:38 +08003222 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00003223 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec42013-08-14 20:54:38 +08003224 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00003225 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
3226}
3227
hayeswang43779f82014-01-02 11:25:10 +08003228static void r8153_init(struct r8152 *tp)
3229{
3230 u32 ocp_data;
3231 int i;
3232
hayeswang68714382014-04-11 17:54:31 +08003233 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3234 return;
3235
hayeswangd70b1132014-09-19 15:17:18 +08003236 r8153_disable_aldps(tp);
hayeswangb9702722014-02-18 21:49:00 +08003237 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003238
3239 for (i = 0; i < 500; i++) {
3240 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3241 AUTOLOAD_DONE)
3242 break;
3243 msleep(20);
3244 }
3245
3246 for (i = 0; i < 500; i++) {
3247 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
3248 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
3249 break;
3250 msleep(20);
3251 }
3252
hayeswangb9702722014-02-18 21:49:00 +08003253 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003254
hayeswang65bab842015-02-12 16:20:46 +08003255 if (tp->version == RTL_VER_04) {
3256 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
3257 ocp_data &= ~pwd_dn_scale_mask;
3258 ocp_data |= pwd_dn_scale(96);
3259 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
3260
3261 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
3262 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
3263 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
3264 } else if (tp->version == RTL_VER_05) {
3265 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
3266 ocp_data &= ~ECM_ALDPS;
3267 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
3268
3269 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
3270 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
3271 ocp_data &= ~DYNAMIC_BURST;
3272 else
3273 ocp_data |= DYNAMIC_BURST;
3274 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
3275 }
3276
3277 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
3278 ocp_data |= EP4_FULL_FC;
3279 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
3280
hayeswang43779f82014-01-02 11:25:10 +08003281 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
3282 ocp_data &= ~TIMER11_EN;
3283 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
3284
hayeswang43779f82014-01-02 11:25:10 +08003285 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3286 ocp_data &= ~LED_MODE_MASK;
3287 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3288
hayeswang65bab842015-02-12 16:20:46 +08003289 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
hayeswang34203e22015-02-06 11:30:46 +08003290 if (tp->version == RTL_VER_04 && tp->udev->speed != USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08003291 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08003292 else
3293 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08003294 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
3295
3296 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
3297 ocp_data &= ~SEN_VAL_MASK;
3298 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
3299 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
3300
hayeswang65bab842015-02-12 16:20:46 +08003301 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
3302
hayeswangb9702722014-02-18 21:49:00 +08003303 r8153_power_cut_en(tp, false);
3304 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003305
hayeswang43779f82014-01-02 11:25:10 +08003306 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
3307 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
3308 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3309 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3310 U1U2_SPDWN_EN | L1_SPDWN_EN);
3311 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3312 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3313 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
3314 EEE_SPDWN_EN);
3315
3316 r8153_enable_eee(tp);
3317 r8153_enable_aldps(tp);
3318 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003319 rtl_tally_reset(tp);
hayeswang43779f82014-01-02 11:25:10 +08003320}
3321
hayeswangac718b62013-05-02 16:01:25 +00003322static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
3323{
3324 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang6cc69f22014-10-17 16:55:08 +08003325 struct net_device *netdev = tp->netdev;
3326 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00003327
hayeswangb5403272014-10-09 18:00:26 +08003328 mutex_lock(&tp->control);
3329
hayeswang6cc69f22014-10-17 16:55:08 +08003330 if (PMSG_IS_AUTO(message)) {
3331 if (netif_running(netdev) && work_busy(&tp->schedule.work)) {
3332 ret = -EBUSY;
3333 goto out1;
3334 }
hayeswangac718b62013-05-02 16:01:25 +00003335
hayeswang6cc69f22014-10-17 16:55:08 +08003336 set_bit(SELECTIVE_SUSPEND, &tp->flags);
3337 } else {
3338 netif_device_detach(netdev);
3339 }
3340
hayeswange3bd1a82014-10-29 11:12:17 +08003341 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangac718b62013-05-02 16:01:25 +00003342 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08003343 usb_kill_urb(tp->intr_urb);
hayeswangd823ab62015-01-12 12:06:23 +08003344 napi_disable(&tp->napi);
hayeswang9a4be1b2014-02-18 21:49:07 +08003345 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswang445f7f42014-09-23 16:31:47 +08003346 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003347 rtl_runtime_suspend_enable(tp, true);
3348 } else {
hayeswang6cc69f22014-10-17 16:55:08 +08003349 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08003350 tp->rtl_ops.down(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003351 }
hayeswangd823ab62015-01-12 12:06:23 +08003352 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003353 }
hayeswang6cc69f22014-10-17 16:55:08 +08003354out1:
hayeswangb5403272014-10-09 18:00:26 +08003355 mutex_unlock(&tp->control);
3356
hayeswang6cc69f22014-10-17 16:55:08 +08003357 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003358}
3359
3360static int rtl8152_resume(struct usb_interface *intf)
3361{
3362 struct r8152 *tp = usb_get_intfdata(intf);
3363
hayeswangb5403272014-10-09 18:00:26 +08003364 mutex_lock(&tp->control);
3365
hayeswang9a4be1b2014-02-18 21:49:07 +08003366 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3367 tp->rtl_ops.init(tp);
3368 netif_device_attach(tp->netdev);
3369 }
3370
hayeswangac718b62013-05-02 16:01:25 +00003371 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003372 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3373 rtl_runtime_suspend_enable(tp, false);
3374 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswang445f7f42014-09-23 16:31:47 +08003375 set_bit(WORK_ENABLE, &tp->flags);
hayeswang51d979f2015-02-06 11:30:47 +08003376 if (netif_carrier_ok(tp->netdev))
hayeswang445f7f42014-09-23 16:31:47 +08003377 rtl_start_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003378 } else {
3379 tp->rtl_ops.up(tp);
3380 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswangb209af92014-08-25 15:53:00 +08003381 tp->mii.supports_gmii ?
3382 SPEED_1000 : SPEED_100,
3383 DUPLEX_FULL);
hayeswang445f7f42014-09-23 16:31:47 +08003384 netif_carrier_off(tp->netdev);
3385 set_bit(WORK_ENABLE, &tp->flags);
hayeswang9a4be1b2014-02-18 21:49:07 +08003386 }
hayeswang40a82912013-08-14 20:54:40 +08003387 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswang923e1ee2014-10-29 11:12:15 +08003388 } else if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3389 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00003390 }
3391
hayeswangb5403272014-10-09 18:00:26 +08003392 mutex_unlock(&tp->control);
3393
hayeswangac718b62013-05-02 16:01:25 +00003394 return 0;
3395}
3396
hayeswang21ff2e82014-02-18 21:49:06 +08003397static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3398{
3399 struct r8152 *tp = netdev_priv(dev);
3400
hayeswang9a4be1b2014-02-18 21:49:07 +08003401 if (usb_autopm_get_interface(tp->intf) < 0)
3402 return;
3403
hayeswangb5403272014-10-09 18:00:26 +08003404 mutex_lock(&tp->control);
3405
hayeswang21ff2e82014-02-18 21:49:06 +08003406 wol->supported = WAKE_ANY;
3407 wol->wolopts = __rtl_get_wol(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003408
hayeswangb5403272014-10-09 18:00:26 +08003409 mutex_unlock(&tp->control);
3410
hayeswang9a4be1b2014-02-18 21:49:07 +08003411 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08003412}
3413
3414static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3415{
3416 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003417 int ret;
3418
3419 ret = usb_autopm_get_interface(tp->intf);
3420 if (ret < 0)
3421 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08003422
hayeswangb5403272014-10-09 18:00:26 +08003423 mutex_lock(&tp->control);
3424
hayeswang21ff2e82014-02-18 21:49:06 +08003425 __rtl_set_wol(tp, wol->wolopts);
3426 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
3427
hayeswangb5403272014-10-09 18:00:26 +08003428 mutex_unlock(&tp->control);
3429
hayeswang9a4be1b2014-02-18 21:49:07 +08003430 usb_autopm_put_interface(tp->intf);
3431
3432out_set_wol:
3433 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08003434}
3435
hayeswanga5ec27c2014-02-18 21:49:11 +08003436static u32 rtl8152_get_msglevel(struct net_device *dev)
3437{
3438 struct r8152 *tp = netdev_priv(dev);
3439
3440 return tp->msg_enable;
3441}
3442
3443static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
3444{
3445 struct r8152 *tp = netdev_priv(dev);
3446
3447 tp->msg_enable = value;
3448}
3449
hayeswangac718b62013-05-02 16:01:25 +00003450static void rtl8152_get_drvinfo(struct net_device *netdev,
3451 struct ethtool_drvinfo *info)
3452{
3453 struct r8152 *tp = netdev_priv(netdev);
3454
hayeswangb0b46c72014-08-26 10:08:23 +08003455 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
3456 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00003457 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
3458}
3459
3460static
3461int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
3462{
3463 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08003464 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003465
3466 if (!tp->mii.mdio_read)
3467 return -EOPNOTSUPP;
3468
hayeswang8d4a4d72014-10-09 18:00:25 +08003469 ret = usb_autopm_get_interface(tp->intf);
3470 if (ret < 0)
3471 goto out;
3472
hayeswangb5403272014-10-09 18:00:26 +08003473 mutex_lock(&tp->control);
3474
hayeswang8d4a4d72014-10-09 18:00:25 +08003475 ret = mii_ethtool_gset(&tp->mii, cmd);
3476
hayeswangb5403272014-10-09 18:00:26 +08003477 mutex_unlock(&tp->control);
3478
hayeswang8d4a4d72014-10-09 18:00:25 +08003479 usb_autopm_put_interface(tp->intf);
3480
3481out:
3482 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003483}
3484
3485static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3486{
3487 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003488 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003489
hayeswang9a4be1b2014-02-18 21:49:07 +08003490 ret = usb_autopm_get_interface(tp->intf);
3491 if (ret < 0)
3492 goto out;
3493
hayeswangb5403272014-10-09 18:00:26 +08003494 mutex_lock(&tp->control);
3495
hayeswang9a4be1b2014-02-18 21:49:07 +08003496 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
3497
hayeswangb5403272014-10-09 18:00:26 +08003498 mutex_unlock(&tp->control);
3499
hayeswang9a4be1b2014-02-18 21:49:07 +08003500 usb_autopm_put_interface(tp->intf);
3501
3502out:
3503 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003504}
3505
hayeswang4f1d4d52014-03-11 16:24:19 +08003506static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
3507 "tx_packets",
3508 "rx_packets",
3509 "tx_errors",
3510 "rx_errors",
3511 "rx_missed",
3512 "align_errors",
3513 "tx_single_collisions",
3514 "tx_multi_collisions",
3515 "rx_unicast",
3516 "rx_broadcast",
3517 "rx_multicast",
3518 "tx_aborted",
3519 "tx_underrun",
3520};
3521
3522static int rtl8152_get_sset_count(struct net_device *dev, int sset)
3523{
3524 switch (sset) {
3525 case ETH_SS_STATS:
3526 return ARRAY_SIZE(rtl8152_gstrings);
3527 default:
3528 return -EOPNOTSUPP;
3529 }
3530}
3531
3532static void rtl8152_get_ethtool_stats(struct net_device *dev,
3533 struct ethtool_stats *stats, u64 *data)
3534{
3535 struct r8152 *tp = netdev_priv(dev);
3536 struct tally_counter tally;
3537
hayeswang0b030242014-07-08 14:49:28 +08003538 if (usb_autopm_get_interface(tp->intf) < 0)
3539 return;
3540
hayeswang4f1d4d52014-03-11 16:24:19 +08003541 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
3542
hayeswang0b030242014-07-08 14:49:28 +08003543 usb_autopm_put_interface(tp->intf);
3544
hayeswang4f1d4d52014-03-11 16:24:19 +08003545 data[0] = le64_to_cpu(tally.tx_packets);
3546 data[1] = le64_to_cpu(tally.rx_packets);
3547 data[2] = le64_to_cpu(tally.tx_errors);
3548 data[3] = le32_to_cpu(tally.rx_errors);
3549 data[4] = le16_to_cpu(tally.rx_missed);
3550 data[5] = le16_to_cpu(tally.align_errors);
3551 data[6] = le32_to_cpu(tally.tx_one_collision);
3552 data[7] = le32_to_cpu(tally.tx_multi_collision);
3553 data[8] = le64_to_cpu(tally.rx_unicast);
3554 data[9] = le64_to_cpu(tally.rx_broadcast);
3555 data[10] = le32_to_cpu(tally.rx_multicast);
3556 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08003557 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08003558}
3559
3560static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3561{
3562 switch (stringset) {
3563 case ETH_SS_STATS:
3564 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
3565 break;
3566 }
3567}
3568
hayeswangdf35d282014-09-25 20:54:02 +08003569static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3570{
3571 u32 ocp_data, lp, adv, supported = 0;
3572 u16 val;
3573
3574 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
3575 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3576
3577 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
3578 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3579
3580 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
3581 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3582
3583 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3584 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3585
3586 eee->eee_enabled = !!ocp_data;
3587 eee->eee_active = !!(supported & adv & lp);
3588 eee->supported = supported;
3589 eee->advertised = adv;
3590 eee->lp_advertised = lp;
3591
3592 return 0;
3593}
3594
3595static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3596{
3597 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3598
3599 r8152_eee_en(tp, eee->eee_enabled);
3600
3601 if (!eee->eee_enabled)
3602 val = 0;
3603
3604 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
3605
3606 return 0;
3607}
3608
3609static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3610{
3611 u32 ocp_data, lp, adv, supported = 0;
3612 u16 val;
3613
3614 val = ocp_reg_read(tp, OCP_EEE_ABLE);
3615 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3616
3617 val = ocp_reg_read(tp, OCP_EEE_ADV);
3618 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3619
3620 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
3621 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3622
3623 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3624 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3625
3626 eee->eee_enabled = !!ocp_data;
3627 eee->eee_active = !!(supported & adv & lp);
3628 eee->supported = supported;
3629 eee->advertised = adv;
3630 eee->lp_advertised = lp;
3631
3632 return 0;
3633}
3634
3635static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3636{
3637 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3638
3639 r8153_eee_en(tp, eee->eee_enabled);
3640
3641 if (!eee->eee_enabled)
3642 val = 0;
3643
3644 ocp_reg_write(tp, OCP_EEE_ADV, val);
3645
3646 return 0;
3647}
3648
3649static int
3650rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
3651{
3652 struct r8152 *tp = netdev_priv(net);
3653 int ret;
3654
3655 ret = usb_autopm_get_interface(tp->intf);
3656 if (ret < 0)
3657 goto out;
3658
hayeswangb5403272014-10-09 18:00:26 +08003659 mutex_lock(&tp->control);
3660
hayeswangdf35d282014-09-25 20:54:02 +08003661 ret = tp->rtl_ops.eee_get(tp, edata);
3662
hayeswangb5403272014-10-09 18:00:26 +08003663 mutex_unlock(&tp->control);
3664
hayeswangdf35d282014-09-25 20:54:02 +08003665 usb_autopm_put_interface(tp->intf);
3666
3667out:
3668 return ret;
3669}
3670
3671static int
3672rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
3673{
3674 struct r8152 *tp = netdev_priv(net);
3675 int ret;
3676
3677 ret = usb_autopm_get_interface(tp->intf);
3678 if (ret < 0)
3679 goto out;
3680
hayeswangb5403272014-10-09 18:00:26 +08003681 mutex_lock(&tp->control);
3682
hayeswangdf35d282014-09-25 20:54:02 +08003683 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08003684 if (!ret)
3685 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08003686
hayeswangb5403272014-10-09 18:00:26 +08003687 mutex_unlock(&tp->control);
3688
hayeswangdf35d282014-09-25 20:54:02 +08003689 usb_autopm_put_interface(tp->intf);
3690
3691out:
3692 return ret;
3693}
3694
hayeswang8884f502014-10-28 14:05:52 +08003695static int rtl8152_nway_reset(struct net_device *dev)
3696{
3697 struct r8152 *tp = netdev_priv(dev);
3698 int ret;
3699
3700 ret = usb_autopm_get_interface(tp->intf);
3701 if (ret < 0)
3702 goto out;
3703
3704 mutex_lock(&tp->control);
3705
3706 ret = mii_nway_restart(&tp->mii);
3707
3708 mutex_unlock(&tp->control);
3709
3710 usb_autopm_put_interface(tp->intf);
3711
3712out:
3713 return ret;
3714}
3715
hayeswangac718b62013-05-02 16:01:25 +00003716static struct ethtool_ops ops = {
3717 .get_drvinfo = rtl8152_get_drvinfo,
3718 .get_settings = rtl8152_get_settings,
3719 .set_settings = rtl8152_set_settings,
3720 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08003721 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08003722 .get_msglevel = rtl8152_get_msglevel,
3723 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08003724 .get_wol = rtl8152_get_wol,
3725 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08003726 .get_strings = rtl8152_get_strings,
3727 .get_sset_count = rtl8152_get_sset_count,
3728 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangdf35d282014-09-25 20:54:02 +08003729 .get_eee = rtl_ethtool_get_eee,
3730 .set_eee = rtl_ethtool_set_eee,
hayeswangac718b62013-05-02 16:01:25 +00003731};
3732
3733static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
3734{
3735 struct r8152 *tp = netdev_priv(netdev);
3736 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08003737 int res;
3738
hayeswang68714382014-04-11 17:54:31 +08003739 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3740 return -ENODEV;
3741
hayeswang9a4be1b2014-02-18 21:49:07 +08003742 res = usb_autopm_get_interface(tp->intf);
3743 if (res < 0)
3744 goto out;
hayeswangac718b62013-05-02 16:01:25 +00003745
3746 switch (cmd) {
3747 case SIOCGMIIPHY:
3748 data->phy_id = R8152_PHY_ID; /* Internal PHY */
3749 break;
3750
3751 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08003752 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003753 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08003754 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003755 break;
3756
3757 case SIOCSMIIREG:
3758 if (!capable(CAP_NET_ADMIN)) {
3759 res = -EPERM;
3760 break;
3761 }
hayeswangb5403272014-10-09 18:00:26 +08003762 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003763 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08003764 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003765 break;
3766
3767 default:
3768 res = -EOPNOTSUPP;
3769 }
3770
hayeswang9a4be1b2014-02-18 21:49:07 +08003771 usb_autopm_put_interface(tp->intf);
3772
3773out:
hayeswangac718b62013-05-02 16:01:25 +00003774 return res;
3775}
3776
hayeswang69b4b7a2014-07-10 10:58:54 +08003777static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
3778{
3779 struct r8152 *tp = netdev_priv(dev);
3780
3781 switch (tp->version) {
3782 case RTL_VER_01:
3783 case RTL_VER_02:
3784 return eth_change_mtu(dev, new_mtu);
3785 default:
3786 break;
3787 }
3788
3789 if (new_mtu < 68 || new_mtu > RTL8153_MAX_MTU)
3790 return -EINVAL;
3791
3792 dev->mtu = new_mtu;
3793
3794 return 0;
3795}
3796
hayeswangac718b62013-05-02 16:01:25 +00003797static const struct net_device_ops rtl8152_netdev_ops = {
3798 .ndo_open = rtl8152_open,
3799 .ndo_stop = rtl8152_close,
3800 .ndo_do_ioctl = rtl8152_ioctl,
3801 .ndo_start_xmit = rtl8152_start_xmit,
3802 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08003803 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00003804 .ndo_set_rx_mode = rtl8152_set_rx_mode,
3805 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08003806 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00003807 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08003808 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00003809};
3810
3811static void r8152b_get_version(struct r8152 *tp)
3812{
3813 u32 ocp_data;
3814 u16 version;
3815
3816 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3817 version = (u16)(ocp_data & VERSION_MASK);
3818
3819 switch (version) {
3820 case 0x4c00:
3821 tp->version = RTL_VER_01;
3822 break;
3823 case 0x4c10:
3824 tp->version = RTL_VER_02;
3825 break;
hayeswang43779f82014-01-02 11:25:10 +08003826 case 0x5c00:
3827 tp->version = RTL_VER_03;
3828 tp->mii.supports_gmii = 1;
3829 break;
3830 case 0x5c10:
3831 tp->version = RTL_VER_04;
3832 tp->mii.supports_gmii = 1;
3833 break;
3834 case 0x5c20:
3835 tp->version = RTL_VER_05;
3836 tp->mii.supports_gmii = 1;
3837 break;
hayeswangac718b62013-05-02 16:01:25 +00003838 default:
3839 netif_info(tp, probe, tp->netdev,
3840 "Unknown version 0x%04x\n", version);
3841 break;
3842 }
3843}
3844
hayeswange3fe0b12014-01-02 11:22:39 +08003845static void rtl8152_unload(struct r8152 *tp)
3846{
hayeswang68714382014-04-11 17:54:31 +08003847 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3848 return;
3849
hayeswang00a5e362014-02-18 21:48:59 +08003850 if (tp->version != RTL_VER_01)
3851 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08003852}
3853
hayeswang43779f82014-01-02 11:25:10 +08003854static void rtl8153_unload(struct r8152 *tp)
3855{
hayeswang68714382014-04-11 17:54:31 +08003856 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3857 return;
3858
hayeswang49be1722014-10-01 13:25:11 +08003859 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003860}
3861
hayeswang55b65472014-11-06 12:47:39 +08003862static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08003863{
3864 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08003865 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08003866
hayeswang55b65472014-11-06 12:47:39 +08003867 switch (tp->version) {
3868 case RTL_VER_01:
3869 case RTL_VER_02:
3870 ops->init = r8152b_init;
3871 ops->enable = rtl8152_enable;
3872 ops->disable = rtl8152_disable;
3873 ops->up = rtl8152_up;
3874 ops->down = rtl8152_down;
3875 ops->unload = rtl8152_unload;
3876 ops->eee_get = r8152_get_eee;
3877 ops->eee_set = r8152_set_eee;
hayeswang43779f82014-01-02 11:25:10 +08003878 break;
3879
hayeswang55b65472014-11-06 12:47:39 +08003880 case RTL_VER_03:
3881 case RTL_VER_04:
3882 case RTL_VER_05:
3883 ops->init = r8153_init;
3884 ops->enable = rtl8153_enable;
3885 ops->disable = rtl8153_disable;
3886 ops->up = rtl8153_up;
3887 ops->down = rtl8153_down;
3888 ops->unload = rtl8153_unload;
3889 ops->eee_get = r8153_get_eee;
3890 ops->eee_set = r8153_set_eee;
hayeswangc81229c2014-01-02 11:22:42 +08003891 break;
3892
3893 default:
hayeswang55b65472014-11-06 12:47:39 +08003894 ret = -ENODEV;
3895 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08003896 break;
3897 }
3898
3899 return ret;
3900}
3901
hayeswangac718b62013-05-02 16:01:25 +00003902static int rtl8152_probe(struct usb_interface *intf,
3903 const struct usb_device_id *id)
3904{
3905 struct usb_device *udev = interface_to_usbdev(intf);
3906 struct r8152 *tp;
3907 struct net_device *netdev;
hayeswangebc2ec42013-08-14 20:54:38 +08003908 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003909
hayeswang10c32712014-03-04 20:47:48 +08003910 if (udev->actconfig->desc.bConfigurationValue != 1) {
3911 usb_driver_set_configuration(udev, 1);
3912 return -ENODEV;
3913 }
3914
3915 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00003916 netdev = alloc_etherdev(sizeof(struct r8152));
3917 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003918 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00003919 return -ENOMEM;
3920 }
3921
hayeswangebc2ec42013-08-14 20:54:38 +08003922 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00003923 tp = netdev_priv(netdev);
3924 tp->msg_enable = 0x7FFF;
3925
hayeswange3ad4122014-01-06 17:08:42 +08003926 tp->udev = udev;
3927 tp->netdev = netdev;
3928 tp->intf = intf;
3929
hayeswang82cf94c2014-11-06 12:47:38 +08003930 r8152b_get_version(tp);
hayeswang55b65472014-11-06 12:47:39 +08003931 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08003932 if (ret)
3933 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08003934
hayeswangb5403272014-10-09 18:00:26 +08003935 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003936 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3937
hayeswangac718b62013-05-02 16:01:25 +00003938 netdev->netdev_ops = &rtl8152_netdev_ops;
3939 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08003940
hayeswang60c89072014-03-07 11:04:39 +08003941 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1b2014-03-07 11:04:40 +08003942 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08003943 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
3944 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08003945 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1b2014-03-07 11:04:40 +08003946 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08003947 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08003948 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08003949 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3950 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
3951 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08003952
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003953 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08003954 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00003955
3956 tp->mii.dev = netdev;
3957 tp->mii.mdio_read = read_mii_word;
3958 tp->mii.mdio_write = write_mii_word;
3959 tp->mii.phy_id_mask = 0x3f;
3960 tp->mii.reg_num_mask = 0x1f;
3961 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00003962
hayeswang464ec102015-02-12 14:33:46 +08003963 switch (udev->speed) {
3964 case USB_SPEED_SUPER:
3965 tp->coalesce = COALESCE_SUPER;
3966 break;
3967 case USB_SPEED_HIGH:
3968 tp->coalesce = COALESCE_HIGH;
3969 break;
3970 default:
3971 tp->coalesce = COALESCE_SLOW;
3972 break;
3973 }
3974
hayeswang9a4be1b2014-02-18 21:49:07 +08003975 intf->needs_remote_wakeup = 1;
3976
hayeswangc81229c2014-01-02 11:22:42 +08003977 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00003978 set_ethernet_addr(tp);
3979
hayeswangac718b62013-05-02 16:01:25 +00003980 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08003981 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00003982
hayeswangebc2ec42013-08-14 20:54:38 +08003983 ret = register_netdev(netdev);
3984 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003985 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec42013-08-14 20:54:38 +08003986 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00003987 }
3988
hayeswang21ff2e82014-02-18 21:49:06 +08003989 tp->saved_wolopts = __rtl_get_wol(tp);
3990 if (tp->saved_wolopts)
3991 device_set_wakeup_enable(&udev->dev, true);
3992 else
3993 device_set_wakeup_enable(&udev->dev, false);
3994
Hayes Wang4a8deae2014-01-07 11:18:22 +08003995 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00003996
3997 return 0;
3998
hayeswangac718b62013-05-02 16:01:25 +00003999out1:
hayeswangd823ab62015-01-12 12:06:23 +08004000 netif_napi_del(&tp->napi);
hayeswangebc2ec42013-08-14 20:54:38 +08004001 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00004002out:
4003 free_netdev(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08004004 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004005}
4006
hayeswangac718b62013-05-02 16:01:25 +00004007static void rtl8152_disconnect(struct usb_interface *intf)
4008{
4009 struct r8152 *tp = usb_get_intfdata(intf);
4010
4011 usb_set_intfdata(intf, NULL);
4012 if (tp) {
hayeswangf561de32014-09-30 16:48:01 +08004013 struct usb_device *udev = tp->udev;
4014
4015 if (udev->state == USB_STATE_NOTATTACHED)
4016 set_bit(RTL8152_UNPLUG, &tp->flags);
4017
hayeswangd823ab62015-01-12 12:06:23 +08004018 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004019 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08004020 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00004021 free_netdev(tp->netdev);
4022 }
4023}
4024
hayeswangd9a28c52014-12-04 10:43:11 +08004025#define REALTEK_USB_DEVICE(vend, prod) \
4026 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
4027 USB_DEVICE_ID_MATCH_INT_CLASS, \
4028 .idVendor = (vend), \
4029 .idProduct = (prod), \
4030 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
4031}, \
4032{ \
4033 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
4034 USB_DEVICE_ID_MATCH_DEVICE, \
4035 .idVendor = (vend), \
4036 .idProduct = (prod), \
4037 .bInterfaceClass = USB_CLASS_COMM, \
4038 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
4039 .bInterfaceProtocol = USB_CDC_PROTO_NONE
4040
hayeswangac718b62013-05-02 16:01:25 +00004041/* table of devices that work with this driver */
4042static struct usb_device_id rtl8152_table[] = {
hayeswangd9a28c52014-12-04 10:43:11 +08004043 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
4044 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
4045 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
hayeswangac718b62013-05-02 16:01:25 +00004046 {}
4047};
4048
4049MODULE_DEVICE_TABLE(usb, rtl8152_table);
4050
4051static struct usb_driver rtl8152_driver = {
4052 .name = MODULENAME,
hayeswangebc2ec42013-08-14 20:54:38 +08004053 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00004054 .probe = rtl8152_probe,
4055 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00004056 .suspend = rtl8152_suspend,
hayeswangebc2ec42013-08-14 20:54:38 +08004057 .resume = rtl8152_resume,
4058 .reset_resume = rtl8152_resume,
hayeswang9a4be1b2014-02-18 21:49:07 +08004059 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08004060 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00004061};
4062
Sachin Kamatb4236daa2013-05-16 17:48:08 +00004063module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00004064
4065MODULE_AUTHOR(DRIVER_AUTHOR);
4066MODULE_DESCRIPTION(DRIVER_DESC);
4067MODULE_LICENSE("GPL");