blob: aafa1a1898e43de0d3d06e7d8367751473f25142 [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
Christian Hesse347eec32015-03-31 14:10:07 +0200496#define VENDOR_ID_LENOVO 0x17ef
hayeswangac718b62013-05-02 16:01:25 +0000497
498#define MCU_TYPE_PLA 0x0100
499#define MCU_TYPE_USB 0x0000
500
hayeswang4f1d4d52014-03-11 16:24:19 +0800501struct tally_counter {
502 __le64 tx_packets;
503 __le64 rx_packets;
504 __le64 tx_errors;
505 __le32 rx_errors;
506 __le16 rx_missed;
507 __le16 align_errors;
508 __le32 tx_one_collision;
509 __le32 tx_multi_collision;
510 __le64 rx_unicast;
511 __le64 rx_broadcast;
512 __le32 rx_multicast;
513 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800514 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800515};
516
hayeswangac718b62013-05-02 16:01:25 +0000517struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800518 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000519#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800520
hayeswang500b6d72013-11-20 17:30:57 +0800521 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800522#define RD_UDP_CS BIT(23)
523#define RD_TCP_CS BIT(22)
524#define RD_IPV6_CS BIT(20)
525#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800526
hayeswang500b6d72013-11-20 17:30:57 +0800527 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800528#define IPF BIT(23) /* IP checksum fail */
529#define UDPF BIT(22) /* UDP checksum fail */
530#define TCPF BIT(21) /* TCP checksum fail */
531#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800532
hayeswang500b6d72013-11-20 17:30:57 +0800533 __le32 opts4;
534 __le32 opts5;
535 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000536};
537
538struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800539 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800540#define TX_FS BIT(31) /* First segment of a packet */
541#define TX_LS BIT(30) /* Final segment of a packet */
542#define GTSENDV4 BIT(28)
543#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800544#define GTTCPHO_SHIFT 18
hayeswang6128d1b2014-03-07 11:04:40 +0800545#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800546#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800547
hayeswang500b6d72013-11-20 17:30:57 +0800548 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800549#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
550#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
551#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
552#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800553#define MSS_SHIFT 17
554#define MSS_MAX 0x7ffU
555#define TCPHO_SHIFT 17
hayeswang6128d1b2014-03-07 11:04:40 +0800556#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800557#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000558};
559
hayeswangdff4e8a2013-08-16 16:09:33 +0800560struct r8152;
561
hayeswangebc2ec42013-08-14 20:54:38 +0800562struct rx_agg {
563 struct list_head list;
564 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800565 struct r8152 *context;
hayeswangebc2ec42013-08-14 20:54:38 +0800566 void *buffer;
567 void *head;
568};
569
570struct tx_agg {
571 struct list_head list;
572 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800573 struct r8152 *context;
hayeswangebc2ec42013-08-14 20:54:38 +0800574 void *buffer;
575 void *head;
576 u32 skb_num;
577 u32 skb_len;
578};
579
hayeswangac718b62013-05-02 16:01:25 +0000580struct r8152 {
581 unsigned long flags;
582 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800583 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800584 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000585 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800586 struct urb *intr_urb;
hayeswangebc2ec42013-08-14 20:54:38 +0800587 struct tx_agg tx_info[RTL8152_MAX_TX];
588 struct rx_agg rx_info[RTL8152_MAX_RX];
589 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800590 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec42013-08-14 20:54:38 +0800591 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000592 struct delayed_work schedule;
593 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800594 struct mutex control; /* use for hw setting */
hayeswangc81229c2014-01-02 11:22:42 +0800595
596 struct rtl_ops {
597 void (*init)(struct r8152 *);
598 int (*enable)(struct r8152 *);
599 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800600 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800601 void (*down)(struct r8152 *);
602 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800603 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
604 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswangc81229c2014-01-02 11:22:42 +0800605 } rtl_ops;
606
hayeswang40a82912013-08-14 20:54:40 +0800607 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800608 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000609 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800610 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800611 u32 coalesce;
hayeswangac718b62013-05-02 16:01:25 +0000612 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800613 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000614 u8 version;
hayeswangac718b62013-05-02 16:01:25 +0000615};
616
617enum rtl_version {
618 RTL_VER_UNKNOWN = 0,
619 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800620 RTL_VER_02,
621 RTL_VER_03,
622 RTL_VER_04,
623 RTL_VER_05,
624 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000625};
626
hayeswang60c89072014-03-07 11:04:39 +0800627enum tx_csum_stat {
628 TX_CSUM_SUCCESS = 0,
629 TX_CSUM_TSO,
630 TX_CSUM_NONE
631};
632
hayeswangac718b62013-05-02 16:01:25 +0000633/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
634 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
635 */
636static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800637static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000638
hayeswang52aec122014-09-02 10:27:52 +0800639#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswang60c89072014-03-07 11:04:39 +0800640 VLAN_ETH_HLEN - VLAN_HLEN)
641
hayeswangac718b62013-05-02 16:01:25 +0000642static
643int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
644{
hayeswang31787f52013-07-31 17:21:25 +0800645 int ret;
646 void *tmp;
647
648 tmp = kmalloc(size, GFP_KERNEL);
649 if (!tmp)
650 return -ENOMEM;
651
652 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800653 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
654 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800655
656 memcpy(data, tmp, size);
657 kfree(tmp);
658
659 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000660}
661
662static
663int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
664{
hayeswang31787f52013-07-31 17:21:25 +0800665 int ret;
666 void *tmp;
667
Benoit Tainec4438f02014-05-26 17:21:23 +0200668 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800669 if (!tmp)
670 return -ENOMEM;
671
hayeswang31787f52013-07-31 17:21:25 +0800672 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800673 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
674 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800675
676 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800677
hayeswang31787f52013-07-31 17:21:25 +0800678 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000679}
680
681static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800682 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000683{
hayeswang45f4a192014-01-06 17:08:41 +0800684 u16 limit = 64;
685 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000686
687 if (test_bit(RTL8152_UNPLUG, &tp->flags))
688 return -ENODEV;
689
690 /* both size and indix must be 4 bytes align */
691 if ((size & 3) || !size || (index & 3) || !data)
692 return -EPERM;
693
694 if ((u32)index + (u32)size > 0xffff)
695 return -EPERM;
696
697 while (size) {
698 if (size > limit) {
699 ret = get_registers(tp, index, type, limit, data);
700 if (ret < 0)
701 break;
702
703 index += limit;
704 data += limit;
705 size -= limit;
706 } else {
707 ret = get_registers(tp, index, type, size, data);
708 if (ret < 0)
709 break;
710
711 index += size;
712 data += size;
713 size = 0;
714 break;
715 }
716 }
717
hayeswang67610492014-10-30 11:46:40 +0800718 if (ret == -ENODEV)
719 set_bit(RTL8152_UNPLUG, &tp->flags);
720
hayeswangac718b62013-05-02 16:01:25 +0000721 return ret;
722}
723
724static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800725 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000726{
hayeswang45f4a192014-01-06 17:08:41 +0800727 int ret;
728 u16 byteen_start, byteen_end, byen;
729 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000730
731 if (test_bit(RTL8152_UNPLUG, &tp->flags))
732 return -ENODEV;
733
734 /* both size and indix must be 4 bytes align */
735 if ((size & 3) || !size || (index & 3) || !data)
736 return -EPERM;
737
738 if ((u32)index + (u32)size > 0xffff)
739 return -EPERM;
740
741 byteen_start = byteen & BYTE_EN_START_MASK;
742 byteen_end = byteen & BYTE_EN_END_MASK;
743
744 byen = byteen_start | (byteen_start << 4);
745 ret = set_registers(tp, index, type | byen, 4, data);
746 if (ret < 0)
747 goto error1;
748
749 index += 4;
750 data += 4;
751 size -= 4;
752
753 if (size) {
754 size -= 4;
755
756 while (size) {
757 if (size > limit) {
758 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800759 type | BYTE_EN_DWORD,
760 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000761 if (ret < 0)
762 goto error1;
763
764 index += limit;
765 data += limit;
766 size -= limit;
767 } else {
768 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800769 type | BYTE_EN_DWORD,
770 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000771 if (ret < 0)
772 goto error1;
773
774 index += size;
775 data += size;
776 size = 0;
777 break;
778 }
779 }
780
781 byen = byteen_end | (byteen_end >> 4);
782 ret = set_registers(tp, index, type | byen, 4, data);
783 if (ret < 0)
784 goto error1;
785 }
786
787error1:
hayeswang67610492014-10-30 11:46:40 +0800788 if (ret == -ENODEV)
789 set_bit(RTL8152_UNPLUG, &tp->flags);
790
hayeswangac718b62013-05-02 16:01:25 +0000791 return ret;
792}
793
794static inline
795int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
796{
797 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
798}
799
800static inline
801int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
802{
803 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
804}
805
806static inline
807int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
808{
809 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
810}
811
812static inline
813int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
814{
815 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
816}
817
818static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
819{
hayeswangc8826de2013-07-31 17:21:26 +0800820 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000821
hayeswangc8826de2013-07-31 17:21:26 +0800822 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000823
824 return __le32_to_cpu(data);
825}
826
827static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
828{
hayeswangc8826de2013-07-31 17:21:26 +0800829 __le32 tmp = __cpu_to_le32(data);
830
831 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000832}
833
834static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
835{
836 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800837 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000838 u8 shift = index & 2;
839
840 index &= ~3;
841
hayeswangc8826de2013-07-31 17:21:26 +0800842 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000843
hayeswangc8826de2013-07-31 17:21:26 +0800844 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000845 data >>= (shift * 8);
846 data &= 0xffff;
847
848 return (u16)data;
849}
850
851static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
852{
hayeswangc8826de2013-07-31 17:21:26 +0800853 u32 mask = 0xffff;
854 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000855 u16 byen = BYTE_EN_WORD;
856 u8 shift = index & 2;
857
858 data &= mask;
859
860 if (index & 2) {
861 byen <<= shift;
862 mask <<= (shift * 8);
863 data <<= (shift * 8);
864 index &= ~3;
865 }
866
hayeswangc8826de2013-07-31 17:21:26 +0800867 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000868
hayeswangc8826de2013-07-31 17:21:26 +0800869 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000870}
871
872static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
873{
874 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800875 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000876 u8 shift = index & 3;
877
878 index &= ~3;
879
hayeswangc8826de2013-07-31 17:21:26 +0800880 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000881
hayeswangc8826de2013-07-31 17:21:26 +0800882 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000883 data >>= (shift * 8);
884 data &= 0xff;
885
886 return (u8)data;
887}
888
889static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
890{
hayeswangc8826de2013-07-31 17:21:26 +0800891 u32 mask = 0xff;
892 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000893 u16 byen = BYTE_EN_BYTE;
894 u8 shift = index & 3;
895
896 data &= mask;
897
898 if (index & 3) {
899 byen <<= shift;
900 mask <<= (shift * 8);
901 data <<= (shift * 8);
902 index &= ~3;
903 }
904
hayeswangc8826de2013-07-31 17:21:26 +0800905 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000906
hayeswangc8826de2013-07-31 17:21:26 +0800907 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000908}
909
hayeswangac244d32014-01-02 11:22:40 +0800910static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
911{
912 u16 ocp_base, ocp_index;
913
914 ocp_base = addr & 0xf000;
915 if (ocp_base != tp->ocp_base) {
916 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
917 tp->ocp_base = ocp_base;
918 }
919
920 ocp_index = (addr & 0x0fff) | 0xb000;
921 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
922}
923
hayeswange3fe0b12014-01-02 11:22:39 +0800924static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
925{
926 u16 ocp_base, ocp_index;
927
928 ocp_base = addr & 0xf000;
929 if (ocp_base != tp->ocp_base) {
930 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
931 tp->ocp_base = ocp_base;
932 }
933
934 ocp_index = (addr & 0x0fff) | 0xb000;
935 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
936}
937
hayeswangac244d32014-01-02 11:22:40 +0800938static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000939{
hayeswangac244d32014-01-02 11:22:40 +0800940 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000941}
942
hayeswangac244d32014-01-02 11:22:40 +0800943static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000944{
hayeswangac244d32014-01-02 11:22:40 +0800945 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000946}
947
hayeswang43779f82014-01-02 11:25:10 +0800948static void sram_write(struct r8152 *tp, u16 addr, u16 data)
949{
950 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
951 ocp_reg_write(tp, OCP_SRAM_DATA, data);
952}
953
hayeswangac718b62013-05-02 16:01:25 +0000954static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
955{
956 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +0800957 int ret;
hayeswangac718b62013-05-02 16:01:25 +0000958
hayeswang68714382014-04-11 17:54:31 +0800959 if (test_bit(RTL8152_UNPLUG, &tp->flags))
960 return -ENODEV;
961
hayeswangac718b62013-05-02 16:01:25 +0000962 if (phy_id != R8152_PHY_ID)
963 return -EINVAL;
964
hayeswang9a4be1b2014-02-18 21:49:07 +0800965 ret = r8152_mdio_read(tp, reg);
966
hayeswang9a4be1b2014-02-18 21:49:07 +0800967 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000968}
969
970static
971void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
972{
973 struct r8152 *tp = netdev_priv(netdev);
974
hayeswang68714382014-04-11 17:54:31 +0800975 if (test_bit(RTL8152_UNPLUG, &tp->flags))
976 return;
977
hayeswangac718b62013-05-02 16:01:25 +0000978 if (phy_id != R8152_PHY_ID)
979 return;
980
981 r8152_mdio_write(tp, reg, val);
982}
983
hayeswangb209af92014-08-25 15:53:00 +0800984static int
985r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec42013-08-14 20:54:38 +0800986
hayeswang8ba789a2014-09-04 16:15:41 +0800987static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
988{
989 struct r8152 *tp = netdev_priv(netdev);
990 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +0800991 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +0800992
993 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +0800994 goto out1;
995
996 ret = usb_autopm_get_interface(tp->intf);
997 if (ret < 0)
998 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +0800999
hayeswangb5403272014-10-09 18:00:26 +08001000 mutex_lock(&tp->control);
1001
hayeswang8ba789a2014-09-04 16:15:41 +08001002 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1003
1004 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1005 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1006 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1007
hayeswangb5403272014-10-09 18:00:26 +08001008 mutex_unlock(&tp->control);
1009
hayeswangea6a7112014-10-02 17:03:12 +08001010 usb_autopm_put_interface(tp->intf);
1011out1:
1012 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001013}
1014
hayeswang179bb6d2014-09-04 16:15:42 +08001015static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001016{
1017 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001018 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001019 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001020
hayeswang8a91c822014-02-18 21:49:01 +08001021 if (tp->version == RTL_VER_01)
hayeswang179bb6d2014-09-04 16:15:42 +08001022 ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001023 else
hayeswang179bb6d2014-09-04 16:15:42 +08001024 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001025
1026 if (ret < 0) {
hayeswang179bb6d2014-09-04 16:15:42 +08001027 netif_err(tp, probe, dev, "Get ether addr fail\n");
1028 } else if (!is_valid_ether_addr(sa.sa_data)) {
1029 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1030 sa.sa_data);
1031 eth_hw_addr_random(dev);
1032 ether_addr_copy(sa.sa_data, dev->dev_addr);
1033 ret = rtl8152_set_mac_address(dev, &sa);
1034 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1035 sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001036 } else {
hayeswang179bb6d2014-09-04 16:15:42 +08001037 if (tp->version == RTL_VER_01)
1038 ether_addr_copy(dev->dev_addr, sa.sa_data);
1039 else
1040 ret = rtl8152_set_mac_address(dev, &sa);
hayeswangac718b62013-05-02 16:01:25 +00001041 }
hayeswang179bb6d2014-09-04 16:15:42 +08001042
1043 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001044}
1045
hayeswangac718b62013-05-02 16:01:25 +00001046static void read_bulk_callback(struct urb *urb)
1047{
hayeswangac718b62013-05-02 16:01:25 +00001048 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001049 int status = urb->status;
hayeswangebc2ec42013-08-14 20:54:38 +08001050 struct rx_agg *agg;
1051 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +00001052
hayeswangebc2ec42013-08-14 20:54:38 +08001053 agg = urb->context;
1054 if (!agg)
1055 return;
1056
1057 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001058 if (!tp)
1059 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001060
hayeswangac718b62013-05-02 16:01:25 +00001061 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1062 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001063
1064 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001065 return;
1066
hayeswangebc2ec42013-08-14 20:54:38 +08001067 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001068
1069 /* When link down, the driver would cancel all bulks. */
1070 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +08001071 if (!netif_carrier_ok(netdev))
1072 return;
1073
hayeswang9a4be1b2014-02-18 21:49:07 +08001074 usb_mark_last_busy(tp->udev);
1075
hayeswangac718b62013-05-02 16:01:25 +00001076 switch (status) {
1077 case 0:
hayeswangebc2ec42013-08-14 20:54:38 +08001078 if (urb->actual_length < ETH_ZLEN)
1079 break;
1080
hayeswang2685d412014-03-07 11:04:34 +08001081 spin_lock(&tp->rx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001082 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001083 spin_unlock(&tp->rx_lock);
hayeswangd823ab62015-01-12 12:06:23 +08001084 napi_schedule(&tp->napi);
hayeswangebc2ec42013-08-14 20:54:38 +08001085 return;
hayeswangac718b62013-05-02 16:01:25 +00001086 case -ESHUTDOWN:
1087 set_bit(RTL8152_UNPLUG, &tp->flags);
1088 netif_device_detach(tp->netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001089 return;
hayeswangac718b62013-05-02 16:01:25 +00001090 case -ENOENT:
1091 return; /* the urb is in unlink state */
1092 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001093 if (net_ratelimit())
1094 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001095 break;
hayeswangac718b62013-05-02 16:01:25 +00001096 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001097 if (net_ratelimit())
1098 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001099 break;
hayeswangac718b62013-05-02 16:01:25 +00001100 }
1101
hayeswanga0fccd42014-11-20 10:29:05 +08001102 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001103}
1104
1105static void write_bulk_callback(struct urb *urb)
1106{
hayeswangebc2ec42013-08-14 20:54:38 +08001107 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001108 struct net_device *netdev;
hayeswangebc2ec42013-08-14 20:54:38 +08001109 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001110 struct r8152 *tp;
1111 int status = urb->status;
1112
hayeswangebc2ec42013-08-14 20:54:38 +08001113 agg = urb->context;
1114 if (!agg)
1115 return;
1116
1117 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001118 if (!tp)
1119 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001120
hayeswangd104eaf2014-03-06 15:07:17 +08001121 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001122 stats = &netdev->stats;
hayeswangebc2ec42013-08-14 20:54:38 +08001123 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001124 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001125 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001126 stats->tx_errors += agg->skb_num;
1127 } else {
1128 stats->tx_packets += agg->skb_num;
1129 stats->tx_bytes += agg->skb_len;
1130 }
1131
hayeswang2685d412014-03-07 11:04:34 +08001132 spin_lock(&tp->tx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001133 list_add_tail(&agg->list, &tp->tx_free);
hayeswang2685d412014-03-07 11:04:34 +08001134 spin_unlock(&tp->tx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001135
hayeswang9a4be1b2014-02-18 21:49:07 +08001136 usb_autopm_put_interface_async(tp->intf);
1137
hayeswangd104eaf2014-03-06 15:07:17 +08001138 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001139 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001140
1141 if (!test_bit(WORK_ENABLE, &tp->flags))
1142 return;
1143
1144 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1145 return;
1146
1147 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001148 napi_schedule(&tp->napi);
hayeswangebc2ec42013-08-14 20:54:38 +08001149}
1150
hayeswang40a82912013-08-14 20:54:40 +08001151static void intr_callback(struct urb *urb)
1152{
1153 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001154 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001155 int status = urb->status;
1156 int res;
1157
1158 tp = urb->context;
1159 if (!tp)
1160 return;
1161
1162 if (!test_bit(WORK_ENABLE, &tp->flags))
1163 return;
1164
1165 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1166 return;
1167
1168 switch (status) {
1169 case 0: /* success */
1170 break;
1171 case -ECONNRESET: /* unlink */
1172 case -ESHUTDOWN:
1173 netif_device_detach(tp->netdev);
1174 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001175 case -EPROTO:
1176 netif_info(tp, intr, tp->netdev,
1177 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001178 return;
1179 case -EOVERFLOW:
1180 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1181 goto resubmit;
1182 /* -EPIPE: should clear the halt */
1183 default:
1184 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1185 goto resubmit;
1186 }
1187
1188 d = urb->transfer_buffer;
1189 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001190 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001191 set_bit(RTL8152_LINK_CHG, &tp->flags);
1192 schedule_delayed_work(&tp->schedule, 0);
1193 }
1194 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001195 if (netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001196 set_bit(RTL8152_LINK_CHG, &tp->flags);
1197 schedule_delayed_work(&tp->schedule, 0);
1198 }
1199 }
1200
1201resubmit:
1202 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001203 if (res == -ENODEV) {
1204 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001205 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001206 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001207 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001208 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001209 }
hayeswang40a82912013-08-14 20:54:40 +08001210}
1211
hayeswangebc2ec42013-08-14 20:54:38 +08001212static inline void *rx_agg_align(void *data)
1213{
hayeswang8e1f51b2014-01-02 11:22:41 +08001214 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001215}
1216
1217static inline void *tx_agg_align(void *data)
1218{
hayeswang8e1f51b2014-01-02 11:22:41 +08001219 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001220}
1221
1222static void free_all_mem(struct r8152 *tp)
1223{
1224 int i;
1225
1226 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001227 usb_free_urb(tp->rx_info[i].urb);
1228 tp->rx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001229
hayeswang9629e3c2014-01-15 10:42:15 +08001230 kfree(tp->rx_info[i].buffer);
1231 tp->rx_info[i].buffer = NULL;
1232 tp->rx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001233 }
1234
1235 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001236 usb_free_urb(tp->tx_info[i].urb);
1237 tp->tx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001238
hayeswang9629e3c2014-01-15 10:42:15 +08001239 kfree(tp->tx_info[i].buffer);
1240 tp->tx_info[i].buffer = NULL;
1241 tp->tx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001242 }
hayeswang40a82912013-08-14 20:54:40 +08001243
hayeswang9629e3c2014-01-15 10:42:15 +08001244 usb_free_urb(tp->intr_urb);
1245 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001246
hayeswang9629e3c2014-01-15 10:42:15 +08001247 kfree(tp->intr_buff);
1248 tp->intr_buff = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001249}
1250
1251static int alloc_all_mem(struct r8152 *tp)
1252{
1253 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001254 struct usb_interface *intf = tp->intf;
1255 struct usb_host_interface *alt = intf->cur_altsetting;
1256 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec42013-08-14 20:54:38 +08001257 struct urb *urb;
1258 int node, i;
1259 u8 *buf;
1260
1261 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1262
1263 spin_lock_init(&tp->rx_lock);
1264 spin_lock_init(&tp->tx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001265 INIT_LIST_HEAD(&tp->tx_free);
1266 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001267 skb_queue_head_init(&tp->rx_queue);
hayeswangebc2ec42013-08-14 20:54:38 +08001268
1269 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001270 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec42013-08-14 20:54:38 +08001271 if (!buf)
1272 goto err1;
1273
1274 if (buf != rx_agg_align(buf)) {
1275 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001276 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001277 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001278 if (!buf)
1279 goto err1;
1280 }
1281
1282 urb = usb_alloc_urb(0, GFP_KERNEL);
1283 if (!urb) {
1284 kfree(buf);
1285 goto err1;
1286 }
1287
1288 INIT_LIST_HEAD(&tp->rx_info[i].list);
1289 tp->rx_info[i].context = tp;
1290 tp->rx_info[i].urb = urb;
1291 tp->rx_info[i].buffer = buf;
1292 tp->rx_info[i].head = rx_agg_align(buf);
1293 }
1294
1295 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001296 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec42013-08-14 20:54:38 +08001297 if (!buf)
1298 goto err1;
1299
1300 if (buf != tx_agg_align(buf)) {
1301 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001302 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001303 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001304 if (!buf)
1305 goto err1;
1306 }
1307
1308 urb = usb_alloc_urb(0, GFP_KERNEL);
1309 if (!urb) {
1310 kfree(buf);
1311 goto err1;
1312 }
1313
1314 INIT_LIST_HEAD(&tp->tx_info[i].list);
1315 tp->tx_info[i].context = tp;
1316 tp->tx_info[i].urb = urb;
1317 tp->tx_info[i].buffer = buf;
1318 tp->tx_info[i].head = tx_agg_align(buf);
1319
1320 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1321 }
1322
hayeswang40a82912013-08-14 20:54:40 +08001323 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1324 if (!tp->intr_urb)
1325 goto err1;
1326
1327 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1328 if (!tp->intr_buff)
1329 goto err1;
1330
1331 tp->intr_interval = (int)ep_intr->desc.bInterval;
1332 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001333 tp->intr_buff, INTBUFSIZE, intr_callback,
1334 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001335
hayeswangebc2ec42013-08-14 20:54:38 +08001336 return 0;
1337
1338err1:
1339 free_all_mem(tp);
1340 return -ENOMEM;
1341}
1342
hayeswang0de98f62013-08-16 16:09:35 +08001343static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1344{
1345 struct tx_agg *agg = NULL;
1346 unsigned long flags;
1347
hayeswang21949ab2014-03-07 11:04:35 +08001348 if (list_empty(&tp->tx_free))
1349 return NULL;
1350
hayeswang0de98f62013-08-16 16:09:35 +08001351 spin_lock_irqsave(&tp->tx_lock, flags);
1352 if (!list_empty(&tp->tx_free)) {
1353 struct list_head *cursor;
1354
1355 cursor = tp->tx_free.next;
1356 list_del_init(cursor);
1357 agg = list_entry(cursor, struct tx_agg, list);
1358 }
1359 spin_unlock_irqrestore(&tp->tx_lock, flags);
1360
1361 return agg;
1362}
1363
hayeswangb209af92014-08-25 15:53:00 +08001364/* r8152_csum_workaround()
hayeswang6128d1b2014-03-07 11:04:40 +08001365 * The hw limites the value the transport offset. When the offset is out of the
1366 * range, calculate the checksum by sw.
1367 */
1368static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1369 struct sk_buff_head *list)
1370{
1371 if (skb_shinfo(skb)->gso_size) {
1372 netdev_features_t features = tp->netdev->features;
1373 struct sk_buff_head seg_list;
1374 struct sk_buff *segs, *nskb;
1375
hayeswanga91d45f2014-07-11 16:48:27 +08001376 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1b2014-03-07 11:04:40 +08001377 segs = skb_gso_segment(skb, features);
1378 if (IS_ERR(segs) || !segs)
1379 goto drop;
1380
1381 __skb_queue_head_init(&seg_list);
1382
1383 do {
1384 nskb = segs;
1385 segs = segs->next;
1386 nskb->next = NULL;
1387 __skb_queue_tail(&seg_list, nskb);
1388 } while (segs);
1389
1390 skb_queue_splice(&seg_list, list);
1391 dev_kfree_skb(skb);
1392 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1393 if (skb_checksum_help(skb) < 0)
1394 goto drop;
1395
1396 __skb_queue_head(list, skb);
1397 } else {
1398 struct net_device_stats *stats;
1399
1400drop:
1401 stats = &tp->netdev->stats;
1402 stats->tx_dropped++;
1403 dev_kfree_skb(skb);
1404 }
1405}
1406
hayeswangb209af92014-08-25 15:53:00 +08001407/* msdn_giant_send_check()
hayeswang6128d1b2014-03-07 11:04:40 +08001408 * According to the document of microsoft, the TCP Pseudo Header excludes the
1409 * packet length for IPv6 TCP large packets.
1410 */
1411static int msdn_giant_send_check(struct sk_buff *skb)
1412{
1413 const struct ipv6hdr *ipv6h;
1414 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001415 int ret;
1416
1417 ret = skb_cow_head(skb, 0);
1418 if (ret)
1419 return ret;
hayeswang6128d1b2014-03-07 11:04:40 +08001420
1421 ipv6h = ipv6_hdr(skb);
1422 th = tcp_hdr(skb);
1423
1424 th->check = 0;
1425 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1426
hayeswangfcb308d2014-03-11 10:20:32 +08001427 return ret;
hayeswang6128d1b2014-03-07 11:04:40 +08001428}
1429
hayeswangc5554292014-09-12 10:43:11 +08001430static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1431{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001432 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001433 u32 opts2;
1434
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001435 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001436 desc->opts2 |= cpu_to_le32(opts2);
1437 }
1438}
1439
1440static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1441{
1442 u32 opts2 = le32_to_cpu(desc->opts2);
1443
1444 if (opts2 & RX_VLAN_TAG)
1445 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1446 swab16(opts2 & 0xffff));
1447}
1448
hayeswang60c89072014-03-07 11:04:39 +08001449static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1450 struct sk_buff *skb, u32 len, u32 transport_offset)
1451{
1452 u32 mss = skb_shinfo(skb)->gso_size;
1453 u32 opts1, opts2 = 0;
1454 int ret = TX_CSUM_SUCCESS;
1455
1456 WARN_ON_ONCE(len > TX_LEN_MAX);
1457
1458 opts1 = len | TX_FS | TX_LS;
1459
1460 if (mss) {
hayeswang6128d1b2014-03-07 11:04:40 +08001461 if (transport_offset > GTTCPHO_MAX) {
1462 netif_warn(tp, tx_err, tp->netdev,
1463 "Invalid transport offset 0x%x for TSO\n",
1464 transport_offset);
1465 ret = TX_CSUM_TSO;
1466 goto unavailable;
1467 }
1468
hayeswang6e74d172015-02-06 11:30:50 +08001469 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001470 case htons(ETH_P_IP):
1471 opts1 |= GTSENDV4;
1472 break;
1473
hayeswang6128d1b2014-03-07 11:04:40 +08001474 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001475 if (msdn_giant_send_check(skb)) {
1476 ret = TX_CSUM_TSO;
1477 goto unavailable;
1478 }
hayeswang6128d1b2014-03-07 11:04:40 +08001479 opts1 |= GTSENDV6;
hayeswang6128d1b2014-03-07 11:04:40 +08001480 break;
1481
hayeswang60c89072014-03-07 11:04:39 +08001482 default:
1483 WARN_ON_ONCE(1);
1484 break;
1485 }
1486
1487 opts1 |= transport_offset << GTTCPHO_SHIFT;
1488 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1489 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001490 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001491
hayeswang6128d1b2014-03-07 11:04:40 +08001492 if (transport_offset > TCPHO_MAX) {
1493 netif_warn(tp, tx_err, tp->netdev,
1494 "Invalid transport offset 0x%x\n",
1495 transport_offset);
1496 ret = TX_CSUM_NONE;
1497 goto unavailable;
1498 }
1499
hayeswang6e74d172015-02-06 11:30:50 +08001500 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001501 case htons(ETH_P_IP):
1502 opts2 |= IPV4_CS;
1503 ip_protocol = ip_hdr(skb)->protocol;
1504 break;
1505
1506 case htons(ETH_P_IPV6):
1507 opts2 |= IPV6_CS;
1508 ip_protocol = ipv6_hdr(skb)->nexthdr;
1509 break;
1510
1511 default:
1512 ip_protocol = IPPROTO_RAW;
1513 break;
1514 }
1515
hayeswang60c89072014-03-07 11:04:39 +08001516 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001517 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001518 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001519 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001520 else
hayeswang5bd23882013-08-14 20:54:39 +08001521 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001522
hayeswang60c89072014-03-07 11:04:39 +08001523 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001524 }
hayeswang60c89072014-03-07 11:04:39 +08001525
1526 desc->opts2 = cpu_to_le32(opts2);
1527 desc->opts1 = cpu_to_le32(opts1);
1528
hayeswang6128d1b2014-03-07 11:04:40 +08001529unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001530 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001531}
1532
hayeswangb1379d92013-08-16 16:09:37 +08001533static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1534{
hayeswangd84130a2014-02-18 21:49:02 +08001535 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001536 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001537 u8 *tx_data;
1538
hayeswangd84130a2014-02-18 21:49:02 +08001539 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001540 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001541 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001542 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001543
hayeswangb1379d92013-08-16 16:09:37 +08001544 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001545 agg->skb_num = 0;
1546 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001547 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001548
hayeswang7937f9e2013-11-20 17:30:54 +08001549 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001550 struct tx_desc *tx_desc;
1551 struct sk_buff *skb;
1552 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001553 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001554
hayeswangd84130a2014-02-18 21:49:02 +08001555 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001556 if (!skb)
1557 break;
1558
hayeswang60c89072014-03-07 11:04:39 +08001559 len = skb->len + sizeof(*tx_desc);
1560
1561 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001562 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001563 break;
1564 }
1565
hayeswang7937f9e2013-11-20 17:30:54 +08001566 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001567 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001568
1569 offset = (u32)skb_transport_offset(skb);
1570
hayeswang6128d1b2014-03-07 11:04:40 +08001571 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1572 r8152_csum_workaround(tp, skb, &skb_head);
1573 continue;
1574 }
hayeswang60c89072014-03-07 11:04:39 +08001575
hayeswangc5554292014-09-12 10:43:11 +08001576 rtl_tx_vlan_tag(tx_desc, skb);
1577
hayeswangb1379d92013-08-16 16:09:37 +08001578 tx_data += sizeof(*tx_desc);
1579
hayeswang60c89072014-03-07 11:04:39 +08001580 len = skb->len;
1581 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1582 struct net_device_stats *stats = &tp->netdev->stats;
1583
1584 stats->tx_dropped++;
1585 dev_kfree_skb_any(skb);
1586 tx_data -= sizeof(*tx_desc);
1587 continue;
1588 }
hayeswangb1379d92013-08-16 16:09:37 +08001589
hayeswang7937f9e2013-11-20 17:30:54 +08001590 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001591 agg->skb_len += len;
1592 agg->skb_num++;
1593
1594 dev_kfree_skb_any(skb);
1595
hayeswang52aec122014-09-02 10:27:52 +08001596 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001597 }
1598
hayeswangd84130a2014-02-18 21:49:02 +08001599 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001600 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001601 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001602 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001603 }
1604
hayeswang0c3121f2014-03-07 11:04:36 +08001605 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001606
1607 if (netif_queue_stopped(tp->netdev) &&
1608 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1609 netif_wake_queue(tp->netdev);
1610
hayeswang0c3121f2014-03-07 11:04:36 +08001611 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001612
hayeswang0c3121f2014-03-07 11:04:36 +08001613 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001614 if (ret < 0)
1615 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001616
hayeswangb1379d92013-08-16 16:09:37 +08001617 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1618 agg->head, (int)(tx_data - (u8 *)agg->head),
1619 (usb_complete_t)write_bulk_callback, agg);
1620
hayeswang0c3121f2014-03-07 11:04:36 +08001621 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001622 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001623 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001624
1625out_tx_fill:
1626 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001627}
1628
hayeswang565cab02014-03-07 11:04:38 +08001629static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1630{
1631 u8 checksum = CHECKSUM_NONE;
1632 u32 opts2, opts3;
1633
1634 if (tp->version == RTL_VER_01)
1635 goto return_result;
1636
1637 opts2 = le32_to_cpu(rx_desc->opts2);
1638 opts3 = le32_to_cpu(rx_desc->opts3);
1639
1640 if (opts2 & RD_IPV4_CS) {
1641 if (opts3 & IPF)
1642 checksum = CHECKSUM_NONE;
1643 else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
1644 checksum = CHECKSUM_NONE;
1645 else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
1646 checksum = CHECKSUM_NONE;
1647 else
1648 checksum = CHECKSUM_UNNECESSARY;
hayeswang6128d1b2014-03-07 11:04:40 +08001649 } else if (RD_IPV6_CS) {
1650 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1651 checksum = CHECKSUM_UNNECESSARY;
1652 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1653 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001654 }
1655
1656return_result:
1657 return checksum;
1658}
1659
hayeswangd823ab62015-01-12 12:06:23 +08001660static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec42013-08-14 20:54:38 +08001661{
hayeswanga5a4f462013-08-16 16:09:34 +08001662 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001663 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08001664 int ret = 0, work_done = 0;
hayeswangd823ab62015-01-12 12:06:23 +08001665
1666 if (!skb_queue_empty(&tp->rx_queue)) {
1667 while (work_done < budget) {
1668 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1669 struct net_device *netdev = tp->netdev;
1670 struct net_device_stats *stats = &netdev->stats;
1671 unsigned int pkt_len;
1672
1673 if (!skb)
1674 break;
1675
1676 pkt_len = skb->len;
1677 napi_gro_receive(&tp->napi, skb);
1678 work_done++;
1679 stats->rx_packets++;
1680 stats->rx_bytes += pkt_len;
1681 }
1682 }
hayeswangebc2ec42013-08-14 20:54:38 +08001683
hayeswangd84130a2014-02-18 21:49:02 +08001684 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08001685 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08001686
1687 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001688 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001689 list_splice_init(&tp->rx_done, &rx_queue);
1690 spin_unlock_irqrestore(&tp->rx_lock, flags);
1691
1692 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001693 struct rx_desc *rx_desc;
1694 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001695 int len_used = 0;
1696 struct urb *urb;
1697 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08001698
hayeswangebc2ec42013-08-14 20:54:38 +08001699 list_del_init(cursor);
hayeswangebc2ec42013-08-14 20:54:38 +08001700
1701 agg = list_entry(cursor, struct rx_agg, list);
1702 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001703 if (urb->actual_length < ETH_ZLEN)
1704 goto submit;
hayeswangebc2ec42013-08-14 20:54:38 +08001705
hayeswangebc2ec42013-08-14 20:54:38 +08001706 rx_desc = agg->head;
1707 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001708 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001709
hayeswang7937f9e2013-11-20 17:30:54 +08001710 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001711 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001712 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001713 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001714 struct sk_buff *skb;
1715
hayeswang7937f9e2013-11-20 17:30:54 +08001716 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec42013-08-14 20:54:38 +08001717 if (pkt_len < ETH_ZLEN)
1718 break;
1719
hayeswang7937f9e2013-11-20 17:30:54 +08001720 len_used += pkt_len;
1721 if (urb->actual_length < len_used)
1722 break;
1723
hayeswang8e1f51b2014-01-02 11:22:41 +08001724 pkt_len -= CRC_SIZE;
hayeswangebc2ec42013-08-14 20:54:38 +08001725 rx_data += sizeof(struct rx_desc);
1726
1727 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1728 if (!skb) {
1729 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001730 goto find_next_rx;
hayeswangebc2ec42013-08-14 20:54:38 +08001731 }
hayeswang565cab02014-03-07 11:04:38 +08001732
1733 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001734 memcpy(skb->data, rx_data, pkt_len);
1735 skb_put(skb, pkt_len);
1736 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08001737 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001738 if (work_done < budget) {
1739 napi_gro_receive(&tp->napi, skb);
1740 work_done++;
1741 stats->rx_packets++;
1742 stats->rx_bytes += pkt_len;
1743 } else {
1744 __skb_queue_tail(&tp->rx_queue, skb);
1745 }
hayeswangebc2ec42013-08-14 20:54:38 +08001746
hayeswang5e2f7482014-03-07 11:04:37 +08001747find_next_rx:
hayeswang8e1f51b2014-01-02 11:22:41 +08001748 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec42013-08-14 20:54:38 +08001749 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec42013-08-14 20:54:38 +08001750 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001751 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001752 }
1753
hayeswang0de98f62013-08-16 16:09:35 +08001754submit:
hayeswange1a2ca92015-02-06 11:30:45 +08001755 if (!ret) {
1756 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1757 } else {
1758 urb->actual_length = 0;
1759 list_add_tail(&agg->list, next);
1760 }
1761 }
1762
1763 if (!list_empty(&rx_queue)) {
1764 spin_lock_irqsave(&tp->rx_lock, flags);
1765 list_splice_tail(&rx_queue, &tp->rx_done);
1766 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001767 }
hayeswangd823ab62015-01-12 12:06:23 +08001768
1769out1:
1770 return work_done;
hayeswangebc2ec42013-08-14 20:54:38 +08001771}
1772
1773static void tx_bottom(struct r8152 *tp)
1774{
hayeswangebc2ec42013-08-14 20:54:38 +08001775 int res;
1776
hayeswangb1379d92013-08-16 16:09:37 +08001777 do {
1778 struct tx_agg *agg;
hayeswangebc2ec42013-08-14 20:54:38 +08001779
hayeswangb1379d92013-08-16 16:09:37 +08001780 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec42013-08-14 20:54:38 +08001781 break;
1782
hayeswangb1379d92013-08-16 16:09:37 +08001783 agg = r8152_get_tx_agg(tp);
1784 if (!agg)
hayeswangebc2ec42013-08-14 20:54:38 +08001785 break;
hayeswangb1379d92013-08-16 16:09:37 +08001786
1787 res = r8152_tx_agg_fill(tp, agg);
1788 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08001789 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08001790
1791 if (res == -ENODEV) {
hayeswang67610492014-10-30 11:46:40 +08001792 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswangb1379d92013-08-16 16:09:37 +08001793 netif_device_detach(netdev);
1794 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08001795 struct net_device_stats *stats = &netdev->stats;
1796 unsigned long flags;
1797
hayeswangb1379d92013-08-16 16:09:37 +08001798 netif_warn(tp, tx_err, netdev,
1799 "failed tx_urb %d\n", res);
1800 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08001801
hayeswangb1379d92013-08-16 16:09:37 +08001802 spin_lock_irqsave(&tp->tx_lock, flags);
1803 list_add_tail(&agg->list, &tp->tx_free);
1804 spin_unlock_irqrestore(&tp->tx_lock, flags);
1805 }
hayeswangebc2ec42013-08-14 20:54:38 +08001806 }
hayeswangb1379d92013-08-16 16:09:37 +08001807 } while (res == 0);
hayeswangebc2ec42013-08-14 20:54:38 +08001808}
1809
hayeswangd823ab62015-01-12 12:06:23 +08001810static void bottom_half(struct r8152 *tp)
hayeswangebc2ec42013-08-14 20:54:38 +08001811{
hayeswangebc2ec42013-08-14 20:54:38 +08001812 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1813 return;
1814
1815 if (!test_bit(WORK_ENABLE, &tp->flags))
1816 return;
1817
hayeswang7559fb2f2013-08-16 16:09:38 +08001818 /* When link down, the driver would cancel all bulks. */
1819 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +08001820 if (!netif_carrier_ok(tp->netdev))
1821 return;
1822
hayeswangd823ab62015-01-12 12:06:23 +08001823 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08001824
hayeswang0c3121f2014-03-07 11:04:36 +08001825 tx_bottom(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001826}
1827
hayeswangd823ab62015-01-12 12:06:23 +08001828static int r8152_poll(struct napi_struct *napi, int budget)
1829{
1830 struct r8152 *tp = container_of(napi, struct r8152, napi);
1831 int work_done;
1832
1833 work_done = rx_bottom(tp, budget);
1834 bottom_half(tp);
1835
1836 if (work_done < budget) {
1837 napi_complete(napi);
1838 if (!list_empty(&tp->rx_done))
1839 napi_schedule(napi);
1840 }
1841
1842 return work_done;
1843}
1844
hayeswangebc2ec42013-08-14 20:54:38 +08001845static
1846int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1847{
hayeswanga0fccd42014-11-20 10:29:05 +08001848 int ret;
1849
hayeswangef827a52015-01-09 10:26:36 +08001850 /* The rx would be stopped, so skip submitting */
1851 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
1852 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
1853 return 0;
1854
hayeswangebc2ec42013-08-14 20:54:38 +08001855 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
hayeswang52aec122014-09-02 10:27:52 +08001856 agg->head, agg_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08001857 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec42013-08-14 20:54:38 +08001858
hayeswanga0fccd42014-11-20 10:29:05 +08001859 ret = usb_submit_urb(agg->urb, mem_flags);
1860 if (ret == -ENODEV) {
1861 set_bit(RTL8152_UNPLUG, &tp->flags);
1862 netif_device_detach(tp->netdev);
1863 } else if (ret) {
1864 struct urb *urb = agg->urb;
1865 unsigned long flags;
1866
1867 urb->actual_length = 0;
1868 spin_lock_irqsave(&tp->rx_lock, flags);
1869 list_add_tail(&agg->list, &tp->rx_done);
1870 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001871
1872 netif_err(tp, rx_err, tp->netdev,
1873 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
1874
1875 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08001876 }
1877
1878 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001879}
1880
hayeswang00a5e362014-02-18 21:48:59 +08001881static void rtl_drop_queued_tx(struct r8152 *tp)
1882{
1883 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001884 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08001885 struct sk_buff *skb;
1886
hayeswangd84130a2014-02-18 21:49:02 +08001887 if (skb_queue_empty(tx_queue))
1888 return;
1889
1890 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001891 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001892 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001893 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001894
1895 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001896 dev_kfree_skb(skb);
1897 stats->tx_dropped++;
1898 }
1899}
1900
hayeswangac718b62013-05-02 16:01:25 +00001901static void rtl8152_tx_timeout(struct net_device *netdev)
1902{
1903 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001904 int i;
1905
Hayes Wang4a8deae2014-01-07 11:18:22 +08001906 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001907 for (i = 0; i < RTL8152_MAX_TX; i++)
1908 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001909}
1910
1911static void rtl8152_set_rx_mode(struct net_device *netdev)
1912{
1913 struct r8152 *tp = netdev_priv(netdev);
1914
hayeswang51d979f2015-02-06 11:30:47 +08001915 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00001916 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001917 schedule_delayed_work(&tp->schedule, 0);
1918 }
hayeswangac718b62013-05-02 16:01:25 +00001919}
1920
1921static void _rtl8152_set_rx_mode(struct net_device *netdev)
1922{
1923 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001924 u32 mc_filter[2]; /* Multicast hash filter */
1925 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001926 u32 ocp_data;
1927
hayeswangac718b62013-05-02 16:01:25 +00001928 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1929 netif_stop_queue(netdev);
1930 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1931 ocp_data &= ~RCR_ACPT_ALL;
1932 ocp_data |= RCR_AB | RCR_APM;
1933
1934 if (netdev->flags & IFF_PROMISC) {
1935 /* Unconditionally log net taps. */
1936 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1937 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08001938 mc_filter[1] = 0xffffffff;
1939 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001940 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1941 (netdev->flags & IFF_ALLMULTI)) {
1942 /* Too many to filter perfectly -- accept all multicasts. */
1943 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08001944 mc_filter[1] = 0xffffffff;
1945 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001946 } else {
1947 struct netdev_hw_addr *ha;
1948
hayeswangb209af92014-08-25 15:53:00 +08001949 mc_filter[1] = 0;
1950 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00001951 netdev_for_each_mc_addr(ha, netdev) {
1952 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08001953
hayeswangac718b62013-05-02 16:01:25 +00001954 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1955 ocp_data |= RCR_AM;
1956 }
1957 }
1958
hayeswang31787f52013-07-31 17:21:25 +08001959 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1960 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001961
hayeswang31787f52013-07-31 17:21:25 +08001962 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001963 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1964 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001965}
1966
hayeswanga5e31252015-01-06 17:41:58 +08001967static netdev_features_t
1968rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
1969 netdev_features_t features)
1970{
1971 u32 mss = skb_shinfo(skb)->gso_size;
1972 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
1973 int offset = skb_transport_offset(skb);
1974
1975 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
1976 features &= ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
1977 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
1978 features &= ~NETIF_F_GSO_MASK;
1979
1980 return features;
1981}
1982
hayeswangac718b62013-05-02 16:01:25 +00001983static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08001984 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00001985{
1986 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001987
hayeswangac718b62013-05-02 16:01:25 +00001988 skb_tx_timestamp(skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001989
hayeswang61598782013-11-20 17:30:55 +08001990 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001991
hayeswang0c3121f2014-03-07 11:04:36 +08001992 if (!list_empty(&tp->tx_free)) {
1993 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08001994 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08001995 schedule_delayed_work(&tp->schedule, 0);
1996 } else {
1997 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08001998 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08001999 }
hayeswangb209af92014-08-25 15:53:00 +08002000 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002001 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002002 }
hayeswangdd1b1192013-11-20 17:30:56 +08002003
hayeswangac718b62013-05-02 16:01:25 +00002004 return NETDEV_TX_OK;
2005}
2006
2007static void r8152b_reset_packet_filter(struct r8152 *tp)
2008{
2009 u32 ocp_data;
2010
2011 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2012 ocp_data &= ~FMC_FCR_MCU_EN;
2013 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2014 ocp_data |= FMC_FCR_MCU_EN;
2015 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2016}
2017
2018static void rtl8152_nic_reset(struct r8152 *tp)
2019{
2020 int i;
2021
2022 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2023
2024 for (i = 0; i < 1000; i++) {
2025 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2026 break;
hayeswangb209af92014-08-25 15:53:00 +08002027 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002028 }
2029}
2030
hayeswangdd1b1192013-11-20 17:30:56 +08002031static void set_tx_qlen(struct r8152 *tp)
2032{
2033 struct net_device *netdev = tp->netdev;
2034
hayeswang52aec122014-09-02 10:27:52 +08002035 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
2036 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002037}
2038
hayeswangac718b62013-05-02 16:01:25 +00002039static inline u8 rtl8152_get_speed(struct r8152 *tp)
2040{
2041 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2042}
2043
hayeswang507605a2014-01-02 11:22:43 +08002044static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002045{
hayeswangebc2ec42013-08-14 20:54:38 +08002046 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002047 u8 speed;
2048
2049 speed = rtl8152_get_speed(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002050 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002051 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08002052 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002053 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2054 } else {
2055 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08002056 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002057 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2058 }
hayeswang507605a2014-01-02 11:22:43 +08002059}
2060
hayeswang00a5e362014-02-18 21:48:59 +08002061static void rxdy_gated_en(struct r8152 *tp, bool enable)
2062{
2063 u32 ocp_data;
2064
2065 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2066 if (enable)
2067 ocp_data |= RXDY_GATED_EN;
2068 else
2069 ocp_data &= ~RXDY_GATED_EN;
2070 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2071}
2072
hayeswang445f7f42014-09-23 16:31:47 +08002073static int rtl_start_rx(struct r8152 *tp)
2074{
2075 int i, ret = 0;
2076
hayeswangd823ab62015-01-12 12:06:23 +08002077 napi_disable(&tp->napi);
hayeswang445f7f42014-09-23 16:31:47 +08002078 INIT_LIST_HEAD(&tp->rx_done);
2079 for (i = 0; i < RTL8152_MAX_RX; i++) {
2080 INIT_LIST_HEAD(&tp->rx_info[i].list);
2081 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2082 if (ret)
2083 break;
2084 }
hayeswangd823ab62015-01-12 12:06:23 +08002085 napi_enable(&tp->napi);
hayeswang445f7f42014-09-23 16:31:47 +08002086
hayeswang7bcf4f62014-11-20 10:29:06 +08002087 if (ret && ++i < RTL8152_MAX_RX) {
2088 struct list_head rx_queue;
2089 unsigned long flags;
2090
2091 INIT_LIST_HEAD(&rx_queue);
2092
2093 do {
2094 struct rx_agg *agg = &tp->rx_info[i++];
2095 struct urb *urb = agg->urb;
2096
2097 urb->actual_length = 0;
2098 list_add_tail(&agg->list, &rx_queue);
2099 } while (i < RTL8152_MAX_RX);
2100
2101 spin_lock_irqsave(&tp->rx_lock, flags);
2102 list_splice_tail(&rx_queue, &tp->rx_done);
2103 spin_unlock_irqrestore(&tp->rx_lock, flags);
2104 }
2105
hayeswang445f7f42014-09-23 16:31:47 +08002106 return ret;
2107}
2108
2109static int rtl_stop_rx(struct r8152 *tp)
2110{
2111 int i;
2112
2113 for (i = 0; i < RTL8152_MAX_RX; i++)
2114 usb_kill_urb(tp->rx_info[i].urb);
2115
hayeswangd823ab62015-01-12 12:06:23 +08002116 while (!skb_queue_empty(&tp->rx_queue))
2117 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2118
hayeswang445f7f42014-09-23 16:31:47 +08002119 return 0;
2120}
2121
hayeswang507605a2014-01-02 11:22:43 +08002122static int rtl_enable(struct r8152 *tp)
2123{
2124 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002125
2126 r8152b_reset_packet_filter(tp);
2127
2128 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2129 ocp_data |= CR_RE | CR_TE;
2130 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2131
hayeswang00a5e362014-02-18 21:48:59 +08002132 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002133
hayeswangaa2e0922015-01-09 10:26:35 +08002134 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002135}
2136
hayeswang507605a2014-01-02 11:22:43 +08002137static int rtl8152_enable(struct r8152 *tp)
2138{
hayeswang68714382014-04-11 17:54:31 +08002139 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2140 return -ENODEV;
2141
hayeswang507605a2014-01-02 11:22:43 +08002142 set_tx_qlen(tp);
2143 rtl_set_eee_plus(tp);
2144
2145 return rtl_enable(tp);
2146}
2147
hayeswang464ec102015-02-12 14:33:46 +08002148static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002149{
hayeswang464ec102015-02-12 14:33:46 +08002150 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002151
hayeswang464ec102015-02-12 14:33:46 +08002152 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, ocp_data);
2153}
2154
2155static void r8153_set_rx_early_size(struct r8152 *tp)
2156{
2157 u32 mtu = tp->netdev->mtu;
2158 u32 ocp_data = (agg_buf_sz - mtu - VLAN_ETH_HLEN - VLAN_HLEN) / 4;
2159
2160 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08002161}
2162
2163static int rtl8153_enable(struct r8152 *tp)
2164{
hayeswang68714382014-04-11 17:54:31 +08002165 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2166 return -ENODEV;
2167
hayeswang43779f82014-01-02 11:25:10 +08002168 set_tx_qlen(tp);
2169 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002170 r8153_set_rx_early_timeout(tp);
2171 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002172
2173 return rtl_enable(tp);
2174}
2175
hayeswangd70b1132014-09-19 15:17:18 +08002176static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002177{
hayeswangebc2ec42013-08-14 20:54:38 +08002178 u32 ocp_data;
2179 int i;
hayeswangac718b62013-05-02 16:01:25 +00002180
hayeswang68714382014-04-11 17:54:31 +08002181 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2182 rtl_drop_queued_tx(tp);
2183 return;
2184 }
2185
hayeswangac718b62013-05-02 16:01:25 +00002186 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2187 ocp_data &= ~RCR_ACPT_ALL;
2188 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2189
hayeswang00a5e362014-02-18 21:48:59 +08002190 rtl_drop_queued_tx(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002191
2192 for (i = 0; i < RTL8152_MAX_TX; i++)
2193 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002194
hayeswang00a5e362014-02-18 21:48:59 +08002195 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002196
2197 for (i = 0; i < 1000; i++) {
2198 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2199 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2200 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002201 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002202 }
2203
2204 for (i = 0; i < 1000; i++) {
2205 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2206 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002207 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002208 }
2209
hayeswang445f7f42014-09-23 16:31:47 +08002210 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002211
2212 rtl8152_nic_reset(tp);
2213}
2214
hayeswang00a5e362014-02-18 21:48:59 +08002215static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2216{
2217 u32 ocp_data;
2218
2219 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2220 if (enable)
2221 ocp_data |= POWER_CUT;
2222 else
2223 ocp_data &= ~POWER_CUT;
2224 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2225
2226 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2227 ocp_data &= ~RESUME_INDICATE;
2228 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002229}
2230
hayeswangc5554292014-09-12 10:43:11 +08002231static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2232{
2233 u32 ocp_data;
2234
2235 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2236 if (enable)
2237 ocp_data |= CPCR_RX_VLAN;
2238 else
2239 ocp_data &= ~CPCR_RX_VLAN;
2240 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2241}
2242
2243static int rtl8152_set_features(struct net_device *dev,
2244 netdev_features_t features)
2245{
2246 netdev_features_t changed = features ^ dev->features;
2247 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002248 int ret;
2249
2250 ret = usb_autopm_get_interface(tp->intf);
2251 if (ret < 0)
2252 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002253
hayeswangb5403272014-10-09 18:00:26 +08002254 mutex_lock(&tp->control);
2255
hayeswangc5554292014-09-12 10:43:11 +08002256 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2257 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2258 rtl_rx_vlan_en(tp, true);
2259 else
2260 rtl_rx_vlan_en(tp, false);
2261 }
2262
hayeswangb5403272014-10-09 18:00:26 +08002263 mutex_unlock(&tp->control);
2264
hayeswang405f8a02014-10-09 18:00:24 +08002265 usb_autopm_put_interface(tp->intf);
2266
2267out:
2268 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002269}
2270
hayeswang21ff2e82014-02-18 21:49:06 +08002271#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2272
2273static u32 __rtl_get_wol(struct r8152 *tp)
2274{
2275 u32 ocp_data;
2276 u32 wolopts = 0;
2277
2278 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2279 if (!(ocp_data & LAN_WAKE_EN))
2280 return 0;
2281
2282 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2283 if (ocp_data & LINK_ON_WAKE_EN)
2284 wolopts |= WAKE_PHY;
2285
2286 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2287 if (ocp_data & UWF_EN)
2288 wolopts |= WAKE_UCAST;
2289 if (ocp_data & BWF_EN)
2290 wolopts |= WAKE_BCAST;
2291 if (ocp_data & MWF_EN)
2292 wolopts |= WAKE_MCAST;
2293
2294 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2295 if (ocp_data & MAGIC_EN)
2296 wolopts |= WAKE_MAGIC;
2297
2298 return wolopts;
2299}
2300
2301static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2302{
2303 u32 ocp_data;
2304
2305 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2306
2307 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2308 ocp_data &= ~LINK_ON_WAKE_EN;
2309 if (wolopts & WAKE_PHY)
2310 ocp_data |= LINK_ON_WAKE_EN;
2311 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2312
2313 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2314 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
2315 if (wolopts & WAKE_UCAST)
2316 ocp_data |= UWF_EN;
2317 if (wolopts & WAKE_BCAST)
2318 ocp_data |= BWF_EN;
2319 if (wolopts & WAKE_MCAST)
2320 ocp_data |= MWF_EN;
2321 if (wolopts & WAKE_ANY)
2322 ocp_data |= LAN_WAKE_EN;
2323 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2324
2325 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2326
2327 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2328 ocp_data &= ~MAGIC_EN;
2329 if (wolopts & WAKE_MAGIC)
2330 ocp_data |= MAGIC_EN;
2331 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2332
2333 if (wolopts & WAKE_ANY)
2334 device_set_wakeup_enable(&tp->udev->dev, true);
2335 else
2336 device_set_wakeup_enable(&tp->udev->dev, false);
2337}
2338
hayeswang9a4be1b2014-02-18 21:49:07 +08002339static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2340{
2341 if (enable) {
2342 u32 ocp_data;
2343
2344 __rtl_set_wol(tp, WAKE_ANY);
2345
2346 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2347
2348 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2349 ocp_data |= LINK_OFF_WAKE_EN;
2350 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2351
2352 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2353 } else {
2354 __rtl_set_wol(tp, tp->saved_wolopts);
2355 }
2356}
2357
hayeswangaa66a5f2014-02-18 21:49:04 +08002358static void rtl_phy_reset(struct r8152 *tp)
2359{
2360 u16 data;
2361 int i;
2362
2363 clear_bit(PHY_RESET, &tp->flags);
2364
2365 data = r8152_mdio_read(tp, MII_BMCR);
2366
2367 /* don't reset again before the previous one complete */
2368 if (data & BMCR_RESET)
2369 return;
2370
2371 data |= BMCR_RESET;
2372 r8152_mdio_write(tp, MII_BMCR, data);
2373
2374 for (i = 0; i < 50; i++) {
2375 msleep(20);
2376 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2377 break;
2378 }
2379}
2380
hayeswang43499682014-02-18 21:48:58 +08002381static void r8153_teredo_off(struct r8152 *tp)
2382{
2383 u32 ocp_data;
2384
2385 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2386 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
2387 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2388
2389 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2390 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2391 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2392}
2393
2394static void r8152b_disable_aldps(struct r8152 *tp)
2395{
2396 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
2397 msleep(20);
2398}
2399
2400static inline void r8152b_enable_aldps(struct r8152 *tp)
2401{
2402 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2403 LINKENA | DIS_SDSAVE);
2404}
2405
hayeswangd70b1132014-09-19 15:17:18 +08002406static void rtl8152_disable(struct r8152 *tp)
2407{
2408 r8152b_disable_aldps(tp);
2409 rtl_disable(tp);
2410 r8152b_enable_aldps(tp);
2411}
2412
hayeswang43499682014-02-18 21:48:58 +08002413static void r8152b_hw_phy_cfg(struct r8152 *tp)
2414{
hayeswangf0cbe0a2014-02-18 21:49:03 +08002415 u16 data;
2416
2417 data = r8152_mdio_read(tp, MII_BMCR);
2418 if (data & BMCR_PDOWN) {
2419 data &= ~BMCR_PDOWN;
2420 r8152_mdio_write(tp, MII_BMCR, data);
2421 }
2422
hayeswangaa66a5f2014-02-18 21:49:04 +08002423 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002424}
2425
hayeswangac718b62013-05-02 16:01:25 +00002426static void r8152b_exit_oob(struct r8152 *tp)
2427{
hayeswangdb8515e2014-03-06 15:07:16 +08002428 u32 ocp_data;
2429 int i;
hayeswangac718b62013-05-02 16:01:25 +00002430
2431 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2432 ocp_data &= ~RCR_ACPT_ALL;
2433 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2434
hayeswang00a5e362014-02-18 21:48:59 +08002435 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002436 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002437 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002438
2439 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2440 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2441
2442 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2443 ocp_data &= ~NOW_IS_OOB;
2444 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2445
2446 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2447 ocp_data &= ~MCU_BORW_EN;
2448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2449
2450 for (i = 0; i < 1000; i++) {
2451 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2452 if (ocp_data & LINK_LIST_READY)
2453 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002454 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002455 }
2456
2457 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2458 ocp_data |= RE_INIT_LL;
2459 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2460
2461 for (i = 0; i < 1000; i++) {
2462 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2463 if (ocp_data & LINK_LIST_READY)
2464 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002465 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002466 }
2467
2468 rtl8152_nic_reset(tp);
2469
2470 /* rx share fifo credit full threshold */
2471 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2472
hayeswanga3cc4652014-07-24 16:37:43 +08002473 if (tp->udev->speed == USB_SPEED_FULL ||
2474 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00002475 /* rx share fifo credit near full threshold */
2476 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2477 RXFIFO_THR2_FULL);
2478 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2479 RXFIFO_THR3_FULL);
2480 } else {
2481 /* rx share fifo credit near full threshold */
2482 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2483 RXFIFO_THR2_HIGH);
2484 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2485 RXFIFO_THR3_HIGH);
2486 }
2487
2488 /* TX share fifo free credit full threshold */
2489 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2490
2491 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002492 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002493 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2494 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2495
hayeswangc5554292014-09-12 10:43:11 +08002496 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00002497
2498 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2499
2500 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2501 ocp_data |= TCR0_AUTO_FIFO;
2502 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2503}
2504
2505static void r8152b_enter_oob(struct r8152 *tp)
2506{
hayeswang45f4a192014-01-06 17:08:41 +08002507 u32 ocp_data;
2508 int i;
hayeswangac718b62013-05-02 16:01:25 +00002509
2510 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2511 ocp_data &= ~NOW_IS_OOB;
2512 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2513
2514 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2515 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2516 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2517
hayeswangd70b1132014-09-19 15:17:18 +08002518 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002519
2520 for (i = 0; i < 1000; i++) {
2521 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2522 if (ocp_data & LINK_LIST_READY)
2523 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002524 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002525 }
2526
2527 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2528 ocp_data |= RE_INIT_LL;
2529 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2530
2531 for (i = 0; i < 1000; i++) {
2532 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2533 if (ocp_data & LINK_LIST_READY)
2534 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002535 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002536 }
2537
2538 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2539
hayeswangc5554292014-09-12 10:43:11 +08002540 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002541
2542 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2543 ocp_data |= ALDPS_PROXY_MODE;
2544 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2545
2546 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2547 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2548 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2549
hayeswang00a5e362014-02-18 21:48:59 +08002550 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002551
2552 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2553 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2554 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2555}
2556
hayeswang43779f82014-01-02 11:25:10 +08002557static void r8153_hw_phy_cfg(struct r8152 *tp)
2558{
2559 u32 ocp_data;
2560 u16 data;
2561
2562 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002563 data = r8152_mdio_read(tp, MII_BMCR);
2564 if (data & BMCR_PDOWN) {
2565 data &= ~BMCR_PDOWN;
2566 r8152_mdio_write(tp, MII_BMCR, data);
2567 }
hayeswang43779f82014-01-02 11:25:10 +08002568
2569 if (tp->version == RTL_VER_03) {
2570 data = ocp_reg_read(tp, OCP_EEE_CFG);
2571 data &= ~CTAP_SHORT_EN;
2572 ocp_reg_write(tp, OCP_EEE_CFG, data);
2573 }
2574
2575 data = ocp_reg_read(tp, OCP_POWER_CFG);
2576 data |= EEE_CLKDIV_EN;
2577 ocp_reg_write(tp, OCP_POWER_CFG, data);
2578
2579 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2580 data |= EN_10M_BGOFF;
2581 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2582 data = ocp_reg_read(tp, OCP_POWER_CFG);
2583 data |= EN_10M_PLLOFF;
2584 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08002585 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08002586
2587 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2588 ocp_data |= PFM_PWM_SWITCH;
2589 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2590
hayeswangb4d99de2015-01-19 17:02:46 +08002591 /* Enable LPF corner auto tune */
2592 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08002593
hayeswangb4d99de2015-01-19 17:02:46 +08002594 /* Adjust 10M Amplitude */
2595 sram_write(tp, SRAM_10M_AMP1, 0x00af);
2596 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08002597
2598 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002599}
2600
hayeswangb9702722014-02-18 21:49:00 +08002601static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002602{
2603 u8 u1u2[8];
2604
2605 if (enable)
2606 memset(u1u2, 0xff, sizeof(u1u2));
2607 else
2608 memset(u1u2, 0x00, sizeof(u1u2));
2609
2610 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2611}
2612
hayeswangb9702722014-02-18 21:49:00 +08002613static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002614{
2615 u32 ocp_data;
2616
2617 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2618 if (enable)
2619 ocp_data |= U2P3_ENABLE;
2620 else
2621 ocp_data &= ~U2P3_ENABLE;
2622 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2623}
2624
hayeswangb9702722014-02-18 21:49:00 +08002625static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002626{
2627 u32 ocp_data;
2628
2629 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2630 if (enable)
2631 ocp_data |= PWR_EN | PHASE2_EN;
2632 else
2633 ocp_data &= ~(PWR_EN | PHASE2_EN);
2634 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2635
2636 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2637 ocp_data &= ~PCUT_STATUS;
2638 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2639}
2640
hayeswang43779f82014-01-02 11:25:10 +08002641static void r8153_first_init(struct r8152 *tp)
2642{
2643 u32 ocp_data;
2644 int i;
2645
hayeswang00a5e362014-02-18 21:48:59 +08002646 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002647 r8153_teredo_off(tp);
2648
2649 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2650 ocp_data &= ~RCR_ACPT_ALL;
2651 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2652
2653 r8153_hw_phy_cfg(tp);
2654
2655 rtl8152_nic_reset(tp);
2656
2657 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2658 ocp_data &= ~NOW_IS_OOB;
2659 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2660
2661 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2662 ocp_data &= ~MCU_BORW_EN;
2663 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2664
2665 for (i = 0; i < 1000; i++) {
2666 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2667 if (ocp_data & LINK_LIST_READY)
2668 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002669 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002670 }
2671
2672 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2673 ocp_data |= RE_INIT_LL;
2674 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2675
2676 for (i = 0; i < 1000; i++) {
2677 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2678 if (ocp_data & LINK_LIST_READY)
2679 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002680 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002681 }
2682
hayeswangc5554292014-09-12 10:43:11 +08002683 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08002684
hayeswang69b4b7a2014-07-10 10:58:54 +08002685 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
2686 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08002687
2688 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2689 ocp_data |= TCR0_AUTO_FIFO;
2690 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2691
2692 rtl8152_nic_reset(tp);
2693
2694 /* rx share fifo credit full threshold */
2695 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2696 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2697 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2698 /* TX share fifo free credit full threshold */
2699 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2700
hayeswang9629e3c2014-01-15 10:42:15 +08002701 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002702 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2703 ocp_data &= ~RX_AGG_DISABLE;
2704 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2705}
2706
2707static void r8153_enter_oob(struct r8152 *tp)
2708{
2709 u32 ocp_data;
2710 int i;
2711
2712 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2713 ocp_data &= ~NOW_IS_OOB;
2714 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2715
hayeswangd70b1132014-09-19 15:17:18 +08002716 rtl_disable(tp);
hayeswang43779f82014-01-02 11:25:10 +08002717
2718 for (i = 0; i < 1000; i++) {
2719 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2720 if (ocp_data & LINK_LIST_READY)
2721 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002722 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002723 }
2724
2725 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2726 ocp_data |= RE_INIT_LL;
2727 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2728
2729 for (i = 0; i < 1000; i++) {
2730 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2731 if (ocp_data & LINK_LIST_READY)
2732 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002733 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002734 }
2735
hayeswang69b4b7a2014-07-10 10:58:54 +08002736 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
hayeswang43779f82014-01-02 11:25:10 +08002737
hayeswang43779f82014-01-02 11:25:10 +08002738 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2739 ocp_data &= ~TEREDO_WAKE_MASK;
2740 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2741
hayeswangc5554292014-09-12 10:43:11 +08002742 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002743
2744 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2745 ocp_data |= ALDPS_PROXY_MODE;
2746 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2747
2748 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2749 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2750 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2751
hayeswang00a5e362014-02-18 21:48:59 +08002752 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002753
2754 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2755 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2756 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2757}
2758
2759static void r8153_disable_aldps(struct r8152 *tp)
2760{
2761 u16 data;
2762
2763 data = ocp_reg_read(tp, OCP_POWER_CFG);
2764 data &= ~EN_ALDPS;
2765 ocp_reg_write(tp, OCP_POWER_CFG, data);
2766 msleep(20);
2767}
2768
2769static void r8153_enable_aldps(struct r8152 *tp)
2770{
2771 u16 data;
2772
2773 data = ocp_reg_read(tp, OCP_POWER_CFG);
2774 data |= EN_ALDPS;
2775 ocp_reg_write(tp, OCP_POWER_CFG, data);
2776}
2777
hayeswangd70b1132014-09-19 15:17:18 +08002778static void rtl8153_disable(struct r8152 *tp)
2779{
2780 r8153_disable_aldps(tp);
2781 rtl_disable(tp);
2782 r8153_enable_aldps(tp);
2783}
2784
hayeswangac718b62013-05-02 16:01:25 +00002785static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2786{
hayeswang43779f82014-01-02 11:25:10 +08002787 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002788 int ret = 0;
2789
2790 cancel_delayed_work_sync(&tp->schedule);
2791 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2792 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2793 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002794 if (tp->mii.supports_gmii) {
2795 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2796 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2797 } else {
2798 gbcr = 0;
2799 }
hayeswangac718b62013-05-02 16:01:25 +00002800
2801 if (autoneg == AUTONEG_DISABLE) {
2802 if (speed == SPEED_10) {
2803 bmcr = 0;
2804 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2805 } else if (speed == SPEED_100) {
2806 bmcr = BMCR_SPEED100;
2807 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002808 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2809 bmcr = BMCR_SPEED1000;
2810 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002811 } else {
2812 ret = -EINVAL;
2813 goto out;
2814 }
2815
2816 if (duplex == DUPLEX_FULL)
2817 bmcr |= BMCR_FULLDPLX;
2818 } else {
2819 if (speed == SPEED_10) {
2820 if (duplex == DUPLEX_FULL)
2821 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2822 else
2823 anar |= ADVERTISE_10HALF;
2824 } else if (speed == SPEED_100) {
2825 if (duplex == DUPLEX_FULL) {
2826 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2827 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2828 } else {
2829 anar |= ADVERTISE_10HALF;
2830 anar |= ADVERTISE_100HALF;
2831 }
hayeswang43779f82014-01-02 11:25:10 +08002832 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2833 if (duplex == DUPLEX_FULL) {
2834 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2835 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2836 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2837 } else {
2838 anar |= ADVERTISE_10HALF;
2839 anar |= ADVERTISE_100HALF;
2840 gbcr |= ADVERTISE_1000HALF;
2841 }
hayeswangac718b62013-05-02 16:01:25 +00002842 } else {
2843 ret = -EINVAL;
2844 goto out;
2845 }
2846
2847 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2848 }
2849
hayeswangaa66a5f2014-02-18 21:49:04 +08002850 if (test_bit(PHY_RESET, &tp->flags))
2851 bmcr |= BMCR_RESET;
2852
hayeswang43779f82014-01-02 11:25:10 +08002853 if (tp->mii.supports_gmii)
2854 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2855
hayeswangac718b62013-05-02 16:01:25 +00002856 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2857 r8152_mdio_write(tp, MII_BMCR, bmcr);
2858
hayeswangaa66a5f2014-02-18 21:49:04 +08002859 if (test_bit(PHY_RESET, &tp->flags)) {
2860 int i;
2861
2862 clear_bit(PHY_RESET, &tp->flags);
2863 for (i = 0; i < 50; i++) {
2864 msleep(20);
2865 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2866 break;
2867 }
2868 }
2869
hayeswangac718b62013-05-02 16:01:25 +00002870out:
hayeswangac718b62013-05-02 16:01:25 +00002871
2872 return ret;
2873}
2874
hayeswangd70b1132014-09-19 15:17:18 +08002875static void rtl8152_up(struct r8152 *tp)
2876{
2877 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2878 return;
2879
2880 r8152b_disable_aldps(tp);
2881 r8152b_exit_oob(tp);
2882 r8152b_enable_aldps(tp);
2883}
2884
hayeswangac718b62013-05-02 16:01:25 +00002885static void rtl8152_down(struct r8152 *tp)
2886{
hayeswang68714382014-04-11 17:54:31 +08002887 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2888 rtl_drop_queued_tx(tp);
2889 return;
2890 }
2891
hayeswang00a5e362014-02-18 21:48:59 +08002892 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002893 r8152b_disable_aldps(tp);
2894 r8152b_enter_oob(tp);
2895 r8152b_enable_aldps(tp);
2896}
2897
hayeswangd70b1132014-09-19 15:17:18 +08002898static void rtl8153_up(struct r8152 *tp)
2899{
2900 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2901 return;
2902
2903 r8153_disable_aldps(tp);
2904 r8153_first_init(tp);
2905 r8153_enable_aldps(tp);
2906}
2907
hayeswang43779f82014-01-02 11:25:10 +08002908static void rtl8153_down(struct r8152 *tp)
2909{
hayeswang68714382014-04-11 17:54:31 +08002910 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2911 rtl_drop_queued_tx(tp);
2912 return;
2913 }
2914
hayeswangb9702722014-02-18 21:49:00 +08002915 r8153_u1u2en(tp, false);
2916 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002917 r8153_disable_aldps(tp);
2918 r8153_enter_oob(tp);
2919 r8153_enable_aldps(tp);
2920}
2921
hayeswangac718b62013-05-02 16:01:25 +00002922static void set_carrier(struct r8152 *tp)
2923{
2924 struct net_device *netdev = tp->netdev;
2925 u8 speed;
2926
hayeswang40a82912013-08-14 20:54:40 +08002927 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002928 speed = rtl8152_get_speed(tp);
2929
2930 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08002931 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08002932 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002933 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2934 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08002935 rtl_start_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002936 }
2937 } else {
hayeswang51d979f2015-02-06 11:30:47 +08002938 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002939 netif_carrier_off(netdev);
hayeswangd823ab62015-01-12 12:06:23 +08002940 napi_disable(&tp->napi);
hayeswangc81229c2014-01-02 11:22:42 +08002941 tp->rtl_ops.disable(tp);
hayeswangd823ab62015-01-12 12:06:23 +08002942 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00002943 }
2944 }
hayeswangac718b62013-05-02 16:01:25 +00002945}
2946
2947static void rtl_work_func_t(struct work_struct *work)
2948{
2949 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2950
hayeswanga1f83fe2014-11-12 10:05:05 +08002951 /* If the device is unplugged or !netif_running(), the workqueue
2952 * doesn't need to wake the device, and could return directly.
2953 */
2954 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
2955 return;
2956
hayeswang9a4be1b2014-02-18 21:49:07 +08002957 if (usb_autopm_get_interface(tp->intf) < 0)
2958 return;
2959
hayeswangac718b62013-05-02 16:01:25 +00002960 if (!test_bit(WORK_ENABLE, &tp->flags))
2961 goto out1;
2962
hayeswangb5403272014-10-09 18:00:26 +08002963 if (!mutex_trylock(&tp->control)) {
2964 schedule_delayed_work(&tp->schedule, 0);
2965 goto out1;
2966 }
2967
hayeswang40a82912013-08-14 20:54:40 +08002968 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2969 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002970
2971 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2972 _rtl8152_set_rx_mode(tp->netdev);
2973
hayeswangd823ab62015-01-12 12:06:23 +08002974 /* don't schedule napi before linking */
2975 if (test_bit(SCHEDULE_NAPI, &tp->flags) &&
hayeswang51d979f2015-02-06 11:30:47 +08002976 netif_carrier_ok(tp->netdev)) {
hayeswangd823ab62015-01-12 12:06:23 +08002977 clear_bit(SCHEDULE_NAPI, &tp->flags);
2978 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002979 }
hayeswangaa66a5f2014-02-18 21:49:04 +08002980
2981 if (test_bit(PHY_RESET, &tp->flags))
2982 rtl_phy_reset(tp);
2983
hayeswangb5403272014-10-09 18:00:26 +08002984 mutex_unlock(&tp->control);
2985
hayeswangac718b62013-05-02 16:01:25 +00002986out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08002987 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002988}
2989
2990static int rtl8152_open(struct net_device *netdev)
2991{
2992 struct r8152 *tp = netdev_priv(netdev);
2993 int res = 0;
2994
hayeswang7e9da482014-02-18 21:49:05 +08002995 res = alloc_all_mem(tp);
2996 if (res)
2997 goto out;
2998
hayeswang51d979f2015-02-06 11:30:47 +08002999 netif_carrier_off(netdev);
hayeswangf4c74762014-10-29 11:12:16 +08003000
hayeswang9a4be1b2014-02-18 21:49:07 +08003001 res = usb_autopm_get_interface(tp->intf);
3002 if (res < 0) {
3003 free_all_mem(tp);
3004 goto out;
3005 }
3006
hayeswangb5403272014-10-09 18:00:26 +08003007 mutex_lock(&tp->control);
3008
hayeswang9a4be1b2014-02-18 21:49:07 +08003009 /* The WORK_ENABLE may be set when autoresume occurs */
3010 if (test_bit(WORK_ENABLE, &tp->flags)) {
3011 clear_bit(WORK_ENABLE, &tp->flags);
3012 usb_kill_urb(tp->intr_urb);
3013 cancel_delayed_work_sync(&tp->schedule);
hayeswangf4c74762014-10-29 11:12:16 +08003014
3015 /* disable the tx/rx, if the workqueue has enabled them. */
hayeswang51d979f2015-02-06 11:30:47 +08003016 if (netif_carrier_ok(netdev))
hayeswang9a4be1b2014-02-18 21:49:07 +08003017 tp->rtl_ops.disable(tp);
3018 }
3019
hayeswang7e9da482014-02-18 21:49:05 +08003020 tp->rtl_ops.up(tp);
3021
hayeswang43779f82014-01-02 11:25:10 +08003022 rtl8152_set_speed(tp, AUTONEG_ENABLE,
3023 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
3024 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08003025 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003026 netif_start_queue(netdev);
3027 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08003028
hayeswang3d55f442014-02-06 11:55:48 +08003029 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3030 if (res) {
3031 if (res == -ENODEV)
3032 netif_device_detach(tp->netdev);
3033 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3034 res);
hayeswang7e9da482014-02-18 21:49:05 +08003035 free_all_mem(tp);
hayeswang93ffbea2014-11-05 10:17:02 +08003036 } else {
hayeswangd823ab62015-01-12 12:06:23 +08003037 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08003038 }
3039
hayeswangb5403272014-10-09 18:00:26 +08003040 mutex_unlock(&tp->control);
3041
hayeswang9a4be1b2014-02-18 21:49:07 +08003042 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003043
hayeswang7e9da482014-02-18 21:49:05 +08003044out:
hayeswangac718b62013-05-02 16:01:25 +00003045 return res;
3046}
3047
3048static int rtl8152_close(struct net_device *netdev)
3049{
3050 struct r8152 *tp = netdev_priv(netdev);
3051 int res = 0;
3052
hayeswangd823ab62015-01-12 12:06:23 +08003053 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003054 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08003055 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00003056 cancel_delayed_work_sync(&tp->schedule);
3057 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003058
3059 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08003060 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003061 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08003062 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003063 } else {
hayeswangb5403272014-10-09 18:00:26 +08003064 mutex_lock(&tp->control);
3065
hayeswangb209af92014-08-25 15:53:00 +08003066 /* The autosuspend may have been enabled and wouldn't
hayeswang9a4be1b2014-02-18 21:49:07 +08003067 * be disable when autoresume occurs, because the
3068 * netif_running() would be false.
3069 */
hayeswang923e1ee2014-10-29 11:12:15 +08003070 rtl_runtime_suspend_enable(tp, false);
hayeswang9a4be1b2014-02-18 21:49:07 +08003071
hayeswang9a4be1b2014-02-18 21:49:07 +08003072 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08003073
3074 mutex_unlock(&tp->control);
3075
hayeswang9a4be1b2014-02-18 21:49:07 +08003076 usb_autopm_put_interface(tp->intf);
3077 }
hayeswangac718b62013-05-02 16:01:25 +00003078
hayeswang7e9da482014-02-18 21:49:05 +08003079 free_all_mem(tp);
3080
hayeswangac718b62013-05-02 16:01:25 +00003081 return res;
3082}
3083
hayeswangd24f6132014-09-25 20:54:01 +08003084static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
hayeswangac718b62013-05-02 16:01:25 +00003085{
hayeswangd24f6132014-09-25 20:54:01 +08003086 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3087 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3088 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3089}
3090
3091static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3092{
3093 u16 data;
3094
3095 r8152_mmd_indirect(tp, dev, reg);
3096 data = ocp_reg_read(tp, OCP_EEE_DATA);
3097 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3098
3099 return data;
3100}
3101
3102static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3103{
3104 r8152_mmd_indirect(tp, dev, reg);
3105 ocp_reg_write(tp, OCP_EEE_DATA, data);
3106 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3107}
3108
3109static void r8152_eee_en(struct r8152 *tp, bool enable)
3110{
3111 u16 config1, config2, config3;
hayeswang45f4a192014-01-06 17:08:41 +08003112 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003113
3114 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
hayeswangd24f6132014-09-25 20:54:01 +08003115 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3116 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3117 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3118
3119 if (enable) {
3120 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3121 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3122 config1 |= sd_rise_time(1);
3123 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3124 config3 |= fast_snr(42);
3125 } else {
3126 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3127 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3128 RX_QUIET_EN);
3129 config1 |= sd_rise_time(7);
3130 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3131 config3 |= fast_snr(511);
3132 }
3133
hayeswangac718b62013-05-02 16:01:25 +00003134 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
hayeswangd24f6132014-09-25 20:54:01 +08003135 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3136 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3137 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3138}
3139
3140static void r8152b_enable_eee(struct r8152 *tp)
3141{
3142 r8152_eee_en(tp, true);
3143 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3144}
3145
3146static void r8153_eee_en(struct r8152 *tp, bool enable)
3147{
3148 u32 ocp_data;
3149 u16 config;
3150
3151 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3152 config = ocp_reg_read(tp, OCP_EEE_CFG);
3153
3154 if (enable) {
3155 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3156 config |= EEE10_EN;
3157 } else {
3158 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3159 config &= ~EEE10_EN;
3160 }
3161
3162 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3163 ocp_reg_write(tp, OCP_EEE_CFG, config);
hayeswangac718b62013-05-02 16:01:25 +00003164}
3165
hayeswang43779f82014-01-02 11:25:10 +08003166static void r8153_enable_eee(struct r8152 *tp)
3167{
hayeswangd24f6132014-09-25 20:54:01 +08003168 r8153_eee_en(tp, true);
3169 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
hayeswang43779f82014-01-02 11:25:10 +08003170}
3171
hayeswangac718b62013-05-02 16:01:25 +00003172static void r8152b_enable_fc(struct r8152 *tp)
3173{
3174 u16 anar;
3175
3176 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3177 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3178 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3179}
3180
hayeswang4f1d4d52014-03-11 16:24:19 +08003181static void rtl_tally_reset(struct r8152 *tp)
3182{
3183 u32 ocp_data;
3184
3185 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
3186 ocp_data |= TALLY_RESET;
3187 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
3188}
3189
hayeswangac718b62013-05-02 16:01:25 +00003190static void r8152b_init(struct r8152 *tp)
3191{
hayeswangebc2ec42013-08-14 20:54:38 +08003192 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003193
hayeswang68714382014-04-11 17:54:31 +08003194 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3195 return;
3196
hayeswangd70b1132014-09-19 15:17:18 +08003197 r8152b_disable_aldps(tp);
3198
hayeswangac718b62013-05-02 16:01:25 +00003199 if (tp->version == RTL_VER_01) {
3200 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3201 ocp_data &= ~LED_MODE_MASK;
3202 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3203 }
3204
hayeswang00a5e362014-02-18 21:48:59 +08003205 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003206
hayeswangac718b62013-05-02 16:01:25 +00003207 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3208 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
3209 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3210 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
3211 ocp_data &= ~MCU_CLK_RATIO_MASK;
3212 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
3213 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
3214 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
3215 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
3216 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
3217
3218 r8152b_enable_eee(tp);
3219 r8152b_enable_aldps(tp);
3220 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003221 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00003222
hayeswangebc2ec42013-08-14 20:54:38 +08003223 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00003224 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec42013-08-14 20:54:38 +08003225 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00003226 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
3227}
3228
hayeswang43779f82014-01-02 11:25:10 +08003229static void r8153_init(struct r8152 *tp)
3230{
3231 u32 ocp_data;
3232 int i;
3233
hayeswang68714382014-04-11 17:54:31 +08003234 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3235 return;
3236
hayeswangd70b1132014-09-19 15:17:18 +08003237 r8153_disable_aldps(tp);
hayeswangb9702722014-02-18 21:49:00 +08003238 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003239
3240 for (i = 0; i < 500; i++) {
3241 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3242 AUTOLOAD_DONE)
3243 break;
3244 msleep(20);
3245 }
3246
3247 for (i = 0; i < 500; i++) {
3248 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
3249 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
3250 break;
3251 msleep(20);
3252 }
3253
hayeswangb9702722014-02-18 21:49:00 +08003254 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003255
hayeswang65bab842015-02-12 16:20:46 +08003256 if (tp->version == RTL_VER_04) {
3257 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
3258 ocp_data &= ~pwd_dn_scale_mask;
3259 ocp_data |= pwd_dn_scale(96);
3260 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
3261
3262 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
3263 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
3264 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
3265 } else if (tp->version == RTL_VER_05) {
3266 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
3267 ocp_data &= ~ECM_ALDPS;
3268 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
3269
3270 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
3271 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
3272 ocp_data &= ~DYNAMIC_BURST;
3273 else
3274 ocp_data |= DYNAMIC_BURST;
3275 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
3276 }
3277
3278 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
3279 ocp_data |= EP4_FULL_FC;
3280 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
3281
hayeswang43779f82014-01-02 11:25:10 +08003282 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
3283 ocp_data &= ~TIMER11_EN;
3284 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
3285
hayeswang43779f82014-01-02 11:25:10 +08003286 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3287 ocp_data &= ~LED_MODE_MASK;
3288 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3289
hayeswang65bab842015-02-12 16:20:46 +08003290 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
hayeswang34203e22015-02-06 11:30:46 +08003291 if (tp->version == RTL_VER_04 && tp->udev->speed != USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08003292 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08003293 else
3294 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08003295 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
3296
3297 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
3298 ocp_data &= ~SEN_VAL_MASK;
3299 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
3300 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
3301
hayeswang65bab842015-02-12 16:20:46 +08003302 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
3303
hayeswangb9702722014-02-18 21:49:00 +08003304 r8153_power_cut_en(tp, false);
3305 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003306
hayeswang43779f82014-01-02 11:25:10 +08003307 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
3308 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
3309 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3310 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3311 U1U2_SPDWN_EN | L1_SPDWN_EN);
3312 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3313 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3314 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
3315 EEE_SPDWN_EN);
3316
3317 r8153_enable_eee(tp);
3318 r8153_enable_aldps(tp);
3319 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003320 rtl_tally_reset(tp);
hayeswang43779f82014-01-02 11:25:10 +08003321}
3322
hayeswangac718b62013-05-02 16:01:25 +00003323static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
3324{
3325 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang6cc69f22014-10-17 16:55:08 +08003326 struct net_device *netdev = tp->netdev;
3327 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00003328
hayeswangb5403272014-10-09 18:00:26 +08003329 mutex_lock(&tp->control);
3330
hayeswang6cc69f22014-10-17 16:55:08 +08003331 if (PMSG_IS_AUTO(message)) {
3332 if (netif_running(netdev) && work_busy(&tp->schedule.work)) {
3333 ret = -EBUSY;
3334 goto out1;
3335 }
hayeswangac718b62013-05-02 16:01:25 +00003336
hayeswang6cc69f22014-10-17 16:55:08 +08003337 set_bit(SELECTIVE_SUSPEND, &tp->flags);
3338 } else {
3339 netif_device_detach(netdev);
3340 }
3341
hayeswange3bd1a82014-10-29 11:12:17 +08003342 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangac718b62013-05-02 16:01:25 +00003343 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08003344 usb_kill_urb(tp->intr_urb);
hayeswangd823ab62015-01-12 12:06:23 +08003345 napi_disable(&tp->napi);
hayeswang9a4be1b2014-02-18 21:49:07 +08003346 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswang445f7f42014-09-23 16:31:47 +08003347 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003348 rtl_runtime_suspend_enable(tp, true);
3349 } else {
hayeswang6cc69f22014-10-17 16:55:08 +08003350 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08003351 tp->rtl_ops.down(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003352 }
hayeswangd823ab62015-01-12 12:06:23 +08003353 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003354 }
hayeswang6cc69f22014-10-17 16:55:08 +08003355out1:
hayeswangb5403272014-10-09 18:00:26 +08003356 mutex_unlock(&tp->control);
3357
hayeswang6cc69f22014-10-17 16:55:08 +08003358 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003359}
3360
3361static int rtl8152_resume(struct usb_interface *intf)
3362{
3363 struct r8152 *tp = usb_get_intfdata(intf);
3364
hayeswangb5403272014-10-09 18:00:26 +08003365 mutex_lock(&tp->control);
3366
hayeswang9a4be1b2014-02-18 21:49:07 +08003367 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3368 tp->rtl_ops.init(tp);
3369 netif_device_attach(tp->netdev);
3370 }
3371
hayeswangac718b62013-05-02 16:01:25 +00003372 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003373 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3374 rtl_runtime_suspend_enable(tp, false);
3375 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswang445f7f42014-09-23 16:31:47 +08003376 set_bit(WORK_ENABLE, &tp->flags);
hayeswang51d979f2015-02-06 11:30:47 +08003377 if (netif_carrier_ok(tp->netdev))
hayeswang445f7f42014-09-23 16:31:47 +08003378 rtl_start_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003379 } else {
3380 tp->rtl_ops.up(tp);
3381 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswangb209af92014-08-25 15:53:00 +08003382 tp->mii.supports_gmii ?
3383 SPEED_1000 : SPEED_100,
3384 DUPLEX_FULL);
hayeswang445f7f42014-09-23 16:31:47 +08003385 netif_carrier_off(tp->netdev);
3386 set_bit(WORK_ENABLE, &tp->flags);
hayeswang9a4be1b2014-02-18 21:49:07 +08003387 }
hayeswang40a82912013-08-14 20:54:40 +08003388 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswang923e1ee2014-10-29 11:12:15 +08003389 } else if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3390 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00003391 }
3392
hayeswangb5403272014-10-09 18:00:26 +08003393 mutex_unlock(&tp->control);
3394
hayeswangac718b62013-05-02 16:01:25 +00003395 return 0;
3396}
3397
hayeswang21ff2e82014-02-18 21:49:06 +08003398static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3399{
3400 struct r8152 *tp = netdev_priv(dev);
3401
hayeswang9a4be1b2014-02-18 21:49:07 +08003402 if (usb_autopm_get_interface(tp->intf) < 0)
3403 return;
3404
hayeswangb5403272014-10-09 18:00:26 +08003405 mutex_lock(&tp->control);
3406
hayeswang21ff2e82014-02-18 21:49:06 +08003407 wol->supported = WAKE_ANY;
3408 wol->wolopts = __rtl_get_wol(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003409
hayeswangb5403272014-10-09 18:00:26 +08003410 mutex_unlock(&tp->control);
3411
hayeswang9a4be1b2014-02-18 21:49:07 +08003412 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08003413}
3414
3415static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3416{
3417 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003418 int ret;
3419
3420 ret = usb_autopm_get_interface(tp->intf);
3421 if (ret < 0)
3422 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08003423
hayeswangb5403272014-10-09 18:00:26 +08003424 mutex_lock(&tp->control);
3425
hayeswang21ff2e82014-02-18 21:49:06 +08003426 __rtl_set_wol(tp, wol->wolopts);
3427 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
3428
hayeswangb5403272014-10-09 18:00:26 +08003429 mutex_unlock(&tp->control);
3430
hayeswang9a4be1b2014-02-18 21:49:07 +08003431 usb_autopm_put_interface(tp->intf);
3432
3433out_set_wol:
3434 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08003435}
3436
hayeswanga5ec27c2014-02-18 21:49:11 +08003437static u32 rtl8152_get_msglevel(struct net_device *dev)
3438{
3439 struct r8152 *tp = netdev_priv(dev);
3440
3441 return tp->msg_enable;
3442}
3443
3444static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
3445{
3446 struct r8152 *tp = netdev_priv(dev);
3447
3448 tp->msg_enable = value;
3449}
3450
hayeswangac718b62013-05-02 16:01:25 +00003451static void rtl8152_get_drvinfo(struct net_device *netdev,
3452 struct ethtool_drvinfo *info)
3453{
3454 struct r8152 *tp = netdev_priv(netdev);
3455
hayeswangb0b46c72014-08-26 10:08:23 +08003456 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
3457 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00003458 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
3459}
3460
3461static
3462int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
3463{
3464 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08003465 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003466
3467 if (!tp->mii.mdio_read)
3468 return -EOPNOTSUPP;
3469
hayeswang8d4a4d72014-10-09 18:00:25 +08003470 ret = usb_autopm_get_interface(tp->intf);
3471 if (ret < 0)
3472 goto out;
3473
hayeswangb5403272014-10-09 18:00:26 +08003474 mutex_lock(&tp->control);
3475
hayeswang8d4a4d72014-10-09 18:00:25 +08003476 ret = mii_ethtool_gset(&tp->mii, cmd);
3477
hayeswangb5403272014-10-09 18:00:26 +08003478 mutex_unlock(&tp->control);
3479
hayeswang8d4a4d72014-10-09 18:00:25 +08003480 usb_autopm_put_interface(tp->intf);
3481
3482out:
3483 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003484}
3485
3486static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3487{
3488 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003489 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003490
hayeswang9a4be1b2014-02-18 21:49:07 +08003491 ret = usb_autopm_get_interface(tp->intf);
3492 if (ret < 0)
3493 goto out;
3494
hayeswangb5403272014-10-09 18:00:26 +08003495 mutex_lock(&tp->control);
3496
hayeswang9a4be1b2014-02-18 21:49:07 +08003497 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
3498
hayeswangb5403272014-10-09 18:00:26 +08003499 mutex_unlock(&tp->control);
3500
hayeswang9a4be1b2014-02-18 21:49:07 +08003501 usb_autopm_put_interface(tp->intf);
3502
3503out:
3504 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003505}
3506
hayeswang4f1d4d52014-03-11 16:24:19 +08003507static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
3508 "tx_packets",
3509 "rx_packets",
3510 "tx_errors",
3511 "rx_errors",
3512 "rx_missed",
3513 "align_errors",
3514 "tx_single_collisions",
3515 "tx_multi_collisions",
3516 "rx_unicast",
3517 "rx_broadcast",
3518 "rx_multicast",
3519 "tx_aborted",
3520 "tx_underrun",
3521};
3522
3523static int rtl8152_get_sset_count(struct net_device *dev, int sset)
3524{
3525 switch (sset) {
3526 case ETH_SS_STATS:
3527 return ARRAY_SIZE(rtl8152_gstrings);
3528 default:
3529 return -EOPNOTSUPP;
3530 }
3531}
3532
3533static void rtl8152_get_ethtool_stats(struct net_device *dev,
3534 struct ethtool_stats *stats, u64 *data)
3535{
3536 struct r8152 *tp = netdev_priv(dev);
3537 struct tally_counter tally;
3538
hayeswang0b030242014-07-08 14:49:28 +08003539 if (usb_autopm_get_interface(tp->intf) < 0)
3540 return;
3541
hayeswang4f1d4d52014-03-11 16:24:19 +08003542 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
3543
hayeswang0b030242014-07-08 14:49:28 +08003544 usb_autopm_put_interface(tp->intf);
3545
hayeswang4f1d4d52014-03-11 16:24:19 +08003546 data[0] = le64_to_cpu(tally.tx_packets);
3547 data[1] = le64_to_cpu(tally.rx_packets);
3548 data[2] = le64_to_cpu(tally.tx_errors);
3549 data[3] = le32_to_cpu(tally.rx_errors);
3550 data[4] = le16_to_cpu(tally.rx_missed);
3551 data[5] = le16_to_cpu(tally.align_errors);
3552 data[6] = le32_to_cpu(tally.tx_one_collision);
3553 data[7] = le32_to_cpu(tally.tx_multi_collision);
3554 data[8] = le64_to_cpu(tally.rx_unicast);
3555 data[9] = le64_to_cpu(tally.rx_broadcast);
3556 data[10] = le32_to_cpu(tally.rx_multicast);
3557 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08003558 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08003559}
3560
3561static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3562{
3563 switch (stringset) {
3564 case ETH_SS_STATS:
3565 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
3566 break;
3567 }
3568}
3569
hayeswangdf35d282014-09-25 20:54:02 +08003570static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3571{
3572 u32 ocp_data, lp, adv, supported = 0;
3573 u16 val;
3574
3575 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
3576 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3577
3578 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
3579 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3580
3581 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
3582 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3583
3584 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3585 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3586
3587 eee->eee_enabled = !!ocp_data;
3588 eee->eee_active = !!(supported & adv & lp);
3589 eee->supported = supported;
3590 eee->advertised = adv;
3591 eee->lp_advertised = lp;
3592
3593 return 0;
3594}
3595
3596static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3597{
3598 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3599
3600 r8152_eee_en(tp, eee->eee_enabled);
3601
3602 if (!eee->eee_enabled)
3603 val = 0;
3604
3605 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
3606
3607 return 0;
3608}
3609
3610static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3611{
3612 u32 ocp_data, lp, adv, supported = 0;
3613 u16 val;
3614
3615 val = ocp_reg_read(tp, OCP_EEE_ABLE);
3616 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3617
3618 val = ocp_reg_read(tp, OCP_EEE_ADV);
3619 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3620
3621 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
3622 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3623
3624 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3625 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3626
3627 eee->eee_enabled = !!ocp_data;
3628 eee->eee_active = !!(supported & adv & lp);
3629 eee->supported = supported;
3630 eee->advertised = adv;
3631 eee->lp_advertised = lp;
3632
3633 return 0;
3634}
3635
3636static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3637{
3638 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3639
3640 r8153_eee_en(tp, eee->eee_enabled);
3641
3642 if (!eee->eee_enabled)
3643 val = 0;
3644
3645 ocp_reg_write(tp, OCP_EEE_ADV, val);
3646
3647 return 0;
3648}
3649
3650static int
3651rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
3652{
3653 struct r8152 *tp = netdev_priv(net);
3654 int ret;
3655
3656 ret = usb_autopm_get_interface(tp->intf);
3657 if (ret < 0)
3658 goto out;
3659
hayeswangb5403272014-10-09 18:00:26 +08003660 mutex_lock(&tp->control);
3661
hayeswangdf35d282014-09-25 20:54:02 +08003662 ret = tp->rtl_ops.eee_get(tp, edata);
3663
hayeswangb5403272014-10-09 18:00:26 +08003664 mutex_unlock(&tp->control);
3665
hayeswangdf35d282014-09-25 20:54:02 +08003666 usb_autopm_put_interface(tp->intf);
3667
3668out:
3669 return ret;
3670}
3671
3672static int
3673rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
3674{
3675 struct r8152 *tp = netdev_priv(net);
3676 int ret;
3677
3678 ret = usb_autopm_get_interface(tp->intf);
3679 if (ret < 0)
3680 goto out;
3681
hayeswangb5403272014-10-09 18:00:26 +08003682 mutex_lock(&tp->control);
3683
hayeswangdf35d282014-09-25 20:54:02 +08003684 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08003685 if (!ret)
3686 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08003687
hayeswangb5403272014-10-09 18:00:26 +08003688 mutex_unlock(&tp->control);
3689
hayeswangdf35d282014-09-25 20:54:02 +08003690 usb_autopm_put_interface(tp->intf);
3691
3692out:
3693 return ret;
3694}
3695
hayeswang8884f502014-10-28 14:05:52 +08003696static int rtl8152_nway_reset(struct net_device *dev)
3697{
3698 struct r8152 *tp = netdev_priv(dev);
3699 int ret;
3700
3701 ret = usb_autopm_get_interface(tp->intf);
3702 if (ret < 0)
3703 goto out;
3704
3705 mutex_lock(&tp->control);
3706
3707 ret = mii_nway_restart(&tp->mii);
3708
3709 mutex_unlock(&tp->control);
3710
3711 usb_autopm_put_interface(tp->intf);
3712
3713out:
3714 return ret;
3715}
3716
hayeswangefb3dd82015-02-12 14:33:48 +08003717static int rtl8152_get_coalesce(struct net_device *netdev,
3718 struct ethtool_coalesce *coalesce)
3719{
3720 struct r8152 *tp = netdev_priv(netdev);
3721
3722 switch (tp->version) {
3723 case RTL_VER_01:
3724 case RTL_VER_02:
3725 return -EOPNOTSUPP;
3726 default:
3727 break;
3728 }
3729
3730 coalesce->rx_coalesce_usecs = tp->coalesce;
3731
3732 return 0;
3733}
3734
3735static int rtl8152_set_coalesce(struct net_device *netdev,
3736 struct ethtool_coalesce *coalesce)
3737{
3738 struct r8152 *tp = netdev_priv(netdev);
3739 int ret;
3740
3741 switch (tp->version) {
3742 case RTL_VER_01:
3743 case RTL_VER_02:
3744 return -EOPNOTSUPP;
3745 default:
3746 break;
3747 }
3748
3749 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
3750 return -EINVAL;
3751
3752 ret = usb_autopm_get_interface(tp->intf);
3753 if (ret < 0)
3754 return ret;
3755
3756 mutex_lock(&tp->control);
3757
3758 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
3759 tp->coalesce = coalesce->rx_coalesce_usecs;
3760
3761 if (netif_running(tp->netdev) && netif_carrier_ok(netdev))
3762 r8153_set_rx_early_timeout(tp);
3763 }
3764
3765 mutex_unlock(&tp->control);
3766
3767 usb_autopm_put_interface(tp->intf);
3768
3769 return ret;
3770}
3771
hayeswangac718b62013-05-02 16:01:25 +00003772static struct ethtool_ops ops = {
3773 .get_drvinfo = rtl8152_get_drvinfo,
3774 .get_settings = rtl8152_get_settings,
3775 .set_settings = rtl8152_set_settings,
3776 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08003777 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08003778 .get_msglevel = rtl8152_get_msglevel,
3779 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08003780 .get_wol = rtl8152_get_wol,
3781 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08003782 .get_strings = rtl8152_get_strings,
3783 .get_sset_count = rtl8152_get_sset_count,
3784 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08003785 .get_coalesce = rtl8152_get_coalesce,
3786 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08003787 .get_eee = rtl_ethtool_get_eee,
3788 .set_eee = rtl_ethtool_set_eee,
hayeswangac718b62013-05-02 16:01:25 +00003789};
3790
3791static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
3792{
3793 struct r8152 *tp = netdev_priv(netdev);
3794 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08003795 int res;
3796
hayeswang68714382014-04-11 17:54:31 +08003797 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3798 return -ENODEV;
3799
hayeswang9a4be1b2014-02-18 21:49:07 +08003800 res = usb_autopm_get_interface(tp->intf);
3801 if (res < 0)
3802 goto out;
hayeswangac718b62013-05-02 16:01:25 +00003803
3804 switch (cmd) {
3805 case SIOCGMIIPHY:
3806 data->phy_id = R8152_PHY_ID; /* Internal PHY */
3807 break;
3808
3809 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08003810 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003811 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08003812 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003813 break;
3814
3815 case SIOCSMIIREG:
3816 if (!capable(CAP_NET_ADMIN)) {
3817 res = -EPERM;
3818 break;
3819 }
hayeswangb5403272014-10-09 18:00:26 +08003820 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003821 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08003822 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003823 break;
3824
3825 default:
3826 res = -EOPNOTSUPP;
3827 }
3828
hayeswang9a4be1b2014-02-18 21:49:07 +08003829 usb_autopm_put_interface(tp->intf);
3830
3831out:
hayeswangac718b62013-05-02 16:01:25 +00003832 return res;
3833}
3834
hayeswang69b4b7a2014-07-10 10:58:54 +08003835static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
3836{
3837 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08003838 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08003839
3840 switch (tp->version) {
3841 case RTL_VER_01:
3842 case RTL_VER_02:
3843 return eth_change_mtu(dev, new_mtu);
3844 default:
3845 break;
3846 }
3847
3848 if (new_mtu < 68 || new_mtu > RTL8153_MAX_MTU)
3849 return -EINVAL;
3850
hayeswang396e2e22015-02-12 14:33:47 +08003851 ret = usb_autopm_get_interface(tp->intf);
3852 if (ret < 0)
3853 return ret;
3854
3855 mutex_lock(&tp->control);
3856
hayeswang69b4b7a2014-07-10 10:58:54 +08003857 dev->mtu = new_mtu;
3858
hayeswang396e2e22015-02-12 14:33:47 +08003859 if (netif_running(dev) && netif_carrier_ok(dev))
3860 r8153_set_rx_early_size(tp);
3861
3862 mutex_unlock(&tp->control);
3863
3864 usb_autopm_put_interface(tp->intf);
3865
3866 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08003867}
3868
hayeswangac718b62013-05-02 16:01:25 +00003869static const struct net_device_ops rtl8152_netdev_ops = {
3870 .ndo_open = rtl8152_open,
3871 .ndo_stop = rtl8152_close,
3872 .ndo_do_ioctl = rtl8152_ioctl,
3873 .ndo_start_xmit = rtl8152_start_xmit,
3874 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08003875 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00003876 .ndo_set_rx_mode = rtl8152_set_rx_mode,
3877 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08003878 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00003879 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08003880 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00003881};
3882
3883static void r8152b_get_version(struct r8152 *tp)
3884{
3885 u32 ocp_data;
3886 u16 version;
3887
3888 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3889 version = (u16)(ocp_data & VERSION_MASK);
3890
3891 switch (version) {
3892 case 0x4c00:
3893 tp->version = RTL_VER_01;
3894 break;
3895 case 0x4c10:
3896 tp->version = RTL_VER_02;
3897 break;
hayeswang43779f82014-01-02 11:25:10 +08003898 case 0x5c00:
3899 tp->version = RTL_VER_03;
3900 tp->mii.supports_gmii = 1;
3901 break;
3902 case 0x5c10:
3903 tp->version = RTL_VER_04;
3904 tp->mii.supports_gmii = 1;
3905 break;
3906 case 0x5c20:
3907 tp->version = RTL_VER_05;
3908 tp->mii.supports_gmii = 1;
3909 break;
hayeswangac718b62013-05-02 16:01:25 +00003910 default:
3911 netif_info(tp, probe, tp->netdev,
3912 "Unknown version 0x%04x\n", version);
3913 break;
3914 }
3915}
3916
hayeswange3fe0b12014-01-02 11:22:39 +08003917static void rtl8152_unload(struct r8152 *tp)
3918{
hayeswang68714382014-04-11 17:54:31 +08003919 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3920 return;
3921
hayeswang00a5e362014-02-18 21:48:59 +08003922 if (tp->version != RTL_VER_01)
3923 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08003924}
3925
hayeswang43779f82014-01-02 11:25:10 +08003926static void rtl8153_unload(struct r8152 *tp)
3927{
hayeswang68714382014-04-11 17:54:31 +08003928 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3929 return;
3930
hayeswang49be1722014-10-01 13:25:11 +08003931 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003932}
3933
hayeswang55b65472014-11-06 12:47:39 +08003934static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08003935{
3936 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08003937 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08003938
hayeswang55b65472014-11-06 12:47:39 +08003939 switch (tp->version) {
3940 case RTL_VER_01:
3941 case RTL_VER_02:
3942 ops->init = r8152b_init;
3943 ops->enable = rtl8152_enable;
3944 ops->disable = rtl8152_disable;
3945 ops->up = rtl8152_up;
3946 ops->down = rtl8152_down;
3947 ops->unload = rtl8152_unload;
3948 ops->eee_get = r8152_get_eee;
3949 ops->eee_set = r8152_set_eee;
hayeswang43779f82014-01-02 11:25:10 +08003950 break;
3951
hayeswang55b65472014-11-06 12:47:39 +08003952 case RTL_VER_03:
3953 case RTL_VER_04:
3954 case RTL_VER_05:
3955 ops->init = r8153_init;
3956 ops->enable = rtl8153_enable;
3957 ops->disable = rtl8153_disable;
3958 ops->up = rtl8153_up;
3959 ops->down = rtl8153_down;
3960 ops->unload = rtl8153_unload;
3961 ops->eee_get = r8153_get_eee;
3962 ops->eee_set = r8153_set_eee;
hayeswangc81229c2014-01-02 11:22:42 +08003963 break;
3964
3965 default:
hayeswang55b65472014-11-06 12:47:39 +08003966 ret = -ENODEV;
3967 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08003968 break;
3969 }
3970
3971 return ret;
3972}
3973
hayeswangac718b62013-05-02 16:01:25 +00003974static int rtl8152_probe(struct usb_interface *intf,
3975 const struct usb_device_id *id)
3976{
3977 struct usb_device *udev = interface_to_usbdev(intf);
3978 struct r8152 *tp;
3979 struct net_device *netdev;
hayeswangebc2ec42013-08-14 20:54:38 +08003980 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003981
hayeswang10c32712014-03-04 20:47:48 +08003982 if (udev->actconfig->desc.bConfigurationValue != 1) {
3983 usb_driver_set_configuration(udev, 1);
3984 return -ENODEV;
3985 }
3986
3987 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00003988 netdev = alloc_etherdev(sizeof(struct r8152));
3989 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003990 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00003991 return -ENOMEM;
3992 }
3993
hayeswangebc2ec42013-08-14 20:54:38 +08003994 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00003995 tp = netdev_priv(netdev);
3996 tp->msg_enable = 0x7FFF;
3997
hayeswange3ad4122014-01-06 17:08:42 +08003998 tp->udev = udev;
3999 tp->netdev = netdev;
4000 tp->intf = intf;
4001
hayeswang82cf94c2014-11-06 12:47:38 +08004002 r8152b_get_version(tp);
hayeswang55b65472014-11-06 12:47:39 +08004003 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08004004 if (ret)
4005 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08004006
hayeswangb5403272014-10-09 18:00:26 +08004007 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004008 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
4009
hayeswangac718b62013-05-02 16:01:25 +00004010 netdev->netdev_ops = &rtl8152_netdev_ops;
4011 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08004012
hayeswang60c89072014-03-07 11:04:39 +08004013 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1b2014-03-07 11:04:40 +08004014 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08004015 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
4016 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08004017 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1b2014-03-07 11:04:40 +08004018 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08004019 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08004020 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08004021 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4022 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
4023 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08004024
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004025 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08004026 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00004027
4028 tp->mii.dev = netdev;
4029 tp->mii.mdio_read = read_mii_word;
4030 tp->mii.mdio_write = write_mii_word;
4031 tp->mii.phy_id_mask = 0x3f;
4032 tp->mii.reg_num_mask = 0x1f;
4033 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00004034
hayeswang464ec102015-02-12 14:33:46 +08004035 switch (udev->speed) {
4036 case USB_SPEED_SUPER:
4037 tp->coalesce = COALESCE_SUPER;
4038 break;
4039 case USB_SPEED_HIGH:
4040 tp->coalesce = COALESCE_HIGH;
4041 break;
4042 default:
4043 tp->coalesce = COALESCE_SLOW;
4044 break;
4045 }
4046
hayeswang9a4be1b2014-02-18 21:49:07 +08004047 intf->needs_remote_wakeup = 1;
4048
hayeswangc81229c2014-01-02 11:22:42 +08004049 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00004050 set_ethernet_addr(tp);
4051
hayeswangac718b62013-05-02 16:01:25 +00004052 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08004053 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00004054
hayeswangebc2ec42013-08-14 20:54:38 +08004055 ret = register_netdev(netdev);
4056 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08004057 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec42013-08-14 20:54:38 +08004058 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00004059 }
4060
hayeswang21ff2e82014-02-18 21:49:06 +08004061 tp->saved_wolopts = __rtl_get_wol(tp);
4062 if (tp->saved_wolopts)
4063 device_set_wakeup_enable(&udev->dev, true);
4064 else
4065 device_set_wakeup_enable(&udev->dev, false);
4066
Hayes Wang4a8deae2014-01-07 11:18:22 +08004067 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00004068
4069 return 0;
4070
hayeswangac718b62013-05-02 16:01:25 +00004071out1:
hayeswangd823ab62015-01-12 12:06:23 +08004072 netif_napi_del(&tp->napi);
hayeswangebc2ec42013-08-14 20:54:38 +08004073 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00004074out:
4075 free_netdev(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08004076 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004077}
4078
hayeswangac718b62013-05-02 16:01:25 +00004079static void rtl8152_disconnect(struct usb_interface *intf)
4080{
4081 struct r8152 *tp = usb_get_intfdata(intf);
4082
4083 usb_set_intfdata(intf, NULL);
4084 if (tp) {
hayeswangf561de32014-09-30 16:48:01 +08004085 struct usb_device *udev = tp->udev;
4086
4087 if (udev->state == USB_STATE_NOTATTACHED)
4088 set_bit(RTL8152_UNPLUG, &tp->flags);
4089
hayeswangd823ab62015-01-12 12:06:23 +08004090 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004091 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08004092 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00004093 free_netdev(tp->netdev);
4094 }
4095}
4096
hayeswangd9a28c52014-12-04 10:43:11 +08004097#define REALTEK_USB_DEVICE(vend, prod) \
4098 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
4099 USB_DEVICE_ID_MATCH_INT_CLASS, \
4100 .idVendor = (vend), \
4101 .idProduct = (prod), \
4102 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
4103}, \
4104{ \
4105 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
4106 USB_DEVICE_ID_MATCH_DEVICE, \
4107 .idVendor = (vend), \
4108 .idProduct = (prod), \
4109 .bInterfaceClass = USB_CLASS_COMM, \
4110 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
4111 .bInterfaceProtocol = USB_CDC_PROTO_NONE
4112
hayeswangac718b62013-05-02 16:01:25 +00004113/* table of devices that work with this driver */
4114static struct usb_device_id rtl8152_table[] = {
hayeswangd9a28c52014-12-04 10:43:11 +08004115 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
4116 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
4117 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Christian Hesse347eec32015-03-31 14:10:07 +02004118 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04004119 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangac718b62013-05-02 16:01:25 +00004120 {}
4121};
4122
4123MODULE_DEVICE_TABLE(usb, rtl8152_table);
4124
4125static struct usb_driver rtl8152_driver = {
4126 .name = MODULENAME,
hayeswangebc2ec42013-08-14 20:54:38 +08004127 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00004128 .probe = rtl8152_probe,
4129 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00004130 .suspend = rtl8152_suspend,
hayeswangebc2ec42013-08-14 20:54:38 +08004131 .resume = rtl8152_resume,
4132 .reset_resume = rtl8152_resume,
hayeswang9a4be1b2014-02-18 21:49:07 +08004133 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08004134 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00004135};
4136
Sachin Kamatb4236daa2013-05-16 17:48:08 +00004137module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00004138
4139MODULE_AUTHOR(DRIVER_AUTHOR);
4140MODULE_DESCRIPTION(DRIVER_DESC);
4141MODULE_LICENSE("GPL");