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