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