blob: 6bb48bc514841956f020b9287e0ab3bca878ff9a [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
hayeswangc7de7de2014-01-15 10:42:16 +08002 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
7 *
8 */
9
hayeswangac718b62013-05-02 16:01:25 +000010#include <linux/signal.h>
11#include <linux/slab.h>
12#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000013#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/mii.h>
16#include <linux/ethtool.h>
17#include <linux/usb.h>
18#include <linux/crc32.h>
19#include <linux/if_vlan.h>
20#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080021#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080022#include <linux/ip.h>
23#include <linux/ipv6.h>
hayeswang6128d1bb2014-03-07 11:04:40 +080024#include <net/ip6_checksum.h>
hayeswang4c4a6b12014-09-25 20:54:00 +080025#include <uapi/linux/mdio.h>
26#include <linux/mdio.h>
hayeswangd9a28c52014-12-04 10:43:11 +080027#include <linux/usb/cdc.h>
hayeswangac718b62013-05-02 16:01:25 +000028
hayeswangd0942472015-09-07 11:57:43 +080029/* Information for net-next */
30#define NETNEXT_VERSION "08"
31
32/* Information for net */
33#define NET_VERSION "1"
34
35#define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
hayeswangac718b62013-05-02 16:01:25 +000036#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080037#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000038#define MODULENAME "r8152"
39
40#define R8152_PHY_ID 32
41
42#define PLA_IDR 0xc000
43#define PLA_RCR 0xc010
44#define PLA_RMS 0xc016
45#define PLA_RXFIFO_CTRL0 0xc0a0
46#define PLA_RXFIFO_CTRL1 0xc0a4
47#define PLA_RXFIFO_CTRL2 0xc0a8
hayeswang65bab842015-02-12 16:20:46 +080048#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000049#define PLA_FMC 0xc0b4
50#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080051#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000052#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080053#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000054#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_TEREDO_TIMER 0xd2cc
56#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000057#define PLA_LEDSEL 0xdd90
58#define PLA_LED_FEATURE 0xdd92
59#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080060#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000061#define PLA_GPHY_INTR_IMR 0xe022
62#define PLA_EEE_CR 0xe040
63#define PLA_EEEP_CR 0xe080
64#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080065#define PLA_MAC_PWR_CTRL2 0xe0ca
66#define PLA_MAC_PWR_CTRL3 0xe0cc
67#define PLA_MAC_PWR_CTRL4 0xe0ce
68#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000069#define PLA_TCR0 0xe610
70#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080071#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000072#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080073#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000074#define PLA_CR 0xe813
75#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080076#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
77#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000078#define PLA_CONFIG5 0xe822
79#define PLA_PHY_PWR 0xe84c
80#define PLA_OOB_CTRL 0xe84f
81#define PLA_CPCR 0xe854
82#define PLA_MISC_0 0xe858
83#define PLA_MISC_1 0xe85a
84#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +080085#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000086#define PLA_SFF_STS_7 0xe8de
87#define PLA_PHYSTATUS 0xe908
88#define PLA_BP_BA 0xfc26
89#define PLA_BP_0 0xfc28
90#define PLA_BP_1 0xfc2a
91#define PLA_BP_2 0xfc2c
92#define PLA_BP_3 0xfc2e
93#define PLA_BP_4 0xfc30
94#define PLA_BP_5 0xfc32
95#define PLA_BP_6 0xfc34
96#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080097#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000098
hayeswang65bab842015-02-12 16:20:46 +080099#define USB_USB2PHY 0xb41e
100#define USB_SSPHYLINK2 0xb428
hayeswang43779f82014-01-02 11:25:10 +0800101#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +0800102#define USB_CSR_DUMMY1 0xb464
103#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +0000104#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800105#define USB_CONNECT_TIMER 0xcbf8
106#define USB_BURST_SIZE 0xcfc0
hayeswangac718b62013-05-02 16:01:25 +0000107#define USB_USB_CTRL 0xd406
108#define USB_PHY_CTRL 0xd408
109#define USB_TX_AGG 0xd40a
110#define USB_RX_BUF_TH 0xd40c
111#define USB_USB_TIMER 0xd428
hayeswang464ec102015-02-12 14:33:46 +0800112#define USB_RX_EARLY_TIMEOUT 0xd42c
113#define USB_RX_EARLY_SIZE 0xd42e
hayeswangac718b62013-05-02 16:01:25 +0000114#define USB_PM_CTRL_STATUS 0xd432
115#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +0800116#define USB_TOLERANCE 0xd490
117#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +0000118#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800119#define USB_MISC_0 0xd81a
120#define USB_POWER_CUT 0xd80a
121#define USB_AFE_CTRL2 0xd824
122#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000123#define USB_BP_BA 0xfc26
124#define USB_BP_0 0xfc28
125#define USB_BP_1 0xfc2a
126#define USB_BP_2 0xfc2c
127#define USB_BP_3 0xfc2e
128#define USB_BP_4 0xfc30
129#define USB_BP_5 0xfc32
130#define USB_BP_6 0xfc34
131#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800132#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000133
134/* OCP Registers */
135#define OCP_ALDPS_CONFIG 0x2010
136#define OCP_EEE_CONFIG1 0x2080
137#define OCP_EEE_CONFIG2 0x2092
138#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800139#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000140#define OCP_EEE_AR 0xa41a
141#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800142#define OCP_PHY_STATUS 0xa420
143#define OCP_POWER_CFG 0xa430
144#define OCP_EEE_CFG 0xa432
145#define OCP_SRAM_ADDR 0xa436
146#define OCP_SRAM_DATA 0xa438
147#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800148#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800149#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800150#define OCP_EEE_LPABLE 0xa5d2
hayeswang43779f82014-01-02 11:25:10 +0800151#define OCP_ADC_CFG 0xbc06
152
153/* SRAM Register */
154#define SRAM_LPF_CFG 0x8012
155#define SRAM_10M_AMP1 0x8080
156#define SRAM_10M_AMP2 0x8082
157#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000158
159/* PLA_RCR */
160#define RCR_AAP 0x00000001
161#define RCR_APM 0x00000002
162#define RCR_AM 0x00000004
163#define RCR_AB 0x00000008
164#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
165
166/* PLA_RXFIFO_CTRL0 */
167#define RXFIFO_THR1_NORMAL 0x00080002
168#define RXFIFO_THR1_OOB 0x01800003
169
170/* PLA_RXFIFO_CTRL1 */
171#define RXFIFO_THR2_FULL 0x00000060
172#define RXFIFO_THR2_HIGH 0x00000038
173#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800174#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000175
176/* PLA_RXFIFO_CTRL2 */
177#define RXFIFO_THR3_FULL 0x00000078
178#define RXFIFO_THR3_HIGH 0x00000048
179#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800180#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000181
182/* PLA_TXFIFO_CTRL */
183#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800184#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000185
hayeswang65bab842015-02-12 16:20:46 +0800186/* PLA_DMY_REG0 */
187#define ECM_ALDPS 0x0002
188
hayeswangac718b62013-05-02 16:01:25 +0000189/* PLA_FMC */
190#define FMC_FCR_MCU_EN 0x0001
191
192/* PLA_EEEP_CR */
193#define EEEP_CR_EEEP_TX 0x0002
194
hayeswang43779f82014-01-02 11:25:10 +0800195/* PLA_WDT6_CTRL */
196#define WDT6_SET_MODE 0x0010
197
hayeswangac718b62013-05-02 16:01:25 +0000198/* PLA_TCR0 */
199#define TCR0_TX_EMPTY 0x0800
200#define TCR0_AUTO_FIFO 0x0080
201
202/* PLA_TCR1 */
203#define VERSION_MASK 0x7cf0
204
hayeswang69b4b7a2014-07-10 10:58:54 +0800205/* PLA_MTPS */
206#define MTPS_JUMBO (12 * 1024 / 64)
207#define MTPS_DEFAULT (6 * 1024 / 64)
208
hayeswang4f1d4d52014-03-11 16:24:19 +0800209/* PLA_RSTTALLY */
210#define TALLY_RESET 0x0001
211
hayeswangac718b62013-05-02 16:01:25 +0000212/* PLA_CR */
213#define CR_RST 0x10
214#define CR_RE 0x08
215#define CR_TE 0x04
216
217/* PLA_CRWECR */
218#define CRWECR_NORAML 0x00
219#define CRWECR_CONFIG 0xc0
220
221/* PLA_OOB_CTRL */
222#define NOW_IS_OOB 0x80
223#define TXFIFO_EMPTY 0x20
224#define RXFIFO_EMPTY 0x10
225#define LINK_LIST_READY 0x02
226#define DIS_MCU_CLROOB 0x01
227#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
228
229/* PLA_MISC_1 */
230#define RXDY_GATED_EN 0x0008
231
232/* PLA_SFF_STS_7 */
233#define RE_INIT_LL 0x8000
234#define MCU_BORW_EN 0x4000
235
236/* PLA_CPCR */
237#define CPCR_RX_VLAN 0x0040
238
239/* PLA_CFG_WOL */
240#define MAGIC_EN 0x0001
241
hayeswang43779f82014-01-02 11:25:10 +0800242/* PLA_TEREDO_CFG */
243#define TEREDO_SEL 0x8000
244#define TEREDO_WAKE_MASK 0x7f00
245#define TEREDO_RS_EVENT_MASK 0x00fe
246#define OOB_TEREDO_EN 0x0001
247
hayeswangac718b62013-05-02 16:01:25 +0000248/* PAL_BDC_CR */
249#define ALDPS_PROXY_MODE 0x0001
250
hayeswang21ff2e82014-02-18 21:49:06 +0800251/* PLA_CONFIG34 */
252#define LINK_ON_WAKE_EN 0x0010
253#define LINK_OFF_WAKE_EN 0x0008
254
hayeswangac718b62013-05-02 16:01:25 +0000255/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800256#define BWF_EN 0x0040
257#define MWF_EN 0x0020
258#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000259#define LAN_WAKE_EN 0x0002
260
261/* PLA_LED_FEATURE */
262#define LED_MODE_MASK 0x0700
263
264/* PLA_PHY_PWR */
265#define TX_10M_IDLE_EN 0x0080
266#define PFM_PWM_SWITCH 0x0040
267
268/* PLA_MAC_PWR_CTRL */
269#define D3_CLK_GATED_EN 0x00004000
270#define MCU_CLK_RATIO 0x07010f07
271#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800272#define ALDPS_SPDWN_RATIO 0x0f87
273
274/* PLA_MAC_PWR_CTRL2 */
275#define EEE_SPDWN_RATIO 0x8007
276
277/* PLA_MAC_PWR_CTRL3 */
278#define PKT_AVAIL_SPDWN_EN 0x0100
279#define SUSPEND_SPDWN_EN 0x0004
280#define U1U2_SPDWN_EN 0x0002
281#define L1_SPDWN_EN 0x0001
282
283/* PLA_MAC_PWR_CTRL4 */
284#define PWRSAVE_SPDWN_EN 0x1000
285#define RXDV_SPDWN_EN 0x0800
286#define TX10MIDLE_EN 0x0100
287#define TP100_SPDWN_EN 0x0020
288#define TP500_SPDWN_EN 0x0010
289#define TP1000_SPDWN_EN 0x0008
290#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000291
292/* PLA_GPHY_INTR_IMR */
293#define GPHY_STS_MSK 0x0001
294#define SPEED_DOWN_MSK 0x0002
295#define SPDWN_RXDV_MSK 0x0004
296#define SPDWN_LINKCHG_MSK 0x0008
297
298/* PLA_PHYAR */
299#define PHYAR_FLAG 0x80000000
300
301/* PLA_EEE_CR */
302#define EEE_RX_EN 0x0001
303#define EEE_TX_EN 0x0002
304
hayeswang43779f82014-01-02 11:25:10 +0800305/* PLA_BOOT_CTRL */
306#define AUTOLOAD_DONE 0x0002
307
hayeswang65bab842015-02-12 16:20:46 +0800308/* USB_USB2PHY */
309#define USB2PHY_SUSPEND 0x0001
310#define USB2PHY_L1 0x0002
311
312/* USB_SSPHYLINK2 */
313#define pwd_dn_scale_mask 0x3ffe
314#define pwd_dn_scale(x) ((x) << 1)
315
316/* USB_CSR_DUMMY1 */
317#define DYNAMIC_BURST 0x0001
318
319/* USB_CSR_DUMMY2 */
320#define EP4_FULL_FC 0x0001
321
hayeswangac718b62013-05-02 16:01:25 +0000322/* USB_DEV_STAT */
323#define STAT_SPEED_MASK 0x0006
324#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800325#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000326
327/* USB_TX_AGG */
328#define TX_AGG_MAX_THRESHOLD 0x03
329
330/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800331#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800332#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800333#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000334
335/* USB_TX_DMA */
336#define TEST_MODE_DISABLE 0x00000001
337#define TX_SIZE_ADJUST1 0x00000100
338
339/* USB_UPS_CTRL */
340#define POWER_CUT 0x0100
341
342/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800343#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000344
345/* USB_USB_CTRL */
346#define RX_AGG_DISABLE 0x0010
hayeswange90fba82015-07-31 11:23:39 +0800347#define RX_ZERO_EN 0x0080
hayeswangac718b62013-05-02 16:01:25 +0000348
hayeswang43779f82014-01-02 11:25:10 +0800349/* USB_U2P3_CTRL */
350#define U2P3_ENABLE 0x0001
351
352/* USB_POWER_CUT */
353#define PWR_EN 0x0001
354#define PHASE2_EN 0x0008
355
356/* USB_MISC_0 */
357#define PCUT_STATUS 0x0001
358
hayeswang464ec102015-02-12 14:33:46 +0800359/* USB_RX_EARLY_TIMEOUT */
360#define COALESCE_SUPER 85000U
361#define COALESCE_HIGH 250000U
362#define COALESCE_SLOW 524280U
hayeswang43779f82014-01-02 11:25:10 +0800363
364/* USB_WDT11_CTRL */
365#define TIMER11_EN 0x0001
366
367/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800368/* bit 4 ~ 5: fifo empty boundary */
369#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
370/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800371#define LPM_TIMER_MASK 0x0c
372#define LPM_TIMER_500MS 0x04 /* 500 ms */
373#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800374#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800375
376/* USB_AFE_CTRL2 */
377#define SEN_VAL_MASK 0xf800
378#define SEN_VAL_NORMAL 0xa000
379#define SEL_RXIDLE 0x0100
380
hayeswangac718b62013-05-02 16:01:25 +0000381/* OCP_ALDPS_CONFIG */
382#define ENPWRSAVE 0x8000
383#define ENPDNPS 0x0200
384#define LINKENA 0x0100
385#define DIS_SDSAVE 0x0010
386
hayeswang43779f82014-01-02 11:25:10 +0800387/* OCP_PHY_STATUS */
388#define PHY_STAT_MASK 0x0007
389#define PHY_STAT_LAN_ON 3
390#define PHY_STAT_PWRDN 5
391
392/* OCP_POWER_CFG */
393#define EEE_CLKDIV_EN 0x8000
394#define EN_ALDPS 0x0004
395#define EN_10M_PLLOFF 0x0001
396
hayeswangac718b62013-05-02 16:01:25 +0000397/* OCP_EEE_CONFIG1 */
398#define RG_TXLPI_MSK_HFDUP 0x8000
399#define RG_MATCLR_EN 0x4000
400#define EEE_10_CAP 0x2000
401#define EEE_NWAY_EN 0x1000
402#define TX_QUIET_EN 0x0200
403#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800404#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800405#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000406#define RG_RXLPI_MSK_HFDUP 0x0008
407#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
408
409/* OCP_EEE_CONFIG2 */
410#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
411#define RG_DACQUIET_EN 0x0400
412#define RG_LDVQUIET_EN 0x0200
413#define RG_CKRSEL 0x0020
414#define RG_EEEPRG_EN 0x0010
415
416/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800417#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800418#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000419#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
420#define MSK_PH 0x0006 /* bit 0 ~ 3 */
421
422/* OCP_EEE_AR */
423/* bit[15:14] function */
424#define FUN_ADDR 0x0000
425#define FUN_DATA 0x4000
426/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000427
hayeswang43779f82014-01-02 11:25:10 +0800428/* OCP_EEE_CFG */
429#define CTAP_SHORT_EN 0x0040
430#define EEE10_EN 0x0010
431
432/* OCP_DOWN_SPEED */
433#define EN_10M_BGOFF 0x0080
434
hayeswang43779f82014-01-02 11:25:10 +0800435/* OCP_ADC_CFG */
436#define CKADSEL_L 0x0100
437#define ADC_EN 0x0080
438#define EN_EMI_L 0x0040
439
440/* SRAM_LPF_CFG */
441#define LPF_AUTO_TUNE 0x8000
442
443/* SRAM_10M_AMP1 */
444#define GDAC_IB_UPALL 0x0008
445
446/* SRAM_10M_AMP2 */
447#define AMP_DN 0x0200
448
449/* SRAM_IMPEDANCE */
450#define RX_DRIVING_MASK 0x6000
451
hayeswangac718b62013-05-02 16:01:25 +0000452enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800453 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000454 _100bps = 0x08,
455 _10bps = 0x04,
456 LINK_STATUS = 0x02,
457 FULL_DUP = 0x01,
458};
459
hayeswang1764bcd2014-08-28 10:24:18 +0800460#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800461#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800462#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800463#define CRC_SIZE 4
464#define TX_ALIGN 4
465#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800466
467#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800468
hayeswangac718b62013-05-02 16:01:25 +0000469#define RTL8152_REQT_READ 0xc0
470#define RTL8152_REQT_WRITE 0x40
471#define RTL8152_REQ_GET_REGS 0x05
472#define RTL8152_REQ_SET_REGS 0x05
473
474#define BYTE_EN_DWORD 0xff
475#define BYTE_EN_WORD 0x33
476#define BYTE_EN_BYTE 0x11
477#define BYTE_EN_SIX_BYTES 0x3f
478#define BYTE_EN_START_MASK 0x0f
479#define BYTE_EN_END_MASK 0xf0
480
hayeswang69b4b7a2014-07-10 10:58:54 +0800481#define RTL8153_MAX_PACKET 9216 /* 9K */
482#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
hayeswangac718b62013-05-02 16:01:25 +0000483#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800484#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800485#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800486#define RTL8152_NAPI_WEIGHT 64
hayeswangac718b62013-05-02 16:01:25 +0000487
488/* rtl8152 flags */
489enum rtl8152_flags {
490 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000491 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800492 WORK_ENABLE,
493 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800494 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800495 PHY_RESET,
hayeswangd823ab62015-01-12 12:06:23 +0800496 SCHEDULE_NAPI,
hayeswangac718b62013-05-02 16:01:25 +0000497};
498
499/* Define these values to match your device */
500#define VENDOR_ID_REALTEK 0x0bda
hayeswang43779f82014-01-02 11:25:10 +0800501#define VENDOR_ID_SAMSUNG 0x04e8
Christian Hesse347eec32015-03-31 14:10:07 +0200502#define VENDOR_ID_LENOVO 0x17ef
Zheng Liud065c3c12015-07-07 13:54:12 -0700503#define VENDOR_ID_NVIDIA 0x0955
hayeswangac718b62013-05-02 16:01:25 +0000504
505#define MCU_TYPE_PLA 0x0100
506#define MCU_TYPE_USB 0x0000
507
hayeswang4f1d4d52014-03-11 16:24:19 +0800508struct tally_counter {
509 __le64 tx_packets;
510 __le64 rx_packets;
511 __le64 tx_errors;
512 __le32 rx_errors;
513 __le16 rx_missed;
514 __le16 align_errors;
515 __le32 tx_one_collision;
516 __le32 tx_multi_collision;
517 __le64 rx_unicast;
518 __le64 rx_broadcast;
519 __le32 rx_multicast;
520 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800521 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800522};
523
hayeswangac718b62013-05-02 16:01:25 +0000524struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800525 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000526#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800527
hayeswang500b6d72013-11-20 17:30:57 +0800528 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800529#define RD_UDP_CS BIT(23)
530#define RD_TCP_CS BIT(22)
531#define RD_IPV6_CS BIT(20)
532#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800533
hayeswang500b6d72013-11-20 17:30:57 +0800534 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800535#define IPF BIT(23) /* IP checksum fail */
536#define UDPF BIT(22) /* UDP checksum fail */
537#define TCPF BIT(21) /* TCP checksum fail */
538#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800539
hayeswang500b6d72013-11-20 17:30:57 +0800540 __le32 opts4;
541 __le32 opts5;
542 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000543};
544
545struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800546 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800547#define TX_FS BIT(31) /* First segment of a packet */
548#define TX_LS BIT(30) /* Final segment of a packet */
549#define GTSENDV4 BIT(28)
550#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800551#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800552#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800553#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800554
hayeswang500b6d72013-11-20 17:30:57 +0800555 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800556#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
557#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
558#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
559#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800560#define MSS_SHIFT 17
561#define MSS_MAX 0x7ffU
562#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800563#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800564#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000565};
566
hayeswangdff4e8a2013-08-16 16:09:33 +0800567struct r8152;
568
hayeswangebc2ec482013-08-14 20:54:38 +0800569struct rx_agg {
570 struct list_head list;
571 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800572 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800573 void *buffer;
574 void *head;
575};
576
577struct tx_agg {
578 struct list_head list;
579 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800580 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800581 void *buffer;
582 void *head;
583 u32 skb_num;
584 u32 skb_len;
585};
586
hayeswangac718b62013-05-02 16:01:25 +0000587struct r8152 {
588 unsigned long flags;
589 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800590 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800591 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000592 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800593 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800594 struct tx_agg tx_info[RTL8152_MAX_TX];
595 struct rx_agg rx_info[RTL8152_MAX_RX];
596 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800597 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800598 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000599 struct delayed_work schedule;
600 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800601 struct mutex control; /* use for hw setting */
hayeswangc81229c2014-01-02 11:22:42 +0800602
603 struct rtl_ops {
604 void (*init)(struct r8152 *);
605 int (*enable)(struct r8152 *);
606 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800607 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800608 void (*down)(struct r8152 *);
609 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800610 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
611 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswangc81229c2014-01-02 11:22:42 +0800612 } rtl_ops;
613
hayeswang40a82912013-08-14 20:54:40 +0800614 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800615 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000616 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800617 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800618 u32 coalesce;
hayeswangac718b62013-05-02 16:01:25 +0000619 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800620 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000621 u8 version;
hayeswangac718b62013-05-02 16:01:25 +0000622};
623
624enum rtl_version {
625 RTL_VER_UNKNOWN = 0,
626 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800627 RTL_VER_02,
628 RTL_VER_03,
629 RTL_VER_04,
630 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +0800631 RTL_VER_06,
hayeswang43779f82014-01-02 11:25:10 +0800632 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000633};
634
hayeswang60c89072014-03-07 11:04:39 +0800635enum tx_csum_stat {
636 TX_CSUM_SUCCESS = 0,
637 TX_CSUM_TSO,
638 TX_CSUM_NONE
639};
640
hayeswangac718b62013-05-02 16:01:25 +0000641/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
642 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
643 */
644static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800645static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000646
hayeswang52aec122014-09-02 10:27:52 +0800647#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswang60c89072014-03-07 11:04:39 +0800648 VLAN_ETH_HLEN - VLAN_HLEN)
649
hayeswangac718b62013-05-02 16:01:25 +0000650static
651int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
652{
hayeswang31787f52013-07-31 17:21:25 +0800653 int ret;
654 void *tmp;
655
656 tmp = kmalloc(size, GFP_KERNEL);
657 if (!tmp)
658 return -ENOMEM;
659
660 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800661 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
662 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800663
664 memcpy(data, tmp, size);
665 kfree(tmp);
666
667 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000668}
669
670static
671int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
672{
hayeswang31787f52013-07-31 17:21:25 +0800673 int ret;
674 void *tmp;
675
Benoit Tainec4438f02014-05-26 17:21:23 +0200676 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800677 if (!tmp)
678 return -ENOMEM;
679
hayeswang31787f52013-07-31 17:21:25 +0800680 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800681 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
682 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800683
684 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800685
hayeswang31787f52013-07-31 17:21:25 +0800686 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000687}
688
689static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800690 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000691{
hayeswang45f4a192014-01-06 17:08:41 +0800692 u16 limit = 64;
693 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000694
695 if (test_bit(RTL8152_UNPLUG, &tp->flags))
696 return -ENODEV;
697
698 /* both size and indix must be 4 bytes align */
699 if ((size & 3) || !size || (index & 3) || !data)
700 return -EPERM;
701
702 if ((u32)index + (u32)size > 0xffff)
703 return -EPERM;
704
705 while (size) {
706 if (size > limit) {
707 ret = get_registers(tp, index, type, limit, data);
708 if (ret < 0)
709 break;
710
711 index += limit;
712 data += limit;
713 size -= limit;
714 } else {
715 ret = get_registers(tp, index, type, size, data);
716 if (ret < 0)
717 break;
718
719 index += size;
720 data += size;
721 size = 0;
722 break;
723 }
724 }
725
hayeswang67610492014-10-30 11:46:40 +0800726 if (ret == -ENODEV)
727 set_bit(RTL8152_UNPLUG, &tp->flags);
728
hayeswangac718b62013-05-02 16:01:25 +0000729 return ret;
730}
731
732static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800733 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000734{
hayeswang45f4a192014-01-06 17:08:41 +0800735 int ret;
736 u16 byteen_start, byteen_end, byen;
737 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000738
739 if (test_bit(RTL8152_UNPLUG, &tp->flags))
740 return -ENODEV;
741
742 /* both size and indix must be 4 bytes align */
743 if ((size & 3) || !size || (index & 3) || !data)
744 return -EPERM;
745
746 if ((u32)index + (u32)size > 0xffff)
747 return -EPERM;
748
749 byteen_start = byteen & BYTE_EN_START_MASK;
750 byteen_end = byteen & BYTE_EN_END_MASK;
751
752 byen = byteen_start | (byteen_start << 4);
753 ret = set_registers(tp, index, type | byen, 4, data);
754 if (ret < 0)
755 goto error1;
756
757 index += 4;
758 data += 4;
759 size -= 4;
760
761 if (size) {
762 size -= 4;
763
764 while (size) {
765 if (size > limit) {
766 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800767 type | BYTE_EN_DWORD,
768 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000769 if (ret < 0)
770 goto error1;
771
772 index += limit;
773 data += limit;
774 size -= limit;
775 } else {
776 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800777 type | BYTE_EN_DWORD,
778 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000779 if (ret < 0)
780 goto error1;
781
782 index += size;
783 data += size;
784 size = 0;
785 break;
786 }
787 }
788
789 byen = byteen_end | (byteen_end >> 4);
790 ret = set_registers(tp, index, type | byen, 4, data);
791 if (ret < 0)
792 goto error1;
793 }
794
795error1:
hayeswang67610492014-10-30 11:46:40 +0800796 if (ret == -ENODEV)
797 set_bit(RTL8152_UNPLUG, &tp->flags);
798
hayeswangac718b62013-05-02 16:01:25 +0000799 return ret;
800}
801
802static inline
803int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
804{
805 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
806}
807
808static inline
809int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
810{
811 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
812}
813
814static inline
815int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
816{
817 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
818}
819
820static inline
821int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
822{
823 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
824}
825
826static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
827{
hayeswangc8826de2013-07-31 17:21:26 +0800828 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000829
hayeswangc8826de2013-07-31 17:21:26 +0800830 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000831
832 return __le32_to_cpu(data);
833}
834
835static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
836{
hayeswangc8826de2013-07-31 17:21:26 +0800837 __le32 tmp = __cpu_to_le32(data);
838
839 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000840}
841
842static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
843{
844 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800845 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000846 u8 shift = index & 2;
847
848 index &= ~3;
849
hayeswangc8826de2013-07-31 17:21:26 +0800850 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000851
hayeswangc8826de2013-07-31 17:21:26 +0800852 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000853 data >>= (shift * 8);
854 data &= 0xffff;
855
856 return (u16)data;
857}
858
859static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
860{
hayeswangc8826de2013-07-31 17:21:26 +0800861 u32 mask = 0xffff;
862 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000863 u16 byen = BYTE_EN_WORD;
864 u8 shift = index & 2;
865
866 data &= mask;
867
868 if (index & 2) {
869 byen <<= shift;
870 mask <<= (shift * 8);
871 data <<= (shift * 8);
872 index &= ~3;
873 }
874
hayeswangc8826de2013-07-31 17:21:26 +0800875 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000876
hayeswangc8826de2013-07-31 17:21:26 +0800877 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000878}
879
880static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
881{
882 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800883 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000884 u8 shift = index & 3;
885
886 index &= ~3;
887
hayeswangc8826de2013-07-31 17:21:26 +0800888 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000889
hayeswangc8826de2013-07-31 17:21:26 +0800890 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000891 data >>= (shift * 8);
892 data &= 0xff;
893
894 return (u8)data;
895}
896
897static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
898{
hayeswangc8826de2013-07-31 17:21:26 +0800899 u32 mask = 0xff;
900 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000901 u16 byen = BYTE_EN_BYTE;
902 u8 shift = index & 3;
903
904 data &= mask;
905
906 if (index & 3) {
907 byen <<= shift;
908 mask <<= (shift * 8);
909 data <<= (shift * 8);
910 index &= ~3;
911 }
912
hayeswangc8826de2013-07-31 17:21:26 +0800913 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000914
hayeswangc8826de2013-07-31 17:21:26 +0800915 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000916}
917
hayeswangac244d32014-01-02 11:22:40 +0800918static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
919{
920 u16 ocp_base, ocp_index;
921
922 ocp_base = addr & 0xf000;
923 if (ocp_base != tp->ocp_base) {
924 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
925 tp->ocp_base = ocp_base;
926 }
927
928 ocp_index = (addr & 0x0fff) | 0xb000;
929 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
930}
931
hayeswange3fe0b12014-01-02 11:22:39 +0800932static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
933{
934 u16 ocp_base, ocp_index;
935
936 ocp_base = addr & 0xf000;
937 if (ocp_base != tp->ocp_base) {
938 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
939 tp->ocp_base = ocp_base;
940 }
941
942 ocp_index = (addr & 0x0fff) | 0xb000;
943 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
944}
945
hayeswangac244d32014-01-02 11:22:40 +0800946static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000947{
hayeswangac244d32014-01-02 11:22:40 +0800948 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000949}
950
hayeswangac244d32014-01-02 11:22:40 +0800951static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000952{
hayeswangac244d32014-01-02 11:22:40 +0800953 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000954}
955
hayeswang43779f82014-01-02 11:25:10 +0800956static void sram_write(struct r8152 *tp, u16 addr, u16 data)
957{
958 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
959 ocp_reg_write(tp, OCP_SRAM_DATA, data);
960}
961
hayeswangac718b62013-05-02 16:01:25 +0000962static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
963{
964 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +0800965 int ret;
hayeswangac718b62013-05-02 16:01:25 +0000966
hayeswang68714382014-04-11 17:54:31 +0800967 if (test_bit(RTL8152_UNPLUG, &tp->flags))
968 return -ENODEV;
969
hayeswangac718b62013-05-02 16:01:25 +0000970 if (phy_id != R8152_PHY_ID)
971 return -EINVAL;
972
hayeswang9a4be1b2014-02-18 21:49:07 +0800973 ret = r8152_mdio_read(tp, reg);
974
hayeswang9a4be1b2014-02-18 21:49:07 +0800975 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000976}
977
978static
979void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
980{
981 struct r8152 *tp = netdev_priv(netdev);
982
hayeswang68714382014-04-11 17:54:31 +0800983 if (test_bit(RTL8152_UNPLUG, &tp->flags))
984 return;
985
hayeswangac718b62013-05-02 16:01:25 +0000986 if (phy_id != R8152_PHY_ID)
987 return;
988
989 r8152_mdio_write(tp, reg, val);
990}
991
hayeswangb209af92014-08-25 15:53:00 +0800992static int
993r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800994
hayeswang8ba789a2014-09-04 16:15:41 +0800995static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
996{
997 struct r8152 *tp = netdev_priv(netdev);
998 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +0800999 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001000
1001 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001002 goto out1;
1003
1004 ret = usb_autopm_get_interface(tp->intf);
1005 if (ret < 0)
1006 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +08001007
hayeswangb5403272014-10-09 18:00:26 +08001008 mutex_lock(&tp->control);
1009
hayeswang8ba789a2014-09-04 16:15:41 +08001010 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1011
1012 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1013 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1014 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1015
hayeswangb5403272014-10-09 18:00:26 +08001016 mutex_unlock(&tp->control);
1017
hayeswangea6a7112014-10-02 17:03:12 +08001018 usb_autopm_put_interface(tp->intf);
1019out1:
1020 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001021}
1022
hayeswang179bb6d2014-09-04 16:15:42 +08001023static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001024{
1025 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001026 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001027 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001028
hayeswang8a91c822014-02-18 21:49:01 +08001029 if (tp->version == RTL_VER_01)
hayeswang179bb6d2014-09-04 16:15:42 +08001030 ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001031 else
hayeswang179bb6d2014-09-04 16:15:42 +08001032 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001033
1034 if (ret < 0) {
hayeswang179bb6d2014-09-04 16:15:42 +08001035 netif_err(tp, probe, dev, "Get ether addr fail\n");
1036 } else if (!is_valid_ether_addr(sa.sa_data)) {
1037 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1038 sa.sa_data);
1039 eth_hw_addr_random(dev);
1040 ether_addr_copy(sa.sa_data, dev->dev_addr);
1041 ret = rtl8152_set_mac_address(dev, &sa);
1042 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1043 sa.sa_data);
hayeswang8a91c822014-02-18 21:49:01 +08001044 } else {
hayeswang179bb6d2014-09-04 16:15:42 +08001045 if (tp->version == RTL_VER_01)
1046 ether_addr_copy(dev->dev_addr, sa.sa_data);
1047 else
1048 ret = rtl8152_set_mac_address(dev, &sa);
hayeswangac718b62013-05-02 16:01:25 +00001049 }
hayeswang179bb6d2014-09-04 16:15:42 +08001050
1051 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001052}
1053
hayeswangac718b62013-05-02 16:01:25 +00001054static void read_bulk_callback(struct urb *urb)
1055{
hayeswangac718b62013-05-02 16:01:25 +00001056 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001057 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001058 struct rx_agg *agg;
1059 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +00001060
hayeswangebc2ec482013-08-14 20:54:38 +08001061 agg = urb->context;
1062 if (!agg)
1063 return;
1064
1065 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001066 if (!tp)
1067 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001068
hayeswangac718b62013-05-02 16:01:25 +00001069 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1070 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001071
1072 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001073 return;
1074
hayeswangebc2ec482013-08-14 20:54:38 +08001075 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001076
1077 /* When link down, the driver would cancel all bulks. */
1078 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001079 if (!netif_carrier_ok(netdev))
1080 return;
1081
hayeswang9a4be1b2014-02-18 21:49:07 +08001082 usb_mark_last_busy(tp->udev);
1083
hayeswangac718b62013-05-02 16:01:25 +00001084 switch (status) {
1085 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001086 if (urb->actual_length < ETH_ZLEN)
1087 break;
1088
hayeswang2685d412014-03-07 11:04:34 +08001089 spin_lock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001090 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001091 spin_unlock(&tp->rx_lock);
hayeswangd823ab62015-01-12 12:06:23 +08001092 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001093 return;
hayeswangac718b62013-05-02 16:01:25 +00001094 case -ESHUTDOWN:
1095 set_bit(RTL8152_UNPLUG, &tp->flags);
1096 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001097 return;
hayeswangac718b62013-05-02 16:01:25 +00001098 case -ENOENT:
1099 return; /* the urb is in unlink state */
1100 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001101 if (net_ratelimit())
1102 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001103 break;
hayeswangac718b62013-05-02 16:01:25 +00001104 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001105 if (net_ratelimit())
1106 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001107 break;
hayeswangac718b62013-05-02 16:01:25 +00001108 }
1109
hayeswanga0fccd42014-11-20 10:29:05 +08001110 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001111}
1112
1113static void write_bulk_callback(struct urb *urb)
1114{
hayeswangebc2ec482013-08-14 20:54:38 +08001115 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001116 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001117 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001118 struct r8152 *tp;
1119 int status = urb->status;
1120
hayeswangebc2ec482013-08-14 20:54:38 +08001121 agg = urb->context;
1122 if (!agg)
1123 return;
1124
1125 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001126 if (!tp)
1127 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001128
hayeswangd104eaf2014-03-06 15:07:17 +08001129 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001130 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001131 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001132 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001133 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001134 stats->tx_errors += agg->skb_num;
1135 } else {
1136 stats->tx_packets += agg->skb_num;
1137 stats->tx_bytes += agg->skb_len;
1138 }
1139
hayeswang2685d412014-03-07 11:04:34 +08001140 spin_lock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001141 list_add_tail(&agg->list, &tp->tx_free);
hayeswang2685d412014-03-07 11:04:34 +08001142 spin_unlock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001143
hayeswang9a4be1b2014-02-18 21:49:07 +08001144 usb_autopm_put_interface_async(tp->intf);
1145
hayeswangd104eaf2014-03-06 15:07:17 +08001146 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001147 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001148
1149 if (!test_bit(WORK_ENABLE, &tp->flags))
1150 return;
1151
1152 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1153 return;
1154
1155 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001156 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001157}
1158
hayeswang40a82912013-08-14 20:54:40 +08001159static void intr_callback(struct urb *urb)
1160{
1161 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001162 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001163 int status = urb->status;
1164 int res;
1165
1166 tp = urb->context;
1167 if (!tp)
1168 return;
1169
1170 if (!test_bit(WORK_ENABLE, &tp->flags))
1171 return;
1172
1173 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1174 return;
1175
1176 switch (status) {
1177 case 0: /* success */
1178 break;
1179 case -ECONNRESET: /* unlink */
1180 case -ESHUTDOWN:
1181 netif_device_detach(tp->netdev);
1182 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001183 case -EPROTO:
1184 netif_info(tp, intr, tp->netdev,
1185 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001186 return;
1187 case -EOVERFLOW:
1188 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1189 goto resubmit;
1190 /* -EPIPE: should clear the halt */
1191 default:
1192 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1193 goto resubmit;
1194 }
1195
1196 d = urb->transfer_buffer;
1197 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001198 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001199 set_bit(RTL8152_LINK_CHG, &tp->flags);
1200 schedule_delayed_work(&tp->schedule, 0);
1201 }
1202 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001203 if (netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001204 set_bit(RTL8152_LINK_CHG, &tp->flags);
1205 schedule_delayed_work(&tp->schedule, 0);
1206 }
1207 }
1208
1209resubmit:
1210 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001211 if (res == -ENODEV) {
1212 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001213 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001214 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001215 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001216 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001217 }
hayeswang40a82912013-08-14 20:54:40 +08001218}
1219
hayeswangebc2ec482013-08-14 20:54:38 +08001220static inline void *rx_agg_align(void *data)
1221{
hayeswang8e1f51b2014-01-02 11:22:41 +08001222 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001223}
1224
1225static inline void *tx_agg_align(void *data)
1226{
hayeswang8e1f51b2014-01-02 11:22:41 +08001227 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001228}
1229
1230static void free_all_mem(struct r8152 *tp)
1231{
1232 int i;
1233
1234 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001235 usb_free_urb(tp->rx_info[i].urb);
1236 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001237
hayeswang9629e3c2014-01-15 10:42:15 +08001238 kfree(tp->rx_info[i].buffer);
1239 tp->rx_info[i].buffer = NULL;
1240 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001241 }
1242
1243 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001244 usb_free_urb(tp->tx_info[i].urb);
1245 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001246
hayeswang9629e3c2014-01-15 10:42:15 +08001247 kfree(tp->tx_info[i].buffer);
1248 tp->tx_info[i].buffer = NULL;
1249 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001250 }
hayeswang40a82912013-08-14 20:54:40 +08001251
hayeswang9629e3c2014-01-15 10:42:15 +08001252 usb_free_urb(tp->intr_urb);
1253 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001254
hayeswang9629e3c2014-01-15 10:42:15 +08001255 kfree(tp->intr_buff);
1256 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001257}
1258
1259static int alloc_all_mem(struct r8152 *tp)
1260{
1261 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001262 struct usb_interface *intf = tp->intf;
1263 struct usb_host_interface *alt = intf->cur_altsetting;
1264 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001265 struct urb *urb;
1266 int node, i;
1267 u8 *buf;
1268
1269 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1270
1271 spin_lock_init(&tp->rx_lock);
1272 spin_lock_init(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001273 INIT_LIST_HEAD(&tp->tx_free);
1274 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001275 skb_queue_head_init(&tp->rx_queue);
hayeswangebc2ec482013-08-14 20:54:38 +08001276
1277 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001278 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001279 if (!buf)
1280 goto err1;
1281
1282 if (buf != rx_agg_align(buf)) {
1283 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001284 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001285 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001286 if (!buf)
1287 goto err1;
1288 }
1289
1290 urb = usb_alloc_urb(0, GFP_KERNEL);
1291 if (!urb) {
1292 kfree(buf);
1293 goto err1;
1294 }
1295
1296 INIT_LIST_HEAD(&tp->rx_info[i].list);
1297 tp->rx_info[i].context = tp;
1298 tp->rx_info[i].urb = urb;
1299 tp->rx_info[i].buffer = buf;
1300 tp->rx_info[i].head = rx_agg_align(buf);
1301 }
1302
1303 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001304 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001305 if (!buf)
1306 goto err1;
1307
1308 if (buf != tx_agg_align(buf)) {
1309 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001310 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001311 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001312 if (!buf)
1313 goto err1;
1314 }
1315
1316 urb = usb_alloc_urb(0, GFP_KERNEL);
1317 if (!urb) {
1318 kfree(buf);
1319 goto err1;
1320 }
1321
1322 INIT_LIST_HEAD(&tp->tx_info[i].list);
1323 tp->tx_info[i].context = tp;
1324 tp->tx_info[i].urb = urb;
1325 tp->tx_info[i].buffer = buf;
1326 tp->tx_info[i].head = tx_agg_align(buf);
1327
1328 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1329 }
1330
hayeswang40a82912013-08-14 20:54:40 +08001331 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1332 if (!tp->intr_urb)
1333 goto err1;
1334
1335 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1336 if (!tp->intr_buff)
1337 goto err1;
1338
1339 tp->intr_interval = (int)ep_intr->desc.bInterval;
1340 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001341 tp->intr_buff, INTBUFSIZE, intr_callback,
1342 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001343
hayeswangebc2ec482013-08-14 20:54:38 +08001344 return 0;
1345
1346err1:
1347 free_all_mem(tp);
1348 return -ENOMEM;
1349}
1350
hayeswang0de98f62013-08-16 16:09:35 +08001351static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1352{
1353 struct tx_agg *agg = NULL;
1354 unsigned long flags;
1355
hayeswang21949ab2014-03-07 11:04:35 +08001356 if (list_empty(&tp->tx_free))
1357 return NULL;
1358
hayeswang0de98f62013-08-16 16:09:35 +08001359 spin_lock_irqsave(&tp->tx_lock, flags);
1360 if (!list_empty(&tp->tx_free)) {
1361 struct list_head *cursor;
1362
1363 cursor = tp->tx_free.next;
1364 list_del_init(cursor);
1365 agg = list_entry(cursor, struct tx_agg, list);
1366 }
1367 spin_unlock_irqrestore(&tp->tx_lock, flags);
1368
1369 return agg;
1370}
1371
hayeswangb209af92014-08-25 15:53:00 +08001372/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001373 * The hw limites the value the transport offset. When the offset is out of the
1374 * range, calculate the checksum by sw.
1375 */
1376static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1377 struct sk_buff_head *list)
1378{
1379 if (skb_shinfo(skb)->gso_size) {
1380 netdev_features_t features = tp->netdev->features;
1381 struct sk_buff_head seg_list;
1382 struct sk_buff *segs, *nskb;
1383
hayeswanga91d45f2014-07-11 16:48:27 +08001384 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001385 segs = skb_gso_segment(skb, features);
1386 if (IS_ERR(segs) || !segs)
1387 goto drop;
1388
1389 __skb_queue_head_init(&seg_list);
1390
1391 do {
1392 nskb = segs;
1393 segs = segs->next;
1394 nskb->next = NULL;
1395 __skb_queue_tail(&seg_list, nskb);
1396 } while (segs);
1397
1398 skb_queue_splice(&seg_list, list);
1399 dev_kfree_skb(skb);
1400 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1401 if (skb_checksum_help(skb) < 0)
1402 goto drop;
1403
1404 __skb_queue_head(list, skb);
1405 } else {
1406 struct net_device_stats *stats;
1407
1408drop:
1409 stats = &tp->netdev->stats;
1410 stats->tx_dropped++;
1411 dev_kfree_skb(skb);
1412 }
1413}
1414
hayeswangb209af92014-08-25 15:53:00 +08001415/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001416 * According to the document of microsoft, the TCP Pseudo Header excludes the
1417 * packet length for IPv6 TCP large packets.
1418 */
1419static int msdn_giant_send_check(struct sk_buff *skb)
1420{
1421 const struct ipv6hdr *ipv6h;
1422 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001423 int ret;
1424
1425 ret = skb_cow_head(skb, 0);
1426 if (ret)
1427 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001428
1429 ipv6h = ipv6_hdr(skb);
1430 th = tcp_hdr(skb);
1431
1432 th->check = 0;
1433 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1434
hayeswangfcb308d2014-03-11 10:20:32 +08001435 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001436}
1437
hayeswangc5554292014-09-12 10:43:11 +08001438static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1439{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001440 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001441 u32 opts2;
1442
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001443 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001444 desc->opts2 |= cpu_to_le32(opts2);
1445 }
1446}
1447
1448static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1449{
1450 u32 opts2 = le32_to_cpu(desc->opts2);
1451
1452 if (opts2 & RX_VLAN_TAG)
1453 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1454 swab16(opts2 & 0xffff));
1455}
1456
hayeswang60c89072014-03-07 11:04:39 +08001457static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1458 struct sk_buff *skb, u32 len, u32 transport_offset)
1459{
1460 u32 mss = skb_shinfo(skb)->gso_size;
1461 u32 opts1, opts2 = 0;
1462 int ret = TX_CSUM_SUCCESS;
1463
1464 WARN_ON_ONCE(len > TX_LEN_MAX);
1465
1466 opts1 = len | TX_FS | TX_LS;
1467
1468 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001469 if (transport_offset > GTTCPHO_MAX) {
1470 netif_warn(tp, tx_err, tp->netdev,
1471 "Invalid transport offset 0x%x for TSO\n",
1472 transport_offset);
1473 ret = TX_CSUM_TSO;
1474 goto unavailable;
1475 }
1476
hayeswang6e74d172015-02-06 11:30:50 +08001477 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001478 case htons(ETH_P_IP):
1479 opts1 |= GTSENDV4;
1480 break;
1481
hayeswang6128d1bb2014-03-07 11:04:40 +08001482 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001483 if (msdn_giant_send_check(skb)) {
1484 ret = TX_CSUM_TSO;
1485 goto unavailable;
1486 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001487 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001488 break;
1489
hayeswang60c89072014-03-07 11:04:39 +08001490 default:
1491 WARN_ON_ONCE(1);
1492 break;
1493 }
1494
1495 opts1 |= transport_offset << GTTCPHO_SHIFT;
1496 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1497 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001498 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001499
hayeswang6128d1bb2014-03-07 11:04:40 +08001500 if (transport_offset > TCPHO_MAX) {
1501 netif_warn(tp, tx_err, tp->netdev,
1502 "Invalid transport offset 0x%x\n",
1503 transport_offset);
1504 ret = TX_CSUM_NONE;
1505 goto unavailable;
1506 }
1507
hayeswang6e74d172015-02-06 11:30:50 +08001508 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001509 case htons(ETH_P_IP):
1510 opts2 |= IPV4_CS;
1511 ip_protocol = ip_hdr(skb)->protocol;
1512 break;
1513
1514 case htons(ETH_P_IPV6):
1515 opts2 |= IPV6_CS;
1516 ip_protocol = ipv6_hdr(skb)->nexthdr;
1517 break;
1518
1519 default:
1520 ip_protocol = IPPROTO_RAW;
1521 break;
1522 }
1523
hayeswang60c89072014-03-07 11:04:39 +08001524 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001525 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001526 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001527 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001528 else
hayeswang5bd23882013-08-14 20:54:39 +08001529 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001530
hayeswang60c89072014-03-07 11:04:39 +08001531 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001532 }
hayeswang60c89072014-03-07 11:04:39 +08001533
1534 desc->opts2 = cpu_to_le32(opts2);
1535 desc->opts1 = cpu_to_le32(opts1);
1536
hayeswang6128d1bb2014-03-07 11:04:40 +08001537unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001538 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001539}
1540
hayeswangb1379d92013-08-16 16:09:37 +08001541static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1542{
hayeswangd84130a2014-02-18 21:49:02 +08001543 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001544 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001545 u8 *tx_data;
1546
hayeswangd84130a2014-02-18 21:49:02 +08001547 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001548 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001549 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001550 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001551
hayeswangb1379d92013-08-16 16:09:37 +08001552 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001553 agg->skb_num = 0;
1554 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001555 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001556
hayeswang7937f9e2013-11-20 17:30:54 +08001557 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001558 struct tx_desc *tx_desc;
1559 struct sk_buff *skb;
1560 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001561 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001562
hayeswangd84130a2014-02-18 21:49:02 +08001563 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001564 if (!skb)
1565 break;
1566
hayeswang60c89072014-03-07 11:04:39 +08001567 len = skb->len + sizeof(*tx_desc);
1568
1569 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001570 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001571 break;
1572 }
1573
hayeswang7937f9e2013-11-20 17:30:54 +08001574 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001575 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001576
1577 offset = (u32)skb_transport_offset(skb);
1578
hayeswang6128d1bb2014-03-07 11:04:40 +08001579 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1580 r8152_csum_workaround(tp, skb, &skb_head);
1581 continue;
1582 }
hayeswang60c89072014-03-07 11:04:39 +08001583
hayeswangc5554292014-09-12 10:43:11 +08001584 rtl_tx_vlan_tag(tx_desc, skb);
1585
hayeswangb1379d92013-08-16 16:09:37 +08001586 tx_data += sizeof(*tx_desc);
1587
hayeswang60c89072014-03-07 11:04:39 +08001588 len = skb->len;
1589 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1590 struct net_device_stats *stats = &tp->netdev->stats;
1591
1592 stats->tx_dropped++;
1593 dev_kfree_skb_any(skb);
1594 tx_data -= sizeof(*tx_desc);
1595 continue;
1596 }
hayeswangb1379d92013-08-16 16:09:37 +08001597
hayeswang7937f9e2013-11-20 17:30:54 +08001598 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001599 agg->skb_len += len;
1600 agg->skb_num++;
1601
1602 dev_kfree_skb_any(skb);
1603
hayeswang52aec122014-09-02 10:27:52 +08001604 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001605 }
1606
hayeswangd84130a2014-02-18 21:49:02 +08001607 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001608 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001609 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001610 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001611 }
1612
hayeswang0c3121f2014-03-07 11:04:36 +08001613 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001614
1615 if (netif_queue_stopped(tp->netdev) &&
1616 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1617 netif_wake_queue(tp->netdev);
1618
hayeswang0c3121f2014-03-07 11:04:36 +08001619 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001620
hayeswang0c3121f2014-03-07 11:04:36 +08001621 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001622 if (ret < 0)
1623 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001624
hayeswangb1379d92013-08-16 16:09:37 +08001625 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1626 agg->head, (int)(tx_data - (u8 *)agg->head),
1627 (usb_complete_t)write_bulk_callback, agg);
1628
hayeswang0c3121f2014-03-07 11:04:36 +08001629 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001630 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001631 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001632
1633out_tx_fill:
1634 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001635}
1636
hayeswang565cab02014-03-07 11:04:38 +08001637static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1638{
1639 u8 checksum = CHECKSUM_NONE;
1640 u32 opts2, opts3;
1641
1642 if (tp->version == RTL_VER_01)
1643 goto return_result;
1644
1645 opts2 = le32_to_cpu(rx_desc->opts2);
1646 opts3 = le32_to_cpu(rx_desc->opts3);
1647
1648 if (opts2 & RD_IPV4_CS) {
1649 if (opts3 & IPF)
1650 checksum = CHECKSUM_NONE;
1651 else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
1652 checksum = CHECKSUM_NONE;
1653 else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
1654 checksum = CHECKSUM_NONE;
1655 else
1656 checksum = CHECKSUM_UNNECESSARY;
hayeswang6128d1bb2014-03-07 11:04:40 +08001657 } else if (RD_IPV6_CS) {
1658 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1659 checksum = CHECKSUM_UNNECESSARY;
1660 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1661 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001662 }
1663
1664return_result:
1665 return checksum;
1666}
1667
hayeswangd823ab62015-01-12 12:06:23 +08001668static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08001669{
hayeswanga5a4f462013-08-16 16:09:34 +08001670 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001671 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08001672 int ret = 0, work_done = 0;
hayeswangd823ab62015-01-12 12:06:23 +08001673
1674 if (!skb_queue_empty(&tp->rx_queue)) {
1675 while (work_done < budget) {
1676 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1677 struct net_device *netdev = tp->netdev;
1678 struct net_device_stats *stats = &netdev->stats;
1679 unsigned int pkt_len;
1680
1681 if (!skb)
1682 break;
1683
1684 pkt_len = skb->len;
1685 napi_gro_receive(&tp->napi, skb);
1686 work_done++;
1687 stats->rx_packets++;
1688 stats->rx_bytes += pkt_len;
1689 }
1690 }
hayeswangebc2ec482013-08-14 20:54:38 +08001691
hayeswangd84130a2014-02-18 21:49:02 +08001692 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08001693 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08001694
1695 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001696 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001697 list_splice_init(&tp->rx_done, &rx_queue);
1698 spin_unlock_irqrestore(&tp->rx_lock, flags);
1699
1700 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001701 struct rx_desc *rx_desc;
1702 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001703 int len_used = 0;
1704 struct urb *urb;
1705 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08001706
hayeswangebc2ec482013-08-14 20:54:38 +08001707 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001708
1709 agg = list_entry(cursor, struct rx_agg, list);
1710 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001711 if (urb->actual_length < ETH_ZLEN)
1712 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001713
hayeswangebc2ec482013-08-14 20:54:38 +08001714 rx_desc = agg->head;
1715 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001716 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001717
hayeswang7937f9e2013-11-20 17:30:54 +08001718 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001719 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001720 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001721 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001722 struct sk_buff *skb;
1723
hayeswang7937f9e2013-11-20 17:30:54 +08001724 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001725 if (pkt_len < ETH_ZLEN)
1726 break;
1727
hayeswang7937f9e2013-11-20 17:30:54 +08001728 len_used += pkt_len;
1729 if (urb->actual_length < len_used)
1730 break;
1731
hayeswang8e1f51b2014-01-02 11:22:41 +08001732 pkt_len -= CRC_SIZE;
hayeswangebc2ec482013-08-14 20:54:38 +08001733 rx_data += sizeof(struct rx_desc);
1734
1735 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1736 if (!skb) {
1737 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001738 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08001739 }
hayeswang565cab02014-03-07 11:04:38 +08001740
1741 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001742 memcpy(skb->data, rx_data, pkt_len);
1743 skb_put(skb, pkt_len);
1744 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08001745 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001746 if (work_done < budget) {
1747 napi_gro_receive(&tp->napi, skb);
1748 work_done++;
1749 stats->rx_packets++;
1750 stats->rx_bytes += pkt_len;
1751 } else {
1752 __skb_queue_tail(&tp->rx_queue, skb);
1753 }
hayeswangebc2ec482013-08-14 20:54:38 +08001754
hayeswang5e2f7482014-03-07 11:04:37 +08001755find_next_rx:
hayeswang8e1f51b2014-01-02 11:22:41 +08001756 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec482013-08-14 20:54:38 +08001757 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001758 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001759 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001760 }
1761
hayeswang0de98f62013-08-16 16:09:35 +08001762submit:
hayeswange1a2ca92015-02-06 11:30:45 +08001763 if (!ret) {
1764 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1765 } else {
1766 urb->actual_length = 0;
1767 list_add_tail(&agg->list, next);
1768 }
1769 }
1770
1771 if (!list_empty(&rx_queue)) {
1772 spin_lock_irqsave(&tp->rx_lock, flags);
1773 list_splice_tail(&rx_queue, &tp->rx_done);
1774 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001775 }
hayeswangd823ab62015-01-12 12:06:23 +08001776
1777out1:
1778 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08001779}
1780
1781static void tx_bottom(struct r8152 *tp)
1782{
hayeswangebc2ec482013-08-14 20:54:38 +08001783 int res;
1784
hayeswangb1379d92013-08-16 16:09:37 +08001785 do {
1786 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001787
hayeswangb1379d92013-08-16 16:09:37 +08001788 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001789 break;
1790
hayeswangb1379d92013-08-16 16:09:37 +08001791 agg = r8152_get_tx_agg(tp);
1792 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001793 break;
hayeswangb1379d92013-08-16 16:09:37 +08001794
1795 res = r8152_tx_agg_fill(tp, agg);
1796 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08001797 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08001798
1799 if (res == -ENODEV) {
hayeswang67610492014-10-30 11:46:40 +08001800 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswangb1379d92013-08-16 16:09:37 +08001801 netif_device_detach(netdev);
1802 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08001803 struct net_device_stats *stats = &netdev->stats;
1804 unsigned long flags;
1805
hayeswangb1379d92013-08-16 16:09:37 +08001806 netif_warn(tp, tx_err, netdev,
1807 "failed tx_urb %d\n", res);
1808 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08001809
hayeswangb1379d92013-08-16 16:09:37 +08001810 spin_lock_irqsave(&tp->tx_lock, flags);
1811 list_add_tail(&agg->list, &tp->tx_free);
1812 spin_unlock_irqrestore(&tp->tx_lock, flags);
1813 }
hayeswangebc2ec482013-08-14 20:54:38 +08001814 }
hayeswangb1379d92013-08-16 16:09:37 +08001815 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001816}
1817
hayeswangd823ab62015-01-12 12:06:23 +08001818static void bottom_half(struct r8152 *tp)
hayeswangebc2ec482013-08-14 20:54:38 +08001819{
hayeswangebc2ec482013-08-14 20:54:38 +08001820 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1821 return;
1822
1823 if (!test_bit(WORK_ENABLE, &tp->flags))
1824 return;
1825
hayeswang7559fb2f2013-08-16 16:09:38 +08001826 /* When link down, the driver would cancel all bulks. */
1827 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001828 if (!netif_carrier_ok(tp->netdev))
1829 return;
1830
hayeswangd823ab62015-01-12 12:06:23 +08001831 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08001832
hayeswang0c3121f2014-03-07 11:04:36 +08001833 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001834}
1835
hayeswangd823ab62015-01-12 12:06:23 +08001836static int r8152_poll(struct napi_struct *napi, int budget)
1837{
1838 struct r8152 *tp = container_of(napi, struct r8152, napi);
1839 int work_done;
1840
1841 work_done = rx_bottom(tp, budget);
1842 bottom_half(tp);
1843
1844 if (work_done < budget) {
1845 napi_complete(napi);
1846 if (!list_empty(&tp->rx_done))
1847 napi_schedule(napi);
1848 }
1849
1850 return work_done;
1851}
1852
hayeswangebc2ec482013-08-14 20:54:38 +08001853static
1854int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1855{
hayeswanga0fccd42014-11-20 10:29:05 +08001856 int ret;
1857
hayeswangef827a52015-01-09 10:26:36 +08001858 /* The rx would be stopped, so skip submitting */
1859 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
1860 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
1861 return 0;
1862
hayeswangebc2ec482013-08-14 20:54:38 +08001863 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
hayeswang52aec122014-09-02 10:27:52 +08001864 agg->head, agg_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08001865 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08001866
hayeswanga0fccd42014-11-20 10:29:05 +08001867 ret = usb_submit_urb(agg->urb, mem_flags);
1868 if (ret == -ENODEV) {
1869 set_bit(RTL8152_UNPLUG, &tp->flags);
1870 netif_device_detach(tp->netdev);
1871 } else if (ret) {
1872 struct urb *urb = agg->urb;
1873 unsigned long flags;
1874
1875 urb->actual_length = 0;
1876 spin_lock_irqsave(&tp->rx_lock, flags);
1877 list_add_tail(&agg->list, &tp->rx_done);
1878 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001879
1880 netif_err(tp, rx_err, tp->netdev,
1881 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
1882
1883 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08001884 }
1885
1886 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001887}
1888
hayeswang00a5e362014-02-18 21:48:59 +08001889static void rtl_drop_queued_tx(struct r8152 *tp)
1890{
1891 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001892 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08001893 struct sk_buff *skb;
1894
hayeswangd84130a2014-02-18 21:49:02 +08001895 if (skb_queue_empty(tx_queue))
1896 return;
1897
1898 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001899 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001900 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001901 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001902
1903 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001904 dev_kfree_skb(skb);
1905 stats->tx_dropped++;
1906 }
1907}
1908
hayeswangac718b62013-05-02 16:01:25 +00001909static void rtl8152_tx_timeout(struct net_device *netdev)
1910{
1911 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001912
Hayes Wang4a8deae2014-01-07 11:18:22 +08001913 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08001914
1915 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00001916}
1917
1918static void rtl8152_set_rx_mode(struct net_device *netdev)
1919{
1920 struct r8152 *tp = netdev_priv(netdev);
1921
hayeswang51d979f2015-02-06 11:30:47 +08001922 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00001923 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001924 schedule_delayed_work(&tp->schedule, 0);
1925 }
hayeswangac718b62013-05-02 16:01:25 +00001926}
1927
1928static void _rtl8152_set_rx_mode(struct net_device *netdev)
1929{
1930 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001931 u32 mc_filter[2]; /* Multicast hash filter */
1932 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001933 u32 ocp_data;
1934
hayeswangac718b62013-05-02 16:01:25 +00001935 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1936 netif_stop_queue(netdev);
1937 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1938 ocp_data &= ~RCR_ACPT_ALL;
1939 ocp_data |= RCR_AB | RCR_APM;
1940
1941 if (netdev->flags & IFF_PROMISC) {
1942 /* Unconditionally log net taps. */
1943 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1944 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08001945 mc_filter[1] = 0xffffffff;
1946 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001947 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1948 (netdev->flags & IFF_ALLMULTI)) {
1949 /* Too many to filter perfectly -- accept all multicasts. */
1950 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08001951 mc_filter[1] = 0xffffffff;
1952 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00001953 } else {
1954 struct netdev_hw_addr *ha;
1955
hayeswangb209af92014-08-25 15:53:00 +08001956 mc_filter[1] = 0;
1957 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00001958 netdev_for_each_mc_addr(ha, netdev) {
1959 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08001960
hayeswangac718b62013-05-02 16:01:25 +00001961 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1962 ocp_data |= RCR_AM;
1963 }
1964 }
1965
hayeswang31787f52013-07-31 17:21:25 +08001966 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1967 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001968
hayeswang31787f52013-07-31 17:21:25 +08001969 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001970 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1971 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001972}
1973
hayeswanga5e31252015-01-06 17:41:58 +08001974static netdev_features_t
1975rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
1976 netdev_features_t features)
1977{
1978 u32 mss = skb_shinfo(skb)->gso_size;
1979 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
1980 int offset = skb_transport_offset(skb);
1981
1982 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
1983 features &= ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
1984 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
1985 features &= ~NETIF_F_GSO_MASK;
1986
1987 return features;
1988}
1989
hayeswangac718b62013-05-02 16:01:25 +00001990static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08001991 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00001992{
1993 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001994
hayeswangac718b62013-05-02 16:01:25 +00001995 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001996
hayeswang61598782013-11-20 17:30:55 +08001997 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001998
hayeswang0c3121f2014-03-07 11:04:36 +08001999 if (!list_empty(&tp->tx_free)) {
2000 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08002001 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002002 schedule_delayed_work(&tp->schedule, 0);
2003 } else {
2004 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08002005 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002006 }
hayeswangb209af92014-08-25 15:53:00 +08002007 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002008 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002009 }
hayeswangdd1b1192013-11-20 17:30:56 +08002010
hayeswangac718b62013-05-02 16:01:25 +00002011 return NETDEV_TX_OK;
2012}
2013
2014static void r8152b_reset_packet_filter(struct r8152 *tp)
2015{
2016 u32 ocp_data;
2017
2018 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2019 ocp_data &= ~FMC_FCR_MCU_EN;
2020 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2021 ocp_data |= FMC_FCR_MCU_EN;
2022 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2023}
2024
2025static void rtl8152_nic_reset(struct r8152 *tp)
2026{
2027 int i;
2028
2029 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2030
2031 for (i = 0; i < 1000; i++) {
2032 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2033 break;
hayeswangb209af92014-08-25 15:53:00 +08002034 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002035 }
2036}
2037
hayeswangdd1b1192013-11-20 17:30:56 +08002038static void set_tx_qlen(struct r8152 *tp)
2039{
2040 struct net_device *netdev = tp->netdev;
2041
hayeswang52aec122014-09-02 10:27:52 +08002042 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
2043 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002044}
2045
hayeswangac718b62013-05-02 16:01:25 +00002046static inline u8 rtl8152_get_speed(struct r8152 *tp)
2047{
2048 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2049}
2050
hayeswang507605a2014-01-02 11:22:43 +08002051static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002052{
hayeswangebc2ec482013-08-14 20:54:38 +08002053 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002054 u8 speed;
2055
2056 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002057 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002058 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002059 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002060 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2061 } else {
2062 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002063 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002064 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2065 }
hayeswang507605a2014-01-02 11:22:43 +08002066}
2067
hayeswang00a5e362014-02-18 21:48:59 +08002068static void rxdy_gated_en(struct r8152 *tp, bool enable)
2069{
2070 u32 ocp_data;
2071
2072 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2073 if (enable)
2074 ocp_data |= RXDY_GATED_EN;
2075 else
2076 ocp_data &= ~RXDY_GATED_EN;
2077 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2078}
2079
hayeswang445f7f42014-09-23 16:31:47 +08002080static int rtl_start_rx(struct r8152 *tp)
2081{
2082 int i, ret = 0;
2083
2084 INIT_LIST_HEAD(&tp->rx_done);
2085 for (i = 0; i < RTL8152_MAX_RX; i++) {
2086 INIT_LIST_HEAD(&tp->rx_info[i].list);
2087 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2088 if (ret)
2089 break;
2090 }
2091
hayeswang7bcf4f62014-11-20 10:29:06 +08002092 if (ret && ++i < RTL8152_MAX_RX) {
2093 struct list_head rx_queue;
2094 unsigned long flags;
2095
2096 INIT_LIST_HEAD(&rx_queue);
2097
2098 do {
2099 struct rx_agg *agg = &tp->rx_info[i++];
2100 struct urb *urb = agg->urb;
2101
2102 urb->actual_length = 0;
2103 list_add_tail(&agg->list, &rx_queue);
2104 } while (i < RTL8152_MAX_RX);
2105
2106 spin_lock_irqsave(&tp->rx_lock, flags);
2107 list_splice_tail(&rx_queue, &tp->rx_done);
2108 spin_unlock_irqrestore(&tp->rx_lock, flags);
2109 }
2110
hayeswang445f7f42014-09-23 16:31:47 +08002111 return ret;
2112}
2113
2114static int rtl_stop_rx(struct r8152 *tp)
2115{
2116 int i;
2117
2118 for (i = 0; i < RTL8152_MAX_RX; i++)
2119 usb_kill_urb(tp->rx_info[i].urb);
2120
hayeswangd823ab62015-01-12 12:06:23 +08002121 while (!skb_queue_empty(&tp->rx_queue))
2122 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2123
hayeswang445f7f42014-09-23 16:31:47 +08002124 return 0;
2125}
2126
hayeswang507605a2014-01-02 11:22:43 +08002127static int rtl_enable(struct r8152 *tp)
2128{
2129 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002130
2131 r8152b_reset_packet_filter(tp);
2132
2133 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2134 ocp_data |= CR_RE | CR_TE;
2135 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2136
hayeswang00a5e362014-02-18 21:48:59 +08002137 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002138
hayeswangaa2e0922015-01-09 10:26:35 +08002139 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002140}
2141
hayeswang507605a2014-01-02 11:22:43 +08002142static int rtl8152_enable(struct r8152 *tp)
2143{
hayeswang68714382014-04-11 17:54:31 +08002144 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2145 return -ENODEV;
2146
hayeswang507605a2014-01-02 11:22:43 +08002147 set_tx_qlen(tp);
2148 rtl_set_eee_plus(tp);
2149
2150 return rtl_enable(tp);
2151}
2152
hayeswang464ec102015-02-12 14:33:46 +08002153static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002154{
hayeswang464ec102015-02-12 14:33:46 +08002155 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002156
hayeswang464ec102015-02-12 14:33:46 +08002157 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, ocp_data);
2158}
2159
2160static void r8153_set_rx_early_size(struct r8152 *tp)
2161{
2162 u32 mtu = tp->netdev->mtu;
2163 u32 ocp_data = (agg_buf_sz - mtu - VLAN_ETH_HLEN - VLAN_HLEN) / 4;
2164
2165 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08002166}
2167
2168static int rtl8153_enable(struct r8152 *tp)
2169{
hayeswang68714382014-04-11 17:54:31 +08002170 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2171 return -ENODEV;
2172
hayeswangb2143962015-07-24 13:54:23 +08002173 usb_disable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08002174 set_tx_qlen(tp);
2175 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002176 r8153_set_rx_early_timeout(tp);
2177 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002178
2179 return rtl_enable(tp);
2180}
2181
hayeswangd70b1132014-09-19 15:17:18 +08002182static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002183{
hayeswangebc2ec482013-08-14 20:54:38 +08002184 u32 ocp_data;
2185 int i;
hayeswangac718b62013-05-02 16:01:25 +00002186
hayeswang68714382014-04-11 17:54:31 +08002187 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2188 rtl_drop_queued_tx(tp);
2189 return;
2190 }
2191
hayeswangac718b62013-05-02 16:01:25 +00002192 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2193 ocp_data &= ~RCR_ACPT_ALL;
2194 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2195
hayeswang00a5e362014-02-18 21:48:59 +08002196 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002197
2198 for (i = 0; i < RTL8152_MAX_TX; i++)
2199 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002200
hayeswang00a5e362014-02-18 21:48:59 +08002201 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002202
2203 for (i = 0; i < 1000; i++) {
2204 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2205 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2206 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002207 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002208 }
2209
2210 for (i = 0; i < 1000; i++) {
2211 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2212 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002213 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002214 }
2215
hayeswang445f7f42014-09-23 16:31:47 +08002216 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002217
2218 rtl8152_nic_reset(tp);
2219}
2220
hayeswang00a5e362014-02-18 21:48:59 +08002221static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2222{
2223 u32 ocp_data;
2224
2225 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2226 if (enable)
2227 ocp_data |= POWER_CUT;
2228 else
2229 ocp_data &= ~POWER_CUT;
2230 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2231
2232 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2233 ocp_data &= ~RESUME_INDICATE;
2234 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002235}
2236
hayeswangc5554292014-09-12 10:43:11 +08002237static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2238{
2239 u32 ocp_data;
2240
2241 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2242 if (enable)
2243 ocp_data |= CPCR_RX_VLAN;
2244 else
2245 ocp_data &= ~CPCR_RX_VLAN;
2246 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2247}
2248
2249static int rtl8152_set_features(struct net_device *dev,
2250 netdev_features_t features)
2251{
2252 netdev_features_t changed = features ^ dev->features;
2253 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002254 int ret;
2255
2256 ret = usb_autopm_get_interface(tp->intf);
2257 if (ret < 0)
2258 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002259
hayeswangb5403272014-10-09 18:00:26 +08002260 mutex_lock(&tp->control);
2261
hayeswangc5554292014-09-12 10:43:11 +08002262 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2263 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2264 rtl_rx_vlan_en(tp, true);
2265 else
2266 rtl_rx_vlan_en(tp, false);
2267 }
2268
hayeswangb5403272014-10-09 18:00:26 +08002269 mutex_unlock(&tp->control);
2270
hayeswang405f8a02014-10-09 18:00:24 +08002271 usb_autopm_put_interface(tp->intf);
2272
2273out:
2274 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002275}
2276
hayeswang21ff2e82014-02-18 21:49:06 +08002277#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2278
2279static u32 __rtl_get_wol(struct r8152 *tp)
2280{
2281 u32 ocp_data;
2282 u32 wolopts = 0;
2283
2284 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2285 if (!(ocp_data & LAN_WAKE_EN))
2286 return 0;
2287
2288 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2289 if (ocp_data & LINK_ON_WAKE_EN)
2290 wolopts |= WAKE_PHY;
2291
2292 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2293 if (ocp_data & UWF_EN)
2294 wolopts |= WAKE_UCAST;
2295 if (ocp_data & BWF_EN)
2296 wolopts |= WAKE_BCAST;
2297 if (ocp_data & MWF_EN)
2298 wolopts |= WAKE_MCAST;
2299
2300 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2301 if (ocp_data & MAGIC_EN)
2302 wolopts |= WAKE_MAGIC;
2303
2304 return wolopts;
2305}
2306
2307static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2308{
2309 u32 ocp_data;
2310
2311 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2312
2313 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2314 ocp_data &= ~LINK_ON_WAKE_EN;
2315 if (wolopts & WAKE_PHY)
2316 ocp_data |= LINK_ON_WAKE_EN;
2317 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2318
2319 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2320 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
2321 if (wolopts & WAKE_UCAST)
2322 ocp_data |= UWF_EN;
2323 if (wolopts & WAKE_BCAST)
2324 ocp_data |= BWF_EN;
2325 if (wolopts & WAKE_MCAST)
2326 ocp_data |= MWF_EN;
2327 if (wolopts & WAKE_ANY)
2328 ocp_data |= LAN_WAKE_EN;
2329 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2330
2331 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2332
2333 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2334 ocp_data &= ~MAGIC_EN;
2335 if (wolopts & WAKE_MAGIC)
2336 ocp_data |= MAGIC_EN;
2337 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2338
2339 if (wolopts & WAKE_ANY)
2340 device_set_wakeup_enable(&tp->udev->dev, true);
2341 else
2342 device_set_wakeup_enable(&tp->udev->dev, false);
2343}
2344
hayeswangb2143962015-07-24 13:54:23 +08002345static void r8153_u1u2en(struct r8152 *tp, bool enable)
2346{
2347 u8 u1u2[8];
2348
2349 if (enable)
2350 memset(u1u2, 0xff, sizeof(u1u2));
2351 else
2352 memset(u1u2, 0x00, sizeof(u1u2));
2353
2354 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2355}
2356
2357static void r8153_u2p3en(struct r8152 *tp, bool enable)
2358{
2359 u32 ocp_data;
2360
2361 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2362 if (enable && tp->version != RTL_VER_03 && tp->version != RTL_VER_04)
2363 ocp_data |= U2P3_ENABLE;
2364 else
2365 ocp_data &= ~U2P3_ENABLE;
2366 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2367}
2368
2369static void r8153_power_cut_en(struct r8152 *tp, bool enable)
2370{
2371 u32 ocp_data;
2372
2373 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2374 if (enable)
2375 ocp_data |= PWR_EN | PHASE2_EN;
2376 else
2377 ocp_data &= ~(PWR_EN | PHASE2_EN);
2378 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2379
2380 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2381 ocp_data &= ~PCUT_STATUS;
2382 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2383}
2384
hayeswang7daed8d2015-07-24 13:54:24 +08002385static bool rtl_can_wakeup(struct r8152 *tp)
2386{
2387 struct usb_device *udev = tp->udev;
2388
2389 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
2390}
2391
hayeswang9a4be1b2014-02-18 21:49:07 +08002392static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2393{
2394 if (enable) {
2395 u32 ocp_data;
2396
hayeswangb2143962015-07-24 13:54:23 +08002397 r8153_u1u2en(tp, false);
2398 r8153_u2p3en(tp, false);
2399
hayeswang9a4be1b2014-02-18 21:49:07 +08002400 __rtl_set_wol(tp, WAKE_ANY);
2401
2402 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2403
2404 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2405 ocp_data |= LINK_OFF_WAKE_EN;
2406 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2407
2408 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2409 } else {
2410 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangb2143962015-07-24 13:54:23 +08002411 r8153_u2p3en(tp, true);
2412 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08002413 }
2414}
2415
hayeswangaa66a5f2014-02-18 21:49:04 +08002416static void rtl_phy_reset(struct r8152 *tp)
2417{
2418 u16 data;
2419 int i;
2420
2421 clear_bit(PHY_RESET, &tp->flags);
2422
2423 data = r8152_mdio_read(tp, MII_BMCR);
2424
2425 /* don't reset again before the previous one complete */
2426 if (data & BMCR_RESET)
2427 return;
2428
2429 data |= BMCR_RESET;
2430 r8152_mdio_write(tp, MII_BMCR, data);
2431
2432 for (i = 0; i < 50; i++) {
2433 msleep(20);
2434 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2435 break;
2436 }
2437}
2438
hayeswang43499682014-02-18 21:48:58 +08002439static void r8153_teredo_off(struct r8152 *tp)
2440{
2441 u32 ocp_data;
2442
2443 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2444 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
2445 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2446
2447 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2449 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2450}
2451
2452static void r8152b_disable_aldps(struct r8152 *tp)
2453{
2454 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
2455 msleep(20);
2456}
2457
2458static inline void r8152b_enable_aldps(struct r8152 *tp)
2459{
2460 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2461 LINKENA | DIS_SDSAVE);
2462}
2463
hayeswangd70b1132014-09-19 15:17:18 +08002464static void rtl8152_disable(struct r8152 *tp)
2465{
2466 r8152b_disable_aldps(tp);
2467 rtl_disable(tp);
2468 r8152b_enable_aldps(tp);
2469}
2470
hayeswang43499682014-02-18 21:48:58 +08002471static void r8152b_hw_phy_cfg(struct r8152 *tp)
2472{
hayeswangf0cbe0a2014-02-18 21:49:03 +08002473 u16 data;
2474
2475 data = r8152_mdio_read(tp, MII_BMCR);
2476 if (data & BMCR_PDOWN) {
2477 data &= ~BMCR_PDOWN;
2478 r8152_mdio_write(tp, MII_BMCR, data);
2479 }
2480
hayeswangaa66a5f2014-02-18 21:49:04 +08002481 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002482}
2483
hayeswangac718b62013-05-02 16:01:25 +00002484static void r8152b_exit_oob(struct r8152 *tp)
2485{
hayeswangdb8515e2014-03-06 15:07:16 +08002486 u32 ocp_data;
2487 int i;
hayeswangac718b62013-05-02 16:01:25 +00002488
2489 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2490 ocp_data &= ~RCR_ACPT_ALL;
2491 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2492
hayeswang00a5e362014-02-18 21:48:59 +08002493 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002494 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002495 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002496
2497 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2498 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2499
2500 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2501 ocp_data &= ~NOW_IS_OOB;
2502 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2503
2504 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2505 ocp_data &= ~MCU_BORW_EN;
2506 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2507
2508 for (i = 0; i < 1000; i++) {
2509 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2510 if (ocp_data & LINK_LIST_READY)
2511 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002512 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002513 }
2514
2515 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2516 ocp_data |= RE_INIT_LL;
2517 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2518
2519 for (i = 0; i < 1000; i++) {
2520 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2521 if (ocp_data & LINK_LIST_READY)
2522 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002523 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002524 }
2525
2526 rtl8152_nic_reset(tp);
2527
2528 /* rx share fifo credit full threshold */
2529 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2530
hayeswanga3cc4652014-07-24 16:37:43 +08002531 if (tp->udev->speed == USB_SPEED_FULL ||
2532 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00002533 /* rx share fifo credit near full threshold */
2534 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2535 RXFIFO_THR2_FULL);
2536 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2537 RXFIFO_THR3_FULL);
2538 } else {
2539 /* rx share fifo credit near full threshold */
2540 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2541 RXFIFO_THR2_HIGH);
2542 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2543 RXFIFO_THR3_HIGH);
2544 }
2545
2546 /* TX share fifo free credit full threshold */
2547 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2548
2549 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002550 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002551 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2552 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2553
hayeswangc5554292014-09-12 10:43:11 +08002554 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00002555
2556 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2557
2558 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2559 ocp_data |= TCR0_AUTO_FIFO;
2560 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2561}
2562
2563static void r8152b_enter_oob(struct r8152 *tp)
2564{
hayeswang45f4a192014-01-06 17:08:41 +08002565 u32 ocp_data;
2566 int i;
hayeswangac718b62013-05-02 16:01:25 +00002567
2568 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2569 ocp_data &= ~NOW_IS_OOB;
2570 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2571
2572 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2573 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2574 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2575
hayeswangd70b1132014-09-19 15:17:18 +08002576 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002577
2578 for (i = 0; i < 1000; i++) {
2579 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2580 if (ocp_data & LINK_LIST_READY)
2581 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002582 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002583 }
2584
2585 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2586 ocp_data |= RE_INIT_LL;
2587 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2588
2589 for (i = 0; i < 1000; i++) {
2590 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2591 if (ocp_data & LINK_LIST_READY)
2592 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002593 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002594 }
2595
2596 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2597
hayeswangc5554292014-09-12 10:43:11 +08002598 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002599
2600 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2601 ocp_data |= ALDPS_PROXY_MODE;
2602 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2603
2604 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2605 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2606 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2607
hayeswang00a5e362014-02-18 21:48:59 +08002608 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002609
2610 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2611 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2612 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2613}
2614
hayeswang43779f82014-01-02 11:25:10 +08002615static void r8153_hw_phy_cfg(struct r8152 *tp)
2616{
2617 u32 ocp_data;
2618 u16 data;
2619
hayeswangfb02eb42015-07-22 15:27:41 +08002620 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
2621 tp->version == RTL_VER_05)
2622 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
2623
hayeswangf0cbe0a2014-02-18 21:49:03 +08002624 data = r8152_mdio_read(tp, MII_BMCR);
2625 if (data & BMCR_PDOWN) {
2626 data &= ~BMCR_PDOWN;
2627 r8152_mdio_write(tp, MII_BMCR, data);
2628 }
hayeswang43779f82014-01-02 11:25:10 +08002629
2630 if (tp->version == RTL_VER_03) {
2631 data = ocp_reg_read(tp, OCP_EEE_CFG);
2632 data &= ~CTAP_SHORT_EN;
2633 ocp_reg_write(tp, OCP_EEE_CFG, data);
2634 }
2635
2636 data = ocp_reg_read(tp, OCP_POWER_CFG);
2637 data |= EEE_CLKDIV_EN;
2638 ocp_reg_write(tp, OCP_POWER_CFG, data);
2639
2640 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2641 data |= EN_10M_BGOFF;
2642 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2643 data = ocp_reg_read(tp, OCP_POWER_CFG);
2644 data |= EN_10M_PLLOFF;
2645 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08002646 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08002647
2648 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2649 ocp_data |= PFM_PWM_SWITCH;
2650 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2651
hayeswangb4d99de2015-01-19 17:02:46 +08002652 /* Enable LPF corner auto tune */
2653 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08002654
hayeswangb4d99de2015-01-19 17:02:46 +08002655 /* Adjust 10M Amplitude */
2656 sram_write(tp, SRAM_10M_AMP1, 0x00af);
2657 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08002658
2659 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002660}
2661
hayeswang43779f82014-01-02 11:25:10 +08002662static void r8153_first_init(struct r8152 *tp)
2663{
2664 u32 ocp_data;
2665 int i;
2666
hayeswang00a5e362014-02-18 21:48:59 +08002667 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002668 r8153_teredo_off(tp);
2669
2670 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2671 ocp_data &= ~RCR_ACPT_ALL;
2672 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2673
2674 r8153_hw_phy_cfg(tp);
2675
2676 rtl8152_nic_reset(tp);
2677
2678 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2679 ocp_data &= ~NOW_IS_OOB;
2680 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2681
2682 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2683 ocp_data &= ~MCU_BORW_EN;
2684 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2685
2686 for (i = 0; i < 1000; i++) {
2687 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2688 if (ocp_data & LINK_LIST_READY)
2689 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002690 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002691 }
2692
2693 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2694 ocp_data |= RE_INIT_LL;
2695 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2696
2697 for (i = 0; i < 1000; i++) {
2698 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2699 if (ocp_data & LINK_LIST_READY)
2700 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002701 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002702 }
2703
hayeswangc5554292014-09-12 10:43:11 +08002704 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08002705
hayeswang69b4b7a2014-07-10 10:58:54 +08002706 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
2707 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08002708
2709 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2710 ocp_data |= TCR0_AUTO_FIFO;
2711 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2712
2713 rtl8152_nic_reset(tp);
2714
2715 /* rx share fifo credit full threshold */
2716 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2717 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2718 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2719 /* TX share fifo free credit full threshold */
2720 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2721
hayeswang9629e3c2014-01-15 10:42:15 +08002722 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002723 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08002724 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswang43779f82014-01-02 11:25:10 +08002725 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2726}
2727
2728static void r8153_enter_oob(struct r8152 *tp)
2729{
2730 u32 ocp_data;
2731 int i;
2732
2733 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2734 ocp_data &= ~NOW_IS_OOB;
2735 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2736
hayeswangd70b1132014-09-19 15:17:18 +08002737 rtl_disable(tp);
hayeswang43779f82014-01-02 11:25:10 +08002738
2739 for (i = 0; i < 1000; i++) {
2740 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2741 if (ocp_data & LINK_LIST_READY)
2742 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002743 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002744 }
2745
2746 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2747 ocp_data |= RE_INIT_LL;
2748 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2749
2750 for (i = 0; i < 1000; i++) {
2751 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2752 if (ocp_data & LINK_LIST_READY)
2753 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002754 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08002755 }
2756
hayeswang69b4b7a2014-07-10 10:58:54 +08002757 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
hayeswang43779f82014-01-02 11:25:10 +08002758
hayeswang43779f82014-01-02 11:25:10 +08002759 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2760 ocp_data &= ~TEREDO_WAKE_MASK;
2761 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2762
hayeswangc5554292014-09-12 10:43:11 +08002763 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002764
2765 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2766 ocp_data |= ALDPS_PROXY_MODE;
2767 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2768
2769 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2770 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2771 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2772
hayeswang00a5e362014-02-18 21:48:59 +08002773 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002774
2775 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2776 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2777 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2778}
2779
2780static void r8153_disable_aldps(struct r8152 *tp)
2781{
2782 u16 data;
2783
2784 data = ocp_reg_read(tp, OCP_POWER_CFG);
2785 data &= ~EN_ALDPS;
2786 ocp_reg_write(tp, OCP_POWER_CFG, data);
2787 msleep(20);
2788}
2789
2790static void r8153_enable_aldps(struct r8152 *tp)
2791{
2792 u16 data;
2793
2794 data = ocp_reg_read(tp, OCP_POWER_CFG);
2795 data |= EN_ALDPS;
2796 ocp_reg_write(tp, OCP_POWER_CFG, data);
2797}
2798
hayeswangd70b1132014-09-19 15:17:18 +08002799static void rtl8153_disable(struct r8152 *tp)
2800{
2801 r8153_disable_aldps(tp);
2802 rtl_disable(tp);
2803 r8153_enable_aldps(tp);
hayeswangb2143962015-07-24 13:54:23 +08002804 usb_enable_lpm(tp->udev);
hayeswangd70b1132014-09-19 15:17:18 +08002805}
2806
hayeswangac718b62013-05-02 16:01:25 +00002807static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2808{
hayeswang43779f82014-01-02 11:25:10 +08002809 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002810 int ret = 0;
2811
2812 cancel_delayed_work_sync(&tp->schedule);
2813 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2814 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2815 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002816 if (tp->mii.supports_gmii) {
2817 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2818 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2819 } else {
2820 gbcr = 0;
2821 }
hayeswangac718b62013-05-02 16:01:25 +00002822
2823 if (autoneg == AUTONEG_DISABLE) {
2824 if (speed == SPEED_10) {
2825 bmcr = 0;
2826 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2827 } else if (speed == SPEED_100) {
2828 bmcr = BMCR_SPEED100;
2829 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002830 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2831 bmcr = BMCR_SPEED1000;
2832 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002833 } else {
2834 ret = -EINVAL;
2835 goto out;
2836 }
2837
2838 if (duplex == DUPLEX_FULL)
2839 bmcr |= BMCR_FULLDPLX;
2840 } else {
2841 if (speed == SPEED_10) {
2842 if (duplex == DUPLEX_FULL)
2843 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2844 else
2845 anar |= ADVERTISE_10HALF;
2846 } else if (speed == SPEED_100) {
2847 if (duplex == DUPLEX_FULL) {
2848 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2849 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2850 } else {
2851 anar |= ADVERTISE_10HALF;
2852 anar |= ADVERTISE_100HALF;
2853 }
hayeswang43779f82014-01-02 11:25:10 +08002854 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2855 if (duplex == DUPLEX_FULL) {
2856 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2857 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2858 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2859 } else {
2860 anar |= ADVERTISE_10HALF;
2861 anar |= ADVERTISE_100HALF;
2862 gbcr |= ADVERTISE_1000HALF;
2863 }
hayeswangac718b62013-05-02 16:01:25 +00002864 } else {
2865 ret = -EINVAL;
2866 goto out;
2867 }
2868
2869 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2870 }
2871
hayeswangaa66a5f2014-02-18 21:49:04 +08002872 if (test_bit(PHY_RESET, &tp->flags))
2873 bmcr |= BMCR_RESET;
2874
hayeswang43779f82014-01-02 11:25:10 +08002875 if (tp->mii.supports_gmii)
2876 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2877
hayeswangac718b62013-05-02 16:01:25 +00002878 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2879 r8152_mdio_write(tp, MII_BMCR, bmcr);
2880
hayeswangaa66a5f2014-02-18 21:49:04 +08002881 if (test_bit(PHY_RESET, &tp->flags)) {
2882 int i;
2883
2884 clear_bit(PHY_RESET, &tp->flags);
2885 for (i = 0; i < 50; i++) {
2886 msleep(20);
2887 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2888 break;
2889 }
2890 }
2891
hayeswangac718b62013-05-02 16:01:25 +00002892out:
hayeswangac718b62013-05-02 16:01:25 +00002893
2894 return ret;
2895}
2896
hayeswangd70b1132014-09-19 15:17:18 +08002897static void rtl8152_up(struct r8152 *tp)
2898{
2899 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2900 return;
2901
2902 r8152b_disable_aldps(tp);
2903 r8152b_exit_oob(tp);
2904 r8152b_enable_aldps(tp);
2905}
2906
hayeswangac718b62013-05-02 16:01:25 +00002907static void rtl8152_down(struct r8152 *tp)
2908{
hayeswang68714382014-04-11 17:54:31 +08002909 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2910 rtl_drop_queued_tx(tp);
2911 return;
2912 }
2913
hayeswang00a5e362014-02-18 21:48:59 +08002914 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002915 r8152b_disable_aldps(tp);
2916 r8152b_enter_oob(tp);
2917 r8152b_enable_aldps(tp);
2918}
2919
hayeswangd70b1132014-09-19 15:17:18 +08002920static void rtl8153_up(struct r8152 *tp)
2921{
2922 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2923 return;
2924
hayeswangb2143962015-07-24 13:54:23 +08002925 r8153_u1u2en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08002926 r8153_disable_aldps(tp);
2927 r8153_first_init(tp);
2928 r8153_enable_aldps(tp);
hayeswangb2143962015-07-24 13:54:23 +08002929 r8153_u2p3en(tp, true);
2930 r8153_u1u2en(tp, true);
2931 usb_enable_lpm(tp->udev);
hayeswangd70b1132014-09-19 15:17:18 +08002932}
2933
hayeswang43779f82014-01-02 11:25:10 +08002934static void rtl8153_down(struct r8152 *tp)
2935{
hayeswang68714382014-04-11 17:54:31 +08002936 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2937 rtl_drop_queued_tx(tp);
2938 return;
2939 }
2940
hayeswangb9702722014-02-18 21:49:00 +08002941 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08002942 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08002943 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002944 r8153_disable_aldps(tp);
2945 r8153_enter_oob(tp);
2946 r8153_enable_aldps(tp);
2947}
2948
hayeswangac718b62013-05-02 16:01:25 +00002949static void set_carrier(struct r8152 *tp)
2950{
2951 struct net_device *netdev = tp->netdev;
2952 u8 speed;
2953
hayeswang40a82912013-08-14 20:54:40 +08002954 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002955 speed = rtl8152_get_speed(tp);
2956
2957 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08002958 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08002959 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002960 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang41cec842015-07-24 13:54:25 +08002961 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00002962 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08002963 rtl_start_rx(tp);
hayeswang41cec842015-07-24 13:54:25 +08002964 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00002965 }
2966 } else {
hayeswang51d979f2015-02-06 11:30:47 +08002967 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002968 netif_carrier_off(netdev);
hayeswangd823ab62015-01-12 12:06:23 +08002969 napi_disable(&tp->napi);
hayeswangc81229c2014-01-02 11:22:42 +08002970 tp->rtl_ops.disable(tp);
hayeswangd823ab62015-01-12 12:06:23 +08002971 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00002972 }
2973 }
hayeswangac718b62013-05-02 16:01:25 +00002974}
2975
2976static void rtl_work_func_t(struct work_struct *work)
2977{
2978 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2979
hayeswanga1f83fe2014-11-12 10:05:05 +08002980 /* If the device is unplugged or !netif_running(), the workqueue
2981 * doesn't need to wake the device, and could return directly.
2982 */
2983 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
2984 return;
2985
hayeswang9a4be1b2014-02-18 21:49:07 +08002986 if (usb_autopm_get_interface(tp->intf) < 0)
2987 return;
2988
hayeswangac718b62013-05-02 16:01:25 +00002989 if (!test_bit(WORK_ENABLE, &tp->flags))
2990 goto out1;
2991
hayeswangb5403272014-10-09 18:00:26 +08002992 if (!mutex_trylock(&tp->control)) {
2993 schedule_delayed_work(&tp->schedule, 0);
2994 goto out1;
2995 }
2996
hayeswang40a82912013-08-14 20:54:40 +08002997 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2998 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002999
3000 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
3001 _rtl8152_set_rx_mode(tp->netdev);
3002
hayeswangd823ab62015-01-12 12:06:23 +08003003 /* don't schedule napi before linking */
3004 if (test_bit(SCHEDULE_NAPI, &tp->flags) &&
hayeswang51d979f2015-02-06 11:30:47 +08003005 netif_carrier_ok(tp->netdev)) {
hayeswangd823ab62015-01-12 12:06:23 +08003006 clear_bit(SCHEDULE_NAPI, &tp->flags);
3007 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08003008 }
hayeswangaa66a5f2014-02-18 21:49:04 +08003009
3010 if (test_bit(PHY_RESET, &tp->flags))
3011 rtl_phy_reset(tp);
3012
hayeswangb5403272014-10-09 18:00:26 +08003013 mutex_unlock(&tp->control);
3014
hayeswangac718b62013-05-02 16:01:25 +00003015out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08003016 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003017}
3018
3019static int rtl8152_open(struct net_device *netdev)
3020{
3021 struct r8152 *tp = netdev_priv(netdev);
3022 int res = 0;
3023
hayeswang7e9da482014-02-18 21:49:05 +08003024 res = alloc_all_mem(tp);
3025 if (res)
3026 goto out;
3027
hayeswang51d979f2015-02-06 11:30:47 +08003028 netif_carrier_off(netdev);
hayeswangf4c74762014-10-29 11:12:16 +08003029
hayeswang9a4be1b2014-02-18 21:49:07 +08003030 res = usb_autopm_get_interface(tp->intf);
3031 if (res < 0) {
3032 free_all_mem(tp);
3033 goto out;
3034 }
3035
hayeswangb5403272014-10-09 18:00:26 +08003036 mutex_lock(&tp->control);
3037
hayeswang9a4be1b2014-02-18 21:49:07 +08003038 /* The WORK_ENABLE may be set when autoresume occurs */
3039 if (test_bit(WORK_ENABLE, &tp->flags)) {
3040 clear_bit(WORK_ENABLE, &tp->flags);
3041 usb_kill_urb(tp->intr_urb);
3042 cancel_delayed_work_sync(&tp->schedule);
hayeswangf4c74762014-10-29 11:12:16 +08003043
3044 /* disable the tx/rx, if the workqueue has enabled them. */
hayeswang51d979f2015-02-06 11:30:47 +08003045 if (netif_carrier_ok(netdev))
hayeswang9a4be1b2014-02-18 21:49:07 +08003046 tp->rtl_ops.disable(tp);
3047 }
3048
hayeswang7e9da482014-02-18 21:49:05 +08003049 tp->rtl_ops.up(tp);
3050
hayeswang43779f82014-01-02 11:25:10 +08003051 rtl8152_set_speed(tp, AUTONEG_ENABLE,
3052 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
3053 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08003054 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003055 netif_start_queue(netdev);
3056 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08003057
hayeswang3d55f442014-02-06 11:55:48 +08003058 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3059 if (res) {
3060 if (res == -ENODEV)
3061 netif_device_detach(tp->netdev);
3062 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3063 res);
hayeswang7e9da482014-02-18 21:49:05 +08003064 free_all_mem(tp);
hayeswang93ffbea2014-11-05 10:17:02 +08003065 } else {
hayeswangd823ab62015-01-12 12:06:23 +08003066 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08003067 }
3068
hayeswangb5403272014-10-09 18:00:26 +08003069 mutex_unlock(&tp->control);
3070
hayeswang9a4be1b2014-02-18 21:49:07 +08003071 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003072
hayeswang7e9da482014-02-18 21:49:05 +08003073out:
hayeswangac718b62013-05-02 16:01:25 +00003074 return res;
3075}
3076
3077static int rtl8152_close(struct net_device *netdev)
3078{
3079 struct r8152 *tp = netdev_priv(netdev);
3080 int res = 0;
3081
hayeswangd823ab62015-01-12 12:06:23 +08003082 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003083 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08003084 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00003085 cancel_delayed_work_sync(&tp->schedule);
3086 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003087
3088 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08003089 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003090 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08003091 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003092 } else {
hayeswangb5403272014-10-09 18:00:26 +08003093 mutex_lock(&tp->control);
3094
hayeswangb209af92014-08-25 15:53:00 +08003095 /* The autosuspend may have been enabled and wouldn't
hayeswang9a4be1b2014-02-18 21:49:07 +08003096 * be disable when autoresume occurs, because the
3097 * netif_running() would be false.
3098 */
hayeswang923e1ee2014-10-29 11:12:15 +08003099 rtl_runtime_suspend_enable(tp, false);
hayeswang9a4be1b2014-02-18 21:49:07 +08003100
hayeswang9a4be1b2014-02-18 21:49:07 +08003101 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08003102
3103 mutex_unlock(&tp->control);
3104
hayeswang9a4be1b2014-02-18 21:49:07 +08003105 usb_autopm_put_interface(tp->intf);
3106 }
hayeswangac718b62013-05-02 16:01:25 +00003107
hayeswang7e9da482014-02-18 21:49:05 +08003108 free_all_mem(tp);
3109
hayeswangac718b62013-05-02 16:01:25 +00003110 return res;
3111}
3112
hayeswangd24f6132014-09-25 20:54:01 +08003113static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
hayeswangac718b62013-05-02 16:01:25 +00003114{
hayeswangd24f6132014-09-25 20:54:01 +08003115 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3116 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3117 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3118}
3119
3120static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3121{
3122 u16 data;
3123
3124 r8152_mmd_indirect(tp, dev, reg);
3125 data = ocp_reg_read(tp, OCP_EEE_DATA);
3126 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3127
3128 return data;
3129}
3130
3131static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3132{
3133 r8152_mmd_indirect(tp, dev, reg);
3134 ocp_reg_write(tp, OCP_EEE_DATA, data);
3135 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3136}
3137
3138static void r8152_eee_en(struct r8152 *tp, bool enable)
3139{
3140 u16 config1, config2, config3;
hayeswang45f4a192014-01-06 17:08:41 +08003141 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003142
3143 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
hayeswangd24f6132014-09-25 20:54:01 +08003144 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3145 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3146 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3147
3148 if (enable) {
3149 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3150 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3151 config1 |= sd_rise_time(1);
3152 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3153 config3 |= fast_snr(42);
3154 } else {
3155 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3156 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3157 RX_QUIET_EN);
3158 config1 |= sd_rise_time(7);
3159 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3160 config3 |= fast_snr(511);
3161 }
3162
hayeswangac718b62013-05-02 16:01:25 +00003163 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
hayeswangd24f6132014-09-25 20:54:01 +08003164 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3165 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3166 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3167}
3168
3169static void r8152b_enable_eee(struct r8152 *tp)
3170{
3171 r8152_eee_en(tp, true);
3172 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3173}
3174
3175static void r8153_eee_en(struct r8152 *tp, bool enable)
3176{
3177 u32 ocp_data;
3178 u16 config;
3179
3180 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3181 config = ocp_reg_read(tp, OCP_EEE_CFG);
3182
3183 if (enable) {
3184 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3185 config |= EEE10_EN;
3186 } else {
3187 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3188 config &= ~EEE10_EN;
3189 }
3190
3191 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3192 ocp_reg_write(tp, OCP_EEE_CFG, config);
hayeswangac718b62013-05-02 16:01:25 +00003193}
3194
hayeswang43779f82014-01-02 11:25:10 +08003195static void r8153_enable_eee(struct r8152 *tp)
3196{
hayeswangd24f6132014-09-25 20:54:01 +08003197 r8153_eee_en(tp, true);
3198 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
hayeswang43779f82014-01-02 11:25:10 +08003199}
3200
hayeswangac718b62013-05-02 16:01:25 +00003201static void r8152b_enable_fc(struct r8152 *tp)
3202{
3203 u16 anar;
3204
3205 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3206 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3207 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3208}
3209
hayeswang4f1d4d52014-03-11 16:24:19 +08003210static void rtl_tally_reset(struct r8152 *tp)
3211{
3212 u32 ocp_data;
3213
3214 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
3215 ocp_data |= TALLY_RESET;
3216 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
3217}
3218
hayeswangac718b62013-05-02 16:01:25 +00003219static void r8152b_init(struct r8152 *tp)
3220{
hayeswangebc2ec482013-08-14 20:54:38 +08003221 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003222
hayeswang68714382014-04-11 17:54:31 +08003223 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3224 return;
3225
hayeswangd70b1132014-09-19 15:17:18 +08003226 r8152b_disable_aldps(tp);
3227
hayeswangac718b62013-05-02 16:01:25 +00003228 if (tp->version == RTL_VER_01) {
3229 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3230 ocp_data &= ~LED_MODE_MASK;
3231 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3232 }
3233
hayeswang00a5e362014-02-18 21:48:59 +08003234 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003235
hayeswangac718b62013-05-02 16:01:25 +00003236 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3237 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
3238 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3239 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
3240 ocp_data &= ~MCU_CLK_RATIO_MASK;
3241 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
3242 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
3243 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
3244 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
3245 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
3246
3247 r8152b_enable_eee(tp);
3248 r8152b_enable_aldps(tp);
3249 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003250 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00003251
hayeswangebc2ec482013-08-14 20:54:38 +08003252 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00003253 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08003254 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00003255 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
3256}
3257
hayeswang43779f82014-01-02 11:25:10 +08003258static void r8153_init(struct r8152 *tp)
3259{
3260 u32 ocp_data;
3261 int i;
3262
hayeswang68714382014-04-11 17:54:31 +08003263 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3264 return;
3265
hayeswangd70b1132014-09-19 15:17:18 +08003266 r8153_disable_aldps(tp);
hayeswangb9702722014-02-18 21:49:00 +08003267 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003268
3269 for (i = 0; i < 500; i++) {
3270 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3271 AUTOLOAD_DONE)
3272 break;
3273 msleep(20);
3274 }
3275
3276 for (i = 0; i < 500; i++) {
3277 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
3278 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
3279 break;
3280 msleep(20);
3281 }
3282
hayeswangb2143962015-07-24 13:54:23 +08003283 usb_disable_lpm(tp->udev);
hayeswangb9702722014-02-18 21:49:00 +08003284 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003285
hayeswang65bab842015-02-12 16:20:46 +08003286 if (tp->version == RTL_VER_04) {
3287 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
3288 ocp_data &= ~pwd_dn_scale_mask;
3289 ocp_data |= pwd_dn_scale(96);
3290 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
3291
3292 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
3293 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
3294 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
3295 } else if (tp->version == RTL_VER_05) {
3296 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
3297 ocp_data &= ~ECM_ALDPS;
3298 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
3299
3300 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
3301 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
3302 ocp_data &= ~DYNAMIC_BURST;
3303 else
3304 ocp_data |= DYNAMIC_BURST;
3305 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08003306 } else if (tp->version == RTL_VER_06) {
3307 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
3308 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
3309 ocp_data &= ~DYNAMIC_BURST;
3310 else
3311 ocp_data |= DYNAMIC_BURST;
3312 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08003313 }
3314
3315 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
3316 ocp_data |= EP4_FULL_FC;
3317 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
3318
hayeswang43779f82014-01-02 11:25:10 +08003319 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
3320 ocp_data &= ~TIMER11_EN;
3321 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
3322
hayeswang43779f82014-01-02 11:25:10 +08003323 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3324 ocp_data &= ~LED_MODE_MASK;
3325 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3326
hayeswang65bab842015-02-12 16:20:46 +08003327 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
hayeswang34203e22015-02-06 11:30:46 +08003328 if (tp->version == RTL_VER_04 && tp->udev->speed != USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08003329 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08003330 else
3331 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08003332 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
3333
3334 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
3335 ocp_data &= ~SEN_VAL_MASK;
3336 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
3337 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
3338
hayeswang65bab842015-02-12 16:20:46 +08003339 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
3340
hayeswangb9702722014-02-18 21:49:00 +08003341 r8153_power_cut_en(tp, false);
3342 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003343
hayeswang43779f82014-01-02 11:25:10 +08003344 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
3345 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
3346 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3347 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3348 U1U2_SPDWN_EN | L1_SPDWN_EN);
3349 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3350 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3351 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
3352 EEE_SPDWN_EN);
3353
3354 r8153_enable_eee(tp);
3355 r8153_enable_aldps(tp);
3356 r8152b_enable_fc(tp);
hayeswang4f1d4d52014-03-11 16:24:19 +08003357 rtl_tally_reset(tp);
hayeswangb2143962015-07-24 13:54:23 +08003358 r8153_u2p3en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003359}
3360
hayeswange5011392015-07-29 20:39:08 +08003361static int rtl8152_pre_reset(struct usb_interface *intf)
3362{
3363 struct r8152 *tp = usb_get_intfdata(intf);
3364 struct net_device *netdev;
3365
3366 if (!tp)
3367 return 0;
3368
3369 netdev = tp->netdev;
3370 if (!netif_running(netdev))
3371 return 0;
3372
3373 napi_disable(&tp->napi);
3374 clear_bit(WORK_ENABLE, &tp->flags);
3375 usb_kill_urb(tp->intr_urb);
3376 cancel_delayed_work_sync(&tp->schedule);
3377 if (netif_carrier_ok(netdev)) {
3378 netif_stop_queue(netdev);
3379 mutex_lock(&tp->control);
3380 tp->rtl_ops.disable(tp);
3381 mutex_unlock(&tp->control);
3382 }
3383
3384 return 0;
3385}
3386
3387static int rtl8152_post_reset(struct usb_interface *intf)
3388{
3389 struct r8152 *tp = usb_get_intfdata(intf);
3390 struct net_device *netdev;
3391
3392 if (!tp)
3393 return 0;
3394
3395 netdev = tp->netdev;
3396 if (!netif_running(netdev))
3397 return 0;
3398
3399 set_bit(WORK_ENABLE, &tp->flags);
3400 if (netif_carrier_ok(netdev)) {
3401 mutex_lock(&tp->control);
3402 tp->rtl_ops.enable(tp);
3403 rtl8152_set_rx_mode(netdev);
3404 mutex_unlock(&tp->control);
3405 netif_wake_queue(netdev);
3406 }
3407
3408 napi_enable(&tp->napi);
3409
3410 return 0;
hayeswangac718b62013-05-02 16:01:25 +00003411}
3412
3413static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
3414{
3415 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang6cc69f22014-10-17 16:55:08 +08003416 struct net_device *netdev = tp->netdev;
3417 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00003418
hayeswangb5403272014-10-09 18:00:26 +08003419 mutex_lock(&tp->control);
3420
hayeswang6cc69f22014-10-17 16:55:08 +08003421 if (PMSG_IS_AUTO(message)) {
3422 if (netif_running(netdev) && work_busy(&tp->schedule.work)) {
3423 ret = -EBUSY;
3424 goto out1;
3425 }
hayeswangac718b62013-05-02 16:01:25 +00003426
hayeswang6cc69f22014-10-17 16:55:08 +08003427 set_bit(SELECTIVE_SUSPEND, &tp->flags);
3428 } else {
3429 netif_device_detach(netdev);
3430 }
3431
hayeswange3bd1a82014-10-29 11:12:17 +08003432 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangac718b62013-05-02 16:01:25 +00003433 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08003434 usb_kill_urb(tp->intr_urb);
hayeswangd823ab62015-01-12 12:06:23 +08003435 napi_disable(&tp->napi);
hayeswang9a4be1b2014-02-18 21:49:07 +08003436 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswang445f7f42014-09-23 16:31:47 +08003437 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003438 rtl_runtime_suspend_enable(tp, true);
3439 } else {
hayeswang6cc69f22014-10-17 16:55:08 +08003440 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08003441 tp->rtl_ops.down(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08003442 }
hayeswangd823ab62015-01-12 12:06:23 +08003443 napi_enable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003444 }
hayeswang6cc69f22014-10-17 16:55:08 +08003445out1:
hayeswangb5403272014-10-09 18:00:26 +08003446 mutex_unlock(&tp->control);
3447
hayeswang6cc69f22014-10-17 16:55:08 +08003448 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003449}
3450
3451static int rtl8152_resume(struct usb_interface *intf)
3452{
3453 struct r8152 *tp = usb_get_intfdata(intf);
3454
hayeswangb5403272014-10-09 18:00:26 +08003455 mutex_lock(&tp->control);
3456
hayeswang9a4be1b2014-02-18 21:49:07 +08003457 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3458 tp->rtl_ops.init(tp);
3459 netif_device_attach(tp->netdev);
3460 }
3461
hayeswangac718b62013-05-02 16:01:25 +00003462 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08003463 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3464 rtl_runtime_suspend_enable(tp, false);
3465 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswang41cec842015-07-24 13:54:25 +08003466 napi_disable(&tp->napi);
hayeswang445f7f42014-09-23 16:31:47 +08003467 set_bit(WORK_ENABLE, &tp->flags);
hayeswang51d979f2015-02-06 11:30:47 +08003468 if (netif_carrier_ok(tp->netdev))
hayeswang445f7f42014-09-23 16:31:47 +08003469 rtl_start_rx(tp);
hayeswang41cec842015-07-24 13:54:25 +08003470 napi_enable(&tp->napi);
hayeswang9a4be1b2014-02-18 21:49:07 +08003471 } else {
3472 tp->rtl_ops.up(tp);
3473 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswangb209af92014-08-25 15:53:00 +08003474 tp->mii.supports_gmii ?
3475 SPEED_1000 : SPEED_100,
3476 DUPLEX_FULL);
hayeswang445f7f42014-09-23 16:31:47 +08003477 netif_carrier_off(tp->netdev);
3478 set_bit(WORK_ENABLE, &tp->flags);
hayeswang9a4be1b2014-02-18 21:49:07 +08003479 }
hayeswang40a82912013-08-14 20:54:40 +08003480 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswang923e1ee2014-10-29 11:12:15 +08003481 } else if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3482 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00003483 }
3484
hayeswangb5403272014-10-09 18:00:26 +08003485 mutex_unlock(&tp->control);
3486
hayeswangac718b62013-05-02 16:01:25 +00003487 return 0;
3488}
3489
hayeswang21ff2e82014-02-18 21:49:06 +08003490static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3491{
3492 struct r8152 *tp = netdev_priv(dev);
3493
hayeswang9a4be1b2014-02-18 21:49:07 +08003494 if (usb_autopm_get_interface(tp->intf) < 0)
3495 return;
3496
hayeswang7daed8d2015-07-24 13:54:24 +08003497 if (!rtl_can_wakeup(tp)) {
3498 wol->supported = 0;
3499 wol->wolopts = 0;
3500 } else {
3501 mutex_lock(&tp->control);
3502 wol->supported = WAKE_ANY;
3503 wol->wolopts = __rtl_get_wol(tp);
3504 mutex_unlock(&tp->control);
3505 }
hayeswangb5403272014-10-09 18:00:26 +08003506
hayeswang9a4be1b2014-02-18 21:49:07 +08003507 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08003508}
3509
3510static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3511{
3512 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003513 int ret;
3514
hayeswang7daed8d2015-07-24 13:54:24 +08003515 if (!rtl_can_wakeup(tp))
3516 return -EOPNOTSUPP;
3517
hayeswang9a4be1b2014-02-18 21:49:07 +08003518 ret = usb_autopm_get_interface(tp->intf);
3519 if (ret < 0)
3520 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08003521
hayeswangb5403272014-10-09 18:00:26 +08003522 mutex_lock(&tp->control);
3523
hayeswang21ff2e82014-02-18 21:49:06 +08003524 __rtl_set_wol(tp, wol->wolopts);
3525 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
3526
hayeswangb5403272014-10-09 18:00:26 +08003527 mutex_unlock(&tp->control);
3528
hayeswang9a4be1b2014-02-18 21:49:07 +08003529 usb_autopm_put_interface(tp->intf);
3530
3531out_set_wol:
3532 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08003533}
3534
hayeswanga5ec27c2014-02-18 21:49:11 +08003535static u32 rtl8152_get_msglevel(struct net_device *dev)
3536{
3537 struct r8152 *tp = netdev_priv(dev);
3538
3539 return tp->msg_enable;
3540}
3541
3542static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
3543{
3544 struct r8152 *tp = netdev_priv(dev);
3545
3546 tp->msg_enable = value;
3547}
3548
hayeswangac718b62013-05-02 16:01:25 +00003549static void rtl8152_get_drvinfo(struct net_device *netdev,
3550 struct ethtool_drvinfo *info)
3551{
3552 struct r8152 *tp = netdev_priv(netdev);
3553
hayeswangb0b46c72014-08-26 10:08:23 +08003554 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
3555 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00003556 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
3557}
3558
3559static
3560int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
3561{
3562 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08003563 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003564
3565 if (!tp->mii.mdio_read)
3566 return -EOPNOTSUPP;
3567
hayeswang8d4a4d72014-10-09 18:00:25 +08003568 ret = usb_autopm_get_interface(tp->intf);
3569 if (ret < 0)
3570 goto out;
3571
hayeswangb5403272014-10-09 18:00:26 +08003572 mutex_lock(&tp->control);
3573
hayeswang8d4a4d72014-10-09 18:00:25 +08003574 ret = mii_ethtool_gset(&tp->mii, cmd);
3575
hayeswangb5403272014-10-09 18:00:26 +08003576 mutex_unlock(&tp->control);
3577
hayeswang8d4a4d72014-10-09 18:00:25 +08003578 usb_autopm_put_interface(tp->intf);
3579
3580out:
3581 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003582}
3583
3584static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3585{
3586 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08003587 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003588
hayeswang9a4be1b2014-02-18 21:49:07 +08003589 ret = usb_autopm_get_interface(tp->intf);
3590 if (ret < 0)
3591 goto out;
3592
hayeswangb5403272014-10-09 18:00:26 +08003593 mutex_lock(&tp->control);
3594
hayeswang9a4be1b2014-02-18 21:49:07 +08003595 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
3596
hayeswangb5403272014-10-09 18:00:26 +08003597 mutex_unlock(&tp->control);
3598
hayeswang9a4be1b2014-02-18 21:49:07 +08003599 usb_autopm_put_interface(tp->intf);
3600
3601out:
3602 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003603}
3604
hayeswang4f1d4d52014-03-11 16:24:19 +08003605static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
3606 "tx_packets",
3607 "rx_packets",
3608 "tx_errors",
3609 "rx_errors",
3610 "rx_missed",
3611 "align_errors",
3612 "tx_single_collisions",
3613 "tx_multi_collisions",
3614 "rx_unicast",
3615 "rx_broadcast",
3616 "rx_multicast",
3617 "tx_aborted",
3618 "tx_underrun",
3619};
3620
3621static int rtl8152_get_sset_count(struct net_device *dev, int sset)
3622{
3623 switch (sset) {
3624 case ETH_SS_STATS:
3625 return ARRAY_SIZE(rtl8152_gstrings);
3626 default:
3627 return -EOPNOTSUPP;
3628 }
3629}
3630
3631static void rtl8152_get_ethtool_stats(struct net_device *dev,
3632 struct ethtool_stats *stats, u64 *data)
3633{
3634 struct r8152 *tp = netdev_priv(dev);
3635 struct tally_counter tally;
3636
hayeswang0b030242014-07-08 14:49:28 +08003637 if (usb_autopm_get_interface(tp->intf) < 0)
3638 return;
3639
hayeswang4f1d4d52014-03-11 16:24:19 +08003640 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
3641
hayeswang0b030242014-07-08 14:49:28 +08003642 usb_autopm_put_interface(tp->intf);
3643
hayeswang4f1d4d52014-03-11 16:24:19 +08003644 data[0] = le64_to_cpu(tally.tx_packets);
3645 data[1] = le64_to_cpu(tally.rx_packets);
3646 data[2] = le64_to_cpu(tally.tx_errors);
3647 data[3] = le32_to_cpu(tally.rx_errors);
3648 data[4] = le16_to_cpu(tally.rx_missed);
3649 data[5] = le16_to_cpu(tally.align_errors);
3650 data[6] = le32_to_cpu(tally.tx_one_collision);
3651 data[7] = le32_to_cpu(tally.tx_multi_collision);
3652 data[8] = le64_to_cpu(tally.rx_unicast);
3653 data[9] = le64_to_cpu(tally.rx_broadcast);
3654 data[10] = le32_to_cpu(tally.rx_multicast);
3655 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08003656 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08003657}
3658
3659static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3660{
3661 switch (stringset) {
3662 case ETH_SS_STATS:
3663 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
3664 break;
3665 }
3666}
3667
hayeswangdf35d282014-09-25 20:54:02 +08003668static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3669{
3670 u32 ocp_data, lp, adv, supported = 0;
3671 u16 val;
3672
3673 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
3674 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3675
3676 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
3677 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3678
3679 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
3680 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3681
3682 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3683 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3684
3685 eee->eee_enabled = !!ocp_data;
3686 eee->eee_active = !!(supported & adv & lp);
3687 eee->supported = supported;
3688 eee->advertised = adv;
3689 eee->lp_advertised = lp;
3690
3691 return 0;
3692}
3693
3694static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3695{
3696 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3697
3698 r8152_eee_en(tp, eee->eee_enabled);
3699
3700 if (!eee->eee_enabled)
3701 val = 0;
3702
3703 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
3704
3705 return 0;
3706}
3707
3708static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
3709{
3710 u32 ocp_data, lp, adv, supported = 0;
3711 u16 val;
3712
3713 val = ocp_reg_read(tp, OCP_EEE_ABLE);
3714 supported = mmd_eee_cap_to_ethtool_sup_t(val);
3715
3716 val = ocp_reg_read(tp, OCP_EEE_ADV);
3717 adv = mmd_eee_adv_to_ethtool_adv_t(val);
3718
3719 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
3720 lp = mmd_eee_adv_to_ethtool_adv_t(val);
3721
3722 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3723 ocp_data &= EEE_RX_EN | EEE_TX_EN;
3724
3725 eee->eee_enabled = !!ocp_data;
3726 eee->eee_active = !!(supported & adv & lp);
3727 eee->supported = supported;
3728 eee->advertised = adv;
3729 eee->lp_advertised = lp;
3730
3731 return 0;
3732}
3733
3734static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
3735{
3736 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
3737
3738 r8153_eee_en(tp, eee->eee_enabled);
3739
3740 if (!eee->eee_enabled)
3741 val = 0;
3742
3743 ocp_reg_write(tp, OCP_EEE_ADV, val);
3744
3745 return 0;
3746}
3747
3748static int
3749rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
3750{
3751 struct r8152 *tp = netdev_priv(net);
3752 int ret;
3753
3754 ret = usb_autopm_get_interface(tp->intf);
3755 if (ret < 0)
3756 goto out;
3757
hayeswangb5403272014-10-09 18:00:26 +08003758 mutex_lock(&tp->control);
3759
hayeswangdf35d282014-09-25 20:54:02 +08003760 ret = tp->rtl_ops.eee_get(tp, edata);
3761
hayeswangb5403272014-10-09 18:00:26 +08003762 mutex_unlock(&tp->control);
3763
hayeswangdf35d282014-09-25 20:54:02 +08003764 usb_autopm_put_interface(tp->intf);
3765
3766out:
3767 return ret;
3768}
3769
3770static int
3771rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
3772{
3773 struct r8152 *tp = netdev_priv(net);
3774 int ret;
3775
3776 ret = usb_autopm_get_interface(tp->intf);
3777 if (ret < 0)
3778 goto out;
3779
hayeswangb5403272014-10-09 18:00:26 +08003780 mutex_lock(&tp->control);
3781
hayeswangdf35d282014-09-25 20:54:02 +08003782 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08003783 if (!ret)
3784 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08003785
hayeswangb5403272014-10-09 18:00:26 +08003786 mutex_unlock(&tp->control);
3787
hayeswangdf35d282014-09-25 20:54:02 +08003788 usb_autopm_put_interface(tp->intf);
3789
3790out:
3791 return ret;
3792}
3793
hayeswang8884f502014-10-28 14:05:52 +08003794static int rtl8152_nway_reset(struct net_device *dev)
3795{
3796 struct r8152 *tp = netdev_priv(dev);
3797 int ret;
3798
3799 ret = usb_autopm_get_interface(tp->intf);
3800 if (ret < 0)
3801 goto out;
3802
3803 mutex_lock(&tp->control);
3804
3805 ret = mii_nway_restart(&tp->mii);
3806
3807 mutex_unlock(&tp->control);
3808
3809 usb_autopm_put_interface(tp->intf);
3810
3811out:
3812 return ret;
3813}
3814
hayeswangefb3dd82015-02-12 14:33:48 +08003815static int rtl8152_get_coalesce(struct net_device *netdev,
3816 struct ethtool_coalesce *coalesce)
3817{
3818 struct r8152 *tp = netdev_priv(netdev);
3819
3820 switch (tp->version) {
3821 case RTL_VER_01:
3822 case RTL_VER_02:
3823 return -EOPNOTSUPP;
3824 default:
3825 break;
3826 }
3827
3828 coalesce->rx_coalesce_usecs = tp->coalesce;
3829
3830 return 0;
3831}
3832
3833static int rtl8152_set_coalesce(struct net_device *netdev,
3834 struct ethtool_coalesce *coalesce)
3835{
3836 struct r8152 *tp = netdev_priv(netdev);
3837 int ret;
3838
3839 switch (tp->version) {
3840 case RTL_VER_01:
3841 case RTL_VER_02:
3842 return -EOPNOTSUPP;
3843 default:
3844 break;
3845 }
3846
3847 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
3848 return -EINVAL;
3849
3850 ret = usb_autopm_get_interface(tp->intf);
3851 if (ret < 0)
3852 return ret;
3853
3854 mutex_lock(&tp->control);
3855
3856 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
3857 tp->coalesce = coalesce->rx_coalesce_usecs;
3858
3859 if (netif_running(tp->netdev) && netif_carrier_ok(netdev))
3860 r8153_set_rx_early_timeout(tp);
3861 }
3862
3863 mutex_unlock(&tp->control);
3864
3865 usb_autopm_put_interface(tp->intf);
3866
3867 return ret;
3868}
3869
hayeswangac718b62013-05-02 16:01:25 +00003870static struct ethtool_ops ops = {
3871 .get_drvinfo = rtl8152_get_drvinfo,
3872 .get_settings = rtl8152_get_settings,
3873 .set_settings = rtl8152_set_settings,
3874 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08003875 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08003876 .get_msglevel = rtl8152_get_msglevel,
3877 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08003878 .get_wol = rtl8152_get_wol,
3879 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08003880 .get_strings = rtl8152_get_strings,
3881 .get_sset_count = rtl8152_get_sset_count,
3882 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08003883 .get_coalesce = rtl8152_get_coalesce,
3884 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08003885 .get_eee = rtl_ethtool_get_eee,
3886 .set_eee = rtl_ethtool_set_eee,
hayeswangac718b62013-05-02 16:01:25 +00003887};
3888
3889static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
3890{
3891 struct r8152 *tp = netdev_priv(netdev);
3892 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08003893 int res;
3894
hayeswang68714382014-04-11 17:54:31 +08003895 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3896 return -ENODEV;
3897
hayeswang9a4be1b2014-02-18 21:49:07 +08003898 res = usb_autopm_get_interface(tp->intf);
3899 if (res < 0)
3900 goto out;
hayeswangac718b62013-05-02 16:01:25 +00003901
3902 switch (cmd) {
3903 case SIOCGMIIPHY:
3904 data->phy_id = R8152_PHY_ID; /* Internal PHY */
3905 break;
3906
3907 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08003908 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003909 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08003910 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003911 break;
3912
3913 case SIOCSMIIREG:
3914 if (!capable(CAP_NET_ADMIN)) {
3915 res = -EPERM;
3916 break;
3917 }
hayeswangb5403272014-10-09 18:00:26 +08003918 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003919 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08003920 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00003921 break;
3922
3923 default:
3924 res = -EOPNOTSUPP;
3925 }
3926
hayeswang9a4be1b2014-02-18 21:49:07 +08003927 usb_autopm_put_interface(tp->intf);
3928
3929out:
hayeswangac718b62013-05-02 16:01:25 +00003930 return res;
3931}
3932
hayeswang69b4b7a2014-07-10 10:58:54 +08003933static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
3934{
3935 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08003936 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08003937
3938 switch (tp->version) {
3939 case RTL_VER_01:
3940 case RTL_VER_02:
3941 return eth_change_mtu(dev, new_mtu);
3942 default:
3943 break;
3944 }
3945
3946 if (new_mtu < 68 || new_mtu > RTL8153_MAX_MTU)
3947 return -EINVAL;
3948
hayeswang396e2e22015-02-12 14:33:47 +08003949 ret = usb_autopm_get_interface(tp->intf);
3950 if (ret < 0)
3951 return ret;
3952
3953 mutex_lock(&tp->control);
3954
hayeswang69b4b7a2014-07-10 10:58:54 +08003955 dev->mtu = new_mtu;
3956
hayeswang396e2e22015-02-12 14:33:47 +08003957 if (netif_running(dev) && netif_carrier_ok(dev))
3958 r8153_set_rx_early_size(tp);
3959
3960 mutex_unlock(&tp->control);
3961
3962 usb_autopm_put_interface(tp->intf);
3963
3964 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08003965}
3966
hayeswangac718b62013-05-02 16:01:25 +00003967static const struct net_device_ops rtl8152_netdev_ops = {
3968 .ndo_open = rtl8152_open,
3969 .ndo_stop = rtl8152_close,
3970 .ndo_do_ioctl = rtl8152_ioctl,
3971 .ndo_start_xmit = rtl8152_start_xmit,
3972 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08003973 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00003974 .ndo_set_rx_mode = rtl8152_set_rx_mode,
3975 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08003976 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00003977 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08003978 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00003979};
3980
3981static void r8152b_get_version(struct r8152 *tp)
3982{
3983 u32 ocp_data;
3984 u16 version;
3985
3986 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3987 version = (u16)(ocp_data & VERSION_MASK);
3988
3989 switch (version) {
3990 case 0x4c00:
3991 tp->version = RTL_VER_01;
3992 break;
3993 case 0x4c10:
3994 tp->version = RTL_VER_02;
3995 break;
hayeswang43779f82014-01-02 11:25:10 +08003996 case 0x5c00:
3997 tp->version = RTL_VER_03;
3998 tp->mii.supports_gmii = 1;
3999 break;
4000 case 0x5c10:
4001 tp->version = RTL_VER_04;
4002 tp->mii.supports_gmii = 1;
4003 break;
4004 case 0x5c20:
4005 tp->version = RTL_VER_05;
4006 tp->mii.supports_gmii = 1;
4007 break;
hayeswangfb02eb42015-07-22 15:27:41 +08004008 case 0x5c30:
4009 tp->version = RTL_VER_06;
4010 tp->mii.supports_gmii = 1;
4011 break;
hayeswangac718b62013-05-02 16:01:25 +00004012 default:
4013 netif_info(tp, probe, tp->netdev,
4014 "Unknown version 0x%04x\n", version);
4015 break;
4016 }
4017}
4018
hayeswange3fe0b12014-01-02 11:22:39 +08004019static void rtl8152_unload(struct r8152 *tp)
4020{
hayeswang68714382014-04-11 17:54:31 +08004021 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4022 return;
4023
hayeswang00a5e362014-02-18 21:48:59 +08004024 if (tp->version != RTL_VER_01)
4025 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08004026}
4027
hayeswang43779f82014-01-02 11:25:10 +08004028static void rtl8153_unload(struct r8152 *tp)
4029{
hayeswang68714382014-04-11 17:54:31 +08004030 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4031 return;
4032
hayeswang49be1722014-10-01 13:25:11 +08004033 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004034}
4035
hayeswang55b65472014-11-06 12:47:39 +08004036static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08004037{
4038 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08004039 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08004040
hayeswang55b65472014-11-06 12:47:39 +08004041 switch (tp->version) {
4042 case RTL_VER_01:
4043 case RTL_VER_02:
4044 ops->init = r8152b_init;
4045 ops->enable = rtl8152_enable;
4046 ops->disable = rtl8152_disable;
4047 ops->up = rtl8152_up;
4048 ops->down = rtl8152_down;
4049 ops->unload = rtl8152_unload;
4050 ops->eee_get = r8152_get_eee;
4051 ops->eee_set = r8152_set_eee;
hayeswang43779f82014-01-02 11:25:10 +08004052 break;
4053
hayeswang55b65472014-11-06 12:47:39 +08004054 case RTL_VER_03:
4055 case RTL_VER_04:
4056 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08004057 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08004058 ops->init = r8153_init;
4059 ops->enable = rtl8153_enable;
4060 ops->disable = rtl8153_disable;
4061 ops->up = rtl8153_up;
4062 ops->down = rtl8153_down;
4063 ops->unload = rtl8153_unload;
4064 ops->eee_get = r8153_get_eee;
4065 ops->eee_set = r8153_set_eee;
hayeswangc81229c2014-01-02 11:22:42 +08004066 break;
4067
4068 default:
hayeswang55b65472014-11-06 12:47:39 +08004069 ret = -ENODEV;
4070 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08004071 break;
4072 }
4073
4074 return ret;
4075}
4076
hayeswangac718b62013-05-02 16:01:25 +00004077static int rtl8152_probe(struct usb_interface *intf,
4078 const struct usb_device_id *id)
4079{
4080 struct usb_device *udev = interface_to_usbdev(intf);
4081 struct r8152 *tp;
4082 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08004083 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004084
hayeswang10c32712014-03-04 20:47:48 +08004085 if (udev->actconfig->desc.bConfigurationValue != 1) {
4086 usb_driver_set_configuration(udev, 1);
4087 return -ENODEV;
4088 }
4089
4090 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00004091 netdev = alloc_etherdev(sizeof(struct r8152));
4092 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08004093 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00004094 return -ENOMEM;
4095 }
4096
hayeswangebc2ec482013-08-14 20:54:38 +08004097 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00004098 tp = netdev_priv(netdev);
4099 tp->msg_enable = 0x7FFF;
4100
hayeswange3ad4122014-01-06 17:08:42 +08004101 tp->udev = udev;
4102 tp->netdev = netdev;
4103 tp->intf = intf;
4104
hayeswang82cf94c2014-11-06 12:47:38 +08004105 r8152b_get_version(tp);
hayeswang55b65472014-11-06 12:47:39 +08004106 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08004107 if (ret)
4108 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08004109
hayeswangb5403272014-10-09 18:00:26 +08004110 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004111 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
4112
hayeswangac718b62013-05-02 16:01:25 +00004113 netdev->netdev_ops = &rtl8152_netdev_ops;
4114 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08004115
hayeswang60c89072014-03-07 11:04:39 +08004116 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08004117 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08004118 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
4119 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08004120 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08004121 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08004122 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39faf2015-02-06 11:30:49 +08004123 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08004124 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4125 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
4126 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08004127
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004128 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08004129 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00004130
4131 tp->mii.dev = netdev;
4132 tp->mii.mdio_read = read_mii_word;
4133 tp->mii.mdio_write = write_mii_word;
4134 tp->mii.phy_id_mask = 0x3f;
4135 tp->mii.reg_num_mask = 0x1f;
4136 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00004137
hayeswang464ec102015-02-12 14:33:46 +08004138 switch (udev->speed) {
4139 case USB_SPEED_SUPER:
4140 tp->coalesce = COALESCE_SUPER;
4141 break;
4142 case USB_SPEED_HIGH:
4143 tp->coalesce = COALESCE_HIGH;
4144 break;
4145 default:
4146 tp->coalesce = COALESCE_SLOW;
4147 break;
4148 }
4149
hayeswang9a4be1b2014-02-18 21:49:07 +08004150 intf->needs_remote_wakeup = 1;
4151
hayeswangc81229c2014-01-02 11:22:42 +08004152 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00004153 set_ethernet_addr(tp);
4154
hayeswangac718b62013-05-02 16:01:25 +00004155 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08004156 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00004157
hayeswangebc2ec482013-08-14 20:54:38 +08004158 ret = register_netdev(netdev);
4159 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08004160 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08004161 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00004162 }
4163
hayeswang7daed8d2015-07-24 13:54:24 +08004164 if (!rtl_can_wakeup(tp))
4165 __rtl_set_wol(tp, 0);
4166
hayeswang21ff2e82014-02-18 21:49:06 +08004167 tp->saved_wolopts = __rtl_get_wol(tp);
4168 if (tp->saved_wolopts)
4169 device_set_wakeup_enable(&udev->dev, true);
4170 else
4171 device_set_wakeup_enable(&udev->dev, false);
4172
Hayes Wang4a8deae2014-01-07 11:18:22 +08004173 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00004174
4175 return 0;
4176
hayeswangac718b62013-05-02 16:01:25 +00004177out1:
hayeswangd823ab62015-01-12 12:06:23 +08004178 netif_napi_del(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08004179 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00004180out:
4181 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08004182 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004183}
4184
hayeswangac718b62013-05-02 16:01:25 +00004185static void rtl8152_disconnect(struct usb_interface *intf)
4186{
4187 struct r8152 *tp = usb_get_intfdata(intf);
4188
4189 usb_set_intfdata(intf, NULL);
4190 if (tp) {
hayeswangf561de32014-09-30 16:48:01 +08004191 struct usb_device *udev = tp->udev;
4192
4193 if (udev->state == USB_STATE_NOTATTACHED)
4194 set_bit(RTL8152_UNPLUG, &tp->flags);
4195
hayeswangd823ab62015-01-12 12:06:23 +08004196 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004197 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08004198 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00004199 free_netdev(tp->netdev);
4200 }
4201}
4202
hayeswangd9a28c52014-12-04 10:43:11 +08004203#define REALTEK_USB_DEVICE(vend, prod) \
4204 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
4205 USB_DEVICE_ID_MATCH_INT_CLASS, \
4206 .idVendor = (vend), \
4207 .idProduct = (prod), \
4208 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
4209}, \
4210{ \
4211 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
4212 USB_DEVICE_ID_MATCH_DEVICE, \
4213 .idVendor = (vend), \
4214 .idProduct = (prod), \
4215 .bInterfaceClass = USB_CLASS_COMM, \
4216 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
4217 .bInterfaceProtocol = USB_CDC_PROTO_NONE
4218
hayeswangac718b62013-05-02 16:01:25 +00004219/* table of devices that work with this driver */
4220static struct usb_device_id rtl8152_table[] = {
hayeswangd9a28c52014-12-04 10:43:11 +08004221 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
4222 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
4223 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Christian Hesse347eec32015-03-31 14:10:07 +02004224 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04004225 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
Zheng Liud065c3c12015-07-07 13:54:12 -07004226 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
hayeswangac718b62013-05-02 16:01:25 +00004227 {}
4228};
4229
4230MODULE_DEVICE_TABLE(usb, rtl8152_table);
4231
4232static struct usb_driver rtl8152_driver = {
4233 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08004234 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00004235 .probe = rtl8152_probe,
4236 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00004237 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08004238 .resume = rtl8152_resume,
4239 .reset_resume = rtl8152_resume,
hayeswange5011392015-07-29 20:39:08 +08004240 .pre_reset = rtl8152_pre_reset,
4241 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08004242 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08004243 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00004244};
4245
Sachin Kamatb4236daa2013-05-16 17:48:08 +00004246module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00004247
4248MODULE_AUTHOR(DRIVER_AUTHOR);
4249MODULE_DESCRIPTION(DRIVER_DESC);
4250MODULE_LICENSE("GPL");