blob: 8bb8782c05de4fd17eb576440d11e6c3a6a1e3bb [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
hayeswangc7de7de2014-01-15 10:42:16 +08002 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
7 *
8 */
9
hayeswangac718b62013-05-02 16:01:25 +000010#include <linux/signal.h>
11#include <linux/slab.h>
12#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000013#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/mii.h>
16#include <linux/ethtool.h>
17#include <linux/usb.h>
18#include <linux/crc32.h>
19#include <linux/if_vlan.h>
20#include <linux/uaccess.h>
hayeswangebc2ec42013-08-14 20:54:38 +080021#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080022#include <linux/ip.h>
23#include <linux/ipv6.h>
hayeswangac718b62013-05-02 16:01:25 +000024
25/* Version Information */
hayeswang21ff2e82014-02-18 21:49:06 +080026#define DRIVER_VERSION "v1.05.0 (2014/02/18)"
hayeswangac718b62013-05-02 16:01:25 +000027#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080028#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000029#define MODULENAME "r8152"
30
31#define R8152_PHY_ID 32
32
33#define PLA_IDR 0xc000
34#define PLA_RCR 0xc010
35#define PLA_RMS 0xc016
36#define PLA_RXFIFO_CTRL0 0xc0a0
37#define PLA_RXFIFO_CTRL1 0xc0a4
38#define PLA_RXFIFO_CTRL2 0xc0a8
39#define PLA_FMC 0xc0b4
40#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080041#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000042#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080043#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000044#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080045#define PLA_TEREDO_TIMER 0xd2cc
46#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000047#define PLA_LEDSEL 0xdd90
48#define PLA_LED_FEATURE 0xdd92
49#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080050#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000051#define PLA_GPHY_INTR_IMR 0xe022
52#define PLA_EEE_CR 0xe040
53#define PLA_EEEP_CR 0xe080
54#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_MAC_PWR_CTRL2 0xe0ca
56#define PLA_MAC_PWR_CTRL3 0xe0cc
57#define PLA_MAC_PWR_CTRL4 0xe0ce
58#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000059#define PLA_TCR0 0xe610
60#define PLA_TCR1 0xe612
61#define PLA_TXFIFO_CTRL 0xe618
62#define PLA_RSTTELLY 0xe800
63#define PLA_CR 0xe813
64#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080065#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
66#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000067#define PLA_CONFIG5 0xe822
68#define PLA_PHY_PWR 0xe84c
69#define PLA_OOB_CTRL 0xe84f
70#define PLA_CPCR 0xe854
71#define PLA_MISC_0 0xe858
72#define PLA_MISC_1 0xe85a
73#define PLA_OCP_GPHY_BASE 0xe86c
74#define PLA_TELLYCNT 0xe890
75#define PLA_SFF_STS_7 0xe8de
76#define PLA_PHYSTATUS 0xe908
77#define PLA_BP_BA 0xfc26
78#define PLA_BP_0 0xfc28
79#define PLA_BP_1 0xfc2a
80#define PLA_BP_2 0xfc2c
81#define PLA_BP_3 0xfc2e
82#define PLA_BP_4 0xfc30
83#define PLA_BP_5 0xfc32
84#define PLA_BP_6 0xfc34
85#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080086#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000087
hayeswang43779f82014-01-02 11:25:10 +080088#define USB_U2P3_CTRL 0xb460
hayeswangac718b62013-05-02 16:01:25 +000089#define USB_DEV_STAT 0xb808
90#define USB_USB_CTRL 0xd406
91#define USB_PHY_CTRL 0xd408
92#define USB_TX_AGG 0xd40a
93#define USB_RX_BUF_TH 0xd40c
94#define USB_USB_TIMER 0xd428
hayeswang43779f82014-01-02 11:25:10 +080095#define USB_RX_EARLY_AGG 0xd42c
hayeswangac718b62013-05-02 16:01:25 +000096#define USB_PM_CTRL_STATUS 0xd432
97#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +080098#define USB_TOLERANCE 0xd490
99#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +0000100#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800101#define USB_MISC_0 0xd81a
102#define USB_POWER_CUT 0xd80a
103#define USB_AFE_CTRL2 0xd824
104#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000105#define USB_BP_BA 0xfc26
106#define USB_BP_0 0xfc28
107#define USB_BP_1 0xfc2a
108#define USB_BP_2 0xfc2c
109#define USB_BP_3 0xfc2e
110#define USB_BP_4 0xfc30
111#define USB_BP_5 0xfc32
112#define USB_BP_6 0xfc34
113#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800114#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000115
116/* OCP Registers */
117#define OCP_ALDPS_CONFIG 0x2010
118#define OCP_EEE_CONFIG1 0x2080
119#define OCP_EEE_CONFIG2 0x2092
120#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800121#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000122#define OCP_EEE_AR 0xa41a
123#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800124#define OCP_PHY_STATUS 0xa420
125#define OCP_POWER_CFG 0xa430
126#define OCP_EEE_CFG 0xa432
127#define OCP_SRAM_ADDR 0xa436
128#define OCP_SRAM_DATA 0xa438
129#define OCP_DOWN_SPEED 0xa442
130#define OCP_EEE_CFG2 0xa5d0
131#define OCP_ADC_CFG 0xbc06
132
133/* SRAM Register */
134#define SRAM_LPF_CFG 0x8012
135#define SRAM_10M_AMP1 0x8080
136#define SRAM_10M_AMP2 0x8082
137#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000138
139/* PLA_RCR */
140#define RCR_AAP 0x00000001
141#define RCR_APM 0x00000002
142#define RCR_AM 0x00000004
143#define RCR_AB 0x00000008
144#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
145
146/* PLA_RXFIFO_CTRL0 */
147#define RXFIFO_THR1_NORMAL 0x00080002
148#define RXFIFO_THR1_OOB 0x01800003
149
150/* PLA_RXFIFO_CTRL1 */
151#define RXFIFO_THR2_FULL 0x00000060
152#define RXFIFO_THR2_HIGH 0x00000038
153#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800154#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000155
156/* PLA_RXFIFO_CTRL2 */
157#define RXFIFO_THR3_FULL 0x00000078
158#define RXFIFO_THR3_HIGH 0x00000048
159#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800160#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000161
162/* PLA_TXFIFO_CTRL */
163#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800164#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000165
166/* PLA_FMC */
167#define FMC_FCR_MCU_EN 0x0001
168
169/* PLA_EEEP_CR */
170#define EEEP_CR_EEEP_TX 0x0002
171
hayeswang43779f82014-01-02 11:25:10 +0800172/* PLA_WDT6_CTRL */
173#define WDT6_SET_MODE 0x0010
174
hayeswangac718b62013-05-02 16:01:25 +0000175/* PLA_TCR0 */
176#define TCR0_TX_EMPTY 0x0800
177#define TCR0_AUTO_FIFO 0x0080
178
179/* PLA_TCR1 */
180#define VERSION_MASK 0x7cf0
181
182/* PLA_CR */
183#define CR_RST 0x10
184#define CR_RE 0x08
185#define CR_TE 0x04
186
187/* PLA_CRWECR */
188#define CRWECR_NORAML 0x00
189#define CRWECR_CONFIG 0xc0
190
191/* PLA_OOB_CTRL */
192#define NOW_IS_OOB 0x80
193#define TXFIFO_EMPTY 0x20
194#define RXFIFO_EMPTY 0x10
195#define LINK_LIST_READY 0x02
196#define DIS_MCU_CLROOB 0x01
197#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
198
199/* PLA_MISC_1 */
200#define RXDY_GATED_EN 0x0008
201
202/* PLA_SFF_STS_7 */
203#define RE_INIT_LL 0x8000
204#define MCU_BORW_EN 0x4000
205
206/* PLA_CPCR */
207#define CPCR_RX_VLAN 0x0040
208
209/* PLA_CFG_WOL */
210#define MAGIC_EN 0x0001
211
hayeswang43779f82014-01-02 11:25:10 +0800212/* PLA_TEREDO_CFG */
213#define TEREDO_SEL 0x8000
214#define TEREDO_WAKE_MASK 0x7f00
215#define TEREDO_RS_EVENT_MASK 0x00fe
216#define OOB_TEREDO_EN 0x0001
217
hayeswangac718b62013-05-02 16:01:25 +0000218/* PAL_BDC_CR */
219#define ALDPS_PROXY_MODE 0x0001
220
hayeswang21ff2e82014-02-18 21:49:06 +0800221/* PLA_CONFIG34 */
222#define LINK_ON_WAKE_EN 0x0010
223#define LINK_OFF_WAKE_EN 0x0008
224
hayeswangac718b62013-05-02 16:01:25 +0000225/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800226#define BWF_EN 0x0040
227#define MWF_EN 0x0020
228#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000229#define LAN_WAKE_EN 0x0002
230
231/* PLA_LED_FEATURE */
232#define LED_MODE_MASK 0x0700
233
234/* PLA_PHY_PWR */
235#define TX_10M_IDLE_EN 0x0080
236#define PFM_PWM_SWITCH 0x0040
237
238/* PLA_MAC_PWR_CTRL */
239#define D3_CLK_GATED_EN 0x00004000
240#define MCU_CLK_RATIO 0x07010f07
241#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800242#define ALDPS_SPDWN_RATIO 0x0f87
243
244/* PLA_MAC_PWR_CTRL2 */
245#define EEE_SPDWN_RATIO 0x8007
246
247/* PLA_MAC_PWR_CTRL3 */
248#define PKT_AVAIL_SPDWN_EN 0x0100
249#define SUSPEND_SPDWN_EN 0x0004
250#define U1U2_SPDWN_EN 0x0002
251#define L1_SPDWN_EN 0x0001
252
253/* PLA_MAC_PWR_CTRL4 */
254#define PWRSAVE_SPDWN_EN 0x1000
255#define RXDV_SPDWN_EN 0x0800
256#define TX10MIDLE_EN 0x0100
257#define TP100_SPDWN_EN 0x0020
258#define TP500_SPDWN_EN 0x0010
259#define TP1000_SPDWN_EN 0x0008
260#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000261
262/* PLA_GPHY_INTR_IMR */
263#define GPHY_STS_MSK 0x0001
264#define SPEED_DOWN_MSK 0x0002
265#define SPDWN_RXDV_MSK 0x0004
266#define SPDWN_LINKCHG_MSK 0x0008
267
268/* PLA_PHYAR */
269#define PHYAR_FLAG 0x80000000
270
271/* PLA_EEE_CR */
272#define EEE_RX_EN 0x0001
273#define EEE_TX_EN 0x0002
274
hayeswang43779f82014-01-02 11:25:10 +0800275/* PLA_BOOT_CTRL */
276#define AUTOLOAD_DONE 0x0002
277
hayeswangac718b62013-05-02 16:01:25 +0000278/* USB_DEV_STAT */
279#define STAT_SPEED_MASK 0x0006
280#define STAT_SPEED_HIGH 0x0000
281#define STAT_SPEED_FULL 0x0001
282
283/* USB_TX_AGG */
284#define TX_AGG_MAX_THRESHOLD 0x03
285
286/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800287#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800288#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800289#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000290
291/* USB_TX_DMA */
292#define TEST_MODE_DISABLE 0x00000001
293#define TX_SIZE_ADJUST1 0x00000100
294
295/* USB_UPS_CTRL */
296#define POWER_CUT 0x0100
297
298/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800299#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000300
301/* USB_USB_CTRL */
302#define RX_AGG_DISABLE 0x0010
303
hayeswang43779f82014-01-02 11:25:10 +0800304/* USB_U2P3_CTRL */
305#define U2P3_ENABLE 0x0001
306
307/* USB_POWER_CUT */
308#define PWR_EN 0x0001
309#define PHASE2_EN 0x0008
310
311/* USB_MISC_0 */
312#define PCUT_STATUS 0x0001
313
314/* USB_RX_EARLY_AGG */
315#define EARLY_AGG_SUPPER 0x0e832981
316#define EARLY_AGG_HIGH 0x0e837a12
317#define EARLY_AGG_SLOW 0x0e83ffff
318
319/* USB_WDT11_CTRL */
320#define TIMER11_EN 0x0001
321
322/* USB_LPM_CTRL */
323#define LPM_TIMER_MASK 0x0c
324#define LPM_TIMER_500MS 0x04 /* 500 ms */
325#define LPM_TIMER_500US 0x0c /* 500 us */
326
327/* USB_AFE_CTRL2 */
328#define SEN_VAL_MASK 0xf800
329#define SEN_VAL_NORMAL 0xa000
330#define SEL_RXIDLE 0x0100
331
hayeswangac718b62013-05-02 16:01:25 +0000332/* OCP_ALDPS_CONFIG */
333#define ENPWRSAVE 0x8000
334#define ENPDNPS 0x0200
335#define LINKENA 0x0100
336#define DIS_SDSAVE 0x0010
337
hayeswang43779f82014-01-02 11:25:10 +0800338/* OCP_PHY_STATUS */
339#define PHY_STAT_MASK 0x0007
340#define PHY_STAT_LAN_ON 3
341#define PHY_STAT_PWRDN 5
342
343/* OCP_POWER_CFG */
344#define EEE_CLKDIV_EN 0x8000
345#define EN_ALDPS 0x0004
346#define EN_10M_PLLOFF 0x0001
347
hayeswangac718b62013-05-02 16:01:25 +0000348/* OCP_EEE_CONFIG1 */
349#define RG_TXLPI_MSK_HFDUP 0x8000
350#define RG_MATCLR_EN 0x4000
351#define EEE_10_CAP 0x2000
352#define EEE_NWAY_EN 0x1000
353#define TX_QUIET_EN 0x0200
354#define RX_QUIET_EN 0x0100
355#define SDRISETIME 0x0010 /* bit 4 ~ 6 */
356#define RG_RXLPI_MSK_HFDUP 0x0008
357#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
358
359/* OCP_EEE_CONFIG2 */
360#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
361#define RG_DACQUIET_EN 0x0400
362#define RG_LDVQUIET_EN 0x0200
363#define RG_CKRSEL 0x0020
364#define RG_EEEPRG_EN 0x0010
365
366/* OCP_EEE_CONFIG3 */
367#define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
368#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
369#define MSK_PH 0x0006 /* bit 0 ~ 3 */
370
371/* OCP_EEE_AR */
372/* bit[15:14] function */
373#define FUN_ADDR 0x0000
374#define FUN_DATA 0x4000
375/* bit[4:0] device addr */
376#define DEVICE_ADDR 0x0007
377
378/* OCP_EEE_DATA */
379#define EEE_ADDR 0x003C
380#define EEE_DATA 0x0002
381
hayeswang43779f82014-01-02 11:25:10 +0800382/* OCP_EEE_CFG */
383#define CTAP_SHORT_EN 0x0040
384#define EEE10_EN 0x0010
385
386/* OCP_DOWN_SPEED */
387#define EN_10M_BGOFF 0x0080
388
389/* OCP_EEE_CFG2 */
390#define MY1000_EEE 0x0004
391#define MY100_EEE 0x0002
392
393/* OCP_ADC_CFG */
394#define CKADSEL_L 0x0100
395#define ADC_EN 0x0080
396#define EN_EMI_L 0x0040
397
398/* SRAM_LPF_CFG */
399#define LPF_AUTO_TUNE 0x8000
400
401/* SRAM_10M_AMP1 */
402#define GDAC_IB_UPALL 0x0008
403
404/* SRAM_10M_AMP2 */
405#define AMP_DN 0x0200
406
407/* SRAM_IMPEDANCE */
408#define RX_DRIVING_MASK 0x6000
409
hayeswangac718b62013-05-02 16:01:25 +0000410enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800411 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000412 _100bps = 0x08,
413 _10bps = 0x04,
414 LINK_STATUS = 0x02,
415 FULL_DUP = 0x01,
416};
417
hayeswangebc2ec42013-08-14 20:54:38 +0800418#define RTL8152_MAX_TX 10
419#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800420#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800421#define CRC_SIZE 4
422#define TX_ALIGN 4
423#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800424
425#define INTR_LINK 0x0004
hayeswangebc2ec42013-08-14 20:54:38 +0800426
hayeswangac718b62013-05-02 16:01:25 +0000427#define RTL8152_REQT_READ 0xc0
428#define RTL8152_REQT_WRITE 0x40
429#define RTL8152_REQ_GET_REGS 0x05
430#define RTL8152_REQ_SET_REGS 0x05
431
432#define BYTE_EN_DWORD 0xff
433#define BYTE_EN_WORD 0x33
434#define BYTE_EN_BYTE 0x11
435#define BYTE_EN_SIX_BYTES 0x3f
436#define BYTE_EN_START_MASK 0x0f
437#define BYTE_EN_END_MASK 0xf0
438
439#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
440#define RTL8152_TX_TIMEOUT (HZ)
441
442/* rtl8152 flags */
443enum rtl8152_flags {
444 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000445 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800446 WORK_ENABLE,
447 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800448 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800449 PHY_RESET,
hayeswang0c3121f2014-03-07 11:04:36 +0800450 SCHEDULE_TASKLET,
hayeswangac718b62013-05-02 16:01:25 +0000451};
452
453/* Define these values to match your device */
454#define VENDOR_ID_REALTEK 0x0bda
455#define PRODUCT_ID_RTL8152 0x8152
hayeswang43779f82014-01-02 11:25:10 +0800456#define PRODUCT_ID_RTL8153 0x8153
457
458#define VENDOR_ID_SAMSUNG 0x04e8
459#define PRODUCT_ID_SAMSUNG 0xa101
hayeswangac718b62013-05-02 16:01:25 +0000460
461#define MCU_TYPE_PLA 0x0100
462#define MCU_TYPE_USB 0x0000
463
hayeswangc7de7de2014-01-15 10:42:16 +0800464#define REALTEK_USB_DEVICE(vend, prod) \
465 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)
466
hayeswangac718b62013-05-02 16:01:25 +0000467struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800468 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000469#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800470
hayeswang500b6d72013-11-20 17:30:57 +0800471 __le32 opts2;
hayeswang565cab02014-03-07 11:04:38 +0800472#define RD_UDP_CS (1 << 23)
473#define RD_TCP_CS (1 << 22)
474#define RD_IPV4_CS (1 << 19)
475
hayeswang500b6d72013-11-20 17:30:57 +0800476 __le32 opts3;
hayeswang565cab02014-03-07 11:04:38 +0800477#define IPF (1 << 23) /* IP checksum fail */
478#define UDPF (1 << 22) /* UDP checksum fail */
479#define TCPF (1 << 21) /* TCP checksum fail */
480
hayeswang500b6d72013-11-20 17:30:57 +0800481 __le32 opts4;
482 __le32 opts5;
483 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000484};
485
486struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800487 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000488#define TX_FS (1 << 31) /* First segment of a packet */
489#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang5bd23882013-08-14 20:54:39 +0800490#define TX_LEN_MASK 0x3ffff
491
hayeswang500b6d72013-11-20 17:30:57 +0800492 __le32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800493#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
494#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
495#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
496#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswangac718b62013-05-02 16:01:25 +0000497};
498
hayeswangdff4e8a2013-08-16 16:09:33 +0800499struct r8152;
500
hayeswangebc2ec42013-08-14 20:54:38 +0800501struct rx_agg {
502 struct list_head list;
503 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800504 struct r8152 *context;
hayeswangebc2ec42013-08-14 20:54:38 +0800505 void *buffer;
506 void *head;
507};
508
509struct tx_agg {
510 struct list_head list;
511 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800512 struct r8152 *context;
hayeswangebc2ec42013-08-14 20:54:38 +0800513 void *buffer;
514 void *head;
515 u32 skb_num;
516 u32 skb_len;
517};
518
hayeswangac718b62013-05-02 16:01:25 +0000519struct r8152 {
520 unsigned long flags;
521 struct usb_device *udev;
522 struct tasklet_struct tl;
hayeswang40a82912013-08-14 20:54:40 +0800523 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000524 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800525 struct urb *intr_urb;
hayeswangebc2ec42013-08-14 20:54:38 +0800526 struct tx_agg tx_info[RTL8152_MAX_TX];
527 struct rx_agg rx_info[RTL8152_MAX_RX];
528 struct list_head rx_done, tx_free;
529 struct sk_buff_head tx_queue;
530 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000531 struct delayed_work schedule;
532 struct mii_if_info mii;
hayeswangc81229c2014-01-02 11:22:42 +0800533
534 struct rtl_ops {
535 void (*init)(struct r8152 *);
536 int (*enable)(struct r8152 *);
537 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800538 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800539 void (*down)(struct r8152 *);
540 void (*unload)(struct r8152 *);
541 } rtl_ops;
542
hayeswang40a82912013-08-14 20:54:40 +0800543 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800544 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000545 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800546 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000547 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800548 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000549 u8 version;
550 u8 speed;
551};
552
553enum rtl_version {
554 RTL_VER_UNKNOWN = 0,
555 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800556 RTL_VER_02,
557 RTL_VER_03,
558 RTL_VER_04,
559 RTL_VER_05,
560 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000561};
562
563/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
564 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
565 */
566static const int multicast_filter_limit = 32;
hayeswangebc2ec42013-08-14 20:54:38 +0800567static unsigned int rx_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000568
569static
570int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
571{
hayeswang31787f52013-07-31 17:21:25 +0800572 int ret;
573 void *tmp;
574
575 tmp = kmalloc(size, GFP_KERNEL);
576 if (!tmp)
577 return -ENOMEM;
578
579 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000580 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
hayeswang31787f52013-07-31 17:21:25 +0800581 value, index, tmp, size, 500);
582
583 memcpy(data, tmp, size);
584 kfree(tmp);
585
586 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000587}
588
589static
590int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
591{
hayeswang31787f52013-07-31 17:21:25 +0800592 int ret;
593 void *tmp;
594
595 tmp = kmalloc(size, GFP_KERNEL);
596 if (!tmp)
597 return -ENOMEM;
598
599 memcpy(tmp, data, size);
600
601 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000602 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
hayeswang31787f52013-07-31 17:21:25 +0800603 value, index, tmp, size, 500);
604
605 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800606
hayeswang31787f52013-07-31 17:21:25 +0800607 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000608}
609
610static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
611 void *data, u16 type)
612{
hayeswang45f4a192014-01-06 17:08:41 +0800613 u16 limit = 64;
614 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000615
616 if (test_bit(RTL8152_UNPLUG, &tp->flags))
617 return -ENODEV;
618
619 /* both size and indix must be 4 bytes align */
620 if ((size & 3) || !size || (index & 3) || !data)
621 return -EPERM;
622
623 if ((u32)index + (u32)size > 0xffff)
624 return -EPERM;
625
626 while (size) {
627 if (size > limit) {
628 ret = get_registers(tp, index, type, limit, data);
629 if (ret < 0)
630 break;
631
632 index += limit;
633 data += limit;
634 size -= limit;
635 } else {
636 ret = get_registers(tp, index, type, size, data);
637 if (ret < 0)
638 break;
639
640 index += size;
641 data += size;
642 size = 0;
643 break;
644 }
645 }
646
647 return ret;
648}
649
650static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
651 u16 size, void *data, u16 type)
652{
hayeswang45f4a192014-01-06 17:08:41 +0800653 int ret;
654 u16 byteen_start, byteen_end, byen;
655 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000656
657 if (test_bit(RTL8152_UNPLUG, &tp->flags))
658 return -ENODEV;
659
660 /* both size and indix must be 4 bytes align */
661 if ((size & 3) || !size || (index & 3) || !data)
662 return -EPERM;
663
664 if ((u32)index + (u32)size > 0xffff)
665 return -EPERM;
666
667 byteen_start = byteen & BYTE_EN_START_MASK;
668 byteen_end = byteen & BYTE_EN_END_MASK;
669
670 byen = byteen_start | (byteen_start << 4);
671 ret = set_registers(tp, index, type | byen, 4, data);
672 if (ret < 0)
673 goto error1;
674
675 index += 4;
676 data += 4;
677 size -= 4;
678
679 if (size) {
680 size -= 4;
681
682 while (size) {
683 if (size > limit) {
684 ret = set_registers(tp, index,
685 type | BYTE_EN_DWORD,
686 limit, data);
687 if (ret < 0)
688 goto error1;
689
690 index += limit;
691 data += limit;
692 size -= limit;
693 } else {
694 ret = set_registers(tp, index,
695 type | BYTE_EN_DWORD,
696 size, data);
697 if (ret < 0)
698 goto error1;
699
700 index += size;
701 data += size;
702 size = 0;
703 break;
704 }
705 }
706
707 byen = byteen_end | (byteen_end >> 4);
708 ret = set_registers(tp, index, type | byen, 4, data);
709 if (ret < 0)
710 goto error1;
711 }
712
713error1:
714 return ret;
715}
716
717static inline
718int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
719{
720 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
721}
722
723static inline
724int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
725{
726 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
727}
728
729static inline
730int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
731{
732 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
733}
734
735static inline
736int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
737{
738 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
739}
740
741static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
742{
hayeswangc8826de2013-07-31 17:21:26 +0800743 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000744
hayeswangc8826de2013-07-31 17:21:26 +0800745 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000746
747 return __le32_to_cpu(data);
748}
749
750static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
751{
hayeswangc8826de2013-07-31 17:21:26 +0800752 __le32 tmp = __cpu_to_le32(data);
753
754 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000755}
756
757static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
758{
759 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800760 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000761 u8 shift = index & 2;
762
763 index &= ~3;
764
hayeswangc8826de2013-07-31 17:21:26 +0800765 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000766
hayeswangc8826de2013-07-31 17:21:26 +0800767 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000768 data >>= (shift * 8);
769 data &= 0xffff;
770
771 return (u16)data;
772}
773
774static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
775{
hayeswangc8826de2013-07-31 17:21:26 +0800776 u32 mask = 0xffff;
777 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000778 u16 byen = BYTE_EN_WORD;
779 u8 shift = index & 2;
780
781 data &= mask;
782
783 if (index & 2) {
784 byen <<= shift;
785 mask <<= (shift * 8);
786 data <<= (shift * 8);
787 index &= ~3;
788 }
789
hayeswangc8826de2013-07-31 17:21:26 +0800790 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000791
hayeswangc8826de2013-07-31 17:21:26 +0800792 data |= __le32_to_cpu(tmp) & ~mask;
793 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000794
hayeswangc8826de2013-07-31 17:21:26 +0800795 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000796}
797
798static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
799{
800 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800801 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000802 u8 shift = index & 3;
803
804 index &= ~3;
805
hayeswangc8826de2013-07-31 17:21:26 +0800806 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000807
hayeswangc8826de2013-07-31 17:21:26 +0800808 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000809 data >>= (shift * 8);
810 data &= 0xff;
811
812 return (u8)data;
813}
814
815static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
816{
hayeswangc8826de2013-07-31 17:21:26 +0800817 u32 mask = 0xff;
818 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000819 u16 byen = BYTE_EN_BYTE;
820 u8 shift = index & 3;
821
822 data &= mask;
823
824 if (index & 3) {
825 byen <<= shift;
826 mask <<= (shift * 8);
827 data <<= (shift * 8);
828 index &= ~3;
829 }
830
hayeswangc8826de2013-07-31 17:21:26 +0800831 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000832
hayeswangc8826de2013-07-31 17:21:26 +0800833 data |= __le32_to_cpu(tmp) & ~mask;
834 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000835
hayeswangc8826de2013-07-31 17:21:26 +0800836 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000837}
838
hayeswangac244d32014-01-02 11:22:40 +0800839static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
840{
841 u16 ocp_base, ocp_index;
842
843 ocp_base = addr & 0xf000;
844 if (ocp_base != tp->ocp_base) {
845 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
846 tp->ocp_base = ocp_base;
847 }
848
849 ocp_index = (addr & 0x0fff) | 0xb000;
850 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
851}
852
hayeswange3fe0b12014-01-02 11:22:39 +0800853static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
854{
855 u16 ocp_base, ocp_index;
856
857 ocp_base = addr & 0xf000;
858 if (ocp_base != tp->ocp_base) {
859 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
860 tp->ocp_base = ocp_base;
861 }
862
863 ocp_index = (addr & 0x0fff) | 0xb000;
864 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
865}
866
hayeswangac244d32014-01-02 11:22:40 +0800867static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000868{
hayeswangac244d32014-01-02 11:22:40 +0800869 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000870}
871
hayeswangac244d32014-01-02 11:22:40 +0800872static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000873{
hayeswangac244d32014-01-02 11:22:40 +0800874 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000875}
876
hayeswang43779f82014-01-02 11:25:10 +0800877static void sram_write(struct r8152 *tp, u16 addr, u16 data)
878{
879 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
880 ocp_reg_write(tp, OCP_SRAM_DATA, data);
881}
882
883static u16 sram_read(struct r8152 *tp, u16 addr)
884{
885 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
886 return ocp_reg_read(tp, OCP_SRAM_DATA);
887}
888
hayeswangac718b62013-05-02 16:01:25 +0000889static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
890{
891 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +0800892 int ret;
hayeswangac718b62013-05-02 16:01:25 +0000893
894 if (phy_id != R8152_PHY_ID)
895 return -EINVAL;
896
hayeswang9a4be1b2014-02-18 21:49:07 +0800897 ret = usb_autopm_get_interface(tp->intf);
898 if (ret < 0)
899 goto out;
900
901 ret = r8152_mdio_read(tp, reg);
902
903 usb_autopm_put_interface(tp->intf);
904
905out:
906 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000907}
908
909static
910void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
911{
912 struct r8152 *tp = netdev_priv(netdev);
913
914 if (phy_id != R8152_PHY_ID)
915 return;
916
hayeswang9a4be1b2014-02-18 21:49:07 +0800917 if (usb_autopm_get_interface(tp->intf) < 0)
918 return;
919
hayeswangac718b62013-05-02 16:01:25 +0000920 r8152_mdio_write(tp, reg, val);
hayeswang9a4be1b2014-02-18 21:49:07 +0800921
922 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +0000923}
924
hayeswangebc2ec42013-08-14 20:54:38 +0800925static
926int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
927
hayeswangac718b62013-05-02 16:01:25 +0000928static inline void set_ethernet_addr(struct r8152 *tp)
929{
930 struct net_device *dev = tp->netdev;
hayeswang8a91c822014-02-18 21:49:01 +0800931 int ret;
hayeswang31787f52013-07-31 17:21:25 +0800932 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000933
hayeswang8a91c822014-02-18 21:49:01 +0800934 if (tp->version == RTL_VER_01)
935 ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id);
936 else
937 ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id);
938
939 if (ret < 0) {
hayeswangac718b62013-05-02 16:01:25 +0000940 netif_notice(tp, probe, dev, "inet addr fail\n");
hayeswang8a91c822014-02-18 21:49:01 +0800941 } else {
942 if (tp->version != RTL_VER_01) {
943 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
944 CRWECR_CONFIG);
945 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES,
946 sizeof(node_id), node_id);
947 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
948 CRWECR_NORAML);
949 }
950
hayeswangac718b62013-05-02 16:01:25 +0000951 memcpy(dev->dev_addr, node_id, dev->addr_len);
952 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
953 }
hayeswangac718b62013-05-02 16:01:25 +0000954}
955
956static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
957{
958 struct r8152 *tp = netdev_priv(netdev);
959 struct sockaddr *addr = p;
960
961 if (!is_valid_ether_addr(addr->sa_data))
962 return -EADDRNOTAVAIL;
963
964 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
965
966 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
967 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
968 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
969
970 return 0;
971}
972
hayeswangac718b62013-05-02 16:01:25 +0000973static void read_bulk_callback(struct urb *urb)
974{
hayeswangac718b62013-05-02 16:01:25 +0000975 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +0000976 int status = urb->status;
hayeswangebc2ec42013-08-14 20:54:38 +0800977 struct rx_agg *agg;
978 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000979 int result;
hayeswangac718b62013-05-02 16:01:25 +0000980
hayeswangebc2ec42013-08-14 20:54:38 +0800981 agg = urb->context;
982 if (!agg)
983 return;
984
985 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000986 if (!tp)
987 return;
hayeswangebc2ec42013-08-14 20:54:38 +0800988
hayeswangac718b62013-05-02 16:01:25 +0000989 if (test_bit(RTL8152_UNPLUG, &tp->flags))
990 return;
hayeswangebc2ec42013-08-14 20:54:38 +0800991
992 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000993 return;
994
hayeswangebc2ec42013-08-14 20:54:38 +0800995 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +0800996
997 /* When link down, the driver would cancel all bulks. */
998 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +0800999 if (!netif_carrier_ok(netdev))
1000 return;
1001
hayeswang9a4be1b2014-02-18 21:49:07 +08001002 usb_mark_last_busy(tp->udev);
1003
hayeswangac718b62013-05-02 16:01:25 +00001004 switch (status) {
1005 case 0:
hayeswangebc2ec42013-08-14 20:54:38 +08001006 if (urb->actual_length < ETH_ZLEN)
1007 break;
1008
hayeswang2685d412014-03-07 11:04:34 +08001009 spin_lock(&tp->rx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001010 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001011 spin_unlock(&tp->rx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001012 tasklet_schedule(&tp->tl);
1013 return;
hayeswangac718b62013-05-02 16:01:25 +00001014 case -ESHUTDOWN:
1015 set_bit(RTL8152_UNPLUG, &tp->flags);
1016 netif_device_detach(tp->netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001017 return;
hayeswangac718b62013-05-02 16:01:25 +00001018 case -ENOENT:
1019 return; /* the urb is in unlink state */
1020 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001021 if (net_ratelimit())
1022 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001023 break;
hayeswangac718b62013-05-02 16:01:25 +00001024 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001025 if (net_ratelimit())
1026 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001027 break;
hayeswangac718b62013-05-02 16:01:25 +00001028 }
1029
hayeswangebc2ec42013-08-14 20:54:38 +08001030 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001031 if (result == -ENODEV) {
1032 netif_device_detach(tp->netdev);
1033 } else if (result) {
hayeswang2685d412014-03-07 11:04:34 +08001034 spin_lock(&tp->rx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001035 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001036 spin_unlock(&tp->rx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001037 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001038 }
hayeswangac718b62013-05-02 16:01:25 +00001039}
1040
1041static void write_bulk_callback(struct urb *urb)
1042{
hayeswangebc2ec42013-08-14 20:54:38 +08001043 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001044 struct net_device *netdev;
hayeswangebc2ec42013-08-14 20:54:38 +08001045 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001046 struct r8152 *tp;
1047 int status = urb->status;
1048
hayeswangebc2ec42013-08-14 20:54:38 +08001049 agg = urb->context;
1050 if (!agg)
1051 return;
1052
1053 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001054 if (!tp)
1055 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001056
hayeswangd104eaf2014-03-06 15:07:17 +08001057 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001058 stats = &netdev->stats;
hayeswangebc2ec42013-08-14 20:54:38 +08001059 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001060 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001061 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001062 stats->tx_errors += agg->skb_num;
1063 } else {
1064 stats->tx_packets += agg->skb_num;
1065 stats->tx_bytes += agg->skb_len;
1066 }
1067
hayeswang2685d412014-03-07 11:04:34 +08001068 spin_lock(&tp->tx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001069 list_add_tail(&agg->list, &tp->tx_free);
hayeswang2685d412014-03-07 11:04:34 +08001070 spin_unlock(&tp->tx_lock);
hayeswangebc2ec42013-08-14 20:54:38 +08001071
hayeswang9a4be1b2014-02-18 21:49:07 +08001072 usb_autopm_put_interface_async(tp->intf);
1073
hayeswangd104eaf2014-03-06 15:07:17 +08001074 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001075 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001076
1077 if (!test_bit(WORK_ENABLE, &tp->flags))
1078 return;
1079
1080 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1081 return;
1082
1083 if (!skb_queue_empty(&tp->tx_queue))
hayeswang0c3121f2014-03-07 11:04:36 +08001084 tasklet_schedule(&tp->tl);
hayeswangebc2ec42013-08-14 20:54:38 +08001085}
1086
hayeswang40a82912013-08-14 20:54:40 +08001087static void intr_callback(struct urb *urb)
1088{
1089 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001090 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001091 int status = urb->status;
1092 int res;
1093
1094 tp = urb->context;
1095 if (!tp)
1096 return;
1097
1098 if (!test_bit(WORK_ENABLE, &tp->flags))
1099 return;
1100
1101 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1102 return;
1103
1104 switch (status) {
1105 case 0: /* success */
1106 break;
1107 case -ECONNRESET: /* unlink */
1108 case -ESHUTDOWN:
1109 netif_device_detach(tp->netdev);
1110 case -ENOENT:
1111 return;
1112 case -EOVERFLOW:
1113 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1114 goto resubmit;
1115 /* -EPIPE: should clear the halt */
1116 default:
1117 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1118 goto resubmit;
1119 }
1120
1121 d = urb->transfer_buffer;
1122 if (INTR_LINK & __le16_to_cpu(d[0])) {
1123 if (!(tp->speed & LINK_STATUS)) {
1124 set_bit(RTL8152_LINK_CHG, &tp->flags);
1125 schedule_delayed_work(&tp->schedule, 0);
1126 }
1127 } else {
1128 if (tp->speed & LINK_STATUS) {
1129 set_bit(RTL8152_LINK_CHG, &tp->flags);
1130 schedule_delayed_work(&tp->schedule, 0);
1131 }
1132 }
1133
1134resubmit:
1135 res = usb_submit_urb(urb, GFP_ATOMIC);
1136 if (res == -ENODEV)
1137 netif_device_detach(tp->netdev);
1138 else if (res)
1139 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001140 "can't resubmit intr, status %d\n", res);
hayeswang40a82912013-08-14 20:54:40 +08001141}
1142
hayeswangebc2ec42013-08-14 20:54:38 +08001143static inline void *rx_agg_align(void *data)
1144{
hayeswang8e1f51b2014-01-02 11:22:41 +08001145 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001146}
1147
1148static inline void *tx_agg_align(void *data)
1149{
hayeswang8e1f51b2014-01-02 11:22:41 +08001150 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001151}
1152
1153static void free_all_mem(struct r8152 *tp)
1154{
1155 int i;
1156
1157 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001158 usb_free_urb(tp->rx_info[i].urb);
1159 tp->rx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001160
hayeswang9629e3c2014-01-15 10:42:15 +08001161 kfree(tp->rx_info[i].buffer);
1162 tp->rx_info[i].buffer = NULL;
1163 tp->rx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001164 }
1165
1166 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001167 usb_free_urb(tp->tx_info[i].urb);
1168 tp->tx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001169
hayeswang9629e3c2014-01-15 10:42:15 +08001170 kfree(tp->tx_info[i].buffer);
1171 tp->tx_info[i].buffer = NULL;
1172 tp->tx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001173 }
hayeswang40a82912013-08-14 20:54:40 +08001174
hayeswang9629e3c2014-01-15 10:42:15 +08001175 usb_free_urb(tp->intr_urb);
1176 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001177
hayeswang9629e3c2014-01-15 10:42:15 +08001178 kfree(tp->intr_buff);
1179 tp->intr_buff = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001180}
1181
1182static int alloc_all_mem(struct r8152 *tp)
1183{
1184 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001185 struct usb_interface *intf = tp->intf;
1186 struct usb_host_interface *alt = intf->cur_altsetting;
1187 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec42013-08-14 20:54:38 +08001188 struct urb *urb;
1189 int node, i;
1190 u8 *buf;
1191
1192 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1193
1194 spin_lock_init(&tp->rx_lock);
1195 spin_lock_init(&tp->tx_lock);
1196 INIT_LIST_HEAD(&tp->rx_done);
1197 INIT_LIST_HEAD(&tp->tx_free);
1198 skb_queue_head_init(&tp->tx_queue);
1199
1200 for (i = 0; i < RTL8152_MAX_RX; i++) {
1201 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1202 if (!buf)
1203 goto err1;
1204
1205 if (buf != rx_agg_align(buf)) {
1206 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001207 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1208 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001209 if (!buf)
1210 goto err1;
1211 }
1212
1213 urb = usb_alloc_urb(0, GFP_KERNEL);
1214 if (!urb) {
1215 kfree(buf);
1216 goto err1;
1217 }
1218
1219 INIT_LIST_HEAD(&tp->rx_info[i].list);
1220 tp->rx_info[i].context = tp;
1221 tp->rx_info[i].urb = urb;
1222 tp->rx_info[i].buffer = buf;
1223 tp->rx_info[i].head = rx_agg_align(buf);
1224 }
1225
1226 for (i = 0; i < RTL8152_MAX_TX; i++) {
1227 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1228 if (!buf)
1229 goto err1;
1230
1231 if (buf != tx_agg_align(buf)) {
1232 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001233 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1234 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001235 if (!buf)
1236 goto err1;
1237 }
1238
1239 urb = usb_alloc_urb(0, GFP_KERNEL);
1240 if (!urb) {
1241 kfree(buf);
1242 goto err1;
1243 }
1244
1245 INIT_LIST_HEAD(&tp->tx_info[i].list);
1246 tp->tx_info[i].context = tp;
1247 tp->tx_info[i].urb = urb;
1248 tp->tx_info[i].buffer = buf;
1249 tp->tx_info[i].head = tx_agg_align(buf);
1250
1251 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1252 }
1253
hayeswang40a82912013-08-14 20:54:40 +08001254 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1255 if (!tp->intr_urb)
1256 goto err1;
1257
1258 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1259 if (!tp->intr_buff)
1260 goto err1;
1261
1262 tp->intr_interval = (int)ep_intr->desc.bInterval;
1263 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1264 tp->intr_buff, INTBUFSIZE, intr_callback,
1265 tp, tp->intr_interval);
1266
hayeswangebc2ec42013-08-14 20:54:38 +08001267 return 0;
1268
1269err1:
1270 free_all_mem(tp);
1271 return -ENOMEM;
1272}
1273
hayeswang0de98f62013-08-16 16:09:35 +08001274static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1275{
1276 struct tx_agg *agg = NULL;
1277 unsigned long flags;
1278
hayeswang21949ab2014-03-07 11:04:35 +08001279 if (list_empty(&tp->tx_free))
1280 return NULL;
1281
hayeswang0de98f62013-08-16 16:09:35 +08001282 spin_lock_irqsave(&tp->tx_lock, flags);
1283 if (!list_empty(&tp->tx_free)) {
1284 struct list_head *cursor;
1285
1286 cursor = tp->tx_free.next;
1287 list_del_init(cursor);
1288 agg = list_entry(cursor, struct tx_agg, list);
1289 }
1290 spin_unlock_irqrestore(&tp->tx_lock, flags);
1291
1292 return agg;
1293}
1294
hayeswang5bd23882013-08-14 20:54:39 +08001295static void
1296r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1297{
1298 memset(desc, 0, sizeof(*desc));
1299
1300 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1301
1302 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1303 __be16 protocol;
1304 u8 ip_protocol;
1305 u32 opts2 = 0;
1306
1307 if (skb->protocol == htons(ETH_P_8021Q))
1308 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1309 else
1310 protocol = skb->protocol;
1311
1312 switch (protocol) {
1313 case htons(ETH_P_IP):
1314 opts2 |= IPV4_CS;
1315 ip_protocol = ip_hdr(skb)->protocol;
1316 break;
1317
1318 case htons(ETH_P_IPV6):
1319 opts2 |= IPV6_CS;
1320 ip_protocol = ipv6_hdr(skb)->nexthdr;
1321 break;
1322
1323 default:
1324 ip_protocol = IPPROTO_RAW;
1325 break;
1326 }
1327
1328 if (ip_protocol == IPPROTO_TCP) {
1329 opts2 |= TCP_CS;
1330 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1331 } else if (ip_protocol == IPPROTO_UDP) {
1332 opts2 |= UDP_CS;
1333 } else {
1334 WARN_ON_ONCE(1);
1335 }
1336
1337 desc->opts2 = cpu_to_le32(opts2);
1338 }
1339}
1340
hayeswangb1379d92013-08-16 16:09:37 +08001341static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1342{
hayeswangd84130a2014-02-18 21:49:02 +08001343 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001344 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001345 u8 *tx_data;
1346
hayeswangd84130a2014-02-18 21:49:02 +08001347 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001348 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001349 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001350 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001351
hayeswangb1379d92013-08-16 16:09:37 +08001352 tx_data = agg->head;
1353 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001354 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001355
hayeswang7937f9e2013-11-20 17:30:54 +08001356 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001357 struct tx_desc *tx_desc;
1358 struct sk_buff *skb;
1359 unsigned int len;
1360
hayeswangd84130a2014-02-18 21:49:02 +08001361 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001362 if (!skb)
1363 break;
1364
hayeswang7937f9e2013-11-20 17:30:54 +08001365 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001366 len = skb->len;
1367 if (remain < len) {
hayeswangd84130a2014-02-18 21:49:02 +08001368 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001369 break;
1370 }
1371
hayeswang7937f9e2013-11-20 17:30:54 +08001372 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001373 tx_desc = (struct tx_desc *)tx_data;
1374 tx_data += sizeof(*tx_desc);
1375
1376 r8152_tx_csum(tp, tx_desc, skb);
1377 memcpy(tx_data, skb->data, len);
1378 agg->skb_num++;
1379 agg->skb_len += len;
1380 dev_kfree_skb_any(skb);
1381
hayeswang7937f9e2013-11-20 17:30:54 +08001382 tx_data += len;
1383 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001384 }
1385
hayeswangd84130a2014-02-18 21:49:02 +08001386 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001387 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001388 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001389 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001390 }
1391
hayeswang0c3121f2014-03-07 11:04:36 +08001392 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001393
1394 if (netif_queue_stopped(tp->netdev) &&
1395 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1396 netif_wake_queue(tp->netdev);
1397
hayeswang0c3121f2014-03-07 11:04:36 +08001398 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001399
hayeswang0c3121f2014-03-07 11:04:36 +08001400 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001401 if (ret < 0)
1402 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001403
hayeswangb1379d92013-08-16 16:09:37 +08001404 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1405 agg->head, (int)(tx_data - (u8 *)agg->head),
1406 (usb_complete_t)write_bulk_callback, agg);
1407
hayeswang0c3121f2014-03-07 11:04:36 +08001408 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001409 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001410 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001411
1412out_tx_fill:
1413 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001414}
1415
hayeswang565cab02014-03-07 11:04:38 +08001416static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1417{
1418 u8 checksum = CHECKSUM_NONE;
1419 u32 opts2, opts3;
1420
1421 if (tp->version == RTL_VER_01)
1422 goto return_result;
1423
1424 opts2 = le32_to_cpu(rx_desc->opts2);
1425 opts3 = le32_to_cpu(rx_desc->opts3);
1426
1427 if (opts2 & RD_IPV4_CS) {
1428 if (opts3 & IPF)
1429 checksum = CHECKSUM_NONE;
1430 else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
1431 checksum = CHECKSUM_NONE;
1432 else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
1433 checksum = CHECKSUM_NONE;
1434 else
1435 checksum = CHECKSUM_UNNECESSARY;
1436 }
1437
1438return_result:
1439 return checksum;
1440}
1441
hayeswangebc2ec42013-08-14 20:54:38 +08001442static void rx_bottom(struct r8152 *tp)
1443{
hayeswanga5a4f462013-08-16 16:09:34 +08001444 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001445 struct list_head *cursor, *next, rx_queue;
hayeswangebc2ec42013-08-14 20:54:38 +08001446
hayeswangd84130a2014-02-18 21:49:02 +08001447 if (list_empty(&tp->rx_done))
1448 return;
1449
1450 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001451 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001452 list_splice_init(&tp->rx_done, &rx_queue);
1453 spin_unlock_irqrestore(&tp->rx_lock, flags);
1454
1455 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001456 struct rx_desc *rx_desc;
1457 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001458 int len_used = 0;
1459 struct urb *urb;
1460 u8 *rx_data;
1461 int ret;
1462
hayeswangebc2ec42013-08-14 20:54:38 +08001463 list_del_init(cursor);
hayeswangebc2ec42013-08-14 20:54:38 +08001464
1465 agg = list_entry(cursor, struct rx_agg, list);
1466 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001467 if (urb->actual_length < ETH_ZLEN)
1468 goto submit;
hayeswangebc2ec42013-08-14 20:54:38 +08001469
hayeswangebc2ec42013-08-14 20:54:38 +08001470 rx_desc = agg->head;
1471 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001472 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001473
hayeswang7937f9e2013-11-20 17:30:54 +08001474 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001475 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001476 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001477 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001478 struct sk_buff *skb;
1479
hayeswang7937f9e2013-11-20 17:30:54 +08001480 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec42013-08-14 20:54:38 +08001481 if (pkt_len < ETH_ZLEN)
1482 break;
1483
hayeswang7937f9e2013-11-20 17:30:54 +08001484 len_used += pkt_len;
1485 if (urb->actual_length < len_used)
1486 break;
1487
hayeswang8e1f51b2014-01-02 11:22:41 +08001488 pkt_len -= CRC_SIZE;
hayeswangebc2ec42013-08-14 20:54:38 +08001489 rx_data += sizeof(struct rx_desc);
1490
1491 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1492 if (!skb) {
1493 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001494 goto find_next_rx;
hayeswangebc2ec42013-08-14 20:54:38 +08001495 }
hayeswang565cab02014-03-07 11:04:38 +08001496
1497 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001498 memcpy(skb->data, rx_data, pkt_len);
1499 skb_put(skb, pkt_len);
1500 skb->protocol = eth_type_trans(skb, netdev);
hayeswang9d9aafa2014-02-18 21:49:09 +08001501 netif_receive_skb(skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001502 stats->rx_packets++;
1503 stats->rx_bytes += pkt_len;
1504
hayeswang5e2f7482014-03-07 11:04:37 +08001505find_next_rx:
hayeswang8e1f51b2014-01-02 11:22:41 +08001506 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec42013-08-14 20:54:38 +08001507 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec42013-08-14 20:54:38 +08001508 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001509 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001510 }
1511
hayeswang0de98f62013-08-16 16:09:35 +08001512submit:
hayeswangebc2ec42013-08-14 20:54:38 +08001513 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangebc2ec42013-08-14 20:54:38 +08001514 if (ret && ret != -ENODEV) {
hayeswangd84130a2014-02-18 21:49:02 +08001515 spin_lock_irqsave(&tp->rx_lock, flags);
1516 list_add_tail(&agg->list, &tp->rx_done);
1517 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001518 tasklet_schedule(&tp->tl);
1519 }
1520 }
hayeswangebc2ec42013-08-14 20:54:38 +08001521}
1522
1523static void tx_bottom(struct r8152 *tp)
1524{
hayeswangebc2ec42013-08-14 20:54:38 +08001525 int res;
1526
hayeswangb1379d92013-08-16 16:09:37 +08001527 do {
1528 struct tx_agg *agg;
hayeswangebc2ec42013-08-14 20:54:38 +08001529
hayeswangb1379d92013-08-16 16:09:37 +08001530 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec42013-08-14 20:54:38 +08001531 break;
1532
hayeswangb1379d92013-08-16 16:09:37 +08001533 agg = r8152_get_tx_agg(tp);
1534 if (!agg)
hayeswangebc2ec42013-08-14 20:54:38 +08001535 break;
hayeswangb1379d92013-08-16 16:09:37 +08001536
1537 res = r8152_tx_agg_fill(tp, agg);
1538 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08001539 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08001540
1541 if (res == -ENODEV) {
1542 netif_device_detach(netdev);
1543 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08001544 struct net_device_stats *stats = &netdev->stats;
1545 unsigned long flags;
1546
hayeswangb1379d92013-08-16 16:09:37 +08001547 netif_warn(tp, tx_err, netdev,
1548 "failed tx_urb %d\n", res);
1549 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08001550
hayeswangb1379d92013-08-16 16:09:37 +08001551 spin_lock_irqsave(&tp->tx_lock, flags);
1552 list_add_tail(&agg->list, &tp->tx_free);
1553 spin_unlock_irqrestore(&tp->tx_lock, flags);
1554 }
hayeswangebc2ec42013-08-14 20:54:38 +08001555 }
hayeswangb1379d92013-08-16 16:09:37 +08001556 } while (res == 0);
hayeswangebc2ec42013-08-14 20:54:38 +08001557}
1558
1559static void bottom_half(unsigned long data)
1560{
1561 struct r8152 *tp;
1562
1563 tp = (struct r8152 *)data;
1564
1565 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1566 return;
1567
1568 if (!test_bit(WORK_ENABLE, &tp->flags))
1569 return;
1570
hayeswang7559fb2f2013-08-16 16:09:38 +08001571 /* When link down, the driver would cancel all bulks. */
1572 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +08001573 if (!netif_carrier_ok(tp->netdev))
1574 return;
1575
1576 rx_bottom(tp);
hayeswang0c3121f2014-03-07 11:04:36 +08001577 tx_bottom(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001578}
1579
1580static
1581int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1582{
1583 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1584 agg->head, rx_buf_sz,
1585 (usb_complete_t)read_bulk_callback, agg);
1586
1587 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001588}
1589
hayeswang00a5e362014-02-18 21:48:59 +08001590static void rtl_drop_queued_tx(struct r8152 *tp)
1591{
1592 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001593 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08001594 struct sk_buff *skb;
1595
hayeswangd84130a2014-02-18 21:49:02 +08001596 if (skb_queue_empty(tx_queue))
1597 return;
1598
1599 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001600 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001601 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001602 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001603
1604 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001605 dev_kfree_skb(skb);
1606 stats->tx_dropped++;
1607 }
1608}
1609
hayeswangac718b62013-05-02 16:01:25 +00001610static void rtl8152_tx_timeout(struct net_device *netdev)
1611{
1612 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001613 int i;
1614
Hayes Wang4a8deae2014-01-07 11:18:22 +08001615 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001616 for (i = 0; i < RTL8152_MAX_TX; i++)
1617 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001618}
1619
1620static void rtl8152_set_rx_mode(struct net_device *netdev)
1621{
1622 struct r8152 *tp = netdev_priv(netdev);
1623
hayeswang40a82912013-08-14 20:54:40 +08001624 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001625 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001626 schedule_delayed_work(&tp->schedule, 0);
1627 }
hayeswangac718b62013-05-02 16:01:25 +00001628}
1629
1630static void _rtl8152_set_rx_mode(struct net_device *netdev)
1631{
1632 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001633 u32 mc_filter[2]; /* Multicast hash filter */
1634 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001635 u32 ocp_data;
1636
hayeswangac718b62013-05-02 16:01:25 +00001637 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1638 netif_stop_queue(netdev);
1639 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1640 ocp_data &= ~RCR_ACPT_ALL;
1641 ocp_data |= RCR_AB | RCR_APM;
1642
1643 if (netdev->flags & IFF_PROMISC) {
1644 /* Unconditionally log net taps. */
1645 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1646 ocp_data |= RCR_AM | RCR_AAP;
1647 mc_filter[1] = mc_filter[0] = 0xffffffff;
1648 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1649 (netdev->flags & IFF_ALLMULTI)) {
1650 /* Too many to filter perfectly -- accept all multicasts. */
1651 ocp_data |= RCR_AM;
1652 mc_filter[1] = mc_filter[0] = 0xffffffff;
1653 } else {
1654 struct netdev_hw_addr *ha;
1655
1656 mc_filter[1] = mc_filter[0] = 0;
1657 netdev_for_each_mc_addr(ha, netdev) {
1658 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1659 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1660 ocp_data |= RCR_AM;
1661 }
1662 }
1663
hayeswang31787f52013-07-31 17:21:25 +08001664 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1665 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001666
hayeswang31787f52013-07-31 17:21:25 +08001667 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001668 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1669 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001670}
1671
1672static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswang0c3121f2014-03-07 11:04:36 +08001673 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00001674{
1675 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001676
hayeswangac718b62013-05-02 16:01:25 +00001677 skb_tx_timestamp(skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001678
hayeswang61598782013-11-20 17:30:55 +08001679 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001680
hayeswang0c3121f2014-03-07 11:04:36 +08001681 if (!list_empty(&tp->tx_free)) {
1682 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
1683 set_bit(SCHEDULE_TASKLET, &tp->flags);
1684 schedule_delayed_work(&tp->schedule, 0);
1685 } else {
1686 usb_mark_last_busy(tp->udev);
1687 tasklet_schedule(&tp->tl);
1688 }
1689 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
hayeswangdd1b1192013-11-20 17:30:56 +08001690 netif_stop_queue(netdev);
1691
hayeswangac718b62013-05-02 16:01:25 +00001692 return NETDEV_TX_OK;
1693}
1694
1695static void r8152b_reset_packet_filter(struct r8152 *tp)
1696{
1697 u32 ocp_data;
1698
1699 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1700 ocp_data &= ~FMC_FCR_MCU_EN;
1701 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1702 ocp_data |= FMC_FCR_MCU_EN;
1703 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1704}
1705
1706static void rtl8152_nic_reset(struct r8152 *tp)
1707{
1708 int i;
1709
1710 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1711
1712 for (i = 0; i < 1000; i++) {
1713 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1714 break;
1715 udelay(100);
1716 }
1717}
1718
hayeswangdd1b1192013-11-20 17:30:56 +08001719static void set_tx_qlen(struct r8152 *tp)
1720{
1721 struct net_device *netdev = tp->netdev;
1722
1723 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1724 sizeof(struct tx_desc));
1725}
1726
hayeswangac718b62013-05-02 16:01:25 +00001727static inline u8 rtl8152_get_speed(struct r8152 *tp)
1728{
1729 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1730}
1731
hayeswang507605a2014-01-02 11:22:43 +08001732static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001733{
hayeswangebc2ec42013-08-14 20:54:38 +08001734 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001735 u8 speed;
1736
1737 speed = rtl8152_get_speed(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001738 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001739 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08001740 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001741 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1742 } else {
1743 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08001744 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001745 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1746 }
hayeswang507605a2014-01-02 11:22:43 +08001747}
1748
hayeswang00a5e362014-02-18 21:48:59 +08001749static void rxdy_gated_en(struct r8152 *tp, bool enable)
1750{
1751 u32 ocp_data;
1752
1753 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1754 if (enable)
1755 ocp_data |= RXDY_GATED_EN;
1756 else
1757 ocp_data &= ~RXDY_GATED_EN;
1758 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1759}
1760
hayeswang507605a2014-01-02 11:22:43 +08001761static int rtl_enable(struct r8152 *tp)
1762{
1763 u32 ocp_data;
1764 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001765
1766 r8152b_reset_packet_filter(tp);
1767
1768 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1769 ocp_data |= CR_RE | CR_TE;
1770 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1771
hayeswang00a5e362014-02-18 21:48:59 +08001772 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001773
hayeswangebc2ec42013-08-14 20:54:38 +08001774 INIT_LIST_HEAD(&tp->rx_done);
1775 ret = 0;
1776 for (i = 0; i < RTL8152_MAX_RX; i++) {
1777 INIT_LIST_HEAD(&tp->rx_info[i].list);
1778 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1779 }
hayeswangac718b62013-05-02 16:01:25 +00001780
hayeswangebc2ec42013-08-14 20:54:38 +08001781 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001782}
1783
hayeswang507605a2014-01-02 11:22:43 +08001784static int rtl8152_enable(struct r8152 *tp)
1785{
1786 set_tx_qlen(tp);
1787 rtl_set_eee_plus(tp);
1788
1789 return rtl_enable(tp);
1790}
1791
hayeswang43779f82014-01-02 11:25:10 +08001792static void r8153_set_rx_agg(struct r8152 *tp)
1793{
1794 u8 speed;
1795
1796 speed = rtl8152_get_speed(tp);
1797 if (speed & _1000bps) {
1798 if (tp->udev->speed == USB_SPEED_SUPER) {
1799 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1800 RX_THR_SUPPER);
1801 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1802 EARLY_AGG_SUPPER);
1803 } else {
1804 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1805 RX_THR_HIGH);
1806 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1807 EARLY_AGG_HIGH);
1808 }
1809 } else {
1810 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1811 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1812 EARLY_AGG_SLOW);
1813 }
1814}
1815
1816static int rtl8153_enable(struct r8152 *tp)
1817{
1818 set_tx_qlen(tp);
1819 rtl_set_eee_plus(tp);
1820 r8153_set_rx_agg(tp);
1821
1822 return rtl_enable(tp);
1823}
1824
hayeswangac718b62013-05-02 16:01:25 +00001825static void rtl8152_disable(struct r8152 *tp)
1826{
hayeswangebc2ec42013-08-14 20:54:38 +08001827 u32 ocp_data;
1828 int i;
hayeswangac718b62013-05-02 16:01:25 +00001829
1830 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1831 ocp_data &= ~RCR_ACPT_ALL;
1832 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1833
hayeswang00a5e362014-02-18 21:48:59 +08001834 rtl_drop_queued_tx(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001835
1836 for (i = 0; i < RTL8152_MAX_TX; i++)
1837 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001838
hayeswang00a5e362014-02-18 21:48:59 +08001839 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00001840
1841 for (i = 0; i < 1000; i++) {
1842 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1843 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1844 break;
1845 mdelay(1);
1846 }
1847
1848 for (i = 0; i < 1000; i++) {
1849 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1850 break;
1851 mdelay(1);
1852 }
1853
hayeswangebc2ec42013-08-14 20:54:38 +08001854 for (i = 0; i < RTL8152_MAX_RX; i++)
1855 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001856
1857 rtl8152_nic_reset(tp);
1858}
1859
hayeswang00a5e362014-02-18 21:48:59 +08001860static void r8152_power_cut_en(struct r8152 *tp, bool enable)
1861{
1862 u32 ocp_data;
1863
1864 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1865 if (enable)
1866 ocp_data |= POWER_CUT;
1867 else
1868 ocp_data &= ~POWER_CUT;
1869 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1870
1871 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1872 ocp_data &= ~RESUME_INDICATE;
1873 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08001874}
1875
hayeswang21ff2e82014-02-18 21:49:06 +08001876#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1877
1878static u32 __rtl_get_wol(struct r8152 *tp)
1879{
1880 u32 ocp_data;
1881 u32 wolopts = 0;
1882
1883 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1884 if (!(ocp_data & LAN_WAKE_EN))
1885 return 0;
1886
1887 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1888 if (ocp_data & LINK_ON_WAKE_EN)
1889 wolopts |= WAKE_PHY;
1890
1891 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1892 if (ocp_data & UWF_EN)
1893 wolopts |= WAKE_UCAST;
1894 if (ocp_data & BWF_EN)
1895 wolopts |= WAKE_BCAST;
1896 if (ocp_data & MWF_EN)
1897 wolopts |= WAKE_MCAST;
1898
1899 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1900 if (ocp_data & MAGIC_EN)
1901 wolopts |= WAKE_MAGIC;
1902
1903 return wolopts;
1904}
1905
1906static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
1907{
1908 u32 ocp_data;
1909
1910 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1911
1912 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1913 ocp_data &= ~LINK_ON_WAKE_EN;
1914 if (wolopts & WAKE_PHY)
1915 ocp_data |= LINK_ON_WAKE_EN;
1916 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
1917
1918 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1919 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
1920 if (wolopts & WAKE_UCAST)
1921 ocp_data |= UWF_EN;
1922 if (wolopts & WAKE_BCAST)
1923 ocp_data |= BWF_EN;
1924 if (wolopts & WAKE_MCAST)
1925 ocp_data |= MWF_EN;
1926 if (wolopts & WAKE_ANY)
1927 ocp_data |= LAN_WAKE_EN;
1928 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
1929
1930 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1931
1932 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1933 ocp_data &= ~MAGIC_EN;
1934 if (wolopts & WAKE_MAGIC)
1935 ocp_data |= MAGIC_EN;
1936 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1937
1938 if (wolopts & WAKE_ANY)
1939 device_set_wakeup_enable(&tp->udev->dev, true);
1940 else
1941 device_set_wakeup_enable(&tp->udev->dev, false);
1942}
1943
hayeswang9a4be1b2014-02-18 21:49:07 +08001944static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
1945{
1946 if (enable) {
1947 u32 ocp_data;
1948
1949 __rtl_set_wol(tp, WAKE_ANY);
1950
1951 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1952
1953 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1954 ocp_data |= LINK_OFF_WAKE_EN;
1955 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
1956
1957 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1958 } else {
1959 __rtl_set_wol(tp, tp->saved_wolopts);
1960 }
1961}
1962
hayeswangaa66a5f2014-02-18 21:49:04 +08001963static void rtl_phy_reset(struct r8152 *tp)
1964{
1965 u16 data;
1966 int i;
1967
1968 clear_bit(PHY_RESET, &tp->flags);
1969
1970 data = r8152_mdio_read(tp, MII_BMCR);
1971
1972 /* don't reset again before the previous one complete */
1973 if (data & BMCR_RESET)
1974 return;
1975
1976 data |= BMCR_RESET;
1977 r8152_mdio_write(tp, MII_BMCR, data);
1978
1979 for (i = 0; i < 50; i++) {
1980 msleep(20);
1981 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
1982 break;
1983 }
1984}
1985
hayeswang43499682014-02-18 21:48:58 +08001986static void rtl_clear_bp(struct r8152 *tp)
1987{
1988 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1989 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1990 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1991 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1992 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1993 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1994 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1995 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1996 mdelay(3);
1997 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1998 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1999}
2000
2001static void r8153_clear_bp(struct r8152 *tp)
2002{
2003 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
2004 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
2005 rtl_clear_bp(tp);
2006}
2007
2008static void r8153_teredo_off(struct r8152 *tp)
2009{
2010 u32 ocp_data;
2011
2012 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2013 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
2014 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2015
2016 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2017 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2018 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2019}
2020
2021static void r8152b_disable_aldps(struct r8152 *tp)
2022{
2023 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
2024 msleep(20);
2025}
2026
2027static inline void r8152b_enable_aldps(struct r8152 *tp)
2028{
2029 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2030 LINKENA | DIS_SDSAVE);
2031}
2032
2033static void r8152b_hw_phy_cfg(struct r8152 *tp)
2034{
hayeswangf0cbe0a2014-02-18 21:49:03 +08002035 u16 data;
2036
2037 data = r8152_mdio_read(tp, MII_BMCR);
2038 if (data & BMCR_PDOWN) {
2039 data &= ~BMCR_PDOWN;
2040 r8152_mdio_write(tp, MII_BMCR, data);
2041 }
2042
hayeswang43499682014-02-18 21:48:58 +08002043 r8152b_disable_aldps(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002044
2045 rtl_clear_bp(tp);
2046
2047 r8152b_enable_aldps(tp);
hayeswangaa66a5f2014-02-18 21:49:04 +08002048 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002049}
2050
hayeswangac718b62013-05-02 16:01:25 +00002051static void r8152b_exit_oob(struct r8152 *tp)
2052{
hayeswangdb8515e2014-03-06 15:07:16 +08002053 u32 ocp_data;
2054 int i;
hayeswangac718b62013-05-02 16:01:25 +00002055
2056 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2057 ocp_data &= ~RCR_ACPT_ALL;
2058 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2059
hayeswang00a5e362014-02-18 21:48:59 +08002060 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002061 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002062 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002063
2064 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2065 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2066
2067 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2068 ocp_data &= ~NOW_IS_OOB;
2069 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2070
2071 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2072 ocp_data &= ~MCU_BORW_EN;
2073 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2074
2075 for (i = 0; i < 1000; i++) {
2076 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2077 if (ocp_data & LINK_LIST_READY)
2078 break;
2079 mdelay(1);
2080 }
2081
2082 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2083 ocp_data |= RE_INIT_LL;
2084 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2085
2086 for (i = 0; i < 1000; i++) {
2087 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2088 if (ocp_data & LINK_LIST_READY)
2089 break;
2090 mdelay(1);
2091 }
2092
2093 rtl8152_nic_reset(tp);
2094
2095 /* rx share fifo credit full threshold */
2096 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2097
2098 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
2099 ocp_data &= STAT_SPEED_MASK;
2100 if (ocp_data == STAT_SPEED_FULL) {
2101 /* rx share fifo credit near full threshold */
2102 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2103 RXFIFO_THR2_FULL);
2104 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2105 RXFIFO_THR3_FULL);
2106 } else {
2107 /* rx share fifo credit near full threshold */
2108 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2109 RXFIFO_THR2_HIGH);
2110 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2111 RXFIFO_THR3_HIGH);
2112 }
2113
2114 /* TX share fifo free credit full threshold */
2115 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2116
2117 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002118 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002119 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2120 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2121
2122 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2123 ocp_data &= ~CPCR_RX_VLAN;
2124 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2125
2126 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2127
2128 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2129 ocp_data |= TCR0_AUTO_FIFO;
2130 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2131}
2132
2133static void r8152b_enter_oob(struct r8152 *tp)
2134{
hayeswang45f4a192014-01-06 17:08:41 +08002135 u32 ocp_data;
2136 int i;
hayeswangac718b62013-05-02 16:01:25 +00002137
2138 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2139 ocp_data &= ~NOW_IS_OOB;
2140 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2141
2142 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2143 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2144 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2145
2146 rtl8152_disable(tp);
2147
2148 for (i = 0; i < 1000; i++) {
2149 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2150 if (ocp_data & LINK_LIST_READY)
2151 break;
2152 mdelay(1);
2153 }
2154
2155 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2156 ocp_data |= RE_INIT_LL;
2157 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2158
2159 for (i = 0; i < 1000; i++) {
2160 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2161 if (ocp_data & LINK_LIST_READY)
2162 break;
2163 mdelay(1);
2164 }
2165
2166 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2167
hayeswangac718b62013-05-02 16:01:25 +00002168 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2169 ocp_data |= CPCR_RX_VLAN;
2170 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2171
2172 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2173 ocp_data |= ALDPS_PROXY_MODE;
2174 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2175
2176 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2177 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2178 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2179
hayeswang00a5e362014-02-18 21:48:59 +08002180 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002181
2182 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2183 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2184 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2185}
2186
hayeswang43779f82014-01-02 11:25:10 +08002187static void r8153_hw_phy_cfg(struct r8152 *tp)
2188{
2189 u32 ocp_data;
2190 u16 data;
2191
2192 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002193 data = r8152_mdio_read(tp, MII_BMCR);
2194 if (data & BMCR_PDOWN) {
2195 data &= ~BMCR_PDOWN;
2196 r8152_mdio_write(tp, MII_BMCR, data);
2197 }
hayeswang43779f82014-01-02 11:25:10 +08002198
hayeswang7e9da482014-02-18 21:49:05 +08002199 r8153_clear_bp(tp);
2200
hayeswang43779f82014-01-02 11:25:10 +08002201 if (tp->version == RTL_VER_03) {
2202 data = ocp_reg_read(tp, OCP_EEE_CFG);
2203 data &= ~CTAP_SHORT_EN;
2204 ocp_reg_write(tp, OCP_EEE_CFG, data);
2205 }
2206
2207 data = ocp_reg_read(tp, OCP_POWER_CFG);
2208 data |= EEE_CLKDIV_EN;
2209 ocp_reg_write(tp, OCP_POWER_CFG, data);
2210
2211 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2212 data |= EN_10M_BGOFF;
2213 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2214 data = ocp_reg_read(tp, OCP_POWER_CFG);
2215 data |= EN_10M_PLLOFF;
2216 ocp_reg_write(tp, OCP_POWER_CFG, data);
2217 data = sram_read(tp, SRAM_IMPEDANCE);
2218 data &= ~RX_DRIVING_MASK;
2219 sram_write(tp, SRAM_IMPEDANCE, data);
2220
2221 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2222 ocp_data |= PFM_PWM_SWITCH;
2223 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2224
2225 data = sram_read(tp, SRAM_LPF_CFG);
2226 data |= LPF_AUTO_TUNE;
2227 sram_write(tp, SRAM_LPF_CFG, data);
2228
2229 data = sram_read(tp, SRAM_10M_AMP1);
2230 data |= GDAC_IB_UPALL;
2231 sram_write(tp, SRAM_10M_AMP1, data);
2232 data = sram_read(tp, SRAM_10M_AMP2);
2233 data |= AMP_DN;
2234 sram_write(tp, SRAM_10M_AMP2, data);
hayeswangaa66a5f2014-02-18 21:49:04 +08002235
2236 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002237}
2238
hayeswangb9702722014-02-18 21:49:00 +08002239static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002240{
2241 u8 u1u2[8];
2242
2243 if (enable)
2244 memset(u1u2, 0xff, sizeof(u1u2));
2245 else
2246 memset(u1u2, 0x00, sizeof(u1u2));
2247
2248 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2249}
2250
hayeswangb9702722014-02-18 21:49:00 +08002251static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002252{
2253 u32 ocp_data;
2254
2255 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2256 if (enable)
2257 ocp_data |= U2P3_ENABLE;
2258 else
2259 ocp_data &= ~U2P3_ENABLE;
2260 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2261}
2262
hayeswangb9702722014-02-18 21:49:00 +08002263static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002264{
2265 u32 ocp_data;
2266
2267 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2268 if (enable)
2269 ocp_data |= PWR_EN | PHASE2_EN;
2270 else
2271 ocp_data &= ~(PWR_EN | PHASE2_EN);
2272 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2273
2274 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2275 ocp_data &= ~PCUT_STATUS;
2276 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2277}
2278
hayeswang43779f82014-01-02 11:25:10 +08002279static void r8153_first_init(struct r8152 *tp)
2280{
2281 u32 ocp_data;
2282 int i;
2283
hayeswang00a5e362014-02-18 21:48:59 +08002284 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002285 r8153_teredo_off(tp);
2286
2287 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2288 ocp_data &= ~RCR_ACPT_ALL;
2289 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2290
2291 r8153_hw_phy_cfg(tp);
2292
2293 rtl8152_nic_reset(tp);
2294
2295 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2296 ocp_data &= ~NOW_IS_OOB;
2297 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2298
2299 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2300 ocp_data &= ~MCU_BORW_EN;
2301 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2302
2303 for (i = 0; i < 1000; i++) {
2304 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2305 if (ocp_data & LINK_LIST_READY)
2306 break;
2307 mdelay(1);
2308 }
2309
2310 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2311 ocp_data |= RE_INIT_LL;
2312 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2313
2314 for (i = 0; i < 1000; i++) {
2315 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2316 if (ocp_data & LINK_LIST_READY)
2317 break;
2318 mdelay(1);
2319 }
2320
2321 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2322 ocp_data &= ~CPCR_RX_VLAN;
2323 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2324
2325 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2326
2327 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2328 ocp_data |= TCR0_AUTO_FIFO;
2329 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2330
2331 rtl8152_nic_reset(tp);
2332
2333 /* rx share fifo credit full threshold */
2334 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2335 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2336 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2337 /* TX share fifo free credit full threshold */
2338 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2339
hayeswang9629e3c2014-01-15 10:42:15 +08002340 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002341 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2342 ocp_data &= ~RX_AGG_DISABLE;
2343 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2344}
2345
2346static void r8153_enter_oob(struct r8152 *tp)
2347{
2348 u32 ocp_data;
2349 int i;
2350
2351 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2352 ocp_data &= ~NOW_IS_OOB;
2353 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2354
2355 rtl8152_disable(tp);
2356
2357 for (i = 0; i < 1000; i++) {
2358 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2359 if (ocp_data & LINK_LIST_READY)
2360 break;
2361 mdelay(1);
2362 }
2363
2364 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2365 ocp_data |= RE_INIT_LL;
2366 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2367
2368 for (i = 0; i < 1000; i++) {
2369 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2370 if (ocp_data & LINK_LIST_READY)
2371 break;
2372 mdelay(1);
2373 }
2374
2375 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2376
hayeswang43779f82014-01-02 11:25:10 +08002377 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2378 ocp_data &= ~TEREDO_WAKE_MASK;
2379 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2380
2381 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2382 ocp_data |= CPCR_RX_VLAN;
2383 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2384
2385 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2386 ocp_data |= ALDPS_PROXY_MODE;
2387 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2388
2389 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2390 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2391 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2392
hayeswang00a5e362014-02-18 21:48:59 +08002393 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002394
2395 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2396 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2397 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2398}
2399
2400static void r8153_disable_aldps(struct r8152 *tp)
2401{
2402 u16 data;
2403
2404 data = ocp_reg_read(tp, OCP_POWER_CFG);
2405 data &= ~EN_ALDPS;
2406 ocp_reg_write(tp, OCP_POWER_CFG, data);
2407 msleep(20);
2408}
2409
2410static void r8153_enable_aldps(struct r8152 *tp)
2411{
2412 u16 data;
2413
2414 data = ocp_reg_read(tp, OCP_POWER_CFG);
2415 data |= EN_ALDPS;
2416 ocp_reg_write(tp, OCP_POWER_CFG, data);
2417}
2418
hayeswangac718b62013-05-02 16:01:25 +00002419static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2420{
hayeswang43779f82014-01-02 11:25:10 +08002421 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002422 int ret = 0;
2423
2424 cancel_delayed_work_sync(&tp->schedule);
2425 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2426 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2427 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002428 if (tp->mii.supports_gmii) {
2429 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2430 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2431 } else {
2432 gbcr = 0;
2433 }
hayeswangac718b62013-05-02 16:01:25 +00002434
2435 if (autoneg == AUTONEG_DISABLE) {
2436 if (speed == SPEED_10) {
2437 bmcr = 0;
2438 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2439 } else if (speed == SPEED_100) {
2440 bmcr = BMCR_SPEED100;
2441 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002442 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2443 bmcr = BMCR_SPEED1000;
2444 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002445 } else {
2446 ret = -EINVAL;
2447 goto out;
2448 }
2449
2450 if (duplex == DUPLEX_FULL)
2451 bmcr |= BMCR_FULLDPLX;
2452 } else {
2453 if (speed == SPEED_10) {
2454 if (duplex == DUPLEX_FULL)
2455 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2456 else
2457 anar |= ADVERTISE_10HALF;
2458 } else if (speed == SPEED_100) {
2459 if (duplex == DUPLEX_FULL) {
2460 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2461 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2462 } else {
2463 anar |= ADVERTISE_10HALF;
2464 anar |= ADVERTISE_100HALF;
2465 }
hayeswang43779f82014-01-02 11:25:10 +08002466 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2467 if (duplex == DUPLEX_FULL) {
2468 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2469 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2470 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2471 } else {
2472 anar |= ADVERTISE_10HALF;
2473 anar |= ADVERTISE_100HALF;
2474 gbcr |= ADVERTISE_1000HALF;
2475 }
hayeswangac718b62013-05-02 16:01:25 +00002476 } else {
2477 ret = -EINVAL;
2478 goto out;
2479 }
2480
2481 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2482 }
2483
hayeswangaa66a5f2014-02-18 21:49:04 +08002484 if (test_bit(PHY_RESET, &tp->flags))
2485 bmcr |= BMCR_RESET;
2486
hayeswang43779f82014-01-02 11:25:10 +08002487 if (tp->mii.supports_gmii)
2488 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2489
hayeswangac718b62013-05-02 16:01:25 +00002490 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2491 r8152_mdio_write(tp, MII_BMCR, bmcr);
2492
hayeswangaa66a5f2014-02-18 21:49:04 +08002493 if (test_bit(PHY_RESET, &tp->flags)) {
2494 int i;
2495
2496 clear_bit(PHY_RESET, &tp->flags);
2497 for (i = 0; i < 50; i++) {
2498 msleep(20);
2499 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2500 break;
2501 }
2502 }
2503
hayeswangac718b62013-05-02 16:01:25 +00002504out:
hayeswangac718b62013-05-02 16:01:25 +00002505
2506 return ret;
2507}
2508
2509static void rtl8152_down(struct r8152 *tp)
2510{
hayeswang00a5e362014-02-18 21:48:59 +08002511 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002512 r8152b_disable_aldps(tp);
2513 r8152b_enter_oob(tp);
2514 r8152b_enable_aldps(tp);
2515}
2516
hayeswang43779f82014-01-02 11:25:10 +08002517static void rtl8153_down(struct r8152 *tp)
2518{
hayeswangb9702722014-02-18 21:49:00 +08002519 r8153_u1u2en(tp, false);
2520 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002521 r8153_disable_aldps(tp);
2522 r8153_enter_oob(tp);
2523 r8153_enable_aldps(tp);
2524}
2525
hayeswangac718b62013-05-02 16:01:25 +00002526static void set_carrier(struct r8152 *tp)
2527{
2528 struct net_device *netdev = tp->netdev;
2529 u8 speed;
2530
hayeswang40a82912013-08-14 20:54:40 +08002531 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002532 speed = rtl8152_get_speed(tp);
2533
2534 if (speed & LINK_STATUS) {
2535 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002536 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002537 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2538 netif_carrier_on(netdev);
2539 }
2540 } else {
2541 if (tp->speed & LINK_STATUS) {
2542 netif_carrier_off(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08002543 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002544 tp->rtl_ops.disable(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002545 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002546 }
2547 }
2548 tp->speed = speed;
2549}
2550
2551static void rtl_work_func_t(struct work_struct *work)
2552{
2553 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2554
hayeswang9a4be1b2014-02-18 21:49:07 +08002555 if (usb_autopm_get_interface(tp->intf) < 0)
2556 return;
2557
hayeswangac718b62013-05-02 16:01:25 +00002558 if (!test_bit(WORK_ENABLE, &tp->flags))
2559 goto out1;
2560
2561 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2562 goto out1;
2563
hayeswang40a82912013-08-14 20:54:40 +08002564 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2565 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002566
2567 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2568 _rtl8152_set_rx_mode(tp->netdev);
2569
hayeswang0c3121f2014-03-07 11:04:36 +08002570 if (test_bit(SCHEDULE_TASKLET, &tp->flags) &&
2571 (tp->speed & LINK_STATUS)) {
2572 clear_bit(SCHEDULE_TASKLET, &tp->flags);
2573 tasklet_schedule(&tp->tl);
2574 }
hayeswangaa66a5f2014-02-18 21:49:04 +08002575
2576 if (test_bit(PHY_RESET, &tp->flags))
2577 rtl_phy_reset(tp);
2578
hayeswangac718b62013-05-02 16:01:25 +00002579out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08002580 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002581}
2582
2583static int rtl8152_open(struct net_device *netdev)
2584{
2585 struct r8152 *tp = netdev_priv(netdev);
2586 int res = 0;
2587
hayeswang7e9da482014-02-18 21:49:05 +08002588 res = alloc_all_mem(tp);
2589 if (res)
2590 goto out;
2591
hayeswang9a4be1b2014-02-18 21:49:07 +08002592 res = usb_autopm_get_interface(tp->intf);
2593 if (res < 0) {
2594 free_all_mem(tp);
2595 goto out;
2596 }
2597
2598 /* The WORK_ENABLE may be set when autoresume occurs */
2599 if (test_bit(WORK_ENABLE, &tp->flags)) {
2600 clear_bit(WORK_ENABLE, &tp->flags);
2601 usb_kill_urb(tp->intr_urb);
2602 cancel_delayed_work_sync(&tp->schedule);
2603 if (tp->speed & LINK_STATUS)
2604 tp->rtl_ops.disable(tp);
2605 }
2606
hayeswang7e9da482014-02-18 21:49:05 +08002607 tp->rtl_ops.up(tp);
2608
hayeswang43779f82014-01-02 11:25:10 +08002609 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2610 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2611 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002612 tp->speed = 0;
2613 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002614 netif_start_queue(netdev);
2615 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08002616
hayeswang3d55f442014-02-06 11:55:48 +08002617 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2618 if (res) {
2619 if (res == -ENODEV)
2620 netif_device_detach(tp->netdev);
2621 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2622 res);
hayeswang7e9da482014-02-18 21:49:05 +08002623 free_all_mem(tp);
hayeswang3d55f442014-02-06 11:55:48 +08002624 }
2625
hayeswang9a4be1b2014-02-18 21:49:07 +08002626 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002627
hayeswang7e9da482014-02-18 21:49:05 +08002628out:
hayeswangac718b62013-05-02 16:01:25 +00002629 return res;
2630}
2631
2632static int rtl8152_close(struct net_device *netdev)
2633{
2634 struct r8152 *tp = netdev_priv(netdev);
2635 int res = 0;
2636
2637 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002638 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002639 cancel_delayed_work_sync(&tp->schedule);
2640 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002641
2642 res = usb_autopm_get_interface(tp->intf);
2643 if (res < 0) {
2644 rtl_drop_queued_tx(tp);
2645 } else {
2646 /*
2647 * The autosuspend may have been enabled and wouldn't
2648 * be disable when autoresume occurs, because the
2649 * netif_running() would be false.
2650 */
2651 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2652 rtl_runtime_suspend_enable(tp, false);
2653 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2654 }
2655
2656 tasklet_disable(&tp->tl);
2657 tp->rtl_ops.down(tp);
2658 tasklet_enable(&tp->tl);
2659 usb_autopm_put_interface(tp->intf);
2660 }
hayeswangac718b62013-05-02 16:01:25 +00002661
hayeswang7e9da482014-02-18 21:49:05 +08002662 free_all_mem(tp);
2663
hayeswangac718b62013-05-02 16:01:25 +00002664 return res;
2665}
2666
hayeswangac718b62013-05-02 16:01:25 +00002667static void r8152b_enable_eee(struct r8152 *tp)
2668{
hayeswang45f4a192014-01-06 17:08:41 +08002669 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002670
2671 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2672 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2673 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2674 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2675 EEE_10_CAP | EEE_NWAY_EN |
2676 TX_QUIET_EN | RX_QUIET_EN |
2677 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2678 SDFALLTIME);
2679 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2680 RG_LDVQUIET_EN | RG_CKRSEL |
2681 RG_EEEPRG_EN);
2682 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2683 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2684 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2685 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2686 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2687 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2688}
2689
hayeswang43779f82014-01-02 11:25:10 +08002690static void r8153_enable_eee(struct r8152 *tp)
2691{
2692 u32 ocp_data;
2693 u16 data;
2694
2695 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2696 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2697 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2698 data = ocp_reg_read(tp, OCP_EEE_CFG);
2699 data |= EEE10_EN;
2700 ocp_reg_write(tp, OCP_EEE_CFG, data);
2701 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2702 data |= MY1000_EEE | MY100_EEE;
2703 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2704}
2705
hayeswangac718b62013-05-02 16:01:25 +00002706static void r8152b_enable_fc(struct r8152 *tp)
2707{
2708 u16 anar;
2709
2710 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2711 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2712 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2713}
2714
hayeswangac718b62013-05-02 16:01:25 +00002715static void r8152b_init(struct r8152 *tp)
2716{
hayeswangebc2ec42013-08-14 20:54:38 +08002717 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002718
hayeswangac718b62013-05-02 16:01:25 +00002719 if (tp->version == RTL_VER_01) {
2720 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2721 ocp_data &= ~LED_MODE_MASK;
2722 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2723 }
2724
hayeswang00a5e362014-02-18 21:48:59 +08002725 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002726
hayeswangac718b62013-05-02 16:01:25 +00002727 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2728 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2729 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2730 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2731 ocp_data &= ~MCU_CLK_RATIO_MASK;
2732 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2733 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2734 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2735 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2736 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2737
2738 r8152b_enable_eee(tp);
2739 r8152b_enable_aldps(tp);
2740 r8152b_enable_fc(tp);
2741
hayeswangebc2ec42013-08-14 20:54:38 +08002742 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002743 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec42013-08-14 20:54:38 +08002744 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002745 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2746}
2747
hayeswang43779f82014-01-02 11:25:10 +08002748static void r8153_init(struct r8152 *tp)
2749{
2750 u32 ocp_data;
2751 int i;
2752
hayeswangb9702722014-02-18 21:49:00 +08002753 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002754
2755 for (i = 0; i < 500; i++) {
2756 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2757 AUTOLOAD_DONE)
2758 break;
2759 msleep(20);
2760 }
2761
2762 for (i = 0; i < 500; i++) {
2763 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2764 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2765 break;
2766 msleep(20);
2767 }
2768
hayeswangb9702722014-02-18 21:49:00 +08002769 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002770
2771 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2772 ocp_data &= ~TIMER11_EN;
2773 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2774
hayeswang43779f82014-01-02 11:25:10 +08002775 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2776 ocp_data &= ~LED_MODE_MASK;
2777 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2778
2779 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2780 ocp_data &= ~LPM_TIMER_MASK;
2781 if (tp->udev->speed == USB_SPEED_SUPER)
2782 ocp_data |= LPM_TIMER_500US;
2783 else
2784 ocp_data |= LPM_TIMER_500MS;
2785 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2786
2787 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2788 ocp_data &= ~SEN_VAL_MASK;
2789 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2790 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2791
hayeswangb9702722014-02-18 21:49:00 +08002792 r8153_power_cut_en(tp, false);
2793 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002794
hayeswang43779f82014-01-02 11:25:10 +08002795 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2796 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2797 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2798 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2799 U1U2_SPDWN_EN | L1_SPDWN_EN);
2800 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2801 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2802 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2803 EEE_SPDWN_EN);
2804
2805 r8153_enable_eee(tp);
2806 r8153_enable_aldps(tp);
2807 r8152b_enable_fc(tp);
hayeswang43779f82014-01-02 11:25:10 +08002808}
2809
hayeswangac718b62013-05-02 16:01:25 +00002810static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2811{
2812 struct r8152 *tp = usb_get_intfdata(intf);
2813
hayeswang9a4be1b2014-02-18 21:49:07 +08002814 if (PMSG_IS_AUTO(message))
2815 set_bit(SELECTIVE_SUSPEND, &tp->flags);
2816 else
2817 netif_device_detach(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002818
2819 if (netif_running(tp->netdev)) {
2820 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002821 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002822 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08002823 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2824 rtl_runtime_suspend_enable(tp, true);
2825 } else {
2826 tasklet_disable(&tp->tl);
2827 tp->rtl_ops.down(tp);
2828 tasklet_enable(&tp->tl);
2829 }
hayeswangac718b62013-05-02 16:01:25 +00002830 }
2831
hayeswangac718b62013-05-02 16:01:25 +00002832 return 0;
2833}
2834
2835static int rtl8152_resume(struct usb_interface *intf)
2836{
2837 struct r8152 *tp = usb_get_intfdata(intf);
2838
hayeswang9a4be1b2014-02-18 21:49:07 +08002839 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2840 tp->rtl_ops.init(tp);
2841 netif_device_attach(tp->netdev);
2842 }
2843
hayeswangac718b62013-05-02 16:01:25 +00002844 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08002845 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2846 rtl_runtime_suspend_enable(tp, false);
2847 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2848 if (tp->speed & LINK_STATUS)
2849 tp->rtl_ops.disable(tp);
2850 } else {
2851 tp->rtl_ops.up(tp);
2852 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswang43779f82014-01-02 11:25:10 +08002853 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2854 DUPLEX_FULL);
hayeswang9a4be1b2014-02-18 21:49:07 +08002855 }
hayeswang40a82912013-08-14 20:54:40 +08002856 tp->speed = 0;
2857 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002858 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002859 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangac718b62013-05-02 16:01:25 +00002860 }
2861
2862 return 0;
2863}
2864
hayeswang21ff2e82014-02-18 21:49:06 +08002865static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2866{
2867 struct r8152 *tp = netdev_priv(dev);
2868
hayeswang9a4be1b2014-02-18 21:49:07 +08002869 if (usb_autopm_get_interface(tp->intf) < 0)
2870 return;
2871
hayeswang21ff2e82014-02-18 21:49:06 +08002872 wol->supported = WAKE_ANY;
2873 wol->wolopts = __rtl_get_wol(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08002874
2875 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08002876}
2877
2878static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2879{
2880 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002881 int ret;
2882
2883 ret = usb_autopm_get_interface(tp->intf);
2884 if (ret < 0)
2885 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08002886
2887 __rtl_set_wol(tp, wol->wolopts);
2888 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
2889
hayeswang9a4be1b2014-02-18 21:49:07 +08002890 usb_autopm_put_interface(tp->intf);
2891
2892out_set_wol:
2893 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08002894}
2895
hayeswanga5ec27c2014-02-18 21:49:11 +08002896static u32 rtl8152_get_msglevel(struct net_device *dev)
2897{
2898 struct r8152 *tp = netdev_priv(dev);
2899
2900 return tp->msg_enable;
2901}
2902
2903static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
2904{
2905 struct r8152 *tp = netdev_priv(dev);
2906
2907 tp->msg_enable = value;
2908}
2909
hayeswangac718b62013-05-02 16:01:25 +00002910static void rtl8152_get_drvinfo(struct net_device *netdev,
2911 struct ethtool_drvinfo *info)
2912{
2913 struct r8152 *tp = netdev_priv(netdev);
2914
2915 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
2916 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
2917 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
2918}
2919
2920static
2921int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
2922{
2923 struct r8152 *tp = netdev_priv(netdev);
2924
2925 if (!tp->mii.mdio_read)
2926 return -EOPNOTSUPP;
2927
2928 return mii_ethtool_gset(&tp->mii, cmd);
2929}
2930
2931static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2932{
2933 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002934 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002935
hayeswang9a4be1b2014-02-18 21:49:07 +08002936 ret = usb_autopm_get_interface(tp->intf);
2937 if (ret < 0)
2938 goto out;
2939
2940 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2941
2942 usb_autopm_put_interface(tp->intf);
2943
2944out:
2945 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002946}
2947
2948static struct ethtool_ops ops = {
2949 .get_drvinfo = rtl8152_get_drvinfo,
2950 .get_settings = rtl8152_get_settings,
2951 .set_settings = rtl8152_set_settings,
2952 .get_link = ethtool_op_get_link,
hayeswanga5ec27c2014-02-18 21:49:11 +08002953 .get_msglevel = rtl8152_get_msglevel,
2954 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08002955 .get_wol = rtl8152_get_wol,
2956 .set_wol = rtl8152_set_wol,
hayeswangac718b62013-05-02 16:01:25 +00002957};
2958
2959static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2960{
2961 struct r8152 *tp = netdev_priv(netdev);
2962 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08002963 int res;
2964
2965 res = usb_autopm_get_interface(tp->intf);
2966 if (res < 0)
2967 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002968
2969 switch (cmd) {
2970 case SIOCGMIIPHY:
2971 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2972 break;
2973
2974 case SIOCGMIIREG:
2975 data->val_out = r8152_mdio_read(tp, data->reg_num);
2976 break;
2977
2978 case SIOCSMIIREG:
2979 if (!capable(CAP_NET_ADMIN)) {
2980 res = -EPERM;
2981 break;
2982 }
2983 r8152_mdio_write(tp, data->reg_num, data->val_in);
2984 break;
2985
2986 default:
2987 res = -EOPNOTSUPP;
2988 }
2989
hayeswang9a4be1b2014-02-18 21:49:07 +08002990 usb_autopm_put_interface(tp->intf);
2991
2992out:
hayeswangac718b62013-05-02 16:01:25 +00002993 return res;
2994}
2995
2996static const struct net_device_ops rtl8152_netdev_ops = {
2997 .ndo_open = rtl8152_open,
2998 .ndo_stop = rtl8152_close,
2999 .ndo_do_ioctl = rtl8152_ioctl,
3000 .ndo_start_xmit = rtl8152_start_xmit,
3001 .ndo_tx_timeout = rtl8152_tx_timeout,
3002 .ndo_set_rx_mode = rtl8152_set_rx_mode,
3003 .ndo_set_mac_address = rtl8152_set_mac_address,
3004
3005 .ndo_change_mtu = eth_change_mtu,
3006 .ndo_validate_addr = eth_validate_addr,
3007};
3008
3009static void r8152b_get_version(struct r8152 *tp)
3010{
3011 u32 ocp_data;
3012 u16 version;
3013
3014 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3015 version = (u16)(ocp_data & VERSION_MASK);
3016
3017 switch (version) {
3018 case 0x4c00:
3019 tp->version = RTL_VER_01;
3020 break;
3021 case 0x4c10:
3022 tp->version = RTL_VER_02;
3023 break;
hayeswang43779f82014-01-02 11:25:10 +08003024 case 0x5c00:
3025 tp->version = RTL_VER_03;
3026 tp->mii.supports_gmii = 1;
3027 break;
3028 case 0x5c10:
3029 tp->version = RTL_VER_04;
3030 tp->mii.supports_gmii = 1;
3031 break;
3032 case 0x5c20:
3033 tp->version = RTL_VER_05;
3034 tp->mii.supports_gmii = 1;
3035 break;
hayeswangac718b62013-05-02 16:01:25 +00003036 default:
3037 netif_info(tp, probe, tp->netdev,
3038 "Unknown version 0x%04x\n", version);
3039 break;
3040 }
3041}
3042
hayeswange3fe0b12014-01-02 11:22:39 +08003043static void rtl8152_unload(struct r8152 *tp)
3044{
hayeswang00a5e362014-02-18 21:48:59 +08003045 if (tp->version != RTL_VER_01)
3046 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08003047}
3048
hayeswang43779f82014-01-02 11:25:10 +08003049static void rtl8153_unload(struct r8152 *tp)
3050{
hayeswangb9702722014-02-18 21:49:00 +08003051 r8153_power_cut_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003052}
3053
hayeswang31ca1de2014-01-06 17:08:43 +08003054static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
hayeswangc81229c2014-01-02 11:22:42 +08003055{
3056 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang31ca1de2014-01-06 17:08:43 +08003057 int ret = -ENODEV;
hayeswangc81229c2014-01-02 11:22:42 +08003058
3059 switch (id->idVendor) {
3060 case VENDOR_ID_REALTEK:
3061 switch (id->idProduct) {
3062 case PRODUCT_ID_RTL8152:
3063 ops->init = r8152b_init;
3064 ops->enable = rtl8152_enable;
3065 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003066 ops->up = r8152b_exit_oob;
hayeswangc81229c2014-01-02 11:22:42 +08003067 ops->down = rtl8152_down;
3068 ops->unload = rtl8152_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003069 ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08003070 break;
hayeswang43779f82014-01-02 11:25:10 +08003071 case PRODUCT_ID_RTL8153:
3072 ops->init = r8153_init;
3073 ops->enable = rtl8153_enable;
3074 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003075 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003076 ops->down = rtl8153_down;
3077 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003078 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003079 break;
3080 default:
hayeswang43779f82014-01-02 11:25:10 +08003081 break;
3082 }
3083 break;
3084
3085 case VENDOR_ID_SAMSUNG:
3086 switch (id->idProduct) {
3087 case PRODUCT_ID_SAMSUNG:
3088 ops->init = r8153_init;
3089 ops->enable = rtl8153_enable;
3090 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003091 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003092 ops->down = rtl8153_down;
3093 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003094 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003095 break;
hayeswangc81229c2014-01-02 11:22:42 +08003096 default:
hayeswangc81229c2014-01-02 11:22:42 +08003097 break;
3098 }
3099 break;
3100
3101 default:
hayeswangc81229c2014-01-02 11:22:42 +08003102 break;
3103 }
3104
hayeswang31ca1de2014-01-06 17:08:43 +08003105 if (ret)
3106 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
3107
hayeswangc81229c2014-01-02 11:22:42 +08003108 return ret;
3109}
3110
hayeswangac718b62013-05-02 16:01:25 +00003111static int rtl8152_probe(struct usb_interface *intf,
3112 const struct usb_device_id *id)
3113{
3114 struct usb_device *udev = interface_to_usbdev(intf);
3115 struct r8152 *tp;
3116 struct net_device *netdev;
hayeswangebc2ec42013-08-14 20:54:38 +08003117 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003118
hayeswangac718b62013-05-02 16:01:25 +00003119 netdev = alloc_etherdev(sizeof(struct r8152));
3120 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003121 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00003122 return -ENOMEM;
3123 }
3124
hayeswangebc2ec42013-08-14 20:54:38 +08003125 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00003126 tp = netdev_priv(netdev);
3127 tp->msg_enable = 0x7FFF;
3128
hayeswange3ad4122014-01-06 17:08:42 +08003129 tp->udev = udev;
3130 tp->netdev = netdev;
3131 tp->intf = intf;
3132
hayeswang31ca1de2014-01-06 17:08:43 +08003133 ret = rtl_ops_init(tp, id);
3134 if (ret)
3135 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08003136
hayeswangebc2ec42013-08-14 20:54:38 +08003137 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00003138 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3139
hayeswangac718b62013-05-02 16:01:25 +00003140 netdev->netdev_ops = &rtl8152_netdev_ops;
3141 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08003142
hayeswang565cab02014-03-07 11:04:38 +08003143 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
3144 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
hayeswangdb8515e2014-03-06 15:07:16 +08003145
hayeswangac718b62013-05-02 16:01:25 +00003146 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00003147
3148 tp->mii.dev = netdev;
3149 tp->mii.mdio_read = read_mii_word;
3150 tp->mii.mdio_write = write_mii_word;
3151 tp->mii.phy_id_mask = 0x3f;
3152 tp->mii.reg_num_mask = 0x1f;
3153 tp->mii.phy_id = R8152_PHY_ID;
3154 tp->mii.supports_gmii = 0;
3155
hayeswang9a4be1b2014-02-18 21:49:07 +08003156 intf->needs_remote_wakeup = 1;
3157
hayeswangac718b62013-05-02 16:01:25 +00003158 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08003159 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00003160 set_ethernet_addr(tp);
3161
hayeswangac718b62013-05-02 16:01:25 +00003162 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00003163
hayeswangebc2ec42013-08-14 20:54:38 +08003164 ret = register_netdev(netdev);
3165 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003166 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec42013-08-14 20:54:38 +08003167 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00003168 }
3169
hayeswang21ff2e82014-02-18 21:49:06 +08003170 tp->saved_wolopts = __rtl_get_wol(tp);
3171 if (tp->saved_wolopts)
3172 device_set_wakeup_enable(&udev->dev, true);
3173 else
3174 device_set_wakeup_enable(&udev->dev, false);
3175
Hayes Wang4a8deae2014-01-07 11:18:22 +08003176 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00003177
3178 return 0;
3179
hayeswangac718b62013-05-02 16:01:25 +00003180out1:
hayeswangebc2ec42013-08-14 20:54:38 +08003181 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00003182out:
3183 free_netdev(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08003184 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003185}
3186
hayeswangac718b62013-05-02 16:01:25 +00003187static void rtl8152_disconnect(struct usb_interface *intf)
3188{
3189 struct r8152 *tp = usb_get_intfdata(intf);
3190
3191 usb_set_intfdata(intf, NULL);
3192 if (tp) {
3193 set_bit(RTL8152_UNPLUG, &tp->flags);
3194 tasklet_kill(&tp->tl);
3195 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08003196 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00003197 free_netdev(tp->netdev);
3198 }
3199}
3200
3201/* table of devices that work with this driver */
3202static struct usb_device_id rtl8152_table[] = {
hayeswangc7de7de2014-01-15 10:42:16 +08003203 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
3204 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
3205 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00003206 {}
3207};
3208
3209MODULE_DEVICE_TABLE(usb, rtl8152_table);
3210
3211static struct usb_driver rtl8152_driver = {
3212 .name = MODULENAME,
hayeswangebc2ec42013-08-14 20:54:38 +08003213 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00003214 .probe = rtl8152_probe,
3215 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00003216 .suspend = rtl8152_suspend,
hayeswangebc2ec42013-08-14 20:54:38 +08003217 .resume = rtl8152_resume,
3218 .reset_resume = rtl8152_resume,
hayeswang9a4be1b2014-02-18 21:49:07 +08003219 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08003220 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00003221};
3222
Sachin Kamatb4236daa2013-05-16 17:48:08 +00003223module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00003224
3225MODULE_AUTHOR(DRIVER_AUTHOR);
3226MODULE_DESCRIPTION(DRIVER_DESC);
3227MODULE_LICENSE("GPL");