blob: 92b51c4c46f57b660f3053804ba74f6df9e71c76 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
4 */
5
6#include <linux/signal.h>
7#include <linux/slab.h>
8#include <linux/module.h>
9#include <linux/netdevice.h>
10#include <linux/etherdevice.h>
11#include <linux/mii.h>
12#include <linux/ethtool.h>
13#include <linux/usb.h>
14#include <linux/crc32.h>
15#include <linux/if_vlan.h>
16#include <linux/uaccess.h>
17#include <linux/list.h>
18#include <linux/ip.h>
19#include <linux/ipv6.h>
20#include <net/ip6_checksum.h>
21#include <uapi/linux/mdio.h>
22#include <linux/mdio.h>
23#include <linux/usb/cdc.h>
24#include <linux/suspend.h>
25#include <linux/atomic.h>
26#include <linux/acpi.h>
27
28/* Information for net-next */
29#define NETNEXT_VERSION "10"
30
31/* Information for net */
32#define NET_VERSION "11"
33
34#define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
35#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
36#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
37#define MODULENAME "r8152"
38
39#define R8152_PHY_ID 32
40
41#define PLA_IDR 0xc000
42#define PLA_RCR 0xc010
43#define PLA_RMS 0xc016
44#define PLA_RXFIFO_CTRL0 0xc0a0
45#define PLA_RXFIFO_CTRL1 0xc0a4
46#define PLA_RXFIFO_CTRL2 0xc0a8
47#define PLA_DMY_REG0 0xc0b0
48#define PLA_FMC 0xc0b4
49#define PLA_CFG_WOL 0xc0b6
50#define PLA_TEREDO_CFG 0xc0bc
51#define PLA_TEREDO_WAKE_BASE 0xc0c4
52#define PLA_MAR 0xcd00
53#define PLA_BACKUP 0xd000
54#define PLA_BDC_CR 0xd1a0
55#define PLA_TEREDO_TIMER 0xd2cc
56#define PLA_REALWOW_TIMER 0xd2e8
57#define PLA_SUSPEND_FLAG 0xd38a
58#define PLA_INDICATE_FALG 0xd38c
59#define PLA_EXTRA_STATUS 0xd398
60#define PLA_EFUSE_DATA 0xdd00
61#define PLA_EFUSE_CMD 0xdd02
62#define PLA_LEDSEL 0xdd90
63#define PLA_LED_FEATURE 0xdd92
64#define PLA_PHYAR 0xde00
65#define PLA_BOOT_CTRL 0xe004
66#define PLA_LWAKE_CTRL_REG 0xe007
67#define PLA_GPHY_INTR_IMR 0xe022
68#define PLA_EEE_CR 0xe040
69#define PLA_EEEP_CR 0xe080
70#define PLA_MAC_PWR_CTRL 0xe0c0
71#define PLA_MAC_PWR_CTRL2 0xe0ca
72#define PLA_MAC_PWR_CTRL3 0xe0cc
73#define PLA_MAC_PWR_CTRL4 0xe0ce
74#define PLA_WDT6_CTRL 0xe428
75#define PLA_TCR0 0xe610
76#define PLA_TCR1 0xe612
77#define PLA_MTPS 0xe615
78#define PLA_TXFIFO_CTRL 0xe618
79#define PLA_RSTTALLY 0xe800
80#define PLA_CR 0xe813
81#define PLA_CRWECR 0xe81c
82#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
83#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
84#define PLA_CONFIG5 0xe822
85#define PLA_PHY_PWR 0xe84c
86#define PLA_OOB_CTRL 0xe84f
87#define PLA_CPCR 0xe854
88#define PLA_MISC_0 0xe858
89#define PLA_MISC_1 0xe85a
90#define PLA_OCP_GPHY_BASE 0xe86c
91#define PLA_TALLYCNT 0xe890
92#define PLA_SFF_STS_7 0xe8de
93#define PLA_PHYSTATUS 0xe908
94#define PLA_CONFIG6 0xe90a /* CONFIG6 */
95#define PLA_BP_BA 0xfc26
96#define PLA_BP_0 0xfc28
97#define PLA_BP_1 0xfc2a
98#define PLA_BP_2 0xfc2c
99#define PLA_BP_3 0xfc2e
100#define PLA_BP_4 0xfc30
101#define PLA_BP_5 0xfc32
102#define PLA_BP_6 0xfc34
103#define PLA_BP_7 0xfc36
104#define PLA_BP_EN 0xfc38
105
106#define USB_USB2PHY 0xb41e
107#define USB_SSPHYLINK1 0xb426
108#define USB_SSPHYLINK2 0xb428
109#define USB_U2P3_CTRL 0xb460
110#define USB_CSR_DUMMY1 0xb464
111#define USB_CSR_DUMMY2 0xb466
112#define USB_DEV_STAT 0xb808
113#define USB_CONNECT_TIMER 0xcbf8
114#define USB_MSC_TIMER 0xcbfc
115#define USB_BURST_SIZE 0xcfc0
116#define USB_LPM_CONFIG 0xcfd8
117#define USB_USB_CTRL 0xd406
118#define USB_PHY_CTRL 0xd408
119#define USB_TX_AGG 0xd40a
120#define USB_RX_BUF_TH 0xd40c
121#define USB_USB_TIMER 0xd428
122#define USB_RX_EARLY_TIMEOUT 0xd42c
123#define USB_RX_EARLY_SIZE 0xd42e
124#define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
125#define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
126#define USB_TX_DMA 0xd434
127#define USB_UPT_RXDMA_OWN 0xd437
128#define USB_TOLERANCE 0xd490
129#define USB_LPM_CTRL 0xd41a
130#define USB_BMU_RESET 0xd4b0
131#define USB_U1U2_TIMER 0xd4da
132#define USB_UPS_CTRL 0xd800
133#define USB_POWER_CUT 0xd80a
134#define USB_MISC_0 0xd81a
135#define USB_MISC_1 0xd81f
136#define USB_AFE_CTRL2 0xd824
137#define USB_UPS_CFG 0xd842
138#define USB_UPS_FLAGS 0xd848
139#define USB_WDT11_CTRL 0xe43c
140#define USB_BP_BA 0xfc26
141#define USB_BP_0 0xfc28
142#define USB_BP_1 0xfc2a
143#define USB_BP_2 0xfc2c
144#define USB_BP_3 0xfc2e
145#define USB_BP_4 0xfc30
146#define USB_BP_5 0xfc32
147#define USB_BP_6 0xfc34
148#define USB_BP_7 0xfc36
149#define USB_BP_EN 0xfc38
150#define USB_BP_8 0xfc38
151#define USB_BP_9 0xfc3a
152#define USB_BP_10 0xfc3c
153#define USB_BP_11 0xfc3e
154#define USB_BP_12 0xfc40
155#define USB_BP_13 0xfc42
156#define USB_BP_14 0xfc44
157#define USB_BP_15 0xfc46
158#define USB_BP2_EN 0xfc48
159
160/* OCP Registers */
161#define OCP_ALDPS_CONFIG 0x2010
162#define OCP_EEE_CONFIG1 0x2080
163#define OCP_EEE_CONFIG2 0x2092
164#define OCP_EEE_CONFIG3 0x2094
165#define OCP_BASE_MII 0xa400
166#define OCP_EEE_AR 0xa41a
167#define OCP_EEE_DATA 0xa41c
168#define OCP_PHY_STATUS 0xa420
169#define OCP_NCTL_CFG 0xa42c
170#define OCP_POWER_CFG 0xa430
171#define OCP_EEE_CFG 0xa432
172#define OCP_SRAM_ADDR 0xa436
173#define OCP_SRAM_DATA 0xa438
174#define OCP_DOWN_SPEED 0xa442
175#define OCP_EEE_ABLE 0xa5c4
176#define OCP_EEE_ADV 0xa5d0
177#define OCP_EEE_LPABLE 0xa5d2
178#define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
179#define OCP_PHY_PATCH_STAT 0xb800
180#define OCP_PHY_PATCH_CMD 0xb820
181#define OCP_ADC_IOFFSET 0xbcfc
182#define OCP_ADC_CFG 0xbc06
183#define OCP_SYSCLK_CFG 0xc416
184
185/* SRAM Register */
186#define SRAM_GREEN_CFG 0x8011
187#define SRAM_LPF_CFG 0x8012
188#define SRAM_10M_AMP1 0x8080
189#define SRAM_10M_AMP2 0x8082
190#define SRAM_IMPEDANCE 0x8084
191
192/* PLA_RCR */
193#define RCR_AAP 0x00000001
194#define RCR_APM 0x00000002
195#define RCR_AM 0x00000004
196#define RCR_AB 0x00000008
197#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
198
199/* PLA_RXFIFO_CTRL0 */
200#define RXFIFO_THR1_NORMAL 0x00080002
201#define RXFIFO_THR1_OOB 0x01800003
202
203/* PLA_RXFIFO_CTRL1 */
204#define RXFIFO_THR2_FULL 0x00000060
205#define RXFIFO_THR2_HIGH 0x00000038
206#define RXFIFO_THR2_OOB 0x0000004a
207#define RXFIFO_THR2_NORMAL 0x00a0
208
209/* PLA_RXFIFO_CTRL2 */
210#define RXFIFO_THR3_FULL 0x00000078
211#define RXFIFO_THR3_HIGH 0x00000048
212#define RXFIFO_THR3_OOB 0x0000005a
213#define RXFIFO_THR3_NORMAL 0x0110
214
215/* PLA_TXFIFO_CTRL */
216#define TXFIFO_THR_NORMAL 0x00400008
217#define TXFIFO_THR_NORMAL2 0x01000008
218
219/* PLA_DMY_REG0 */
220#define ECM_ALDPS 0x0002
221
222/* PLA_FMC */
223#define FMC_FCR_MCU_EN 0x0001
224
225/* PLA_EEEP_CR */
226#define EEEP_CR_EEEP_TX 0x0002
227
228/* PLA_WDT6_CTRL */
229#define WDT6_SET_MODE 0x0010
230
231/* PLA_TCR0 */
232#define TCR0_TX_EMPTY 0x0800
233#define TCR0_AUTO_FIFO 0x0080
234
235/* PLA_TCR1 */
236#define VERSION_MASK 0x7cf0
237
238/* PLA_MTPS */
239#define MTPS_JUMBO (12 * 1024 / 64)
240#define MTPS_DEFAULT (6 * 1024 / 64)
241
242/* PLA_RSTTALLY */
243#define TALLY_RESET 0x0001
244
245/* PLA_CR */
246#define CR_RST 0x10
247#define CR_RE 0x08
248#define CR_TE 0x04
249
250/* PLA_CRWECR */
251#define CRWECR_NORAML 0x00
252#define CRWECR_CONFIG 0xc0
253
254/* PLA_OOB_CTRL */
255#define NOW_IS_OOB 0x80
256#define TXFIFO_EMPTY 0x20
257#define RXFIFO_EMPTY 0x10
258#define LINK_LIST_READY 0x02
259#define DIS_MCU_CLROOB 0x01
260#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
261
262/* PLA_MISC_1 */
263#define RXDY_GATED_EN 0x0008
264
265/* PLA_SFF_STS_7 */
266#define RE_INIT_LL 0x8000
267#define MCU_BORW_EN 0x4000
268
269/* PLA_CPCR */
270#define CPCR_RX_VLAN 0x0040
271
272/* PLA_CFG_WOL */
273#define MAGIC_EN 0x0001
274
275/* PLA_TEREDO_CFG */
276#define TEREDO_SEL 0x8000
277#define TEREDO_WAKE_MASK 0x7f00
278#define TEREDO_RS_EVENT_MASK 0x00fe
279#define OOB_TEREDO_EN 0x0001
280
281/* PLA_BDC_CR */
282#define ALDPS_PROXY_MODE 0x0001
283
284/* PLA_EFUSE_CMD */
285#define EFUSE_READ_CMD BIT(15)
286#define EFUSE_DATA_BIT16 BIT(7)
287
288/* PLA_CONFIG34 */
289#define LINK_ON_WAKE_EN 0x0010
290#define LINK_OFF_WAKE_EN 0x0008
291
292/* PLA_CONFIG6 */
293#define LANWAKE_CLR_EN BIT(0)
294
295/* PLA_CONFIG5 */
296#define BWF_EN 0x0040
297#define MWF_EN 0x0020
298#define UWF_EN 0x0010
299#define LAN_WAKE_EN 0x0002
300
301/* PLA_LED_FEATURE */
302#define LED_MODE_MASK 0x0700
303
304/* PLA_PHY_PWR */
305#define TX_10M_IDLE_EN 0x0080
306#define PFM_PWM_SWITCH 0x0040
307#define TEST_IO_OFF BIT(4)
308
309/* PLA_MAC_PWR_CTRL */
310#define D3_CLK_GATED_EN 0x00004000
311#define MCU_CLK_RATIO 0x07010f07
312#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
313#define ALDPS_SPDWN_RATIO 0x0f87
314
315/* PLA_MAC_PWR_CTRL2 */
316#define EEE_SPDWN_RATIO 0x8007
317#define MAC_CLK_SPDWN_EN BIT(15)
318
319/* PLA_MAC_PWR_CTRL3 */
320#define PLA_MCU_SPDWN_EN BIT(14)
321#define PKT_AVAIL_SPDWN_EN 0x0100
322#define SUSPEND_SPDWN_EN 0x0004
323#define U1U2_SPDWN_EN 0x0002
324#define L1_SPDWN_EN 0x0001
325
326/* PLA_MAC_PWR_CTRL4 */
327#define PWRSAVE_SPDWN_EN 0x1000
328#define RXDV_SPDWN_EN 0x0800
329#define TX10MIDLE_EN 0x0100
330#define TP100_SPDWN_EN 0x0020
331#define TP500_SPDWN_EN 0x0010
332#define TP1000_SPDWN_EN 0x0008
333#define EEE_SPDWN_EN 0x0001
334
335/* PLA_GPHY_INTR_IMR */
336#define GPHY_STS_MSK 0x0001
337#define SPEED_DOWN_MSK 0x0002
338#define SPDWN_RXDV_MSK 0x0004
339#define SPDWN_LINKCHG_MSK 0x0008
340
341/* PLA_PHYAR */
342#define PHYAR_FLAG 0x80000000
343
344/* PLA_EEE_CR */
345#define EEE_RX_EN 0x0001
346#define EEE_TX_EN 0x0002
347
348/* PLA_BOOT_CTRL */
349#define AUTOLOAD_DONE 0x0002
350
351/* PLA_LWAKE_CTRL_REG */
352#define LANWAKE_PIN BIT(7)
353
354/* PLA_SUSPEND_FLAG */
355#define LINK_CHG_EVENT BIT(0)
356
357/* PLA_INDICATE_FALG */
358#define UPCOMING_RUNTIME_D3 BIT(0)
359
360/* PLA_EXTRA_STATUS */
361#define LINK_CHANGE_FLAG BIT(8)
362
363/* USB_USB2PHY */
364#define USB2PHY_SUSPEND 0x0001
365#define USB2PHY_L1 0x0002
366
367/* USB_SSPHYLINK1 */
368#define DELAY_PHY_PWR_CHG BIT(1)
369
370/* USB_SSPHYLINK2 */
371#define pwd_dn_scale_mask 0x3ffe
372#define pwd_dn_scale(x) ((x) << 1)
373
374/* USB_CSR_DUMMY1 */
375#define DYNAMIC_BURST 0x0001
376
377/* USB_CSR_DUMMY2 */
378#define EP4_FULL_FC 0x0001
379
380/* USB_DEV_STAT */
381#define STAT_SPEED_MASK 0x0006
382#define STAT_SPEED_HIGH 0x0000
383#define STAT_SPEED_FULL 0x0002
384
385/* USB_LPM_CONFIG */
386#define LPM_U1U2_EN BIT(0)
387
388/* USB_TX_AGG */
389#define TX_AGG_MAX_THRESHOLD 0x03
390
391/* USB_RX_BUF_TH */
392#define RX_THR_SUPPER 0x0c350180
393#define RX_THR_HIGH 0x7a120180
394#define RX_THR_SLOW 0xffff0180
395#define RX_THR_B 0x00010001
396
397/* USB_TX_DMA */
398#define TEST_MODE_DISABLE 0x00000001
399#define TX_SIZE_ADJUST1 0x00000100
400
401/* USB_BMU_RESET */
402#define BMU_RESET_EP_IN 0x01
403#define BMU_RESET_EP_OUT 0x02
404
405/* USB_UPT_RXDMA_OWN */
406#define OWN_UPDATE BIT(0)
407#define OWN_CLEAR BIT(1)
408
409/* USB_UPS_CTRL */
410#define POWER_CUT 0x0100
411
412/* USB_PM_CTRL_STATUS */
413#define RESUME_INDICATE 0x0001
414
415/* USB_USB_CTRL */
416#define RX_AGG_DISABLE 0x0010
417#define RX_ZERO_EN 0x0080
418
419/* USB_U2P3_CTRL */
420#define U2P3_ENABLE 0x0001
421
422/* USB_POWER_CUT */
423#define PWR_EN 0x0001
424#define PHASE2_EN 0x0008
425#define UPS_EN BIT(4)
426#define USP_PREWAKE BIT(5)
427
428/* USB_MISC_0 */
429#define PCUT_STATUS 0x0001
430
431/* USB_RX_EARLY_TIMEOUT */
432#define COALESCE_SUPER 85000U
433#define COALESCE_HIGH 250000U
434#define COALESCE_SLOW 524280U
435
436/* USB_WDT11_CTRL */
437#define TIMER11_EN 0x0001
438
439/* USB_LPM_CTRL */
440/* bit 4 ~ 5: fifo empty boundary */
441#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
442/* bit 2 ~ 3: LMP timer */
443#define LPM_TIMER_MASK 0x0c
444#define LPM_TIMER_500MS 0x04 /* 500 ms */
445#define LPM_TIMER_500US 0x0c /* 500 us */
446#define ROK_EXIT_LPM 0x02
447
448/* USB_AFE_CTRL2 */
449#define SEN_VAL_MASK 0xf800
450#define SEN_VAL_NORMAL 0xa000
451#define SEL_RXIDLE 0x0100
452
453/* USB_UPS_CFG */
454#define SAW_CNT_1MS_MASK 0x0fff
455
456/* USB_UPS_FLAGS */
457#define UPS_FLAGS_R_TUNE BIT(0)
458#define UPS_FLAGS_EN_10M_CKDIV BIT(1)
459#define UPS_FLAGS_250M_CKDIV BIT(2)
460#define UPS_FLAGS_EN_ALDPS BIT(3)
461#define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
462#define ups_flags_speed(x) ((x) << 16)
463#define UPS_FLAGS_EN_EEE BIT(20)
464#define UPS_FLAGS_EN_500M_EEE BIT(21)
465#define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
466#define UPS_FLAGS_EEE_PLLOFF_100 BIT(23)
467#define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
468#define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
469#define UPS_FLAGS_EN_GREEN BIT(26)
470#define UPS_FLAGS_EN_FLOW_CTR BIT(27)
471
472enum spd_duplex {
473 NWAY_10M_HALF,
474 NWAY_10M_FULL,
475 NWAY_100M_HALF,
476 NWAY_100M_FULL,
477 NWAY_1000M_FULL,
478 FORCE_10M_HALF,
479 FORCE_10M_FULL,
480 FORCE_100M_HALF,
481 FORCE_100M_FULL,
482};
483
484/* OCP_ALDPS_CONFIG */
485#define ENPWRSAVE 0x8000
486#define ENPDNPS 0x0200
487#define LINKENA 0x0100
488#define DIS_SDSAVE 0x0010
489
490/* OCP_PHY_STATUS */
491#define PHY_STAT_MASK 0x0007
492#define PHY_STAT_EXT_INIT 2
493#define PHY_STAT_LAN_ON 3
494#define PHY_STAT_PWRDN 5
495
496/* OCP_NCTL_CFG */
497#define PGA_RETURN_EN BIT(1)
498
499/* OCP_POWER_CFG */
500#define EEE_CLKDIV_EN 0x8000
501#define EN_ALDPS 0x0004
502#define EN_10M_PLLOFF 0x0001
503
504/* OCP_EEE_CONFIG1 */
505#define RG_TXLPI_MSK_HFDUP 0x8000
506#define RG_MATCLR_EN 0x4000
507#define EEE_10_CAP 0x2000
508#define EEE_NWAY_EN 0x1000
509#define TX_QUIET_EN 0x0200
510#define RX_QUIET_EN 0x0100
511#define sd_rise_time_mask 0x0070
512#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
513#define RG_RXLPI_MSK_HFDUP 0x0008
514#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
515
516/* OCP_EEE_CONFIG2 */
517#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
518#define RG_DACQUIET_EN 0x0400
519#define RG_LDVQUIET_EN 0x0200
520#define RG_CKRSEL 0x0020
521#define RG_EEEPRG_EN 0x0010
522
523/* OCP_EEE_CONFIG3 */
524#define fast_snr_mask 0xff80
525#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
526#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
527#define MSK_PH 0x0006 /* bit 0 ~ 3 */
528
529/* OCP_EEE_AR */
530/* bit[15:14] function */
531#define FUN_ADDR 0x0000
532#define FUN_DATA 0x4000
533/* bit[4:0] device addr */
534
535/* OCP_EEE_CFG */
536#define CTAP_SHORT_EN 0x0040
537#define EEE10_EN 0x0010
538
539/* OCP_DOWN_SPEED */
540#define EN_EEE_CMODE BIT(14)
541#define EN_EEE_1000 BIT(13)
542#define EN_EEE_100 BIT(12)
543#define EN_10M_CLKDIV BIT(11)
544#define EN_10M_BGOFF 0x0080
545
546/* OCP_PHY_STATE */
547#define TXDIS_STATE 0x01
548#define ABD_STATE 0x02
549
550/* OCP_PHY_PATCH_STAT */
551#define PATCH_READY BIT(6)
552
553/* OCP_PHY_PATCH_CMD */
554#define PATCH_REQUEST BIT(4)
555
556/* OCP_ADC_CFG */
557#define CKADSEL_L 0x0100
558#define ADC_EN 0x0080
559#define EN_EMI_L 0x0040
560
561/* OCP_SYSCLK_CFG */
562#define clk_div_expo(x) (min(x, 5) << 8)
563
564/* SRAM_GREEN_CFG */
565#define GREEN_ETH_EN BIT(15)
566#define R_TUNE_EN BIT(11)
567
568/* SRAM_LPF_CFG */
569#define LPF_AUTO_TUNE 0x8000
570
571/* SRAM_10M_AMP1 */
572#define GDAC_IB_UPALL 0x0008
573
574/* SRAM_10M_AMP2 */
575#define AMP_DN 0x0200
576
577/* SRAM_IMPEDANCE */
578#define RX_DRIVING_MASK 0x6000
579
580/* MAC PASSTHRU */
581#define AD_MASK 0xfee0
582#define BND_MASK 0x0004
583#define BD_MASK 0x0001
584#define EFUSE 0xcfdb
585#define PASS_THRU_MASK 0x1
586
587enum rtl_register_content {
588 _1000bps = 0x10,
589 _100bps = 0x08,
590 _10bps = 0x04,
591 LINK_STATUS = 0x02,
592 FULL_DUP = 0x01,
593};
594
595#define RTL8152_MAX_TX 4
596#define RTL8152_MAX_RX 10
597#define INTBUFSIZE 2
598#define TX_ALIGN 4
599#define RX_ALIGN 8
600
601#define RTL8152_RX_MAX_PENDING 4096
602#define RTL8152_RXFG_HEADSZ 256
603
604#define INTR_LINK 0x0004
605
606#define RTL8152_REQT_READ 0xc0
607#define RTL8152_REQT_WRITE 0x40
608#define RTL8152_REQ_GET_REGS 0x05
609#define RTL8152_REQ_SET_REGS 0x05
610
611#define BYTE_EN_DWORD 0xff
612#define BYTE_EN_WORD 0x33
613#define BYTE_EN_BYTE 0x11
614#define BYTE_EN_SIX_BYTES 0x3f
615#define BYTE_EN_START_MASK 0x0f
616#define BYTE_EN_END_MASK 0xf0
617
618#define RTL8153_MAX_PACKET 9216 /* 9K */
619#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
620 ETH_FCS_LEN)
621#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
622#define RTL8153_RMS RTL8153_MAX_PACKET
623#define RTL8152_TX_TIMEOUT (5 * HZ)
624#define RTL8152_NAPI_WEIGHT 64
625#define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
626 sizeof(struct rx_desc) + RX_ALIGN)
627
628/* rtl8152 flags */
629enum rtl8152_flags {
630 RTL8152_UNPLUG = 0,
631 RTL8152_SET_RX_MODE,
632 WORK_ENABLE,
633 RTL8152_LINK_CHG,
634 SELECTIVE_SUSPEND,
635 PHY_RESET,
636 SCHEDULE_TASKLET,
637 GREEN_ETHERNET,
638 DELL_TB_RX_AGG_BUG,
639};
640
641/* Define these values to match your device */
642#define VENDOR_ID_REALTEK 0x0bda
643#define VENDOR_ID_MICROSOFT 0x045e
644#define VENDOR_ID_SAMSUNG 0x04e8
645#define VENDOR_ID_LENOVO 0x17ef
646#define VENDOR_ID_LINKSYS 0x13b1
647#define VENDOR_ID_NVIDIA 0x0955
648#define VENDOR_ID_TPLINK 0x2357
649
650#define MCU_TYPE_PLA 0x0100
651#define MCU_TYPE_USB 0x0000
652
653struct tally_counter {
654 __le64 tx_packets;
655 __le64 rx_packets;
656 __le64 tx_errors;
657 __le32 rx_errors;
658 __le16 rx_missed;
659 __le16 align_errors;
660 __le32 tx_one_collision;
661 __le32 tx_multi_collision;
662 __le64 rx_unicast;
663 __le64 rx_broadcast;
664 __le32 rx_multicast;
665 __le16 tx_aborted;
666 __le16 tx_underrun;
667};
668
669struct rx_desc {
670 __le32 opts1;
671#define RX_LEN_MASK 0x7fff
672
673 __le32 opts2;
674#define RD_UDP_CS BIT(23)
675#define RD_TCP_CS BIT(22)
676#define RD_IPV6_CS BIT(20)
677#define RD_IPV4_CS BIT(19)
678
679 __le32 opts3;
680#define IPF BIT(23) /* IP checksum fail */
681#define UDPF BIT(22) /* UDP checksum fail */
682#define TCPF BIT(21) /* TCP checksum fail */
683#define RX_VLAN_TAG BIT(16)
684
685 __le32 opts4;
686 __le32 opts5;
687 __le32 opts6;
688};
689
690struct tx_desc {
691 __le32 opts1;
692#define TX_FS BIT(31) /* First segment of a packet */
693#define TX_LS BIT(30) /* Final segment of a packet */
694#define GTSENDV4 BIT(28)
695#define GTSENDV6 BIT(27)
696#define GTTCPHO_SHIFT 18
697#define GTTCPHO_MAX 0x7fU
698#define TX_LEN_MAX 0x3ffffU
699
700 __le32 opts2;
701#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
702#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
703#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
704#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
705#define MSS_SHIFT 17
706#define MSS_MAX 0x7ffU
707#define TCPHO_SHIFT 17
708#define TCPHO_MAX 0x7ffU
709#define TX_VLAN_TAG BIT(16)
710};
711
712struct r8152;
713
714struct rx_agg {
715 struct list_head list, info_list;
716 struct urb *urb;
717 struct r8152 *context;
718 struct page *page;
719 void *buffer;
720};
721
722struct tx_agg {
723 struct list_head list;
724 struct urb *urb;
725 struct r8152 *context;
726 void *buffer;
727 void *head;
728 u32 skb_num;
729 u32 skb_len;
730};
731
732struct r8152 {
733 unsigned long flags;
734 struct usb_device *udev;
735 struct napi_struct napi;
736 struct usb_interface *intf;
737 struct net_device *netdev;
738 struct urb *intr_urb;
739 struct tx_agg tx_info[RTL8152_MAX_TX];
740 struct list_head rx_info, rx_used;
741 struct list_head rx_done, tx_free;
742 struct sk_buff_head tx_queue, rx_queue;
743 spinlock_t rx_lock, tx_lock;
744 struct delayed_work schedule, hw_phy_work;
745 struct mii_if_info mii;
746 struct mutex control; /* use for hw setting */
747#ifdef CONFIG_PM_SLEEP
748 struct notifier_block pm_notifier;
749#endif
750 struct tasklet_struct tx_tl;
751
752 struct rtl_ops {
753 void (*init)(struct r8152 *);
754 int (*enable)(struct r8152 *);
755 void (*disable)(struct r8152 *);
756 void (*up)(struct r8152 *);
757 void (*down)(struct r8152 *);
758 void (*unload)(struct r8152 *);
759 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
760 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
761 bool (*in_nway)(struct r8152 *);
762 void (*hw_phy_cfg)(struct r8152 *);
763 void (*autosuspend_en)(struct r8152 *tp, bool enable);
764 } rtl_ops;
765
766 struct ups_info {
767 u32 _10m_ckdiv:1;
768 u32 _250m_ckdiv:1;
769 u32 aldps:1;
770 u32 lite_mode:2;
771 u32 speed_duplex:4;
772 u32 eee:1;
773 u32 eee_lite:1;
774 u32 eee_ckdiv:1;
775 u32 eee_plloff_100:1;
776 u32 eee_plloff_giga:1;
777 u32 eee_cmod_lv:1;
778 u32 green:1;
779 u32 flow_control:1;
780 u32 ctap_short_off:1;
781 } ups_info;
782
783 atomic_t rx_count;
784
785 bool eee_en;
786 int intr_interval;
787 u32 saved_wolopts;
788 u32 msg_enable;
789 u32 tx_qlen;
790 u32 coalesce;
791 u32 advertising;
792 u32 rx_buf_sz;
793 u32 rx_copybreak;
794 u32 rx_pending;
795
796 u16 ocp_base;
797 u16 speed;
798 u16 eee_adv;
799 u8 *intr_buff;
800 u8 version;
801 u8 duplex;
802 u8 autoneg;
803};
804
805enum rtl_version {
806 RTL_VER_UNKNOWN = 0,
807 RTL_VER_01,
808 RTL_VER_02,
809 RTL_VER_03,
810 RTL_VER_04,
811 RTL_VER_05,
812 RTL_VER_06,
813 RTL_VER_07,
814 RTL_VER_08,
815 RTL_VER_09,
816 RTL_VER_MAX
817};
818
819enum tx_csum_stat {
820 TX_CSUM_SUCCESS = 0,
821 TX_CSUM_TSO,
822 TX_CSUM_NONE
823};
824
825#define RTL_ADVERTISED_10_HALF BIT(0)
826#define RTL_ADVERTISED_10_FULL BIT(1)
827#define RTL_ADVERTISED_100_HALF BIT(2)
828#define RTL_ADVERTISED_100_FULL BIT(3)
829#define RTL_ADVERTISED_1000_HALF BIT(4)
830#define RTL_ADVERTISED_1000_FULL BIT(5)
831
832/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
833 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
834 */
835static const int multicast_filter_limit = 32;
836static unsigned int agg_buf_sz = 16384;
837
838#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
839 VLAN_ETH_HLEN - ETH_FCS_LEN)
840
841static
842int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
843{
844 int ret;
845 void *tmp;
846
847 tmp = kmalloc(size, GFP_KERNEL);
848 if (!tmp)
849 return -ENOMEM;
850
851 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
852 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
853 value, index, tmp, size, USB_CTRL_GET_TIMEOUT);
854 if (ret < 0)
855 memset(data, 0xff, size);
856 else
857 memcpy(data, tmp, size);
858
859 kfree(tmp);
860
861 return ret;
862}
863
864static
865int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
866{
867 int ret;
868 void *tmp;
869
870 tmp = kmemdup(data, size, GFP_KERNEL);
871 if (!tmp)
872 return -ENOMEM;
873
874 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
875 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
876 value, index, tmp, size, USB_CTRL_SET_TIMEOUT);
877
878 kfree(tmp);
879
880 return ret;
881}
882
883static void rtl_set_unplug(struct r8152 *tp)
884{
885 if (tp->udev->state == USB_STATE_NOTATTACHED) {
886 set_bit(RTL8152_UNPLUG, &tp->flags);
887 smp_mb__after_atomic();
888 }
889}
890
891static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
892 void *data, u16 type)
893{
894 u16 limit = 64;
895 int ret = 0;
896
897 if (test_bit(RTL8152_UNPLUG, &tp->flags))
898 return -ENODEV;
899
900 /* both size and indix must be 4 bytes align */
901 if ((size & 3) || !size || (index & 3) || !data)
902 return -EPERM;
903
904 if ((u32)index + (u32)size > 0xffff)
905 return -EPERM;
906
907 while (size) {
908 if (size > limit) {
909 ret = get_registers(tp, index, type, limit, data);
910 if (ret < 0)
911 break;
912
913 index += limit;
914 data += limit;
915 size -= limit;
916 } else {
917 ret = get_registers(tp, index, type, size, data);
918 if (ret < 0)
919 break;
920
921 index += size;
922 data += size;
923 size = 0;
924 break;
925 }
926 }
927
928 if (ret == -ENODEV)
929 rtl_set_unplug(tp);
930
931 return ret;
932}
933
934static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
935 u16 size, void *data, u16 type)
936{
937 int ret;
938 u16 byteen_start, byteen_end, byen;
939 u16 limit = 512;
940
941 if (test_bit(RTL8152_UNPLUG, &tp->flags))
942 return -ENODEV;
943
944 /* both size and indix must be 4 bytes align */
945 if ((size & 3) || !size || (index & 3) || !data)
946 return -EPERM;
947
948 if ((u32)index + (u32)size > 0xffff)
949 return -EPERM;
950
951 byteen_start = byteen & BYTE_EN_START_MASK;
952 byteen_end = byteen & BYTE_EN_END_MASK;
953
954 byen = byteen_start | (byteen_start << 4);
955 ret = set_registers(tp, index, type | byen, 4, data);
956 if (ret < 0)
957 goto error1;
958
959 index += 4;
960 data += 4;
961 size -= 4;
962
963 if (size) {
964 size -= 4;
965
966 while (size) {
967 if (size > limit) {
968 ret = set_registers(tp, index,
969 type | BYTE_EN_DWORD,
970 limit, data);
971 if (ret < 0)
972 goto error1;
973
974 index += limit;
975 data += limit;
976 size -= limit;
977 } else {
978 ret = set_registers(tp, index,
979 type | BYTE_EN_DWORD,
980 size, data);
981 if (ret < 0)
982 goto error1;
983
984 index += size;
985 data += size;
986 size = 0;
987 break;
988 }
989 }
990
991 byen = byteen_end | (byteen_end >> 4);
992 ret = set_registers(tp, index, type | byen, 4, data);
993 if (ret < 0)
994 goto error1;
995 }
996
997error1:
998 if (ret == -ENODEV)
999 rtl_set_unplug(tp);
1000
1001 return ret;
1002}
1003
1004static inline
1005int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
1006{
1007 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
1008}
1009
1010static inline
1011int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1012{
1013 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
1014}
1015
1016static inline
1017int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1018{
1019 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
1020}
1021
1022static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
1023{
1024 __le32 data;
1025
1026 generic_ocp_read(tp, index, sizeof(data), &data, type);
1027
1028 return __le32_to_cpu(data);
1029}
1030
1031static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
1032{
1033 __le32 tmp = __cpu_to_le32(data);
1034
1035 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
1036}
1037
1038static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
1039{
1040 u32 data;
1041 __le32 tmp;
1042 u16 byen = BYTE_EN_WORD;
1043 u8 shift = index & 2;
1044
1045 index &= ~3;
1046 byen <<= shift;
1047
1048 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
1049
1050 data = __le32_to_cpu(tmp);
1051 data >>= (shift * 8);
1052 data &= 0xffff;
1053
1054 return (u16)data;
1055}
1056
1057static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1058{
1059 u32 mask = 0xffff;
1060 __le32 tmp;
1061 u16 byen = BYTE_EN_WORD;
1062 u8 shift = index & 2;
1063
1064 data &= mask;
1065
1066 if (index & 2) {
1067 byen <<= shift;
1068 mask <<= (shift * 8);
1069 data <<= (shift * 8);
1070 index &= ~3;
1071 }
1072
1073 tmp = __cpu_to_le32(data);
1074
1075 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1076}
1077
1078static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1079{
1080 u32 data;
1081 __le32 tmp;
1082 u8 shift = index & 3;
1083
1084 index &= ~3;
1085
1086 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
1087
1088 data = __le32_to_cpu(tmp);
1089 data >>= (shift * 8);
1090 data &= 0xff;
1091
1092 return (u8)data;
1093}
1094
1095static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1096{
1097 u32 mask = 0xff;
1098 __le32 tmp;
1099 u16 byen = BYTE_EN_BYTE;
1100 u8 shift = index & 3;
1101
1102 data &= mask;
1103
1104 if (index & 3) {
1105 byen <<= shift;
1106 mask <<= (shift * 8);
1107 data <<= (shift * 8);
1108 index &= ~3;
1109 }
1110
1111 tmp = __cpu_to_le32(data);
1112
1113 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1114}
1115
1116static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1117{
1118 u16 ocp_base, ocp_index;
1119
1120 ocp_base = addr & 0xf000;
1121 if (ocp_base != tp->ocp_base) {
1122 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1123 tp->ocp_base = ocp_base;
1124 }
1125
1126 ocp_index = (addr & 0x0fff) | 0xb000;
1127 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1128}
1129
1130static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1131{
1132 u16 ocp_base, ocp_index;
1133
1134 ocp_base = addr & 0xf000;
1135 if (ocp_base != tp->ocp_base) {
1136 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1137 tp->ocp_base = ocp_base;
1138 }
1139
1140 ocp_index = (addr & 0x0fff) | 0xb000;
1141 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1142}
1143
1144static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
1145{
1146 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
1147}
1148
1149static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
1150{
1151 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
1152}
1153
1154static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1155{
1156 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1157 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1158}
1159
1160static u16 sram_read(struct r8152 *tp, u16 addr)
1161{
1162 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1163 return ocp_reg_read(tp, OCP_SRAM_DATA);
1164}
1165
1166static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1167{
1168 struct r8152 *tp = netdev_priv(netdev);
1169 int ret;
1170
1171 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1172 return -ENODEV;
1173
1174 if (phy_id != R8152_PHY_ID)
1175 return -EINVAL;
1176
1177 ret = r8152_mdio_read(tp, reg);
1178
1179 return ret;
1180}
1181
1182static
1183void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1184{
1185 struct r8152 *tp = netdev_priv(netdev);
1186
1187 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1188 return;
1189
1190 if (phy_id != R8152_PHY_ID)
1191 return;
1192
1193 r8152_mdio_write(tp, reg, val);
1194}
1195
1196static int
1197r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
1198
1199static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1200{
1201 struct r8152 *tp = netdev_priv(netdev);
1202 struct sockaddr *addr = p;
1203 int ret = -EADDRNOTAVAIL;
1204
1205 if (!is_valid_ether_addr(addr->sa_data))
1206 goto out1;
1207
1208 ret = usb_autopm_get_interface(tp->intf);
1209 if (ret < 0)
1210 goto out1;
1211
1212 mutex_lock(&tp->control);
1213
1214 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1215
1216 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1217 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1218 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1219
1220 mutex_unlock(&tp->control);
1221
1222 usb_autopm_put_interface(tp->intf);
1223out1:
1224 return ret;
1225}
1226
1227/* Devices containing proper chips can support a persistent
1228 * host system provided MAC address.
1229 * Examples of this are Dell TB15 and Dell WD15 docks
1230 */
1231static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1232{
1233 acpi_status status;
1234 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1235 union acpi_object *obj;
1236 int ret = -EINVAL;
1237 u32 ocp_data;
1238 unsigned char buf[6];
1239
1240 /* test for -AD variant of RTL8153 */
1241 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1242 if ((ocp_data & AD_MASK) == 0x1000) {
1243 /* test for MAC address pass-through bit */
1244 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1245 if ((ocp_data & PASS_THRU_MASK) != 1) {
1246 netif_dbg(tp, probe, tp->netdev,
1247 "No efuse for RTL8153-AD MAC pass through\n");
1248 return -ENODEV;
1249 }
1250 } else {
1251 /* test for RTL8153-BND and RTL8153-BD */
1252 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
1253 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
1254 netif_dbg(tp, probe, tp->netdev,
1255 "Invalid variant for MAC pass through\n");
1256 return -ENODEV;
1257 }
1258 }
1259
1260 /* returns _AUXMAC_#AABBCCDDEEFF# */
1261 status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
1262 obj = (union acpi_object *)buffer.pointer;
1263 if (!ACPI_SUCCESS(status))
1264 return -ENODEV;
1265 if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
1266 netif_warn(tp, probe, tp->netdev,
1267 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
1268 obj->type, obj->string.length);
1269 goto amacout;
1270 }
1271 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1272 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1273 netif_warn(tp, probe, tp->netdev,
1274 "Invalid header when reading pass-thru MAC addr\n");
1275 goto amacout;
1276 }
1277 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1278 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1279 netif_warn(tp, probe, tp->netdev,
1280 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1281 ret, buf);
1282 ret = -EINVAL;
1283 goto amacout;
1284 }
1285 memcpy(sa->sa_data, buf, 6);
1286 netif_info(tp, probe, tp->netdev,
1287 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1288
1289amacout:
1290 kfree(obj);
1291 return ret;
1292}
1293
1294static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1295{
1296 struct net_device *dev = tp->netdev;
1297 int ret;
1298
1299 sa->sa_family = dev->type;
1300
1301 if (tp->version == RTL_VER_01) {
1302 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1303 } else {
1304 /* if device doesn't support MAC pass through this will
1305 * be expected to be non-zero
1306 */
1307 ret = vendor_mac_passthru_addr_read(tp, sa);
1308 if (ret < 0)
1309 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1310 }
1311
1312 if (ret < 0) {
1313 netif_err(tp, probe, dev, "Get ether addr fail\n");
1314 } else if (!is_valid_ether_addr(sa->sa_data)) {
1315 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1316 sa->sa_data);
1317 eth_hw_addr_random(dev);
1318 ether_addr_copy(sa->sa_data, dev->dev_addr);
1319 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1320 sa->sa_data);
1321 return 0;
1322 }
1323
1324 return ret;
1325}
1326
1327static int set_ethernet_addr(struct r8152 *tp)
1328{
1329 struct net_device *dev = tp->netdev;
1330 struct sockaddr sa;
1331 int ret;
1332
1333 ret = determine_ethernet_addr(tp, &sa);
1334 if (ret < 0)
1335 return ret;
1336
1337 if (tp->version == RTL_VER_01)
1338 ether_addr_copy(dev->dev_addr, sa.sa_data);
1339 else
1340 ret = rtl8152_set_mac_address(dev, &sa);
1341
1342 return ret;
1343}
1344
1345static void read_bulk_callback(struct urb *urb)
1346{
1347 struct net_device *netdev;
1348 int status = urb->status;
1349 struct rx_agg *agg;
1350 struct r8152 *tp;
1351 unsigned long flags;
1352
1353 agg = urb->context;
1354 if (!agg)
1355 return;
1356
1357 tp = agg->context;
1358 if (!tp)
1359 return;
1360
1361 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1362 return;
1363
1364 if (!test_bit(WORK_ENABLE, &tp->flags))
1365 return;
1366
1367 netdev = tp->netdev;
1368
1369 /* When link down, the driver would cancel all bulks. */
1370 /* This avoid the re-submitting bulk */
1371 if (!netif_carrier_ok(netdev))
1372 return;
1373
1374 usb_mark_last_busy(tp->udev);
1375
1376 switch (status) {
1377 case 0:
1378 if (urb->actual_length < ETH_ZLEN)
1379 break;
1380
1381 spin_lock_irqsave(&tp->rx_lock, flags);
1382 list_add_tail(&agg->list, &tp->rx_done);
1383 spin_unlock_irqrestore(&tp->rx_lock, flags);
1384 napi_schedule(&tp->napi);
1385 return;
1386 case -ESHUTDOWN:
1387 rtl_set_unplug(tp);
1388 netif_device_detach(tp->netdev);
1389 return;
1390 case -ENOENT:
1391 return; /* the urb is in unlink state */
1392 case -ETIME:
1393 if (net_ratelimit())
1394 netdev_warn(netdev, "maybe reset is needed?\n");
1395 break;
1396 default:
1397 if (net_ratelimit())
1398 netdev_warn(netdev, "Rx status %d\n", status);
1399 break;
1400 }
1401
1402 r8152_submit_rx(tp, agg, GFP_ATOMIC);
1403}
1404
1405static void write_bulk_callback(struct urb *urb)
1406{
1407 struct net_device_stats *stats;
1408 struct net_device *netdev;
1409 struct tx_agg *agg;
1410 struct r8152 *tp;
1411 unsigned long flags;
1412 int status = urb->status;
1413
1414 agg = urb->context;
1415 if (!agg)
1416 return;
1417
1418 tp = agg->context;
1419 if (!tp)
1420 return;
1421
1422 netdev = tp->netdev;
1423 stats = &netdev->stats;
1424 if (status) {
1425 if (net_ratelimit())
1426 netdev_warn(netdev, "Tx status %d\n", status);
1427 stats->tx_errors += agg->skb_num;
1428 } else {
1429 stats->tx_packets += agg->skb_num;
1430 stats->tx_bytes += agg->skb_len;
1431 }
1432
1433 spin_lock_irqsave(&tp->tx_lock, flags);
1434 list_add_tail(&agg->list, &tp->tx_free);
1435 spin_unlock_irqrestore(&tp->tx_lock, flags);
1436
1437 usb_autopm_put_interface_async(tp->intf);
1438
1439 if (!netif_carrier_ok(netdev))
1440 return;
1441
1442 if (!test_bit(WORK_ENABLE, &tp->flags))
1443 return;
1444
1445 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1446 return;
1447
1448 if (!skb_queue_empty(&tp->tx_queue))
1449 tasklet_schedule(&tp->tx_tl);
1450}
1451
1452static void intr_callback(struct urb *urb)
1453{
1454 struct r8152 *tp;
1455 __le16 *d;
1456 int status = urb->status;
1457 int res;
1458
1459 tp = urb->context;
1460 if (!tp)
1461 return;
1462
1463 if (!test_bit(WORK_ENABLE, &tp->flags))
1464 return;
1465
1466 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1467 return;
1468
1469 switch (status) {
1470 case 0: /* success */
1471 break;
1472 case -ECONNRESET: /* unlink */
1473 case -ESHUTDOWN:
1474 netif_device_detach(tp->netdev);
1475 /* fall through */
1476 case -ENOENT:
1477 case -EPROTO:
1478 netif_info(tp, intr, tp->netdev,
1479 "Stop submitting intr, status %d\n", status);
1480 return;
1481 case -EOVERFLOW:
1482 if (net_ratelimit())
1483 netif_info(tp, intr, tp->netdev,
1484 "intr status -EOVERFLOW\n");
1485 goto resubmit;
1486 /* -EPIPE: should clear the halt */
1487 default:
1488 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1489 goto resubmit;
1490 }
1491
1492 d = urb->transfer_buffer;
1493 if (INTR_LINK & __le16_to_cpu(d[0])) {
1494 if (!netif_carrier_ok(tp->netdev)) {
1495 set_bit(RTL8152_LINK_CHG, &tp->flags);
1496 schedule_delayed_work(&tp->schedule, 0);
1497 }
1498 } else {
1499 if (netif_carrier_ok(tp->netdev)) {
1500 netif_stop_queue(tp->netdev);
1501 set_bit(RTL8152_LINK_CHG, &tp->flags);
1502 schedule_delayed_work(&tp->schedule, 0);
1503 }
1504 }
1505
1506resubmit:
1507 res = usb_submit_urb(urb, GFP_ATOMIC);
1508 if (res == -ENODEV) {
1509 rtl_set_unplug(tp);
1510 netif_device_detach(tp->netdev);
1511 } else if (res) {
1512 netif_err(tp, intr, tp->netdev,
1513 "can't resubmit intr, status %d\n", res);
1514 }
1515}
1516
1517static inline void *rx_agg_align(void *data)
1518{
1519 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
1520}
1521
1522static inline void *tx_agg_align(void *data)
1523{
1524 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
1525}
1526
1527static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1528{
1529 list_del(&agg->info_list);
1530
1531 usb_free_urb(agg->urb);
1532 put_page(agg->page);
1533 kfree(agg);
1534
1535 atomic_dec(&tp->rx_count);
1536}
1537
1538static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1539{
1540 struct net_device *netdev = tp->netdev;
1541 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1542 unsigned int order = get_order(tp->rx_buf_sz);
1543 struct rx_agg *rx_agg;
1544 unsigned long flags;
1545
1546 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1547 if (!rx_agg)
1548 return NULL;
1549
1550 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
1551 if (!rx_agg->page)
1552 goto free_rx;
1553
1554 rx_agg->buffer = page_address(rx_agg->page);
1555
1556 rx_agg->urb = usb_alloc_urb(0, mflags);
1557 if (!rx_agg->urb)
1558 goto free_buf;
1559
1560 rx_agg->context = tp;
1561
1562 INIT_LIST_HEAD(&rx_agg->list);
1563 INIT_LIST_HEAD(&rx_agg->info_list);
1564 spin_lock_irqsave(&tp->rx_lock, flags);
1565 list_add_tail(&rx_agg->info_list, &tp->rx_info);
1566 spin_unlock_irqrestore(&tp->rx_lock, flags);
1567
1568 atomic_inc(&tp->rx_count);
1569
1570 return rx_agg;
1571
1572free_buf:
1573 __free_pages(rx_agg->page, order);
1574free_rx:
1575 kfree(rx_agg);
1576 return NULL;
1577}
1578
1579static void free_all_mem(struct r8152 *tp)
1580{
1581 struct rx_agg *agg, *agg_next;
1582 unsigned long flags;
1583 int i;
1584
1585 spin_lock_irqsave(&tp->rx_lock, flags);
1586
1587 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1588 free_rx_agg(tp, agg);
1589
1590 spin_unlock_irqrestore(&tp->rx_lock, flags);
1591
1592 WARN_ON(atomic_read(&tp->rx_count));
1593
1594 for (i = 0; i < RTL8152_MAX_TX; i++) {
1595 usb_free_urb(tp->tx_info[i].urb);
1596 tp->tx_info[i].urb = NULL;
1597
1598 kfree(tp->tx_info[i].buffer);
1599 tp->tx_info[i].buffer = NULL;
1600 tp->tx_info[i].head = NULL;
1601 }
1602
1603 usb_free_urb(tp->intr_urb);
1604 tp->intr_urb = NULL;
1605
1606 kfree(tp->intr_buff);
1607 tp->intr_buff = NULL;
1608}
1609
1610static int alloc_all_mem(struct r8152 *tp)
1611{
1612 struct net_device *netdev = tp->netdev;
1613 struct usb_interface *intf = tp->intf;
1614 struct usb_host_interface *alt = intf->cur_altsetting;
1615 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
1616 int node, i;
1617
1618 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1619
1620 spin_lock_init(&tp->rx_lock);
1621 spin_lock_init(&tp->tx_lock);
1622 INIT_LIST_HEAD(&tp->rx_info);
1623 INIT_LIST_HEAD(&tp->tx_free);
1624 INIT_LIST_HEAD(&tp->rx_done);
1625 skb_queue_head_init(&tp->tx_queue);
1626 skb_queue_head_init(&tp->rx_queue);
1627 atomic_set(&tp->rx_count, 0);
1628
1629 for (i = 0; i < RTL8152_MAX_RX; i++) {
1630 if (!alloc_rx_agg(tp, GFP_KERNEL))
1631 goto err1;
1632 }
1633
1634 for (i = 0; i < RTL8152_MAX_TX; i++) {
1635 struct urb *urb;
1636 u8 *buf;
1637
1638 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
1639 if (!buf)
1640 goto err1;
1641
1642 if (buf != tx_agg_align(buf)) {
1643 kfree(buf);
1644 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
1645 node);
1646 if (!buf)
1647 goto err1;
1648 }
1649
1650 urb = usb_alloc_urb(0, GFP_KERNEL);
1651 if (!urb) {
1652 kfree(buf);
1653 goto err1;
1654 }
1655
1656 INIT_LIST_HEAD(&tp->tx_info[i].list);
1657 tp->tx_info[i].context = tp;
1658 tp->tx_info[i].urb = urb;
1659 tp->tx_info[i].buffer = buf;
1660 tp->tx_info[i].head = tx_agg_align(buf);
1661
1662 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1663 }
1664
1665 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1666 if (!tp->intr_urb)
1667 goto err1;
1668
1669 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1670 if (!tp->intr_buff)
1671 goto err1;
1672
1673 tp->intr_interval = (int)ep_intr->desc.bInterval;
1674 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1675 tp->intr_buff, INTBUFSIZE, intr_callback,
1676 tp, tp->intr_interval);
1677
1678 return 0;
1679
1680err1:
1681 free_all_mem(tp);
1682 return -ENOMEM;
1683}
1684
1685static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1686{
1687 struct tx_agg *agg = NULL;
1688 unsigned long flags;
1689
1690 if (list_empty(&tp->tx_free))
1691 return NULL;
1692
1693 spin_lock_irqsave(&tp->tx_lock, flags);
1694 if (!list_empty(&tp->tx_free)) {
1695 struct list_head *cursor;
1696
1697 cursor = tp->tx_free.next;
1698 list_del_init(cursor);
1699 agg = list_entry(cursor, struct tx_agg, list);
1700 }
1701 spin_unlock_irqrestore(&tp->tx_lock, flags);
1702
1703 return agg;
1704}
1705
1706/* r8152_csum_workaround()
1707 * The hw limites the value the transport offset. When the offset is out of the
1708 * range, calculate the checksum by sw.
1709 */
1710static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1711 struct sk_buff_head *list)
1712{
1713 if (skb_shinfo(skb)->gso_size) {
1714 netdev_features_t features = tp->netdev->features;
1715 struct sk_buff_head seg_list;
1716 struct sk_buff *segs, *nskb;
1717
1718 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1719 segs = skb_gso_segment(skb, features);
1720 if (IS_ERR(segs) || !segs)
1721 goto drop;
1722
1723 __skb_queue_head_init(&seg_list);
1724
1725 do {
1726 nskb = segs;
1727 segs = segs->next;
1728 nskb->next = NULL;
1729 __skb_queue_tail(&seg_list, nskb);
1730 } while (segs);
1731
1732 skb_queue_splice(&seg_list, list);
1733 dev_kfree_skb(skb);
1734 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1735 if (skb_checksum_help(skb) < 0)
1736 goto drop;
1737
1738 __skb_queue_head(list, skb);
1739 } else {
1740 struct net_device_stats *stats;
1741
1742drop:
1743 stats = &tp->netdev->stats;
1744 stats->tx_dropped++;
1745 dev_kfree_skb(skb);
1746 }
1747}
1748
1749/* msdn_giant_send_check()
1750 * According to the document of microsoft, the TCP Pseudo Header excludes the
1751 * packet length for IPv6 TCP large packets.
1752 */
1753static int msdn_giant_send_check(struct sk_buff *skb)
1754{
1755 const struct ipv6hdr *ipv6h;
1756 struct tcphdr *th;
1757 int ret;
1758
1759 ret = skb_cow_head(skb, 0);
1760 if (ret)
1761 return ret;
1762
1763 ipv6h = ipv6_hdr(skb);
1764 th = tcp_hdr(skb);
1765
1766 th->check = 0;
1767 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1768
1769 return ret;
1770}
1771
1772static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1773{
1774 if (skb_vlan_tag_present(skb)) {
1775 u32 opts2;
1776
1777 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
1778 desc->opts2 |= cpu_to_le32(opts2);
1779 }
1780}
1781
1782static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1783{
1784 u32 opts2 = le32_to_cpu(desc->opts2);
1785
1786 if (opts2 & RX_VLAN_TAG)
1787 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1788 swab16(opts2 & 0xffff));
1789}
1790
1791static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1792 struct sk_buff *skb, u32 len, u32 transport_offset)
1793{
1794 u32 mss = skb_shinfo(skb)->gso_size;
1795 u32 opts1, opts2 = 0;
1796 int ret = TX_CSUM_SUCCESS;
1797
1798 WARN_ON_ONCE(len > TX_LEN_MAX);
1799
1800 opts1 = len | TX_FS | TX_LS;
1801
1802 if (mss) {
1803 if (transport_offset > GTTCPHO_MAX) {
1804 netif_warn(tp, tx_err, tp->netdev,
1805 "Invalid transport offset 0x%x for TSO\n",
1806 transport_offset);
1807 ret = TX_CSUM_TSO;
1808 goto unavailable;
1809 }
1810
1811 switch (vlan_get_protocol(skb)) {
1812 case htons(ETH_P_IP):
1813 opts1 |= GTSENDV4;
1814 break;
1815
1816 case htons(ETH_P_IPV6):
1817 if (msdn_giant_send_check(skb)) {
1818 ret = TX_CSUM_TSO;
1819 goto unavailable;
1820 }
1821 opts1 |= GTSENDV6;
1822 break;
1823
1824 default:
1825 WARN_ON_ONCE(1);
1826 break;
1827 }
1828
1829 opts1 |= transport_offset << GTTCPHO_SHIFT;
1830 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1831 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1832 u8 ip_protocol;
1833
1834 if (transport_offset > TCPHO_MAX) {
1835 netif_warn(tp, tx_err, tp->netdev,
1836 "Invalid transport offset 0x%x\n",
1837 transport_offset);
1838 ret = TX_CSUM_NONE;
1839 goto unavailable;
1840 }
1841
1842 switch (vlan_get_protocol(skb)) {
1843 case htons(ETH_P_IP):
1844 opts2 |= IPV4_CS;
1845 ip_protocol = ip_hdr(skb)->protocol;
1846 break;
1847
1848 case htons(ETH_P_IPV6):
1849 opts2 |= IPV6_CS;
1850 ip_protocol = ipv6_hdr(skb)->nexthdr;
1851 break;
1852
1853 default:
1854 ip_protocol = IPPROTO_RAW;
1855 break;
1856 }
1857
1858 if (ip_protocol == IPPROTO_TCP)
1859 opts2 |= TCP_CS;
1860 else if (ip_protocol == IPPROTO_UDP)
1861 opts2 |= UDP_CS;
1862 else
1863 WARN_ON_ONCE(1);
1864
1865 opts2 |= transport_offset << TCPHO_SHIFT;
1866 }
1867
1868 desc->opts2 = cpu_to_le32(opts2);
1869 desc->opts1 = cpu_to_le32(opts1);
1870
1871unavailable:
1872 return ret;
1873}
1874
1875static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1876{
1877 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1878 int remain, ret;
1879 u8 *tx_data;
1880
1881 __skb_queue_head_init(&skb_head);
1882 spin_lock(&tx_queue->lock);
1883 skb_queue_splice_init(tx_queue, &skb_head);
1884 spin_unlock(&tx_queue->lock);
1885
1886 tx_data = agg->head;
1887 agg->skb_num = 0;
1888 agg->skb_len = 0;
1889 remain = agg_buf_sz;
1890
1891 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
1892 struct tx_desc *tx_desc;
1893 struct sk_buff *skb;
1894 unsigned int len;
1895 u32 offset;
1896
1897 skb = __skb_dequeue(&skb_head);
1898 if (!skb)
1899 break;
1900
1901 len = skb->len + sizeof(*tx_desc);
1902
1903 if (len > remain) {
1904 __skb_queue_head(&skb_head, skb);
1905 break;
1906 }
1907
1908 tx_data = tx_agg_align(tx_data);
1909 tx_desc = (struct tx_desc *)tx_data;
1910
1911 offset = (u32)skb_transport_offset(skb);
1912
1913 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1914 r8152_csum_workaround(tp, skb, &skb_head);
1915 continue;
1916 }
1917
1918 rtl_tx_vlan_tag(tx_desc, skb);
1919
1920 tx_data += sizeof(*tx_desc);
1921
1922 len = skb->len;
1923 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1924 struct net_device_stats *stats = &tp->netdev->stats;
1925
1926 stats->tx_dropped++;
1927 dev_kfree_skb_any(skb);
1928 tx_data -= sizeof(*tx_desc);
1929 continue;
1930 }
1931
1932 tx_data += len;
1933 agg->skb_len += len;
1934 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
1935
1936 dev_kfree_skb_any(skb);
1937
1938 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
1939
1940 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
1941 break;
1942 }
1943
1944 if (!skb_queue_empty(&skb_head)) {
1945 spin_lock(&tx_queue->lock);
1946 skb_queue_splice(&skb_head, tx_queue);
1947 spin_unlock(&tx_queue->lock);
1948 }
1949
1950 netif_tx_lock(tp->netdev);
1951
1952 if (netif_queue_stopped(tp->netdev) &&
1953 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1954 netif_wake_queue(tp->netdev);
1955
1956 netif_tx_unlock(tp->netdev);
1957
1958 ret = usb_autopm_get_interface_async(tp->intf);
1959 if (ret < 0)
1960 goto out_tx_fill;
1961
1962 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1963 agg->head, (int)(tx_data - (u8 *)agg->head),
1964 (usb_complete_t)write_bulk_callback, agg);
1965
1966 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
1967 if (ret < 0)
1968 usb_autopm_put_interface_async(tp->intf);
1969
1970out_tx_fill:
1971 return ret;
1972}
1973
1974static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1975{
1976 u8 checksum = CHECKSUM_NONE;
1977 u32 opts2, opts3;
1978
1979 if (!(tp->netdev->features & NETIF_F_RXCSUM))
1980 goto return_result;
1981
1982 opts2 = le32_to_cpu(rx_desc->opts2);
1983 opts3 = le32_to_cpu(rx_desc->opts3);
1984
1985 if (opts2 & RD_IPV4_CS) {
1986 if (opts3 & IPF)
1987 checksum = CHECKSUM_NONE;
1988 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1989 checksum = CHECKSUM_UNNECESSARY;
1990 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1991 checksum = CHECKSUM_UNNECESSARY;
1992 } else if (opts2 & RD_IPV6_CS) {
1993 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1994 checksum = CHECKSUM_UNNECESSARY;
1995 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1996 checksum = CHECKSUM_UNNECESSARY;
1997 }
1998
1999return_result:
2000 return checksum;
2001}
2002
2003static inline bool rx_count_exceed(struct r8152 *tp)
2004{
2005 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
2006}
2007
2008static inline int agg_offset(struct rx_agg *agg, void *addr)
2009{
2010 return (int)(addr - agg->buffer);
2011}
2012
2013static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
2014{
2015 struct rx_agg *agg, *agg_next, *agg_free = NULL;
2016 unsigned long flags;
2017
2018 spin_lock_irqsave(&tp->rx_lock, flags);
2019
2020 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
2021 if (page_count(agg->page) == 1) {
2022 if (!agg_free) {
2023 list_del_init(&agg->list);
2024 agg_free = agg;
2025 continue;
2026 }
2027 if (rx_count_exceed(tp)) {
2028 list_del_init(&agg->list);
2029 free_rx_agg(tp, agg);
2030 }
2031 break;
2032 }
2033 }
2034
2035 spin_unlock_irqrestore(&tp->rx_lock, flags);
2036
2037 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
2038 agg_free = alloc_rx_agg(tp, mflags);
2039
2040 return agg_free;
2041}
2042
2043static int rx_bottom(struct r8152 *tp, int budget)
2044{
2045 unsigned long flags;
2046 struct list_head *cursor, *next, rx_queue;
2047 int ret = 0, work_done = 0;
2048 struct napi_struct *napi = &tp->napi;
2049
2050 if (!skb_queue_empty(&tp->rx_queue)) {
2051 while (work_done < budget) {
2052 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2053 struct net_device *netdev = tp->netdev;
2054 struct net_device_stats *stats = &netdev->stats;
2055 unsigned int pkt_len;
2056
2057 if (!skb)
2058 break;
2059
2060 pkt_len = skb->len;
2061 napi_gro_receive(napi, skb);
2062 work_done++;
2063 stats->rx_packets++;
2064 stats->rx_bytes += pkt_len;
2065 }
2066 }
2067
2068 if (list_empty(&tp->rx_done))
2069 goto out1;
2070
2071 INIT_LIST_HEAD(&rx_queue);
2072 spin_lock_irqsave(&tp->rx_lock, flags);
2073 list_splice_init(&tp->rx_done, &rx_queue);
2074 spin_unlock_irqrestore(&tp->rx_lock, flags);
2075
2076 list_for_each_safe(cursor, next, &rx_queue) {
2077 struct rx_desc *rx_desc;
2078 struct rx_agg *agg, *agg_free;
2079 int len_used = 0;
2080 struct urb *urb;
2081 u8 *rx_data;
2082
2083 list_del_init(cursor);
2084
2085 agg = list_entry(cursor, struct rx_agg, list);
2086 urb = agg->urb;
2087 if (urb->actual_length < ETH_ZLEN)
2088 goto submit;
2089
2090 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2091
2092 rx_desc = agg->buffer;
2093 rx_data = agg->buffer;
2094 len_used += sizeof(struct rx_desc);
2095
2096 while (urb->actual_length > len_used) {
2097 struct net_device *netdev = tp->netdev;
2098 struct net_device_stats *stats = &netdev->stats;
2099 unsigned int pkt_len, rx_frag_head_sz;
2100 struct sk_buff *skb;
2101
2102 /* limite the skb numbers for rx_queue */
2103 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2104 break;
2105
2106 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
2107 if (pkt_len < ETH_ZLEN)
2108 break;
2109
2110 len_used += pkt_len;
2111 if (urb->actual_length < len_used)
2112 break;
2113
2114 pkt_len -= ETH_FCS_LEN;
2115 rx_data += sizeof(struct rx_desc);
2116
2117 if (!agg_free || tp->rx_copybreak > pkt_len)
2118 rx_frag_head_sz = pkt_len;
2119 else
2120 rx_frag_head_sz = tp->rx_copybreak;
2121
2122 skb = napi_alloc_skb(napi, rx_frag_head_sz);
2123 if (!skb) {
2124 stats->rx_dropped++;
2125 goto find_next_rx;
2126 }
2127
2128 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
2129 memcpy(skb->data, rx_data, rx_frag_head_sz);
2130 skb_put(skb, rx_frag_head_sz);
2131 pkt_len -= rx_frag_head_sz;
2132 rx_data += rx_frag_head_sz;
2133 if (pkt_len) {
2134 skb_add_rx_frag(skb, 0, agg->page,
2135 agg_offset(agg, rx_data),
2136 pkt_len,
2137 SKB_DATA_ALIGN(pkt_len));
2138 get_page(agg->page);
2139 }
2140
2141 skb->protocol = eth_type_trans(skb, netdev);
2142 rtl_rx_vlan_tag(rx_desc, skb);
2143 if (work_done < budget) {
2144 work_done++;
2145 stats->rx_packets++;
2146 stats->rx_bytes += skb->len;
2147 napi_gro_receive(napi, skb);
2148 } else {
2149 __skb_queue_tail(&tp->rx_queue, skb);
2150 }
2151
2152find_next_rx:
2153 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
2154 rx_desc = (struct rx_desc *)rx_data;
2155 len_used = agg_offset(agg, rx_data);
2156 len_used += sizeof(struct rx_desc);
2157 }
2158
2159 WARN_ON(!agg_free && page_count(agg->page) > 1);
2160
2161 if (agg_free) {
2162 spin_lock_irqsave(&tp->rx_lock, flags);
2163 if (page_count(agg->page) == 1) {
2164 list_add(&agg_free->list, &tp->rx_used);
2165 } else {
2166 list_add_tail(&agg->list, &tp->rx_used);
2167 agg = agg_free;
2168 urb = agg->urb;
2169 }
2170 spin_unlock_irqrestore(&tp->rx_lock, flags);
2171 }
2172
2173submit:
2174 if (!ret) {
2175 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2176 } else {
2177 urb->actual_length = 0;
2178 list_add_tail(&agg->list, next);
2179 }
2180 }
2181
2182 if (!list_empty(&rx_queue)) {
2183 spin_lock_irqsave(&tp->rx_lock, flags);
2184 list_splice_tail(&rx_queue, &tp->rx_done);
2185 spin_unlock_irqrestore(&tp->rx_lock, flags);
2186 }
2187
2188out1:
2189 return work_done;
2190}
2191
2192static void tx_bottom(struct r8152 *tp)
2193{
2194 int res;
2195
2196 do {
2197 struct tx_agg *agg;
2198
2199 if (skb_queue_empty(&tp->tx_queue))
2200 break;
2201
2202 agg = r8152_get_tx_agg(tp);
2203 if (!agg)
2204 break;
2205
2206 res = r8152_tx_agg_fill(tp, agg);
2207 if (res) {
2208 struct net_device *netdev = tp->netdev;
2209
2210 if (res == -ENODEV) {
2211 rtl_set_unplug(tp);
2212 netif_device_detach(netdev);
2213 } else {
2214 struct net_device_stats *stats = &netdev->stats;
2215 unsigned long flags;
2216
2217 netif_warn(tp, tx_err, netdev,
2218 "failed tx_urb %d\n", res);
2219 stats->tx_dropped += agg->skb_num;
2220
2221 spin_lock_irqsave(&tp->tx_lock, flags);
2222 list_add_tail(&agg->list, &tp->tx_free);
2223 spin_unlock_irqrestore(&tp->tx_lock, flags);
2224 }
2225 }
2226 } while (res == 0);
2227}
2228
2229static void bottom_half(unsigned long data)
2230{
2231 struct r8152 *tp;
2232
2233 tp = (struct r8152 *)data;
2234
2235 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2236 return;
2237
2238 if (!test_bit(WORK_ENABLE, &tp->flags))
2239 return;
2240
2241 /* When link down, the driver would cancel all bulks. */
2242 /* This avoid the re-submitting bulk */
2243 if (!netif_carrier_ok(tp->netdev))
2244 return;
2245
2246 clear_bit(SCHEDULE_TASKLET, &tp->flags);
2247
2248 tx_bottom(tp);
2249}
2250
2251static int r8152_poll(struct napi_struct *napi, int budget)
2252{
2253 struct r8152 *tp = container_of(napi, struct r8152, napi);
2254 int work_done;
2255
2256 if (!budget)
2257 return 0;
2258
2259 work_done = rx_bottom(tp, budget);
2260
2261 if (work_done < budget) {
2262 if (!napi_complete_done(napi, work_done))
2263 goto out;
2264 if (!list_empty(&tp->rx_done))
2265 napi_schedule(napi);
2266 }
2267
2268out:
2269 return work_done;
2270}
2271
2272static
2273int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2274{
2275 int ret;
2276
2277 /* The rx would be stopped, so skip submitting */
2278 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2279 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2280 return 0;
2281
2282 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
2283 agg->buffer, tp->rx_buf_sz,
2284 (usb_complete_t)read_bulk_callback, agg);
2285
2286 ret = usb_submit_urb(agg->urb, mem_flags);
2287 if (ret == -ENODEV) {
2288 rtl_set_unplug(tp);
2289 netif_device_detach(tp->netdev);
2290 } else if (ret) {
2291 struct urb *urb = agg->urb;
2292 unsigned long flags;
2293
2294 urb->actual_length = 0;
2295 spin_lock_irqsave(&tp->rx_lock, flags);
2296 list_add_tail(&agg->list, &tp->rx_done);
2297 spin_unlock_irqrestore(&tp->rx_lock, flags);
2298
2299 netif_err(tp, rx_err, tp->netdev,
2300 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2301
2302 napi_schedule(&tp->napi);
2303 }
2304
2305 return ret;
2306}
2307
2308static void rtl_drop_queued_tx(struct r8152 *tp)
2309{
2310 struct net_device_stats *stats = &tp->netdev->stats;
2311 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2312 struct sk_buff *skb;
2313
2314 if (skb_queue_empty(tx_queue))
2315 return;
2316
2317 __skb_queue_head_init(&skb_head);
2318 spin_lock_bh(&tx_queue->lock);
2319 skb_queue_splice_init(tx_queue, &skb_head);
2320 spin_unlock_bh(&tx_queue->lock);
2321
2322 while ((skb = __skb_dequeue(&skb_head))) {
2323 dev_kfree_skb(skb);
2324 stats->tx_dropped++;
2325 }
2326}
2327
2328static void rtl8152_tx_timeout(struct net_device *netdev)
2329{
2330 struct r8152 *tp = netdev_priv(netdev);
2331
2332 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
2333
2334 usb_queue_reset_device(tp->intf);
2335}
2336
2337static void rtl8152_set_rx_mode(struct net_device *netdev)
2338{
2339 struct r8152 *tp = netdev_priv(netdev);
2340
2341 if (netif_carrier_ok(netdev)) {
2342 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2343 schedule_delayed_work(&tp->schedule, 0);
2344 }
2345}
2346
2347static void _rtl8152_set_rx_mode(struct net_device *netdev)
2348{
2349 struct r8152 *tp = netdev_priv(netdev);
2350 u32 mc_filter[2]; /* Multicast hash filter */
2351 __le32 tmp[2];
2352 u32 ocp_data;
2353
2354 netif_stop_queue(netdev);
2355 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2356 ocp_data &= ~RCR_ACPT_ALL;
2357 ocp_data |= RCR_AB | RCR_APM;
2358
2359 if (netdev->flags & IFF_PROMISC) {
2360 /* Unconditionally log net taps. */
2361 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2362 ocp_data |= RCR_AM | RCR_AAP;
2363 mc_filter[1] = 0xffffffff;
2364 mc_filter[0] = 0xffffffff;
2365 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2366 (netdev->flags & IFF_ALLMULTI)) {
2367 /* Too many to filter perfectly -- accept all multicasts. */
2368 ocp_data |= RCR_AM;
2369 mc_filter[1] = 0xffffffff;
2370 mc_filter[0] = 0xffffffff;
2371 } else {
2372 struct netdev_hw_addr *ha;
2373
2374 mc_filter[1] = 0;
2375 mc_filter[0] = 0;
2376 netdev_for_each_mc_addr(ha, netdev) {
2377 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
2378
2379 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2380 ocp_data |= RCR_AM;
2381 }
2382 }
2383
2384 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2385 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
2386
2387 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
2388 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2389 netif_wake_queue(netdev);
2390}
2391
2392static netdev_features_t
2393rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2394 netdev_features_t features)
2395{
2396 u32 mss = skb_shinfo(skb)->gso_size;
2397 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2398 int offset = skb_transport_offset(skb);
2399
2400 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
2401 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2402 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2403 features &= ~NETIF_F_GSO_MASK;
2404
2405 return features;
2406}
2407
2408static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
2409 struct net_device *netdev)
2410{
2411 struct r8152 *tp = netdev_priv(netdev);
2412
2413 skb_tx_timestamp(skb);
2414
2415 skb_queue_tail(&tp->tx_queue, skb);
2416
2417 if (!list_empty(&tp->tx_free)) {
2418 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2419 set_bit(SCHEDULE_TASKLET, &tp->flags);
2420 schedule_delayed_work(&tp->schedule, 0);
2421 } else {
2422 usb_mark_last_busy(tp->udev);
2423 tasklet_schedule(&tp->tx_tl);
2424 }
2425 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
2426 netif_stop_queue(netdev);
2427 }
2428
2429 return NETDEV_TX_OK;
2430}
2431
2432static void r8152b_reset_packet_filter(struct r8152 *tp)
2433{
2434 u32 ocp_data;
2435
2436 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2437 ocp_data &= ~FMC_FCR_MCU_EN;
2438 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2439 ocp_data |= FMC_FCR_MCU_EN;
2440 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2441}
2442
2443static void rtl8152_nic_reset(struct r8152 *tp)
2444{
2445 int i;
2446
2447 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2448
2449 for (i = 0; i < 1000; i++) {
2450 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2451 break;
2452 usleep_range(100, 400);
2453 }
2454}
2455
2456static void set_tx_qlen(struct r8152 *tp)
2457{
2458 struct net_device *netdev = tp->netdev;
2459
2460 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
2461 sizeof(struct tx_desc));
2462}
2463
2464static inline u8 rtl8152_get_speed(struct r8152 *tp)
2465{
2466 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2467}
2468
2469static void rtl_set_eee_plus(struct r8152 *tp)
2470{
2471 u32 ocp_data;
2472 u8 speed;
2473
2474 speed = rtl8152_get_speed(tp);
2475 if (speed & _10bps) {
2476 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2477 ocp_data |= EEEP_CR_EEEP_TX;
2478 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2479 } else {
2480 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2481 ocp_data &= ~EEEP_CR_EEEP_TX;
2482 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2483 }
2484}
2485
2486static void rxdy_gated_en(struct r8152 *tp, bool enable)
2487{
2488 u32 ocp_data;
2489
2490 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2491 if (enable)
2492 ocp_data |= RXDY_GATED_EN;
2493 else
2494 ocp_data &= ~RXDY_GATED_EN;
2495 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2496}
2497
2498static int rtl_start_rx(struct r8152 *tp)
2499{
2500 struct rx_agg *agg, *agg_next;
2501 struct list_head tmp_list;
2502 unsigned long flags;
2503 int ret = 0, i = 0;
2504
2505 INIT_LIST_HEAD(&tmp_list);
2506
2507 spin_lock_irqsave(&tp->rx_lock, flags);
2508
2509 INIT_LIST_HEAD(&tp->rx_done);
2510 INIT_LIST_HEAD(&tp->rx_used);
2511
2512 list_splice_init(&tp->rx_info, &tmp_list);
2513
2514 spin_unlock_irqrestore(&tp->rx_lock, flags);
2515
2516 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2517 INIT_LIST_HEAD(&agg->list);
2518
2519 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2520 if (++i > RTL8152_MAX_RX) {
2521 spin_lock_irqsave(&tp->rx_lock, flags);
2522 list_add_tail(&agg->list, &tp->rx_used);
2523 spin_unlock_irqrestore(&tp->rx_lock, flags);
2524 } else if (unlikely(ret < 0)) {
2525 spin_lock_irqsave(&tp->rx_lock, flags);
2526 list_add_tail(&agg->list, &tp->rx_done);
2527 spin_unlock_irqrestore(&tp->rx_lock, flags);
2528 } else {
2529 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
2530 }
2531 }
2532
2533 spin_lock_irqsave(&tp->rx_lock, flags);
2534 WARN_ON(!list_empty(&tp->rx_info));
2535 list_splice(&tmp_list, &tp->rx_info);
2536 spin_unlock_irqrestore(&tp->rx_lock, flags);
2537
2538 return ret;
2539}
2540
2541static int rtl_stop_rx(struct r8152 *tp)
2542{
2543 struct rx_agg *agg, *agg_next;
2544 struct list_head tmp_list;
2545 unsigned long flags;
2546
2547 INIT_LIST_HEAD(&tmp_list);
2548
2549 /* The usb_kill_urb() couldn't be used in atomic.
2550 * Therefore, move the list of rx_info to a tmp one.
2551 * Then, list_for_each_entry_safe could be used without
2552 * spin lock.
2553 */
2554
2555 spin_lock_irqsave(&tp->rx_lock, flags);
2556 list_splice_init(&tp->rx_info, &tmp_list);
2557 spin_unlock_irqrestore(&tp->rx_lock, flags);
2558
2559 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2560 /* At least RTL8152_MAX_RX rx_agg have the page_count being
2561 * equal to 1, so the other ones could be freed safely.
2562 */
2563 if (page_count(agg->page) > 1)
2564 free_rx_agg(tp, agg);
2565 else
2566 usb_kill_urb(agg->urb);
2567 }
2568
2569 /* Move back the list of temp to the rx_info */
2570 spin_lock_irqsave(&tp->rx_lock, flags);
2571 WARN_ON(!list_empty(&tp->rx_info));
2572 list_splice(&tmp_list, &tp->rx_info);
2573 spin_unlock_irqrestore(&tp->rx_lock, flags);
2574
2575 while (!skb_queue_empty(&tp->rx_queue))
2576 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2577
2578 return 0;
2579}
2580
2581static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2582{
2583 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2584 OWN_UPDATE | OWN_CLEAR);
2585}
2586
2587static int rtl_enable(struct r8152 *tp)
2588{
2589 u32 ocp_data;
2590
2591 r8152b_reset_packet_filter(tp);
2592
2593 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2594 ocp_data |= CR_RE | CR_TE;
2595 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2596
2597 switch (tp->version) {
2598 case RTL_VER_08:
2599 case RTL_VER_09:
2600 r8153b_rx_agg_chg_indicate(tp);
2601 break;
2602 default:
2603 break;
2604 }
2605
2606 rxdy_gated_en(tp, false);
2607
2608 return 0;
2609}
2610
2611static int rtl8152_enable(struct r8152 *tp)
2612{
2613 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2614 return -ENODEV;
2615
2616 set_tx_qlen(tp);
2617 rtl_set_eee_plus(tp);
2618
2619 return rtl_enable(tp);
2620}
2621
2622static void r8153_set_rx_early_timeout(struct r8152 *tp)
2623{
2624 u32 ocp_data = tp->coalesce / 8;
2625
2626 switch (tp->version) {
2627 case RTL_VER_03:
2628 case RTL_VER_04:
2629 case RTL_VER_05:
2630 case RTL_VER_06:
2631 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2632 ocp_data);
2633 break;
2634
2635 case RTL_VER_08:
2636 case RTL_VER_09:
2637 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2638 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2639 */
2640 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2641 128 / 8);
2642 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2643 ocp_data);
2644 break;
2645
2646 default:
2647 break;
2648 }
2649}
2650
2651static void r8153_set_rx_early_size(struct r8152 *tp)
2652{
2653 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
2654
2655 switch (tp->version) {
2656 case RTL_VER_03:
2657 case RTL_VER_04:
2658 case RTL_VER_05:
2659 case RTL_VER_06:
2660 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2661 ocp_data / 4);
2662 break;
2663 case RTL_VER_08:
2664 case RTL_VER_09:
2665 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2666 ocp_data / 8);
2667 break;
2668 default:
2669 WARN_ON_ONCE(1);
2670 break;
2671 }
2672}
2673
2674static int rtl8153_enable(struct r8152 *tp)
2675{
2676 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2677 return -ENODEV;
2678
2679 set_tx_qlen(tp);
2680 rtl_set_eee_plus(tp);
2681 r8153_set_rx_early_timeout(tp);
2682 r8153_set_rx_early_size(tp);
2683
2684 return rtl_enable(tp);
2685}
2686
2687static void rtl_disable(struct r8152 *tp)
2688{
2689 u32 ocp_data;
2690 int i;
2691
2692 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2693 rtl_drop_queued_tx(tp);
2694 return;
2695 }
2696
2697 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2698 ocp_data &= ~RCR_ACPT_ALL;
2699 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2700
2701 rtl_drop_queued_tx(tp);
2702
2703 for (i = 0; i < RTL8152_MAX_TX; i++)
2704 usb_kill_urb(tp->tx_info[i].urb);
2705
2706 rxdy_gated_en(tp, true);
2707
2708 for (i = 0; i < 1000; i++) {
2709 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2710 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2711 break;
2712 usleep_range(1000, 2000);
2713 }
2714
2715 for (i = 0; i < 1000; i++) {
2716 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2717 break;
2718 usleep_range(1000, 2000);
2719 }
2720
2721 rtl_stop_rx(tp);
2722
2723 rtl8152_nic_reset(tp);
2724}
2725
2726static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2727{
2728 u32 ocp_data;
2729
2730 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2731 if (enable)
2732 ocp_data |= POWER_CUT;
2733 else
2734 ocp_data &= ~POWER_CUT;
2735 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2736
2737 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2738 ocp_data &= ~RESUME_INDICATE;
2739 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2740}
2741
2742static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2743{
2744 u32 ocp_data;
2745
2746 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2747 if (enable)
2748 ocp_data |= CPCR_RX_VLAN;
2749 else
2750 ocp_data &= ~CPCR_RX_VLAN;
2751 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2752}
2753
2754static int rtl8152_set_features(struct net_device *dev,
2755 netdev_features_t features)
2756{
2757 netdev_features_t changed = features ^ dev->features;
2758 struct r8152 *tp = netdev_priv(dev);
2759 int ret;
2760
2761 ret = usb_autopm_get_interface(tp->intf);
2762 if (ret < 0)
2763 goto out;
2764
2765 mutex_lock(&tp->control);
2766
2767 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2768 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2769 rtl_rx_vlan_en(tp, true);
2770 else
2771 rtl_rx_vlan_en(tp, false);
2772 }
2773
2774 mutex_unlock(&tp->control);
2775
2776 usb_autopm_put_interface(tp->intf);
2777
2778out:
2779 return ret;
2780}
2781
2782#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2783
2784static u32 __rtl_get_wol(struct r8152 *tp)
2785{
2786 u32 ocp_data;
2787 u32 wolopts = 0;
2788
2789 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2790 if (ocp_data & LINK_ON_WAKE_EN)
2791 wolopts |= WAKE_PHY;
2792
2793 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2794 if (ocp_data & UWF_EN)
2795 wolopts |= WAKE_UCAST;
2796 if (ocp_data & BWF_EN)
2797 wolopts |= WAKE_BCAST;
2798 if (ocp_data & MWF_EN)
2799 wolopts |= WAKE_MCAST;
2800
2801 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2802 if (ocp_data & MAGIC_EN)
2803 wolopts |= WAKE_MAGIC;
2804
2805 return wolopts;
2806}
2807
2808static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2809{
2810 u32 ocp_data;
2811
2812 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2813
2814 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2815 ocp_data &= ~LINK_ON_WAKE_EN;
2816 if (wolopts & WAKE_PHY)
2817 ocp_data |= LINK_ON_WAKE_EN;
2818 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2819
2820 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2821 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
2822 if (wolopts & WAKE_UCAST)
2823 ocp_data |= UWF_EN;
2824 if (wolopts & WAKE_BCAST)
2825 ocp_data |= BWF_EN;
2826 if (wolopts & WAKE_MCAST)
2827 ocp_data |= MWF_EN;
2828 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2829
2830 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2831
2832 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2833 ocp_data &= ~MAGIC_EN;
2834 if (wolopts & WAKE_MAGIC)
2835 ocp_data |= MAGIC_EN;
2836 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2837
2838 if (wolopts & WAKE_ANY)
2839 device_set_wakeup_enable(&tp->udev->dev, true);
2840 else
2841 device_set_wakeup_enable(&tp->udev->dev, false);
2842}
2843
2844static void r8153_u1u2en(struct r8152 *tp, bool enable)
2845{
2846 u8 u1u2[8];
2847
2848 if (enable)
2849 memset(u1u2, 0xff, sizeof(u1u2));
2850 else
2851 memset(u1u2, 0x00, sizeof(u1u2));
2852
2853 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2854}
2855
2856static void r8153b_u1u2en(struct r8152 *tp, bool enable)
2857{
2858 u32 ocp_data;
2859
2860 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
2861 if (enable)
2862 ocp_data |= LPM_U1U2_EN;
2863 else
2864 ocp_data &= ~LPM_U1U2_EN;
2865
2866 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
2867}
2868
2869static void r8153_u2p3en(struct r8152 *tp, bool enable)
2870{
2871 u32 ocp_data;
2872
2873 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2874 if (enable)
2875 ocp_data |= U2P3_ENABLE;
2876 else
2877 ocp_data &= ~U2P3_ENABLE;
2878 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2879}
2880
2881static void r8153b_ups_flags(struct r8152 *tp)
2882{
2883 u32 ups_flags = 0;
2884
2885 if (tp->ups_info.green)
2886 ups_flags |= UPS_FLAGS_EN_GREEN;
2887
2888 if (tp->ups_info.aldps)
2889 ups_flags |= UPS_FLAGS_EN_ALDPS;
2890
2891 if (tp->ups_info.eee)
2892 ups_flags |= UPS_FLAGS_EN_EEE;
2893
2894 if (tp->ups_info.flow_control)
2895 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
2896
2897 if (tp->ups_info.eee_ckdiv)
2898 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
2899
2900 if (tp->ups_info.eee_cmod_lv)
2901 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
2902
2903 if (tp->ups_info._10m_ckdiv)
2904 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
2905
2906 if (tp->ups_info.eee_plloff_100)
2907 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
2908
2909 if (tp->ups_info.eee_plloff_giga)
2910 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
2911
2912 if (tp->ups_info._250m_ckdiv)
2913 ups_flags |= UPS_FLAGS_250M_CKDIV;
2914
2915 if (tp->ups_info.ctap_short_off)
2916 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
2917
2918 switch (tp->ups_info.speed_duplex) {
2919 case NWAY_10M_HALF:
2920 ups_flags |= ups_flags_speed(1);
2921 break;
2922 case NWAY_10M_FULL:
2923 ups_flags |= ups_flags_speed(2);
2924 break;
2925 case NWAY_100M_HALF:
2926 ups_flags |= ups_flags_speed(3);
2927 break;
2928 case NWAY_100M_FULL:
2929 ups_flags |= ups_flags_speed(4);
2930 break;
2931 case NWAY_1000M_FULL:
2932 ups_flags |= ups_flags_speed(5);
2933 break;
2934 case FORCE_10M_HALF:
2935 ups_flags |= ups_flags_speed(6);
2936 break;
2937 case FORCE_10M_FULL:
2938 ups_flags |= ups_flags_speed(7);
2939 break;
2940 case FORCE_100M_HALF:
2941 ups_flags |= ups_flags_speed(8);
2942 break;
2943 case FORCE_100M_FULL:
2944 ups_flags |= ups_flags_speed(9);
2945 break;
2946 default:
2947 break;
2948 }
2949
2950 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
2951}
2952
2953static void r8153b_green_en(struct r8152 *tp, bool enable)
2954{
2955 u16 data;
2956
2957 if (enable) {
2958 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
2959 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
2960 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
2961 } else {
2962 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
2963 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
2964 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
2965 }
2966
2967 data = sram_read(tp, SRAM_GREEN_CFG);
2968 data |= GREEN_ETH_EN;
2969 sram_write(tp, SRAM_GREEN_CFG, data);
2970
2971 tp->ups_info.green = enable;
2972}
2973
2974static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
2975{
2976 u16 data;
2977 int i;
2978
2979 for (i = 0; i < 500; i++) {
2980 data = ocp_reg_read(tp, OCP_PHY_STATUS);
2981 data &= PHY_STAT_MASK;
2982 if (desired) {
2983 if (data == desired)
2984 break;
2985 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
2986 data == PHY_STAT_EXT_INIT) {
2987 break;
2988 }
2989
2990 msleep(20);
2991 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2992 break;
2993 }
2994
2995 return data;
2996}
2997
2998static void r8153b_ups_en(struct r8152 *tp, bool enable)
2999{
3000 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3001
3002 if (enable) {
3003 r8153b_ups_flags(tp);
3004
3005 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3006 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3007
3008 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
3009 ocp_data |= BIT(0);
3010 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
3011 } else {
3012 u16 data;
3013
3014 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3015 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3016
3017 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
3018 ocp_data &= ~BIT(0);
3019 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
3020
3021 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3022 ocp_data &= ~PCUT_STATUS;
3023 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3024
3025 data = r8153_phy_status(tp, 0);
3026
3027 switch (data) {
3028 case PHY_STAT_PWRDN:
3029 case PHY_STAT_EXT_INIT:
3030 r8153b_green_en(tp,
3031 test_bit(GREEN_ETHERNET, &tp->flags));
3032
3033 data = r8152_mdio_read(tp, MII_BMCR);
3034 data &= ~BMCR_PDOWN;
3035 data |= BMCR_RESET;
3036 r8152_mdio_write(tp, MII_BMCR, data);
3037
3038 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
3039 /* fall through */
3040
3041 default:
3042 if (data != PHY_STAT_LAN_ON)
3043 netif_warn(tp, link, tp->netdev,
3044 "PHY not ready");
3045 break;
3046 }
3047 }
3048}
3049
3050static void r8153_power_cut_en(struct r8152 *tp, bool enable)
3051{
3052 u32 ocp_data;
3053
3054 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3055 if (enable)
3056 ocp_data |= PWR_EN | PHASE2_EN;
3057 else
3058 ocp_data &= ~(PWR_EN | PHASE2_EN);
3059 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3060
3061 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3062 ocp_data &= ~PCUT_STATUS;
3063 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3064}
3065
3066static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
3067{
3068 u32 ocp_data;
3069
3070 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3071 if (enable)
3072 ocp_data |= PWR_EN | PHASE2_EN;
3073 else
3074 ocp_data &= ~PWR_EN;
3075 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3076
3077 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3078 ocp_data &= ~PCUT_STATUS;
3079 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3080}
3081
3082static void r8153_queue_wake(struct r8152 *tp, bool enable)
3083{
3084 u32 ocp_data;
3085
3086 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
3087 if (enable)
3088 ocp_data |= UPCOMING_RUNTIME_D3;
3089 else
3090 ocp_data &= ~UPCOMING_RUNTIME_D3;
3091 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
3092
3093 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3094 ocp_data &= ~LINK_CHG_EVENT;
3095 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3096
3097 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3098 ocp_data &= ~LINK_CHANGE_FLAG;
3099 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
3100}
3101
3102static bool rtl_can_wakeup(struct r8152 *tp)
3103{
3104 struct usb_device *udev = tp->udev;
3105
3106 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3107}
3108
3109static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3110{
3111 if (enable) {
3112 u32 ocp_data;
3113
3114 __rtl_set_wol(tp, WAKE_ANY);
3115
3116 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3117
3118 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3119 ocp_data |= LINK_OFF_WAKE_EN;
3120 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3121
3122 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3123 } else {
3124 u32 ocp_data;
3125
3126 __rtl_set_wol(tp, tp->saved_wolopts);
3127
3128 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3129
3130 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3131 ocp_data &= ~LINK_OFF_WAKE_EN;
3132 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3133
3134 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3135 }
3136}
3137
3138static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3139{
3140 if (enable) {
3141 r8153_u1u2en(tp, false);
3142 r8153_u2p3en(tp, false);
3143 rtl_runtime_suspend_enable(tp, true);
3144 } else {
3145 rtl_runtime_suspend_enable(tp, false);
3146
3147 switch (tp->version) {
3148 case RTL_VER_03:
3149 case RTL_VER_04:
3150 break;
3151 case RTL_VER_05:
3152 case RTL_VER_06:
3153 default:
3154 r8153_u2p3en(tp, true);
3155 break;
3156 }
3157
3158 r8153_u1u2en(tp, true);
3159 }
3160}
3161
3162static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3163{
3164 if (enable) {
3165 r8153_queue_wake(tp, true);
3166 r8153b_u1u2en(tp, false);
3167 r8153_u2p3en(tp, false);
3168 rtl_runtime_suspend_enable(tp, true);
3169 r8153b_ups_en(tp, true);
3170 } else {
3171 r8153b_ups_en(tp, false);
3172 r8153_queue_wake(tp, false);
3173 rtl_runtime_suspend_enable(tp, false);
3174 r8153b_u1u2en(tp, true);
3175 }
3176}
3177
3178static void r8153_teredo_off(struct r8152 *tp)
3179{
3180 u32 ocp_data;
3181
3182 switch (tp->version) {
3183 case RTL_VER_01:
3184 case RTL_VER_02:
3185 case RTL_VER_03:
3186 case RTL_VER_04:
3187 case RTL_VER_05:
3188 case RTL_VER_06:
3189 case RTL_VER_07:
3190 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3191 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3192 OOB_TEREDO_EN);
3193 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3194 break;
3195
3196 case RTL_VER_08:
3197 case RTL_VER_09:
3198 /* The bit 0 ~ 7 are relative with teredo settings. They are
3199 * W1C (write 1 to clear), so set all 1 to disable it.
3200 */
3201 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3202 break;
3203
3204 default:
3205 break;
3206 }
3207
3208 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3209 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3210 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3211}
3212
3213static void rtl_reset_bmu(struct r8152 *tp)
3214{
3215 u32 ocp_data;
3216
3217 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3218 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3219 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3220 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3221 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3222}
3223
3224static void r8152_aldps_en(struct r8152 *tp, bool enable)
3225{
3226 if (enable) {
3227 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
3228 LINKENA | DIS_SDSAVE);
3229 } else {
3230 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
3231 DIS_SDSAVE);
3232 msleep(20);
3233 }
3234}
3235
3236static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
3237{
3238 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3239 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3240 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3241}
3242
3243static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3244{
3245 u16 data;
3246
3247 r8152_mmd_indirect(tp, dev, reg);
3248 data = ocp_reg_read(tp, OCP_EEE_DATA);
3249 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3250
3251 return data;
3252}
3253
3254static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3255{
3256 r8152_mmd_indirect(tp, dev, reg);
3257 ocp_reg_write(tp, OCP_EEE_DATA, data);
3258 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3259}
3260
3261static void r8152_eee_en(struct r8152 *tp, bool enable)
3262{
3263 u16 config1, config2, config3;
3264 u32 ocp_data;
3265
3266 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3267 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3268 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3269 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3270
3271 if (enable) {
3272 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3273 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3274 config1 |= sd_rise_time(1);
3275 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3276 config3 |= fast_snr(42);
3277 } else {
3278 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3279 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3280 RX_QUIET_EN);
3281 config1 |= sd_rise_time(7);
3282 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3283 config3 |= fast_snr(511);
3284 }
3285
3286 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3287 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3288 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3289 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3290}
3291
3292static void r8153_eee_en(struct r8152 *tp, bool enable)
3293{
3294 u32 ocp_data;
3295 u16 config;
3296
3297 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3298 config = ocp_reg_read(tp, OCP_EEE_CFG);
3299
3300 if (enable) {
3301 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3302 config |= EEE10_EN;
3303 } else {
3304 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3305 config &= ~EEE10_EN;
3306 }
3307
3308 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3309 ocp_reg_write(tp, OCP_EEE_CFG, config);
3310
3311 tp->ups_info.eee = enable;
3312}
3313
3314static void rtl_eee_enable(struct r8152 *tp, bool enable)
3315{
3316 switch (tp->version) {
3317 case RTL_VER_01:
3318 case RTL_VER_02:
3319 case RTL_VER_07:
3320 if (enable) {
3321 r8152_eee_en(tp, true);
3322 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
3323 tp->eee_adv);
3324 } else {
3325 r8152_eee_en(tp, false);
3326 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
3327 }
3328 break;
3329 case RTL_VER_03:
3330 case RTL_VER_04:
3331 case RTL_VER_05:
3332 case RTL_VER_06:
3333 case RTL_VER_08:
3334 case RTL_VER_09:
3335 if (enable) {
3336 r8153_eee_en(tp, true);
3337 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
3338 } else {
3339 r8153_eee_en(tp, false);
3340 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3341 }
3342 break;
3343 default:
3344 break;
3345 }
3346}
3347
3348static void r8152b_enable_fc(struct r8152 *tp)
3349{
3350 u16 anar;
3351
3352 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3353 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3354 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3355
3356 tp->ups_info.flow_control = true;
3357}
3358
3359static void rtl8152_disable(struct r8152 *tp)
3360{
3361 r8152_aldps_en(tp, false);
3362 rtl_disable(tp);
3363 r8152_aldps_en(tp, true);
3364}
3365
3366static void r8152b_hw_phy_cfg(struct r8152 *tp)
3367{
3368 rtl_eee_enable(tp, tp->eee_en);
3369 r8152_aldps_en(tp, true);
3370 r8152b_enable_fc(tp);
3371
3372 set_bit(PHY_RESET, &tp->flags);
3373}
3374
3375static void wait_oob_link_list_ready(struct r8152 *tp)
3376{
3377 u32 ocp_data;
3378 int i;
3379
3380 for (i = 0; i < 1000; i++) {
3381 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3382 if (ocp_data & LINK_LIST_READY)
3383 break;
3384 usleep_range(1000, 2000);
3385 }
3386}
3387
3388static void r8152b_exit_oob(struct r8152 *tp)
3389{
3390 u32 ocp_data;
3391
3392 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3393 ocp_data &= ~RCR_ACPT_ALL;
3394 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3395
3396 rxdy_gated_en(tp, true);
3397 r8153_teredo_off(tp);
3398 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3399 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
3400
3401 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3402 ocp_data &= ~NOW_IS_OOB;
3403 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3404
3405 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3406 ocp_data &= ~MCU_BORW_EN;
3407 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3408
3409 wait_oob_link_list_ready(tp);
3410
3411 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3412 ocp_data |= RE_INIT_LL;
3413 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3414
3415 wait_oob_link_list_ready(tp);
3416
3417 rtl8152_nic_reset(tp);
3418
3419 /* rx share fifo credit full threshold */
3420 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3421
3422 if (tp->udev->speed == USB_SPEED_FULL ||
3423 tp->udev->speed == USB_SPEED_LOW) {
3424 /* rx share fifo credit near full threshold */
3425 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3426 RXFIFO_THR2_FULL);
3427 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3428 RXFIFO_THR3_FULL);
3429 } else {
3430 /* rx share fifo credit near full threshold */
3431 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3432 RXFIFO_THR2_HIGH);
3433 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3434 RXFIFO_THR3_HIGH);
3435 }
3436
3437 /* TX share fifo free credit full threshold */
3438 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
3439
3440 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
3441 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
3442 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
3443 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
3444
3445 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
3446
3447 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3448
3449 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3450 ocp_data |= TCR0_AUTO_FIFO;
3451 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3452}
3453
3454static void r8152b_enter_oob(struct r8152 *tp)
3455{
3456 u32 ocp_data;
3457
3458 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3459 ocp_data &= ~NOW_IS_OOB;
3460 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3461
3462 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
3463 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
3464 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
3465
3466 rtl_disable(tp);
3467
3468 wait_oob_link_list_ready(tp);
3469
3470 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3471 ocp_data |= RE_INIT_LL;
3472 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3473
3474 wait_oob_link_list_ready(tp);
3475
3476 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3477
3478 rtl_rx_vlan_en(tp, true);
3479
3480 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
3481 ocp_data |= ALDPS_PROXY_MODE;
3482 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
3483
3484 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3485 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3486 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3487
3488 rxdy_gated_en(tp, false);
3489
3490 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3491 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3492 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3493}
3494
3495static int r8153_patch_request(struct r8152 *tp, bool request)
3496{
3497 u16 data;
3498 int i;
3499
3500 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3501 if (request)
3502 data |= PATCH_REQUEST;
3503 else
3504 data &= ~PATCH_REQUEST;
3505 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3506
3507 for (i = 0; request && i < 5000; i++) {
3508 usleep_range(1000, 2000);
3509 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3510 break;
3511 }
3512
3513 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3514 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3515 r8153_patch_request(tp, false);
3516 return -ETIME;
3517 } else {
3518 return 0;
3519 }
3520}
3521
3522static void r8153_aldps_en(struct r8152 *tp, bool enable)
3523{
3524 u16 data;
3525
3526 data = ocp_reg_read(tp, OCP_POWER_CFG);
3527 if (enable) {
3528 data |= EN_ALDPS;
3529 ocp_reg_write(tp, OCP_POWER_CFG, data);
3530 } else {
3531 int i;
3532
3533 data &= ~EN_ALDPS;
3534 ocp_reg_write(tp, OCP_POWER_CFG, data);
3535 for (i = 0; i < 20; i++) {
3536 usleep_range(1000, 2000);
3537 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
3538 break;
3539 }
3540 }
3541
3542 tp->ups_info.aldps = enable;
3543}
3544
3545static void r8153_hw_phy_cfg(struct r8152 *tp)
3546{
3547 u32 ocp_data;
3548 u16 data;
3549
3550 /* disable ALDPS before updating the PHY parameters */
3551 r8153_aldps_en(tp, false);
3552
3553 /* disable EEE before updating the PHY parameters */
3554 rtl_eee_enable(tp, false);
3555
3556 if (tp->version == RTL_VER_03) {
3557 data = ocp_reg_read(tp, OCP_EEE_CFG);
3558 data &= ~CTAP_SHORT_EN;
3559 ocp_reg_write(tp, OCP_EEE_CFG, data);
3560 }
3561
3562 data = ocp_reg_read(tp, OCP_POWER_CFG);
3563 data |= EEE_CLKDIV_EN;
3564 ocp_reg_write(tp, OCP_POWER_CFG, data);
3565
3566 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3567 data |= EN_10M_BGOFF;
3568 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3569 data = ocp_reg_read(tp, OCP_POWER_CFG);
3570 data |= EN_10M_PLLOFF;
3571 ocp_reg_write(tp, OCP_POWER_CFG, data);
3572 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
3573
3574 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3575 ocp_data |= PFM_PWM_SWITCH;
3576 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3577
3578 /* Enable LPF corner auto tune */
3579 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
3580
3581 /* Adjust 10M Amplitude */
3582 sram_write(tp, SRAM_10M_AMP1, 0x00af);
3583 sram_write(tp, SRAM_10M_AMP2, 0x0208);
3584
3585 if (tp->eee_en)
3586 rtl_eee_enable(tp, true);
3587
3588 r8153_aldps_en(tp, true);
3589 r8152b_enable_fc(tp);
3590
3591 switch (tp->version) {
3592 case RTL_VER_03:
3593 case RTL_VER_04:
3594 break;
3595 case RTL_VER_05:
3596 case RTL_VER_06:
3597 default:
3598 r8153_u2p3en(tp, true);
3599 break;
3600 }
3601
3602 set_bit(PHY_RESET, &tp->flags);
3603}
3604
3605static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
3606{
3607 u32 ocp_data;
3608
3609 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
3610 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
3611 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
3612 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
3613
3614 return ocp_data;
3615}
3616
3617static void r8153b_hw_phy_cfg(struct r8152 *tp)
3618{
3619 u32 ocp_data;
3620 u16 data;
3621
3622 /* disable ALDPS before updating the PHY parameters */
3623 r8153_aldps_en(tp, false);
3624
3625 /* disable EEE before updating the PHY parameters */
3626 rtl_eee_enable(tp, false);
3627
3628 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
3629
3630 data = sram_read(tp, SRAM_GREEN_CFG);
3631 data |= R_TUNE_EN;
3632 sram_write(tp, SRAM_GREEN_CFG, data);
3633 data = ocp_reg_read(tp, OCP_NCTL_CFG);
3634 data |= PGA_RETURN_EN;
3635 ocp_reg_write(tp, OCP_NCTL_CFG, data);
3636
3637 /* ADC Bias Calibration:
3638 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
3639 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
3640 * ADC ioffset.
3641 */
3642 ocp_data = r8152_efuse_read(tp, 0x7d);
3643 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
3644 if (data != 0xffff)
3645 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
3646
3647 /* ups mode tx-link-pulse timing adjustment:
3648 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
3649 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
3650 */
3651 ocp_data = ocp_reg_read(tp, 0xc426);
3652 ocp_data &= 0x3fff;
3653 if (ocp_data) {
3654 u32 swr_cnt_1ms_ini;
3655
3656 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
3657 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
3658 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
3659 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
3660 }
3661
3662 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3663 ocp_data |= PFM_PWM_SWITCH;
3664 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3665
3666 /* Advnace EEE */
3667 if (!r8153_patch_request(tp, true)) {
3668 data = ocp_reg_read(tp, OCP_POWER_CFG);
3669 data |= EEE_CLKDIV_EN;
3670 ocp_reg_write(tp, OCP_POWER_CFG, data);
3671 tp->ups_info.eee_ckdiv = true;
3672
3673 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3674 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
3675 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3676 tp->ups_info.eee_cmod_lv = true;
3677 tp->ups_info._10m_ckdiv = true;
3678 tp->ups_info.eee_plloff_giga = true;
3679
3680 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
3681 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
3682 tp->ups_info._250m_ckdiv = true;
3683
3684 r8153_patch_request(tp, false);
3685 }
3686
3687 if (tp->eee_en)
3688 rtl_eee_enable(tp, true);
3689
3690 r8153_aldps_en(tp, true);
3691 r8152b_enable_fc(tp);
3692
3693 set_bit(PHY_RESET, &tp->flags);
3694}
3695
3696static void r8153_first_init(struct r8152 *tp)
3697{
3698 u32 ocp_data;
3699
3700 rxdy_gated_en(tp, true);
3701 r8153_teredo_off(tp);
3702
3703 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3704 ocp_data &= ~RCR_ACPT_ALL;
3705 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3706
3707 rtl8152_nic_reset(tp);
3708 rtl_reset_bmu(tp);
3709
3710 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3711 ocp_data &= ~NOW_IS_OOB;
3712 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3713
3714 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3715 ocp_data &= ~MCU_BORW_EN;
3716 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3717
3718 wait_oob_link_list_ready(tp);
3719
3720 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3721 ocp_data |= RE_INIT_LL;
3722 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3723
3724 wait_oob_link_list_ready(tp);
3725
3726 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
3727
3728 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
3729 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
3730 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
3731
3732 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3733 ocp_data |= TCR0_AUTO_FIFO;
3734 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3735
3736 rtl8152_nic_reset(tp);
3737
3738 /* rx share fifo credit full threshold */
3739 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3740 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
3741 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
3742 /* TX share fifo free credit full threshold */
3743 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
3744}
3745
3746static void r8153_enter_oob(struct r8152 *tp)
3747{
3748 u32 ocp_data;
3749
3750 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3751 ocp_data &= ~NOW_IS_OOB;
3752 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3753
3754 rtl_disable(tp);
3755 rtl_reset_bmu(tp);
3756
3757 wait_oob_link_list_ready(tp);
3758
3759 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3760 ocp_data |= RE_INIT_LL;
3761 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3762
3763 wait_oob_link_list_ready(tp);
3764
3765 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
3766 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
3767
3768 switch (tp->version) {
3769 case RTL_VER_03:
3770 case RTL_VER_04:
3771 case RTL_VER_05:
3772 case RTL_VER_06:
3773 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3774 ocp_data &= ~TEREDO_WAKE_MASK;
3775 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3776 break;
3777
3778 case RTL_VER_08:
3779 case RTL_VER_09:
3780 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
3781 * type. Set it to zero. bits[7:0] are the W1C bits about
3782 * the events. Set them to all 1 to clear them.
3783 */
3784 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
3785 break;
3786
3787 default:
3788 break;
3789 }
3790
3791 rtl_rx_vlan_en(tp, true);
3792
3793 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
3794 ocp_data |= ALDPS_PROXY_MODE;
3795 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
3796
3797 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3798 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3799 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3800
3801 rxdy_gated_en(tp, false);
3802
3803 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3804 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3805 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3806}
3807
3808static void rtl8153_disable(struct r8152 *tp)
3809{
3810 r8153_aldps_en(tp, false);
3811 rtl_disable(tp);
3812 rtl_reset_bmu(tp);
3813 r8153_aldps_en(tp, true);
3814}
3815
3816static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
3817 u32 advertising)
3818{
3819 u16 bmcr;
3820 int ret = 0;
3821
3822 if (autoneg == AUTONEG_DISABLE) {
3823 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
3824 return -EINVAL;
3825
3826 switch (speed) {
3827 case SPEED_10:
3828 bmcr = BMCR_SPEED10;
3829 if (duplex == DUPLEX_FULL) {
3830 bmcr |= BMCR_FULLDPLX;
3831 tp->ups_info.speed_duplex = FORCE_10M_FULL;
3832 } else {
3833 tp->ups_info.speed_duplex = FORCE_10M_HALF;
3834 }
3835 break;
3836 case SPEED_100:
3837 bmcr = BMCR_SPEED100;
3838 if (duplex == DUPLEX_FULL) {
3839 bmcr |= BMCR_FULLDPLX;
3840 tp->ups_info.speed_duplex = FORCE_100M_FULL;
3841 } else {
3842 tp->ups_info.speed_duplex = FORCE_100M_HALF;
3843 }
3844 break;
3845 case SPEED_1000:
3846 if (tp->mii.supports_gmii) {
3847 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
3848 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
3849 break;
3850 }
3851 /* fall through */
3852 default:
3853 ret = -EINVAL;
3854 goto out;
3855 }
3856
3857 if (duplex == DUPLEX_FULL)
3858 tp->mii.full_duplex = 1;
3859 else
3860 tp->mii.full_duplex = 0;
3861
3862 tp->mii.force_media = 1;
3863 } else {
3864 u16 anar, tmp1;
3865 u32 support;
3866
3867 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
3868 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
3869
3870 if (tp->mii.supports_gmii)
3871 support |= RTL_ADVERTISED_1000_FULL;
3872
3873 if (!(advertising & support))
3874 return -EINVAL;
3875
3876 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3877 tmp1 = anar & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
3878 ADVERTISE_100HALF | ADVERTISE_100FULL);
3879 if (advertising & RTL_ADVERTISED_10_HALF) {
3880 tmp1 |= ADVERTISE_10HALF;
3881 tp->ups_info.speed_duplex = NWAY_10M_HALF;
3882 }
3883 if (advertising & RTL_ADVERTISED_10_FULL) {
3884 tmp1 |= ADVERTISE_10FULL;
3885 tp->ups_info.speed_duplex = NWAY_10M_FULL;
3886 }
3887
3888 if (advertising & RTL_ADVERTISED_100_HALF) {
3889 tmp1 |= ADVERTISE_100HALF;
3890 tp->ups_info.speed_duplex = NWAY_100M_HALF;
3891 }
3892 if (advertising & RTL_ADVERTISED_100_FULL) {
3893 tmp1 |= ADVERTISE_100FULL;
3894 tp->ups_info.speed_duplex = NWAY_100M_FULL;
3895 }
3896
3897 if (anar != tmp1) {
3898 r8152_mdio_write(tp, MII_ADVERTISE, tmp1);
3899 tp->mii.advertising = tmp1;
3900 }
3901
3902 if (tp->mii.supports_gmii) {
3903 u16 gbcr;
3904
3905 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
3906 tmp1 = gbcr & ~(ADVERTISE_1000FULL |
3907 ADVERTISE_1000HALF);
3908
3909 if (advertising & RTL_ADVERTISED_1000_FULL) {
3910 tmp1 |= ADVERTISE_1000FULL;
3911 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
3912 }
3913
3914 if (gbcr != tmp1)
3915 r8152_mdio_write(tp, MII_CTRL1000, tmp1);
3916 }
3917
3918 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
3919
3920 tp->mii.force_media = 0;
3921 }
3922
3923 if (test_and_clear_bit(PHY_RESET, &tp->flags))
3924 bmcr |= BMCR_RESET;
3925
3926 r8152_mdio_write(tp, MII_BMCR, bmcr);
3927
3928 if (bmcr & BMCR_RESET) {
3929 int i;
3930
3931 for (i = 0; i < 50; i++) {
3932 msleep(20);
3933 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
3934 break;
3935 }
3936 }
3937
3938out:
3939 return ret;
3940}
3941
3942static void rtl8152_up(struct r8152 *tp)
3943{
3944 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3945 return;
3946
3947 r8152_aldps_en(tp, false);
3948 r8152b_exit_oob(tp);
3949 r8152_aldps_en(tp, true);
3950}
3951
3952static void rtl8152_down(struct r8152 *tp)
3953{
3954 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3955 rtl_drop_queued_tx(tp);
3956 return;
3957 }
3958
3959 r8152_power_cut_en(tp, false);
3960 r8152_aldps_en(tp, false);
3961 r8152b_enter_oob(tp);
3962 r8152_aldps_en(tp, true);
3963}
3964
3965static void rtl8153_up(struct r8152 *tp)
3966{
3967 u32 ocp_data;
3968
3969 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3970 return;
3971
3972 r8153_u1u2en(tp, false);
3973 r8153_u2p3en(tp, false);
3974 r8153_aldps_en(tp, false);
3975 r8153_first_init(tp);
3976
3977 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
3978 ocp_data |= LANWAKE_CLR_EN;
3979 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
3980
3981 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
3982 ocp_data &= ~LANWAKE_PIN;
3983 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
3984
3985 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1);
3986 ocp_data &= ~DELAY_PHY_PWR_CHG;
3987 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data);
3988
3989 r8153_aldps_en(tp, true);
3990
3991 switch (tp->version) {
3992 case RTL_VER_03:
3993 case RTL_VER_04:
3994 break;
3995 case RTL_VER_05:
3996 case RTL_VER_06:
3997 default:
3998 r8153_u2p3en(tp, true);
3999 break;
4000 }
4001
4002 r8153_u1u2en(tp, true);
4003}
4004
4005static void rtl8153_down(struct r8152 *tp)
4006{
4007 u32 ocp_data;
4008
4009 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
4010 rtl_drop_queued_tx(tp);
4011 return;
4012 }
4013
4014 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
4015 ocp_data &= ~LANWAKE_CLR_EN;
4016 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
4017
4018 r8153_u1u2en(tp, false);
4019 r8153_u2p3en(tp, false);
4020 r8153_power_cut_en(tp, false);
4021 r8153_aldps_en(tp, false);
4022 r8153_enter_oob(tp);
4023 r8153_aldps_en(tp, true);
4024}
4025
4026static void rtl8153b_up(struct r8152 *tp)
4027{
4028 u32 ocp_data;
4029
4030 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4031 return;
4032
4033 r8153b_u1u2en(tp, false);
4034 r8153_u2p3en(tp, false);
4035 r8153_aldps_en(tp, false);
4036
4037 r8153_first_init(tp);
4038 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
4039
4040 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
4041 ocp_data &= ~PLA_MCU_SPDWN_EN;
4042 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
4043
4044 r8153_aldps_en(tp, true);
4045 r8153b_u1u2en(tp, true);
4046}
4047
4048static void rtl8153b_down(struct r8152 *tp)
4049{
4050 u32 ocp_data;
4051
4052 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
4053 rtl_drop_queued_tx(tp);
4054 return;
4055 }
4056
4057 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
4058 ocp_data |= PLA_MCU_SPDWN_EN;
4059 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
4060
4061 r8153b_u1u2en(tp, false);
4062 r8153_u2p3en(tp, false);
4063 r8153b_power_cut_en(tp, false);
4064 r8153_aldps_en(tp, false);
4065 r8153_enter_oob(tp);
4066 r8153_aldps_en(tp, true);
4067}
4068
4069static bool rtl8152_in_nway(struct r8152 *tp)
4070{
4071 u16 nway_state;
4072
4073 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
4074 tp->ocp_base = 0x2000;
4075 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
4076 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
4077
4078 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
4079 if (nway_state & 0xc000)
4080 return false;
4081 else
4082 return true;
4083}
4084
4085static bool rtl8153_in_nway(struct r8152 *tp)
4086{
4087 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
4088
4089 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
4090 return false;
4091 else
4092 return true;
4093}
4094
4095static void set_carrier(struct r8152 *tp)
4096{
4097 struct net_device *netdev = tp->netdev;
4098 struct napi_struct *napi = &tp->napi;
4099 u8 speed;
4100
4101 speed = rtl8152_get_speed(tp);
4102
4103 if (speed & LINK_STATUS) {
4104 if (!netif_carrier_ok(netdev)) {
4105 tp->rtl_ops.enable(tp);
4106 netif_stop_queue(netdev);
4107 napi_disable(napi);
4108 netif_carrier_on(netdev);
4109 rtl_start_rx(tp);
4110 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
4111 _rtl8152_set_rx_mode(netdev);
4112 napi_enable(&tp->napi);
4113 netif_wake_queue(netdev);
4114 netif_info(tp, link, netdev, "carrier on\n");
4115 } else if (netif_queue_stopped(netdev) &&
4116 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
4117 netif_wake_queue(netdev);
4118 }
4119 } else {
4120 if (netif_carrier_ok(netdev)) {
4121 netif_carrier_off(netdev);
4122 tasklet_disable(&tp->tx_tl);
4123 napi_disable(napi);
4124 tp->rtl_ops.disable(tp);
4125 napi_enable(napi);
4126 tasklet_enable(&tp->tx_tl);
4127 netif_info(tp, link, netdev, "carrier off\n");
4128 }
4129 }
4130}
4131
4132static void rtl_work_func_t(struct work_struct *work)
4133{
4134 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
4135
4136 /* If the device is unplugged or !netif_running(), the workqueue
4137 * doesn't need to wake the device, and could return directly.
4138 */
4139 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
4140 return;
4141
4142 if (usb_autopm_get_interface(tp->intf) < 0)
4143 return;
4144
4145 if (!test_bit(WORK_ENABLE, &tp->flags))
4146 goto out1;
4147
4148 if (!mutex_trylock(&tp->control)) {
4149 schedule_delayed_work(&tp->schedule, 0);
4150 goto out1;
4151 }
4152
4153 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
4154 set_carrier(tp);
4155
4156 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
4157 _rtl8152_set_rx_mode(tp->netdev);
4158
4159 /* don't schedule tasket before linking */
4160 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
4161 netif_carrier_ok(tp->netdev))
4162 tasklet_schedule(&tp->tx_tl);
4163
4164 mutex_unlock(&tp->control);
4165
4166out1:
4167 usb_autopm_put_interface(tp->intf);
4168}
4169
4170static void rtl_hw_phy_work_func_t(struct work_struct *work)
4171{
4172 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
4173
4174 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4175 return;
4176
4177 if (usb_autopm_get_interface(tp->intf) < 0)
4178 return;
4179
4180 mutex_lock(&tp->control);
4181
4182 tp->rtl_ops.hw_phy_cfg(tp);
4183
4184 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
4185 tp->advertising);
4186
4187 mutex_unlock(&tp->control);
4188
4189 usb_autopm_put_interface(tp->intf);
4190}
4191
4192#ifdef CONFIG_PM_SLEEP
4193static int rtl_notifier(struct notifier_block *nb, unsigned long action,
4194 void *data)
4195{
4196 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
4197
4198 switch (action) {
4199 case PM_HIBERNATION_PREPARE:
4200 case PM_SUSPEND_PREPARE:
4201 usb_autopm_get_interface(tp->intf);
4202 break;
4203
4204 case PM_POST_HIBERNATION:
4205 case PM_POST_SUSPEND:
4206 usb_autopm_put_interface(tp->intf);
4207 break;
4208
4209 case PM_POST_RESTORE:
4210 case PM_RESTORE_PREPARE:
4211 default:
4212 break;
4213 }
4214
4215 return NOTIFY_DONE;
4216}
4217#endif
4218
4219static int rtl8152_open(struct net_device *netdev)
4220{
4221 struct r8152 *tp = netdev_priv(netdev);
4222 int res = 0;
4223
4224 res = alloc_all_mem(tp);
4225 if (res)
4226 goto out;
4227
4228 res = usb_autopm_get_interface(tp->intf);
4229 if (res < 0)
4230 goto out_free;
4231
4232 mutex_lock(&tp->control);
4233
4234 tp->rtl_ops.up(tp);
4235
4236 netif_carrier_off(netdev);
4237 netif_start_queue(netdev);
4238 set_bit(WORK_ENABLE, &tp->flags);
4239
4240 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
4241 if (res) {
4242 if (res == -ENODEV)
4243 netif_device_detach(tp->netdev);
4244 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
4245 res);
4246 goto out_unlock;
4247 }
4248 napi_enable(&tp->napi);
4249 tasklet_enable(&tp->tx_tl);
4250
4251 mutex_unlock(&tp->control);
4252
4253 usb_autopm_put_interface(tp->intf);
4254#ifdef CONFIG_PM_SLEEP
4255 tp->pm_notifier.notifier_call = rtl_notifier;
4256 register_pm_notifier(&tp->pm_notifier);
4257#endif
4258 return 0;
4259
4260out_unlock:
4261 mutex_unlock(&tp->control);
4262 usb_autopm_put_interface(tp->intf);
4263out_free:
4264 free_all_mem(tp);
4265out:
4266 return res;
4267}
4268
4269static int rtl8152_close(struct net_device *netdev)
4270{
4271 struct r8152 *tp = netdev_priv(netdev);
4272 int res = 0;
4273
4274#ifdef CONFIG_PM_SLEEP
4275 unregister_pm_notifier(&tp->pm_notifier);
4276#endif
4277 tasklet_disable(&tp->tx_tl);
4278 clear_bit(WORK_ENABLE, &tp->flags);
4279 usb_kill_urb(tp->intr_urb);
4280 cancel_delayed_work_sync(&tp->schedule);
4281 napi_disable(&tp->napi);
4282 netif_stop_queue(netdev);
4283
4284 res = usb_autopm_get_interface(tp->intf);
4285 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
4286 rtl_drop_queued_tx(tp);
4287 rtl_stop_rx(tp);
4288 } else {
4289 mutex_lock(&tp->control);
4290
4291 tp->rtl_ops.down(tp);
4292
4293 mutex_unlock(&tp->control);
4294 }
4295
4296 if (!res)
4297 usb_autopm_put_interface(tp->intf);
4298
4299 free_all_mem(tp);
4300
4301 return res;
4302}
4303
4304static void rtl_tally_reset(struct r8152 *tp)
4305{
4306 u32 ocp_data;
4307
4308 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
4309 ocp_data |= TALLY_RESET;
4310 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
4311}
4312
4313static void r8152b_init(struct r8152 *tp)
4314{
4315 u32 ocp_data;
4316 u16 data;
4317
4318 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4319 return;
4320
4321 data = r8152_mdio_read(tp, MII_BMCR);
4322 if (data & BMCR_PDOWN) {
4323 data &= ~BMCR_PDOWN;
4324 r8152_mdio_write(tp, MII_BMCR, data);
4325 }
4326
4327 r8152_aldps_en(tp, false);
4328
4329 if (tp->version == RTL_VER_01) {
4330 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4331 ocp_data &= ~LED_MODE_MASK;
4332 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4333 }
4334
4335 r8152_power_cut_en(tp, false);
4336
4337 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4338 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
4339 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4340 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
4341 ocp_data &= ~MCU_CLK_RATIO_MASK;
4342 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
4343 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
4344 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
4345 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
4346 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
4347
4348 rtl_tally_reset(tp);
4349
4350 /* enable rx aggregation */
4351 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4352 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4353 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4354}
4355
4356static void r8153_init(struct r8152 *tp)
4357{
4358 u32 ocp_data;
4359 u16 data;
4360 int i;
4361
4362 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4363 return;
4364
4365 r8153_u1u2en(tp, false);
4366
4367 for (i = 0; i < 500; i++) {
4368 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4369 AUTOLOAD_DONE)
4370 break;
4371
4372 msleep(20);
4373 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4374 break;
4375 }
4376
4377 data = r8153_phy_status(tp, 0);
4378
4379 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
4380 tp->version == RTL_VER_05)
4381 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
4382
4383 data = r8152_mdio_read(tp, MII_BMCR);
4384 if (data & BMCR_PDOWN) {
4385 data &= ~BMCR_PDOWN;
4386 r8152_mdio_write(tp, MII_BMCR, data);
4387 }
4388
4389 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4390
4391 r8153_u2p3en(tp, false);
4392
4393 if (tp->version == RTL_VER_04) {
4394 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
4395 ocp_data &= ~pwd_dn_scale_mask;
4396 ocp_data |= pwd_dn_scale(96);
4397 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
4398
4399 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4400 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4401 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4402 } else if (tp->version == RTL_VER_05) {
4403 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
4404 ocp_data &= ~ECM_ALDPS;
4405 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
4406
4407 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4408 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4409 ocp_data &= ~DYNAMIC_BURST;
4410 else
4411 ocp_data |= DYNAMIC_BURST;
4412 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
4413 } else if (tp->version == RTL_VER_06) {
4414 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4415 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4416 ocp_data &= ~DYNAMIC_BURST;
4417 else
4418 ocp_data |= DYNAMIC_BURST;
4419 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
4420 }
4421
4422 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
4423 ocp_data |= EP4_FULL_FC;
4424 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
4425
4426 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
4427 ocp_data &= ~TIMER11_EN;
4428 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
4429
4430 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4431 ocp_data &= ~LED_MODE_MASK;
4432 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4433
4434 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
4435 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
4436 ocp_data |= LPM_TIMER_500MS;
4437 else
4438 ocp_data |= LPM_TIMER_500US;
4439 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
4440
4441 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
4442 ocp_data &= ~SEN_VAL_MASK;
4443 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
4444 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
4445
4446 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
4447
4448 /* MAC clock speed down */
4449 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
4450 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
4451 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
4452 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
4453
4454 r8153_power_cut_en(tp, false);
4455 r8153_u1u2en(tp, true);
4456 usb_enable_lpm(tp->udev);
4457
4458 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
4459 ocp_data |= LANWAKE_CLR_EN;
4460 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
4461
4462 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
4463 ocp_data &= ~LANWAKE_PIN;
4464 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
4465
4466 /* rx aggregation */
4467 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4468 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4469 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
4470 ocp_data |= RX_AGG_DISABLE;
4471
4472 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4473
4474 rtl_tally_reset(tp);
4475
4476 switch (tp->udev->speed) {
4477 case USB_SPEED_SUPER:
4478 case USB_SPEED_SUPER_PLUS:
4479 tp->coalesce = COALESCE_SUPER;
4480 break;
4481 case USB_SPEED_HIGH:
4482 tp->coalesce = COALESCE_HIGH;
4483 break;
4484 default:
4485 tp->coalesce = COALESCE_SLOW;
4486 break;
4487 }
4488}
4489
4490static void r8153b_init(struct r8152 *tp)
4491{
4492 u32 ocp_data;
4493 u16 data;
4494 int i;
4495
4496 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4497 return;
4498
4499 r8153b_u1u2en(tp, false);
4500
4501 for (i = 0; i < 500; i++) {
4502 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4503 AUTOLOAD_DONE)
4504 break;
4505
4506 msleep(20);
4507 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4508 break;
4509 }
4510
4511 data = r8153_phy_status(tp, 0);
4512
4513 data = r8152_mdio_read(tp, MII_BMCR);
4514 if (data & BMCR_PDOWN) {
4515 data &= ~BMCR_PDOWN;
4516 r8152_mdio_write(tp, MII_BMCR, data);
4517 }
4518
4519 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4520
4521 r8153_u2p3en(tp, false);
4522
4523 /* MSC timer = 0xfff * 8ms = 32760 ms */
4524 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
4525
4526 /* U1/U2/L1 idle timer. 500 us */
4527 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
4528
4529 r8153b_power_cut_en(tp, false);
4530 r8153b_ups_en(tp, false);
4531 r8153_queue_wake(tp, false);
4532 rtl_runtime_suspend_enable(tp, false);
4533 r8153b_u1u2en(tp, true);
4534 usb_enable_lpm(tp->udev);
4535
4536 /* MAC clock speed down */
4537 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
4538 ocp_data |= MAC_CLK_SPDWN_EN;
4539 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
4540
4541 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
4542 ocp_data &= ~PLA_MCU_SPDWN_EN;
4543 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
4544
4545 if (tp->version == RTL_VER_09) {
4546 /* Disable Test IO for 32QFN */
4547 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) {
4548 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4549 ocp_data |= TEST_IO_OFF;
4550 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4551 }
4552 }
4553
4554 set_bit(GREEN_ETHERNET, &tp->flags);
4555
4556 /* rx aggregation */
4557 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4558 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4559 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4560
4561 rtl_tally_reset(tp);
4562
4563 tp->coalesce = 15000; /* 15 us */
4564}
4565
4566static int rtl8152_pre_reset(struct usb_interface *intf)
4567{
4568 struct r8152 *tp = usb_get_intfdata(intf);
4569 struct net_device *netdev;
4570
4571 if (!tp)
4572 return 0;
4573
4574 netdev = tp->netdev;
4575 if (!netif_running(netdev))
4576 return 0;
4577
4578 netif_stop_queue(netdev);
4579 tasklet_disable(&tp->tx_tl);
4580 clear_bit(WORK_ENABLE, &tp->flags);
4581 usb_kill_urb(tp->intr_urb);
4582 cancel_delayed_work_sync(&tp->schedule);
4583 napi_disable(&tp->napi);
4584 if (netif_carrier_ok(netdev)) {
4585 mutex_lock(&tp->control);
4586 tp->rtl_ops.disable(tp);
4587 mutex_unlock(&tp->control);
4588 }
4589
4590 return 0;
4591}
4592
4593static int rtl8152_post_reset(struct usb_interface *intf)
4594{
4595 struct r8152 *tp = usb_get_intfdata(intf);
4596 struct net_device *netdev;
4597 struct sockaddr sa;
4598
4599 if (!tp)
4600 return 0;
4601
4602 /* reset the MAC adddress in case of policy change */
4603 if (determine_ethernet_addr(tp, &sa) >= 0) {
4604 rtnl_lock();
4605 dev_set_mac_address (tp->netdev, &sa, NULL);
4606 rtnl_unlock();
4607 }
4608
4609 netdev = tp->netdev;
4610 if (!netif_running(netdev))
4611 return 0;
4612
4613 set_bit(WORK_ENABLE, &tp->flags);
4614 if (netif_carrier_ok(netdev)) {
4615 mutex_lock(&tp->control);
4616 tp->rtl_ops.enable(tp);
4617 rtl_start_rx(tp);
4618 _rtl8152_set_rx_mode(netdev);
4619 mutex_unlock(&tp->control);
4620 }
4621
4622 napi_enable(&tp->napi);
4623 tasklet_enable(&tp->tx_tl);
4624 netif_wake_queue(netdev);
4625 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
4626
4627 if (!list_empty(&tp->rx_done))
4628 napi_schedule(&tp->napi);
4629
4630 return 0;
4631}
4632
4633static bool delay_autosuspend(struct r8152 *tp)
4634{
4635 bool sw_linking = !!netif_carrier_ok(tp->netdev);
4636 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
4637
4638 /* This means a linking change occurs and the driver doesn't detect it,
4639 * yet. If the driver has disabled tx/rx and hw is linking on, the
4640 * device wouldn't wake up by receiving any packet.
4641 */
4642 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
4643 return true;
4644
4645 /* If the linking down is occurred by nway, the device may miss the
4646 * linking change event. And it wouldn't wake when linking on.
4647 */
4648 if (!sw_linking && tp->rtl_ops.in_nway(tp))
4649 return true;
4650 else if (!skb_queue_empty(&tp->tx_queue))
4651 return true;
4652 else
4653 return false;
4654}
4655
4656static int rtl8152_runtime_resume(struct r8152 *tp)
4657{
4658 struct net_device *netdev = tp->netdev;
4659
4660 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4661 struct napi_struct *napi = &tp->napi;
4662
4663 tp->rtl_ops.autosuspend_en(tp, false);
4664 napi_disable(napi);
4665 set_bit(WORK_ENABLE, &tp->flags);
4666
4667 if (netif_carrier_ok(netdev)) {
4668 if (rtl8152_get_speed(tp) & LINK_STATUS) {
4669 rtl_start_rx(tp);
4670 } else {
4671 netif_carrier_off(netdev);
4672 tp->rtl_ops.disable(tp);
4673 netif_info(tp, link, netdev, "linking down\n");
4674 }
4675 }
4676
4677 napi_enable(napi);
4678 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4679 smp_mb__after_atomic();
4680
4681 if (!list_empty(&tp->rx_done))
4682 napi_schedule(&tp->napi);
4683
4684 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4685 } else {
4686 if (netdev->flags & IFF_UP)
4687 tp->rtl_ops.autosuspend_en(tp, false);
4688
4689 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4690 }
4691
4692 return 0;
4693}
4694
4695static int rtl8152_system_resume(struct r8152 *tp)
4696{
4697 struct net_device *netdev = tp->netdev;
4698
4699 netif_device_attach(netdev);
4700
4701 if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
4702 tp->rtl_ops.up(tp);
4703 netif_carrier_off(netdev);
4704 set_bit(WORK_ENABLE, &tp->flags);
4705 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4706 }
4707
4708 return 0;
4709}
4710
4711static int rtl8152_runtime_suspend(struct r8152 *tp)
4712{
4713 struct net_device *netdev = tp->netdev;
4714 int ret = 0;
4715
4716 set_bit(SELECTIVE_SUSPEND, &tp->flags);
4717 smp_mb__after_atomic();
4718
4719 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
4720 u32 rcr = 0;
4721
4722 if (netif_carrier_ok(netdev)) {
4723 u32 ocp_data;
4724
4725 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4726 ocp_data = rcr & ~RCR_ACPT_ALL;
4727 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4728 rxdy_gated_en(tp, true);
4729 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
4730 PLA_OOB_CTRL);
4731 if (!(ocp_data & RXFIFO_EMPTY)) {
4732 rxdy_gated_en(tp, false);
4733 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
4734 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4735 smp_mb__after_atomic();
4736 ret = -EBUSY;
4737 goto out1;
4738 }
4739 }
4740
4741 clear_bit(WORK_ENABLE, &tp->flags);
4742 usb_kill_urb(tp->intr_urb);
4743
4744 tp->rtl_ops.autosuspend_en(tp, true);
4745
4746 if (netif_carrier_ok(netdev)) {
4747 struct napi_struct *napi = &tp->napi;
4748
4749 napi_disable(napi);
4750 rtl_stop_rx(tp);
4751 rxdy_gated_en(tp, false);
4752 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
4753 napi_enable(napi);
4754 }
4755
4756 if (delay_autosuspend(tp)) {
4757 rtl8152_runtime_resume(tp);
4758 ret = -EBUSY;
4759 }
4760 }
4761
4762out1:
4763 return ret;
4764}
4765
4766static int rtl8152_system_suspend(struct r8152 *tp)
4767{
4768 struct net_device *netdev = tp->netdev;
4769
4770 netif_device_detach(netdev);
4771
4772 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
4773 struct napi_struct *napi = &tp->napi;
4774
4775 clear_bit(WORK_ENABLE, &tp->flags);
4776 usb_kill_urb(tp->intr_urb);
4777 tasklet_disable(&tp->tx_tl);
4778 napi_disable(napi);
4779 cancel_delayed_work_sync(&tp->schedule);
4780 tp->rtl_ops.down(tp);
4781 napi_enable(napi);
4782 tasklet_enable(&tp->tx_tl);
4783 }
4784
4785 return 0;
4786}
4787
4788static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
4789{
4790 struct r8152 *tp = usb_get_intfdata(intf);
4791 int ret;
4792
4793 mutex_lock(&tp->control);
4794
4795 if (PMSG_IS_AUTO(message))
4796 ret = rtl8152_runtime_suspend(tp);
4797 else
4798 ret = rtl8152_system_suspend(tp);
4799
4800 mutex_unlock(&tp->control);
4801
4802 return ret;
4803}
4804
4805static int rtl8152_resume(struct usb_interface *intf)
4806{
4807 struct r8152 *tp = usb_get_intfdata(intf);
4808 int ret;
4809
4810 mutex_lock(&tp->control);
4811
4812 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
4813 ret = rtl8152_runtime_resume(tp);
4814 else
4815 ret = rtl8152_system_resume(tp);
4816
4817 mutex_unlock(&tp->control);
4818
4819 return ret;
4820}
4821
4822static int rtl8152_reset_resume(struct usb_interface *intf)
4823{
4824 struct r8152 *tp = usb_get_intfdata(intf);
4825
4826 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4827 tp->rtl_ops.init(tp);
4828 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
4829 set_ethernet_addr(tp);
4830 return rtl8152_resume(intf);
4831}
4832
4833static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4834{
4835 struct r8152 *tp = netdev_priv(dev);
4836
4837 if (usb_autopm_get_interface(tp->intf) < 0)
4838 return;
4839
4840 if (!rtl_can_wakeup(tp)) {
4841 wol->supported = 0;
4842 wol->wolopts = 0;
4843 } else {
4844 mutex_lock(&tp->control);
4845 wol->supported = WAKE_ANY;
4846 wol->wolopts = __rtl_get_wol(tp);
4847 mutex_unlock(&tp->control);
4848 }
4849
4850 usb_autopm_put_interface(tp->intf);
4851}
4852
4853static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4854{
4855 struct r8152 *tp = netdev_priv(dev);
4856 int ret;
4857
4858 if (!rtl_can_wakeup(tp))
4859 return -EOPNOTSUPP;
4860
4861 if (wol->wolopts & ~WAKE_ANY)
4862 return -EINVAL;
4863
4864 ret = usb_autopm_get_interface(tp->intf);
4865 if (ret < 0)
4866 goto out_set_wol;
4867
4868 mutex_lock(&tp->control);
4869
4870 __rtl_set_wol(tp, wol->wolopts);
4871 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
4872
4873 mutex_unlock(&tp->control);
4874
4875 usb_autopm_put_interface(tp->intf);
4876
4877out_set_wol:
4878 return ret;
4879}
4880
4881static u32 rtl8152_get_msglevel(struct net_device *dev)
4882{
4883 struct r8152 *tp = netdev_priv(dev);
4884
4885 return tp->msg_enable;
4886}
4887
4888static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
4889{
4890 struct r8152 *tp = netdev_priv(dev);
4891
4892 tp->msg_enable = value;
4893}
4894
4895static void rtl8152_get_drvinfo(struct net_device *netdev,
4896 struct ethtool_drvinfo *info)
4897{
4898 struct r8152 *tp = netdev_priv(netdev);
4899
4900 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
4901 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
4902 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
4903}
4904
4905static
4906int rtl8152_get_link_ksettings(struct net_device *netdev,
4907 struct ethtool_link_ksettings *cmd)
4908{
4909 struct r8152 *tp = netdev_priv(netdev);
4910 int ret;
4911
4912 if (!tp->mii.mdio_read)
4913 return -EOPNOTSUPP;
4914
4915 ret = usb_autopm_get_interface(tp->intf);
4916 if (ret < 0)
4917 goto out;
4918
4919 mutex_lock(&tp->control);
4920
4921 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
4922
4923 mutex_unlock(&tp->control);
4924
4925 usb_autopm_put_interface(tp->intf);
4926
4927out:
4928 return ret;
4929}
4930
4931static int rtl8152_set_link_ksettings(struct net_device *dev,
4932 const struct ethtool_link_ksettings *cmd)
4933{
4934 struct r8152 *tp = netdev_priv(dev);
4935 u32 advertising = 0;
4936 int ret;
4937
4938 ret = usb_autopm_get_interface(tp->intf);
4939 if (ret < 0)
4940 goto out;
4941
4942 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
4943 cmd->link_modes.advertising))
4944 advertising |= RTL_ADVERTISED_10_HALF;
4945
4946 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
4947 cmd->link_modes.advertising))
4948 advertising |= RTL_ADVERTISED_10_FULL;
4949
4950 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
4951 cmd->link_modes.advertising))
4952 advertising |= RTL_ADVERTISED_100_HALF;
4953
4954 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
4955 cmd->link_modes.advertising))
4956 advertising |= RTL_ADVERTISED_100_FULL;
4957
4958 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
4959 cmd->link_modes.advertising))
4960 advertising |= RTL_ADVERTISED_1000_HALF;
4961
4962 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
4963 cmd->link_modes.advertising))
4964 advertising |= RTL_ADVERTISED_1000_FULL;
4965
4966 mutex_lock(&tp->control);
4967
4968 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
4969 cmd->base.duplex, advertising);
4970 if (!ret) {
4971 tp->autoneg = cmd->base.autoneg;
4972 tp->speed = cmd->base.speed;
4973 tp->duplex = cmd->base.duplex;
4974 tp->advertising = advertising;
4975 }
4976
4977 mutex_unlock(&tp->control);
4978
4979 usb_autopm_put_interface(tp->intf);
4980
4981out:
4982 return ret;
4983}
4984
4985static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
4986 "tx_packets",
4987 "rx_packets",
4988 "tx_errors",
4989 "rx_errors",
4990 "rx_missed",
4991 "align_errors",
4992 "tx_single_collisions",
4993 "tx_multi_collisions",
4994 "rx_unicast",
4995 "rx_broadcast",
4996 "rx_multicast",
4997 "tx_aborted",
4998 "tx_underrun",
4999};
5000
5001static int rtl8152_get_sset_count(struct net_device *dev, int sset)
5002{
5003 switch (sset) {
5004 case ETH_SS_STATS:
5005 return ARRAY_SIZE(rtl8152_gstrings);
5006 default:
5007 return -EOPNOTSUPP;
5008 }
5009}
5010
5011static void rtl8152_get_ethtool_stats(struct net_device *dev,
5012 struct ethtool_stats *stats, u64 *data)
5013{
5014 struct r8152 *tp = netdev_priv(dev);
5015 struct tally_counter tally;
5016
5017 if (usb_autopm_get_interface(tp->intf) < 0)
5018 return;
5019
5020 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
5021
5022 usb_autopm_put_interface(tp->intf);
5023
5024 data[0] = le64_to_cpu(tally.tx_packets);
5025 data[1] = le64_to_cpu(tally.rx_packets);
5026 data[2] = le64_to_cpu(tally.tx_errors);
5027 data[3] = le32_to_cpu(tally.rx_errors);
5028 data[4] = le16_to_cpu(tally.rx_missed);
5029 data[5] = le16_to_cpu(tally.align_errors);
5030 data[6] = le32_to_cpu(tally.tx_one_collision);
5031 data[7] = le32_to_cpu(tally.tx_multi_collision);
5032 data[8] = le64_to_cpu(tally.rx_unicast);
5033 data[9] = le64_to_cpu(tally.rx_broadcast);
5034 data[10] = le32_to_cpu(tally.rx_multicast);
5035 data[11] = le16_to_cpu(tally.tx_aborted);
5036 data[12] = le16_to_cpu(tally.tx_underrun);
5037}
5038
5039static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
5040{
5041 switch (stringset) {
5042 case ETH_SS_STATS:
5043 memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings));
5044 break;
5045 }
5046}
5047
5048static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
5049{
5050 u32 lp, adv, supported = 0;
5051 u16 val;
5052
5053 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
5054 supported = mmd_eee_cap_to_ethtool_sup_t(val);
5055
5056 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
5057 adv = mmd_eee_adv_to_ethtool_adv_t(val);
5058
5059 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
5060 lp = mmd_eee_adv_to_ethtool_adv_t(val);
5061
5062 eee->eee_enabled = tp->eee_en;
5063 eee->eee_active = !!(supported & adv & lp);
5064 eee->supported = supported;
5065 eee->advertised = tp->eee_adv;
5066 eee->lp_advertised = lp;
5067
5068 return 0;
5069}
5070
5071static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
5072{
5073 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
5074
5075 tp->eee_en = eee->eee_enabled;
5076 tp->eee_adv = val;
5077
5078 rtl_eee_enable(tp, tp->eee_en);
5079
5080 return 0;
5081}
5082
5083static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
5084{
5085 u32 lp, adv, supported = 0;
5086 u16 val;
5087
5088 val = ocp_reg_read(tp, OCP_EEE_ABLE);
5089 supported = mmd_eee_cap_to_ethtool_sup_t(val);
5090
5091 val = ocp_reg_read(tp, OCP_EEE_ADV);
5092 adv = mmd_eee_adv_to_ethtool_adv_t(val);
5093
5094 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
5095 lp = mmd_eee_adv_to_ethtool_adv_t(val);
5096
5097 eee->eee_enabled = tp->eee_en;
5098 eee->eee_active = !!(supported & adv & lp);
5099 eee->supported = supported;
5100 eee->advertised = tp->eee_adv;
5101 eee->lp_advertised = lp;
5102
5103 return 0;
5104}
5105
5106static int
5107rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
5108{
5109 struct r8152 *tp = netdev_priv(net);
5110 int ret;
5111
5112 ret = usb_autopm_get_interface(tp->intf);
5113 if (ret < 0)
5114 goto out;
5115
5116 mutex_lock(&tp->control);
5117
5118 ret = tp->rtl_ops.eee_get(tp, edata);
5119
5120 mutex_unlock(&tp->control);
5121
5122 usb_autopm_put_interface(tp->intf);
5123
5124out:
5125 return ret;
5126}
5127
5128static int
5129rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
5130{
5131 struct r8152 *tp = netdev_priv(net);
5132 int ret;
5133
5134 ret = usb_autopm_get_interface(tp->intf);
5135 if (ret < 0)
5136 goto out;
5137
5138 mutex_lock(&tp->control);
5139
5140 ret = tp->rtl_ops.eee_set(tp, edata);
5141 if (!ret)
5142 ret = mii_nway_restart(&tp->mii);
5143
5144 mutex_unlock(&tp->control);
5145
5146 usb_autopm_put_interface(tp->intf);
5147
5148out:
5149 return ret;
5150}
5151
5152static int rtl8152_nway_reset(struct net_device *dev)
5153{
5154 struct r8152 *tp = netdev_priv(dev);
5155 int ret;
5156
5157 ret = usb_autopm_get_interface(tp->intf);
5158 if (ret < 0)
5159 goto out;
5160
5161 mutex_lock(&tp->control);
5162
5163 ret = mii_nway_restart(&tp->mii);
5164
5165 mutex_unlock(&tp->control);
5166
5167 usb_autopm_put_interface(tp->intf);
5168
5169out:
5170 return ret;
5171}
5172
5173static int rtl8152_get_coalesce(struct net_device *netdev,
5174 struct ethtool_coalesce *coalesce)
5175{
5176 struct r8152 *tp = netdev_priv(netdev);
5177
5178 switch (tp->version) {
5179 case RTL_VER_01:
5180 case RTL_VER_02:
5181 case RTL_VER_07:
5182 return -EOPNOTSUPP;
5183 default:
5184 break;
5185 }
5186
5187 coalesce->rx_coalesce_usecs = tp->coalesce;
5188
5189 return 0;
5190}
5191
5192static int rtl8152_set_coalesce(struct net_device *netdev,
5193 struct ethtool_coalesce *coalesce)
5194{
5195 struct r8152 *tp = netdev_priv(netdev);
5196 int ret;
5197
5198 switch (tp->version) {
5199 case RTL_VER_01:
5200 case RTL_VER_02:
5201 case RTL_VER_07:
5202 return -EOPNOTSUPP;
5203 default:
5204 break;
5205 }
5206
5207 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
5208 return -EINVAL;
5209
5210 ret = usb_autopm_get_interface(tp->intf);
5211 if (ret < 0)
5212 return ret;
5213
5214 mutex_lock(&tp->control);
5215
5216 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
5217 tp->coalesce = coalesce->rx_coalesce_usecs;
5218
5219 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
5220 netif_stop_queue(netdev);
5221 napi_disable(&tp->napi);
5222 tp->rtl_ops.disable(tp);
5223 tp->rtl_ops.enable(tp);
5224 rtl_start_rx(tp);
5225 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
5226 _rtl8152_set_rx_mode(netdev);
5227 napi_enable(&tp->napi);
5228 netif_wake_queue(netdev);
5229 }
5230 }
5231
5232 mutex_unlock(&tp->control);
5233
5234 usb_autopm_put_interface(tp->intf);
5235
5236 return ret;
5237}
5238
5239static int rtl8152_get_tunable(struct net_device *netdev,
5240 const struct ethtool_tunable *tunable, void *d)
5241{
5242 struct r8152 *tp = netdev_priv(netdev);
5243
5244 switch (tunable->id) {
5245 case ETHTOOL_RX_COPYBREAK:
5246 *(u32 *)d = tp->rx_copybreak;
5247 break;
5248 default:
5249 return -EOPNOTSUPP;
5250 }
5251
5252 return 0;
5253}
5254
5255static int rtl8152_set_tunable(struct net_device *netdev,
5256 const struct ethtool_tunable *tunable,
5257 const void *d)
5258{
5259 struct r8152 *tp = netdev_priv(netdev);
5260 u32 val;
5261
5262 switch (tunable->id) {
5263 case ETHTOOL_RX_COPYBREAK:
5264 val = *(u32 *)d;
5265 if (val < ETH_ZLEN) {
5266 netif_err(tp, rx_err, netdev,
5267 "Invalid rx copy break value\n");
5268 return -EINVAL;
5269 }
5270
5271 if (tp->rx_copybreak != val) {
5272 if (netdev->flags & IFF_UP) {
5273 mutex_lock(&tp->control);
5274 napi_disable(&tp->napi);
5275 tp->rx_copybreak = val;
5276 napi_enable(&tp->napi);
5277 mutex_unlock(&tp->control);
5278 } else {
5279 tp->rx_copybreak = val;
5280 }
5281 }
5282 break;
5283 default:
5284 return -EOPNOTSUPP;
5285 }
5286
5287 return 0;
5288}
5289
5290static void rtl8152_get_ringparam(struct net_device *netdev,
5291 struct ethtool_ringparam *ring)
5292{
5293 struct r8152 *tp = netdev_priv(netdev);
5294
5295 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
5296 ring->rx_pending = tp->rx_pending;
5297}
5298
5299static int rtl8152_set_ringparam(struct net_device *netdev,
5300 struct ethtool_ringparam *ring)
5301{
5302 struct r8152 *tp = netdev_priv(netdev);
5303
5304 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
5305 return -EINVAL;
5306
5307 if (tp->rx_pending != ring->rx_pending) {
5308 if (netdev->flags & IFF_UP) {
5309 mutex_lock(&tp->control);
5310 napi_disable(&tp->napi);
5311 tp->rx_pending = ring->rx_pending;
5312 napi_enable(&tp->napi);
5313 mutex_unlock(&tp->control);
5314 } else {
5315 tp->rx_pending = ring->rx_pending;
5316 }
5317 }
5318
5319 return 0;
5320}
5321
5322static const struct ethtool_ops ops = {
5323 .get_drvinfo = rtl8152_get_drvinfo,
5324 .get_link = ethtool_op_get_link,
5325 .nway_reset = rtl8152_nway_reset,
5326 .get_msglevel = rtl8152_get_msglevel,
5327 .set_msglevel = rtl8152_set_msglevel,
5328 .get_wol = rtl8152_get_wol,
5329 .set_wol = rtl8152_set_wol,
5330 .get_strings = rtl8152_get_strings,
5331 .get_sset_count = rtl8152_get_sset_count,
5332 .get_ethtool_stats = rtl8152_get_ethtool_stats,
5333 .get_coalesce = rtl8152_get_coalesce,
5334 .set_coalesce = rtl8152_set_coalesce,
5335 .get_eee = rtl_ethtool_get_eee,
5336 .set_eee = rtl_ethtool_set_eee,
5337 .get_link_ksettings = rtl8152_get_link_ksettings,
5338 .set_link_ksettings = rtl8152_set_link_ksettings,
5339 .get_tunable = rtl8152_get_tunable,
5340 .set_tunable = rtl8152_set_tunable,
5341 .get_ringparam = rtl8152_get_ringparam,
5342 .set_ringparam = rtl8152_set_ringparam,
5343};
5344
5345static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
5346{
5347 struct r8152 *tp = netdev_priv(netdev);
5348 struct mii_ioctl_data *data = if_mii(rq);
5349 int res;
5350
5351 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5352 return -ENODEV;
5353
5354 res = usb_autopm_get_interface(tp->intf);
5355 if (res < 0)
5356 goto out;
5357
5358 switch (cmd) {
5359 case SIOCGMIIPHY:
5360 data->phy_id = R8152_PHY_ID; /* Internal PHY */
5361 break;
5362
5363 case SIOCGMIIREG:
5364 mutex_lock(&tp->control);
5365 data->val_out = r8152_mdio_read(tp, data->reg_num);
5366 mutex_unlock(&tp->control);
5367 break;
5368
5369 case SIOCSMIIREG:
5370 if (!capable(CAP_NET_ADMIN)) {
5371 res = -EPERM;
5372 break;
5373 }
5374 mutex_lock(&tp->control);
5375 r8152_mdio_write(tp, data->reg_num, data->val_in);
5376 mutex_unlock(&tp->control);
5377 break;
5378
5379 default:
5380 res = -EOPNOTSUPP;
5381 }
5382
5383 usb_autopm_put_interface(tp->intf);
5384
5385out:
5386 return res;
5387}
5388
5389static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
5390{
5391 struct r8152 *tp = netdev_priv(dev);
5392 int ret;
5393
5394 switch (tp->version) {
5395 case RTL_VER_01:
5396 case RTL_VER_02:
5397 case RTL_VER_07:
5398 dev->mtu = new_mtu;
5399 return 0;
5400 default:
5401 break;
5402 }
5403
5404 ret = usb_autopm_get_interface(tp->intf);
5405 if (ret < 0)
5406 return ret;
5407
5408 mutex_lock(&tp->control);
5409
5410 dev->mtu = new_mtu;
5411
5412 if (netif_running(dev)) {
5413 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
5414
5415 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
5416
5417 if (netif_carrier_ok(dev))
5418 r8153_set_rx_early_size(tp);
5419 }
5420
5421 mutex_unlock(&tp->control);
5422
5423 usb_autopm_put_interface(tp->intf);
5424
5425 return ret;
5426}
5427
5428static const struct net_device_ops rtl8152_netdev_ops = {
5429 .ndo_open = rtl8152_open,
5430 .ndo_stop = rtl8152_close,
5431 .ndo_do_ioctl = rtl8152_ioctl,
5432 .ndo_start_xmit = rtl8152_start_xmit,
5433 .ndo_tx_timeout = rtl8152_tx_timeout,
5434 .ndo_set_features = rtl8152_set_features,
5435 .ndo_set_rx_mode = rtl8152_set_rx_mode,
5436 .ndo_set_mac_address = rtl8152_set_mac_address,
5437 .ndo_change_mtu = rtl8152_change_mtu,
5438 .ndo_validate_addr = eth_validate_addr,
5439 .ndo_features_check = rtl8152_features_check,
5440};
5441
5442static void rtl8152_unload(struct r8152 *tp)
5443{
5444 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5445 return;
5446
5447 if (tp->version != RTL_VER_01)
5448 r8152_power_cut_en(tp, true);
5449}
5450
5451static void rtl8153_unload(struct r8152 *tp)
5452{
5453 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5454 return;
5455
5456 r8153_power_cut_en(tp, false);
5457}
5458
5459static void rtl8153b_unload(struct r8152 *tp)
5460{
5461 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5462 return;
5463
5464 r8153b_power_cut_en(tp, false);
5465}
5466
5467static int rtl_ops_init(struct r8152 *tp)
5468{
5469 struct rtl_ops *ops = &tp->rtl_ops;
5470 int ret = 0;
5471
5472 switch (tp->version) {
5473 case RTL_VER_01:
5474 case RTL_VER_02:
5475 case RTL_VER_07:
5476 ops->init = r8152b_init;
5477 ops->enable = rtl8152_enable;
5478 ops->disable = rtl8152_disable;
5479 ops->up = rtl8152_up;
5480 ops->down = rtl8152_down;
5481 ops->unload = rtl8152_unload;
5482 ops->eee_get = r8152_get_eee;
5483 ops->eee_set = r8152_set_eee;
5484 ops->in_nway = rtl8152_in_nway;
5485 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
5486 ops->autosuspend_en = rtl_runtime_suspend_enable;
5487 tp->rx_buf_sz = 16 * 1024;
5488 tp->eee_en = true;
5489 tp->eee_adv = MDIO_EEE_100TX;
5490 break;
5491
5492 case RTL_VER_03:
5493 case RTL_VER_04:
5494 case RTL_VER_05:
5495 case RTL_VER_06:
5496 ops->init = r8153_init;
5497 ops->enable = rtl8153_enable;
5498 ops->disable = rtl8153_disable;
5499 ops->up = rtl8153_up;
5500 ops->down = rtl8153_down;
5501 ops->unload = rtl8153_unload;
5502 ops->eee_get = r8153_get_eee;
5503 ops->eee_set = r8152_set_eee;
5504 ops->in_nway = rtl8153_in_nway;
5505 ops->hw_phy_cfg = r8153_hw_phy_cfg;
5506 ops->autosuspend_en = rtl8153_runtime_enable;
5507 if (tp->udev->speed < USB_SPEED_SUPER)
5508 tp->rx_buf_sz = 16 * 1024;
5509 else
5510 tp->rx_buf_sz = 32 * 1024;
5511 tp->eee_en = true;
5512 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
5513 break;
5514
5515 case RTL_VER_08:
5516 case RTL_VER_09:
5517 ops->init = r8153b_init;
5518 ops->enable = rtl8153_enable;
5519 ops->disable = rtl8153_disable;
5520 ops->up = rtl8153b_up;
5521 ops->down = rtl8153b_down;
5522 ops->unload = rtl8153b_unload;
5523 ops->eee_get = r8153_get_eee;
5524 ops->eee_set = r8152_set_eee;
5525 ops->in_nway = rtl8153_in_nway;
5526 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
5527 ops->autosuspend_en = rtl8153b_runtime_enable;
5528 tp->rx_buf_sz = 32 * 1024;
5529 tp->eee_en = true;
5530 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
5531 break;
5532
5533 default:
5534 ret = -ENODEV;
5535 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
5536 break;
5537 }
5538
5539 return ret;
5540}
5541
5542static u8 rtl_get_version(struct usb_interface *intf)
5543{
5544 struct usb_device *udev = interface_to_usbdev(intf);
5545 u32 ocp_data = 0;
5546 __le32 *tmp;
5547 u8 version;
5548 int ret;
5549
5550 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
5551 if (!tmp)
5552 return 0;
5553
5554 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5555 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
5556 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp),
5557 USB_CTRL_GET_TIMEOUT);
5558 if (ret > 0)
5559 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
5560
5561 kfree(tmp);
5562
5563 switch (ocp_data) {
5564 case 0x4c00:
5565 version = RTL_VER_01;
5566 break;
5567 case 0x4c10:
5568 version = RTL_VER_02;
5569 break;
5570 case 0x5c00:
5571 version = RTL_VER_03;
5572 break;
5573 case 0x5c10:
5574 version = RTL_VER_04;
5575 break;
5576 case 0x5c20:
5577 version = RTL_VER_05;
5578 break;
5579 case 0x5c30:
5580 version = RTL_VER_06;
5581 break;
5582 case 0x4800:
5583 version = RTL_VER_07;
5584 break;
5585 case 0x6000:
5586 version = RTL_VER_08;
5587 break;
5588 case 0x6010:
5589 version = RTL_VER_09;
5590 break;
5591 default:
5592 version = RTL_VER_UNKNOWN;
5593 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
5594 break;
5595 }
5596
5597 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
5598
5599 return version;
5600}
5601
5602static int rtl8152_probe(struct usb_interface *intf,
5603 const struct usb_device_id *id)
5604{
5605 struct usb_device *udev = interface_to_usbdev(intf);
5606 u8 version = rtl_get_version(intf);
5607 struct r8152 *tp;
5608 struct net_device *netdev;
5609 int ret;
5610
5611 if (version == RTL_VER_UNKNOWN)
5612 return -ENODEV;
5613
5614 if (udev->actconfig->desc.bConfigurationValue != 1) {
5615 usb_driver_set_configuration(udev, 1);
5616 return -ENODEV;
5617 }
5618
5619 if (intf->cur_altsetting->desc.bNumEndpoints < 3)
5620 return -ENODEV;
5621
5622 usb_reset_device(udev);
5623 netdev = alloc_etherdev(sizeof(struct r8152));
5624 if (!netdev) {
5625 dev_err(&intf->dev, "Out of memory\n");
5626 return -ENOMEM;
5627 }
5628
5629 SET_NETDEV_DEV(netdev, &intf->dev);
5630 tp = netdev_priv(netdev);
5631 tp->msg_enable = 0x7FFF;
5632
5633 tp->udev = udev;
5634 tp->netdev = netdev;
5635 tp->intf = intf;
5636 tp->version = version;
5637
5638 switch (version) {
5639 case RTL_VER_01:
5640 case RTL_VER_02:
5641 case RTL_VER_07:
5642 tp->mii.supports_gmii = 0;
5643 break;
5644 default:
5645 tp->mii.supports_gmii = 1;
5646 break;
5647 }
5648
5649 ret = rtl_ops_init(tp);
5650 if (ret)
5651 goto out;
5652
5653 mutex_init(&tp->control);
5654 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
5655 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
5656 tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp);
5657 tasklet_disable(&tp->tx_tl);
5658
5659 netdev->netdev_ops = &rtl8152_netdev_ops;
5660 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
5661
5662 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
5663 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
5664 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
5665 NETIF_F_HW_VLAN_CTAG_TX;
5666 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
5667 NETIF_F_TSO | NETIF_F_FRAGLIST |
5668 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
5669 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
5670 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
5671 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
5672 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
5673
5674 if (tp->version == RTL_VER_01) {
5675 netdev->features &= ~NETIF_F_RXCSUM;
5676 netdev->hw_features &= ~NETIF_F_RXCSUM;
5677 }
5678
5679 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
5680 (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) {
5681 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
5682 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
5683 }
5684
5685 netdev->ethtool_ops = &ops;
5686 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
5687
5688 /* MTU range: 68 - 1500 or 9194 */
5689 netdev->min_mtu = ETH_MIN_MTU;
5690 switch (tp->version) {
5691 case RTL_VER_01:
5692 case RTL_VER_02:
5693 netdev->max_mtu = ETH_DATA_LEN;
5694 break;
5695 default:
5696 netdev->max_mtu = RTL8153_MAX_MTU;
5697 break;
5698 }
5699
5700 tp->mii.dev = netdev;
5701 tp->mii.mdio_read = read_mii_word;
5702 tp->mii.mdio_write = write_mii_word;
5703 tp->mii.phy_id_mask = 0x3f;
5704 tp->mii.reg_num_mask = 0x1f;
5705 tp->mii.phy_id = R8152_PHY_ID;
5706
5707 tp->autoneg = AUTONEG_ENABLE;
5708 tp->speed = SPEED_100;
5709 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
5710 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
5711 if (tp->mii.supports_gmii) {
5712 tp->speed = SPEED_1000;
5713 tp->advertising |= RTL_ADVERTISED_1000_FULL;
5714 }
5715 tp->duplex = DUPLEX_FULL;
5716
5717 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
5718 tp->rx_pending = 10 * RTL8152_MAX_RX;
5719
5720 intf->needs_remote_wakeup = 1;
5721
5722 if (!rtl_can_wakeup(tp))
5723 __rtl_set_wol(tp, 0);
5724 else
5725 tp->saved_wolopts = __rtl_get_wol(tp);
5726
5727 tp->rtl_ops.init(tp);
5728 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
5729 set_ethernet_addr(tp);
5730
5731 usb_set_intfdata(intf, tp);
5732 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
5733
5734 ret = register_netdev(netdev);
5735 if (ret != 0) {
5736 netif_err(tp, probe, netdev, "couldn't register the device\n");
5737 goto out1;
5738 }
5739
5740 if (tp->saved_wolopts)
5741 device_set_wakeup_enable(&udev->dev, true);
5742 else
5743 device_set_wakeup_enable(&udev->dev, false);
5744
5745 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
5746
5747 return 0;
5748
5749out1:
5750 tasklet_kill(&tp->tx_tl);
5751 cancel_delayed_work_sync(&tp->hw_phy_work);
5752 if (tp->rtl_ops.unload)
5753 tp->rtl_ops.unload(tp);
5754 usb_set_intfdata(intf, NULL);
5755out:
5756 free_netdev(netdev);
5757 return ret;
5758}
5759
5760static void rtl8152_disconnect(struct usb_interface *intf)
5761{
5762 struct r8152 *tp = usb_get_intfdata(intf);
5763
5764 usb_set_intfdata(intf, NULL);
5765 if (tp) {
5766 rtl_set_unplug(tp);
5767
5768 unregister_netdev(tp->netdev);
5769 tasklet_kill(&tp->tx_tl);
5770 cancel_delayed_work_sync(&tp->hw_phy_work);
5771 tp->rtl_ops.unload(tp);
5772 free_netdev(tp->netdev);
5773 }
5774}
5775
5776#define REALTEK_USB_DEVICE(vend, prod) \
5777 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
5778 USB_DEVICE_ID_MATCH_INT_CLASS, \
5779 .idVendor = (vend), \
5780 .idProduct = (prod), \
5781 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
5782}, \
5783{ \
5784 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
5785 USB_DEVICE_ID_MATCH_DEVICE, \
5786 .idVendor = (vend), \
5787 .idProduct = (prod), \
5788 .bInterfaceClass = USB_CLASS_COMM, \
5789 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
5790 .bInterfaceProtocol = USB_CDC_PROTO_NONE
5791
5792/* table of devices that work with this driver */
5793static const struct usb_device_id rtl8152_table[] = {
5794 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
5795 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
5796 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
5797 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
5798 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
5799 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927)},
5800 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
5801 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
5802 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3054)},
5803 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
5804 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
5805 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
5806 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
5807 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
5808 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x721e)},
5809 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387)},
5810 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
5811 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
5812 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
5813 {}
5814};
5815
5816MODULE_DEVICE_TABLE(usb, rtl8152_table);
5817
5818static struct usb_driver rtl8152_driver = {
5819 .name = MODULENAME,
5820 .id_table = rtl8152_table,
5821 .probe = rtl8152_probe,
5822 .disconnect = rtl8152_disconnect,
5823 .suspend = rtl8152_suspend,
5824 .resume = rtl8152_resume,
5825 .reset_resume = rtl8152_reset_resume,
5826 .pre_reset = rtl8152_pre_reset,
5827 .post_reset = rtl8152_post_reset,
5828 .supports_autosuspend = 1,
5829 .disable_hub_initiated_lpm = 1,
5830};
5831
5832module_usb_driver(rtl8152_driver);
5833
5834MODULE_AUTHOR(DRIVER_AUTHOR);
5835MODULE_DESCRIPTION(DRIVER_DESC);
5836MODULE_LICENSE("GPL");
5837MODULE_VERSION(DRIVER_VERSION);